global.c 57 KB

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