global.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259
  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("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. p->rgObject[i].rgwData[6] = objects[i].rgwData[5]; // wFlags
  346. p->rgObject[i].rgwData[5] = 0; // wScriptDesc or wReserved2
  347. }
  348. }
  349. PAL_MKFReadChunk((LPBYTE)(&(p->PlayerRoles)), sizeof(PLAYERROLES),
  350. 3, gpGlobals->f.fpDATA);
  351. DO_BYTESWAP(&(p->PlayerRoles), sizeof(PLAYERROLES));
  352. //
  353. // Set some other default data.
  354. //
  355. gpGlobals->dwCash = 0;
  356. gpGlobals->wNumMusic = 0;
  357. gpGlobals->wNumPalette = 0;
  358. gpGlobals->wNumScene = 1;
  359. gpGlobals->wCollectValue = 0;
  360. gpGlobals->fNightPalette = FALSE;
  361. gpGlobals->wMaxPartyMemberIndex = 0;
  362. gpGlobals->viewport = PAL_XY(0, 0);
  363. gpGlobals->wLayer = 0;
  364. gpGlobals->wChaseRange = 1;
  365. #ifndef PAL_CLASSIC
  366. gpGlobals->bBattleSpeed = 2;
  367. #endif
  368. memset(gpGlobals->rgInventory, 0, sizeof(gpGlobals->rgInventory));
  369. memset(gpGlobals->rgPoisonStatus, 0, sizeof(gpGlobals->rgPoisonStatus));
  370. memset(gpGlobals->rgParty, 0, sizeof(gpGlobals->rgParty));
  371. memset(gpGlobals->rgTrail, 0, sizeof(gpGlobals->rgTrail));
  372. memset(&(gpGlobals->Exp), 0, sizeof(gpGlobals->Exp));
  373. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  374. {
  375. gpGlobals->Exp.rgPrimaryExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  376. gpGlobals->Exp.rgHealthExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  377. gpGlobals->Exp.rgMagicExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  378. gpGlobals->Exp.rgAttackExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  379. gpGlobals->Exp.rgMagicPowerExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  380. gpGlobals->Exp.rgDefenseExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  381. gpGlobals->Exp.rgDexterityExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  382. gpGlobals->Exp.rgFleeExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  383. }
  384. gpGlobals->fEnteringScene = TRUE;
  385. }
  386. typedef struct tagSAVEDGAME_COMMON
  387. {
  388. WORD wSavedTimes; // saved times
  389. WORD wViewportX, wViewportY; // viewport location
  390. WORD nPartyMember; // number of members in party
  391. WORD wNumScene; // scene number
  392. WORD wPaletteOffset;
  393. WORD wPartyDirection; // party direction
  394. WORD wNumMusic; // music number
  395. WORD wNumBattleMusic; // battle music number
  396. WORD wNumBattleField; // battle field number
  397. WORD wScreenWave; // level of screen waving
  398. WORD wBattleSpeed; // battle speed
  399. WORD wCollectValue; // value of "collected" items
  400. WORD wLayer;
  401. WORD wChaseRange;
  402. WORD wChasespeedChangeCycles;
  403. WORD nFollower;
  404. WORD rgwReserved2[3]; // unused
  405. DWORD dwCash; // amount of cash
  406. PARTY rgParty[MAX_PLAYABLE_PLAYER_ROLES]; // player party
  407. TRAIL rgTrail[MAX_PLAYABLE_PLAYER_ROLES]; // player trail
  408. ALLEXPERIENCE Exp; // experience data
  409. PLAYERROLES PlayerRoles;
  410. POISONSTATUS rgPoisonStatus[MAX_POISONS][MAX_PLAYABLE_PLAYER_ROLES]; // poison status
  411. INVENTORY rgInventory[MAX_INVENTORY]; // inventory status
  412. SCENE rgScene[MAX_SCENES];
  413. } SAVEDGAME_COMMON, *LPSAVEDGAME_COMMON;
  414. typedef struct tagSAVEDGAME_DOS
  415. {
  416. WORD wSavedTimes; // saved times
  417. WORD wViewportX, wViewportY; // viewport location
  418. WORD nPartyMember; // number of members in party
  419. WORD wNumScene; // scene number
  420. WORD wPaletteOffset;
  421. WORD wPartyDirection; // party direction
  422. WORD wNumMusic; // music number
  423. WORD wNumBattleMusic; // battle music number
  424. WORD wNumBattleField; // battle field number
  425. WORD wScreenWave; // level of screen waving
  426. WORD wBattleSpeed; // battle speed
  427. WORD wCollectValue; // value of "collected" items
  428. WORD wLayer;
  429. WORD wChaseRange;
  430. WORD wChasespeedChangeCycles;
  431. WORD nFollower;
  432. WORD rgwReserved2[3]; // unused
  433. DWORD dwCash; // amount of cash
  434. PARTY rgParty[MAX_PLAYABLE_PLAYER_ROLES]; // player party
  435. TRAIL rgTrail[MAX_PLAYABLE_PLAYER_ROLES]; // player trail
  436. ALLEXPERIENCE Exp; // experience data
  437. PLAYERROLES PlayerRoles;
  438. POISONSTATUS rgPoisonStatus[MAX_POISONS][MAX_PLAYABLE_PLAYER_ROLES]; // poison status
  439. INVENTORY rgInventory[MAX_INVENTORY]; // inventory status
  440. SCENE rgScene[MAX_SCENES];
  441. OBJECT_DOS rgObject[MAX_OBJECTS];
  442. EVENTOBJECT rgEventObject[MAX_EVENT_OBJECTS];
  443. } SAVEDGAME_DOS, *LPSAVEDGAME_DOS;
  444. typedef struct tagSAVEDGAME_WIN
  445. {
  446. WORD wSavedTimes; // saved times
  447. WORD wViewportX, wViewportY; // viewport location
  448. WORD nPartyMember; // number of members in party
  449. WORD wNumScene; // scene number
  450. WORD wPaletteOffset;
  451. WORD wPartyDirection; // party direction
  452. WORD wNumMusic; // music number
  453. WORD wNumBattleMusic; // battle music number
  454. WORD wNumBattleField; // battle field number
  455. WORD wScreenWave; // level of screen waving
  456. WORD wBattleSpeed; // battle speed
  457. WORD wCollectValue; // value of "collected" items
  458. WORD wLayer;
  459. WORD wChaseRange;
  460. WORD wChasespeedChangeCycles;
  461. WORD nFollower;
  462. WORD rgwReserved2[3]; // unused
  463. DWORD dwCash; // amount of cash
  464. PARTY rgParty[MAX_PLAYABLE_PLAYER_ROLES]; // player party
  465. TRAIL rgTrail[MAX_PLAYABLE_PLAYER_ROLES]; // player trail
  466. ALLEXPERIENCE Exp; // experience data
  467. PLAYERROLES PlayerRoles;
  468. POISONSTATUS rgPoisonStatus[MAX_POISONS][MAX_PLAYABLE_PLAYER_ROLES]; // poison status
  469. INVENTORY rgInventory[MAX_INVENTORY]; // inventory status
  470. SCENE rgScene[MAX_SCENES];
  471. OBJECT rgObject[MAX_OBJECTS];
  472. EVENTOBJECT rgEventObject[MAX_EVENT_OBJECTS];
  473. } SAVEDGAME_WIN, *LPSAVEDGAME_WIN;
  474. static BOOL
  475. PAL_LoadGame_Common(
  476. int iSaveSlot,
  477. LPSAVEDGAME_COMMON s,
  478. size_t size
  479. )
  480. {
  481. //
  482. // Try to open the specified file
  483. //
  484. FILE *fp = UTIL_OpenFileAtPath(gConfig.pszSavePath, PAL_va(1, "%d.rpg", iSaveSlot));
  485. //
  486. // Read all data from the file and close.
  487. //
  488. size_t n = fp ? fread(s, 1, size, fp) : 0;
  489. if (fp != NULL)
  490. {
  491. fclose(fp);
  492. }
  493. if (n < size - sizeof(EVENTOBJECT) * MAX_EVENT_OBJECTS)
  494. {
  495. return FALSE;
  496. }
  497. //
  498. // Adjust endianness
  499. //
  500. DO_BYTESWAP(&s, size);
  501. //
  502. // Cash amount is in DWORD, so do a wordswap in Big-Endian.
  503. //
  504. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  505. s->dwCash = ((s->dwCash >> 16) | (s->dwCash << 16));
  506. #endif
  507. //
  508. // Get common data from the saved game struct.
  509. //
  510. gpGlobals->viewport = PAL_XY(s->wViewportX, s->wViewportY);
  511. gpGlobals->wMaxPartyMemberIndex = s->nPartyMember;
  512. gpGlobals->wNumScene = s->wNumScene;
  513. gpGlobals->fNightPalette = (s->wPaletteOffset != 0);
  514. gpGlobals->wPartyDirection = s->wPartyDirection;
  515. gpGlobals->wNumMusic = s->wNumMusic;
  516. gpGlobals->wNumBattleMusic = s->wNumBattleMusic;
  517. gpGlobals->wNumBattleField = s->wNumBattleField;
  518. gpGlobals->wScreenWave = s->wScreenWave;
  519. gpGlobals->sWaveProgression = 0;
  520. gpGlobals->wCollectValue = s->wCollectValue;
  521. gpGlobals->wLayer = s->wLayer;
  522. gpGlobals->wChaseRange = s->wChaseRange;
  523. gpGlobals->wChasespeedChangeCycles = s->wChasespeedChangeCycles;
  524. gpGlobals->nFollower = s->nFollower;
  525. gpGlobals->dwCash = s->dwCash;
  526. #ifndef PAL_CLASSIC
  527. gpGlobals->bBattleSpeed = s->wBattleSpeed;
  528. if (gpGlobals->bBattleSpeed > 5 || gpGlobals->bBattleSpeed == 0)
  529. {
  530. gpGlobals->bBattleSpeed = 2;
  531. }
  532. #endif
  533. memcpy(gpGlobals->rgParty, s->rgParty, sizeof(gpGlobals->rgParty));
  534. memcpy(gpGlobals->rgTrail, s->rgTrail, sizeof(gpGlobals->rgTrail));
  535. gpGlobals->Exp = s->Exp;
  536. gpGlobals->g.PlayerRoles = s->PlayerRoles;
  537. memset(gpGlobals->rgPoisonStatus, 0, sizeof(gpGlobals->rgPoisonStatus));
  538. memcpy(gpGlobals->rgInventory, s->rgInventory, sizeof(gpGlobals->rgInventory));
  539. memcpy(gpGlobals->g.rgScene, s->rgScene, sizeof(gpGlobals->g.rgScene));
  540. gpGlobals->fEnteringScene = FALSE;
  541. PAL_CompressInventory();
  542. return TRUE;
  543. }
  544. static INT
  545. PAL_LoadGame_DOS(
  546. int iSaveSlot
  547. )
  548. /*++
  549. Purpose:
  550. Load a saved game.
  551. Parameters:
  552. [IN] szFileName - file name of saved game.
  553. Return value:
  554. 0 if success, -1 if failed.
  555. --*/
  556. {
  557. PAL_LARGE SAVEDGAME_DOS s;
  558. int i;
  559. //
  560. // Get all the data from the saved game struct.
  561. //
  562. if (!PAL_LoadGame_Common(iSaveSlot, (LPSAVEDGAME_COMMON)&s, sizeof(SAVEDGAME_DOS)))
  563. return -1;
  564. //
  565. // Convert the DOS-style data structure to WIN-style data structure
  566. //
  567. for (i = 0; i < MAX_OBJECTS; i++)
  568. {
  569. memcpy(&gpGlobals->g.rgObject[i], &s.rgObject[i], sizeof(OBJECT_DOS));
  570. gpGlobals->g.rgObject[i].rgwData[6] = s.rgObject[i].rgwData[5]; // wFlags
  571. gpGlobals->g.rgObject[i].rgwData[5] = 0; // wScriptDesc or wReserved2
  572. }
  573. memcpy(gpGlobals->g.lprgEventObject, s.rgEventObject, sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  574. //
  575. // Success
  576. //
  577. return 0;
  578. }
  579. static INT
  580. PAL_LoadGame_WIN(
  581. int iSaveSlot
  582. )
  583. /*++
  584. Purpose:
  585. Load a saved game.
  586. Parameters:
  587. [IN] szFileName - file name of saved game.
  588. Return value:
  589. 0 if success, -1 if failed.
  590. --*/
  591. {
  592. PAL_LARGE SAVEDGAME_WIN s;
  593. //
  594. // Get all the data from the saved game struct.
  595. //
  596. if (!PAL_LoadGame_Common(iSaveSlot, (LPSAVEDGAME_COMMON)&s, sizeof(SAVEDGAME_WIN)))
  597. return -1;
  598. memcpy(gpGlobals->g.rgObject, s.rgObject, sizeof(gpGlobals->g.rgObject));
  599. memcpy(gpGlobals->g.lprgEventObject, s.rgEventObject, sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  600. //
  601. // Success
  602. //
  603. return 0;
  604. }
  605. static INT
  606. PAL_LoadGame(
  607. int iSaveSlot
  608. )
  609. {
  610. return gConfig.fIsWIN95 ? PAL_LoadGame_WIN(iSaveSlot) : PAL_LoadGame_DOS(iSaveSlot);
  611. }
  612. static VOID
  613. PAL_SaveGame_Common(
  614. int iSaveSlot,
  615. WORD wSavedTimes,
  616. LPSAVEDGAME_COMMON s,
  617. size_t size
  618. )
  619. {
  620. FILE *fp;
  621. int i;
  622. s->wSavedTimes = wSavedTimes;
  623. s->wViewportX = PAL_X(gpGlobals->viewport);
  624. s->wViewportY = PAL_Y(gpGlobals->viewport);
  625. s->nPartyMember = gpGlobals->wMaxPartyMemberIndex;
  626. s->wNumScene = gpGlobals->wNumScene;
  627. s->wPaletteOffset = (gpGlobals->fNightPalette ? 0x180 : 0);
  628. s->wPartyDirection = gpGlobals->wPartyDirection;
  629. s->wNumMusic = gpGlobals->wNumMusic;
  630. s->wNumBattleMusic = gpGlobals->wNumBattleMusic;
  631. s->wNumBattleField = gpGlobals->wNumBattleField;
  632. s->wScreenWave = gpGlobals->wScreenWave;
  633. s->wCollectValue = gpGlobals->wCollectValue;
  634. s->wLayer = gpGlobals->wLayer;
  635. s->wChaseRange = gpGlobals->wChaseRange;
  636. s->wChasespeedChangeCycles = gpGlobals->wChasespeedChangeCycles;
  637. s->nFollower = gpGlobals->nFollower;
  638. s->dwCash = gpGlobals->dwCash;
  639. #ifndef PAL_CLASSIC
  640. s->wBattleSpeed = gpGlobals->bBattleSpeed;
  641. #else
  642. s->wBattleSpeed = 2;
  643. #endif
  644. memcpy(s->rgParty, gpGlobals->rgParty, sizeof(gpGlobals->rgParty));
  645. memcpy(s->rgTrail, gpGlobals->rgTrail, sizeof(gpGlobals->rgTrail));
  646. s->Exp = gpGlobals->Exp;
  647. s->PlayerRoles = gpGlobals->g.PlayerRoles;
  648. memcpy(s->rgPoisonStatus, gpGlobals->rgPoisonStatus, sizeof(gpGlobals->rgPoisonStatus));
  649. memcpy(s->rgInventory, gpGlobals->rgInventory, sizeof(gpGlobals->rgInventory));
  650. memcpy(s->rgScene, gpGlobals->g.rgScene, sizeof(gpGlobals->g.rgScene));
  651. //
  652. // Adjust endianness
  653. //
  654. DO_BYTESWAP(&s, size);
  655. //
  656. // Cash amount is in DWORD, so do a wordswap in Big-Endian.
  657. //
  658. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  659. s->dwCash = ((s->dwCash >> 16) | (s->dwCash << 16));
  660. #endif
  661. //
  662. // Try writing to file
  663. //
  664. if ((fp = UTIL_OpenFileAtPathForMode(gConfig.pszSavePath, PAL_va(1, "%d.rpg", iSaveSlot), "wb")) == NULL)
  665. {
  666. return;
  667. }
  668. i = PAL_MKFGetChunkSize(0, gpGlobals->f.fpSSS);
  669. i += size - sizeof(EVENTOBJECT) * MAX_EVENT_OBJECTS;
  670. fwrite(s, i, 1, fp);
  671. fclose(fp);
  672. }
  673. static VOID
  674. PAL_SaveGame_DOS(
  675. int iSaveSlot,
  676. WORD wSavedTimes
  677. )
  678. /*++
  679. Purpose:
  680. Save the current game state to file.
  681. Parameters:
  682. [IN] szFileName - file name of saved game.
  683. Return value:
  684. None.
  685. --*/
  686. {
  687. PAL_LARGE SAVEDGAME_DOS s;
  688. UINT32 i;
  689. //
  690. // Convert the WIN-style data structure to DOS-style data structure
  691. //
  692. for (i = 0; i < MAX_OBJECTS; i++)
  693. {
  694. memcpy(&s.rgObject[i], &gpGlobals->g.rgObject[i], sizeof(OBJECT_DOS));
  695. s.rgObject[i].rgwData[5] = gpGlobals->g.rgObject[i].rgwData[6]; // wFlags
  696. }
  697. memcpy(s.rgEventObject, gpGlobals->g.lprgEventObject, sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  698. //
  699. // Put all the data to the saved game struct.
  700. //
  701. PAL_SaveGame_Common(iSaveSlot, wSavedTimes, (LPSAVEDGAME_COMMON)&s, sizeof(SAVEDGAME_DOS));
  702. }
  703. static VOID
  704. PAL_SaveGame_WIN(
  705. int iSaveSlot,
  706. WORD wSavedTimes
  707. )
  708. /*++
  709. Purpose:
  710. Save the current game state to file.
  711. Parameters:
  712. [IN] szFileName - file name of saved game.
  713. Return value:
  714. None.
  715. --*/
  716. {
  717. PAL_LARGE SAVEDGAME_WIN s;
  718. //
  719. // Put all the data to the saved game struct.
  720. //
  721. memcpy(s.rgObject, gpGlobals->g.rgObject, sizeof(gpGlobals->g.rgObject));
  722. memcpy(s.rgEventObject, gpGlobals->g.lprgEventObject, sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  723. PAL_SaveGame_Common(iSaveSlot, wSavedTimes, (LPSAVEDGAME_COMMON)&s, sizeof(SAVEDGAME_WIN));
  724. }
  725. VOID
  726. PAL_SaveGame(
  727. int iSaveSlot,
  728. WORD wSavedTimes
  729. )
  730. {
  731. if (gConfig.fIsWIN95)
  732. PAL_SaveGame_WIN(iSaveSlot, wSavedTimes);
  733. else
  734. PAL_SaveGame_DOS(iSaveSlot, wSavedTimes);
  735. }
  736. VOID
  737. PAL_InitGameData(
  738. INT iSaveSlot
  739. )
  740. /*++
  741. Purpose:
  742. Initialize the game data (used when starting a new game or loading a saved game).
  743. Parameters:
  744. [IN] iSaveSlot - Slot of saved game.
  745. Return value:
  746. None.
  747. --*/
  748. {
  749. PAL_InitGlobalGameData();
  750. gpGlobals->bCurrentSaveSlot = (BYTE)iSaveSlot;
  751. //
  752. // try loading from the saved game file.
  753. //
  754. if (iSaveSlot == 0 || PAL_LoadGame(iSaveSlot) != 0)
  755. {
  756. //
  757. // Cannot load the saved game file. Load the defaults.
  758. //
  759. PAL_LoadDefaultGame();
  760. }
  761. gpGlobals->fGameStart = TRUE;
  762. gpGlobals->fNeedToFadeIn = FALSE;
  763. gpGlobals->iCurInvMenuItem = 0;
  764. gpGlobals->fInBattle = FALSE;
  765. memset(gpGlobals->rgPlayerStatus, 0, sizeof(gpGlobals->rgPlayerStatus));
  766. PAL_UpdateEquipments();
  767. }
  768. BOOL
  769. PAL_AddItemToInventory(
  770. WORD wObjectID,
  771. INT iNum
  772. )
  773. /*++
  774. Purpose:
  775. Add or remove the specified kind of item in the inventory.
  776. Parameters:
  777. [IN] wObjectID - object number of the item.
  778. [IN] iNum - number to be added (positive value) or removed (negative value).
  779. Return value:
  780. TRUE if succeeded, FALSE if failed.
  781. --*/
  782. {
  783. int index;
  784. BOOL fFound;
  785. if (wObjectID == 0)
  786. {
  787. return FALSE;
  788. }
  789. if (iNum == 0)
  790. {
  791. iNum = 1;
  792. }
  793. index = 0;
  794. fFound = FALSE;
  795. //
  796. // Search for the specified item in the inventory
  797. //
  798. while (index < MAX_INVENTORY)
  799. {
  800. if (gpGlobals->rgInventory[index].wItem == wObjectID)
  801. {
  802. fFound = TRUE;
  803. break;
  804. }
  805. else if (gpGlobals->rgInventory[index].wItem == 0)
  806. {
  807. break;
  808. }
  809. index++;
  810. }
  811. if (iNum > 0)
  812. {
  813. //
  814. // Add item
  815. //
  816. if (index >= MAX_INVENTORY)
  817. {
  818. //
  819. // inventory is full. cannot add item
  820. //
  821. return FALSE;
  822. }
  823. if (fFound)
  824. {
  825. gpGlobals->rgInventory[index].nAmount += iNum;
  826. if (gpGlobals->rgInventory[index].nAmount > 99)
  827. {
  828. //
  829. // Maximum number is 99
  830. //
  831. gpGlobals->rgInventory[index].nAmount = 99;
  832. }
  833. }
  834. else
  835. {
  836. gpGlobals->rgInventory[index].wItem = wObjectID;
  837. if (iNum > 99)
  838. {
  839. iNum = 99;
  840. }
  841. gpGlobals->rgInventory[index].nAmount = iNum;
  842. }
  843. return TRUE;
  844. }
  845. else
  846. {
  847. //
  848. // Remove item
  849. //
  850. if (fFound)
  851. {
  852. iNum *= -1;
  853. if (gpGlobals->rgInventory[index].nAmount < iNum)
  854. {
  855. //
  856. // This item has been run out
  857. //
  858. gpGlobals->rgInventory[index].nAmount = 0;
  859. return FALSE;
  860. }
  861. gpGlobals->rgInventory[index].nAmount -= iNum;
  862. //
  863. /// Need process last item
  864. //
  865. if(gpGlobals->rgInventory[index].nAmount == 0 && index == gpGlobals->iCurInvMenuItem && index+1 < MAX_INVENTORY && gpGlobals->rgInventory[index+1].nAmount <= 0)
  866. gpGlobals->iCurInvMenuItem --;
  867. return TRUE;
  868. }
  869. return FALSE;
  870. }
  871. }
  872. INT
  873. PAL_GetItemAmount(
  874. WORD wItem
  875. )
  876. /*++
  877. Purpose:
  878. Get the amount of the specified item in the inventory.
  879. Parameters:
  880. [IN] wItem - the object ID of the item.
  881. Return value:
  882. The amount of the item in the inventory.
  883. --*/
  884. {
  885. int i;
  886. for (i = 0; i < MAX_INVENTORY; i++)
  887. {
  888. if (gpGlobals->rgInventory[i].wItem == 0)
  889. {
  890. break;
  891. }
  892. if (gpGlobals->rgInventory[i].wItem == wItem)
  893. {
  894. return gpGlobals->rgInventory[i].nAmount;
  895. }
  896. }
  897. return 0;
  898. }
  899. VOID
  900. PAL_CompressInventory(
  901. VOID
  902. )
  903. /*++
  904. Purpose:
  905. Remove all the items in inventory which has a number of zero.
  906. Parameters:
  907. None.
  908. Return value:
  909. None.
  910. --*/
  911. {
  912. int i, j;
  913. j = 0;
  914. for (i = 0; i < MAX_INVENTORY; i++)
  915. {
  916. if (gpGlobals->rgInventory[i].wItem == 0)
  917. {
  918. break;
  919. }
  920. if (gpGlobals->rgInventory[i].nAmount > 0)
  921. {
  922. gpGlobals->rgInventory[j] = gpGlobals->rgInventory[i];
  923. j++;
  924. }
  925. }
  926. for (; j < MAX_INVENTORY; j++)
  927. {
  928. gpGlobals->rgInventory[j].nAmount = 0;
  929. gpGlobals->rgInventory[j].nAmountInUse = 0;
  930. gpGlobals->rgInventory[j].wItem = 0;
  931. }
  932. }
  933. BOOL
  934. PAL_IncreaseHPMP(
  935. WORD wPlayerRole,
  936. SHORT sHP,
  937. SHORT sMP
  938. )
  939. /*++
  940. Purpose:
  941. Increase or decrease player's HP and/or MP.
  942. Parameters:
  943. [IN] wPlayerRole - the number of player role.
  944. [IN] sHP - number of HP to be increased (positive value) or decrased
  945. (negative value).
  946. [IN] sMP - number of MP to be increased (positive value) or decrased
  947. (negative value).
  948. Return value:
  949. TRUE if the operation is succeeded, FALSE if not.
  950. --*/
  951. {
  952. BOOL fSuccess = FALSE;
  953. //
  954. // Only care about alive players
  955. //
  956. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  957. {
  958. //
  959. // change HP
  960. //
  961. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] += sHP;
  962. if ((SHORT)(gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole]) < 0)
  963. {
  964. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] = 0;
  965. }
  966. else if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] >
  967. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole])
  968. {
  969. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] =
  970. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole];
  971. }
  972. //
  973. // Change MP
  974. //
  975. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] += sMP;
  976. if ((SHORT)(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole]) < 0)
  977. {
  978. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] = 0;
  979. }
  980. else if (gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] >
  981. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole])
  982. {
  983. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] =
  984. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole];
  985. }
  986. fSuccess = TRUE;
  987. }
  988. return fSuccess;
  989. }
  990. VOID
  991. PAL_UpdateEquipments(
  992. VOID
  993. )
  994. /*++
  995. Purpose:
  996. Update the effects of all equipped items for all players.
  997. Parameters:
  998. None.
  999. Return value:
  1000. None.
  1001. --*/
  1002. {
  1003. int i, j;
  1004. WORD w;
  1005. memset(&(gpGlobals->rgEquipmentEffect), 0, sizeof(gpGlobals->rgEquipmentEffect));
  1006. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  1007. {
  1008. for (j = 0; j < MAX_PLAYER_EQUIPMENTS; j++)
  1009. {
  1010. w = gpGlobals->g.PlayerRoles.rgwEquipment[j][i];
  1011. if (w != 0)
  1012. {
  1013. gpGlobals->g.rgObject[w].item.wScriptOnEquip =
  1014. PAL_RunTriggerScript(gpGlobals->g.rgObject[w].item.wScriptOnEquip, (WORD)i);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. VOID
  1020. PAL_RemoveEquipmentEffect(
  1021. WORD wPlayerRole,
  1022. WORD wEquipPart
  1023. )
  1024. /*++
  1025. Purpose:
  1026. Remove all the effects of the equipment for the player.
  1027. Parameters:
  1028. [IN] wPlayerRole - the player role.
  1029. [IN] wEquipPart - the part of the equipment.
  1030. Return value:
  1031. None.
  1032. --*/
  1033. {
  1034. WORD *p;
  1035. int i, j;
  1036. p = (WORD *)(&gpGlobals->rgEquipmentEffect[wEquipPart]); // HACKHACK
  1037. for (i = 0; i < sizeof(PLAYERROLES) / sizeof(PLAYERS); i++)
  1038. {
  1039. p[i * MAX_PLAYER_ROLES + wPlayerRole] = 0;
  1040. }
  1041. //
  1042. // Reset some parameters to default when appropriate
  1043. //
  1044. if (wEquipPart == kBodyPartHand)
  1045. {
  1046. //
  1047. // reset the dual attack status
  1048. //
  1049. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusDualAttack] = 0;
  1050. }
  1051. else if (wEquipPart == kBodyPartWear)
  1052. {
  1053. //
  1054. // Remove all poisons leveled 99
  1055. //
  1056. for (i = 0; i <= (short)gpGlobals->wMaxPartyMemberIndex; i++)
  1057. {
  1058. if (gpGlobals->rgParty[i].wPlayerRole == wPlayerRole)
  1059. {
  1060. wPlayerRole = i;
  1061. break;
  1062. }
  1063. }
  1064. if (i <= (short)gpGlobals->wMaxPartyMemberIndex)
  1065. {
  1066. j = 0;
  1067. for (i = 0; i < MAX_POISONS; i++)
  1068. {
  1069. WORD w = gpGlobals->rgPoisonStatus[i][wPlayerRole].wPoisonID;
  1070. if (w == 0)
  1071. {
  1072. break;
  1073. }
  1074. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel < 99)
  1075. {
  1076. gpGlobals->rgPoisonStatus[j][wPlayerRole] =
  1077. gpGlobals->rgPoisonStatus[i][wPlayerRole];
  1078. j++;
  1079. }
  1080. }
  1081. while (j < MAX_POISONS)
  1082. {
  1083. gpGlobals->rgPoisonStatus[j][wPlayerRole].wPoisonID = 0;
  1084. gpGlobals->rgPoisonStatus[j][wPlayerRole].wPoisonScript = 0;
  1085. j++;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. VOID
  1091. PAL_AddPoisonForPlayer(
  1092. WORD wPlayerRole,
  1093. WORD wPoisonID
  1094. )
  1095. /*++
  1096. Purpose:
  1097. Add the specified poison to the player.
  1098. Parameters:
  1099. [IN] wPlayerRole - the player role ID.
  1100. [IN] wPoisonID - the poison to be added.
  1101. Return value:
  1102. None.
  1103. --*/
  1104. {
  1105. int i, index;
  1106. WORD w;
  1107. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1108. {
  1109. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1110. {
  1111. break;
  1112. }
  1113. }
  1114. if (index > gpGlobals->wMaxPartyMemberIndex)
  1115. {
  1116. return; // don't go further
  1117. }
  1118. for (i = 0; i < MAX_POISONS; i++)
  1119. {
  1120. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  1121. if (w == 0)
  1122. {
  1123. break;
  1124. }
  1125. if (w == wPoisonID)
  1126. {
  1127. return; // already poisoned
  1128. }
  1129. }
  1130. if (i < MAX_POISONS)
  1131. {
  1132. gpGlobals->rgPoisonStatus[i][index].wPoisonID = wPoisonID;
  1133. gpGlobals->rgPoisonStatus[i][index].wPoisonScript =
  1134. gpGlobals->g.rgObject[wPoisonID].poison.wPlayerScript;
  1135. }
  1136. }
  1137. VOID
  1138. PAL_CurePoisonByKind(
  1139. WORD wPlayerRole,
  1140. WORD wPoisonID
  1141. )
  1142. /*++
  1143. Purpose:
  1144. Remove the specified poison from the player.
  1145. Parameters:
  1146. [IN] wPlayerRole - the player role ID.
  1147. [IN] wPoisonID - the poison to be removed.
  1148. Return value:
  1149. None.
  1150. --*/
  1151. {
  1152. int i, index;
  1153. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1154. {
  1155. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1156. {
  1157. break;
  1158. }
  1159. }
  1160. if (index > gpGlobals->wMaxPartyMemberIndex)
  1161. {
  1162. return; // don't go further
  1163. }
  1164. for (i = 0; i < MAX_POISONS; i++)
  1165. {
  1166. if (gpGlobals->rgPoisonStatus[i][index].wPoisonID == wPoisonID)
  1167. {
  1168. gpGlobals->rgPoisonStatus[i][index].wPoisonID = 0;
  1169. gpGlobals->rgPoisonStatus[i][index].wPoisonScript = 0;
  1170. }
  1171. }
  1172. }
  1173. VOID
  1174. PAL_CurePoisonByLevel(
  1175. WORD wPlayerRole,
  1176. WORD wMaxLevel
  1177. )
  1178. /*++
  1179. Purpose:
  1180. Remove the poisons which have a maximum level of wMaxLevel from the player.
  1181. Parameters:
  1182. [IN] wPlayerRole - the player role ID.
  1183. [IN] wMaxLevel - the maximum level of poisons to be removed.
  1184. Return value:
  1185. None.
  1186. --*/
  1187. {
  1188. int i, index;
  1189. WORD w;
  1190. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1191. {
  1192. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1193. {
  1194. break;
  1195. }
  1196. }
  1197. if (index > gpGlobals->wMaxPartyMemberIndex)
  1198. {
  1199. return; // don't go further
  1200. }
  1201. for (i = 0; i < MAX_POISONS; i++)
  1202. {
  1203. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  1204. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel <= wMaxLevel)
  1205. {
  1206. gpGlobals->rgPoisonStatus[i][index].wPoisonID = 0;
  1207. gpGlobals->rgPoisonStatus[i][index].wPoisonScript = 0;
  1208. }
  1209. }
  1210. }
  1211. BOOL
  1212. PAL_IsPlayerPoisonedByLevel(
  1213. WORD wPlayerRole,
  1214. WORD wMinLevel
  1215. )
  1216. /*++
  1217. Purpose:
  1218. Check if the player is poisoned by poisons at a minimum level of wMinLevel.
  1219. Parameters:
  1220. [IN] wPlayerRole - the player role ID.
  1221. [IN] wMinLevel - the minimum level of poison.
  1222. Return value:
  1223. TRUE if the player is poisoned by poisons at a minimum level of wMinLevel;
  1224. FALSE if not.
  1225. --*/
  1226. {
  1227. int i, index;
  1228. WORD w;
  1229. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1230. {
  1231. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1232. {
  1233. break;
  1234. }
  1235. }
  1236. if (index > gpGlobals->wMaxPartyMemberIndex)
  1237. {
  1238. return FALSE; // don't go further
  1239. }
  1240. for (i = 0; i < MAX_POISONS; i++)
  1241. {
  1242. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  1243. w = gpGlobals->g.rgObject[w].poison.wPoisonLevel;
  1244. if (w >= 99)
  1245. {
  1246. //
  1247. // Ignore poisons which has a level of 99 (usually effect of equipment)
  1248. //
  1249. continue;
  1250. }
  1251. if (w >= wMinLevel)
  1252. {
  1253. return TRUE;
  1254. }
  1255. }
  1256. return FALSE;
  1257. }
  1258. BOOL
  1259. PAL_IsPlayerPoisonedByKind(
  1260. WORD wPlayerRole,
  1261. WORD wPoisonID
  1262. )
  1263. /*++
  1264. Purpose:
  1265. Check if the player is poisoned by the specified poison.
  1266. Parameters:
  1267. [IN] wPlayerRole - the player role ID.
  1268. [IN] wPoisonID - the poison to be checked.
  1269. Return value:
  1270. TRUE if player is poisoned by the specified poison;
  1271. FALSE if not.
  1272. --*/
  1273. {
  1274. int i, index;
  1275. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1276. {
  1277. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1278. {
  1279. break;
  1280. }
  1281. }
  1282. if (index > gpGlobals->wMaxPartyMemberIndex)
  1283. {
  1284. return FALSE; // don't go further
  1285. }
  1286. for (i = 0; i < MAX_POISONS; i++)
  1287. {
  1288. if (gpGlobals->rgPoisonStatus[i][index].wPoisonID == wPoisonID)
  1289. {
  1290. return TRUE;
  1291. }
  1292. }
  1293. return FALSE;
  1294. }
  1295. WORD
  1296. PAL_GetPlayerAttackStrength(
  1297. WORD wPlayerRole
  1298. )
  1299. /*++
  1300. Purpose:
  1301. Get the player's attack strength, count in the effect of equipments.
  1302. Parameters:
  1303. [IN] wPlayerRole - the player role ID.
  1304. Return value:
  1305. The total attack strength of the player.
  1306. --*/
  1307. {
  1308. WORD w;
  1309. int i;
  1310. w = gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole];
  1311. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1312. {
  1313. w += gpGlobals->rgEquipmentEffect[i].rgwAttackStrength[wPlayerRole];
  1314. }
  1315. return w;
  1316. }
  1317. WORD
  1318. PAL_GetPlayerMagicStrength(
  1319. WORD wPlayerRole
  1320. )
  1321. /*++
  1322. Purpose:
  1323. Get the player's magic strength, count in the effect of equipments.
  1324. Parameters:
  1325. [IN] wPlayerRole - the player role ID.
  1326. Return value:
  1327. The total magic strength of the player.
  1328. --*/
  1329. {
  1330. WORD w;
  1331. int i;
  1332. w = gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole];
  1333. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1334. {
  1335. w += gpGlobals->rgEquipmentEffect[i].rgwMagicStrength[wPlayerRole];
  1336. }
  1337. return w;
  1338. }
  1339. WORD
  1340. PAL_GetPlayerDefense(
  1341. WORD wPlayerRole
  1342. )
  1343. /*++
  1344. Purpose:
  1345. Get the player's defense value, count in the effect of equipments.
  1346. Parameters:
  1347. [IN] wPlayerRole - the player role ID.
  1348. Return value:
  1349. The total defense value of the player.
  1350. --*/
  1351. {
  1352. WORD w;
  1353. int i;
  1354. w = gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole];
  1355. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1356. {
  1357. w += gpGlobals->rgEquipmentEffect[i].rgwDefense[wPlayerRole];
  1358. }
  1359. return w;
  1360. }
  1361. WORD
  1362. PAL_GetPlayerDexterity(
  1363. WORD wPlayerRole
  1364. )
  1365. /*++
  1366. Purpose:
  1367. Get the player's dexterity, count in the effect of equipments.
  1368. Parameters:
  1369. [IN] wPlayerRole - the player role ID.
  1370. Return value:
  1371. The total dexterity of the player.
  1372. --*/
  1373. {
  1374. WORD w;
  1375. int i;
  1376. w = gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole];
  1377. #ifdef PAL_CLASSIC
  1378. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1379. #else
  1380. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS - 1; i++)
  1381. #endif
  1382. {
  1383. w += gpGlobals->rgEquipmentEffect[i].rgwDexterity[wPlayerRole];
  1384. }
  1385. return w;
  1386. }
  1387. WORD
  1388. PAL_GetPlayerFleeRate(
  1389. WORD wPlayerRole
  1390. )
  1391. /*++
  1392. Purpose:
  1393. Get the player's flee rate, count in the effect of equipments.
  1394. Parameters:
  1395. [IN] wPlayerRole - the player role ID.
  1396. Return value:
  1397. The total flee rate of the player.
  1398. --*/
  1399. {
  1400. WORD w;
  1401. int i;
  1402. w = gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole];
  1403. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1404. {
  1405. w += gpGlobals->rgEquipmentEffect[i].rgwFleeRate[wPlayerRole];
  1406. }
  1407. return w;
  1408. }
  1409. WORD
  1410. PAL_GetPlayerPoisonResistance(
  1411. WORD wPlayerRole
  1412. )
  1413. /*++
  1414. Purpose:
  1415. Get the player's resistance to poisons, count in the effect of equipments.
  1416. Parameters:
  1417. [IN] wPlayerRole - the player role ID.
  1418. Return value:
  1419. The total resistance to poisons of the player.
  1420. --*/
  1421. {
  1422. WORD w;
  1423. int i;
  1424. w = gpGlobals->g.PlayerRoles.rgwPoisonResistance[wPlayerRole];
  1425. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1426. {
  1427. w += gpGlobals->rgEquipmentEffect[i].rgwPoisonResistance[wPlayerRole];
  1428. }
  1429. if (w > 100)
  1430. {
  1431. w = 100;
  1432. }
  1433. return w;
  1434. }
  1435. WORD
  1436. PAL_GetPlayerElementalResistance(
  1437. WORD wPlayerRole,
  1438. INT iAttrib
  1439. )
  1440. /*++
  1441. Purpose:
  1442. Get the player's resistance to attributed magics, count in the effect
  1443. of equipments.
  1444. Parameters:
  1445. [IN] wPlayerRole - the player role ID.
  1446. [IN] iAttrib - the attribute of magics.
  1447. Return value:
  1448. The total resistance to the attributed magics of the player.
  1449. --*/
  1450. {
  1451. WORD w;
  1452. int i;
  1453. w = gpGlobals->g.PlayerRoles.rgwElementalResistance[iAttrib][wPlayerRole];
  1454. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1455. {
  1456. w += gpGlobals->rgEquipmentEffect[i].rgwElementalResistance[iAttrib][wPlayerRole];
  1457. }
  1458. if (w > 100)
  1459. {
  1460. w = 100;
  1461. }
  1462. return w;
  1463. }
  1464. WORD
  1465. PAL_GetPlayerBattleSprite(
  1466. WORD wPlayerRole
  1467. )
  1468. /*++
  1469. Purpose:
  1470. Get player's battle sprite.
  1471. Parameters:
  1472. [IN] wPlayerRole - the player role ID.
  1473. Return value:
  1474. Number of the player's battle sprite.
  1475. --*/
  1476. {
  1477. int i;
  1478. WORD w;
  1479. w = gpGlobals->g.PlayerRoles.rgwSpriteNumInBattle[wPlayerRole];
  1480. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1481. {
  1482. if (gpGlobals->rgEquipmentEffect[i].rgwSpriteNumInBattle[wPlayerRole] != 0)
  1483. {
  1484. w = gpGlobals->rgEquipmentEffect[i].rgwSpriteNumInBattle[wPlayerRole];
  1485. }
  1486. }
  1487. return w;
  1488. }
  1489. WORD
  1490. PAL_GetPlayerCooperativeMagic(
  1491. WORD wPlayerRole
  1492. )
  1493. /*++
  1494. Purpose:
  1495. Get player's cooperative magic.
  1496. Parameters:
  1497. [IN] wPlayerRole - the player role ID.
  1498. Return value:
  1499. Object ID of the player's cooperative magic.
  1500. --*/
  1501. {
  1502. int i;
  1503. WORD w;
  1504. w = gpGlobals->g.PlayerRoles.rgwCooperativeMagic[wPlayerRole];
  1505. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1506. {
  1507. if (gpGlobals->rgEquipmentEffect[i].rgwCooperativeMagic[wPlayerRole] != 0)
  1508. {
  1509. w = gpGlobals->rgEquipmentEffect[i].rgwCooperativeMagic[wPlayerRole];
  1510. }
  1511. }
  1512. return w;
  1513. }
  1514. BOOL
  1515. PAL_PlayerCanAttackAll(
  1516. WORD wPlayerRole
  1517. )
  1518. /*++
  1519. Purpose:
  1520. Check if the player can attack all of the enemies in one move.
  1521. Parameters:
  1522. [IN] wPlayerRole - the player role ID.
  1523. Return value:
  1524. TRUE if player can attack all of the enemies in one move, FALSE if not.
  1525. --*/
  1526. {
  1527. int i;
  1528. BOOL f;
  1529. f = FALSE;
  1530. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1531. {
  1532. if (gpGlobals->rgEquipmentEffect[i].rgwAttackAll[wPlayerRole] != 0)
  1533. {
  1534. f = TRUE;
  1535. break;
  1536. }
  1537. }
  1538. return f;
  1539. }
  1540. BOOL
  1541. PAL_AddMagic(
  1542. WORD wPlayerRole,
  1543. WORD wMagic
  1544. )
  1545. /*++
  1546. Purpose:
  1547. Add a magic to the player.
  1548. Parameters:
  1549. [IN] wPlayerRole - the player role ID.
  1550. [IN] wMagic - the object ID of the magic.
  1551. Return value:
  1552. TRUE if succeeded, FALSE if failed.
  1553. --*/
  1554. {
  1555. int i;
  1556. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1557. {
  1558. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == wMagic)
  1559. {
  1560. //
  1561. // already have this magic
  1562. //
  1563. return FALSE;
  1564. }
  1565. }
  1566. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1567. {
  1568. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == 0)
  1569. {
  1570. break;
  1571. }
  1572. }
  1573. if (i >= MAX_PLAYER_MAGICS)
  1574. {
  1575. //
  1576. // Not enough slots
  1577. //
  1578. return FALSE;
  1579. }
  1580. gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] = wMagic;
  1581. return TRUE;
  1582. }
  1583. VOID
  1584. PAL_RemoveMagic(
  1585. WORD wPlayerRole,
  1586. WORD wMagic
  1587. )
  1588. /*++
  1589. Purpose:
  1590. Remove a magic to the player.
  1591. Parameters:
  1592. [IN] wPlayerRole - the player role ID.
  1593. [IN] wMagic - the object ID of the magic.
  1594. Return value:
  1595. None.
  1596. --*/
  1597. {
  1598. int i;
  1599. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1600. {
  1601. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == wMagic)
  1602. {
  1603. gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] = 0;
  1604. break;
  1605. }
  1606. }
  1607. }
  1608. VOID
  1609. PAL_SetPlayerStatus(
  1610. WORD wPlayerRole,
  1611. WORD wStatusID,
  1612. WORD wNumRound
  1613. )
  1614. /*++
  1615. Purpose:
  1616. Set one of the statuses for the player.
  1617. Parameters:
  1618. [IN] wPlayerRole - the player ID.
  1619. [IN] wStatusID - the status to be set.
  1620. [IN] wNumRound - the effective rounds of the status.
  1621. Return value:
  1622. None.
  1623. --*/
  1624. {
  1625. #ifndef PAL_CLASSIC
  1626. if (wStatusID == kStatusSlow &&
  1627. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusHaste] > 0)
  1628. {
  1629. //
  1630. // Remove the haste status
  1631. //
  1632. PAL_RemovePlayerStatus(wPlayerRole, kStatusHaste);
  1633. return;
  1634. }
  1635. if (wStatusID == kStatusHaste &&
  1636. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSlow] > 0)
  1637. {
  1638. //
  1639. // Remove the slow status
  1640. //
  1641. PAL_RemovePlayerStatus(wPlayerRole, kStatusSlow);
  1642. return;
  1643. }
  1644. #endif
  1645. switch (wStatusID)
  1646. {
  1647. case kStatusConfused:
  1648. case kStatusSleep:
  1649. case kStatusSilence:
  1650. #ifdef PAL_CLASSIC
  1651. case kStatusParalyzed:
  1652. #else
  1653. case kStatusSlow:
  1654. #endif
  1655. //
  1656. // for "bad" statuses, don't set the status when we already have it
  1657. //
  1658. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] != 0 &&
  1659. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] == 0)
  1660. {
  1661. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1662. }
  1663. break;
  1664. case kStatusPuppet:
  1665. //
  1666. // only allow dead players for "puppet" status
  1667. //
  1668. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 &&
  1669. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] < wNumRound)
  1670. {
  1671. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1672. }
  1673. break;
  1674. case kStatusBravery:
  1675. case kStatusProtect:
  1676. case kStatusDualAttack:
  1677. case kStatusHaste:
  1678. //
  1679. // for "good" statuses, reset the status if the status to be set lasts longer
  1680. //
  1681. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] != 0 &&
  1682. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] < wNumRound)
  1683. {
  1684. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1685. }
  1686. break;
  1687. default:
  1688. assert(FALSE);
  1689. break;
  1690. }
  1691. }
  1692. VOID
  1693. PAL_RemovePlayerStatus(
  1694. WORD wPlayerRole,
  1695. WORD wStatusID
  1696. )
  1697. /*++
  1698. Purpose:
  1699. Remove one of the status for player.
  1700. Parameters:
  1701. [IN] wPlayerRole - the player ID.
  1702. [IN] wStatusID - the status to be set.
  1703. Return value:
  1704. None.
  1705. --*/
  1706. {
  1707. //
  1708. // Don't remove effects of equipments
  1709. //
  1710. if (gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] <= 999)
  1711. {
  1712. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = 0;
  1713. }
  1714. }
  1715. VOID
  1716. PAL_ClearAllPlayerStatus(
  1717. VOID
  1718. )
  1719. /*++
  1720. Purpose:
  1721. Clear all player status.
  1722. Parameters:
  1723. None.
  1724. Return value:
  1725. None.
  1726. --*/
  1727. {
  1728. int i, j;
  1729. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  1730. {
  1731. for (j = 0; j < kStatusAll; j++)
  1732. {
  1733. //
  1734. // Don't remove effects of equipments
  1735. //
  1736. if (gpGlobals->rgPlayerStatus[i][j] <= 999)
  1737. {
  1738. gpGlobals->rgPlayerStatus[i][j] = 0;
  1739. }
  1740. }
  1741. }
  1742. }
  1743. VOID
  1744. PAL_PlayerLevelUp(
  1745. WORD wPlayerRole,
  1746. WORD wNumLevel
  1747. )
  1748. /*++
  1749. Purpose:
  1750. Increase the player's level by wLevels.
  1751. Parameters:
  1752. [IN] wPlayerRole - player role ID.
  1753. [IN] wNumLevel - number of levels to be increased.
  1754. Return value:
  1755. None.
  1756. --*/
  1757. {
  1758. WORD i;
  1759. //
  1760. // Add the level
  1761. //
  1762. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] += wNumLevel;
  1763. if (gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] > MAX_LEVELS)
  1764. {
  1765. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] = MAX_LEVELS;
  1766. }
  1767. for (i = 0; i < wNumLevel; i++)
  1768. {
  1769. //
  1770. // Increase player's stats
  1771. //
  1772. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole] += 10 + RandomLong(0, 8);
  1773. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole] += 8 + RandomLong(0, 6);
  1774. gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole] += 4 + RandomLong(0, 1);
  1775. gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole] += 4 + RandomLong(0, 1);
  1776. gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole] += 2 + RandomLong(0, 1);
  1777. gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole] += 2 + RandomLong(0, 1);
  1778. gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole] += 2;
  1779. }
  1780. #define STAT_LIMIT(t) { if ((t) > 999) (t) = 999; }
  1781. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole]);
  1782. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole]);
  1783. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole]);
  1784. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole]);
  1785. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole]);
  1786. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole]);
  1787. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole]);
  1788. #undef STAT_LIMIT
  1789. //
  1790. // Reset experience points to zero
  1791. //
  1792. gpGlobals->Exp.rgPrimaryExp[wPlayerRole].wExp = 0;
  1793. gpGlobals->Exp.rgPrimaryExp[wPlayerRole].wLevel =
  1794. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole];
  1795. }