global.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476
  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. }
  410. static VOID
  411. PAL_InitGlobalGameData(
  412. VOID
  413. )
  414. /*++
  415. Purpose:
  416. Initialize global game data.
  417. Parameters:
  418. None.
  419. Return value:
  420. None.
  421. --*/
  422. {
  423. int len;
  424. #define PAL_DOALLOCATE(fp, num, type, lptype, ptr, n) \
  425. { \
  426. len = PAL_MKFGetChunkSize(num, fp); \
  427. ptr = (lptype)malloc(len); \
  428. n = len / sizeof(type); \
  429. if (ptr == NULL) \
  430. { \
  431. TerminateOnError("PAL_InitGlobalGameData(): Memory allocation error!"); \
  432. } \
  433. }
  434. //
  435. // If the memory has not been allocated, allocate first.
  436. //
  437. if (gpGlobals->g.lprgEventObject == NULL)
  438. {
  439. PAL_DOALLOCATE(gpGlobals->f.fpSSS, 0, EVENTOBJECT, LPEVENTOBJECT,
  440. gpGlobals->g.lprgEventObject, gpGlobals->g.nEventObject);
  441. PAL_DOALLOCATE(gpGlobals->f.fpSSS, 4, SCRIPTENTRY, LPSCRIPTENTRY,
  442. gpGlobals->g.lprgScriptEntry, gpGlobals->g.nScriptEntry);
  443. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 0, STORE, LPSTORE,
  444. gpGlobals->g.lprgStore, gpGlobals->g.nStore);
  445. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 1, ENEMY, LPENEMY,
  446. gpGlobals->g.lprgEnemy, gpGlobals->g.nEnemy);
  447. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 2, ENEMYTEAM, LPENEMYTEAM,
  448. gpGlobals->g.lprgEnemyTeam, gpGlobals->g.nEnemyTeam);
  449. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 4, MAGIC, LPMAGIC,
  450. gpGlobals->g.lprgMagic, gpGlobals->g.nMagic);
  451. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 5, BATTLEFIELD, LPBATTLEFIELD,
  452. gpGlobals->g.lprgBattleField, gpGlobals->g.nBattleField);
  453. PAL_DOALLOCATE(gpGlobals->f.fpDATA, 6, LEVELUPMAGIC_ALL, LPLEVELUPMAGIC_ALL,
  454. gpGlobals->g.lprgLevelUpMagic, gpGlobals->g.nLevelUpMagic);
  455. PAL_ReadGlobalGameData();
  456. }
  457. #undef PAL_DOALLOCATE
  458. }
  459. static VOID
  460. PAL_LoadDefaultGame(
  461. VOID
  462. )
  463. /*++
  464. Purpose:
  465. Load the default game data.
  466. Parameters:
  467. None.
  468. Return value:
  469. None.
  470. --*/
  471. {
  472. GAMEDATA *p = &gpGlobals->g;
  473. UINT32 i;
  474. //
  475. // Load the default data from the game data files.
  476. //
  477. LOAD_DATA(p->lprgEventObject, p->nEventObject * sizeof(EVENTOBJECT),
  478. 0, gpGlobals->f.fpSSS);
  479. PAL_MKFReadChunk((LPBYTE)(p->rgScene), sizeof(p->rgScene), 1, gpGlobals->f.fpSSS);
  480. DO_BYTESWAP(p->rgScene, sizeof(p->rgScene));
  481. if (gConfig.fIsWIN95)
  482. {
  483. PAL_MKFReadChunk((LPBYTE)(p->rgObject), sizeof(p->rgObject), 2, gpGlobals->f.fpSSS);
  484. DO_BYTESWAP(p->rgObject, sizeof(p->rgObject));
  485. }
  486. else
  487. {
  488. OBJECT_DOS objects[MAX_OBJECTS];
  489. PAL_MKFReadChunk((LPBYTE)(objects), sizeof(objects), 2, gpGlobals->f.fpSSS);
  490. DO_BYTESWAP(objects, sizeof(objects));
  491. //
  492. // Convert the DOS-style data structure to WIN-style data structure
  493. //
  494. for (i = 0; i < MAX_OBJECTS; i++)
  495. {
  496. memcpy(&p->rgObject[i], &objects[i], sizeof(OBJECT_DOS));
  497. if (i >= OBJECT_ITEM_START && i <= OBJECT_MAGIC_END)
  498. {
  499. p->rgObject[i].rgwData[6] = objects[i].rgwData[5]; // wFlags
  500. p->rgObject[i].rgwData[5] = 0; // wScriptDesc or wReserved2
  501. }
  502. else
  503. {
  504. p->rgObject[i].rgwData[6] = 0;
  505. }
  506. }
  507. }
  508. PAL_MKFReadChunk((LPBYTE)(&(p->PlayerRoles)), sizeof(PLAYERROLES),
  509. 3, gpGlobals->f.fpDATA);
  510. DO_BYTESWAP(&(p->PlayerRoles), sizeof(PLAYERROLES));
  511. //
  512. // Set some other default data.
  513. //
  514. gpGlobals->dwCash = 0;
  515. gpGlobals->wNumMusic = 0;
  516. gpGlobals->wNumPalette = 0;
  517. gpGlobals->wNumScene = 1;
  518. gpGlobals->wCollectValue = 0;
  519. gpGlobals->fNightPalette = FALSE;
  520. gpGlobals->wMaxPartyMemberIndex = 0;
  521. gpGlobals->viewport = PAL_XY(0, 0);
  522. gpGlobals->wLayer = 0;
  523. gpGlobals->wChaseRange = 1;
  524. #ifndef PAL_CLASSIC
  525. gpGlobals->bBattleSpeed = 2;
  526. #endif
  527. memset(gpGlobals->rgInventory, 0, sizeof(gpGlobals->rgInventory));
  528. memset(gpGlobals->rgPoisonStatus, 0, sizeof(gpGlobals->rgPoisonStatus));
  529. memset(gpGlobals->rgParty, 0, sizeof(gpGlobals->rgParty));
  530. memset(gpGlobals->rgTrail, 0, sizeof(gpGlobals->rgTrail));
  531. memset(&(gpGlobals->Exp), 0, sizeof(gpGlobals->Exp));
  532. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  533. {
  534. gpGlobals->Exp.rgPrimaryExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  535. gpGlobals->Exp.rgHealthExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  536. gpGlobals->Exp.rgMagicExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  537. gpGlobals->Exp.rgAttackExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  538. gpGlobals->Exp.rgMagicPowerExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  539. gpGlobals->Exp.rgDefenseExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  540. gpGlobals->Exp.rgDexterityExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  541. gpGlobals->Exp.rgFleeExp[i].wLevel = p->PlayerRoles.rgwLevel[i];
  542. }
  543. gpGlobals->fEnteringScene = TRUE;
  544. }
  545. typedef struct tagSAVEDGAME_COMMON
  546. {
  547. WORD wSavedTimes; // saved times
  548. WORD wViewportX, wViewportY; // viewport location
  549. WORD nPartyMember; // number of members in party
  550. WORD wNumScene; // scene number
  551. WORD wPaletteOffset;
  552. WORD wPartyDirection; // party direction
  553. WORD wNumMusic; // music number
  554. WORD wNumBattleMusic; // battle music number
  555. WORD wNumBattleField; // battle field number
  556. WORD wScreenWave; // level of screen waving
  557. WORD wBattleSpeed; // battle speed
  558. WORD wCollectValue; // value of "collected" items
  559. WORD wLayer;
  560. WORD wChaseRange;
  561. WORD wChasespeedChangeCycles;
  562. WORD nFollower;
  563. WORD rgwReserved2[3]; // unused
  564. DWORD dwCash; // amount of cash
  565. PARTY rgParty[MAX_PLAYABLE_PLAYER_ROLES]; // player party
  566. TRAIL rgTrail[MAX_PLAYABLE_PLAYER_ROLES]; // player trail
  567. ALLEXPERIENCE Exp; // experience data
  568. PLAYERROLES PlayerRoles;
  569. POISONSTATUS rgPoisonStatus[MAX_POISONS][MAX_PLAYABLE_PLAYER_ROLES]; // poison status
  570. INVENTORY rgInventory[MAX_INVENTORY]; // inventory status
  571. SCENE rgScene[MAX_SCENES];
  572. } SAVEDGAME_COMMON, *LPSAVEDGAME_COMMON;
  573. typedef struct tagSAVEDGAME_DOS
  574. {
  575. WORD wSavedTimes; // saved times
  576. WORD wViewportX, wViewportY; // viewport location
  577. WORD nPartyMember; // number of members in party
  578. WORD wNumScene; // scene number
  579. WORD wPaletteOffset;
  580. WORD wPartyDirection; // party direction
  581. WORD wNumMusic; // music number
  582. WORD wNumBattleMusic; // battle music number
  583. WORD wNumBattleField; // battle field number
  584. WORD wScreenWave; // level of screen waving
  585. WORD wBattleSpeed; // battle speed
  586. WORD wCollectValue; // value of "collected" items
  587. WORD wLayer;
  588. WORD wChaseRange;
  589. WORD wChasespeedChangeCycles;
  590. WORD nFollower;
  591. WORD rgwReserved2[3]; // unused
  592. DWORD dwCash; // amount of cash
  593. PARTY rgParty[MAX_PLAYABLE_PLAYER_ROLES]; // player party
  594. TRAIL rgTrail[MAX_PLAYABLE_PLAYER_ROLES]; // player trail
  595. ALLEXPERIENCE Exp; // experience data
  596. PLAYERROLES PlayerRoles;
  597. POISONSTATUS rgPoisonStatus[MAX_POISONS][MAX_PLAYABLE_PLAYER_ROLES]; // poison status
  598. INVENTORY rgInventory[MAX_INVENTORY]; // inventory status
  599. SCENE rgScene[MAX_SCENES];
  600. OBJECT_DOS rgObject[MAX_OBJECTS];
  601. EVENTOBJECT rgEventObject[MAX_EVENT_OBJECTS];
  602. } SAVEDGAME_DOS, *LPSAVEDGAME_DOS;
  603. typedef struct tagSAVEDGAME_WIN
  604. {
  605. WORD wSavedTimes; // saved times
  606. WORD wViewportX, wViewportY; // viewport location
  607. WORD nPartyMember; // number of members in party
  608. WORD wNumScene; // scene number
  609. WORD wPaletteOffset;
  610. WORD wPartyDirection; // party direction
  611. WORD wNumMusic; // music number
  612. WORD wNumBattleMusic; // battle music number
  613. WORD wNumBattleField; // battle field number
  614. WORD wScreenWave; // level of screen waving
  615. WORD wBattleSpeed; // battle speed
  616. WORD wCollectValue; // value of "collected" items
  617. WORD wLayer;
  618. WORD wChaseRange;
  619. WORD wChasespeedChangeCycles;
  620. WORD nFollower;
  621. WORD rgwReserved2[3]; // unused
  622. DWORD dwCash; // amount of cash
  623. PARTY rgParty[MAX_PLAYABLE_PLAYER_ROLES]; // player party
  624. TRAIL rgTrail[MAX_PLAYABLE_PLAYER_ROLES]; // player trail
  625. ALLEXPERIENCE Exp; // experience data
  626. PLAYERROLES PlayerRoles;
  627. POISONSTATUS rgPoisonStatus[MAX_POISONS][MAX_PLAYABLE_PLAYER_ROLES]; // poison status
  628. INVENTORY rgInventory[MAX_INVENTORY]; // inventory status
  629. SCENE rgScene[MAX_SCENES];
  630. OBJECT rgObject[MAX_OBJECTS];
  631. EVENTOBJECT rgEventObject[MAX_EVENT_OBJECTS];
  632. } SAVEDGAME_WIN, *LPSAVEDGAME_WIN;
  633. static VOID
  634. PAL_LoadGame_Common(
  635. const LPSAVEDGAME_COMMON s
  636. )
  637. {
  638. gpGlobals->viewport = PAL_XY(s->wViewportX, s->wViewportY);
  639. gpGlobals->wMaxPartyMemberIndex = s->nPartyMember;
  640. gpGlobals->wNumScene = s->wNumScene;
  641. gpGlobals->fNightPalette = (s->wPaletteOffset != 0);
  642. gpGlobals->wPartyDirection = s->wPartyDirection;
  643. gpGlobals->wNumMusic = s->wNumMusic;
  644. gpGlobals->wNumBattleMusic = s->wNumBattleMusic;
  645. gpGlobals->wNumBattleField = s->wNumBattleField;
  646. gpGlobals->wScreenWave = s->wScreenWave;
  647. gpGlobals->sWaveProgression = 0;
  648. gpGlobals->wCollectValue = s->wCollectValue;
  649. gpGlobals->wLayer = s->wLayer;
  650. gpGlobals->wChaseRange = s->wChaseRange;
  651. gpGlobals->wChasespeedChangeCycles = s->wChasespeedChangeCycles;
  652. gpGlobals->nFollower = s->nFollower;
  653. gpGlobals->dwCash = s->dwCash;
  654. #ifndef PAL_CLASSIC
  655. gpGlobals->bBattleSpeed = s->wBattleSpeed;
  656. if (gpGlobals->bBattleSpeed > 5 || gpGlobals->bBattleSpeed == 0)
  657. {
  658. gpGlobals->bBattleSpeed = 2;
  659. }
  660. #endif
  661. memcpy(gpGlobals->rgParty, s->rgParty, sizeof(gpGlobals->rgParty));
  662. memcpy(gpGlobals->rgTrail, s->rgTrail, sizeof(gpGlobals->rgTrail));
  663. gpGlobals->Exp = s->Exp;
  664. gpGlobals->g.PlayerRoles = s->PlayerRoles;
  665. memset(gpGlobals->rgPoisonStatus, 0, sizeof(gpGlobals->rgPoisonStatus));
  666. memcpy(gpGlobals->rgInventory, s->rgInventory, sizeof(gpGlobals->rgInventory));
  667. memcpy(gpGlobals->g.rgScene, s->rgScene, sizeof(gpGlobals->g.rgScene));
  668. }
  669. static INT
  670. PAL_LoadGame_DOS(
  671. LPCSTR szFileName
  672. )
  673. /*++
  674. Purpose:
  675. Load a saved game.
  676. Parameters:
  677. [IN] szFileName - file name of saved game.
  678. Return value:
  679. 0 if success, -1 if failed.
  680. --*/
  681. {
  682. FILE *fp;
  683. PAL_LARGE SAVEDGAME_DOS s;
  684. int i;
  685. //
  686. // Try to open the specified file
  687. //
  688. fp = fopen(szFileName, "rb");
  689. if (fp == NULL)
  690. {
  691. return -1;
  692. }
  693. //
  694. // Read all data from the file and close.
  695. //
  696. fread(&s, sizeof(SAVEDGAME_DOS), 1, fp);
  697. fclose(fp);
  698. //
  699. // Adjust endianness
  700. //
  701. DO_BYTESWAP(&s, sizeof(SAVEDGAME_DOS));
  702. //
  703. // Cash amount is in DWORD, so do a wordswap in Big-Endian.
  704. //
  705. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  706. s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
  707. #endif
  708. //
  709. // Get all the data from the saved game struct.
  710. //
  711. PAL_LoadGame_Common((LPSAVEDGAME_COMMON)&s);
  712. //
  713. // Convert the DOS-style data structure to WIN-style data structure
  714. //
  715. for (i = 0; i < MAX_OBJECTS; i++)
  716. {
  717. memcpy(&gpGlobals->g.rgObject[i], &s.rgObject[i], sizeof(OBJECT_DOS));
  718. if (i >= OBJECT_ITEM_START && i <= OBJECT_MAGIC_END)
  719. {
  720. gpGlobals->g.rgObject[i].rgwData[6] = s.rgObject[i].rgwData[5]; // wFlags
  721. gpGlobals->g.rgObject[i].rgwData[5] = 0; // wScriptDesc or wReserved2
  722. }
  723. else
  724. {
  725. gpGlobals->g.rgObject[i].rgwData[6] = 0;
  726. }
  727. }
  728. memcpy(gpGlobals->g.lprgEventObject, s.rgEventObject,
  729. sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  730. gpGlobals->fEnteringScene = FALSE;
  731. PAL_CompressInventory();
  732. //
  733. // Success
  734. //
  735. return 0;
  736. }
  737. static INT
  738. PAL_LoadGame_WIN(
  739. LPCSTR szFileName
  740. )
  741. /*++
  742. Purpose:
  743. Load a saved game.
  744. Parameters:
  745. [IN] szFileName - file name of saved game.
  746. Return value:
  747. 0 if success, -1 if failed.
  748. --*/
  749. {
  750. FILE *fp;
  751. PAL_LARGE SAVEDGAME_WIN s;
  752. //
  753. // Try to open the specified file
  754. //
  755. fp = fopen(szFileName, "rb");
  756. if (fp == NULL)
  757. {
  758. return -1;
  759. }
  760. //
  761. // Read all data from the file and close.
  762. //
  763. fread(&s, sizeof(SAVEDGAME_WIN), 1, fp);
  764. fclose(fp);
  765. //
  766. // Adjust endianness
  767. //
  768. DO_BYTESWAP(&s, sizeof(SAVEDGAME_WIN));
  769. //
  770. // Cash amount is in DWORD, so do a wordswap in Big-Endian.
  771. //
  772. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  773. s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
  774. #endif
  775. //
  776. // Get all the data from the saved game struct.
  777. //
  778. PAL_LoadGame_Common((LPSAVEDGAME_COMMON)&s);
  779. memcpy(gpGlobals->g.rgObject, s.rgObject, sizeof(gpGlobals->g.rgObject));
  780. memcpy(gpGlobals->g.lprgEventObject, s.rgEventObject,
  781. sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  782. gpGlobals->fEnteringScene = FALSE;
  783. PAL_CompressInventory();
  784. //
  785. // Success
  786. //
  787. return 0;
  788. }
  789. static INT
  790. PAL_LoadGame(
  791. LPCSTR szFileName
  792. )
  793. {
  794. return gConfig.fIsWIN95 ? PAL_LoadGame_WIN(szFileName) : PAL_LoadGame_DOS(szFileName);
  795. }
  796. static VOID
  797. PAL_SaveGame_Common(
  798. const LPSAVEDGAME_COMMON s
  799. )
  800. {
  801. s->wViewportX = PAL_X(gpGlobals->viewport);
  802. s->wViewportY = PAL_Y(gpGlobals->viewport);
  803. s->nPartyMember = gpGlobals->wMaxPartyMemberIndex;
  804. s->wNumScene = gpGlobals->wNumScene;
  805. s->wPaletteOffset = (gpGlobals->fNightPalette ? 0x180 : 0);
  806. s->wPartyDirection = gpGlobals->wPartyDirection;
  807. s->wNumMusic = gpGlobals->wNumMusic;
  808. s->wNumBattleMusic = gpGlobals->wNumBattleMusic;
  809. s->wNumBattleField = gpGlobals->wNumBattleField;
  810. s->wScreenWave = gpGlobals->wScreenWave;
  811. s->wCollectValue = gpGlobals->wCollectValue;
  812. s->wLayer = gpGlobals->wLayer;
  813. s->wChaseRange = gpGlobals->wChaseRange;
  814. s->wChasespeedChangeCycles = gpGlobals->wChasespeedChangeCycles;
  815. s->nFollower = gpGlobals->nFollower;
  816. s->dwCash = gpGlobals->dwCash;
  817. #ifndef PAL_CLASSIC
  818. s->wBattleSpeed = gpGlobals->bBattleSpeed;
  819. #else
  820. s->wBattleSpeed = 2;
  821. #endif
  822. memcpy(s->rgParty, gpGlobals->rgParty, sizeof(gpGlobals->rgParty));
  823. memcpy(s->rgTrail, gpGlobals->rgTrail, sizeof(gpGlobals->rgTrail));
  824. s->Exp = gpGlobals->Exp;
  825. s->PlayerRoles = gpGlobals->g.PlayerRoles;
  826. memcpy(s->rgPoisonStatus, gpGlobals->rgPoisonStatus, sizeof(gpGlobals->rgPoisonStatus));
  827. memcpy(s->rgInventory, gpGlobals->rgInventory, sizeof(gpGlobals->rgInventory));
  828. memcpy(s->rgScene, gpGlobals->g.rgScene, sizeof(gpGlobals->g.rgScene));
  829. }
  830. static VOID
  831. PAL_SaveGame_DOS(
  832. LPCSTR szFileName,
  833. WORD wSavedTimes
  834. )
  835. /*++
  836. Purpose:
  837. Save the current game state to file.
  838. Parameters:
  839. [IN] szFileName - file name of saved game.
  840. Return value:
  841. None.
  842. --*/
  843. {
  844. FILE *fp;
  845. PAL_LARGE SAVEDGAME_DOS s;
  846. UINT32 i;
  847. //
  848. // Put all the data to the saved game struct.
  849. //
  850. PAL_SaveGame_Common((LPSAVEDGAME_COMMON)&s);
  851. //
  852. // Convert the WIN-style data structure to DOS-style data structure
  853. //
  854. for (i = 0; i < MAX_OBJECTS; i++)
  855. {
  856. memcpy(&s.rgObject[i], &gpGlobals->g.rgObject[i], sizeof(OBJECT_DOS));
  857. if (i >= OBJECT_ITEM_START && i <= OBJECT_MAGIC_END)
  858. {
  859. s.rgObject[i].rgwData[5] = gpGlobals->g.rgObject[i].rgwData[6]; // wFlags
  860. }
  861. }
  862. memcpy(s.rgEventObject, gpGlobals->g.lprgEventObject,
  863. sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  864. s.wSavedTimes = wSavedTimes;
  865. //
  866. // Adjust endianness
  867. //
  868. DO_BYTESWAP(&s, sizeof(SAVEDGAME));
  869. //
  870. // Cash amount is in DWORD, so do a wordswap in Big-Endian.
  871. //
  872. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  873. s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
  874. #endif
  875. //
  876. // Try writing to file
  877. //
  878. fp = fopen(szFileName, "wb");
  879. if (fp == NULL)
  880. {
  881. return;
  882. }
  883. i = PAL_MKFGetChunkSize(0, gpGlobals->f.fpSSS);
  884. i += sizeof(SAVEDGAME_DOS) - sizeof(EVENTOBJECT) * MAX_EVENT_OBJECTS;
  885. fwrite(&s, i, 1, fp);
  886. fclose(fp);
  887. }
  888. static VOID
  889. PAL_SaveGame_WIN(
  890. LPCSTR szFileName,
  891. WORD wSavedTimes
  892. )
  893. /*++
  894. Purpose:
  895. Save the current game state to file.
  896. Parameters:
  897. [IN] szFileName - file name of saved game.
  898. Return value:
  899. None.
  900. --*/
  901. {
  902. FILE *fp;
  903. PAL_LARGE SAVEDGAME_WIN s;
  904. UINT32 i;
  905. //
  906. // Put all the data to the saved game struct.
  907. //
  908. PAL_SaveGame_Common((LPSAVEDGAME_COMMON)&s);
  909. memcpy(s.rgObject, gpGlobals->g.rgObject, sizeof(gpGlobals->g.rgObject));
  910. memcpy(s.rgEventObject, gpGlobals->g.lprgEventObject,
  911. sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
  912. s.wSavedTimes = wSavedTimes;
  913. //
  914. // Adjust endianness
  915. //
  916. DO_BYTESWAP(&s, sizeof(SAVEDGAME));
  917. //
  918. // Cash amount is in DWORD, so do a wordswap in Big-Endian.
  919. //
  920. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  921. s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
  922. #endif
  923. //
  924. // Try writing to file
  925. //
  926. fp = fopen(szFileName, "wb");
  927. if (fp == NULL)
  928. {
  929. return;
  930. }
  931. i = PAL_MKFGetChunkSize(0, gpGlobals->f.fpSSS);
  932. i += sizeof(SAVEDGAME_WIN) - sizeof(EVENTOBJECT) * MAX_EVENT_OBJECTS;
  933. fwrite(&s, i, 1, fp);
  934. fclose(fp);
  935. }
  936. VOID
  937. PAL_SaveGame(
  938. LPCSTR szFileName,
  939. WORD wSavedTimes
  940. )
  941. {
  942. if (gConfig.fIsWIN95)
  943. PAL_SaveGame_WIN(szFileName, wSavedTimes);
  944. else
  945. PAL_SaveGame_DOS(szFileName, wSavedTimes);
  946. }
  947. VOID
  948. PAL_InitGameData(
  949. INT iSaveSlot
  950. )
  951. /*++
  952. Purpose:
  953. Initialize the game data (used when starting a new game or loading a saved game).
  954. Parameters:
  955. [IN] iSaveSlot - Slot of saved game.
  956. Return value:
  957. None.
  958. --*/
  959. {
  960. PAL_InitGlobalGameData();
  961. gpGlobals->bCurrentSaveSlot = (BYTE)iSaveSlot;
  962. //
  963. // try loading from the saved game file.
  964. //
  965. if (iSaveSlot == 0 || PAL_LoadGame(va("%s%d%s", PAL_SAVE_PREFIX, iSaveSlot, ".rpg")) != 0)
  966. {
  967. //
  968. // Cannot load the saved game file. Load the defaults.
  969. //
  970. PAL_LoadDefaultGame();
  971. }
  972. gpGlobals->fGameStart = TRUE;
  973. gpGlobals->fNeedToFadeIn = FALSE;
  974. gpGlobals->iCurInvMenuItem = 0;
  975. gpGlobals->fInBattle = FALSE;
  976. memset(gpGlobals->rgPlayerStatus, 0, sizeof(gpGlobals->rgPlayerStatus));
  977. PAL_UpdateEquipments();
  978. }
  979. BOOL
  980. PAL_AddItemToInventory(
  981. WORD wObjectID,
  982. INT iNum
  983. )
  984. /*++
  985. Purpose:
  986. Add or remove the specified kind of item in the inventory.
  987. Parameters:
  988. [IN] wObjectID - object number of the item.
  989. [IN] iNum - number to be added (positive value) or removed (negative value).
  990. Return value:
  991. TRUE if succeeded, FALSE if failed.
  992. --*/
  993. {
  994. int index;
  995. BOOL fFound;
  996. if (wObjectID == 0)
  997. {
  998. return FALSE;
  999. }
  1000. if (iNum == 0)
  1001. {
  1002. iNum = 1;
  1003. }
  1004. index = 0;
  1005. fFound = FALSE;
  1006. //
  1007. // Search for the specified item in the inventory
  1008. //
  1009. while (index < MAX_INVENTORY)
  1010. {
  1011. if (gpGlobals->rgInventory[index].wItem == wObjectID)
  1012. {
  1013. fFound = TRUE;
  1014. break;
  1015. }
  1016. else if (gpGlobals->rgInventory[index].wItem == 0)
  1017. {
  1018. break;
  1019. }
  1020. index++;
  1021. }
  1022. if (iNum > 0)
  1023. {
  1024. //
  1025. // Add item
  1026. //
  1027. if (index >= MAX_INVENTORY)
  1028. {
  1029. //
  1030. // inventory is full. cannot add item
  1031. //
  1032. return FALSE;
  1033. }
  1034. if (fFound)
  1035. {
  1036. gpGlobals->rgInventory[index].nAmount += iNum;
  1037. if (gpGlobals->rgInventory[index].nAmount > 99)
  1038. {
  1039. //
  1040. // Maximum number is 99
  1041. //
  1042. gpGlobals->rgInventory[index].nAmount = 99;
  1043. }
  1044. }
  1045. else
  1046. {
  1047. gpGlobals->rgInventory[index].wItem = wObjectID;
  1048. if (iNum > 99)
  1049. {
  1050. iNum = 99;
  1051. }
  1052. gpGlobals->rgInventory[index].nAmount = iNum;
  1053. }
  1054. return TRUE;
  1055. }
  1056. else
  1057. {
  1058. //
  1059. // Remove item
  1060. //
  1061. if (fFound)
  1062. {
  1063. iNum *= -1;
  1064. if (gpGlobals->rgInventory[index].nAmount < iNum)
  1065. {
  1066. //
  1067. // This item has been run out
  1068. //
  1069. gpGlobals->rgInventory[index].nAmount = 0;
  1070. return FALSE;
  1071. }
  1072. gpGlobals->rgInventory[index].nAmount -= iNum;
  1073. return TRUE;
  1074. }
  1075. return FALSE;
  1076. }
  1077. }
  1078. INT
  1079. PAL_GetItemAmount(
  1080. WORD wItem
  1081. )
  1082. /*++
  1083. Purpose:
  1084. Get the amount of the specified item in the inventory.
  1085. Parameters:
  1086. [IN] wItem - the object ID of the item.
  1087. Return value:
  1088. The amount of the item in the inventory.
  1089. --*/
  1090. {
  1091. int i;
  1092. for (i = 0; i < MAX_INVENTORY; i++)
  1093. {
  1094. if (gpGlobals->rgInventory[i].wItem == 0)
  1095. {
  1096. break;
  1097. }
  1098. if (gpGlobals->rgInventory[i].wItem == wItem)
  1099. {
  1100. return gpGlobals->rgInventory[i].nAmount;
  1101. }
  1102. }
  1103. return 0;
  1104. }
  1105. VOID
  1106. PAL_CompressInventory(
  1107. VOID
  1108. )
  1109. /*++
  1110. Purpose:
  1111. Remove all the items in inventory which has a number of zero.
  1112. Parameters:
  1113. None.
  1114. Return value:
  1115. None.
  1116. --*/
  1117. {
  1118. int i, j;
  1119. j = 0;
  1120. for (i = 0; i < MAX_INVENTORY; i++)
  1121. {
  1122. if (gpGlobals->rgInventory[i].wItem == 0)
  1123. {
  1124. break;
  1125. }
  1126. if (gpGlobals->rgInventory[i].nAmount > 0)
  1127. {
  1128. gpGlobals->rgInventory[j] = gpGlobals->rgInventory[i];
  1129. j++;
  1130. }
  1131. }
  1132. for (; j < MAX_INVENTORY; j++)
  1133. {
  1134. gpGlobals->rgInventory[j].nAmount = 0;
  1135. gpGlobals->rgInventory[j].nAmountInUse = 0;
  1136. gpGlobals->rgInventory[j].wItem = 0;
  1137. }
  1138. }
  1139. BOOL
  1140. PAL_IncreaseHPMP(
  1141. WORD wPlayerRole,
  1142. SHORT sHP,
  1143. SHORT sMP
  1144. )
  1145. /*++
  1146. Purpose:
  1147. Increase or decrease player's HP and/or MP.
  1148. Parameters:
  1149. [IN] wPlayerRole - the number of player role.
  1150. [IN] sHP - number of HP to be increased (positive value) or decrased
  1151. (negative value).
  1152. [IN] sMP - number of MP to be increased (positive value) or decrased
  1153. (negative value).
  1154. Return value:
  1155. TRUE if the operation is succeeded, FALSE if not.
  1156. --*/
  1157. {
  1158. BOOL fSuccess = FALSE;
  1159. //
  1160. // Only care about alive players
  1161. //
  1162. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  1163. {
  1164. //
  1165. // change HP
  1166. //
  1167. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] += sHP;
  1168. if ((SHORT)(gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole]) < 0)
  1169. {
  1170. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] = 0;
  1171. }
  1172. else if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] >
  1173. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole])
  1174. {
  1175. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] =
  1176. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole];
  1177. }
  1178. //
  1179. // Change MP
  1180. //
  1181. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] += sMP;
  1182. if ((SHORT)(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole]) < 0)
  1183. {
  1184. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] = 0;
  1185. }
  1186. else if (gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] >
  1187. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole])
  1188. {
  1189. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] =
  1190. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole];
  1191. }
  1192. fSuccess = TRUE;
  1193. }
  1194. return fSuccess;
  1195. }
  1196. VOID
  1197. PAL_UpdateEquipments(
  1198. VOID
  1199. )
  1200. /*++
  1201. Purpose:
  1202. Update the effects of all equipped items for all players.
  1203. Parameters:
  1204. None.
  1205. Return value:
  1206. None.
  1207. --*/
  1208. {
  1209. int i, j;
  1210. WORD w;
  1211. memset(&(gpGlobals->rgEquipmentEffect), 0, sizeof(gpGlobals->rgEquipmentEffect));
  1212. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  1213. {
  1214. for (j = 0; j < MAX_PLAYER_EQUIPMENTS; j++)
  1215. {
  1216. w = gpGlobals->g.PlayerRoles.rgwEquipment[j][i];
  1217. if (w != 0)
  1218. {
  1219. gpGlobals->g.rgObject[w].item.wScriptOnEquip =
  1220. PAL_RunTriggerScript(gpGlobals->g.rgObject[w].item.wScriptOnEquip, (WORD)i);
  1221. }
  1222. }
  1223. }
  1224. }
  1225. VOID
  1226. PAL_RemoveEquipmentEffect(
  1227. WORD wPlayerRole,
  1228. WORD wEquipPart
  1229. )
  1230. /*++
  1231. Purpose:
  1232. Remove all the effects of the equipment for the player.
  1233. Parameters:
  1234. [IN] wPlayerRole - the player role.
  1235. [IN] wEquipPart - the part of the equipment.
  1236. Return value:
  1237. None.
  1238. --*/
  1239. {
  1240. WORD *p;
  1241. int i, j;
  1242. p = (WORD *)(&gpGlobals->rgEquipmentEffect[wEquipPart]); // HACKHACK
  1243. for (i = 0; i < sizeof(PLAYERROLES) / sizeof(PLAYERS); i++)
  1244. {
  1245. p[i * MAX_PLAYER_ROLES + wPlayerRole] = 0;
  1246. }
  1247. //
  1248. // Reset some parameters to default when appropriate
  1249. //
  1250. if (wEquipPart == kBodyPartHand)
  1251. {
  1252. //
  1253. // reset the dual attack status
  1254. //
  1255. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusDualAttack] = 0;
  1256. }
  1257. else if (wEquipPart == kBodyPartWear)
  1258. {
  1259. //
  1260. // Remove all poisons leveled 99
  1261. //
  1262. for (i = 0; i <= (short)gpGlobals->wMaxPartyMemberIndex; i++)
  1263. {
  1264. if (gpGlobals->rgParty[i].wPlayerRole == wPlayerRole)
  1265. {
  1266. wPlayerRole = i;
  1267. break;
  1268. }
  1269. }
  1270. if (i <= (short)gpGlobals->wMaxPartyMemberIndex)
  1271. {
  1272. j = 0;
  1273. for (i = 0; i < MAX_POISONS; i++)
  1274. {
  1275. WORD w = gpGlobals->rgPoisonStatus[i][wPlayerRole].wPoisonID;
  1276. if (w == 0)
  1277. {
  1278. break;
  1279. }
  1280. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel < 99)
  1281. {
  1282. gpGlobals->rgPoisonStatus[j][wPlayerRole] =
  1283. gpGlobals->rgPoisonStatus[i][wPlayerRole];
  1284. j++;
  1285. }
  1286. }
  1287. while (j < MAX_POISONS)
  1288. {
  1289. gpGlobals->rgPoisonStatus[j][wPlayerRole].wPoisonID = 0;
  1290. gpGlobals->rgPoisonStatus[j][wPlayerRole].wPoisonScript = 0;
  1291. j++;
  1292. }
  1293. }
  1294. }
  1295. }
  1296. VOID
  1297. PAL_AddPoisonForPlayer(
  1298. WORD wPlayerRole,
  1299. WORD wPoisonID
  1300. )
  1301. /*++
  1302. Purpose:
  1303. Add the specified poison to the player.
  1304. Parameters:
  1305. [IN] wPlayerRole - the player role ID.
  1306. [IN] wPoisonID - the poison to be added.
  1307. Return value:
  1308. None.
  1309. --*/
  1310. {
  1311. int i, index;
  1312. WORD w;
  1313. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1314. {
  1315. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1316. {
  1317. break;
  1318. }
  1319. }
  1320. if (index > gpGlobals->wMaxPartyMemberIndex)
  1321. {
  1322. return; // don't go further
  1323. }
  1324. for (i = 0; i < MAX_POISONS; i++)
  1325. {
  1326. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  1327. if (w == 0)
  1328. {
  1329. break;
  1330. }
  1331. if (w == wPoisonID)
  1332. {
  1333. return; // already poisoned
  1334. }
  1335. }
  1336. if (i < MAX_POISONS)
  1337. {
  1338. gpGlobals->rgPoisonStatus[i][index].wPoisonID = wPoisonID;
  1339. gpGlobals->rgPoisonStatus[i][index].wPoisonScript =
  1340. gpGlobals->g.rgObject[wPoisonID].poison.wPlayerScript;
  1341. }
  1342. }
  1343. VOID
  1344. PAL_CurePoisonByKind(
  1345. WORD wPlayerRole,
  1346. WORD wPoisonID
  1347. )
  1348. /*++
  1349. Purpose:
  1350. Remove the specified poison from the player.
  1351. Parameters:
  1352. [IN] wPlayerRole - the player role ID.
  1353. [IN] wPoisonID - the poison to be removed.
  1354. Return value:
  1355. None.
  1356. --*/
  1357. {
  1358. int i, index;
  1359. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1360. {
  1361. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1362. {
  1363. break;
  1364. }
  1365. }
  1366. if (index > gpGlobals->wMaxPartyMemberIndex)
  1367. {
  1368. return; // don't go further
  1369. }
  1370. for (i = 0; i < MAX_POISONS; i++)
  1371. {
  1372. if (gpGlobals->rgPoisonStatus[i][index].wPoisonID == wPoisonID)
  1373. {
  1374. gpGlobals->rgPoisonStatus[i][index].wPoisonID = 0;
  1375. gpGlobals->rgPoisonStatus[i][index].wPoisonScript = 0;
  1376. }
  1377. }
  1378. }
  1379. VOID
  1380. PAL_CurePoisonByLevel(
  1381. WORD wPlayerRole,
  1382. WORD wMaxLevel
  1383. )
  1384. /*++
  1385. Purpose:
  1386. Remove the poisons which have a maximum level of wMaxLevel from the player.
  1387. Parameters:
  1388. [IN] wPlayerRole - the player role ID.
  1389. [IN] wMaxLevel - the maximum level of poisons to be removed.
  1390. Return value:
  1391. None.
  1392. --*/
  1393. {
  1394. int i, index;
  1395. WORD w;
  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. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  1410. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel <= wMaxLevel)
  1411. {
  1412. gpGlobals->rgPoisonStatus[i][index].wPoisonID = 0;
  1413. gpGlobals->rgPoisonStatus[i][index].wPoisonScript = 0;
  1414. }
  1415. }
  1416. }
  1417. BOOL
  1418. PAL_IsPlayerPoisonedByLevel(
  1419. WORD wPlayerRole,
  1420. WORD wMinLevel
  1421. )
  1422. /*++
  1423. Purpose:
  1424. Check if the player is poisoned by poisons at a minimum level of wMinLevel.
  1425. Parameters:
  1426. [IN] wPlayerRole - the player role ID.
  1427. [IN] wMinLevel - the minimum level of poison.
  1428. Return value:
  1429. TRUE if the player is poisoned by poisons at a minimum level of wMinLevel;
  1430. FALSE if not.
  1431. --*/
  1432. {
  1433. int i, index;
  1434. WORD w;
  1435. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1436. {
  1437. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1438. {
  1439. break;
  1440. }
  1441. }
  1442. if (index > gpGlobals->wMaxPartyMemberIndex)
  1443. {
  1444. return FALSE; // don't go further
  1445. }
  1446. for (i = 0; i < MAX_POISONS; i++)
  1447. {
  1448. w = gpGlobals->rgPoisonStatus[i][index].wPoisonID;
  1449. w = gpGlobals->g.rgObject[w].poison.wPoisonLevel;
  1450. if (w >= 99)
  1451. {
  1452. //
  1453. // Ignore poisons which has a level of 99 (usually effect of equipment)
  1454. //
  1455. continue;
  1456. }
  1457. if (w >= wMinLevel)
  1458. {
  1459. return TRUE;
  1460. }
  1461. }
  1462. return FALSE;
  1463. }
  1464. BOOL
  1465. PAL_IsPlayerPoisonedByKind(
  1466. WORD wPlayerRole,
  1467. WORD wPoisonID
  1468. )
  1469. /*++
  1470. Purpose:
  1471. Check if the player is poisoned by the specified poison.
  1472. Parameters:
  1473. [IN] wPlayerRole - the player role ID.
  1474. [IN] wPoisonID - the poison to be checked.
  1475. Return value:
  1476. TRUE if player is poisoned by the specified poison;
  1477. FALSE if not.
  1478. --*/
  1479. {
  1480. int i, index;
  1481. for (index = 0; index <= gpGlobals->wMaxPartyMemberIndex; index++)
  1482. {
  1483. if (gpGlobals->rgParty[index].wPlayerRole == wPlayerRole)
  1484. {
  1485. break;
  1486. }
  1487. }
  1488. if (index > gpGlobals->wMaxPartyMemberIndex)
  1489. {
  1490. return FALSE; // don't go further
  1491. }
  1492. for (i = 0; i < MAX_POISONS; i++)
  1493. {
  1494. if (gpGlobals->rgPoisonStatus[i][index].wPoisonID == wPoisonID)
  1495. {
  1496. return TRUE;
  1497. }
  1498. }
  1499. return FALSE;
  1500. }
  1501. WORD
  1502. PAL_GetPlayerAttackStrength(
  1503. WORD wPlayerRole
  1504. )
  1505. /*++
  1506. Purpose:
  1507. Get the player's attack strength, count in the effect of equipments.
  1508. Parameters:
  1509. [IN] wPlayerRole - the player role ID.
  1510. Return value:
  1511. The total attack strength of the player.
  1512. --*/
  1513. {
  1514. WORD w;
  1515. int i;
  1516. w = gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole];
  1517. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1518. {
  1519. w += gpGlobals->rgEquipmentEffect[i].rgwAttackStrength[wPlayerRole];
  1520. }
  1521. return w;
  1522. }
  1523. WORD
  1524. PAL_GetPlayerMagicStrength(
  1525. WORD wPlayerRole
  1526. )
  1527. /*++
  1528. Purpose:
  1529. Get the player's magic strength, count in the effect of equipments.
  1530. Parameters:
  1531. [IN] wPlayerRole - the player role ID.
  1532. Return value:
  1533. The total magic strength of the player.
  1534. --*/
  1535. {
  1536. WORD w;
  1537. int i;
  1538. w = gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole];
  1539. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1540. {
  1541. w += gpGlobals->rgEquipmentEffect[i].rgwMagicStrength[wPlayerRole];
  1542. }
  1543. return w;
  1544. }
  1545. WORD
  1546. PAL_GetPlayerDefense(
  1547. WORD wPlayerRole
  1548. )
  1549. /*++
  1550. Purpose:
  1551. Get the player's defense value, count in the effect of equipments.
  1552. Parameters:
  1553. [IN] wPlayerRole - the player role ID.
  1554. Return value:
  1555. The total defense value of the player.
  1556. --*/
  1557. {
  1558. WORD w;
  1559. int i;
  1560. w = gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole];
  1561. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1562. {
  1563. w += gpGlobals->rgEquipmentEffect[i].rgwDefense[wPlayerRole];
  1564. }
  1565. return w;
  1566. }
  1567. WORD
  1568. PAL_GetPlayerDexterity(
  1569. WORD wPlayerRole
  1570. )
  1571. /*++
  1572. Purpose:
  1573. Get the player's dexterity, count in the effect of equipments.
  1574. Parameters:
  1575. [IN] wPlayerRole - the player role ID.
  1576. Return value:
  1577. The total dexterity of the player.
  1578. --*/
  1579. {
  1580. WORD w;
  1581. int i;
  1582. w = gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole];
  1583. #ifdef PAL_CLASSIC
  1584. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1585. #else
  1586. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS - 1; i++)
  1587. #endif
  1588. {
  1589. w += gpGlobals->rgEquipmentEffect[i].rgwDexterity[wPlayerRole];
  1590. }
  1591. return w;
  1592. }
  1593. WORD
  1594. PAL_GetPlayerFleeRate(
  1595. WORD wPlayerRole
  1596. )
  1597. /*++
  1598. Purpose:
  1599. Get the player's flee rate, count in the effect of equipments.
  1600. Parameters:
  1601. [IN] wPlayerRole - the player role ID.
  1602. Return value:
  1603. The total flee rate of the player.
  1604. --*/
  1605. {
  1606. WORD w;
  1607. int i;
  1608. w = gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole];
  1609. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1610. {
  1611. w += gpGlobals->rgEquipmentEffect[i].rgwFleeRate[wPlayerRole];
  1612. }
  1613. return w;
  1614. }
  1615. WORD
  1616. PAL_GetPlayerPoisonResistance(
  1617. WORD wPlayerRole
  1618. )
  1619. /*++
  1620. Purpose:
  1621. Get the player's resistance to poisons, count in the effect of equipments.
  1622. Parameters:
  1623. [IN] wPlayerRole - the player role ID.
  1624. Return value:
  1625. The total resistance to poisons of the player.
  1626. --*/
  1627. {
  1628. WORD w;
  1629. int i;
  1630. w = gpGlobals->g.PlayerRoles.rgwPoisonResistance[wPlayerRole];
  1631. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1632. {
  1633. w += gpGlobals->rgEquipmentEffect[i].rgwPoisonResistance[wPlayerRole];
  1634. }
  1635. if (w > 100)
  1636. {
  1637. w = 100;
  1638. }
  1639. return w;
  1640. }
  1641. WORD
  1642. PAL_GetPlayerElementalResistance(
  1643. WORD wPlayerRole,
  1644. INT iAttrib
  1645. )
  1646. /*++
  1647. Purpose:
  1648. Get the player's resistance to attributed magics, count in the effect
  1649. of equipments.
  1650. Parameters:
  1651. [IN] wPlayerRole - the player role ID.
  1652. [IN] iAttrib - the attribute of magics.
  1653. Return value:
  1654. The total resistance to the attributed magics of the player.
  1655. --*/
  1656. {
  1657. WORD w;
  1658. int i;
  1659. w = gpGlobals->g.PlayerRoles.rgwElementalResistance[iAttrib][wPlayerRole];
  1660. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1661. {
  1662. w += gpGlobals->rgEquipmentEffect[i].rgwElementalResistance[iAttrib][wPlayerRole];
  1663. }
  1664. if (w > 100)
  1665. {
  1666. w = 100;
  1667. }
  1668. return w;
  1669. }
  1670. WORD
  1671. PAL_GetPlayerBattleSprite(
  1672. WORD wPlayerRole
  1673. )
  1674. /*++
  1675. Purpose:
  1676. Get player's battle sprite.
  1677. Parameters:
  1678. [IN] wPlayerRole - the player role ID.
  1679. Return value:
  1680. Number of the player's battle sprite.
  1681. --*/
  1682. {
  1683. int i;
  1684. WORD w;
  1685. w = gpGlobals->g.PlayerRoles.rgwSpriteNumInBattle[wPlayerRole];
  1686. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1687. {
  1688. if (gpGlobals->rgEquipmentEffect[i].rgwSpriteNumInBattle[wPlayerRole] != 0)
  1689. {
  1690. w = gpGlobals->rgEquipmentEffect[i].rgwSpriteNumInBattle[wPlayerRole];
  1691. }
  1692. }
  1693. return w;
  1694. }
  1695. WORD
  1696. PAL_GetPlayerCooperativeMagic(
  1697. WORD wPlayerRole
  1698. )
  1699. /*++
  1700. Purpose:
  1701. Get player's cooperative magic.
  1702. Parameters:
  1703. [IN] wPlayerRole - the player role ID.
  1704. Return value:
  1705. Object ID of the player's cooperative magic.
  1706. --*/
  1707. {
  1708. int i;
  1709. WORD w;
  1710. w = gpGlobals->g.PlayerRoles.rgwCooperativeMagic[wPlayerRole];
  1711. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1712. {
  1713. if (gpGlobals->rgEquipmentEffect[i].rgwCooperativeMagic[wPlayerRole] != 0)
  1714. {
  1715. w = gpGlobals->rgEquipmentEffect[i].rgwCooperativeMagic[wPlayerRole];
  1716. }
  1717. }
  1718. return w;
  1719. }
  1720. BOOL
  1721. PAL_PlayerCanAttackAll(
  1722. WORD wPlayerRole
  1723. )
  1724. /*++
  1725. Purpose:
  1726. Check if the player can attack all of the enemies in one move.
  1727. Parameters:
  1728. [IN] wPlayerRole - the player role ID.
  1729. Return value:
  1730. TRUE if player can attack all of the enemies in one move, FALSE if not.
  1731. --*/
  1732. {
  1733. int i;
  1734. BOOL f;
  1735. f = FALSE;
  1736. for (i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
  1737. {
  1738. if (gpGlobals->rgEquipmentEffect[i].rgwAttackAll[wPlayerRole] != 0)
  1739. {
  1740. f = TRUE;
  1741. break;
  1742. }
  1743. }
  1744. return f;
  1745. }
  1746. BOOL
  1747. PAL_AddMagic(
  1748. WORD wPlayerRole,
  1749. WORD wMagic
  1750. )
  1751. /*++
  1752. Purpose:
  1753. Add a magic to the player.
  1754. Parameters:
  1755. [IN] wPlayerRole - the player role ID.
  1756. [IN] wMagic - the object ID of the magic.
  1757. Return value:
  1758. TRUE if succeeded, FALSE if failed.
  1759. --*/
  1760. {
  1761. int i;
  1762. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1763. {
  1764. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == wMagic)
  1765. {
  1766. //
  1767. // already have this magic
  1768. //
  1769. return FALSE;
  1770. }
  1771. }
  1772. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1773. {
  1774. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == 0)
  1775. {
  1776. break;
  1777. }
  1778. }
  1779. if (i >= MAX_PLAYER_MAGICS)
  1780. {
  1781. //
  1782. // Not enough slots
  1783. //
  1784. return FALSE;
  1785. }
  1786. gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] = wMagic;
  1787. return TRUE;
  1788. }
  1789. VOID
  1790. PAL_RemoveMagic(
  1791. WORD wPlayerRole,
  1792. WORD wMagic
  1793. )
  1794. /*++
  1795. Purpose:
  1796. Remove a magic to the player.
  1797. Parameters:
  1798. [IN] wPlayerRole - the player role ID.
  1799. [IN] wMagic - the object ID of the magic.
  1800. Return value:
  1801. None.
  1802. --*/
  1803. {
  1804. int i;
  1805. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  1806. {
  1807. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == wMagic)
  1808. {
  1809. gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] = 0;
  1810. break;
  1811. }
  1812. }
  1813. }
  1814. VOID
  1815. PAL_SetPlayerStatus(
  1816. WORD wPlayerRole,
  1817. WORD wStatusID,
  1818. WORD wNumRound
  1819. )
  1820. /*++
  1821. Purpose:
  1822. Set one of the statuses for the player.
  1823. Parameters:
  1824. [IN] wPlayerRole - the player ID.
  1825. [IN] wStatusID - the status to be set.
  1826. [IN] wNumRound - the effective rounds of the status.
  1827. Return value:
  1828. None.
  1829. --*/
  1830. {
  1831. #ifndef PAL_CLASSIC
  1832. if (wStatusID == kStatusSlow &&
  1833. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusHaste] > 0)
  1834. {
  1835. //
  1836. // Remove the haste status
  1837. //
  1838. PAL_RemovePlayerStatus(wPlayerRole, kStatusHaste);
  1839. return;
  1840. }
  1841. if (wStatusID == kStatusHaste &&
  1842. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSlow] > 0)
  1843. {
  1844. //
  1845. // Remove the slow status
  1846. //
  1847. PAL_RemovePlayerStatus(wPlayerRole, kStatusSlow);
  1848. return;
  1849. }
  1850. #endif
  1851. switch (wStatusID)
  1852. {
  1853. case kStatusConfused:
  1854. case kStatusSleep:
  1855. case kStatusSilence:
  1856. #ifdef PAL_CLASSIC
  1857. case kStatusParalyzed:
  1858. #else
  1859. case kStatusSlow:
  1860. #endif
  1861. //
  1862. // for "bad" statuses, don't set the status when we already have it
  1863. //
  1864. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] != 0 &&
  1865. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] == 0)
  1866. {
  1867. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1868. }
  1869. break;
  1870. case kStatusPuppet:
  1871. //
  1872. // only allow dead players for "puppet" status
  1873. //
  1874. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 &&
  1875. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] < wNumRound)
  1876. {
  1877. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1878. }
  1879. break;
  1880. case kStatusBravery:
  1881. case kStatusProtect:
  1882. case kStatusDualAttack:
  1883. case kStatusHaste:
  1884. //
  1885. // for "good" statuses, reset the status if the status to be set lasts longer
  1886. //
  1887. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] != 0 &&
  1888. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] < wNumRound)
  1889. {
  1890. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
  1891. }
  1892. break;
  1893. default:
  1894. assert(FALSE);
  1895. break;
  1896. }
  1897. }
  1898. VOID
  1899. PAL_RemovePlayerStatus(
  1900. WORD wPlayerRole,
  1901. WORD wStatusID
  1902. )
  1903. /*++
  1904. Purpose:
  1905. Remove one of the status for player.
  1906. Parameters:
  1907. [IN] wPlayerRole - the player ID.
  1908. [IN] wStatusID - the status to be set.
  1909. Return value:
  1910. None.
  1911. --*/
  1912. {
  1913. //
  1914. // Don't remove effects of equipments
  1915. //
  1916. if (gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] <= 999)
  1917. {
  1918. gpGlobals->rgPlayerStatus[wPlayerRole][wStatusID] = 0;
  1919. }
  1920. }
  1921. VOID
  1922. PAL_ClearAllPlayerStatus(
  1923. VOID
  1924. )
  1925. /*++
  1926. Purpose:
  1927. Clear all player status.
  1928. Parameters:
  1929. None.
  1930. Return value:
  1931. None.
  1932. --*/
  1933. {
  1934. int i, j;
  1935. for (i = 0; i < MAX_PLAYER_ROLES; i++)
  1936. {
  1937. for (j = 0; j < kStatusAll; j++)
  1938. {
  1939. //
  1940. // Don't remove effects of equipments
  1941. //
  1942. if (gpGlobals->rgPlayerStatus[i][j] <= 999)
  1943. {
  1944. gpGlobals->rgPlayerStatus[i][j] = 0;
  1945. }
  1946. }
  1947. }
  1948. }
  1949. VOID
  1950. PAL_PlayerLevelUp(
  1951. WORD wPlayerRole,
  1952. WORD wNumLevel
  1953. )
  1954. /*++
  1955. Purpose:
  1956. Increase the player's level by wLevels.
  1957. Parameters:
  1958. [IN] wPlayerRole - player role ID.
  1959. [IN] wNumLevel - number of levels to be increased.
  1960. Return value:
  1961. None.
  1962. --*/
  1963. {
  1964. WORD i;
  1965. //
  1966. // Add the level
  1967. //
  1968. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] += wNumLevel;
  1969. if (gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] > MAX_LEVELS)
  1970. {
  1971. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole] = MAX_LEVELS;
  1972. }
  1973. for (i = 0; i < wNumLevel; i++)
  1974. {
  1975. //
  1976. // Increase player's stats
  1977. //
  1978. gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole] += 10 + RandomLong(0, 8);
  1979. gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole] += 8 + RandomLong(0, 6);
  1980. gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole] += 4 + RandomLong(0, 1);
  1981. gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole] += 4 + RandomLong(0, 1);
  1982. gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole] += 2 + RandomLong(0, 1);
  1983. gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole] += 2 + RandomLong(0, 1);
  1984. gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole] += 2;
  1985. }
  1986. #define STAT_LIMIT(t) { if ((t) > 999) (t) = 999; }
  1987. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole]);
  1988. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole]);
  1989. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwAttackStrength[wPlayerRole]);
  1990. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwMagicStrength[wPlayerRole]);
  1991. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwDefense[wPlayerRole]);
  1992. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwDexterity[wPlayerRole]);
  1993. STAT_LIMIT(gpGlobals->g.PlayerRoles.rgwFleeRate[wPlayerRole]);
  1994. #undef STAT_LIMIT
  1995. //
  1996. // Reset experience points to zero
  1997. //
  1998. gpGlobals->Exp.rgPrimaryExp[wPlayerRole].wExp = 0;
  1999. gpGlobals->Exp.rgPrimaryExp[wPlayerRole].wLevel =
  2000. gpGlobals->g.PlayerRoles.rgwLevel[wPlayerRole];
  2001. }