global.c 51 KB

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