// // Common non-engine code/data for EDuke32 and Mapster32 // #include "ns.h" // Must come before everything else! #include "compat.h" #include "build.h" #include "baselayer.h" #include "palette.h" #include "gamecvars.h" #include "cmdlib.h" #include "grpscan.h" #include "rts.h" #include "vfs.h" #ifdef _WIN32 # include "windows_inc.h" # include "win32/winbits.h" #elif defined __APPLE__ # include "osxbits.h" #endif #include "common.h" #include "common_game.h" BEGIN_DUKE_NS struct grpfile_t const *g_selectedGrp; int32_t g_gameType = GAMEFLAG_DUKE; int g_addonNum = 0; // g_gameNamePtr can point to one of: grpfiles[].name (string literal), string // literal, malloc'd block (XXX: possible leak) const char *g_gameNamePtr = NULL; // grp/con handling static const char *defaultconfilename = "GAME.CON"; #ifndef EDUKE32_STANDALONE static const char *defaultgamegrp[GAMECOUNT] = { "DUKE3D.GRP", "NAM.GRP", "NAPALM.GRP", "WW2GI.GRP" }; static const char *defaultdeffilename[GAMECOUNT] = { "duke3d.def", "nam.def", "napalm.def", "ww2gi.def" }; static const char *defaultgameconfilename[GAMECOUNT] = { "EDUKE.CON", "NAM.CON", "NAPALM.CON", "WW2GI.CON" }; #endif // g_grpNamePtr can ONLY point to a malloc'd block (length BMAX_PATH) char *g_grpNamePtr = NULL; // g_scriptNamePtr can ONLY point to a malloc'd block (length BMAX_PATH) char *g_scriptNamePtr = NULL; void clearGrpNamePtr(void) { Xfree(g_grpNamePtr); // g_grpNamePtr assumed to be assigned to right after } void clearScriptNamePtr(void) { Xfree(g_scriptNamePtr); // g_scriptNamePtr assumed to be assigned to right after } const char *G_DefaultGrpFile(void) { #ifndef EDUKE32_STANDALONE if (DUKE) return defaultgamegrp[GAME_DUKE]; else if (NAPALM) return defaultgamegrp[GAME_NAPALM]; else if (WW2GI) return defaultgamegrp[GAME_WW2GI]; else if (NAM) return defaultgamegrp[GAME_NAM]; return defaultgamegrp[0]; #else return "(none)"; #endif } const char *G_DefaultDefFile(void) { #ifndef EDUKE32_STANDALONE if (DUKE) return defaultdeffilename[GAME_DUKE]; else if (WW2GI) return defaultdeffilename[GAME_WW2GI]; else if (NAPALM) { if (!testkopen(defaultdeffilename[GAME_NAPALM],0) && testkopen(defaultdeffilename[GAME_NAM],0)) return defaultdeffilename[GAME_NAM]; // NAM/NAPALM Sharing else return defaultdeffilename[GAME_NAPALM]; } else if (NAM) { if (!testkopen(defaultdeffilename[GAME_NAM],0) && testkopen(defaultdeffilename[GAME_NAPALM],0)) return defaultdeffilename[GAME_NAPALM]; // NAM/NAPALM Sharing else return defaultdeffilename[GAME_NAM]; } return defaultdeffilename[0]; #else return "(none)"; #endif } const char *G_DefaultConFile(void) { #ifndef EDUKE32_STANDALONE if (DUKE && testkopen(defaultgameconfilename[GAME_DUKE],0)) return defaultgameconfilename[GAME_DUKE]; else if (WW2GI && testkopen(defaultgameconfilename[GAME_WW2GI],0)) return defaultgameconfilename[GAME_WW2GI]; else if (NAPALM) { if (!testkopen(defaultgameconfilename[GAME_NAPALM],0)) { if (testkopen(defaultgameconfilename[GAME_NAM],0)) return defaultgameconfilename[GAME_NAM]; // NAM/NAPALM Sharing } else return defaultgameconfilename[GAME_NAPALM]; } else if (NAM) { if (!testkopen(defaultgameconfilename[GAME_NAM],0)) { if (testkopen(defaultgameconfilename[GAME_NAPALM],0)) return defaultgameconfilename[GAME_NAPALM]; // NAM/NAPALM Sharing } else return defaultgameconfilename[GAME_NAM]; } #endif return defaultconfilename; } const char *G_GrpFile(void) { return (g_grpNamePtr == NULL) ? G_DefaultGrpFile() : g_grpNamePtr; } const char *G_DefFile(void) { return (g_defNamePtr == NULL) ? G_DefaultDefFile() : g_defNamePtr; } const char *G_ConFile(void) { return (g_scriptNamePtr == NULL) ? G_DefaultConFile() : g_scriptNamePtr; } ////////// // Set up new-style multi-psky handling. void G_InitMultiPsky(int CLOUDYOCEAN__DYN, int MOONSKY1__DYN, int BIGORBIT1__DYN, int LA__DYN) { // When adding other multi-skies, take care that the tileofs[] values are // <= PSKYOFF_MAX. (It can be increased up to MAXPSKYTILES, but should be // set as tight as possible.) // The default sky properties (all others are implicitly zero): psky_t *sky = tileSetupSky(DEFAULTPSKY); sky->lognumtiles = 3; sky->horizfrac = 32768; // CLOUDYOCEAN // Aligns with the drawn scene horizon because it has one itself. sky = tileSetupSky(CLOUDYOCEAN__DYN); sky->lognumtiles = 3; sky->horizfrac = 65536; // MOONSKY1 // earth mountain mountain sun sky = tileSetupSky(MOONSKY1__DYN); sky->lognumtiles = 3; sky->horizfrac = 32768; sky->tileofs[6] = 1; sky->tileofs[1] = 2; sky->tileofs[4] = 2; sky->tileofs[2] = 3; // BIGORBIT1 // orbit // earth1 2 3 moon/sun sky = tileSetupSky(BIGORBIT1__DYN); sky->lognumtiles = 3; sky->horizfrac = 32768; sky->tileofs[5] = 1; sky->tileofs[6] = 2; sky->tileofs[7] = 3; sky->tileofs[2] = 4; // LA // la city // earth1 2 3 moon/sun sky = tileSetupSky(LA__DYN); sky->lognumtiles = 3; sky->horizfrac = 16384 + 1024; sky->tileofs[0] = 1; sky->tileofs[1] = 2; sky->tileofs[2] = 1; sky->tileofs[3] = 3; sky->tileofs[4] = 4; sky->tileofs[5] = 0; sky->tileofs[6] = 2; sky->tileofs[7] = 3; #if 0 // This assertion should hold. See note above. for (bssize_t i=0; i= 0; i--) { if (sector[i].ceilingstat & 1) { skyIdx = getpskyidx(sector[i].ceilingpicnum); if (skyIdx > 0) break; } } g_pskyidx = skyIdx; } ////////// //char g_modDir[BMAX_PATH] = "/"; static void G_LoadAddon(void); int32_t g_groupFileHandle; struct strllist* CommandPaths, * CommandGrps; void G_ExtInit(void) { #ifdef EDUKE32_OSX char *appdir = Bgetappdir(); addsearchpath(appdir); Xfree(appdir); #endif char cwd[BMAX_PATH]; #ifdef USE_PHYSFS strncpy(cwd, PHYSFS_getBaseDir(), ARRAY_SIZE(cwd)); cwd[ARRAY_SIZE(cwd)-1] = '\0'; #else if (buildvfs_getcwd(cwd, ARRAY_SIZE(cwd)) && Bstrcmp(cwd, "/") != 0) #endif addsearchpath(cwd); if (CommandPaths) { int32_t i; struct strllist *s; while (CommandPaths) { s = CommandPaths->next; i = addsearchpath(CommandPaths->str); if (i < 0) { initprintf("Failed adding %s for game data: %s\n", CommandPaths->str, i==-1 ? "not a directory" : "no such directory"); } Xfree(CommandPaths->str); Xfree(CommandPaths); CommandPaths = s; } } } void G_ScanGroups(void) { ScanGroups(); g_selectedGrp = NULL; char const * const currentGrp = G_GrpFile(); for (grpfile_t const *fg = foundgrps; fg; fg=fg->next) { if (!Bstrcasecmp(fg->filename, currentGrp)) { g_selectedGrp = fg; break; } } if (g_selectedGrp == NULL) g_selectedGrp = foundgrps; } static int32_t G_TryLoadingGrp(char const * const grpfile) { int32_t i; if ((i = initgroupfile(grpfile)) == -1) initprintf("Warning: could not find main data file \"%s\"!\n", grpfile); else initprintf("Using \"%s\" as main game data file.\n", grpfile); return i; } static int32_t G_LoadGrpDependencyChain(grpfile_t const * const grp) { if (!grp) return -1; if (grp->type->dependency && grp->type->dependency != grp->type->crcval) G_LoadGrpDependencyChain(FindGroup(grp->type->dependency)); int32_t const i = G_TryLoadingGrp(grp->filename); if (grp->type->postprocessing) grp->type->postprocessing(i); return i; } void G_LoadGroups() { if (g_modDir[0] != '/') { char cwd[BMAX_PATH]; FString g_rootDir = progdir + g_modDir; addsearchpath(g_rootDir); // addsearchpath(mod_dir); char path[BMAX_PATH]; if (buildvfs_getcwd(cwd, BMAX_PATH)) { Bsnprintf(path, sizeof(path), "%s/%s", cwd, g_modDir); if (!Bstrcmp(g_rootDir, path)) { addsearchpath(path); } } } if (g_addonNum) G_LoadAddon(); const char *grpfile; int32_t i; if ((i = G_LoadGrpDependencyChain(g_selectedGrp)) != -1) { grpfile = g_selectedGrp->filename; clearGrpNamePtr(); g_grpNamePtr = dup_filename(grpfile); grpinfo_t const * const type = g_selectedGrp->type; g_gameType = type->game; g_gameNamePtr = type->name; if (type->scriptname && g_scriptNamePtr == NULL) g_scriptNamePtr = dup_filename(type->scriptname); if (type->defname && g_defNamePtr == NULL) g_defNamePtr = dup_filename(type->defname); if (type->rtsname) RTS_Init(type->rtsname); } else { grpfile = G_GrpFile(); i = G_TryLoadingGrp(grpfile); } if (G_AllowAutoload()) { G_LoadGroupsInDir("autoload"); if (i != -1) G_DoAutoload(grpfile); } if (g_modDir[0] != '/') G_LoadGroupsInDir(g_modDir); #ifndef EDUKE32_STANDALONE if (g_defNamePtr == NULL) { const char *tmpptr = getenv("DUKE3DDEF"); if (tmpptr) { clearDefNamePtr(); g_defNamePtr = dup_filename(tmpptr); initprintf("Using \"%s\" as definitions file\n", g_defNamePtr); } } #endif loaddefinitions_game(G_DefFile(), TRUE); struct strllist *s; int const bakpathsearchmode = pathsearchmode; pathsearchmode = 1; while (CommandGrps) { int32_t j; s = CommandGrps->next; if ((j = initgroupfile(CommandGrps->str)) == -1) initprintf("Could not find file \"%s\".\n", CommandGrps->str); else { g_groupFileHandle = j; initprintf("Using file \"%s\" as game data.\n", CommandGrps->str); if (G_AllowAutoload()) G_DoAutoload(CommandGrps->str); } Xfree(CommandGrps->str); Xfree(CommandGrps); CommandGrps = s; } pathsearchmode = bakpathsearchmode; } static void G_LoadAddon(void) { #ifndef EDUKE32_STANDALONE int32_t crc = 0; // compiler-happy switch (g_addonNum) { case ADDON_DUKEDC: crc = DUKEDC_CRC; break; case ADDON_NWINTER: crc = DUKENW_CRC; break; case ADDON_CARIBBEAN: crc = DUKECB_CRC; break; } if (!crc) return; grpfile_t const * const grp = FindGroup(crc); if (grp) g_selectedGrp = grp; #endif } void G_CleanupSearchPaths(void) { removesearchpaths_withuser(SEARCHPATH_REMOVE); if (!NAM) removesearchpaths_withuser(SEARCHPATH_NAM); if (!WW2GI) removesearchpaths_withuser(SEARCHPATH_WW2GI); } ////////// GrowArray g_scriptModules; void G_AddGroup(const char *buffer) { char buf[BMAX_PATH]; struct strllist *s = (struct strllist *)Xcalloc(1,sizeof(struct strllist)); Bstrcpy(buf, buffer); if (Bstrchr(buf,'.') == 0) Bstrcat(buf,".grp"); s->str = Xstrdup(buf); if (CommandGrps) { struct strllist *t; for (t = CommandGrps; t->next; t=t->next) ; t->next = s; return; } CommandGrps = s; } void G_AddPath(const char *buffer) { struct strllist *s = (struct strllist *)Xcalloc(1,sizeof(struct strllist)); s->str = Xstrdup(buffer); if (CommandPaths) { struct strllist *t; for (t = CommandPaths; t->next; t=t->next) ; t->next = s; return; } CommandPaths = s; } void G_AddCon(const char *buffer) { clearScriptNamePtr(); g_scriptNamePtr = dup_filename(buffer); initprintf("Using CON file \"%s\".\n",g_scriptNamePtr); } void G_AddConModule(const char *buffer) { g_scriptModules.append(Xstrdup(buffer)); } ////////// // loads all group (grp, zip, pk3/4) files in the given directory void G_LoadGroupsInDir(const char *dirname) { static const char *extensions[] = { "*.grp", "*.zip", "*.ssi", "*.pk3", "*.pk4" }; char buf[BMAX_PATH]; fnlist_t fnlist = FNLIST_INITIALIZER; for (auto & extension : extensions) { CACHE1D_FIND_REC *rec; fnlist_getnames(&fnlist, dirname, extension, -1, 0); for (rec=fnlist.findfiles; rec; rec=rec->next) { Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name); initprintf("Using group file \"%s\".\n", buf); initgroupfile(buf); } fnlist_clearnames(&fnlist); } } void G_DoAutoload(const char *dirname) { char buf[BMAX_PATH]; Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname); G_LoadGroupsInDir(buf); } ////////// void G_LoadLookups(void) { int32_t j; auto fr = kopenFileReader("lookup.dat", 0); if (!fr.isOpen()) return; j = paletteLoadLookupTable(fr); if (j < 0) { if (j == -1) initprintf("ERROR loading \"lookup.dat\": failed reading enough data.\n"); return; } uint8_t paldata[768]; for (j=1; j<=5; j++) { // Account for TITLE and REALMS swap between basepal number and on-disk order. int32_t basepalnum = (j == 3 || j == 4) ? 4+3-j : j; if (fr.Read(paldata, 768) != 768) return; for (unsigned char & k : paldata) k <<= 2; paletteSetColorTable(basepalnum, paldata); } } ////////// #ifdef FORMAT_UPGRADE_ELIGIBLE static FileReader S_TryFormats(char * const testfn, char * const fn_suffix, char const searchfirst) { #ifdef HAVE_FLAC { Bstrcpy(fn_suffix, ".flac"); auto fp = kopenFileReader(testfn, searchfirst); if (fp.isOpen()) return fp; } #endif #ifdef HAVE_VORBIS { Bstrcpy(fn_suffix, ".ogg"); auto fp = kopenFileReader(testfn, searchfirst); if (fp.isOpen()) return fp; } #endif return FileReader(); } static FileReader S_TryExtensionReplacements(char * const testfn, char const searchfirst, uint8_t const ismusic) { char * extension = Bstrrchr(testfn, '.'); char * const fn_end = Bstrchr(testfn, '\0'); // ex: grabbag.voc --> grabbag_voc.* if (extension != NULL) { *extension = '_'; auto fp = S_TryFormats(testfn, fn_end, searchfirst); if (fp.isOpen()) return fp; } else { extension = fn_end; } // ex: grabbag.mid --> grabbag.* if (ismusic) { auto fp = S_TryFormats(testfn, extension, searchfirst); if (fp.isOpen()) return fp; } return FileReader(); } FileReader S_OpenAudio(const char *fn, char searchfirst, uint8_t const ismusic) { auto origfp = kopenFileReader(fn, searchfirst); if (!snd_tryformats) return origfp; char const* const origparent = origfp.isOpen() ? kfileparent(origfp) : NULL; uint32_t const parentlength = origparent != NULL ? Bstrlen(origparent) : 0; auto testfn = (char *)Xmalloc(Bstrlen(fn) + 12 + parentlength); // "music/" + overestimation of parent minus extension + ".flac" + '\0' // look in ./ // ex: ./grabbag.mid Bstrcpy(testfn, fn); auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); if (fp.isOpen()) { Bfree(testfn); return fp; } // look in ./music// // ex: ./music/duke3d/grabbag.mid // ex: ./music/nwinter/grabbag.mid if (origparent != NULL) { char const * const parentextension = Bstrrchr(origparent, '.'); uint32_t const namelength = parentextension != NULL ? (unsigned)(parentextension - origparent) : parentlength; Bsprintf(testfn, "music/%.*s/%s", namelength, origparent, fn); auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); if (fp.isOpen()) { Bfree(testfn); return fp; } } // look in ./music/ // ex: ./music/grabbag.mid { Bsprintf(testfn, "music/%s", fn); auto fp = S_TryExtensionReplacements(testfn, searchfirst, ismusic); if (fp.isOpen()) { Bfree(testfn); return fp; } } Bfree(testfn); return origfp; } void Duke_CommonCleanup(void) { DO_FREE_AND_NULL(g_grpNamePtr); DO_FREE_AND_NULL(g_scriptNamePtr); } #endif END_DUKE_NS