global.c 40 KB

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