global.c 48 KB

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