global.c 59 KB

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