global.c 53 KB

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