global.c 39 KB

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