global.c 50 KB

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