global.c 54 KB

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