global.c 54 KB

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