global.c 49 KB

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