Skip to content

Commit

Permalink
Refactor|libcommon: Moved the MapInfo set into a simple HexDefs struc…
Browse files Browse the repository at this point in the history
…ture
  • Loading branch information
danij-deng committed Jul 21, 2014
1 parent 097a39d commit 928ec71
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 40 deletions.
35 changes: 24 additions & 11 deletions doomsday/plugins/common/include/mapinfo.h
Expand Up @@ -27,6 +27,8 @@

namespace common {

struct HexDefs; // Forward

class MapInfo : public de::Record
{
public:
Expand All @@ -36,10 +38,6 @@ class MapInfo : public de::Record
void resetToDefaults();
};

// Central MapInfo database.
typedef std::map<std::string, MapInfo> MapInfos;
extern MapInfos mapInfos;

/**
* Parser for Hexen's MAPINFO definition lumps.
*/
Expand All @@ -50,13 +48,13 @@ class MapInfoParser
DENG2_ERROR(ParseError);

public:
MapInfoParser();
MapInfoParser(HexDefs &db);

void parse(AutoStr const &buffer, de::String sourceFile);

/**
* Clear any custom default map definition currently in use. Map definitions
* read after this is called will use the games' default map definition as a
* Clear any custom default MapInfo definition currently in use. MapInfos
* read after this is called will use the games' default definition as a
* basis (unless specified otherwise).
*/
void clearDefaultMap();
Expand All @@ -66,12 +64,27 @@ class MapInfoParser
};

/**
* @param mapUri Identifier of the map to lookup info for. Can be @c 0 in which
* case the info for the @em current map will be returned (if set).
* Central database of definitions read from Hexen-derived definition formats.
*
* @return MAPINFO data for the specified @a mapUri; otherwise @c 0 (not found).
* @note Ultimately the definitions this contains should instead have their sources
* translated into DED syntax and be made available from the main DED db instead.
*/
MapInfo *P_MapInfo(de::Uri const *mapUri = 0);
struct HexDefs
{
typedef std::map<std::string, MapInfo> MapInfos;
MapInfos mapInfos;

void clear();

/**
* @param mapUri Identifier of the map to lookup info for. Can be @c 0 in which
* case the info for the @em current map will be returned (if set).
*
* @return MAPINFO data for the specified @a mapUri; otherwise @c 0 (not found).
*/
MapInfo *getMapInfo(de::Uri const *mapUri = 0);
};
extern HexDefs hexDefs;

/**
* Translates a warp map number to unique map identifier, if possible.
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/common/src/fi_lib.cpp
Expand Up @@ -88,12 +88,12 @@ static void initStateConditions(fi_state_t *s)

#if __JHEXEN__
// Leaving the current hub?
if(MapInfo *curMapInfo = P_MapInfo(0/*current map*/))
if(MapInfo *curMapInfo = hexDefs.getMapInfo(0/*current map*/))
{
s->conditions.leave_hub = true;
if(!nextMapUri.path().isEmpty())
{
if(curMapInfo->geti("hub") == P_MapInfo(&nextMapUri)->geti("hub"))
if(curMapInfo->geti("hub") == hexDefs.getMapInfo(&nextMapUri)->geti("hub"))
{
s->conditions.leave_hub = false;
}
Expand Down
10 changes: 5 additions & 5 deletions doomsday/plugins/common/src/g_game.cpp
Expand Up @@ -1193,7 +1193,7 @@ static void printMapBanner()
{
String text = String("Map: ") + gameMapUri.path().asText();
#if __JHEXEN__
MapInfo const *mapInfo = P_MapInfo(0/*current map*/);
MapInfo const *mapInfo = hexDefs.getMapInfo(0/*current map*/);
text += String(" (%1)").arg(mapInfo? mapInfo->geti("warpTrans") + 1 : 0);
#endif
text += String(" - " DE2_ESC(b)) + title;
Expand Down Expand Up @@ -1796,7 +1796,7 @@ void G_PlayerLeaveMap(int player)
dd_bool newHub = true;
if(!nextMapUri.path().isEmpty())
{
newHub = (P_MapInfo(0/*current map*/)->geti("hub") != P_MapInfo(&nextMapUri)->geti("hub"));
newHub = (hexDefs.getMapInfo(0/*current map*/)->geti("hub") != hexDefs.getMapInfo(&nextMapUri)->geti("hub"));
}
#endif

Expand Down Expand Up @@ -2412,7 +2412,7 @@ de::Uri G_ComposeMapUri(uint episode, uint map)
de::Uri G_NextMap(dd_bool secretExit)
{
#if __JHEXEN__
return P_TranslateMap(P_MapInfo(&gameMapUri)->geti("nextMap"));
return P_TranslateMap(hexDefs.getMapInfo(&gameMapUri)->geti("nextMap"));
DENG2_UNUSED(secretExit);

#elif __JDOOM64__
Expand Down Expand Up @@ -2577,7 +2577,7 @@ String G_MapTitle(de::Uri const *mapUri)
// In Hexen we can also look in MAPINFO for the map title.
if(title.isEmpty())
{
if(MapInfo const *mapInfo = P_MapInfo(mapUri))
if(MapInfo const *mapInfo = hexDefs.getMapInfo(mapUri))
{
title = mapInfo->gets("title");
}
Expand Down Expand Up @@ -2691,7 +2691,7 @@ char const *G_InFineDebriefing(de::Uri const *mapUri)
#if __JHEXEN__
if(cfg.overrideHubMsg && G_GameState() == GS_MAP && !nextMapUri.path().isEmpty())
{
if(P_MapInfo(mapUri)->geti("hub") != P_MapInfo(&nextMapUri)->geti("hub"))
if(hexDefs.getMapInfo(mapUri)->geti("hub") != hexDefs.getMapInfo(&nextMapUri)->geti("hub"))
{
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/gamesession.cpp
Expand Up @@ -970,7 +970,7 @@ void GameSession::leaveMap()

// Are we entering a new hub?
#if __JHEXEN__
if(P_MapInfo(0/*current map*/)->geti("hub") != P_MapInfo(&nextMapUri)->geti("hub"))
if(hexDefs.getMapInfo(0/*current map*/)->geti("hub") != hexDefs.getMapInfo(&nextMapUri)->geti("hub"))
#endif
{
// Clear all saved map states in the old hub.
Expand Down
33 changes: 21 additions & 12 deletions doomsday/plugins/common/src/mapinfo.cpp
Expand Up @@ -32,7 +32,7 @@ using namespace de;

namespace common {

MapInfos mapInfos;
HexDefs hexDefs;

namespace internal {

Expand Down Expand Up @@ -104,11 +104,16 @@ void MapInfo::resetToDefaults()
*/
DENG2_PIMPL(MapInfoParser)
{
AutoStr const *buffer;
HexDefs &db;
HexLex lexer;
MapInfo *defaultMap;

Instance(Public *i) : Base(i), buffer(0), defaultMap(0) {}
Instance(Public *i, HexDefs &db)
: Base(i)
, db (db)
, defaultMap(0)
{}

~Instance() { clearDefaultMap(); }

void addDefaultMapIfNeeded(bool resetToDefaultsIfPresent = true)
Expand Down Expand Up @@ -377,12 +382,12 @@ DENG2_PIMPL(MapInfoParser)
}

// Lookup an existing map info from the database.
info = P_MapInfo(&mapUri);
info = db.getMapInfo(&mapUri);

if(!info)
{
// A new map info.
info = &mapInfos[mapUri.path().asText().toLower().toUtf8().constData()];
info = &db.mapInfos[mapUri.path().asText().toLower().toUtf8().constData()];

// Initialize with custom default values?
if(defaultMap)
Expand Down Expand Up @@ -934,7 +939,7 @@ DENG2_PIMPL(MapInfoParser)
}
};

MapInfoParser::MapInfoParser()
MapInfoParser::MapInfoParser(HexDefs &db) : d(new Instance(this, db))
{}

void MapInfoParser::clearDefaultMap()
Expand All @@ -947,11 +952,10 @@ void MapInfoParser::parse(AutoStr const &buffer, String /*sourceFile*/)
LOG_AS("MapInfoParser");

// Nothing to parse?
d->buffer = &buffer;
if(Str_IsEmpty(d->buffer))
if(Str_IsEmpty(&buffer))
return;

d->lexer.parse(d->buffer);
d->lexer.parse(&buffer);
while(d->lexer.readToken())
{
if(!Str_CompareIgnoreCase(d->lexer.token(), "cd_start_track"))
Expand Down Expand Up @@ -1043,7 +1047,12 @@ void MapInfoParser::parse(AutoStr const &buffer, String /*sourceFile*/)
}
}

MapInfo *P_MapInfo(de::Uri const *mapUri)
void HexDefs::clear()
{
mapInfos.clear();
}

MapInfo *HexDefs::getMapInfo(de::Uri const *mapUri)
{
if(!mapUri) mapUri = &gameMapUri;

Expand All @@ -1054,15 +1063,15 @@ MapInfo *P_MapInfo(de::Uri const *mapUri)
{
return &found->second;
}
//App_Log(DE2_DEV_MAP_NOTE, "Unknown MAPINFO definition '%s'", Str_Text(mapUriStr));
//LOGDEV_MAP_NOTE("Unknown MAPINFO definition '%s'") << Str_Text(mapUriStr);
return 0;
}

de::Uri P_TranslateMapIfExists(uint map)
{
de::Uri matchedWithoutHub("Maps:", RC_NULL);

for(MapInfos::const_iterator i = mapInfos.begin(); i != mapInfos.end(); ++i)
for(HexDefs::MapInfos::const_iterator i = hexDefs.mapInfos.begin(); i != hexDefs.mapInfos.end(); ++i)
{
MapInfo const &info = i->second;

Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/p_mapsetup.cpp
Expand Up @@ -661,7 +661,7 @@ static void initFog(ddmapinfo_t *ddMapInfo)
}

#if __JHEXEN__
if(MapInfo const *mapInfo = P_MapInfo(0/*current map*/))
if(MapInfo const *mapInfo = hexDefs.getMapInfo(0/*current map*/))
{
int fadeTable = CentralLumpIndex().findLast(mapInfo->gets("fadeTable") + ".lmp");
if(fadeTable == CentralLumpIndex().findLast("COLORMAP.lmp"))
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/common/src/p_sound.cpp
Expand Up @@ -40,7 +40,7 @@ void S_MapMusic(de::Uri const *mapUri)
if(!mapUri) mapUri = &gameMapUri;

#ifdef __JHEXEN__
MapInfo const *mapInfo = P_MapInfo(mapUri);
MapInfo const *mapInfo = hexDefs.getMapInfo(mapUri);
int const cdTrack = mapInfo->geti("cdTrack");
String const lump = mapInfo->gets("songLump").compareWithoutCase("DEFSONG")? mapInfo->gets("songLump") : "";

Expand Down Expand Up @@ -132,7 +132,7 @@ void SndInfoParser(ddstring_s const *path)
if(mapNumber > 0)
{
de::Uri mapUri = G_ComposeMapUri(0, mapNumber - 1);
if(MapInfo *mapInfo = P_MapInfo(&mapUri))
if(MapInfo *mapInfo = hexDefs.getMapInfo(&mapUri))
{
mapInfo->set("songLump", Str_Text(lumpName));
}
Expand Down
8 changes: 4 additions & 4 deletions doomsday/plugins/common/src/p_start.cpp
Expand Up @@ -206,11 +206,11 @@ static void readOneMapInfoDefinition(MapInfoParser &parser, AutoStr const &buffe

static void readMapInfoDefinitions()
{
// Clear the MapInfo database.
mapInfos.clear();
// Clear the database.
hexDefs.clear();

// Initialize a new parser.
MapInfoParser parser;
MapInfoParser parser(hexDefs);

// Read the primary MAPINFO (from the IWAD).
AutoStr *sourceFile = sc_FileScripts? Str_Appendf(AutoStr_New(), "%sMAPINFO.txt", sc_ScriptsDir)
Expand All @@ -227,7 +227,7 @@ static void readMapInfoDefinitions()
}

#ifdef DENG_DEBUG
for(MapInfos::const_iterator i = mapInfos.begin(); i != mapInfos.end(); ++i)
for(HexDefs::MapInfos::const_iterator i = hexDefs.mapInfos.begin(); i != hexDefs.mapInfos.end(); ++i)
{
MapInfo const &info = i->second;
LOG_RES_MSG("MAPINFO %s { title: \"%s\" hub: %i map: %s warp: %i }")
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/hexen/src/p_spec.cpp
Expand Up @@ -79,7 +79,7 @@ void P_InitLava(void)

void P_InitSky(de::Uri const &mapUri)
{
if(MapInfo const *mapInfo = P_MapInfo(&mapUri))
if(MapInfo const *mapInfo = hexDefs.getMapInfo(&mapUri))
{
sky1Material = Materials_ResolveUriCString(mapInfo->gets("sky1Material").toUtf8().constData());
sky2Material = Materials_ResolveUriCString(mapInfo->gets("sky2Material").toUtf8().constData());
Expand Down Expand Up @@ -1044,7 +1044,7 @@ void P_ForceLightning(void)
void P_InitLightning(void)
{
int i, secCount;
MapInfo const *mapInfo = P_MapInfo(0/*current map*/);
MapInfo const *mapInfo = hexDefs.getMapInfo(0/*current map*/);

if(!mapInfo || !mapInfo->getb("lightning"))
{
Expand Down

0 comments on commit 928ec71

Please sign in to comment.