global.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // This file is part of SDLPAL.
  7. //
  8. // SDLPAL is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
  22. //
  23. #include "main.h"
  24. LPGLOBALVARS gpGlobals = NULL;
  25. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  26. #define DO_BYTESWAP(buf, size)
  27. #else
  28. #define DO_BYTESWAP(buf, size) \
  29. do { \
  30. int i; \
  31. for (i = 0; i < (size) / 2; i++) \
  32. { \
  33. ((LPWORD)(buf))[i] = SWAP16(((LPWORD)(buf))[i]); \
  34. } \
  35. } while(0)
  36. #endif
  37. #define LOAD_DATA(buf, size, chunknum, fp) \
  38. do { \
  39. PAL_MKFReadChunk((LPBYTE)(buf), (size), (chunknum), (fp)); \
  40. DO_BYTESWAP(buf, size); \
  41. } while(0)
  42. INT
  43. PAL_InitGlobals(
  44. VOID
  45. )
  46. /*++
  47. Purpose:
  48. Initialize global data.
  49. Parameters:
  50. [IN] iCodePage - the code page for text conversion.
  51. [IN] dwWordLength - the length of each word.
  52. Return value:
  53. 0 = success, -1 = error.
  54. --*/
  55. {
  56. #ifdef PAL_UNICODE
  57. FILE *fp;
  58. CODEPAGE iCodePage = CP_UNKNOWN;
  59. DWORD dwWordLength = 10; // Default for PAL DOS/WIN95
  60. DWORD dwExtraMagicDescLines = 0; // Default for PAL DOS/WIN95
  61. DWORD dwExtraItemDescLines = 0; // Default for PAL DOS/WIN95
  62. #endif
  63. if (gpGlobals == NULL)
  64. {
  65. gpGlobals = (LPGLOBALVARS)calloc(1, sizeof(GLOBALVARS));
  66. if (gpGlobals == NULL)
  67. {
  68. return -1;
  69. }
  70. }
  71. #ifdef PAL_UNICODE
  72. if (fp = UTIL_OpenFile("sdlpal.cfg"))
  73. {
  74. PAL_LARGE char buf[512];
  75. //
  76. // Load the configuration data
  77. //
  78. while (fgets(buf, 512, fp) != NULL)
  79. {
  80. char *p = buf;
  81. //
  82. // Skip leading spaces
  83. //
  84. while (*p && isspace(*p)) p++;
  85. //
  86. // Skip comments
  87. //
  88. if (*p && *p != '#')
  89. {
  90. char *ptr;
  91. if (ptr = strchr(p, '='))
  92. {
  93. char *end = ptr - 1;
  94. *ptr++ = 0;
  95. //
  96. // Skip tailing & leading spaces
  97. //
  98. while (isspace(*end) && end >= p) *end-- = 0;
  99. if (SDL_strcasecmp(p, "CODEPAGE") == 0)
  100. {
  101. sscanf(ptr, "%d", &iCodePage);
  102. }
  103. else if (SDL_strcasecmp(p, "WORDLENGTH") == 0)
  104. {
  105. sscanf(ptr, "%u", &dwWordLength);
  106. }
  107. else if (SDL_strcasecmp(p, "EXTRAMAGICDESCLINES") == 0)
  108. {
  109. sscanf(ptr, "%u", &dwExtraMagicDescLines);
  110. }
  111. else if (SDL_strcasecmp(p, "EXTRAITEMDESCLINES") == 0)
  112. {
  113. sscanf(ptr, "%u", &dwExtraItemDescLines);
  114. }
  115. }
  116. }
  117. }
  118. UTIL_CloseFile(fp);
  119. }
  120. // Codepage auto detect
  121. if (CP_UNKNOWN == iCodePage)
  122. {
  123. // Try to convert the content of word.dat with different codepages,
  124. // and use the codepage with minimal inconvertible characters
  125. // Works fine currently with SC/TC/JP.
  126. if (fp = UTIL_OpenFile("word.dat"))
  127. {
  128. char *buf;
  129. long len;
  130. int i, j, c, m = INT_MAX, m_i;
  131. fseek(fp, 0, SEEK_END);
  132. len = ftell(fp);
  133. buf = (char *)malloc(len);
  134. fseek(fp, 0, SEEK_SET);
  135. fread(buf, 1, len, fp);
  136. UTIL_CloseFile(fp);
  137. for (i = CP_MIN; i < CP_MAX; i++)
  138. {
  139. int wlen = PAL_MultiByteToWideChar(buf, len, NULL, 0);
  140. WCHAR *wbuf = (WCHAR *)malloc(wlen * sizeof(WCHAR));
  141. PAL_MultiByteToWideChar(buf, len, wbuf, wlen);
  142. for (j = c = 0; j < wlen; j++)
  143. {
  144. c += (wbuf[j] == PAL_GetInvalidChar(i)) ? 1 : 0;
  145. }
  146. if (c < m)
  147. {
  148. m = c;
  149. m_i = i;
  150. }
  151. free(wbuf);
  152. }
  153. free(buf);
  154. iCodePage = m_i;
  155. }
  156. else
  157. {
  158. iCodePage = CP_BIG5;
  159. }
  160. }
  161. #endif
  162. //
  163. // Open files
  164. //
  165. gpGlobals->f.fpFBP = UTIL_OpenRequiredFile("fbp.mkf");
  166. gpGlobals->f.fpMGO = UTIL_OpenRequiredFile("mgo.mkf");
  167. gpGlobals->f.fpBALL = UTIL_OpenRequiredFile("ball.mkf");
  168. gpGlobals->f.fpDATA = UTIL_OpenRequiredFile("data.mkf");
  169. gpGlobals->f.fpF = UTIL_OpenRequiredFile("f.mkf");
  170. gpGlobals->f.fpFIRE = UTIL_OpenRequiredFile("fire.mkf");
  171. gpGlobals->f.fpRGM = UTIL_OpenRequiredFile("rgm.mkf");
  172. gpGlobals->f.fpSSS = UTIL_OpenRequiredFile("sss.mkf");
  173. #ifndef PAL_WIN95
  174. gpGlobals->lpObjectDesc = PAL_LoadObjectDesc(va("%s%s", PAL_PREFIX, "desc.dat"));
  175. #endif
  176. gpGlobals->bCurrentSaveSlot = 1;
  177. #ifdef PAL_UNICODE
  178. gpGlobals->iCodePage = iCodePage;
  179. gpGlobals->dwWordLength = dwWordLength;
  180. gpGlobals->dwExtraMagicDescLines = dwExtraMagicDescLines;
  181. gpGlobals->dwExtraItemDescLines = dwExtraItemDescLines;
  182. #endif
  183. return 0;
  184. }
  185. VOID
  186. PAL_FreeGlobals(
  187. VOID
  188. )
  189. /*++
  190. Purpose:
  191. Free global data.
  192. Parameters:
  193. None.
  194. Return value:
  195. None.
  196. --*/
  197. {
  198. if (gpGlobals != NULL)
  199. {
  200. //
  201. // Close all opened files
  202. //
  203. UTIL_CloseFile(gpGlobals->f.fpFBP);
  204. UTIL_CloseFile(gpGlobals->f.fpMGO);
  205. UTIL_CloseFile(gpGlobals->f.fpBALL);
  206. UTIL_CloseFile(gpGlobals->f.fpDATA);
  207. UTIL_CloseFile(gpGlobals->f.fpF);
  208. UTIL_CloseFile(gpGlobals->f.fpFIRE);
  209. UTIL_CloseFile(gpGlobals->f.fpRGM);
  210. UTIL_CloseFile(gpGlobals->f.fpSSS);
  211. //
  212. // Free the game data
  213. //
  214. free(gpGlobals->g.lprgEventObject);
  215. free(gpGlobals->g.lprgScriptEntry);
  216. free(gpGlobals->g.lprgStore);
  217. free(gpGlobals->g.lprgEnemy);
  218. free(gpGlobals->g.lprgEnemyTeam);
  219. free(gpGlobals->g.lprgMagic);
  220. free(gpGlobals->g.lprgBattleField);
  221. free(gpGlobals->g.lprgLevelUpMagic);
  222. //
  223. // Free the object description data
  224. //
  225. #ifndef PAL_WIN95
  226. PAL_FreeObjectDesc(gpGlobals->lpObjectDesc);
  227. #endif
  228. //
  229. // Delete the instance
  230. //
  231. free(gpGlobals);
  232. }
  233. gpGlobals = NULL;
  234. }
  235. static VOID
  236. PAL_ReadGlobalGameData(
  237. VOID
  238. )
  239. /*++
  240. Purpose:
  241. Read global game data from data files.
  242. Parameters:
  243. None.
  244. Return value:
  245. None.
  246. --*/
  247. {
  248. const GAMEDATA *p = &gpGlobals->g;
  249. LOAD_DATA(p->lprgScriptEntry, p->nScriptEntry * sizeof(SCRIPTENTRY),
  250. 4, gpGlobals->f.fpSSS);
  251. LOAD_DATA(p->lprgStore, p->nStore * sizeof(STORE), 0, gpGlobals->f.fpDATA);
  252. LOAD_DATA(p->lprgEnemy, p->nEnemy * sizeof(ENEMY), 1, gpGlobals->f.fpDATA);
  253. LOAD_DATA(p->lprgEnemyTeam, p->nEnemyTeam * sizeof(ENEMYTEAM),
  254. 2, gpGlobals->f.fpDATA);
  255. LOAD_DATA(p->lprgMagic, p->nMagic * sizeof(MAGIC), 4, gpGlobals->f.fpDATA);
  256. LOAD_DATA(p->lprgBattleField, p->nBattleField * sizeof(BATTLEFIELD),
  257. 5, gpGlobals->f.fpDATA);
  258. LOAD_DATA(p->lprgLevelUpMagic, p->nLevelUpMagic * sizeof(LEVELUPMAGIC_ALL),
  259. 6, gpGlobals->f.fpDATA);
  260. LOAD_DATA(p->rgwBattleEffectIndex, sizeof(p->rgwBattleEffectIndex),
  261. 11, gpGlobals->f.fpDATA);
  262. PAL_MKFReadChunk((LPBYTE)&(p->EnemyPos), sizeof(p->EnemyPos),
  263. 13, gpGlobals->f.fpDATA);
  264. DO_BYTESWAP(&(p->EnemyPos), sizeof(p->EnemyPos));
  265. PAL_MKFReadChunk((LPBYTE)(p->rgLevelUpExp), sizeof(p->rgLevelUpExp),
  266. 14, gpGlobals->f.fpDATA);
  267. DO_BYTESWAP(p->rgLevelUpExp, sizeof(p->rgLevelUpExp));
  268. }
  269. static VOID
  270. PAL_InitGlobalGameData(
  271. VOID
  272. )
  273. /*++
  274. Purpose:
  275. Initialize global game data.
  276. Parameters:
  277. None.
  278. Return value:
  279. None.
  280. --*/
  281. {
  282. int len;
  283. #define PAL_DOALLOCATE(fp, num, type, lptype, ptr, n) \
  284. { \
  285. len = PAL_MKFGetChunkSize(num, fp); \
  286. ptr = (lptype)malloc(len); \
  287. n = len / sizeof(type); \
  288. if (ptr == NULL) \
  289. { \
  290. TerminateOnError("PAL_InitGlobalGameData(): Memory allocation error!"); \
  291. } \
  292. }
  293. //
  294. // If the memory has not been allocated, allocate first.
  295. //
  296. if (gpGlobals->g.lprgEventObject == NULL)
  297. {
  298. PAL_DOALLOCATE(gpGlobals->f.fpSSS, 0, EVENTOBJECT, LPEVENTOBJECT,
  299. gpGlobals->g.lprgEventObject, gpGlobals->g.nEventObject);
  300. PAL_DOALLOCATE(gpGlobals->f.fpSSS, 4, SCRIPTENTRY, LPSCRIPTENTRY,
  301. gpGlobals->g.lprgScriptEntry, gpGlobals->g.nScriptEntry);
  302. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 0, STORE, LPSTORE,
  303. gpGlobals->g.lprgStore, gpGlobals->g.nStore);
  304. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 1, ENEMY, LPENEMY,
  305. gpGlobals->g.lprgEnemy, gpGlobals->g.nEnemy);
  306. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 2, ENEMYTEAM, LPENEMYTEAM,
  307. gpGlobals->g.lprgEnemyTeam, gpGlobals->g.nEnemyTeam);
  308. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 4, MAGIC, LPMAGIC,
  309. gpGlobals->g.lprgMagic, gpGlobals->g.nMagic);
  310. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 5, BATTLEFIELD, LPBATTLEFIELD,
  311. gpGlobals->g.lprgBattleField, gpGlobals->g.nBattleField);
  312. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 6, LEVELUPMAGIC_ALL, LPLEVELUPMAGIC_ALL,
  313. gpGlobals->g.lprgLevelUpMagic, gpGlobals->g.nLevelUpMagic);
  314. PAL_ReadGlobalGameData();
  315. }
  316. #undef PAL_DOALLOCATE
  317. }
  318. static VOID
  319. PAL_LoadDefaultGame(
  320. VOID
  321. )
  322. /*++
  323. Purpose:
  324. Load the default game data.
  325. Parameters:
  326. None.
  327. Return value:
  328. None.
  329. --*/
  330. {
  331. const GAMEDATA *p = &gpGlobals->g;
  332. UINT32 i;
  333. //
  334. // Load the default data from the game data files.
  335. //
  336. LOAD_DATA(p->lprgEventObject, p->nEventObject * sizeof(EVENTOBJECT),
  337. 0, gpGlobals->f.fpSSS);
  338. PAL_MKFReadChunk((LPBYTE)(p->rgScene), sizeof(p->rgScene), 1, gpGlobals->f.fpSSS);
  339. DO_BYTESWAP(p->rgScene, sizeof(p->rgScene));
  340. PAL_MKFReadChunk((LPBYTE)(p->rgObject), sizeof(p->rgObject), 2, gpGlobals->f.fpSSS);
  341. DO_BYTESWAP(p->rgObject, sizeof(p->rgObject));
  342. PAL_MKFReadChunk((LPBYTE)(&(p->PlayerRoles)), sizeof(PLAYERROLES),
  343. 3, gpGlobals->f.fpDATA);
  344. DO_BYTESWAP(&(p->PlayerRoles), sizeof(PLAYERROLES));
  345. //
  346. // Set some other default data.
  347. //
  348. gpGlobals->dwCash = 0;
  349. gpGlobals->wNumMusic = 0;
  350. gpGlobals->wNumPalette = 0;
  351. gpGlobals->wNumScene = 1;
  352. gpGlobals->wCollectValue = 0;
  353. gpGlobals->fNightPalette = FALSE;
  354. gpGlobals->wMaxPartyMemberIndex = 0;
  355. gpGlobals->viewport = PAL_XY(0, 0);
  356. gpGlobals->wLayer = 0;
  357. gpGlobals->wChaseRange = 1;
  358. #ifndef PAL_CLASSIC
  359. gpGlobals->bBattleSpeed = 2;
  360. #endif
  361. memset(gpGlobals->rgInventory, 0, sizeof(gpGlobals->rgInventory));
  362. memset(gpGlobals->rgPoisonStatus, 0, sizeof(gpGlobals->rgPoisonStatus));
  363. memset(gpGlobals->rgParty, 0, sizeof(gpGlobals->rgParty));
  364. memset(gpGlobals->rgTrail, 0, sizeof(gpGlobals->rgTrail));
  365. memset(&(gpGlobals->Exp), 0, sizeof(gpGlobals->Exp));
  366. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  367. {
  368. gpGlobals->Exp.rgPrimaryExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  369. gpGlobals->Exp.rgHealthExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  370. gpGlobals->Exp.rgMagicExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  371. gpGlobals->Exp.rgAttackExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  372. gpGlobals->Exp.rgMagicPowerExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  373. gpGlobals->Exp.rgDefenseExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  374. gpGlobals->Exp.rgDexterityExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  375. gpGlobals->Exp.rgFleeExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  376. }
  377. gpGlobals->fEnteringScene = TRUE;
  378. }
  379. static INT
  380. PAL_LoadGame(
  381. LPCSTR szFileName
  382. )
  383. /*++
  384. Purpose:
  385. Load a saved game.
  386. Parameters:
  387. [IN] szFileName - file name of saved game.
  388. Return value:
  389. 0 if success, -1 if failed.
  390. --*/
  391. {
  392. FILE *fp;
  393. PAL_LARGE SAVEDGAME s;
  394. //
  395. // Try to open the specified file
  396. //
  397. fp = fopen(szFileName, "rb");
  398. if (fp == NULL)
  399. {
  400. return -1;
  401. }
  402. //
  403. // Read all data from the file and close.
  404. //
  405. fread(&s, sizeof(SAVEDGAME), 1, fp);
  406. fclose(fp);
  407. //
  408. // Adjust endianness
  409. //
  410. DO_BYTESWAP(&s, sizeof(SAVEDGAME));
  411. //
  412. // Cash amount is in DWORD, so do a wordswap in Big-Endian.
  413. //
  414. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  415. s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
  416. #endif
  417. //
  418. // Get all the data from the saved game struct.
  419. //
  420. gpGlobals->viewport = PAL_XY(s.wViewportX, s.wViewportY);
  421. gpGlobals->wMaxPartyMemberIndex = s.nPartyMember;
  422. gpGlobals->wNumScene = s.wNumScene;
  423. gpGlobals->fNightPalette = (s.wPaletteOffset != 0);
  424. gpGlobals->wPartyDirection = s.wPartyDirection;
  425. gpGlobals->wNumMusic = s.wNumMusic;
  426. gpGlobals->wNumBattleMusic = s.wNumBattleMusic;
  427. gpGlobals->wNumBattleField = s.wNumBattleField;
  428. gpGlobals->wScreenWave = s.wScreenWave;
  429. gpGlobals->sWaveProgression = 0;
  430. gpGlobals->wCollectValue = s.wCollectValue;
  431. gpGlobals->wLayer = s.wLayer;
  432. gpGlobals->wChaseRange = s.wChaseRange;
  433. gpGlobals->wChasespeedChangeCycles = s.wChasespeedChangeCycles;
  434. gpGlobals->nFollower = s.nFollower;
  435. gpGlobals->dwCash = s.dwCash;
  436. #ifndef PAL_CLASSIC
  437. gpGlobals->bBattleSpeed = s.wBattleSpeed;
  438. if (gpGlobals->bBattleSpeed > 5 || gpGlobals->bBattleSpeed == 0)
  439. {
  440. gpGlobals->bBattleSpeed = 2;
  441. }
  442. #endif
  443. memcpy(gpGlobals->rgParty, s.rgParty, sizeof(gpGlobals->rgParty));
  444. memcpy(gpGlobals->rgTrail, s.rgTrail, sizeof(gpGlobals->rgTrail));
  445. gpGlobals->Exp = s.Exp;
  446. gpGlobals->g.PlayerRoles = s.PlayerRoles;
  447. memset(gpGlobals->rgPoisonStatus, 0, sizeof(gpGlobals->rgPoisonStatus));
  448. memcpy(gpGlobals->rgInventory, s.rgInventory, sizeof(gpGlobals->rgInventory));
  449. memcpy(gpGlobals->g.rgScene, s.rgScene, sizeof(gpGlobals->g.rgScene));
  450. memcpy(gpGlobals->g.rgObject, s.rgObject, sizeof(gpGlobals->g.rgObject));
  451. memcpy(gpGlobals->g.lprgEventObject, s.rgEventObject,
  452. sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  453. gpGlobals->fEnteringScene = FALSE;
  454. PAL_CompressInventory();
  455. //
  456. // Success
  457. //
  458. return 0;
  459. }
  460. VOID
  461. PAL_SaveGame(
  462. LPCSTR szFileName,
  463. WORD wSavedTimes
  464. )
  465. /*++
  466. Purpose:
  467. Save the current game state to file.
  468. Parameters:
  469. [IN] szFileName - file name of saved game.
  470. Return value:
  471. None.
  472. --*/
  473. {
  474. FILE *fp;
  475. PAL_LARGE SAVEDGAME s;
  476. UINT32 i;
  477. //
  478. // Put all the data to the saved game struct.
  479. //
  480. s.wViewportX = PAL_X(gpGlobals->viewport);
  481. s.wViewportY = PAL_Y(gpGlobals->viewport);
  482. s.nPartyMember = gpGlobals->wMaxPartyMemberIndex;
  483. s.wNumScene = gpGlobals->wNumScene;
  484. s.wPaletteOffset = (gpGlobals->fNightPalette ? 0x180 : 0);
  485. s.wPartyDirection = gpGlobals->wPartyDirection;
  486. s.wNumMusic = gpGlobals->wNumMusic;
  487. s.wNumBattleMusic = gpGlobals->wNumBattleMusic;
  488. s.wNumBattleField = gpGlobals->wNumBattleField;
  489. s.wScreenWave = gpGlobals->wScreenWave;
  490. s.wCollectValue = gpGlobals->wCollectValue;
  491. s.wLayer = gpGlobals->wLayer;
  492. s.wChaseRange = gpGlobals->wChaseRange;
  493. s.wChasespeedChangeCycles = gpGlobals->wChasespeedChangeCycles;
  494. s.nFollower = gpGlobals->nFollower;
  495. s.dwCash = gpGlobals->dwCash;
  496. #ifndef PAL_CLASSIC
  497. s.wBattleSpeed = gpGlobals->bBattleSpeed;
  498. #else
  499. s.wBattleSpeed = 2;
  500. #endif
  501. memcpy(s.rgParty, gpGlobals->rgParty, sizeof(gpGlobals->rgParty));
  502. memcpy(s.rgTrail, gpGlobals->rgTrail, sizeof(gpGlobals->rgTrail));
  503. s.Exp = gpGlobals->Exp;
  504. s.PlayerRoles = gpGlobals->g.PlayerRoles;
  505. memcpy(s.rgPoisonStatus, gpGlobals->rgPoisonStatus, sizeof(gpGlobals->rgPoisonStatus));
  506. memcpy(s.rgInventory, gpGlobals->rgInventory, sizeof(gpGlobals->rgInventory));
  507. memcpy(s.rgScene, gpGlobals->g.rgScene, sizeof(gpGlobals->g.rgScene));
  508. memcpy(s.rgObject, gpGlobals->g.rgObject, sizeof(gpGlobals->g.rgObject));
  509. memcpy(s.rgEventObject, gpGlobals->g.lprgEventObject,
  510. sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  511. s.wSavedTimes = wSavedTimes;
  512. //
  513. // Adjust endianness
  514. //
  515. DO_BYTESWAP(&s, sizeof(SAVEDGAME));
  516. //
  517. // Cash amount is in DWORD, so do a wordswap in Big-Endian.
  518. //
  519. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  520. s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
  521. #endif
  522. //
  523. // Try writing to file
  524. //
  525. fp = fopen(szFileName, "wb");
  526. if (fp == NULL)
  527. {
  528. return;
  529. }
  530. i = PAL_MKFGetChunkSize(0, gpGlobals->f.fpSSS);
  531. i += sizeof(SAVEDGAME) - sizeof(EVENTOBJECT) * MAX_EVENT_OBJECTS;
  532. fwrite(&s, i, 1, fp);
  533. fclose(fp);
  534. }
  535. VOID
  536. PAL_InitGameData(
  537. INT iSaveSlot
  538. )
  539. /*++
  540. Purpose:
  541. Initialize the game data (used when starting a new game or loading a saved game).
  542. Parameters:
  543. [IN] iSaveSlot - Slot of saved game.
  544. Return value:
  545. None.
  546. --*/
  547. {
  548. PAL_InitGlobalGameData();
  549. gpGlobals->bCurrentSaveSlot = (BYTE)iSaveSlot;
  550. //
  551. // try loading from the saved game file.
  552. //
  553. if (iSaveSlot == 0 || PAL_LoadGame(va("%s%d%s", PAL_SAVE_PREFIX, iSaveSlot, ".rpg")) != 0)
  554. {
  555. //
  556. // Cannot load the saved game file. Load the defaults.
  557. //
  558. PAL_LoadDefaultGame();
  559. }
  560. gpGlobals->fGameStart = TRUE;
  561. gpGlobals->fNeedToFadeIn = FALSE;
  562. gpGlobals->iCurInvMenuItem = 0;
  563. gpGlobals->fInBattle = FALSE;
  564. memset(gpGlobals->rgPlayerStatus, 0, sizeof(gpGlobals->rgPlayerStatus));
  565. PAL_UpdateEquipments();
  566. }
  567. BOOL
  568. PAL_AddItemToInventory(
  569. WORD wObjectID,
  570. INT iNum
  571. )
  572. /*++
  573. Purpose:
  574. Add or remove the specified kind of item in the inventory.
  575. Parameters:
  576. [IN] wObjectID - object number of the item.
  577. [IN] iNum - number to be added (positive value) or removed (negative value).
  578. Return value:
  579. TRUE if succeeded, FALSE if failed.
  580. --*/
  581. {
  582. int index;
  583. BOOL fFound;
  584. if (wObjectID == 0)
  585. {
  586. return FALSE;
  587. }
  588. if (iNum == 0)
  589. {
  590. iNum = 1;
  591. }
  592. index = 0;
  593. fFound = FALSE;
  594. //
  595. // Search for the specified item in the inventory
  596. //
  597. while (index < MAX_INVENTORY)
  598. {
  599. if (gpGlobals->rgInventory[index].wItem == wObjectID)
  600. {
  601. fFound = TRUE;
  602. break;
  603. }
  604. else if (gpGlobals->rgInventory[index].wItem == 0)
  605. {
  606. break;
  607. }
  608. index++;
  609. }
  610. if (iNum > 0)
  611. {
  612. //
  613. // Add item
  614. //
  615. if (index >= MAX_INVENTORY)
  616. {
  617. //
  618. // inventory is full. cannot add item
  619. //
  620. return FALSE;
  621. }
  622. if (fFound)
  623. {
  624. gpGlobals->rgInventory[index].nAmount += iNum;
  625. if (gpGlobals->rgInventory[index].nAmount > 99)
  626. {
  627. //
  628. // Maximum number is 99
  629. //
  630. gpGlobals->rgInventory[index].nAmount = 99;
  631. }
  632. }
  633. else
  634. {
  635. gpGlobals->rgInventory[index].wItem = wObjectID;
  636. if (iNum > 99)
  637. {
  638. iNum = 99;
  639. }
  640. gpGlobals->rgInventory[index].nAmount = iNum;
  641. }
  642. return TRUE;
  643. }
  644. else
  645. {
  646. //
  647. // Remove item
  648. //
  649. if (fFound)
  650. {
  651. iNum *= -1;
  652. if (gpGlobals->rgInventory[index].nAmount < iNum)
  653. {
  654. //
  655. // This item has been run out
  656. //
  657. gpGlobals->rgInventory[index].nAmount = 0;
  658. return FALSE;
  659. }
  660. gpGlobals->rgInventory[index].nAmount -= iNum;
  661. return TRUE;
  662. }
  663. return FALSE;
  664. }
  665. }
  666. INT
  667. PAL_GetItemAmount(
  668. WORD wItem
  669. )
  670. /*++
  671. Purpose:
  672. Get the amount of the specified item in the inventory.
  673. Parameters:
  674. [IN] wItem - the object ID of the item.
  675. Return value:
  676. The amount of the item in the inventory.
  677. --*/
  678. {
  679. int i;
  680. for (i = 0; i < MAX_INVENTORY; i++)
  681. {
  682. if (gpGlobals->rgInventory[i].wItem == 0)
  683. {
  684. break;
  685. }
  686. if (gpGlobals->rgInventory[i].wItem == wItem)
  687. {
  688. return gpGlobals->rgInventory[i].nAmount;
  689. }
  690. }
  691. return 0;
  692. }
  693. VOID
  694. PAL_CompressInventory(
  695. VOID
  696. )
  697. /*++
  698. Purpose:
  699. Remove all the items in inventory which has a number of zero.
  700. Parameters:
  701. None.
  702. Return value:
  703. None.
  704. --*/
  705. {
  706. int i, j;
  707. j = 0;
  708. for (i = 0; i < MAX_INVENTORY; i++)
  709. {
  710. if (gpGlobals->rgInventory[i].wItem == 0)
  711. {
  712. break;
  713. }
  714. if (gpGlobals->rgInventory[i].nAmount > 0)
  715. {
  716. gpGlobals->rgInventory[j] = gpGlobals->rgInventory[i];
  717. j++;
  718. }
  719. }
  720. for (; j < MAX_INVENTORY; j++)
  721. {
  722. gpGlobals->rgInventory[j].nAmount = 0;
  723. gpGlobals->rgInventory[j].nAmountInUse = 0;
  724. gpGlobals->rgInventory[j].wItem = 0;
  725. }
  726. }
  727. BOOL
  728. PAL_IncreaseHPMP(
  729. WORD wPlayerRole,
  730. SHORT sHP,
  731. SHORT sMP
  732. )
  733. /*++
  734. Purpose:
  735. Increase or decrease player's HP and/or MP.
  736. Parameters:
  737. [IN] wPlayerRole - the number of player role.
  738. [IN] sHP - number of HP to be increased (positive value) or decrased
  739. (negative value).
  740. [IN] sMP - number of MP to be increased (positive value) or decrased
  741. (negative value).
  742. Return value:
  743. TRUE if the operation is succeeded, FALSE if not.
  744. --*/
  745. {
  746. BOOL fSuccess = FALSE;
  747. //
  748. // Only care about alive players
  749. //
  750. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  751. {
  752. //
  753. // change HP
  754. //
  755. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] += sHP;
  756. if ((SHORT)(gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole]) < 0)
  757. {
  758. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] = 0;
  759. }
  760. else if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] >
  761. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole])
  762. {
  763. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] =
  764. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole];
  765. }
  766. //
  767. // Change MP
  768. //
  769. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] += sMP;
  770. if ((SHORT)(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole]) < 0)
  771. {
  772. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] = 0;
  773. }
  774. else if (gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] >
  775. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole])
  776. {
  777. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] =
  778. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole];
  779. }
  780. fSuccess = TRUE;
  781. }
  782. return fSuccess;
  783. }
  784. VOID
  785. PAL_UpdateEquipments(
  786. VOID
  787. )
  788. /*++
  789. Purpose:
  790. Update the effects of all equipped items for all players.
  791. Parameters:
  792. None.
  793. Return value:
  794. None.
  795. --*/
  796. {
  797. int i, j;
  798. WORD w;
  799. memset(&(gpGlobals->rgEquipmentEffect), 0, sizeof(gpGlobals->rgEquipmentEffect));
  800. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  801. {
  802. for (j = 0; j < MAX_PLAYER_EQUIPMENTS; j++)
  803. {
  804. w = gpGlobals->g.PlayerRoles.rgwEquipment[j][i];
  805. if (w != 0)
  806. {
  807. gpGlobals->g.rgObject[w].item.wScriptOnEquip =
  808. PAL_RunTriggerScript(gpGlobals->g.rgObject[w].item.wScriptOnEquip, (WORD)i);
  809. }
  810. }
  811. }
  812. }
  813. VOID
  814. PAL_RemoveEquipmentEffect(
  815. WORD wPlayerRole,
  816. WORD wEquipPart
  817. )
  818. /*++
  819. Purpose:
  820. Remove all the effects of the equipment for the player.
  821. Parameters:
  822. [IN] wPlayerRole - the player role.
  823. [IN] wEquipPart - the part of the equipment.
  824. Return value:
  825. None.
  826. --*/
  827. {
  828. WORD *p;
  829. int i, j;
  830. p = (WORD *)(&gpGlobals->rgEquipmentEffect[wEquipPart]); // HACKHACK
  831. for (i = 0; i < sizeof(PLAYERROLES) / sizeof(PLAYERS); i++)
  832. {
  833. p[i * MAX_PLAYER_ROLES + wPlayerRole] = 0;
  834. }
  835. //
  836. // Reset some parameters to default when appropriate
  837. //
  838. if (wEquipPart == kBodyPartHand)
  839. {
  840. //
  841. // reset the dual attack status
  842. //
  843. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusDualAttack] = 0;
  844. }
  845. else if (wEquipPart == kBodyPartWear)
  846. {
  847. //
  848. // Remove all poisons leveled 99
  849. //
  850. for (i = 0; i <= (short)gpGlobals->wMaxPartyMemberIndex; i++)
  851. {
  852. if (gpGlobals->rgParty[i].wPlayerRole == wPlayerRole)
  853. {
  854. wPlayerRole = i;
  855. break;
  856. }
  857. }
  858. if (i <= (short)gpGlobals->wMaxPartyMemberIndex)
  859. {
  860. j = 0;
  861. for (i = 0; i < MAX_POISONS; i++)
  862. {
  863. WORD w = gpGlobals->rgPoisonStatus[i][wPlayerRole].wPoisonID;
  864. if (w == 0)
  865. {
  866. break;
  867. }
  868. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel < 99)
  869. {
  870. gpGlobals->rgPoisonStatus[j][wPlayerRole] =
  871. gpGlobals->rgPoisonStatus[i][wPlayerRole];
  872. j++;
  873. }
  874. }
  875. while (j < MAX_POISONS)
  876. {
  877. gpGlobals->rgPoisonStatus[j][wPlayerRole].wPoisonID = 0;
  878. gpGlobals->rgPoisonStatus[j][wPlayerRole].wPoisonScript = 0;
  879. j++;
  880. }
  881. }
  882. }
  883. }
  884. VOID
  885. PAL_AddPoisonForPlayer(
  886. WORD wPlayerRole,
  887. WORD wPoisonID
  888. )
  889. /*++
  890. Purpose:
  891. Add the specified poison to the player.
  892. Parameters:
  893. [IN] wPlayerRole - the player role ID.
  894. [IN] wPoisonID - the poison to be added.
  895. Return value:
  896. None.
  897. --*/
  898. {
  899. int i, index;
  900. WORD w;
  901. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  902. {
  903. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  904. {
  905. break;
  906. }
  907. }
  908. if (index > gpGlobals->wMaxPartyMemberIndex)
  909. {
  910. return; // don't go further
  911. }
  912. for (i = 0; i < MAX_POISONS; i++)
  913. {
  914. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  915. if (w == 0)
  916. {
  917. break;
  918. }
  919. if (w == wPoisonID)
  920. {
  921. return; // already poisoned
  922. }
  923. }
  924. if (i < MAX_POISONS)
  925. {
  926. gpGlobals->rgPoisonStatus[i][index].wPoisonID = wPoisonID;
  927. gpGlobals->rgPoisonStatus[i][index].wPoisonScript =
  928. gpGlobals->g.rgObject[wPoisonID].poison.wPlayerScript;
  929. }
  930. }
  931. VOID
  932. PAL_CurePoisonByKind(
  933. WORD wPlayerRole,
  934. WORD wPoisonID
  935. )
  936. /*++
  937. Purpose:
  938. Remove the specified poison from the player.
  939. Parameters:
  940. [IN] wPlayerRole - the player role ID.
  941. [IN] wPoisonID - the poison to be removed.
  942. Return value:
  943. None.
  944. --*/
  945. {
  946. int i, index;
  947. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  948. {
  949. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  950. {
  951. break;
  952. }
  953. }
  954. if (index > gpGlobals->wMaxPartyMemberIndex)
  955. {
  956. return; // don't go further
  957. }
  958. for (i = 0; i < MAX_POISONS; i++)
  959. {
  960. if (gpGlobals->rgPoisonStatus[i][index].wPoisonID == wPoisonID)
  961. {
  962. gpGlobals->rgPoisonStatus[i][index].wPoisonID = 0;
  963. gpGlobals->rgPoisonStatus[i][index].wPoisonScript = 0;
  964. }
  965. }
  966. }
  967. VOID
  968. PAL_CurePoisonByLevel(
  969. WORD wPlayerRole,
  970. WORD wMaxLevel
  971. )
  972. /*++
  973. Purpose:
  974. Remove the poisons which have a maximum level of wMaxLevel from the player.
  975. Parameters:
  976. [IN] wPlayerRole - the player role ID.
  977. [IN] wMaxLevel - the maximum level of poisons to be removed.
  978. Return value:
  979. None.
  980. --*/
  981. {
  982. int i, index;
  983. WORD w;
  984. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  985. {
  986. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  987. {
  988. break;
  989. }
  990. }
  991. if (index > gpGlobals->wMaxPartyMemberIndex)
  992. {
  993. return; // don't go further
  994. }
  995. for (i = 0; i < MAX_POISONS; i++)
  996. {
  997. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  998. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel <= wMaxLevel)
  999. {
  1000. gpGlobals->rgPoisonStatus[i][index].wPoisonID = 0;
  1001. gpGlobals->rgPoisonStatus[i][index].wPoisonScript = 0;
  1002. }
  1003. }
  1004. }
  1005. BOOL
  1006. PAL_IsPlayerPoisonedByLevel(
  1007. WORD wPlayerRole,
  1008. WORD wMinLevel
  1009. )
  1010. /*++
  1011. Purpose:
  1012. Check if the player is poisoned by poisons at a minimum level of wMinLevel.
  1013. Parameters:
  1014. [IN] wPlayerRole - the player role ID.
  1015. [IN] wMinLevel - the minimum level of poison.
  1016. Return value:
  1017. TRUE if the player is poisoned by poisons at a minimum level of wMinLevel;
  1018. FALSE if not.
  1019. --*/
  1020. {
  1021. int i, index;
  1022. WORD w;
  1023. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1024. {
  1025. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1026. {
  1027. break;
  1028. }
  1029. }
  1030. if (index > gpGlobals->wMaxPartyMemberIndex)
  1031. {
  1032. return FALSE; // don't go further
  1033. }
  1034. for (i = 0; i < MAX_POISONS; i++)
  1035. {
  1036. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  1037. w = gpGlobals->g.rgObject[w].poison.wPoisonLevel;
  1038. if (w >= 99)
  1039. {
  1040. //
  1041. // Ignore poisons which has a level of 99 (usually effect of equipment)
  1042. //
  1043. continue;
  1044. }
  1045. if (w >= wMinLevel)
  1046. {
  1047. return TRUE;
  1048. }
  1049. }
  1050. return FALSE;
  1051. }
  1052. BOOL
  1053. PAL_IsPlayerPoisonedByKind(
  1054. WORD wPlayerRole,
  1055. WORD wPoisonID
  1056. )
  1057. /*++
  1058. Purpose:
  1059. Check if the player is poisoned by the specified poison.
  1060. Parameters:
  1061. [IN] wPlayerRole - the player role ID.
  1062. [IN] wPoisonID - the poison to be checked.
  1063. Return value:
  1064. TRUE if player is poisoned by the specified poison;
  1065. FALSE if not.
  1066. --*/
  1067. {
  1068. int i, index;
  1069. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1070. {
  1071. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1072. {
  1073. break;
  1074. }
  1075. }
  1076. if (index > gpGlobals->wMaxPartyMemberIndex)
  1077. {
  1078. return FALSE; // don't go further
  1079. }
  1080. for (i = 0; i < MAX_POISONS; i++)
  1081. {
  1082. if (gpGlobals->rgPoisonStatus[i][index].wPoisonID == wPoisonID)
  1083. {
  1084. return TRUE;
  1085. }
  1086. }
  1087. return FALSE;
  1088. }
  1089. WORD
  1090. PAL_GetPlayerAttackStrength(
  1091. WORD wPlayerRole
  1092. )
  1093. /*++
  1094. Purpose:
  1095. Get the player's attack strength, count in the effect of equipments.
  1096. Parameters:
  1097. [IN] wPlayerRole - the player role ID.
  1098. Return value:
  1099. The total attack strength of the player.
  1100. --*/
  1101. {
  1102. WORD w;
  1103. int i;
  1104. w = gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole];
  1105. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1106. {
  1107. w += gpGlobals->rgEquipmentEffect[i].rgwAttackStrength[wPlayerRole];
  1108. }
  1109. return w;
  1110. }
  1111. WORD
  1112. PAL_GetPlayerMagicStrength(
  1113. WORD wPlayerRole
  1114. )
  1115. /*++
  1116. Purpose:
  1117. Get the player's magic strength, count in the effect of equipments.
  1118. Parameters:
  1119. [IN] wPlayerRole - the player role ID.
  1120. Return value:
  1121. The total magic strength of the player.
  1122. --*/
  1123. {
  1124. WORD w;
  1125. int i;
  1126. w = gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole];
  1127. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1128. {
  1129. w += gpGlobals->rgEquipmentEffect[i].rgwMagicStrength[wPlayerRole];
  1130. }
  1131. return w;
  1132. }
  1133. WORD
  1134. PAL_GetPlayerDefense(
  1135. WORD wPlayerRole
  1136. )
  1137. /*++
  1138. Purpose:
  1139. Get the player's defense value, count in the effect of equipments.
  1140. Parameters:
  1141. [IN] wPlayerRole - the player role ID.
  1142. Return value:
  1143. The total defense value of the player.
  1144. --*/
  1145. {
  1146. WORD w;
  1147. int i;
  1148. w = gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole];
  1149. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1150. {
  1151. w += gpGlobals->rgEquipmentEffect[i].rgwDefense[wPlayerRole];
  1152. }
  1153. return w;
  1154. }
  1155. WORD
  1156. PAL_GetPlayerDexterity(
  1157. WORD wPlayerRole
  1158. )
  1159. /*++
  1160. Purpose:
  1161. Get the player's dexterity, count in the effect of equipments.
  1162. Parameters:
  1163. [IN] wPlayerRole - the player role ID.
  1164. Return value:
  1165. The total dexterity of the player.
  1166. --*/
  1167. {
  1168. WORD w;
  1169. int i;
  1170. w = gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole];
  1171. #ifdef PAL_CLASSIC
  1172. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1173. #else
  1174. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS - 1; i++)
  1175. #endif
  1176. {
  1177. w += gpGlobals->rgEquipmentEffect[i].rgwDexterity[wPlayerRole];
  1178. }
  1179. return w;
  1180. }
  1181. WORD
  1182. PAL_GetPlayerFleeRate(
  1183. WORD wPlayerRole
  1184. )
  1185. /*++
  1186. Purpose:
  1187. Get the player's flee rate, count in the effect of equipments.
  1188. Parameters:
  1189. [IN] wPlayerRole - the player role ID.
  1190. Return value:
  1191. The total flee rate of the player.
  1192. --*/
  1193. {
  1194. WORD w;
  1195. int i;
  1196. w = gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole];
  1197. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1198. {
  1199. w += gpGlobals->rgEquipmentEffect[i].rgwFleeRate[wPlayerRole];
  1200. }
  1201. return w;
  1202. }
  1203. WORD
  1204. PAL_GetPlayerPoisonResistance(
  1205. WORD wPlayerRole
  1206. )
  1207. /*++
  1208. Purpose:
  1209. Get the player's resistance to poisons, count in the effect of equipments.
  1210. Parameters:
  1211. [IN] wPlayerRole - the player role ID.
  1212. Return value:
  1213. The total resistance to poisons of the player.
  1214. --*/
  1215. {
  1216. WORD w;
  1217. int i;
  1218. w = gpGlobals->g.PlayerRoles.rgwPoisonResistance[wPlayerRole];
  1219. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1220. {
  1221. w += gpGlobals->rgEquipmentEffect[i].rgwPoisonResistance[wPlayerRole];
  1222. }
  1223. if (w > 100)
  1224. {
  1225. w = 100;
  1226. }
  1227. return w;
  1228. }
  1229. WORD
  1230. PAL_GetPlayerElementalResistance(
  1231. WORD wPlayerRole,
  1232. INT iAttrib
  1233. )
  1234. /*++
  1235. Purpose:
  1236. Get the player's resistance to attributed magics, count in the effect
  1237. of equipments.
  1238. Parameters:
  1239. [IN] wPlayerRole - the player role ID.
  1240. [IN] iAttrib - the attribute of magics.
  1241. Return value:
  1242. The total resistance to the attributed magics of the player.
  1243. --*/
  1244. {
  1245. WORD w;
  1246. int i;
  1247. w = gpGlobals->g.PlayerRoles.rgwElementalResistance[iAttrib][wPlayerRole];
  1248. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1249. {
  1250. w += gpGlobals->rgEquipmentEffect[i].rgwElementalResistance[iAttrib][wPlayerRole];
  1251. }
  1252. if (w > 100)
  1253. {
  1254. w = 100;
  1255. }
  1256. return w;
  1257. }
  1258. WORD
  1259. PAL_GetPlayerBattleSprite(
  1260. WORD wPlayerRole
  1261. )
  1262. /*++
  1263. Purpose:
  1264. Get player's battle sprite.
  1265. Parameters:
  1266. [IN] wPlayerRole - the player role ID.
  1267. Return value:
  1268. Number of the player's battle sprite.
  1269. --*/
  1270. {
  1271. int i;
  1272. WORD w;
  1273. w = gpGlobals->g.PlayerRoles.rgwSpriteNumInBattle[wPlayerRole];
  1274. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1275. {
  1276. if (gpGlobals->rgEquipmentEffect[i].rgwSpriteNumInBattle[wPlayerRole] != 0)
  1277. {
  1278. w = gpGlobals->rgEquipmentEffect[i].rgwSpriteNumInBattle[wPlayerRole];
  1279. }
  1280. }
  1281. return w;
  1282. }
  1283. WORD
  1284. PAL_GetPlayerCooperativeMagic(
  1285. WORD wPlayerRole
  1286. )
  1287. /*++
  1288. Purpose:
  1289. Get player's cooperative magic.
  1290. Parameters:
  1291. [IN] wPlayerRole - the player role ID.
  1292. Return value:
  1293. Object ID of the player's cooperative magic.
  1294. --*/
  1295. {
  1296. int i;
  1297. WORD w;
  1298. w = gpGlobals->g.PlayerRoles.rgwCooperativeMagic[wPlayerRole];
  1299. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1300. {
  1301. if (gpGlobals->rgEquipmentEffect[i].rgwCooperativeMagic[wPlayerRole] != 0)
  1302. {
  1303. w = gpGlobals->rgEquipmentEffect[i].rgwCooperativeMagic[wPlayerRole];
  1304. }
  1305. }
  1306. return w;
  1307. }
  1308. BOOL
  1309. PAL_PlayerCanAttackAll(
  1310. WORD wPlayerRole
  1311. )
  1312. /*++
  1313. Purpose:
  1314. Check if the player can attack all of the enemies in one move.
  1315. Parameters:
  1316. [IN] wPlayerRole - the player role ID.
  1317. Return value:
  1318. TRUE if player can attack all of the enemies in one move, FALSE if not.
  1319. --*/
  1320. {
  1321. int i;
  1322. BOOL f;
  1323. f = FALSE;
  1324. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1325. {
  1326. if (gpGlobals->rgEquipmentEffect[i].rgwAttackAll[wPlayerRole] != 0)
  1327. {
  1328. f = TRUE;
  1329. break;
  1330. }
  1331. }
  1332. return f;
  1333. }
  1334. BOOL
  1335. PAL_AddMagic(
  1336. WORD wPlayerRole,
  1337. WORD wMagic
  1338. )
  1339. /*++
  1340. Purpose:
  1341. Add a magic to the player.
  1342. Parameters:
  1343. [IN] wPlayerRole - the player role ID.
  1344. [IN] wMagic - the object ID of the magic.
  1345. Return value:
  1346. TRUE if succeeded, FALSE if failed.
  1347. --*/
  1348. {
  1349. int i;
  1350. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1351. {
  1352. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == wMagic)
  1353. {
  1354. //
  1355. // already have this magic
  1356. //
  1357. return FALSE;
  1358. }
  1359. }
  1360. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1361. {
  1362. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == 0)
  1363. {
  1364. break;
  1365. }
  1366. }
  1367. if (i >= MAX_PLAYER_MAGICS)
  1368. {
  1369. //
  1370. // Not enough slots
  1371. //
  1372. return FALSE;
  1373. }
  1374. gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] = wMagic;
  1375. return TRUE;
  1376. }
  1377. VOID
  1378. PAL_RemoveMagic(
  1379. WORD wPlayerRole,
  1380. WORD wMagic
  1381. )
  1382. /*++
  1383. Purpose:
  1384. Remove a magic to the player.
  1385. Parameters:
  1386. [IN] wPlayerRole - the player role ID.
  1387. [IN] wMagic - the object ID of the magic.
  1388. Return value:
  1389. None.
  1390. --*/
  1391. {
  1392. int i;
  1393. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1394. {
  1395. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == wMagic)
  1396. {
  1397. gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] = 0;
  1398. break;
  1399. }
  1400. }
  1401. }
  1402. VOID
  1403. PAL_SetPlayerStatus(
  1404. WORD wPlayerRole,
  1405. WORD wStatusID,
  1406. WORD wNumRound
  1407. )
  1408. /*++
  1409. Purpose:
  1410. Set one of the statuses for the player.
  1411. Parameters:
  1412. [IN] wPlayerRole - the player ID.
  1413. [IN] wStatusID - the status to be set.
  1414. [IN] wNumRound - the effective rounds of the status.
  1415. Return value:
  1416. None.
  1417. --*/
  1418. {
  1419. #ifndef PAL_CLASSIC
  1420. if (wStatusID == kStatusSlow &&
  1421. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusHaste] > 0)
  1422. {
  1423. //
  1424. // Remove the haste status
  1425. //
  1426. PAL_RemovePlayerStatus(wPlayerRole, kStatusHaste);
  1427. return;
  1428. }
  1429. if (wStatusID == kStatusHaste &&
  1430. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSlow] > 0)
  1431. {
  1432. //
  1433. // Remove the slow status
  1434. //
  1435. PAL_RemovePlayerStatus(wPlayerRole, kStatusSlow);
  1436. return;
  1437. }
  1438. #endif
  1439. switch (wStatusID)
  1440. {
  1441. case kStatusConfused:
  1442. case kStatusSleep:
  1443. case kStatusSilence:
  1444. #ifdef PAL_CLASSIC
  1445. case kStatusParalyzed:
  1446. #else
  1447. case kStatusSlow:
  1448. #endif
  1449. //
  1450. // for "bad" statuses, don't set the status when we already have it
  1451. //
  1452. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] != 0 &&
  1453. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] == 0)
  1454. {
  1455. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1456. }
  1457. break;
  1458. case kStatusPuppet:
  1459. //
  1460. // only allow dead players for "puppet" status
  1461. //
  1462. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 &&
  1463. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] < wNumRound)
  1464. {
  1465. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1466. }
  1467. break;
  1468. case kStatusBravery:
  1469. case kStatusProtect:
  1470. case kStatusDualAttack:
  1471. case kStatusHaste:
  1472. //
  1473. // for "good" statuses, reset the status if the status to be set lasts longer
  1474. //
  1475. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] != 0 &&
  1476. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] < wNumRound)
  1477. {
  1478. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1479. }
  1480. break;
  1481. default:
  1482. assert(FALSE);
  1483. break;
  1484. }
  1485. }
  1486. VOID
  1487. PAL_RemovePlayerStatus(
  1488. WORD wPlayerRole,
  1489. WORD wStatusID
  1490. )
  1491. /*++
  1492. Purpose:
  1493. Remove one of the status for player.
  1494. Parameters:
  1495. [IN] wPlayerRole - the player ID.
  1496. [IN] wStatusID - the status to be set.
  1497. Return value:
  1498. None.
  1499. --*/
  1500. {
  1501. //
  1502. // Don't remove effects of equipments
  1503. //
  1504. if (gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] <= 999)
  1505. {
  1506. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = 0;
  1507. }
  1508. }
  1509. VOID
  1510. PAL_ClearAllPlayerStatus(
  1511. VOID
  1512. )
  1513. /*++
  1514. Purpose:
  1515. Clear all player status.
  1516. Parameters:
  1517. None.
  1518. Return value:
  1519. None.
  1520. --*/
  1521. {
  1522. int i, j;
  1523. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  1524. {
  1525. for (j = 0; j < kStatusAll; j++)
  1526. {
  1527. //
  1528. // Don't remove effects of equipments
  1529. //
  1530. if (gpGlobals->rgPlayerStatus[i][j] <= 999)
  1531. {
  1532. gpGlobals->rgPlayerStatus[i][j] = 0;
  1533. }
  1534. }
  1535. }
  1536. }
  1537. VOID
  1538. PAL_PlayerLevelUp(
  1539. WORD wPlayerRole,
  1540. WORD wNumLevel
  1541. )
  1542. /*++
  1543. Purpose:
  1544. Increase the player's level by wLevels.
  1545. Parameters:
  1546. [IN] wPlayerRole - player role ID.
  1547. [IN] wNumLevel - number of levels to be increased.
  1548. Return value:
  1549. None.
  1550. --*/
  1551. {
  1552. WORD i;
  1553. //
  1554. // Add the level
  1555. //
  1556. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] += wNumLevel;
  1557. if (gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] > MAX_LEVELS)
  1558. {
  1559. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] = MAX_LEVELS;
  1560. }
  1561. for (i = 0; i < wNumLevel; i++)
  1562. {
  1563. //
  1564. // Increase player's stats
  1565. //
  1566. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole] += 10 + RandomLong(0, 8);
  1567. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole] += 8 + RandomLong(0, 6);
  1568. gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole] += 4 + RandomLong(0, 1);
  1569. gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole] += 4 + RandomLong(0, 1);
  1570. gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole] += 2 + RandomLong(0, 1);
  1571. gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole] += 2 + RandomLong(0, 1);
  1572. gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole] += 2;
  1573. }
  1574. #define STAT_LIMIT(t) { if ((t) > 999) (t) = 999; }
  1575. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole]);
  1576. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole]);
  1577. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole]);
  1578. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole]);
  1579. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole]);
  1580. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole]);
  1581. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole]);
  1582. #undef STAT_LIMIT
  1583. //
  1584. // Reset experience points to zero
  1585. //
  1586. gpGlobals->Exp.rgPrimaryExp[wPlayerRole].wExp = 0;
  1587. gpGlobals->Exp.rgPrimaryExp[wPlayerRole].wLevel =
  1588. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole];
  1589. }