global.c 58 KB

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