global.c 39 KB

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