1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

Reduce non-const access to gamestate on server

This commit is contained in:
Ivan Savenko
2025-05-20 20:38:13 +03:00
parent 8255a4b9ae
commit af20b39fe6
13 changed files with 32 additions and 32 deletions

View File

@@ -574,7 +574,7 @@ EDiggingStatus CGameInfoCallback::getTileDigStatus(int3 tile, bool verbose) cons
return getTile(tile)->getDiggingStatus(); return getTile(tile)->getDiggingStatus();
} }
EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID ) EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID ) const
{ {
ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED); ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED);
@@ -947,7 +947,7 @@ void CGameInfoCallback::getAllTiles(std::unordered_set<int3> & tiles, std::optio
} }
} }
void CGameInfoCallback::getAllowedSpells(std::vector<SpellID> & out, std::optional<ui16> level) void CGameInfoCallback::getAllowedSpells(std::vector<SpellID> & out, std::optional<ui16> level) const
{ {
for (auto const & spellID : gameState().getMap().allowedSpells) for (auto const & spellID : gameState().getMap().allowedSpells)
{ {

View File

@@ -93,7 +93,7 @@ public:
int howManyTowns(PlayerColor Player) const; int howManyTowns(PlayerColor Player) const;
std::vector<const CGHeroInstance *> getAvailableHeroes(const CGObjectInstance * townOrTavern) const; std::vector<const CGHeroInstance *> getAvailableHeroes(const CGObjectInstance * townOrTavern) const;
std::string getTavernRumor(const CGObjectInstance * townOrTavern) const; std::string getTavernRumor(const CGObjectInstance * townOrTavern) const;
EBuildingState canBuildStructure(const CGTownInstance *t, BuildingID ID); EBuildingState canBuildStructure(const CGTownInstance *t, BuildingID ID) const;
bool getTownInfo(const CGObjectInstance * town, InfoAboutTown & dest, const CGObjectInstance * selectedObject = nullptr) const; bool getTownInfo(const CGObjectInstance * town, InfoAboutTown & dest, const CGObjectInstance * selectedObject = nullptr) const;
//from gs //from gs
@@ -115,7 +115,7 @@ public:
void getTilesInRange(std::unordered_set<int3> & tiles, const int3 & pos, int radius, ETileVisibility mode, std::optional<PlayerColor> player = std::optional<PlayerColor>(), int3::EDistanceFormula formula = int3::DIST_2D) const override; void getTilesInRange(std::unordered_set<int3> & tiles, const int3 & pos, int radius, ETileVisibility mode, std::optional<PlayerColor> player = std::optional<PlayerColor>(), int3::EDistanceFormula formula = int3::DIST_2D) const override;
void getAllTiles(std::unordered_set<int3> &tiles, std::optional<PlayerColor> player, int level, std::function<bool(const TerrainTile *)> filter) const override; void getAllTiles(std::unordered_set<int3> &tiles, std::optional<PlayerColor> player, int level, std::function<bool(const TerrainTile *)> filter) const override;
void getAllowedSpells(std::vector<SpellID> &out, std::optional<ui16> level = std::nullopt); void getAllowedSpells(std::vector<SpellID> &out, std::optional<ui16> level = std::nullopt) const;
#if SCRIPTING_ENABLED #if SCRIPTING_ENABLED
virtual scripting::Pool * getGlobalContextPool() const override; virtual scripting::Pool * getGlobalContextPool() const override;

View File

@@ -1028,7 +1028,7 @@ BattleInfo * CGameState::getBattle(const BattleID & battle)
return nullptr; return nullptr;
} }
BattleField CGameState::battleGetBattlefieldType(int3 tile, vstd::RNG & randomGenerator) BattleField CGameState::battleGetBattlefieldType(int3 tile, vstd::RNG & randomGenerator) const
{ {
assert(tile.isValid()); assert(tile.isValid());
@@ -1394,7 +1394,7 @@ bool CGameState::checkForStandardLoss(const PlayerColor & player) const
return pState.checkVanquished(); return pState.checkVanquished();
} }
void CGameState::obtainPlayersStats(SThievesGuildInfo & tgi, int level) void CGameState::obtainPlayersStats(SThievesGuildInfo & tgi, int level) const
{ {
auto playerInactive = [&](const PlayerColor & color) auto playerInactive = [&](const PlayerColor & color)
{ {

View File

@@ -95,7 +95,7 @@ public:
HeroTypeID pickNextHeroType(vstd::RNG & randomGenerator, const PlayerColor & owner); HeroTypeID pickNextHeroType(vstd::RNG & randomGenerator, const PlayerColor & owner);
void apply(CPackForClient & pack); void apply(CPackForClient & pack);
BattleField battleGetBattlefieldType(int3 tile, vstd::RNG & randomGenerator); BattleField battleGetBattlefieldType(int3 tile, vstd::RNG & randomGenerator) const;
PlayerRelations getPlayerRelations(PlayerColor color1, PlayerColor color2) const override; PlayerRelations getPlayerRelations(PlayerColor color1, PlayerColor color2) const override;
void calculatePaths(const std::shared_ptr<PathfinderConfig> & config) const override; void calculatePaths(const std::shared_ptr<PathfinderConfig> & config) const override;
@@ -123,7 +123,8 @@ public:
PlayerColor checkForStandardWin() const; //returns color of player that accomplished standard victory conditions or 255 (NEUTRAL) if no winner PlayerColor checkForStandardWin() const; //returns color of player that accomplished standard victory conditions or 255 (NEUTRAL) if no winner
bool checkForStandardLoss(const PlayerColor & player) const; //checks if given player lost the game bool checkForStandardLoss(const PlayerColor & player) const; //checks if given player lost the game
void obtainPlayersStats(SThievesGuildInfo & tgi, int level); //fills tgi with info about other players that is available at given level of thieves' guild //fills tgi with info about other players that is available at given level of thieves' guild
void obtainPlayersStats(SThievesGuildInfo & tgi, int level) const;
const IGameSettings & getSettings() const override; const IGameSettings & getSettings() const override;
StartInfo * getStartInfo() StartInfo * getStartInfo()

View File

@@ -79,7 +79,7 @@ StatisticDataSetEntry StatisticDataSet::createEntry(const PlayerState * ps, cons
return data; return data;
} }
std::string StatisticDataSet::toCsv(std::string sep) std::string StatisticDataSet::toCsv(std::string sep) const
{ {
std::stringstream ss; std::stringstream ss;
@@ -162,7 +162,7 @@ std::string StatisticDataSet::toCsv(std::string sep)
for(auto & resource : resources) for(auto & resource : resources)
ss << sep << entry.resources[resource]; ss << sep << entry.resources[resource];
for(auto & resource : resources) for(auto & resource : resources)
ss << sep << entry.numMines[resource]; ss << sep << entry.numMines.at(resource);
for(auto & resource : resources) for(auto & resource : resources)
ss << sep << entry.spentResourcesForArmy[resource]; ss << sep << entry.spentResourcesForArmy[resource];
for(auto & resource : resources) for(auto & resource : resources)
@@ -175,7 +175,7 @@ std::string StatisticDataSet::toCsv(std::string sep)
return ss.str(); return ss.str();
} }
std::string StatisticDataSet::writeCsv() std::string StatisticDataSet::writeCsv() const
{ {
const boost::filesystem::path outPath = VCMIDirs::get().userCachePath() / "statistic"; const boost::filesystem::path outPath = VCMIDirs::get().userCachePath() / "statistic";
boost::filesystem::create_directories(outPath); boost::filesystem::create_directories(outPath);

View File

@@ -102,8 +102,8 @@ class DLL_LINKAGE StatisticDataSet
public: public:
void add(StatisticDataSetEntry entry); void add(StatisticDataSetEntry entry);
static StatisticDataSetEntry createEntry(const PlayerState * ps, const CGameState * gs); static StatisticDataSetEntry createEntry(const PlayerState * ps, const CGameState * gs);
std::string toCsv(std::string sep); std::string toCsv(std::string sep) const;
std::string writeCsv(); std::string writeCsv() const;
struct PlayerAccumulatedValueStorage // holds some actual values needed for stats struct PlayerAccumulatedValueStorage // holds some actual values needed for stats
{ {

View File

@@ -868,12 +868,12 @@ const CArtifactInstance * CMap::getArtifactInstance(const ArtifactInstanceID & a
return artInstances.at(artifactID.getNum()).get(); return artInstances.at(artifactID.getNum()).get();
} }
const std::vector<ObjectInstanceID> & CMap::getAllTowns() const std::vector<ObjectInstanceID> & CMap::getAllTowns() const
{ {
return towns; return towns;
} }
const std::vector<ObjectInstanceID> & CMap::getHeroesOnMap() const std::vector<ObjectInstanceID> & CMap::getHeroesOnMap() const
{ {
return heroesOnMap; return heroesOnMap;
} }

View File

@@ -236,10 +236,10 @@ public:
/// Returns ID's of all heroes that are currently present on map /// Returns ID's of all heroes that are currently present on map
/// Includes all garrisoned and imprisoned heroes /// Includes all garrisoned and imprisoned heroes
const std::vector<ObjectInstanceID> & getHeroesOnMap(); const std::vector<ObjectInstanceID> & getHeroesOnMap() const;
/// Returns ID's of all towns present on map /// Returns ID's of all towns present on map
const std::vector<ObjectInstanceID> & getAllTowns(); const std::vector<ObjectInstanceID> & getAllTowns() const;
/// Sets the victory/loss condition objectives ?? /// Sets the victory/loss condition objectives ??
void checkForObjectives(); void checkForObjectives();

View File

@@ -438,9 +438,6 @@ void CGameHandler::handleClientDisconnection(std::shared_ptr<CConnection> c)
for(auto & playerConnections : connections) for(auto & playerConnections : connections)
{ {
PlayerColor playerId = playerConnections.first; PlayerColor playerId = playerConnections.first;
const auto * playerSettings = gameState().getStartInfo()->getPlayersSettings(playerId.getNum());
if(!playerSettings)
continue;
auto playerConnection = vstd::find(playerConnections.second, c); auto playerConnection = vstd::find(playerConnections.second, c);
if(playerConnection == playerConnections.second.end()) if(playerConnection == playerConnections.second.end())
@@ -818,8 +815,8 @@ bool CGameHandler::moveHero(ObjectInstanceID hid, int3 dst, EMovementMode moveme
const TerrainTile t = *gameInfo().getTile(hmpos); const TerrainTile t = *gameInfo().getTile(hmpos);
const int3 guardPos = gameState().guardingCreaturePosition(hmpos); const int3 guardPos = gameState().guardingCreaturePosition(hmpos);
CGObjectInstance * objectToVisit = nullptr; const CGObjectInstance * objectToVisit = nullptr;
CGObjectInstance * guardian = nullptr; const CGObjectInstance * guardian = nullptr;
if (!t.visitableObjects.empty()) if (!t.visitableObjects.empty())
objectToVisit = gameState().getObjInstance(t.visitableObjects.back()); objectToVisit = gameState().getObjInstance(t.visitableObjects.back());
@@ -1003,7 +1000,7 @@ bool CGameHandler::moveHero(ObjectInstanceID hid, int3 dst, EMovementMode moveme
// do not visit any other objects, e.g. monoliths to avoid double-teleporting // do not visit any other objects, e.g. monoliths to avoid double-teleporting
if (objectToVisit) if (objectToVisit)
{ {
if (CGTownInstance * town = dynamic_cast<CGTownInstance *>(objectToVisit)) if (const CGTownInstance * town = dynamic_cast<const CGTownInstance *>(objectToVisit))
objectVisited(town, h); objectVisited(town, h);
} }
@@ -2320,7 +2317,7 @@ bool CGameHandler::razeStructure (ObjectInstanceID tid, BuildingID bid)
bool CGameHandler::spellResearch(ObjectInstanceID tid, SpellID spellAtSlot, bool accepted) bool CGameHandler::spellResearch(ObjectInstanceID tid, SpellID spellAtSlot, bool accepted)
{ {
CGTownInstance *t = gameState().getTown(tid); const CGTownInstance * t = gameState().getTown(tid);
if(!gameInfo().getSettings().getBoolean(EGameSettings::TOWNS_SPELL_RESEARCH) && complain("Spell research not allowed!")) if(!gameInfo().getSettings().getBoolean(EGameSettings::TOWNS_SPELL_RESEARCH) && complain("Spell research not allowed!"))
return false; return false;
@@ -2721,7 +2718,7 @@ bool CGameHandler::bulkMoveArtifacts(const PlayerColor & player, ObjectInstanceI
auto & slotsDstSrc = ma.artsPack1; auto & slotsDstSrc = ma.artsPack1;
// Temporary fitting set for artifacts. Used to select available slots before sending data. // Temporary fitting set for artifacts. Used to select available slots before sending data.
CArtifactFittingSet artFittingSet(&gameState(), pdstSet->bearerType()); CArtifactFittingSet artFittingSet(&gameInfo(), pdstSet->bearerType());
auto moveArtifact = [this, &artFittingSet, dstId](const CArtifactInstance * artifact, auto moveArtifact = [this, &artFittingSet, dstId](const CArtifactInstance * artifact,
ArtifactPosition srcSlot, std::vector<MoveArtifactInfo> & slots) -> void ArtifactPosition srcSlot, std::vector<MoveArtifactInfo> & slots) -> void
@@ -4288,7 +4285,7 @@ std::shared_ptr<CGObjectInstance> CGameHandler::createNewObject(const int3 & vis
auto handler = LIBRARY->objtypeh->getHandlerFor(objectID, subID); auto handler = LIBRARY->objtypeh->getHandlerFor(objectID, subID);
auto o = handler->create(&gameState(), nullptr); auto o = handler->create(&gameInfo(), nullptr);
handler->configureObject(o.get(), *randomizer); handler->configureObject(o.get(), *randomizer);
assert(o->ID == objectID); assert(o->ID == objectID);
gameState().getMap().generateUniqueInstanceName(o.get()); gameState().getMap().generateUniqueInstanceName(o.get());
@@ -4321,7 +4318,7 @@ void CGameHandler::createWanderingMonster(const int3 & visitablePosition, Creatu
cre->gainedArtifact = ArtifactID::NONE; cre->gainedArtifact = ArtifactID::NONE;
cre->identifier = -1; cre->identifier = -1;
cre->temppower = static_cast<int64_t>(unitSize) * 1000; cre->temppower = static_cast<int64_t>(unitSize) * 1000;
cre->addToSlot(SlotID(0), std::make_unique<CStackInstance>(&gameState(), creature, unitSize)); cre->addToSlot(SlotID(0), std::make_unique<CStackInstance>(&gameInfo(), creature, unitSize));
newObject(createdObject, PlayerColor::NEUTRAL); newObject(createdObject, PlayerColor::NEUTRAL);
} }

View File

@@ -11,7 +11,6 @@
#include <vcmi/Environment.h> #include <vcmi/Environment.h>
#include "../lib/callback/CGameInfoCallback.h"
#include "../lib/callback/IGameEventCallback.h" #include "../lib/callback/IGameEventCallback.h"
#include "../lib/LoadProgress.h" #include "../lib/LoadProgress.h"
#include "../lib/ScriptHandler.h" #include "../lib/ScriptHandler.h"
@@ -29,6 +28,8 @@ class EVictoryLossCheckResult;
class CRandomGenerator; class CRandomGenerator;
class GameRandomizer; class GameRandomizer;
struct StartInfo;
struct TerrainTile;
struct CPackForServer; struct CPackForServer;
struct NewTurn; struct NewTurn;
struct CGarrisonOperationPack; struct CGarrisonOperationPack;

View File

@@ -175,7 +175,7 @@ BattleID BattleProcessor::setupBattle(int3 tile, BattleSideArray<const CArmedIns
//send info about battles //send info about battles
BattleStart bs; BattleStart bs;
bs.info = BattleInfo::setupBattle(&gameHandler->gameState(), tile, terrain, battlefieldType, armies, heroes, layout, town); bs.info = BattleInfo::setupBattle(&gameHandler->gameInfo(), tile, terrain, battlefieldType, armies, heroes, layout, town);
bs.battleID = gameHandler->gameState().nextBattleID; bs.battleID = gameHandler->gameState().nextBattleID;
engageIntoBattle(bs.info->getSide(BattleSide::ATTACKER).color); engageIntoBattle(bs.info->getSide(BattleSide::ATTACKER).color);

View File

@@ -567,7 +567,7 @@ std::vector<SetMana> NewTurnProcessor::updateHeroesManaPoints()
for (auto & elem : gameHandler->gameState().players) for (auto & elem : gameHandler->gameState().players)
{ {
for (CGHeroInstance *h : elem.second.getHeroes()) for (const CGHeroInstance *h : elem.second.getHeroes())
{ {
int32_t newMana = h->getManaNewTurn(); int32_t newMana = h->getManaNewTurn();
@@ -584,7 +584,7 @@ std::vector<SetMovePoints> NewTurnProcessor::updateHeroesMovementPoints()
for (auto & elem : gameHandler->gameState().players) for (auto & elem : gameHandler->gameState().players)
{ {
for (CGHeroInstance *h : elem.second.getHeroes()) for (const CGHeroInstance *h : elem.second.getHeroes())
{ {
auto ti = h->getTurnInfo(1); auto ti = h->getTurnInfo(1);
// NOTE: this code executed when bonuses of previous day not yet updated (this happen in NewTurn::applyGs). See issue 2356 // NOTE: this code executed when bonuses of previous day not yet updated (this happen in NewTurn::applyGs). See issue 2356

View File

@@ -13,6 +13,7 @@
#include "QueriesProcessor.h" #include "QueriesProcessor.h"
#include "../CGameHandler.h" #include "../CGameHandler.h"
#include "../TurnTimerHandler.h" #include "../TurnTimerHandler.h"
#include "../../lib/callback/IGameInfoCallback.h"
#include "../../lib/mapObjects/CGHeroInstance.h" #include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/mapObjects/MiscObjects.h" #include "../../lib/mapObjects/MiscObjects.h"
#include "../../lib/networkPacks/PacksForServer.h" #include "../../lib/networkPacks/PacksForServer.h"