global.c 50 KB

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