1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-23 00:28:08 +02:00

StartInfo in gamestate now use unique_ptr

This commit is contained in:
Ivan Savenko
2025-03-03 14:46:15 +00:00
parent 82506d5eff
commit b6026084ea
13 changed files with 44 additions and 30 deletions

View File

@ -49,7 +49,6 @@
#include "../lib/rmg/CMapGenOptions.h" #include "../lib/rmg/CMapGenOptions.h"
#include "../lib/serializer/Connection.h" #include "../lib/serializer/Connection.h"
#include "../lib/filesystem/Filesystem.h" #include "../lib/filesystem/Filesystem.h"
#include "../lib/serializer/CMemorySerializer.h"
#include "../lib/UnlockGuard.h" #include "../lib/UnlockGuard.h"
#include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid.hpp>

View File

@ -254,7 +254,7 @@ void CClient::initPlayerEnvironments()
void CClient::initPlayerInterfaces() void CClient::initPlayerInterfaces()
{ {
for(auto & playerInfo : gs->scenarioOps->playerInfos) for(auto & playerInfo : gs->getStartInfo()->playerInfos)
{ {
PlayerColor color = playerInfo.first; PlayerColor color = playerInfo.first;
if(!vstd::contains(GAME->server().getAllClientPlayers(GAME->server().logicConnection->connectionID), color)) if(!vstd::contains(GAME->server().getAllClientPlayers(GAME->server().logicConnection->connectionID), color))
@ -266,7 +266,7 @@ void CClient::initPlayerInterfaces()
if(playerInfo.second.isControlledByAI() || settings["session"]["onlyai"].Bool()) if(playerInfo.second.isControlledByAI() || settings["session"]["onlyai"].Bool())
{ {
bool alliedToHuman = false; bool alliedToHuman = false;
for(auto & allyInfo : gs->scenarioOps->playerInfos) for(auto & allyInfo : gs->getStartInfo()->playerInfos)
if (gs->getPlayerTeam(allyInfo.first) == gs->getPlayerTeam(playerInfo.first) && allyInfo.second.isControlledByHuman()) if (gs->getPlayerTeam(allyInfo.first) == gs->getPlayerTeam(playerInfo.first) && allyInfo.second.isControlledByHuman())
alliedToHuman = true; alliedToHuman = true;

View File

@ -53,7 +53,7 @@ int CGameInfoCallback::getResource(PlayerColor Player, GameResID which) const
const PlayerSettings * CGameInfoCallback::getPlayerSettings(PlayerColor color) const const PlayerSettings * CGameInfoCallback::getPlayerSettings(PlayerColor color) const
{ {
return &gs->scenarioOps->getIthPlayersSettings(color); return &gs->getStartInfo()->getIthPlayersSettings(color);
} }
bool CGameInfoCallback::isAllowed(SpellID id) const bool CGameInfoCallback::isAllowed(SpellID id) const
@ -192,13 +192,14 @@ void CGameInfoCallback::fillUpgradeInfo(const CArmedInstance *obj, SlotID stackP
//return gs->fillUpgradeInfo(obj->getStack(stackPos)); //return gs->fillUpgradeInfo(obj->getStack(stackPos));
} }
const StartInfo * CGameInfoCallback::getStartInfo(bool beforeRandomization) const const StartInfo * CGameInfoCallback::getStartInfo() const
{ {
//std::shared_lock<std::shared_mutex> lock(*gs->mx); return gs->getStartInfo();
if(beforeRandomization) }
return gs->initialOpts;
else const StartInfo * CGameInfoCallback::getInitialStartInfo() const
return gs->scenarioOps; {
return gs->getInitialStartInfo();
} }
int32_t CGameInfoCallback::getSpellCost(const spells::Spell * sp, const CGHeroInstance * caster) const int32_t CGameInfoCallback::getSpellCost(const spells::Spell * sp, const CGHeroInstance * caster) const
@ -374,7 +375,7 @@ bool CGameInfoCallback::getHeroInfo(const CGObjectInstance * hero, InfoAboutHero
for(auto & elem : info.army) for(auto & elem : info.army)
elem.second.count = 0; elem.second.count = 0;
const auto factionIndex = getStartInfo(false)->playerInfos.at(h->tempOwner).castle; const auto factionIndex = getStartInfo()->playerInfos.at(h->tempOwner).castle;
int maxAIValue = 0; int maxAIValue = 0;
const CCreature * mostStrong = nullptr; const CCreature * mostStrong = nullptr;

View File

@ -58,7 +58,8 @@ public:
// //various // //various
virtual int getDate(Date mode=Date::DAY) const = 0; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month virtual int getDate(Date mode=Date::DAY) const = 0; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
virtual const StartInfo * getStartInfo(bool beforeRandomization = false) const = 0; virtual const StartInfo * getStartInfo() const = 0;
virtual const StartInfo * getInitialStartInfo() const = 0;
virtual bool isAllowed(SpellID id) const = 0; virtual bool isAllowed(SpellID id) const = 0;
virtual bool isAllowed(ArtifactID id) const = 0; virtual bool isAllowed(ArtifactID id) const = 0;
virtual bool isAllowed(SecondarySkill id) const = 0; virtual bool isAllowed(SecondarySkill id) const = 0;
@ -145,7 +146,8 @@ protected:
public: public:
//various //various
int getDate(Date mode=Date::DAY)const override; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month int getDate(Date mode=Date::DAY)const override; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
const StartInfo * getStartInfo(bool beforeRandomization = false) const override; const StartInfo * getStartInfo() const override;
const StartInfo * getInitialStartInfo() const override;
bool isAllowed(SpellID id) const override; bool isAllowed(SpellID id) const override;
bool isAllowed(ArtifactID id) const override; bool isAllowed(ArtifactID id) const override;
bool isAllowed(SecondarySkill id) const override; bool isAllowed(SecondarySkill id) const override;

View File

@ -210,7 +210,7 @@ void CPrivilegedInfoCallback::saveCommonState(CSaveFile & out) const
logGlobal->info("\tSaving header"); logGlobal->info("\tSaving header");
out.serializer & static_cast<CMapHeader&>(*gs->map); out.serializer & static_cast<CMapHeader&>(*gs->map);
logGlobal->info("\tSaving options"); logGlobal->info("\tSaving options");
out.serializer & gs->scenarioOps; out.serializer & gs->getStartInfo();
logGlobal->info("\tSaving mod list"); logGlobal->info("\tSaving mod list");
out.serializer & activeMods; out.serializer & activeMods;
logGlobal->info("\tSaving gamestate"); logGlobal->info("\tSaving gamestate");

View File

@ -151,8 +151,6 @@ CGameState::~CGameState()
// explicitly delete all ongoing battles first - BattleInfo destructor requires valid CGameState // explicitly delete all ongoing battles first - BattleInfo destructor requires valid CGameState
currentBattles.clear(); currentBattles.clear();
map.dellNull(); map.dellNull();
scenarioOps.dellNull();
initialOpts.dellNull();
} }
const IGameSettings & CGameState::getSettings() const const IGameSettings & CGameState::getSettings() const
@ -170,8 +168,8 @@ void CGameState::init(const IMapService * mapService, StartInfo * si, Load::Prog
{ {
assert(services); assert(services);
assert(callback); assert(callback);
scenarioOps = CMemorySerializer::deepCopy(*si).release(); scenarioOps = CMemorySerializer::deepCopy(*si);
initialOpts = CMemorySerializer::deepCopy(*si).release(); initialOpts = CMemorySerializer::deepCopy(*si);
si = nullptr; si = nullptr;
switch(scenarioOps->mode) switch(scenarioOps->mode)

View File

@ -46,6 +46,9 @@ DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const EVictoryLossCheck
class DLL_LINKAGE CGameState : public CNonConstInfoCallback, public Serializeable class DLL_LINKAGE CGameState : public CNonConstInfoCallback, public Serializeable
{ {
friend class CGameStateCampaign; friend class CGameStateCampaign;
std::unique_ptr<StartInfo> initialOpts; //copy of settings received from pregame (not randomized)
std::unique_ptr<StartInfo> scenarioOps;
public: public:
/// Stores number of times each artifact was placed on map via randomization /// Stores number of times each artifact was placed on map via randomization
std::map<ArtifactID, int> allocatedArtifacts; std::map<ArtifactID, int> allocatedArtifacts;
@ -71,8 +74,6 @@ public:
void init(const IMapService * mapService, StartInfo * si, Load::ProgressAccumulator &, bool allowSavingRandomMap = true); void init(const IMapService * mapService, StartInfo * si, Load::ProgressAccumulator &, bool allowSavingRandomMap = true);
void updateOnLoad(StartInfo * si); void updateOnLoad(StartInfo * si);
ConstTransitivePtr<StartInfo> scenarioOps;
ConstTransitivePtr<StartInfo> initialOpts; //copy of settings received from pregame (not randomized)
ui32 day; //total number of days in game ui32 day; //total number of days in game
ConstTransitivePtr<CMap> map; ConstTransitivePtr<CMap> map;
std::map<PlayerColor, PlayerState> players; std::map<PlayerColor, PlayerState> players;
@ -123,6 +124,19 @@ public:
void obtainPlayersStats(SThievesGuildInfo & tgi, int level); //fills tgi with info about other players that is available at given level of thieves' guild void obtainPlayersStats(SThievesGuildInfo & tgi, int level); //fills tgi with info about other players that is available at given level of thieves' guild
const IGameSettings & getSettings() const; const IGameSettings & getSettings() const;
StartInfo * getStartInfo()
{
return scenarioOps.get();
}
const StartInfo * getStartInfo() const final
{
return scenarioOps.get();
}
const StartInfo * getInitialStartInfo() const final
{
return initialOpts.get();
}
bool isVisible(int3 pos, const std::optional<PlayerColor> & player) const override; bool isVisible(int3 pos, const std::optional<PlayerColor> & player) const override;
bool isVisible(const CGObjectInstance * obj, const std::optional<PlayerColor> & player) const override; bool isVisible(const CGObjectInstance * obj, const std::optional<PlayerColor> & player) const override;

View File

@ -28,7 +28,6 @@
#include "../mapping/CMap.h" #include "../mapping/CMap.h"
#include "../ArtifactUtils.h" #include "../ArtifactUtils.h"
#include "../CPlayerState.h" #include "../CPlayerState.h"
#include "../serializer/CMemorySerializer.h"
#include <vstd/RNG.h> #include <vstd/RNG.h>
#include <vcmi/HeroTypeService.h> #include <vcmi/HeroTypeService.h>

View File

@ -444,10 +444,10 @@ DamageRange CGTownInstance::getKeepDamageRange() const
FactionID CGTownInstance::randomizeFaction(vstd::RNG & rand) FactionID CGTownInstance::randomizeFaction(vstd::RNG & rand)
{ {
if(getOwner().isValidPlayer()) if(getOwner().isValidPlayer())
return cb->gameState()->scenarioOps->getIthPlayersSettings(getOwner()).castle; return cb->getStartInfo()->getIthPlayersSettings(getOwner()).castle;
if(alignmentToPlayer.isValidPlayer()) if(alignmentToPlayer.isValidPlayer())
return cb->gameState()->scenarioOps->getIthPlayersSettings(alignmentToPlayer).castle; return cb->getStartInfo()->getIthPlayersSettings(alignmentToPlayer).castle;
std::vector<FactionID> potentialPicks; std::vector<FactionID> potentialPicks;

View File

@ -1122,14 +1122,14 @@ void PlayerEndsGame::applyGs(CGameState *gs)
// TODO: Campaign-specific code might as well go somewhere else // TODO: Campaign-specific code might as well go somewhere else
// keep all heroes from the winning player // keep all heroes from the winning player
if(p->human && gs->scenarioOps->campState) if(p->human && gs->getStartInfo()->campState)
{ {
std::vector<CGHeroInstance *> crossoverHeroes; std::vector<CGHeroInstance *> crossoverHeroes;
for (CGHeroInstance * hero : gs->map->heroesOnMap) for (CGHeroInstance * hero : gs->map->heroesOnMap)
if (hero->tempOwner == player) if (hero->tempOwner == player)
crossoverHeroes.push_back(hero); crossoverHeroes.push_back(hero);
gs->scenarioOps->campState->setCurrentMapAsConquered(crossoverHeroes); gs->getStartInfo()->campState->setCurrentMapAsConquered(crossoverHeroes);
} }
} }
else else
@ -1143,14 +1143,14 @@ void PlayerEndsGame::applyGs(CGameState *gs)
void PlayerReinitInterface::applyGs(CGameState *gs) void PlayerReinitInterface::applyGs(CGameState *gs)
{ {
if(!gs || !gs->scenarioOps) if(!gs || !gs->getStartInfo())
return; return;
//TODO: what does mean if more that one player connected? //TODO: what does mean if more that one player connected?
if(playerConnectionId == PlayerSettings::PLAYER_AI) if(playerConnectionId == PlayerSettings::PLAYER_AI)
{ {
for(const auto & player : players) for(const auto & player : players)
gs->scenarioOps->getIthPlayersSettings(player).connectedPlayerIDs.clear(); gs->getStartInfo()->getIthPlayersSettings(player).connectedPlayerIDs.clear();
} }
} }

View File

@ -208,7 +208,7 @@ void CVCMIServer::prepareToRestart()
return; return;
} }
* si = * gh->gs->initialOpts; * si = * gh->gs->getInitialStartInfo();
setState(EServerState::LOBBY); setState(EServerState::LOBBY);
if (si->campState) if (si->campState)
{ {

View File

@ -225,7 +225,7 @@ void ApplyOnServerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
return; return;
} }
pack.initializedStartInfo = std::make_shared<StartInfo>(*srv.gh->getStartInfo(true)); pack.initializedStartInfo = std::make_shared<StartInfo>(*srv.gh->getInitialStartInfo());
pack.initializedGameState = srv.gh->gameState(); pack.initializedGameState = srv.gh->gameState();
result = true; result = true;
} }

View File

@ -17,7 +17,8 @@ class IGameInfoCallbackMock : public IGameInfoCallback
public: public:
//various //various
MOCK_CONST_METHOD1(getDate, int(Date)); MOCK_CONST_METHOD1(getDate, int(Date));
MOCK_CONST_METHOD1(getStartInfo, const StartInfo *(bool)); MOCK_CONST_METHOD0(getStartInfo, const StartInfo *());
MOCK_CONST_METHOD0(getInitialStartInfo, const StartInfo *());
MOCK_CONST_METHOD1(isAllowed, bool(SpellID)); MOCK_CONST_METHOD1(isAllowed, bool(SpellID));
MOCK_CONST_METHOD1(isAllowed, bool(ArtifactID)); MOCK_CONST_METHOD1(isAllowed, bool(ArtifactID));