Skip to content

Commit

Permalink
libcommon: GameSession owns the current game rules
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Apr 10, 2014
1 parent d7abc6f commit 260b746
Show file tree
Hide file tree
Showing 30 changed files with 348 additions and 328 deletions.
13 changes: 0 additions & 13 deletions doomsday/plugins/common/include/g_common.h
Expand Up @@ -245,7 +245,6 @@ D_CMD( CCmdExitLevel );

#if __cplusplus
#include <de/String>
#include "gamerules.h"

class SaveSlots;

Expand All @@ -271,17 +270,5 @@ void G_ApplyCurrentSessionMetadata(de::game::SessionMetadata &metadata);
*/
SaveSlots &G_SaveSlots();

/**
* Returns the game's GameRuleset.
*/
GameRuleset &G_Rules();

/**
* To be called when a new game begins to effect the game rules. Note that some
* of the rules may be overridden here (e.g., in a networked game).
*/
void G_ApplyNewGameRules(GameRuleset const &rules);

#endif // __cplusplus

#endif // LIBCOMMON_GAME_H
14 changes: 14 additions & 0 deletions doomsday/plugins/common/include/gamesession.h
Expand Up @@ -63,6 +63,20 @@ class GameSession : public de::game::Session
*/
inline de::String gameId() const { return Session::profile().gameId; }

/**
* Returns the current ruleset for the game session.
* @todo Return value should be const. (Need to fix the myriad state management issues...)
*/
GameRuleset /*const*/ &rules() const;

/**
* To be called when a new game begins to effect the game rules. Note that some
* of the rules may be overridden here (e.g., in a networked game).
*
* @todo Prevent this outright if the game session is already in progress!
*/
void applyNewRules(GameRuleset const &rules);

/**
* Determines whether saved game progress will be restored when the current map is reloaded,
* according to the current game state and user configuration.
Expand Down
33 changes: 18 additions & 15 deletions doomsday/plugins/common/src/d_net.cpp
Expand Up @@ -103,16 +103,17 @@ void NetSv_ApplyGameRulesFromConfig()
{
if(IS_CLIENT) return;

G_Rules().deathmatch = cfg.netDeathmatch;
G_Rules().noMonsters = cfg.netNoMonsters;
GameRuleset &gameRules = COMMON_GAMESESSION->rules();
gameRules.deathmatch = cfg.netDeathmatch;
gameRules.noMonsters = cfg.netNoMonsters;
cfg.jumpEnabled = cfg.netJumping;

#if __JDOOM__ || __JHERETIC__ || __JDOOM64__
G_Rules().respawnMonsters = cfg.netRespawn;
gameRules.respawnMonsters = cfg.netRespawn;
#endif

#if __JHEXEN__
G_Rules().randomClasses = cfg.netRandomClass;
gameRules.randomClasses = cfg.netRandomClass;
#endif

NetSv_UpdateGameConfigDescription();
Expand Down Expand Up @@ -148,7 +149,7 @@ int D_NetServerStarted(int before)

Uri *netMapUri = G_ComposeMapUri(netEpisode, netMap);

GameRuleset netRules = G_Rules(); // Make a copy of the current rules.
GameRuleset netRules = COMMON_GAMESESSION->rules(); // Make a copy of the current rules.
netRules.skill = skillmode_t(cfg.netSkill);

COMMON_GAMESESSION->end();
Expand All @@ -170,10 +171,11 @@ int D_NetServerClose(int before)
/// @todo fixme: "normal" is defined by the game rules config which may
/// be changed from the command line (e.g., -fast, -nomonsters).
/// In order to "restore normal" this logic is insufficient.
G_Rules().deathmatch = false;
G_Rules().noMonsters = false;
GameRuleset &gameRules = COMMON_GAMESESSION->rules();
gameRules.deathmatch = false;
gameRules.noMonsters = false;
#if __JHEXEN__
G_Rules().randomClasses = false;
gameRules.randomClasses = false;
#endif
D_NetMessage(CONSOLEPLAYER, "NETGAME ENDS");

Expand Down Expand Up @@ -204,10 +206,11 @@ int D_NetDisconnect(int before)
if(before) return true;

// Restore normal game state.
G_Rules().deathmatch = false;
G_Rules().noMonsters = false;
GameRuleset &gameRules = COMMON_GAMESESSION->rules();
gameRules.deathmatch = false;
gameRules.noMonsters = false;
#if __JHEXEN__
G_Rules().randomClasses = false;
gameRules.randomClasses = false;
#endif

D_NetClearBuffer();
Expand Down Expand Up @@ -352,14 +355,14 @@ int D_NetWorldEvent(int type, int parm, void *data)
G_DemoEnds();

// Restore normal game state.
G_Rules().deathmatch = false;
G_Rules().noMonsters = false;
COMMON_GAMESESSION->rules().deathmatch = false;
COMMON_GAMESESSION->rules().noMonsters = false;
#if __JDOOM__ || __JHERETIC__ || __JDOOM64__
G_Rules().respawnMonsters = false;
COMMON_GAMESESSION->rules().respawnMonsters = false;
#endif

#if __JHEXEN__
G_Rules().randomClasses = false;
COMMON_GAMESESSION->rules().randomClasses = false;
#endif
break;
#endif
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/common/src/d_netcl.cpp
Expand Up @@ -58,7 +58,7 @@ void NetCl_UpdateGameState(Reader *msg)

byte configFlags = Reader_ReadByte(msg);

GameRuleset gsRules(G_Rules()); // Initialize with a copy of the current rules.
GameRuleset gsRules(COMMON_GAMESESSION->rules()); // Initialize with a copy of the current rules.
gsRules.deathmatch = configFlags & 0x3;
gsRules.noMonsters = !(configFlags & 0x4? true : false);
#if !__JHEXEN__
Expand Down Expand Up @@ -111,7 +111,7 @@ void NetCl_UpdateGameState(Reader *msg)
/// begun and setting the current map and/or entrance is illogical at this point.
DENG2_ASSERT(Uri_Equality(gsMapUri, gameMapUri));

G_ApplyNewGameRules(gsRules);
COMMON_GAMESESSION->applyNewRules(gsRules);
//COMMON_GAMESESSION->setMap(*gsMapUri);
//gameMapEntrance = gsMapEntrance;
}
Expand Down
26 changes: 14 additions & 12 deletions doomsday/plugins/common/src/d_netsv.cpp
Expand Up @@ -98,14 +98,16 @@ void NetSv_UpdateGameConfigDescription()
{
if(IS_CLIENT) return;

GameRuleset const &gameRules = COMMON_GAMESESSION->rules();

de::zap(gameConfigString);
sprintf(gameConfigString, "skill%i", G_Rules().skill + 1);
sprintf(gameConfigString, "skill%i", gameRules.skill + 1);

if(G_Rules().deathmatch > 1)
if(gameRules.deathmatch > 1)
{
sprintf(gameConfigString, " dm%i", G_Rules().deathmatch);
sprintf(gameConfigString, " dm%i", gameRules.deathmatch);
}
else if(G_Rules().deathmatch)
else if(gameRules.deathmatch)
{
strcat(gameConfigString, " dm");
}
Expand All @@ -114,12 +116,12 @@ void NetSv_UpdateGameConfigDescription()
strcat(gameConfigString, " coop");
}

if(G_Rules().noMonsters)
if(gameRules.noMonsters)
{
strcat(gameConfigString, " nomonst");
}
#if !__JHEXEN__
if(G_Rules().respawnMonsters)
if(gameRules.respawnMonsters)
{
strcat(gameConfigString, " respawn");
}
Expand Down Expand Up @@ -582,7 +584,7 @@ void NetSv_NewPlayerEnters(int plrNum)
NetSv_ResetPlayerFrags(plrNum);

// Spawn the player into the world.
if(G_Rules().deathmatch)
if(COMMON_GAMESESSION->rules().deathmatch)
{
G_DeathMatchSpawnPlayer(plrNum);
}
Expand Down Expand Up @@ -707,17 +709,17 @@ void NetSv_SendGameState(int flags, int to)
Writer_WriteByte(writer, gameEpisode);
Writer_WriteByte(writer, gameMap);

Writer_WriteByte(writer, (G_Rules().deathmatch & 0x3)
| (!G_Rules().noMonsters? 0x4 : 0)
Writer_WriteByte(writer, (COMMON_GAMESESSION->rules().deathmatch & 0x3)
| (!COMMON_GAMESESSION->rules().noMonsters? 0x4 : 0)
#if !__JHEXEN__
| (G_Rules().respawnMonsters? 0x8 : 0)
| (COMMON_GAMESESSION->rules().respawnMonsters? 0x8 : 0)
#else
| 0
#endif
| (cfg.jumpEnabled? 0x10 : 0));

// Note that SM_NOTHINGS will result in a value of '7'.
Writer_WriteByte(writer, G_Rules().skill & 0x7);
Writer_WriteByte(writer, COMMON_GAMESESSION->rules().skill & 0x7);
Writer_WriteFloat(writer, (float)P_GetGravity());

if(flags & GSF_CAMERA_INIT)
Expand Down Expand Up @@ -1141,7 +1143,7 @@ void NetSv_KillMessage(player_t *killer, player_t *fragged, dd_bool stomping)
{
#if __JDOOM__ || __JDOOM64__
if(!cfg.killMessages) return;
if(!G_Rules().deathmatch) return;
if(!COMMON_GAMESESSION->rules().deathmatch) return;

char buf[500];
buf[0] = 0;
Expand Down
3 changes: 2 additions & 1 deletion doomsday/plugins/common/src/fi_lib.cpp
Expand Up @@ -27,6 +27,7 @@
#include "hu_stuff.h"
#include "am_map.h"
#include "g_common.h"
#include "gamesession.h"
#include "r_common.h"
#include "d_net.h"

Expand Down Expand Up @@ -506,7 +507,7 @@ int Hook_FinaleScriptEvalIf(int /*hookType*/, int finaleId, void *context)

if(!stricmp(p->token, "deathmatch"))
{
p->returnVal = (G_Rules().deathmatch != false);
p->returnVal = (COMMON_GAMESESSION->rules().deathmatch != false);
return true;
}

Expand Down

0 comments on commit 260b746

Please sign in to comment.