global.c 57 KB

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