global.c 58 KB

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