global.c 50 KB

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