global.c 56 KB

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