global.c 51 KB

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