global.c 58 KB

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