global.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  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. #define DO_BYTESWAP(buf, size) \
  26. for (i = 0; i < (size) / 2; i++) \
  27. { \
  28. ((LPWORD)(buf))[i] = SWAP16(((LPWORD)(buf))[i]); \
  29. }
  30. #define LOAD_DATA(buf, size, chunknum, fp) \
  31. { \
  32. PAL_MKFReadChunk((LPBYTE)(buf), (size), (chunknum), (fp)); \
  33. DO_BYTESWAP(buf, size); \
  34. }
  35. INT
  36. PAL_InitGlobals(
  37. VOID
  38. )
  39. /*++
  40. Purpose:
  41. Initialize global data.
  42. Parameters:
  43. [IN] iCodePage - the code page for text conversion.
  44. [IN] dwWordLength - the length of each word.
  45. Return value:
  46. 0 = success, -1 = error.
  47. --*/
  48. {
  49. #ifdef PAL_UNICODE
  50. FILE *fp;
  51. CODEPAGE iCodePage = CP_BIG5; // Default for PAL DOS
  52. DWORD dwWordLength = 10; // Default for PAL DOS/WIN95
  53. #endif
  54. if (gpGlobals == NULL)
  55. {
  56. gpGlobals = (LPGLOBALVARS)calloc(1, sizeof(GLOBALVARS));
  57. if (gpGlobals == NULL)
  58. {
  59. return -1;
  60. }
  61. }
  62. #ifdef PAL_UNICODE
  63. if (fp = UTIL_OpenFile("sdlpal.cfg"))
  64. {
  65. PAL_LARGE char buf[512];
  66. //
  67. // Load the configuration data
  68. //
  69. while (fgets(buf, 512, fp) != NULL)
  70. {
  71. char *p = buf;
  72. //
  73. // Skip leading spaces
  74. //
  75. while (*p && isspace(*p)) p++;
  76. //
  77. // Skip comments
  78. //
  79. if (*p && *p != '#')
  80. {
  81. char *ptr;
  82. if (ptr = strchr(p, '='))
  83. {
  84. char *end = ptr - 1;
  85. *ptr++ = 0;
  86. //
  87. // Skip tailing & leading spaces
  88. //
  89. while (isspace(*end) && end >= p) *end-- = 0;
  90. if (SDL_strcasecmp(p, "CODEPAGE") == 0)
  91. {
  92. sscanf(ptr, "%d", &iCodePage);
  93. }
  94. else if (SDL_strcasecmp(p, "WORDLENGTH") == 0)
  95. {
  96. sscanf(ptr, "%u", &dwWordLength);
  97. }
  98. }
  99. }
  100. }
  101. UTIL_CloseFile(fp);
  102. }
  103. #endif
  104. //
  105. // Open files
  106. //
  107. gpGlobals->f.fpFBP = UTIL_OpenRequiredFile("fbp.mkf");
  108. gpGlobals->f.fpMGO = UTIL_OpenRequiredFile("mgo.mkf");
  109. gpGlobals->f.fpBALL = UTIL_OpenRequiredFile("ball.mkf");
  110. gpGlobals->f.fpDATA = UTIL_OpenRequiredFile("data.mkf");
  111. gpGlobals->f.fpF = UTIL_OpenRequiredFile("f.mkf");
  112. gpGlobals->f.fpFIRE = UTIL_OpenRequiredFile("fire.mkf");
  113. gpGlobals->f.fpRGM = UTIL_OpenRequiredFile("rgm.mkf");
  114. gpGlobals->f.fpSSS = UTIL_OpenRequiredFile("sss.mkf");
  115. #ifndef PAL_WIN95
  116. gpGlobals->lpObjectDesc = PAL_LoadObjectDesc(va("%s%s", PAL_PREFIX, "desc.dat"));
  117. #endif
  118. gpGlobals->bCurrentSaveSlot = 1;
  119. #ifdef PAL_UNICODE
  120. gpGlobals->iCodePage = iCodePage;
  121. gpGlobals->dwWordLength = dwWordLength;
  122. #endif
  123. return 0;
  124. }
  125. VOID
  126. PAL_FreeGlobals(
  127. VOID
  128. )
  129. /*++
  130. Purpose:
  131. Free global data.
  132. Parameters:
  133. None.
  134. Return value:
  135. None.
  136. --*/
  137. {
  138. if (gpGlobals != NULL)
  139. {
  140. //
  141. // Close all opened files
  142. //
  143. UTIL_CloseFile(gpGlobals->f.fpFBP);
  144. UTIL_CloseFile(gpGlobals->f.fpMGO);
  145. UTIL_CloseFile(gpGlobals->f.fpBALL);
  146. UTIL_CloseFile(gpGlobals->f.fpDATA);
  147. UTIL_CloseFile(gpGlobals->f.fpF);
  148. UTIL_CloseFile(gpGlobals->f.fpFIRE);
  149. UTIL_CloseFile(gpGlobals->f.fpRGM);
  150. UTIL_CloseFile(gpGlobals->f.fpSSS);
  151. //
  152. // Free the game data
  153. //
  154. free(gpGlobals->g.lprgEventObject);
  155. free(gpGlobals->g.lprgScriptEntry);
  156. free(gpGlobals->g.lprgStore);
  157. free(gpGlobals->g.lprgEnemy);
  158. free(gpGlobals->g.lprgEnemyTeam);
  159. free(gpGlobals->g.lprgMagic);
  160. free(gpGlobals->g.lprgBattleField);
  161. free(gpGlobals->g.lprgLevelUpMagic);
  162. //
  163. // Free the object description data
  164. //
  165. #ifndef PAL_WIN95
  166. PAL_FreeObjectDesc(gpGlobals->lpObjectDesc);
  167. #endif
  168. //
  169. // Delete the instance
  170. //
  171. free(gpGlobals);
  172. }
  173. gpGlobals = NULL;
  174. }
  175. static VOID
  176. PAL_ReadGlobalGameData(
  177. VOID
  178. )
  179. /*++
  180. Purpose:
  181. Read global game data from data files.
  182. Parameters:
  183. None.
  184. Return value:
  185. None.
  186. --*/
  187. {
  188. const GAMEDATA *p = &gpGlobals->g;
  189. unsigned int i;
  190. LOAD_DATA(p->lprgScriptEntry, p->nScriptEntry * sizeof(SCRIPTENTRY),
  191. 4, gpGlobals->f.fpSSS);
  192. LOAD_DATA(p->lprgStore, p->nStore * sizeof(STORE), 0, gpGlobals->f.fpDATA);
  193. LOAD_DATA(p->lprgEnemy, p->nEnemy * sizeof(ENEMY), 1, gpGlobals->f.fpDATA);
  194. LOAD_DATA(p->lprgEnemyTeam, p->nEnemyTeam * sizeof(ENEMYTEAM),
  195. 2, gpGlobals->f.fpDATA);
  196. LOAD_DATA(p->lprgMagic, p->nMagic * sizeof(MAGIC), 4, gpGlobals->f.fpDATA);
  197. LOAD_DATA(p->lprgBattleField, p->nBattleField * sizeof(BATTLEFIELD),
  198. 5, gpGlobals->f.fpDATA);
  199. LOAD_DATA(p->lprgLevelUpMagic, p->nLevelUpMagic * sizeof(LEVELUPMAGIC_ALL),
  200. 6, gpGlobals->f.fpDATA);
  201. LOAD_DATA(p->rgwBattleEffectIndex, sizeof(p->rgwBattleEffectIndex),
  202. 11, gpGlobals->f.fpDATA);
  203. PAL_MKFReadChunk((LPBYTE)&(p->EnemyPos), sizeof(p->EnemyPos),
  204. 13, gpGlobals->f.fpDATA);
  205. DO_BYTESWAP(&(p->EnemyPos), sizeof(p->EnemyPos));
  206. PAL_MKFReadChunk((LPBYTE)(p->rgLevelUpExp), sizeof(p->rgLevelUpExp),
  207. 14, gpGlobals->f.fpDATA);
  208. DO_BYTESWAP(p->rgLevelUpExp, sizeof(p->rgLevelUpExp));
  209. }
  210. static VOID
  211. PAL_InitGlobalGameData(
  212. VOID
  213. )
  214. /*++
  215. Purpose:
  216. Initialize global game data.
  217. Parameters:
  218. None.
  219. Return value:
  220. None.
  221. --*/
  222. {
  223. int len;
  224. #define PAL_DOALLOCATE(fp, num, type, lptype, ptr, n) \
  225. { \
  226. len = PAL_MKFGetChunkSize(num, fp); \
  227. ptr = (lptype)malloc(len); \
  228. n = len / sizeof(type); \
  229. if (ptr == NULL) \
  230. { \
  231. TerminateOnError("PAL_InitGlobalGameData(): Memory allocation error!"); \
  232. } \
  233. }
  234. //
  235. // If the memory has not been allocated, allocate first.
  236. //
  237. if (gpGlobals->g.lprgEventObject == NULL)
  238. {
  239. PAL_DOALLOCATE(gpGlobals->f.fpSSS, 0, EVENTOBJECT, LPEVENTOBJECT,
  240. gpGlobals->g.lprgEventObject, gpGlobals->g.nEventObject);
  241. PAL_DOALLOCATE(gpGlobals->f.fpSSS, 4, SCRIPTENTRY, LPSCRIPTENTRY,
  242. gpGlobals->g.lprgScriptEntry, gpGlobals->g.nScriptEntry);
  243. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 0, STORE, LPSTORE,
  244. gpGlobals->g.lprgStore, gpGlobals->g.nStore);
  245. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 1, ENEMY, LPENEMY,
  246. gpGlobals->g.lprgEnemy, gpGlobals->g.nEnemy);
  247. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 2, ENEMYTEAM, LPENEMYTEAM,
  248. gpGlobals->g.lprgEnemyTeam, gpGlobals->g.nEnemyTeam);
  249. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 4, MAGIC, LPMAGIC,
  250. gpGlobals->g.lprgMagic, gpGlobals->g.nMagic);
  251. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 5, BATTLEFIELD, LPBATTLEFIELD,
  252. gpGlobals->g.lprgBattleField, gpGlobals->g.nBattleField);
  253. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 6, LEVELUPMAGIC_ALL, LPLEVELUPMAGIC_ALL,
  254. gpGlobals->g.lprgLevelUpMagic, gpGlobals->g.nLevelUpMagic);
  255. PAL_ReadGlobalGameData();
  256. }
  257. #undef PAL_DOALLOCATE
  258. }
  259. static VOID
  260. PAL_LoadDefaultGame(
  261. VOID
  262. )
  263. /*++
  264. Purpose:
  265. Load the default game data.
  266. Parameters:
  267. None.
  268. Return value:
  269. None.
  270. --*/
  271. {
  272. const GAMEDATA *p = &gpGlobals->g;
  273. UINT32 i;
  274. //
  275. // Load the default data from the game data files.
  276. //
  277. LOAD_DATA(p->lprgEventObject, p->nEventObject * sizeof(EVENTOBJECT),
  278. 0, gpGlobals->f.fpSSS);
  279. PAL_MKFReadChunk((LPBYTE)(p->rgScene), sizeof(p->rgScene), 1, gpGlobals->f.fpSSS);
  280. DO_BYTESWAP(p->rgScene, sizeof(p->rgScene));
  281. PAL_MKFReadChunk((LPBYTE)(p->rgObject), sizeof(p->rgObject), 2, gpGlobals->f.fpSSS);
  282. DO_BYTESWAP(p->rgObject, sizeof(p->rgObject));
  283. PAL_MKFReadChunk((LPBYTE)(&(p->PlayerRoles)), sizeof(PLAYERROLES),
  284. 3, gpGlobals->f.fpDATA);
  285. DO_BYTESWAP(&(p->PlayerRoles), sizeof(PLAYERROLES));
  286. //
  287. // Set some other default data.
  288. //
  289. gpGlobals->dwCash = 0;
  290. gpGlobals->wNumMusic = 0;
  291. gpGlobals->wNumPalette = 0;
  292. gpGlobals->wNumScene = 1;
  293. gpGlobals->wCollectValue = 0;
  294. gpGlobals->fNightPalette = FALSE;
  295. gpGlobals->wMaxPartyMemberIndex = 0;
  296. gpGlobals->viewport = PAL_XY(0, 0);
  297. gpGlobals->wLayer = 0;
  298. gpGlobals->wChaseRange = 1;
  299. #ifndef PAL_CLASSIC
  300. gpGlobals->bBattleSpeed = 2;
  301. #endif
  302. memset(gpGlobals->rgInventory, 0, sizeof(gpGlobals->rgInventory));
  303. memset(gpGlobals->rgPoisonStatus, 0, sizeof(gpGlobals->rgPoisonStatus));
  304. memset(gpGlobals->rgParty, 0, sizeof(gpGlobals->rgParty));
  305. memset(gpGlobals->rgTrail, 0, sizeof(gpGlobals->rgTrail));
  306. memset(&(gpGlobals->Exp), 0, sizeof(gpGlobals->Exp));
  307. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  308. {
  309. gpGlobals->Exp.rgPrimaryExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  310. gpGlobals->Exp.rgHealthExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  311. gpGlobals->Exp.rgMagicExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  312. gpGlobals->Exp.rgAttackExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  313. gpGlobals->Exp.rgMagicPowerExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  314. gpGlobals->Exp.rgDefenseExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  315. gpGlobals->Exp.rgDexterityExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  316. gpGlobals->Exp.rgFleeExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  317. }
  318. gpGlobals->fEnteringScene = TRUE;
  319. }
  320. static INT
  321. PAL_LoadGame(
  322. LPCSTR szFileName
  323. )
  324. /*++
  325. Purpose:
  326. Load a saved game.
  327. Parameters:
  328. [IN] szFileName - file name of saved game.
  329. Return value:
  330. 0 if success, -1 if failed.
  331. --*/
  332. {
  333. FILE *fp;
  334. PAL_LARGE SAVEDGAME s;
  335. UINT32 i;
  336. //
  337. // Try to open the specified file
  338. //
  339. fp = fopen(szFileName, "rb");
  340. if (fp == NULL)
  341. {
  342. return -1;
  343. }
  344. //
  345. // Read all data from the file and close.
  346. //
  347. fread(&s, sizeof(SAVEDGAME), 1, fp);
  348. fclose(fp);
  349. //
  350. // Adjust endianness
  351. //
  352. DO_BYTESWAP(&s, sizeof(SAVEDGAME));
  353. //
  354. // Cash amount is in DWORD, so do a wordswap in Big-Endian.
  355. //
  356. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  357. s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
  358. #endif
  359. //
  360. // Get all the data from the saved game struct.
  361. //
  362. gpGlobals->viewport = PAL_XY(s.wViewportX, s.wViewportY);
  363. gpGlobals->wMaxPartyMemberIndex = s.nPartyMember;
  364. gpGlobals->wNumScene = s.wNumScene;
  365. gpGlobals->fNightPalette = (s.wPaletteOffset != 0);
  366. gpGlobals->wPartyDirection = s.wPartyDirection;
  367. gpGlobals->wNumMusic = s.wNumMusic;
  368. gpGlobals->wNumBattleMusic = s.wNumBattleMusic;
  369. gpGlobals->wNumBattleField = s.wNumBattleField;
  370. gpGlobals->wScreenWave = s.wScreenWave;
  371. gpGlobals->sWaveProgression = 0;
  372. gpGlobals->wCollectValue = s.wCollectValue;
  373. gpGlobals->wLayer = s.wLayer;
  374. gpGlobals->wChaseRange = s.wChaseRange;
  375. gpGlobals->wChasespeedChangeCycles = s.wChasespeedChangeCycles;
  376. gpGlobals->nFollower = s.nFollower;
  377. gpGlobals->dwCash = s.dwCash;
  378. #ifndef PAL_CLASSIC
  379. gpGlobals->bBattleSpeed = s.wBattleSpeed;
  380. if (gpGlobals->bBattleSpeed > 5 || gpGlobals->bBattleSpeed == 0)
  381. {
  382. gpGlobals->bBattleSpeed = 2;
  383. }
  384. #endif
  385. memcpy(gpGlobals->rgParty, s.rgParty, sizeof(gpGlobals->rgParty));
  386. memcpy(gpGlobals->rgTrail, s.rgTrail, sizeof(gpGlobals->rgTrail));
  387. gpGlobals->Exp = s.Exp;
  388. gpGlobals->g.PlayerRoles = s.PlayerRoles;
  389. memset(gpGlobals->rgPoisonStatus, 0, sizeof(gpGlobals->rgPoisonStatus));
  390. memcpy(gpGlobals->rgInventory, s.rgInventory, sizeof(gpGlobals->rgInventory));
  391. memcpy(gpGlobals->g.rgScene, s.rgScene, sizeof(gpGlobals->g.rgScene));
  392. memcpy(gpGlobals->g.rgObject, s.rgObject, sizeof(gpGlobals->g.rgObject));
  393. memcpy(gpGlobals->g.lprgEventObject, s.rgEventObject,
  394. sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  395. gpGlobals->fEnteringScene = FALSE;
  396. PAL_CompressInventory();
  397. //
  398. // Success
  399. //
  400. return 0;
  401. }
  402. VOID
  403. PAL_SaveGame(
  404. LPCSTR szFileName,
  405. WORD wSavedTimes
  406. )
  407. /*++
  408. Purpose:
  409. Save the current game state to file.
  410. Parameters:
  411. [IN] szFileName - file name of saved game.
  412. Return value:
  413. None.
  414. --*/
  415. {
  416. FILE *fp;
  417. PAL_LARGE SAVEDGAME s;
  418. UINT32 i;
  419. //
  420. // Put all the data to the saved game struct.
  421. //
  422. s.wViewportX = PAL_X(gpGlobals->viewport);
  423. s.wViewportY = PAL_Y(gpGlobals->viewport);
  424. s.nPartyMember = gpGlobals->wMaxPartyMemberIndex;
  425. s.wNumScene = gpGlobals->wNumScene;
  426. s.wPaletteOffset = (gpGlobals->fNightPalette ? 0x180 : 0);
  427. s.wPartyDirection = gpGlobals->wPartyDirection;
  428. s.wNumMusic = gpGlobals->wNumMusic;
  429. s.wNumBattleMusic = gpGlobals->wNumBattleMusic;
  430. s.wNumBattleField = gpGlobals->wNumBattleField;
  431. s.wScreenWave = gpGlobals->wScreenWave;
  432. s.wCollectValue = gpGlobals->wCollectValue;
  433. s.wLayer = gpGlobals->wLayer;
  434. s.wChaseRange = gpGlobals->wChaseRange;
  435. s.wChasespeedChangeCycles = gpGlobals->wChasespeedChangeCycles;
  436. s.nFollower = gpGlobals->nFollower;
  437. s.dwCash = gpGlobals->dwCash;
  438. #ifndef PAL_CLASSIC
  439. s.wBattleSpeed = gpGlobals->bBattleSpeed;
  440. #else
  441. s.wBattleSpeed = 2;
  442. #endif
  443. memcpy(s.rgParty, gpGlobals->rgParty, sizeof(gpGlobals->rgParty));
  444. memcpy(s.rgTrail, gpGlobals->rgTrail, sizeof(gpGlobals->rgTrail));
  445. s.Exp = gpGlobals->Exp;
  446. s.PlayerRoles = gpGlobals->g.PlayerRoles;
  447. memcpy(s.rgPoisonStatus, gpGlobals->rgPoisonStatus, sizeof(gpGlobals->rgPoisonStatus));
  448. memcpy(s.rgInventory, gpGlobals->rgInventory, sizeof(gpGlobals->rgInventory));
  449. memcpy(s.rgScene, gpGlobals->g.rgScene, sizeof(gpGlobals->g.rgScene));
  450. memcpy(s.rgObject, gpGlobals->g.rgObject, sizeof(gpGlobals->g.rgObject));
  451. memcpy(s.rgEventObject, gpGlobals->g.lprgEventObject,
  452. sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  453. s.wSavedTimes = wSavedTimes;
  454. //
  455. // Adjust endianness
  456. //
  457. DO_BYTESWAP(&s, sizeof(SAVEDGAME));
  458. //
  459. // Cash amount is in DWORD, so do a wordswap in Big-Endian.
  460. //
  461. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  462. s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
  463. #endif
  464. //
  465. // Try writing to file
  466. //
  467. fp = fopen(szFileName, "wb");
  468. if (fp == NULL)
  469. {
  470. return;
  471. }
  472. i = PAL_MKFGetChunkSize(0, gpGlobals->f.fpSSS);
  473. i += sizeof(SAVEDGAME) - sizeof(EVENTOBJECT) * MAX_EVENT_OBJECTS;
  474. fwrite(&s, i, 1, fp);
  475. fclose(fp);
  476. }
  477. VOID
  478. PAL_InitGameData(
  479. INT iSaveSlot
  480. )
  481. /*++
  482. Purpose:
  483. Initialize the game data (used when starting a new game or loading a saved game).
  484. Parameters:
  485. [IN] iSaveSlot - Slot of saved game.
  486. Return value:
  487. None.
  488. --*/
  489. {
  490. PAL_InitGlobalGameData();
  491. gpGlobals->bCurrentSaveSlot = (BYTE)iSaveSlot;
  492. //
  493. // try loading from the saved game file.
  494. //
  495. if (iSaveSlot == 0 || PAL_LoadGame(va("%s%d%s", PAL_SAVE_PREFIX, iSaveSlot, ".rpg")) != 0)
  496. {
  497. //
  498. // Cannot load the saved game file. Load the defaults.
  499. //
  500. PAL_LoadDefaultGame();
  501. }
  502. gpGlobals->fGameStart = TRUE;
  503. gpGlobals->fNeedToFadeIn = FALSE;
  504. gpGlobals->iCurInvMenuItem = 0;
  505. gpGlobals->fInBattle = FALSE;
  506. memset(gpGlobals->rgPlayerStatus, 0, sizeof(gpGlobals->rgPlayerStatus));
  507. PAL_UpdateEquipments();
  508. }
  509. BOOL
  510. PAL_AddItemToInventory(
  511. WORD wObjectID,
  512. INT iNum
  513. )
  514. /*++
  515. Purpose:
  516. Add or remove the specified kind of item in the inventory.
  517. Parameters:
  518. [IN] wObjectID - object number of the item.
  519. [IN] iNum - number to be added (positive value) or removed (negative value).
  520. Return value:
  521. TRUE if succeeded, FALSE if failed.
  522. --*/
  523. {
  524. int index;
  525. BOOL fFound;
  526. if (wObjectID == 0)
  527. {
  528. return FALSE;
  529. }
  530. if (iNum == 0)
  531. {
  532. iNum = 1;
  533. }
  534. index = 0;
  535. fFound = FALSE;
  536. //
  537. // Search for the specified item in the inventory
  538. //
  539. while (index < MAX_INVENTORY)
  540. {
  541. if (gpGlobals->rgInventory[index].wItem == wObjectID)
  542. {
  543. fFound = TRUE;
  544. break;
  545. }
  546. else if (gpGlobals->rgInventory[index].wItem == 0)
  547. {
  548. break;
  549. }
  550. index++;
  551. }
  552. if (iNum > 0)
  553. {
  554. //
  555. // Add item
  556. //
  557. if (index >= MAX_INVENTORY)
  558. {
  559. //
  560. // inventory is full. cannot add item
  561. //
  562. return FALSE;
  563. }
  564. if (fFound)
  565. {
  566. gpGlobals->rgInventory[index].nAmount += iNum;
  567. if (gpGlobals->rgInventory[index].nAmount > 99)
  568. {
  569. //
  570. // Maximum number is 99
  571. //
  572. gpGlobals->rgInventory[index].nAmount = 99;
  573. }
  574. }
  575. else
  576. {
  577. gpGlobals->rgInventory[index].wItem = wObjectID;
  578. if (iNum > 99)
  579. {
  580. iNum = 99;
  581. }
  582. gpGlobals->rgInventory[index].nAmount = iNum;
  583. }
  584. return TRUE;
  585. }
  586. else
  587. {
  588. //
  589. // Remove item
  590. //
  591. if (fFound)
  592. {
  593. iNum *= -1;
  594. if (gpGlobals->rgInventory[index].nAmount < iNum)
  595. {
  596. //
  597. // This item has been run out
  598. //
  599. gpGlobals->rgInventory[index].nAmount = 0;
  600. return FALSE;
  601. }
  602. gpGlobals->rgInventory[index].nAmount -= iNum;
  603. return TRUE;
  604. }
  605. return FALSE;
  606. }
  607. }
  608. INT
  609. PAL_GetItemAmount(
  610. WORD wItem
  611. )
  612. /*++
  613. Purpose:
  614. Get the amount of the specified item in the inventory.
  615. Parameters:
  616. [IN] wItem - the object ID of the item.
  617. Return value:
  618. The amount of the item in the inventory.
  619. --*/
  620. {
  621. int i;
  622. for (i = 0; i < MAX_INVENTORY; i++)
  623. {
  624. if (gpGlobals->rgInventory[i].wItem == 0)
  625. {
  626. break;
  627. }
  628. if (gpGlobals->rgInventory[i].wItem == wItem)
  629. {
  630. return gpGlobals->rgInventory[i].nAmount;
  631. }
  632. }
  633. return 0;
  634. }
  635. VOID
  636. PAL_CompressInventory(
  637. VOID
  638. )
  639. /*++
  640. Purpose:
  641. Remove all the items in inventory which has a number of zero.
  642. Parameters:
  643. None.
  644. Return value:
  645. None.
  646. --*/
  647. {
  648. int i, j;
  649. j = 0;
  650. for (i = 0; i < MAX_INVENTORY; i++)
  651. {
  652. if (gpGlobals->rgInventory[i].wItem == 0)
  653. {
  654. break;
  655. }
  656. if (gpGlobals->rgInventory[i].nAmount > 0)
  657. {
  658. gpGlobals->rgInventory[j] = gpGlobals->rgInventory[i];
  659. j++;
  660. }
  661. }
  662. for (; j < MAX_INVENTORY; j++)
  663. {
  664. gpGlobals->rgInventory[j].nAmount = 0;
  665. gpGlobals->rgInventory[j].nAmountInUse = 0;
  666. gpGlobals->rgInventory[j].wItem = 0;
  667. }
  668. }
  669. BOOL
  670. PAL_IncreaseHPMP(
  671. WORD wPlayerRole,
  672. SHORT sHP,
  673. SHORT sMP
  674. )
  675. /*++
  676. Purpose:
  677. Increase or decrease player's HP and/or MP.
  678. Parameters:
  679. [IN] wPlayerRole - the number of player role.
  680. [IN] sHP - number of HP to be increased (positive value) or decrased
  681. (negative value).
  682. [IN] sMP - number of MP to be increased (positive value) or decrased
  683. (negative value).
  684. Return value:
  685. TRUE if the operation is succeeded, FALSE if not.
  686. --*/
  687. {
  688. BOOL fSuccess = FALSE;
  689. //
  690. // Only care about alive players
  691. //
  692. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  693. {
  694. //
  695. // change HP
  696. //
  697. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] += sHP;
  698. if ((SHORT)(gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole]) < 0)
  699. {
  700. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] = 0;
  701. }
  702. else if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] >
  703. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole])
  704. {
  705. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] =
  706. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole];
  707. }
  708. //
  709. // Change MP
  710. //
  711. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] += sMP;
  712. if ((SHORT)(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole]) < 0)
  713. {
  714. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] = 0;
  715. }
  716. else if (gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] >
  717. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole])
  718. {
  719. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] =
  720. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole];
  721. }
  722. fSuccess = TRUE;
  723. }
  724. return fSuccess;
  725. }
  726. VOID
  727. PAL_UpdateEquipments(
  728. VOID
  729. )
  730. /*++
  731. Purpose:
  732. Update the effects of all equipped items for all players.
  733. Parameters:
  734. None.
  735. Return value:
  736. None.
  737. --*/
  738. {
  739. int i, j;
  740. WORD w;
  741. memset(&(gpGlobals->rgEquipmentEffect), 0, sizeof(gpGlobals->rgEquipmentEffect));
  742. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  743. {
  744. for (j = 0; j < MAX_PLAYER_EQUIPMENTS; j++)
  745. {
  746. w = gpGlobals->g.PlayerRoles.rgwEquipment[j][i];
  747. if (w != 0)
  748. {
  749. gpGlobals->g.rgObject[w].item.wScriptOnEquip =
  750. PAL_RunTriggerScript(gpGlobals->g.rgObject[w].item.wScriptOnEquip, (WORD)i);
  751. }
  752. }
  753. }
  754. }
  755. VOID
  756. PAL_RemoveEquipmentEffect(
  757. WORD wPlayerRole,
  758. WORD wEquipPart
  759. )
  760. /*++
  761. Purpose:
  762. Remove all the effects of the equipment for the player.
  763. Parameters:
  764. [IN] wPlayerRole - the player role.
  765. [IN] wEquipPart - the part of the equipment.
  766. Return value:
  767. None.
  768. --*/
  769. {
  770. WORD *p;
  771. int i, j;
  772. p = (WORD *)(&gpGlobals->rgEquipmentEffect[wEquipPart]); // HACKHACK
  773. for (i = 0; i < sizeof(PLAYERROLES) / sizeof(PLAYERS); i++)
  774. {
  775. p[i * MAX_PLAYER_ROLES + wPlayerRole] = 0;
  776. }
  777. //
  778. // Reset some parameters to default when appropriate
  779. //
  780. if (wEquipPart == kBodyPartHand)
  781. {
  782. //
  783. // reset the dual attack status
  784. //
  785. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusDualAttack] = 0;
  786. }
  787. else if (wEquipPart == kBodyPartWear)
  788. {
  789. //
  790. // Remove all poisons leveled 99
  791. //
  792. for (i = 0; i <= (short)gpGlobals->wMaxPartyMemberIndex; i++)
  793. {
  794. if (gpGlobals->rgParty[i].wPlayerRole == wPlayerRole)
  795. {
  796. wPlayerRole = i;
  797. break;
  798. }
  799. }
  800. if (i <= (short)gpGlobals->wMaxPartyMemberIndex)
  801. {
  802. j = 0;
  803. for (i = 0; i < MAX_POISONS; i++)
  804. {
  805. WORD w = gpGlobals->rgPoisonStatus[i][wPlayerRole].wPoisonID;
  806. if (w == 0)
  807. {
  808. break;
  809. }
  810. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel < 99)
  811. {
  812. gpGlobals->rgPoisonStatus[j][wPlayerRole] =
  813. gpGlobals->rgPoisonStatus[i][wPlayerRole];
  814. j++;
  815. }
  816. }
  817. while (j < MAX_POISONS)
  818. {
  819. gpGlobals->rgPoisonStatus[j][wPlayerRole].wPoisonID = 0;
  820. gpGlobals->rgPoisonStatus[j][wPlayerRole].wPoisonScript = 0;
  821. j++;
  822. }
  823. }
  824. }
  825. }
  826. VOID
  827. PAL_AddPoisonForPlayer(
  828. WORD wPlayerRole,
  829. WORD wPoisonID
  830. )
  831. /*++
  832. Purpose:
  833. Add the specified poison to the player.
  834. Parameters:
  835. [IN] wPlayerRole - the player role ID.
  836. [IN] wPoisonID - the poison to be added.
  837. Return value:
  838. None.
  839. --*/
  840. {
  841. int i, index;
  842. WORD w;
  843. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  844. {
  845. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  846. {
  847. break;
  848. }
  849. }
  850. if (index > gpGlobals->wMaxPartyMemberIndex)
  851. {
  852. return; // don't go further
  853. }
  854. for (i = 0; i < MAX_POISONS; i++)
  855. {
  856. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  857. if (w == 0)
  858. {
  859. break;
  860. }
  861. if (w == wPoisonID)
  862. {
  863. return; // already poisoned
  864. }
  865. }
  866. if (i < MAX_POISONS)
  867. {
  868. gpGlobals->rgPoisonStatus[i][index].wPoisonID = wPoisonID;
  869. gpGlobals->rgPoisonStatus[i][index].wPoisonScript =
  870. gpGlobals->g.rgObject[wPoisonID].poison.wPlayerScript;
  871. }
  872. }
  873. VOID
  874. PAL_CurePoisonByKind(
  875. WORD wPlayerRole,
  876. WORD wPoisonID
  877. )
  878. /*++
  879. Purpose:
  880. Remove the specified poison from the player.
  881. Parameters:
  882. [IN] wPlayerRole - the player role ID.
  883. [IN] wPoisonID - the poison to be removed.
  884. Return value:
  885. None.
  886. --*/
  887. {
  888. int i, index;
  889. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  890. {
  891. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  892. {
  893. break;
  894. }
  895. }
  896. if (index > gpGlobals->wMaxPartyMemberIndex)
  897. {
  898. return; // don't go further
  899. }
  900. for (i = 0; i < MAX_POISONS; i++)
  901. {
  902. if (gpGlobals->rgPoisonStatus[i][index].wPoisonID == wPoisonID)
  903. {
  904. gpGlobals->rgPoisonStatus[i][index].wPoisonID = 0;
  905. gpGlobals->rgPoisonStatus[i][index].wPoisonScript = 0;
  906. }
  907. }
  908. }
  909. VOID
  910. PAL_CurePoisonByLevel(
  911. WORD wPlayerRole,
  912. WORD wMaxLevel
  913. )
  914. /*++
  915. Purpose:
  916. Remove the poisons which have a maximum level of wMaxLevel from the player.
  917. Parameters:
  918. [IN] wPlayerRole - the player role ID.
  919. [IN] wMaxLevel - the maximum level of poisons to be removed.
  920. Return value:
  921. None.
  922. --*/
  923. {
  924. int i, index;
  925. WORD w;
  926. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  927. {
  928. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  929. {
  930. break;
  931. }
  932. }
  933. if (index > gpGlobals->wMaxPartyMemberIndex)
  934. {
  935. return; // don't go further
  936. }
  937. for (i = 0; i < MAX_POISONS; i++)
  938. {
  939. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  940. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel <= wMaxLevel)
  941. {
  942. gpGlobals->rgPoisonStatus[i][index].wPoisonID = 0;
  943. gpGlobals->rgPoisonStatus[i][index].wPoisonScript = 0;
  944. }
  945. }
  946. }
  947. BOOL
  948. PAL_IsPlayerPoisonedByLevel(
  949. WORD wPlayerRole,
  950. WORD wMinLevel
  951. )
  952. /*++
  953. Purpose:
  954. Check if the player is poisoned by poisons at a minimum level of wMinLevel.
  955. Parameters:
  956. [IN] wPlayerRole - the player role ID.
  957. [IN] wMinLevel - the minimum level of poison.
  958. Return value:
  959. TRUE if the player is poisoned by poisons at a minimum level of wMinLevel;
  960. FALSE if not.
  961. --*/
  962. {
  963. int i, index;
  964. WORD w;
  965. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  966. {
  967. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  968. {
  969. break;
  970. }
  971. }
  972. if (index > gpGlobals->wMaxPartyMemberIndex)
  973. {
  974. return FALSE; // don't go further
  975. }
  976. for (i = 0; i < MAX_POISONS; i++)
  977. {
  978. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  979. w = gpGlobals->g.rgObject[w].poison.wPoisonLevel;
  980. if (w >= 99)
  981. {
  982. //
  983. // Ignore poisons which has a level of 99 (usually effect of equipment)
  984. //
  985. continue;
  986. }
  987. if (w >= wMinLevel)
  988. {
  989. return TRUE;
  990. }
  991. }
  992. return FALSE;
  993. }
  994. BOOL
  995. PAL_IsPlayerPoisonedByKind(
  996. WORD wPlayerRole,
  997. WORD wPoisonID
  998. )
  999. /*++
  1000. Purpose:
  1001. Check if the player is poisoned by the specified poison.
  1002. Parameters:
  1003. [IN] wPlayerRole - the player role ID.
  1004. [IN] wPoisonID - the poison to be checked.
  1005. Return value:
  1006. TRUE if player is poisoned by the specified poison;
  1007. FALSE if not.
  1008. --*/
  1009. {
  1010. int i, index;
  1011. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1012. {
  1013. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1014. {
  1015. break;
  1016. }
  1017. }
  1018. if (index > gpGlobals->wMaxPartyMemberIndex)
  1019. {
  1020. return FALSE; // don't go further
  1021. }
  1022. for (i = 0; i < MAX_POISONS; i++)
  1023. {
  1024. if (gpGlobals->rgPoisonStatus[i][index].wPoisonID == wPoisonID)
  1025. {
  1026. return TRUE;
  1027. }
  1028. }
  1029. return FALSE;
  1030. }
  1031. WORD
  1032. PAL_GetPlayerAttackStrength(
  1033. WORD wPlayerRole
  1034. )
  1035. /*++
  1036. Purpose:
  1037. Get the player's attack strength, count in the effect of equipments.
  1038. Parameters:
  1039. [IN] wPlayerRole - the player role ID.
  1040. Return value:
  1041. The total attack strength of the player.
  1042. --*/
  1043. {
  1044. WORD w;
  1045. int i;
  1046. w = gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole];
  1047. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1048. {
  1049. w += gpGlobals->rgEquipmentEffect[i].rgwAttackStrength[wPlayerRole];
  1050. }
  1051. return w;
  1052. }
  1053. WORD
  1054. PAL_GetPlayerMagicStrength(
  1055. WORD wPlayerRole
  1056. )
  1057. /*++
  1058. Purpose:
  1059. Get the player's magic strength, count in the effect of equipments.
  1060. Parameters:
  1061. [IN] wPlayerRole - the player role ID.
  1062. Return value:
  1063. The total magic strength of the player.
  1064. --*/
  1065. {
  1066. WORD w;
  1067. int i;
  1068. w = gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole];
  1069. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1070. {
  1071. w += gpGlobals->rgEquipmentEffect[i].rgwMagicStrength[wPlayerRole];
  1072. }
  1073. return w;
  1074. }
  1075. WORD
  1076. PAL_GetPlayerDefense(
  1077. WORD wPlayerRole
  1078. )
  1079. /*++
  1080. Purpose:
  1081. Get the player's defense value, count in the effect of equipments.
  1082. Parameters:
  1083. [IN] wPlayerRole - the player role ID.
  1084. Return value:
  1085. The total defense value of the player.
  1086. --*/
  1087. {
  1088. WORD w;
  1089. int i;
  1090. w = gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole];
  1091. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1092. {
  1093. w += gpGlobals->rgEquipmentEffect[i].rgwDefense[wPlayerRole];
  1094. }
  1095. return w;
  1096. }
  1097. WORD
  1098. PAL_GetPlayerDexterity(
  1099. WORD wPlayerRole
  1100. )
  1101. /*++
  1102. Purpose:
  1103. Get the player's dexterity, count in the effect of equipments.
  1104. Parameters:
  1105. [IN] wPlayerRole - the player role ID.
  1106. Return value:
  1107. The total dexterity of the player.
  1108. --*/
  1109. {
  1110. WORD w;
  1111. int i;
  1112. w = gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole];
  1113. #ifdef PAL_CLASSIC
  1114. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1115. #else
  1116. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS - 1; i++)
  1117. #endif
  1118. {
  1119. w += gpGlobals->rgEquipmentEffect[i].rgwDexterity[wPlayerRole];
  1120. }
  1121. return w;
  1122. }
  1123. WORD
  1124. PAL_GetPlayerFleeRate(
  1125. WORD wPlayerRole
  1126. )
  1127. /*++
  1128. Purpose:
  1129. Get the player's flee rate, count in the effect of equipments.
  1130. Parameters:
  1131. [IN] wPlayerRole - the player role ID.
  1132. Return value:
  1133. The total flee rate of the player.
  1134. --*/
  1135. {
  1136. WORD w;
  1137. int i;
  1138. w = gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole];
  1139. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1140. {
  1141. w += gpGlobals->rgEquipmentEffect[i].rgwFleeRate[wPlayerRole];
  1142. }
  1143. return w;
  1144. }
  1145. WORD
  1146. PAL_GetPlayerPoisonResistance(
  1147. WORD wPlayerRole
  1148. )
  1149. /*++
  1150. Purpose:
  1151. Get the player's resistance to poisons, count in the effect of equipments.
  1152. Parameters:
  1153. [IN] wPlayerRole - the player role ID.
  1154. Return value:
  1155. The total resistance to poisons of the player.
  1156. --*/
  1157. {
  1158. WORD w;
  1159. int i;
  1160. w = gpGlobals->g.PlayerRoles.rgwPoisonResistance[wPlayerRole];
  1161. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1162. {
  1163. w += gpGlobals->rgEquipmentEffect[i].rgwPoisonResistance[wPlayerRole];
  1164. }
  1165. if (w > 100)
  1166. {
  1167. w = 100;
  1168. }
  1169. return w;
  1170. }
  1171. WORD
  1172. PAL_GetPlayerElementalResistance(
  1173. WORD wPlayerRole,
  1174. INT iAttrib
  1175. )
  1176. /*++
  1177. Purpose:
  1178. Get the player's resistance to attributed magics, count in the effect
  1179. of equipments.
  1180. Parameters:
  1181. [IN] wPlayerRole - the player role ID.
  1182. [IN] iAttrib - the attribute of magics.
  1183. Return value:
  1184. The total resistance to the attributed magics of the player.
  1185. --*/
  1186. {
  1187. WORD w;
  1188. int i;
  1189. w = gpGlobals->g.PlayerRoles.rgwElementalResistance[iAttrib][wPlayerRole];
  1190. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1191. {
  1192. w += gpGlobals->rgEquipmentEffect[i].rgwElementalResistance[iAttrib][wPlayerRole];
  1193. }
  1194. if (w > 100)
  1195. {
  1196. w = 100;
  1197. }
  1198. return w;
  1199. }
  1200. WORD
  1201. PAL_GetPlayerBattleSprite(
  1202. WORD wPlayerRole
  1203. )
  1204. /*++
  1205. Purpose:
  1206. Get player's battle sprite.
  1207. Parameters:
  1208. [IN] wPlayerRole - the player role ID.
  1209. Return value:
  1210. Number of the player's battle sprite.
  1211. --*/
  1212. {
  1213. int i;
  1214. WORD w;
  1215. w = gpGlobals->g.PlayerRoles.rgwSpriteNumInBattle[wPlayerRole];
  1216. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1217. {
  1218. if (gpGlobals->rgEquipmentEffect[i].rgwSpriteNumInBattle[wPlayerRole] != 0)
  1219. {
  1220. w = gpGlobals->rgEquipmentEffect[i].rgwSpriteNumInBattle[wPlayerRole];
  1221. }
  1222. }
  1223. return w;
  1224. }
  1225. WORD
  1226. PAL_GetPlayerCooperativeMagic(
  1227. WORD wPlayerRole
  1228. )
  1229. /*++
  1230. Purpose:
  1231. Get player's cooperative magic.
  1232. Parameters:
  1233. [IN] wPlayerRole - the player role ID.
  1234. Return value:
  1235. Object ID of the player's cooperative magic.
  1236. --*/
  1237. {
  1238. int i;
  1239. WORD w;
  1240. w = gpGlobals->g.PlayerRoles.rgwCooperativeMagic[wPlayerRole];
  1241. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1242. {
  1243. if (gpGlobals->rgEquipmentEffect[i].rgwCooperativeMagic[wPlayerRole] != 0)
  1244. {
  1245. w = gpGlobals->rgEquipmentEffect[i].rgwCooperativeMagic[wPlayerRole];
  1246. }
  1247. }
  1248. return w;
  1249. }
  1250. BOOL
  1251. PAL_PlayerCanAttackAll(
  1252. WORD wPlayerRole
  1253. )
  1254. /*++
  1255. Purpose:
  1256. Check if the player can attack all of the enemies in one move.
  1257. Parameters:
  1258. [IN] wPlayerRole - the player role ID.
  1259. Return value:
  1260. TRUE if player can attack all of the enemies in one move, FALSE if not.
  1261. --*/
  1262. {
  1263. int i;
  1264. BOOL f;
  1265. f = FALSE;
  1266. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1267. {
  1268. if (gpGlobals->rgEquipmentEffect[i].rgwAttackAll[wPlayerRole] != 0)
  1269. {
  1270. f = TRUE;
  1271. break;
  1272. }
  1273. }
  1274. return f;
  1275. }
  1276. BOOL
  1277. PAL_AddMagic(
  1278. WORD wPlayerRole,
  1279. WORD wMagic
  1280. )
  1281. /*++
  1282. Purpose:
  1283. Add a magic to the player.
  1284. Parameters:
  1285. [IN] wPlayerRole - the player role ID.
  1286. [IN] wMagic - the object ID of the magic.
  1287. Return value:
  1288. TRUE if succeeded, FALSE if failed.
  1289. --*/
  1290. {
  1291. int i;
  1292. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1293. {
  1294. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == wMagic)
  1295. {
  1296. //
  1297. // already have this magic
  1298. //
  1299. return FALSE;
  1300. }
  1301. }
  1302. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1303. {
  1304. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == 0)
  1305. {
  1306. break;
  1307. }
  1308. }
  1309. if (i >= MAX_PLAYER_MAGICS)
  1310. {
  1311. //
  1312. // Not enough slots
  1313. //
  1314. return FALSE;
  1315. }
  1316. gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] = wMagic;
  1317. return TRUE;
  1318. }
  1319. VOID
  1320. PAL_RemoveMagic(
  1321. WORD wPlayerRole,
  1322. WORD wMagic
  1323. )
  1324. /*++
  1325. Purpose:
  1326. Remove a magic to the player.
  1327. Parameters:
  1328. [IN] wPlayerRole - the player role ID.
  1329. [IN] wMagic - the object ID of the magic.
  1330. Return value:
  1331. None.
  1332. --*/
  1333. {
  1334. int i;
  1335. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1336. {
  1337. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == wMagic)
  1338. {
  1339. gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] = 0;
  1340. break;
  1341. }
  1342. }
  1343. }
  1344. VOID
  1345. PAL_SetPlayerStatus(
  1346. WORD wPlayerRole,
  1347. WORD wStatusID,
  1348. WORD wNumRound
  1349. )
  1350. /*++
  1351. Purpose:
  1352. Set one of the statuses for the player.
  1353. Parameters:
  1354. [IN] wPlayerRole - the player ID.
  1355. [IN] wStatusID - the status to be set.
  1356. [IN] wNumRound - the effective rounds of the status.
  1357. Return value:
  1358. None.
  1359. --*/
  1360. {
  1361. #ifndef PAL_CLASSIC
  1362. if (wStatusID == kStatusSlow &&
  1363. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusHaste] > 0)
  1364. {
  1365. //
  1366. // Remove the haste status
  1367. //
  1368. PAL_RemovePlayerStatus(wPlayerRole, kStatusHaste);
  1369. return;
  1370. }
  1371. if (wStatusID == kStatusHaste &&
  1372. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSlow] > 0)
  1373. {
  1374. //
  1375. // Remove the slow status
  1376. //
  1377. PAL_RemovePlayerStatus(wPlayerRole, kStatusSlow);
  1378. return;
  1379. }
  1380. #endif
  1381. switch (wStatusID)
  1382. {
  1383. case kStatusConfused:
  1384. case kStatusSleep:
  1385. case kStatusSilence:
  1386. #ifdef PAL_CLASSIC
  1387. case kStatusParalyzed:
  1388. #else
  1389. case kStatusSlow:
  1390. #endif
  1391. //
  1392. // for "bad" statuses, don't set the status when we already have it
  1393. //
  1394. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] != 0 &&
  1395. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] == 0)
  1396. {
  1397. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1398. }
  1399. break;
  1400. case kStatusPuppet:
  1401. //
  1402. // only allow dead players for "puppet" status
  1403. //
  1404. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 &&
  1405. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] < wNumRound)
  1406. {
  1407. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1408. }
  1409. break;
  1410. case kStatusBravery:
  1411. case kStatusProtect:
  1412. case kStatusDualAttack:
  1413. case kStatusHaste:
  1414. //
  1415. // for "good" statuses, reset the status if the status to be set lasts longer
  1416. //
  1417. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] != 0 &&
  1418. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] < wNumRound)
  1419. {
  1420. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1421. }
  1422. break;
  1423. default:
  1424. assert(FALSE);
  1425. break;
  1426. }
  1427. }
  1428. VOID
  1429. PAL_RemovePlayerStatus(
  1430. WORD wPlayerRole,
  1431. WORD wStatusID
  1432. )
  1433. /*++
  1434. Purpose:
  1435. Remove one of the status for player.
  1436. Parameters:
  1437. [IN] wPlayerRole - the player ID.
  1438. [IN] wStatusID - the status to be set.
  1439. Return value:
  1440. None.
  1441. --*/
  1442. {
  1443. //
  1444. // Don't remove effects of equipments
  1445. //
  1446. if (gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] <= 999)
  1447. {
  1448. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = 0;
  1449. }
  1450. }
  1451. VOID
  1452. PAL_ClearAllPlayerStatus(
  1453. VOID
  1454. )
  1455. /*++
  1456. Purpose:
  1457. Clear all player status.
  1458. Parameters:
  1459. None.
  1460. Return value:
  1461. None.
  1462. --*/
  1463. {
  1464. int i, j;
  1465. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  1466. {
  1467. for (j = 0; j < kStatusAll; j++)
  1468. {
  1469. //
  1470. // Don't remove effects of equipments
  1471. //
  1472. if (gpGlobals->rgPlayerStatus[i][j] <= 999)
  1473. {
  1474. gpGlobals->rgPlayerStatus[i][j] = 0;
  1475. }
  1476. }
  1477. }
  1478. }
  1479. VOID
  1480. PAL_PlayerLevelUp(
  1481. WORD wPlayerRole,
  1482. WORD wNumLevel
  1483. )
  1484. /*++
  1485. Purpose:
  1486. Increase the player's level by wLevels.
  1487. Parameters:
  1488. [IN] wPlayerRole - player role ID.
  1489. [IN] wNumLevel - number of levels to be increased.
  1490. Return value:
  1491. None.
  1492. --*/
  1493. {
  1494. WORD i;
  1495. //
  1496. // Add the level
  1497. //
  1498. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] += wNumLevel;
  1499. if (gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] > MAX_LEVELS)
  1500. {
  1501. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] = MAX_LEVELS;
  1502. }
  1503. for (i = 0; i < wNumLevel; i++)
  1504. {
  1505. //
  1506. // Increase player's stats
  1507. //
  1508. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole] += 10 + RandomLong(0, 8);
  1509. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole] += 8 + RandomLong(0, 6);
  1510. gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole] += 4 + RandomLong(0, 1);
  1511. gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole] += 4 + RandomLong(0, 1);
  1512. gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole] += 2 + RandomLong(0, 1);
  1513. gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole] += 2 + RandomLong(0, 1);
  1514. gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole] += 2;
  1515. }
  1516. #define STAT_LIMIT(t) { if ((t) > 999) (t) = 999; }
  1517. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole]);
  1518. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole]);
  1519. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole]);
  1520. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole]);
  1521. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole]);
  1522. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole]);
  1523. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole]);
  1524. #undef STAT_LIMIT
  1525. //
  1526. // Reset experience points to zero
  1527. //
  1528. gpGlobals->Exp.rgPrimaryExp[wPlayerRole].wExp = 0;
  1529. gpGlobals->Exp.rgPrimaryExp[wPlayerRole].wLevel =
  1530. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole];
  1531. }