Skip to content

Commit

Permalink
Refactor|libcommon|GameSession: Moved global variable gameMapUri to c…
Browse files Browse the repository at this point in the history
…ommon::GameSession
  • Loading branch information
danij-deng committed Aug 19, 2014
1 parent 39534cb commit 889028e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 52 deletions.
1 change: 0 additions & 1 deletion doomsday/plugins/common/include/g_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ DENG_EXTERN_C dd_bool singledemo;
DENG_EXTERN_C uint gameMapEntrance;

#if __cplusplus
extern de::Uri gameMapUri;
extern GameRuleset defaultGameRules;

extern "C" {
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/common/include/gamesession.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ namespace common {
* preferences. Additionally, the user may configure the game so that the internal backing store
* is periodically (e.g., when the map changes) copied to a new "autosave" automatically.
*
* The "scope" of a continous game session progression depends on the configuration of the World
* and the Maps within it. Upon leaving one map and entering another, if both are attributed to
* The "scope" of a continous game session progression depends on the configuration of the Episode
* and the maps within it. Upon leaving one map and entering another, if both are attributed to
* the same logical "hub" then the current state of the map is written to the backing store so
* that it may be reloaded later if the player(s) decide to revisit. However, if the new map is
* in another hub, or no hub is defined, then all saved map progress for current hub is discarded.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/include/p_sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* Start the song for the specified map.
*/
void S_MapMusic(de::Uri const *mapUri);
void S_MapMusic(de::Uri const &mapUri);

extern "C" {
#endif
Expand Down
7 changes: 3 additions & 4 deletions doomsday/plugins/common/src/fi_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ int Hook_FinaleScriptStop(int /*hookType*/, int finaleId, void * /*context*/)
{
App_Log(DE2_DEV_SCR_MSG, "Hook_FinaleScriptStop: Clientside script stopped, clearing remote state");
de::zap(remoteFinaleState);

return true;
}

Expand All @@ -395,9 +394,9 @@ int Hook_FinaleScriptStop(int /*hookType*/, int finaleId, void * /*context*/)
// Finale was not initiated by us...
return true;
}
gamestate_t initialGamestate = s->initialGamestate;

finale_mode_t mode = s->mode;
finale_mode_t mode = s->mode;
gamestate_t initialGamestate = s->initialGamestate;

// Should we go back to NULL?
if(finaleStackSize > 1)
Expand Down Expand Up @@ -431,7 +430,7 @@ int Hook_FinaleScriptStop(int /*hookType*/, int finaleId, void * /*context*/)
else if(mode == FIMODE_BEFORE) // A briefing has ended.
{
// Its time to start the map; que music and begin!
S_MapMusic(0/*current map*/);
S_MapMusic(COMMON_GAMESESSION->mapUri());
HU_WakeWidgets(-1 /* all players */);
G_BeginMap();
Pause_End(); // skip forced period
Expand Down
2 changes: 0 additions & 2 deletions doomsday/plugins/common/src/g_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ int Hook_DemoStop(int hookType, int val, void *parm);

game_config_t cfg; // The global cfg.

de::Uri gameMapUri;
uint gameMapEntrance; ///< Entry point, for reborn.

de::Uri nextMapUri;
Expand Down Expand Up @@ -516,7 +515,6 @@ void G_CommonPreInit()
{
quitInProgress = false;
verbose = CommandLine_Exists("-verbose");
::gameMapUri.clear();

// Apply the default game rules.
COMMON_GAMESESSION->applyNewRules(defaultGameRules = GameRuleset());
Expand Down
3 changes: 2 additions & 1 deletion doomsday/plugins/common/src/g_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <cctype>
#include "animdefs.h"
#include "g_common.h"
#include "gamesession.h"
#include "hu_chat.h"
#include "hu_log.h"
#include "hu_menu.h"
Expand Down Expand Up @@ -128,7 +129,7 @@ void G_UpdateState(int step)
#endif

#if __JDOOM__ || __JHERETIC__ || __JDOOM64__
S_MapMusic(0/*current map*/);
S_MapMusic(COMMON_GAMESESSION->mapUri());
#endif
break;

Expand Down
67 changes: 32 additions & 35 deletions doomsday/plugins/common/src/gamesession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ namespace internal
}

/**
* Lookup the briefing Finale for the current episode and map (if any).
* Lookup the briefing Finale for the current episode, map (if any).
*/
static Record const *finaleBriefing()
static Record const *finaleBriefing(de::Uri const &mapUri)
{
if(::briefDisabled) return 0;

Expand All @@ -99,7 +99,7 @@ namespace internal
if(G_GameState() == GS_INFINE) return 0;

// Is there such a finale definition?
return Defs().finales.tryFind("before", ::gameMapUri.compose());
return Defs().finales.tryFind("before", mapUri.compose());
}
}

Expand All @@ -112,10 +112,11 @@ static String const internalSavePath = "/home/cache/internal.save";
DENG2_PIMPL(GameSession), public SavedSession::IMapStateReaderFactory
{
String episodeId;
de::Uri mapUri;
GameRuleset rules;
bool inProgress; ///< @c true= session is in progress / internal.save exists.
bool inProgress = false; ///< @c true= session is in progress / internal.save exists.

Instance(Public *i) : Base(i), inProgress(false)
Instance(Public *i) : Base(i)
{
DENG2_ASSERT(singleton == 0);
singleton = thisPublic;
Expand Down Expand Up @@ -149,7 +150,7 @@ DENG2_PIMPL(GameSession), public SavedSession::IMapStateReaderFactory
meta.set("gameIdentityKey", Session::gameId());
meta.set("episode", episodeId);
meta.set("userDescription", "(Unsaved)");
meta.set("mapUri", ::gameMapUri.compose());
meta.set("mapUri", mapUri.compose());
meta.set("mapTime", ::mapTime);

meta.add("gameRules", self.rules().toRecord()); // Takes ownership.
Expand Down Expand Up @@ -208,7 +209,7 @@ DENG2_PIMPL(GameSession), public SavedSession::IMapStateReaderFactory
Folder &mapsFolder = App::fileSystem().makeFolder(saved->path() / "maps");
DENG2_ASSERT(mapsFolder.mode().testFlag(File::Write));

mapsFolder.replaceFile(::gameMapUri.path() + "State")
mapsFolder.replaceFile(mapUri.path() + "State")
<< serializeCurrentMapState();

saved->flush(); // No need to populate; FS2 Files already in sync with source data.
Expand Down Expand Up @@ -489,22 +490,22 @@ DENG2_PIMPL(GameSession), public SavedSession::IMapStateReaderFactory
::mapTime = metadata.geti("mapTime");
#endif

String const gameMapUriAsText = ::gameMapUri.compose();
makeMapStateReader(saved, gameMapUriAsText)->read(gameMapUriAsText);
String const mapUriAsText = mapUri.compose();
makeMapStateReader(saved, mapUriAsText)->read(mapUriAsText);
}

void setMap(de::Uri const &mapUri)
void setMap(de::Uri const &newMapUri)
{
DENG2_ASSERT(inProgress);

::gameMapUri = mapUri;
mapUri = newMapUri;

// Update game status cvars:
Con_SetString2("map-episode", episodeId.toUtf8().constData(), SVF_WRITE_OVERRIDE);
Con_SetUri2 ("map-id", reinterpret_cast<uri_s *>(&::gameMapUri), SVF_WRITE_OVERRIDE);
Con_SetUri2 ("map-id", reinterpret_cast<uri_s const *>(&mapUri), SVF_WRITE_OVERRIDE);

String hubId;
if(Record const *hubRec = defn::Episode(*self.episodeDef()).tryFindHubByMapId(::gameMapUri.compose()))
if(Record const *hubRec = defn::Episode(*self.episodeDef()).tryFindHubByMapId(mapUri.compose()))
{
hubId = hubRec->gets("id");
}
Expand Down Expand Up @@ -537,7 +538,7 @@ DENG2_PIMPL(GameSession), public SavedSession::IMapStateReaderFactory
{
::briefDisabled = true;
}
Record const *briefing = finaleBriefing();
Record const *briefing = finaleBriefing(mapUri);

// Restart the map music?
if(!briefing)
Expand All @@ -559,11 +560,11 @@ DENG2_PIMPL(GameSession), public SavedSession::IMapStateReaderFactory
//S_StartMusic("chess", true); // Waiting-for-map-load song
#endif

S_MapMusic(&::gameMapUri);
S_MapMusic(mapUri);
S_PauseMusic(true);
}

P_SetupMap(::gameMapUri);
P_SetupMap(mapUri);

if(revisit)
{
Expand All @@ -573,10 +574,10 @@ DENG2_PIMPL(GameSession), public SavedSession::IMapStateReaderFactory
#endif

SavedSession const &saved = App::rootFolder().locate<SavedSession>(internalSavePath);
String const gameMapUriAsText = ::gameMapUri.compose();
String const mapUriAsText = mapUri.compose();
std::unique_ptr<SavedSession::MapStateReader> reader(
makeMapStateReader(saved, gameMapUriAsText));
reader->read(gameMapUriAsText);
makeMapStateReader(saved, mapUriAsText));
reader->read(mapUriAsText);
}

if(!briefing || !G_StartFinale(briefing->gets("script").toUtf8().constData(), 0, FIMODE_BEFORE, 0))
Expand Down Expand Up @@ -831,24 +832,20 @@ Record *GameSession::mapGraphNodeDef()
if(Record const *episode = episodeDef())
{
/// @todo cache this result?
return defn::Episode(*episode).tryFindMapGraphNode(::gameMapUri.compose());
return defn::Episode(*episode).tryFindMapGraphNode(mapUri().compose());
}
return 0;
}

Record *GameSession::mapInfo()
{
if(hasBegun())
{
/// @todo cache this result?
return Defs().mapInfos.tryFind("id", ::gameMapUri.compose());
}
return 0;
/// @todo cache this result?
return Defs().mapInfos.tryFind("id", mapUri().compose());
}

de::Uri GameSession::mapUri()
{
return hasBegun()? ::gameMapUri : de::Uri();
return hasBegun()? d->mapUri : de::Uri();
}

de::Uri GameSession::mapUriForNamedExit(String name)
Expand Down Expand Up @@ -882,7 +879,7 @@ de::Uri GameSession::mapUriForNamedExit(String name)
{
LOG_SCR_WARNING("Episode '%s' map \"%s\" defines no Exit with ID '%s'")
<< d->episodeId
<< ::gameMapUri
<< d->mapUri
<< name;
}
}
Expand All @@ -900,7 +897,7 @@ de::Uri GameSession::mapUriForNamedExit(String name)
{
LOG_SCR_WARNING("Episode '%s' map \"%s\" defines no exits")
<< d->episodeId
<< ::gameMapUri;
<< d->mapUri;
}

if(chosenExit)
Expand Down Expand Up @@ -1063,7 +1060,7 @@ void GameSession::reloadMap()
// Restart the session entirely.
briefDisabled = true; // We won't brief again.
end();
begin(d->rules, d->episodeId, ::gameMapUri, ::gameMapEntrance);
begin(d->rules, d->episodeId, mapUri(), ::gameMapEntrance);
}
}

Expand Down Expand Up @@ -1108,7 +1105,7 @@ void GameSession::leaveMap()
// Are we entering a new hub?
#if __JHEXEN__
defn::Episode epsd(*episodeDef());
Record const *currentHub = epsd.tryFindHubByMapId(::gameMapUri.compose());
Record const *currentHub = epsd.tryFindHubByMapId(d->mapUri.compose());
if(currentHub != epsd.tryFindHubByMapId(::nextMapUri.compose()))
#endif
{
Expand All @@ -1122,7 +1119,7 @@ void GameSession::leaveMap()
#if __JHEXEN__
else
{
File &outFile = mapsFolder.replaceFile(::gameMapUri.path() + "State");
File &outFile = mapsFolder.replaceFile(d->mapUri.path() + "State");
outFile << serializeCurrentMapState(true /*exclude players*/);
// We'll flush whole package soon.
}
Expand Down Expand Up @@ -1158,7 +1155,7 @@ void GameSession::leaveMap()
::gameMapEntrance = ::nextMapEntrance;

// Are we revisiting a previous map?
bool const revisit = saved && saved->hasState(String("maps") / ::gameMapUri.path());
bool const revisit = saved && saved->hasState(String("maps") / d->mapUri.path());

d->reloadMap(revisit);

Expand All @@ -1183,7 +1180,7 @@ void GameSession::leaveMap()
d->rules.randomClasses = oldRandomClassesRule;

// Launch waiting scripts.
Game_ACScriptInterpreter().runDeferredTasks(::gameMapUri);
Game_ACScriptInterpreter().runDeferredTasks(d->mapUri);
#endif

if(saved)
Expand All @@ -1206,7 +1203,7 @@ void GameSession::leaveMap()
Folder &mapsFolder = saved->locate<Folder>("maps");
DENG2_ASSERT(mapsFolder.mode().testFlag(File::Write));

File &outFile = mapsFolder.replaceFile(::gameMapUri.path() + "State");
File &outFile = mapsFolder.replaceFile(d->mapUri.path() + "State");
outFile << serializeCurrentMapState();

// Write all changes to the package.
Expand Down
10 changes: 4 additions & 6 deletions doomsday/plugins/common/src/p_sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@
using namespace de;
using namespace common;

void S_MapMusic(de::Uri const *mapUri)
void S_MapMusic(de::Uri const &mapUri)
{
if(!mapUri) mapUri = &::gameMapUri;

#ifdef __JHEXEN__
Record const &mapInfo = Defs().mapInfos.find("id", mapUri->compose());
Record const &mapInfo = Defs().mapInfos.find("id", mapUri.compose());
int const cdTrack = mapInfo.geti("cdTrack");
String const lump = mapInfo.gets("songLump").compareWithoutCase("DEFSONG")? mapInfo.gets("songLump") : "";

LOG_RES_VERBOSE("S_MapMusic: %s lump: %s") << mapUri->compose() << lump;
LOG_RES_VERBOSE("S_MapMusic: %s lump: %s") << mapUri.compose() << lump;

// Update the 'currentmap' music definition.
Record &music = Defs().musics.find("id", "currentmap");
Expand All @@ -56,7 +54,7 @@ void S_MapMusic(de::Uri const *mapUri)
Con_SetInteger2("map-music", Defs().getMusicNum(music.gets("id").toUtf8().constData()), SVF_WRITE_OVERRIDE);
}
#else
if(Record const *mapInfo = Defs().mapInfos.tryFind("id", mapUri->compose()))
if(Record const *mapInfo = Defs().mapInfos.tryFind("id", mapUri.compose()))
{
int songNumber = Defs().getMusicNum(mapInfo->gets("music").toUtf8().constData());
if(S_StartMusicNum(songNumber, true))
Expand Down

0 comments on commit 889028e

Please sign in to comment.