global.c 39 KB

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