global.c 49 KB

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