global.c 57 KB

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