global.c 49 KB

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