global.c 50 KB

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