global.c 50 KB

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