global.c 41 KB

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