Skip to content

Commit

Permalink
Decouple g_game in true singleton (#264)
Browse files Browse the repository at this point in the history
Game class decoupled in a true singleton, thus allowing direct calls without needing to instantiate in each file.
Credits to @lgrossi for made the decouple logic.
  • Loading branch information
dudantas committed Apr 11, 2022
1 parent 46cf35b commit 126e7df
Show file tree
Hide file tree
Showing 67 changed files with 730 additions and 785 deletions.
6 changes: 2 additions & 4 deletions src/config/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#define lua_strlen lua_rawlen
#endif

extern Game g_game;

namespace {

std::string getGlobalString(lua_State* L, const char* identifier, const char* defaultValue)
Expand Down Expand Up @@ -278,8 +276,8 @@ bool ConfigManager::load()
bool ConfigManager::reload()
{
bool result = load();
if (transformToSHA1(getString(MOTD)) != g_game.getMotdHash()) {
g_game.incrementMotdNum();
if (transformToSHA1(getString(MOTD)) != g_game().getMotdHash()) {
g_game().incrementMotdNum();
}
return result;
}
Expand Down
59 changes: 28 additions & 31 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "creatures/monsters/monsters.h"
#include "items/weapons/weapons.h"

extern Game g_game;
extern Weapons* g_weapons;
extern Events* g_events;
extern Monsters g_monsters;
Expand Down Expand Up @@ -70,7 +69,7 @@ CombatDamage Combat::getCombatDamage(Creature* creature, Creature* target) const
if (params.useCharges) {
uint16_t charges = tool->getCharges();
if (charges != 0) {
g_game.transformItem(tool, tool->getID(), charges - 1);
g_game().transformItem(tool, tool->getID(), charges - 1);
}
}
} else {
Expand All @@ -94,10 +93,10 @@ void Combat::getCombatArea(const Position& centerPos, const Position& targetPos,
if (area) {
area->getList(centerPos, targetPos, list);
} else {
Tile* tile = g_game.map.getTile(targetPos);
Tile* tile = g_game().map.getTile(targetPos);
if (!tile) {
tile = new StaticTile(targetPos.x, targetPos.y, targetPos.z);
g_game.map.setTile(targetPos, tile);
g_game().map.setTile(targetPos, tile);
}
list.push_front(tile);
}
Expand Down Expand Up @@ -366,7 +365,7 @@ ReturnValue Combat::canDoCombat(Creature* attacker, Creature* target)
}
}

if (g_game.getWorldType() == WORLD_TYPE_NO_PVP) {
if (g_game().getWorldType() == WORLD_TYPE_NO_PVP) {
if (attacker->getPlayer() || (attackerMaster && attackerMaster->getPlayer())) {
if (target->getPlayer()) {
if (!isInPvpZone(attacker, target)) {
Expand Down Expand Up @@ -510,11 +509,11 @@ void Combat::CombatHealthFunc(Creature* caster, Creature* target, const CombatPa
}
}

if (g_game.combatBlockHit(damage, caster, target, params.blockedByShield, params.blockedByArmor, params.itemId != 0)) {
if (g_game().combatBlockHit(damage, caster, target, params.blockedByShield, params.blockedByArmor, params.itemId != 0)) {
return;
}

if (g_game.combatChangeHealth(caster, target, damage)) {
if (g_game().combatChangeHealth(caster, target, damage)) {
CombatConditionFunc(caster, target, params, &damage);
CombatDispelFunc(caster, target, params, nullptr);
}
Expand Down Expand Up @@ -557,7 +556,7 @@ void Combat::CombatManaFunc(Creature* caster, Creature* target, const CombatPara
damage.primary.value /= 2;
}
}
if (g_game.combatChangeMana(caster, target, damage)) {
if (g_game().combatChangeMana(caster, target, damage)) {
CombatConditionFunc(caster, target, params, nullptr);
CombatDispelFunc(caster, target, params, nullptr);
}
Expand Down Expand Up @@ -667,7 +666,7 @@ void Combat::combatTileEffects(const SpectatorHashSet& spectators, Creature* cas
}

if (casterPlayer) {
if (g_game.getWorldType() == WORLD_TYPE_NO_PVP || tile->hasFlag(TILESTATE_NOPVPZONE)) {
if (g_game().getWorldType() == WORLD_TYPE_NO_PVP || tile->hasFlag(TILESTATE_NOPVPZONE)) {
if (itemId == ITEM_FIREFIELD_PVP_FULL) {
itemId = ITEM_FIREFIELD_NOPVP;
} else if (itemId == ITEM_POISONFIELD_PVP) {
Expand All @@ -690,7 +689,7 @@ void Combat::combatTileEffects(const SpectatorHashSet& spectators, Creature* cas
item->setOwner(caster->getID());
}

ReturnValue ret = g_game.internalAddItem(tile, item);
ReturnValue ret = g_game().internalAddItem(tile, item);
if (ret == RETURNVALUE_NOERROR) {
item->startDecaying();
} else {
Expand Down Expand Up @@ -743,7 +742,7 @@ void Combat::addDistanceEffect(Creature* caster, const Position& fromPos, const
}

if (effect != CONST_ANI_NONE) {
g_game.addDistanceEffect(fromPos, toPos, effect);
g_game().addDistanceEffect(fromPos, toPos, effect);
}
}

Expand Down Expand Up @@ -808,7 +807,7 @@ void Combat::CombatFunc(Creature* caster, const Position& pos, const AreaCombat*

const int32_t rangeX = maxX + Map::maxViewportX;
const int32_t rangeY = maxY + Map::maxViewportY;
g_game.map.getSpectators(spectators, pos, true, true, rangeX, rangeX, rangeY, rangeY);
g_game().map.getSpectators(spectators, pos, true, true, rangeX, rangeX, rangeY, rangeY);

int affected = 0;
for (Tile* tile : tileList) {
Expand Down Expand Up @@ -888,7 +887,7 @@ void Combat::doCombatHealth(Creature* caster, Creature* target, CombatDamage& da
if ( (caster && target)
&& (caster == target || canCombat)
&& (params.impactEffect != CONST_ME_NONE)) {
g_game.addMagicEffect(target->getPosition(), params.impactEffect);
g_game().addMagicEffect(target->getPosition(), params.impactEffect);
}

if (params.combatType == COMBAT_HEALING && target->getMonster()){
Expand Down Expand Up @@ -952,7 +951,7 @@ void Combat::doCombatMana(Creature* caster, Creature* target, CombatDamage& dama
if ( (caster && target)
&& (caster == target || canCombat)
&& (params.impactEffect != CONST_ME_NONE)) {
g_game.addMagicEffect(target->getPosition(), params.impactEffect);
g_game().addMagicEffect(target->getPosition(), params.impactEffect);
}

if(caster && caster->getPlayer()){
Expand Down Expand Up @@ -999,7 +998,7 @@ void Combat::doCombatCondition(Creature* caster, Creature* target, const CombatP
{
bool canCombat = !params.aggressive || (caster != target && Combat::canDoCombat(caster, target) == RETURNVALUE_NOERROR);
if ((caster == target || canCombat) && params.impactEffect != CONST_ME_NONE) {
g_game.addMagicEffect(target->getPosition(), params.impactEffect);
g_game().addMagicEffect(target->getPosition(), params.impactEffect);
}

if (canCombat) {
Expand All @@ -1025,7 +1024,7 @@ void Combat::doCombatDispel(Creature* caster, Creature* target, const CombatPara
if ( (caster && target)
&& (caster == target || canCombat)
&& (params.impactEffect != CONST_ME_NONE)) {
g_game.addMagicEffect(target->getPosition(), params.impactEffect);
g_game().addMagicEffect(target->getPosition(), params.impactEffect);
}

if (canCombat) {
Expand All @@ -1044,7 +1043,7 @@ void Combat::doCombatDefault(Creature* caster, Creature* target, const CombatPar
{
if (!params.aggressive || (caster != target && Combat::canDoCombat(caster, target) == RETURNVALUE_NOERROR)) {
SpectatorHashSet spectators;
g_game.map.getSpectators(spectators, target->getPosition(), true, true);
g_game().map.getSpectators(spectators, target->getPosition(), true, true);

CombatNullFunc(caster, target, params, nullptr);
combatTileEffects(spectators, caster, target->getTile(), params);
Expand All @@ -1055,7 +1054,7 @@ void Combat::doCombatDefault(Creature* caster, Creature* target, const CombatPar

/*
if (params.impactEffect != CONST_ME_NONE) {
g_game.addMagicEffect(target->getPosition(), params.impactEffect);
g_game().addMagicEffect(target->getPosition(), params.impactEffect);
}
*/

Expand Down Expand Up @@ -1136,7 +1135,7 @@ void ValueCallback::getMinMaxValues(Player* player, CombatDamage& damage, bool u
if (useCharges) {
uint16_t charges = tool->getCharges();
if (charges != 0) {
g_game.transformItem(tool, tool->getID(), charges - 1);
g_game().transformItem(tool, tool->getID(), charges - 1);
}
}
}
Expand Down Expand Up @@ -1305,15 +1304,13 @@ void AreaCombat::getList(const Position& centerPos, const Position& targetPos, s
uint32_t cols = area->getCols();
for (uint32_t y = 0, rows = area->getRows(); y < rows; ++y) {
for (uint32_t x = 0; x < cols; ++x) {
if (area->getValue(y, x) != 0) {
if (g_game.isSightClear(targetPos, tmpPos, true)) {
Tile* tile = g_game.map.getTile(tmpPos);
if (!tile) {
tile = new StaticTile(tmpPos.x, tmpPos.y, tmpPos.z);
g_game.map.setTile(tmpPos, tile);
}
list.push_front(tile);
if (area->getValue(y, x) != 0 && g_game().isSightClear(targetPos, tmpPos, true)) {
Tile* tile = g_game().map.getTile(tmpPos);
if (!tile) {
tile = new StaticTile(tmpPos.x, tmpPos.y, tmpPos.z);
g_game().map.setTile(tmpPos, tile);
}
list.push_front(tile);
}
tmpPos.x++;
}
Expand Down Expand Up @@ -1569,7 +1566,7 @@ void MagicField::onStepInField(Creature* creature)
//remove magic walls/wild growth
if (id == ITEM_MAGICWALL || id == ITEM_WILDGROWTH || id == ITEM_MAGICWALL_SAFE || id == ITEM_WILDGROWTH_SAFE || isBlocking()) {
if (!creature->isInGhostMode()) {
g_game.internalRemoveItem(this, 1);
g_game().internalRemoveItem(this, 1);
}

return;
Expand All @@ -1582,8 +1579,8 @@ void MagicField::onStepInField(Creature* creature)
if (ownerId) {
bool harmfulField = true;

if (g_game.getWorldType() == WORLD_TYPE_NO_PVP || getTile()->hasFlag(TILESTATE_NOPVPZONE)) {
Creature* owner = g_game.getCreatureByID(ownerId);
if (g_game().getWorldType() == WORLD_TYPE_NO_PVP || getTile()->hasFlag(TILESTATE_NOPVPZONE)) {
Creature* owner = g_game().getCreatureByID(ownerId);
if (owner) {
if (owner->getPlayer() || (owner->isSummon() && owner->getMaster()->getPlayer())) {
harmfulField = false;
Expand All @@ -1593,7 +1590,7 @@ void MagicField::onStepInField(Creature* creature)

Player* targetPlayer = creature->getPlayer();
if (targetPlayer) {
Player* attackerPlayer = g_game.getPlayerByID(ownerId);
const Player* attackerPlayer = g_game().getPlayerByID(ownerId);
if (attackerPlayer) {
if (Combat::isProtected(attackerPlayer, targetPlayer)) {
harmfulField = false;
Expand Down

0 comments on commit 126e7df

Please sign in to comment.