global.c 52 KB

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