global.c 56 KB

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