global.c 58 KB

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