Reduce non-const access to gamestate on server

This commit is contained in:
Ivan Savenko
2025-05-23 14:17:25 +03:00
parent 8255a4b9ae
commit af20b39fe6
13 changed files with 32 additions and 32 deletions
+2 -2
View File
@@ -574,7 +574,7 @@ EDiggingStatus CGameInfoCallback::getTileDigStatus(int3 tile, bool verbose) cons
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);
@@ -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)
{
+2 -2
View File
@@ -93,7 +93,7 @@ public:
int howManyTowns(PlayerColor Player) const;
std::vector<const CGHeroInstance *> getAvailableHeroes(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;
//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 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
virtual scripting::Pool * getGlobalContextPool() const override;
+2 -2
View File
@@ -1028,7 +1028,7 @@ BattleInfo * CGameState::getBattle(const BattleID & battle)
return nullptr;
}
BattleField CGameState::battleGetBattlefieldType(int3 tile, vstd::RNG & randomGenerator)
BattleField CGameState::battleGetBattlefieldType(int3 tile, vstd::RNG & randomGenerator) const
{
assert(tile.isValid());
@@ -1394,7 +1394,7 @@ bool CGameState::checkForStandardLoss(const PlayerColor & player) const
return pState.checkVanquished();
}
void CGameState::obtainPlayersStats(SThievesGuildInfo & tgi, int level)
void CGameState::obtainPlayersStats(SThievesGuildInfo & tgi, int level) const
{
auto playerInactive = [&](const PlayerColor & color)
{
+3 -2
View File
@@ -95,7 +95,7 @@ public:
HeroTypeID pickNextHeroType(vstd::RNG & randomGenerator, const PlayerColor & owner);
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;
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
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;
StartInfo * getStartInfo()
+3 -3
View File
@@ -79,7 +79,7 @@ StatisticDataSetEntry StatisticDataSet::createEntry(const PlayerState * ps, cons
return data;
}
std::string StatisticDataSet::toCsv(std::string sep)
std::string StatisticDataSet::toCsv(std::string sep) const
{
std::stringstream ss;
@@ -162,7 +162,7 @@ std::string StatisticDataSet::toCsv(std::string sep)
for(auto & resource : resources)
ss << sep << entry.resources[resource];
for(auto & resource : resources)
ss << sep << entry.numMines[resource];
ss << sep << entry.numMines.at(resource);
for(auto & resource : resources)
ss << sep << entry.spentResourcesForArmy[resource];
for(auto & resource : resources)
@@ -175,7 +175,7 @@ std::string StatisticDataSet::toCsv(std::string sep)
return ss.str();
}
std::string StatisticDataSet::writeCsv()
std::string StatisticDataSet::writeCsv() const
{
const boost::filesystem::path outPath = VCMIDirs::get().userCachePath() / "statistic";
boost::filesystem::create_directories(outPath);
+2 -2
View File
@@ -102,8 +102,8 @@ class DLL_LINKAGE StatisticDataSet
public:
void add(StatisticDataSetEntry entry);
static StatisticDataSetEntry createEntry(const PlayerState * ps, const CGameState * gs);
std::string toCsv(std::string sep);
std::string writeCsv();
std::string toCsv(std::string sep) const;
std::string writeCsv() const;
struct PlayerAccumulatedValueStorage // holds some actual values needed for stats
{
+2 -2
View File
@@ -868,12 +868,12 @@ const CArtifactInstance * CMap::getArtifactInstance(const ArtifactInstanceID & a
return artInstances.at(artifactID.getNum()).get();
}
const std::vector<ObjectInstanceID> & CMap::getAllTowns()
const std::vector<ObjectInstanceID> & CMap::getAllTowns() const
{
return towns;
}
const std::vector<ObjectInstanceID> & CMap::getHeroesOnMap()
const std::vector<ObjectInstanceID> & CMap::getHeroesOnMap() const
{
return heroesOnMap;
}
+2 -2
View File
@@ -236,10 +236,10 @@ public:
/// Returns ID's of all heroes that are currently present on map
/// 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
const std::vector<ObjectInstanceID> & getAllTowns();
const std::vector<ObjectInstanceID> & getAllTowns() const;
/// Sets the victory/loss condition objectives ??
void checkForObjectives();
+8 -11
View File
@@ -438,10 +438,7 @@ void CGameHandler::handleClientDisconnection(std::shared_ptr<CConnection> c)
for(auto & playerConnections : connections)
{
PlayerColor playerId = playerConnections.first;
const auto * playerSettings = gameState().getStartInfo()->getPlayersSettings(playerId.getNum());
if(!playerSettings)
continue;
auto playerConnection = vstd::find(playerConnections.second, c);
if(playerConnection == playerConnections.second.end())
continue;
@@ -818,8 +815,8 @@ bool CGameHandler::moveHero(ObjectInstanceID hid, int3 dst, EMovementMode moveme
const TerrainTile t = *gameInfo().getTile(hmpos);
const int3 guardPos = gameState().guardingCreaturePosition(hmpos);
CGObjectInstance * objectToVisit = nullptr;
CGObjectInstance * guardian = nullptr;
const CGObjectInstance * objectToVisit = nullptr;
const CGObjectInstance * guardian = nullptr;
if (!t.visitableObjects.empty())
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
if (objectToVisit)
{
if (CGTownInstance * town = dynamic_cast<CGTownInstance *>(objectToVisit))
if (const CGTownInstance * town = dynamic_cast<const CGTownInstance *>(objectToVisit))
objectVisited(town, h);
}
@@ -2320,7 +2317,7 @@ bool CGameHandler::razeStructure (ObjectInstanceID tid, BuildingID bid)
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!"))
return false;
@@ -2721,7 +2718,7 @@ bool CGameHandler::bulkMoveArtifacts(const PlayerColor & player, ObjectInstanceI
auto & slotsDstSrc = ma.artsPack1;
// 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,
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 o = handler->create(&gameState(), nullptr);
auto o = handler->create(&gameInfo(), nullptr);
handler->configureObject(o.get(), *randomizer);
assert(o->ID == objectID);
gameState().getMap().generateUniqueInstanceName(o.get());
@@ -4321,7 +4318,7 @@ void CGameHandler::createWanderingMonster(const int3 & visitablePosition, Creatu
cre->gainedArtifact = ArtifactID::NONE;
cre->identifier = -1;
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);
}
+2 -1
View File
@@ -11,7 +11,6 @@
#include <vcmi/Environment.h>
#include "../lib/callback/CGameInfoCallback.h"
#include "../lib/callback/IGameEventCallback.h"
#include "../lib/LoadProgress.h"
#include "../lib/ScriptHandler.h"
@@ -29,6 +28,8 @@ class EVictoryLossCheckResult;
class CRandomGenerator;
class GameRandomizer;
struct StartInfo;
struct TerrainTile;
struct CPackForServer;
struct NewTurn;
struct CGarrisonOperationPack;
+1 -1
View File
@@ -175,7 +175,7 @@ BattleID BattleProcessor::setupBattle(int3 tile, BattleSideArray<const CArmedIns
//send info about battles
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;
engageIntoBattle(bs.info->getSide(BattleSide::ATTACKER).color);
+2 -2
View File
@@ -567,7 +567,7 @@ std::vector<SetMana> NewTurnProcessor::updateHeroesManaPoints()
for (auto & elem : gameHandler->gameState().players)
{
for (CGHeroInstance *h : elem.second.getHeroes())
for (const CGHeroInstance *h : elem.second.getHeroes())
{
int32_t newMana = h->getManaNewTurn();
@@ -584,7 +584,7 @@ std::vector<SetMovePoints> NewTurnProcessor::updateHeroesMovementPoints()
for (auto & elem : gameHandler->gameState().players)
{
for (CGHeroInstance *h : elem.second.getHeroes())
for (const CGHeroInstance *h : elem.second.getHeroes())
{
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
+1
View File
@@ -13,6 +13,7 @@
#include "QueriesProcessor.h"
#include "../CGameHandler.h"
#include "../TurnTimerHandler.h"
#include "../../lib/callback/IGameInfoCallback.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/mapObjects/MiscObjects.h"
#include "../../lib/networkPacks/PacksForServer.h"