global.c 58 KB

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