global.c 55 KB

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