global.c 50 KB

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