global.c 55 KB

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