global.c 50 KB

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