global.c 55 KB

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