global.c 39 KB

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