Skip to content

Commit

Permalink
Refactor|libcommon|GameRuleset: Began remodeling GameRuleset into a C…
Browse files Browse the repository at this point in the history
…++ class

A C wrapper API for accessing rule values is necessary because it is
not presently feasible to switch all the relevant source files to C++
due to the large amount of C-isms also present. All source files which
need write access to the game rules are now in C++, however.

Todo: Fix new compiler warnings output by GCC/clang since this latest
batch of C++ switches.
  • Loading branch information
danij-deng committed Feb 25, 2014
1 parent a452959 commit 48a720b
Show file tree
Hide file tree
Showing 64 changed files with 501 additions and 358 deletions.
2 changes: 1 addition & 1 deletion doomsday/plugins/common/common.pri
Expand Up @@ -90,7 +90,7 @@ SOURCES += \
$$common_src/g_eventsequence.cpp \
$$common_src/g_game.cpp \
$$common_src/g_update.c \
$$common_src/gamerules.c \
$$common_src/gamerules.cpp \
$$common_src/gamestatereader.cpp \
$$common_src/gamestatewriter.cpp \
$$common_src/gl_drawpatch.c \
Expand Down
73 changes: 44 additions & 29 deletions doomsday/plugins/common/include/g_common.h
Expand Up @@ -24,11 +24,11 @@
#include "dd_share.h"
#include "mobj.h"
#include "player.h"
#include "gamerules.h"

DENG_EXTERN_C dd_bool singledemo;

DENG_EXTERN_C dd_bool gameInProgress;
DENG_EXTERN_C GameRuleset gameRules;
DENG_EXTERN_C uint gameEpisode;

DENG_EXTERN_C Uri *gameMapUri;
Expand Down Expand Up @@ -75,17 +75,6 @@ void G_StartTitle(void);
*/
void G_StartHelp(void);

/**
* @param mapUri Map identifier.
* @param mapEntrance Logical map entry point number.
* @param rules Game rules to apply.
*/
void G_NewSession(Uri const *mapUri, uint mapEntrance, GameRuleset const *rules);

void G_DeferredNewSession(Uri const *mapUri, uint mapEntrance, GameRuleset const *rules);

void G_EndSession(void);

/**
* Signal that play on the current map may now begin.
*/
Expand Down Expand Up @@ -186,25 +175,15 @@ class GameStateReaderFactory;
class SaveSlots;

/**
* Returns the game's SaveSlots.
* @param mapUri Map identifier.
* @param mapEntrance Logical map entry point number.
* @param rules Game rules to apply.
*/
SaveSlots &G_SaveSlots();
void G_NewSession(Uri const *mapUri, uint mapEntrance, GameRuleset const *rules);

/**
* Parse @a str and determine whether it references a logical game-save slot.
*
* @param str String to be parsed. Parse is divided into three passes.
* Pass 1: Check for a known game-save description which matches this.
* Search is in logical save slot creation order.
* Pass 2: Check for keyword identifiers.
* <auto> = The "auto save" slot.
* <last> = The last used slot.
* <quick> = The currently nominated "quick save" slot.
* Pass 3: Check for a unique save slot identifier.
*
* @return The parsed slot id if found; otherwise a zero-length string.
*/
de::String G_SaveSlotIdFromUserInput(de::String str);
void G_DeferredNewSession(Uri const *mapUri, uint mapEntrance, GameRuleset const *rules);

void G_EndSession(void);

/**
* Determines whether game session loading is presently possible.
Expand Down Expand Up @@ -242,6 +221,42 @@ bool G_LoadSession(de::String slotId);
*/
GameStateReaderFactory &G_GameStateReaderFactory();

/**
* Returns the game's SaveSlots.
*/
SaveSlots &G_SaveSlots();

/**
* Parse @a str and determine whether it references a logical game-save slot.
*
* @param str String to be parsed. Parse is divided into three passes.
* Pass 1: Check for a known game-save description which matches this.
* Search is in logical save slot creation order.
* Pass 2: Check for keyword identifiers.
* <auto> = The "auto save" slot.
* <last> = The last used slot.
* <quick> = The currently nominated "quick save" slot.
* Pass 3: Check for a unique save slot identifier.
*
* @return The parsed slot id if found; otherwise a zero-length string.
*/
de::String G_SaveSlotIdFromUserInput(de::String str);

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

#endif // __cplusplus

#ifdef __cplusplus
extern "C" {
#endif

GameRuleset *G_RulesPtr();

#ifdef __cplusplus
} // extern "C"
#endif

#endif // LIBCOMMON_GAME_H
35 changes: 31 additions & 4 deletions doomsday/plugins/common/include/gamerules.h
Expand Up @@ -23,13 +23,16 @@

#include "common.h"

#ifdef __cplusplus

/**
* @ingroup libcommon
*
* @todo Separate behaviors so that each rule is singular.
*/
typedef struct gameruleset_s
class GameRuleset
{
public:
skillmode_t skill;
#if !__JHEXEN__
byte fast;
Expand All @@ -41,14 +44,38 @@ typedef struct gameruleset_s
#else
byte respawnMonsters;
#endif
} GameRuleset;

public:
GameRuleset();
GameRuleset(GameRuleset const &other);

GameRuleset &operator = (GameRuleset const &other);

void write(Writer *writer) const;
void read(Reader *reader);
};

#endif // __cplusplus

// C wrapper API ---------------------------------------------------------------

#ifdef __cplusplus
extern "C" {
#else
typedef void *GameRuleset;
#endif

void GameRuleset_Write(GameRuleset const *rules, Writer *writer);
void GameRuleset_Read(GameRuleset *rules, Reader *reader);
skillmode_t GameRuleset_Skill(GameRuleset const *rules);
#if !__JHEXEN__
byte GameRuleset_Fast(GameRuleset const *rules);
#endif
byte GameRuleset_Deathmatch(GameRuleset const *rules);
byte GameRuleset_NoMonsters(GameRuleset const *rules);
#if __JHEXEN__
byte GameRuleset_RandomClasses(GameRuleset const *rules);
#else
byte GameRuleset_RespawnMonsters(GameRuleset const *rules);
#endif

#ifdef __cplusplus
} // extern "C"
Expand Down
30 changes: 15 additions & 15 deletions doomsday/plugins/common/src/d_net.cpp
Expand Up @@ -100,16 +100,16 @@ void NetSv_ApplyGameRulesFromConfig()
{
if(IS_CLIENT) return;

gameRules.deathmatch = cfg.netDeathmatch;
gameRules.noMonsters = cfg.netNoMonsters;
G_Rules().deathmatch = cfg.netDeathmatch;
G_Rules().noMonsters = cfg.netNoMonsters;
cfg.jumpEnabled = cfg.netJumping;

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

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

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

Uri *netMapUri = G_ComposeMapUri(netEpisode, netMap);

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

G_NewSession(netMapUri, 0/*default*/, &netRules);
Expand All @@ -168,10 +168,10 @@ 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.
gameRules.deathmatch = false;
gameRules.noMonsters = false;
G_Rules().deathmatch = false;
G_Rules().noMonsters = false;
#if __JHEXEN__
gameRules.randomClasses = false;
G_Rules().randomClasses = false;
#endif
D_NetMessage(CONSOLEPLAYER, "NETGAME ENDS");

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

// Restore normal game state.
gameRules.deathmatch = false;
gameRules.noMonsters = false;
G_Rules().deathmatch = false;
G_Rules().noMonsters = false;
#if __JHEXEN__
gameRules.randomClasses = false;
G_Rules().randomClasses = false;
#endif

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

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

#if __JHEXEN__
gameRules.randomClasses = false;
G_Rules().randomClasses = false;
#endif
break;
#endif
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/common/src/d_netcl.cpp
Expand Up @@ -50,7 +50,7 @@ void NetCl_UpdateGameState(Reader *msg)
byte gsJumping = 0;
//byte gsSkill = 0;
coord_t gsGravity = 0;
GameRuleset gsRules = gameRules; // Make a copy of the current rules.
GameRuleset gsRules = G_Rules(); // Make a copy of the current rules.

BusyMode_FreezeGameForBusyMode();

Expand Down Expand Up @@ -147,7 +147,7 @@ void NetCl_UpdateGameState(Reader *msg)
gameMap = gsMap;
Uri_Copy(gameMapUri, mapUri);
//gameMapEntrance = gsMapEntrance; /// @todo Not communicated to clients??
gameRules = gsRules;
G_Rules() = gsRules;
}

// Set gravity.
Expand Down
24 changes: 12 additions & 12 deletions doomsday/plugins/common/src/d_netsv.cpp
Expand Up @@ -99,19 +99,19 @@ void NetSv_UpdateGameConfigDescription()
return;

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

if(gameRules.deathmatch > 1)
sprintf(gameConfigString, " dm%i", gameRules.deathmatch);
else if(gameRules.deathmatch)
if(G_Rules().deathmatch > 1)
sprintf(gameConfigString, " dm%i", G_Rules().deathmatch);
else if(G_Rules().deathmatch)
strcat(gameConfigString, " dm");
else
strcat(gameConfigString, " coop");

if(gameRules.noMonsters)
if(G_Rules().noMonsters)
strcat(gameConfigString, " nomonst");
#if !__JHEXEN__
if(gameRules.respawnMonsters)
if(G_Rules().respawnMonsters)
strcat(gameConfigString, " respawn");
#endif

Expand Down Expand Up @@ -570,7 +570,7 @@ void NetSv_NewPlayerEnters(int plrNum)
NetSv_ResetPlayerFrags(plrNum);

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

Writer_WriteByte(writer, (gameRules.deathmatch & 0x3)
| (!gameRules.noMonsters? 0x4 : 0)
Writer_WriteByte(writer, (G_Rules().deathmatch & 0x3)
| (!G_Rules().noMonsters? 0x4 : 0)
#if !__JHEXEN__
| (gameRules.respawnMonsters? 0x8 : 0)
| (G_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, gameRules.skill & 0x7);
Writer_WriteByte(writer, G_Rules().skill & 0x7);
Writer_WriteFloat(writer, (float)P_GetGravity());

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

char buf[500];
buf[0] = 0;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/fi_lib.cpp
Expand Up @@ -505,7 +505,7 @@ int Hook_FinaleScriptEvalIf(int /*hookType*/, int finaleId, void *context)

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

Expand Down
10 changes: 10 additions & 0 deletions doomsday/plugins/common/src/g_game.cpp
Expand Up @@ -2387,6 +2387,16 @@ static void G_ApplyNewGameRules()
}
}

GameRuleset &G_Rules()
{
return gameRules;
}

GameRuleset *G_RulesPtr()
{
return &gameRules;
}

void G_LeaveMap(uint newMap, uint _entryPoint, dd_bool _secretExit)
{
#if __JHEXEN__
Expand Down

0 comments on commit 48a720b

Please sign in to comment.