1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-10 22:31:40 +02:00

Removed pointers from PlayerState

This commit is contained in:
Ivan Savenko
2025-03-26 15:24:34 +00:00
parent 6d65641a43
commit 331debaa3f
7 changed files with 46 additions and 84 deletions

View File

@@ -229,7 +229,7 @@ void CGameInfoCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObj
if(obj->ID == Obj::TOWN || obj->ID == Obj::TAVERN) if(obj->ID == Obj::TOWN || obj->ID == Obj::TAVERN)
{ {
int taverns = gameState()->players[*getPlayerID()].valOfBonuses(BonusType::THIEVES_GUILD_ACCESS); int taverns = gameState()->players.at(*getPlayerID()).valOfBonuses(BonusType::THIEVES_GUILD_ACCESS);
gameState()->obtainPlayersStats(thi, taverns); gameState()->obtainPlayersStats(thi, taverns);
} }
else if(obj->ID == Obj::DEN_OF_THIEVES) else if(obj->ID == Obj::DEN_OF_THIEVES)

View File

@@ -10,18 +10,25 @@
#include "StdInc.h" #include "StdInc.h"
#include "CPlayerState.h" #include "CPlayerState.h"
#include "json/JsonNode.h" #include "GameLibrary.h"
#include "mapObjects/CGDwelling.h" #include "IGameCallback.h"
#include "mapObjects/CGTownInstance.h"
#include "mapObjects/CGHeroInstance.h" #include "mapObjects/CGHeroInstance.h"
#include "mapObjects/CGTownInstance.h"
#include "gameState/CGameState.h"
#include "gameState/QuestInfo.h" #include "gameState/QuestInfo.h"
#include "texts/CGeneralTextHandler.h" #include "texts/CGeneralTextHandler.h"
#include "GameLibrary.h" #include "json/JsonNode.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
PlayerState::PlayerState() PlayerState::PlayerState()
: color(-1) :PlayerState(nullptr)
{}
PlayerState::PlayerState(IGameCallback *cb)
: CBonusSystemNode(PLAYER)
, GameCallbackHolder(cb)
, color(-1)
, human(false) , human(false)
, cheated(false) , cheated(false)
, playerLocalSettings(std::make_unique<JsonNode>()) , playerLocalSettings(std::make_unique<JsonNode>())
@@ -29,7 +36,6 @@ PlayerState::PlayerState()
, enteredLosingCheatCode(false) , enteredLosingCheatCode(false)
, status(EPlayerStatus::INGAME) , status(EPlayerStatus::INGAME)
{ {
setNodeType(PLAYER);
} }
PlayerState::~PlayerState() = default; PlayerState::~PlayerState() = default;
@@ -103,90 +109,50 @@ template<typename T>
std::vector<T> PlayerState::getObjectsOfType() const std::vector<T> PlayerState::getObjectsOfType() const
{ {
std::vector<T> result; std::vector<T> result;
for (auto const & object : ownedObjects) for (const ObjectInstanceID & objectID : ownedObjects)
{ {
auto casted = dynamic_cast<T>(object); auto objectPtr = cb->gameState()->getObjInstance(objectID);
auto casted = dynamic_cast<T>(objectPtr);
if (casted) if (casted)
result.push_back(casted); result.push_back(casted);
} }
return result; return result;
} }
const std::vector<const CGHeroInstance *> & PlayerState::getHeroes() const std::vector<const CGHeroInstance *> PlayerState::getHeroes() const
{ {
return constOwnedHeroes; return getObjectsOfType<const CGHeroInstance *>();
} }
const std::vector<const CGTownInstance *> & PlayerState::getTowns() const std::vector<const CGTownInstance *> PlayerState::getTowns() const
{ {
return constOwnedTowns; return getObjectsOfType<const CGTownInstance *>();
} }
const std::vector<CGHeroInstance *> & PlayerState::getHeroes() std::vector<CGHeroInstance *> PlayerState::getHeroes()
{ {
return ownedHeroes; return getObjectsOfType<CGHeroInstance *>();
} }
const std::vector<CGTownInstance *> & PlayerState::getTowns() std::vector<CGTownInstance *> PlayerState::getTowns()
{ {
return ownedTowns; return getObjectsOfType<CGTownInstance *>();
} }
std::vector<const CGObjectInstance *> PlayerState::getOwnedObjects() const std::vector<const CGObjectInstance *> PlayerState::getOwnedObjects() const
{ {
return {ownedObjects.begin(), ownedObjects.end()}; return getObjectsOfType<const CGObjectInstance *>();
} }
void PlayerState::addOwnedObject(CGObjectInstance * object) void PlayerState::addOwnedObject(CGObjectInstance * object)
{ {
assert(object->asOwnable() != nullptr); assert(object->asOwnable() != nullptr);
ownedObjects.push_back(object); ownedObjects.push_back(object->id);
auto * town = dynamic_cast<CGTownInstance*>(object);
auto * hero = dynamic_cast<CGHeroInstance*>(object);
if (town)
{
ownedTowns.push_back(town);
constOwnedTowns.push_back(town);
}
if (hero)
{
ownedHeroes.push_back(hero);
constOwnedHeroes.push_back(hero);
}
}
void PlayerState::postDeserialize()
{
for (const auto& object : ownedObjects)
{
auto* town = dynamic_cast<CGTownInstance*>(object);
auto* hero = dynamic_cast<CGHeroInstance*>(object);
if (town)
{
ownedTowns.push_back(town);
constOwnedTowns.push_back(town);
}
if (hero)
{
ownedHeroes.push_back(hero);
constOwnedHeroes.push_back(hero);
}
}
} }
void PlayerState::removeOwnedObject(CGObjectInstance * object) void PlayerState::removeOwnedObject(CGObjectInstance * object)
{ {
vstd::erase(ownedObjects, object); vstd::erase(ownedObjects, object->id);
vstd::erase(ownedTowns, object);
vstd::erase(constOwnedTowns, object);
vstd::erase(ownedHeroes, object);
vstd::erase(constOwnedHeroes, object);
} }
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@@ -12,10 +12,11 @@
#include <vcmi/Player.h> #include <vcmi/Player.h>
#include <vcmi/Team.h> #include <vcmi/Team.h>
#include "bonuses/Bonus.h" #include "GameCallbackHolder.h"
#include "bonuses/CBonusSystemNode.h"
#include "ResourceSet.h" #include "ResourceSet.h"
#include "TurnTimerInfo.h" #include "TurnTimerInfo.h"
#include "bonuses/Bonus.h"
#include "bonuses/CBonusSystemNode.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
@@ -25,7 +26,7 @@ class CGTownInstance;
class CGDwelling; class CGDwelling;
struct QuestInfo; struct QuestInfo;
class DLL_LINKAGE PlayerState : public CBonusSystemNode, public Player class DLL_LINKAGE PlayerState : public CBonusSystemNode, public Player, public GameCallbackHolder
{ {
struct VisitedObjectGlobal struct VisitedObjectGlobal
{ {
@@ -47,12 +48,7 @@ class DLL_LINKAGE PlayerState : public CBonusSystemNode, public Player
} }
}; };
std::vector<CGObjectInstance*> ownedObjects; std::vector<ObjectInstanceID> ownedObjects;
std::vector<const CGTownInstance*> constOwnedTowns; //not serialized
std::vector<const CGHeroInstance*> constOwnedHeroes; //not serialized
std::vector<CGTownInstance*> ownedTowns; //not serialized
std::vector<CGHeroInstance*> ownedHeroes; //not serialized
template<typename T> template<typename T>
std::vector<T> getObjectsOfType() const; std::vector<T> getObjectsOfType() const;
@@ -78,6 +74,7 @@ public:
std::optional<ui8> daysWithoutCastle; std::optional<ui8> daysWithoutCastle;
TurnTimerInfo turnTimer; TurnTimerInfo turnTimer;
PlayerState(IGameCallback *cb);
PlayerState(); PlayerState();
~PlayerState(); ~PlayerState();
@@ -97,16 +94,15 @@ public:
std::string getNameTextID() const override; std::string getNameTextID() const override;
void registerIcons(const IconRegistar & cb) const override; void registerIcons(const IconRegistar & cb) const override;
const std::vector<const CGHeroInstance* > & getHeroes() const; std::vector<const CGHeroInstance* > getHeroes() const;
const std::vector<const CGTownInstance* > & getTowns() const; std::vector<const CGTownInstance* > getTowns() const;
const std::vector<CGHeroInstance* > & getHeroes(); std::vector<CGHeroInstance* > getHeroes();
const std::vector<CGTownInstance* > & getTowns(); std::vector<CGTownInstance* > getTowns();
std::vector<const CGObjectInstance* > getOwnedObjects() const; std::vector<const CGObjectInstance* > getOwnedObjects() const;
void addOwnedObject(CGObjectInstance * object); void addOwnedObject(CGObjectInstance * object);
void removeOwnedObject(CGObjectInstance * object); void removeOwnedObject(CGObjectInstance * object);
void postDeserialize();
bool checkVanquished() const bool checkVanquished() const
{ {
@@ -135,9 +131,6 @@ public:
h & enteredWinningCheatCode; h & enteredWinningCheatCode;
h & static_cast<CBonusSystemNode&>(*this); h & static_cast<CBonusSystemNode&>(*this);
h & destroyedObjects; h & destroyedObjects;
if (!h.saving)
postDeserialize();
} }
}; };

View File

@@ -16,7 +16,7 @@ class IGameCallback;
class DLL_LINKAGE GameCallbackHolder class DLL_LINKAGE GameCallbackHolder
{ {
public: public:
IGameCallback * const cb; IGameCallback * cb;
explicit GameCallbackHolder(IGameCallback *cb): explicit GameCallbackHolder(IGameCallback *cb):
cb(cb) cb(cb)

View File

@@ -274,7 +274,7 @@ void CGameState::updateOnLoad(StartInfo * si)
assert(callback); assert(callback);
scenarioOps->playerInfos = si->playerInfos; scenarioOps->playerInfos = si->playerInfos;
for(auto & i : si->playerInfos) for(auto & i : si->playerInfos)
players[i.first].human = i.second.isControlledByHuman(); players.at(i.first).human = i.second.isControlledByHuman();
scenarioOps->extraOptionsInfo = si->extraOptionsInfo; scenarioOps->extraOptionsInfo = si->extraOptionsInfo;
scenarioOps->turnTimerInfo = si->turnTimerInfo; scenarioOps->turnTimerInfo = si->turnTimerInfo;
scenarioOps->simturnsInfo = si->simturnsInfo; scenarioOps->simturnsInfo = si->simturnsInfo;
@@ -514,7 +514,7 @@ void CGameState::initPlayerStates()
logGlobal->debug("\tCreating player entries in gs"); logGlobal->debug("\tCreating player entries in gs");
for(auto & elem : scenarioOps->playerInfos) for(auto & elem : scenarioOps->playerInfos)
{ {
PlayerState & p = players[elem.first]; PlayerState & p = players.at(elem.first);
p.color=elem.first; p.color=elem.first;
p.human = elem.second.isControlledByHuman(); p.human = elem.second.isControlledByHuman();
p.team = map->players[elem.first.getNum()].team; p.team = map->players[elem.first.getNum()].team;
@@ -1572,6 +1572,9 @@ void CGameState::buildBonusSystemTree()
void CGameState::deserializationFix() void CGameState::deserializationFix()
{ {
for(auto & player : players)
player.second.cb = callback;
buildGlobalTeamPlayerTree(); buildGlobalTeamPlayerTree();
attachArmedObjects(); attachArmedObjects();
} }

View File

@@ -523,7 +523,7 @@ void CGameStateCampaign::initHeroes()
} }
assert(humanPlayer != PlayerColor::NEUTRAL); assert(humanPlayer != PlayerColor::NEUTRAL);
const auto & heroes = gameState->players[humanPlayer].getHeroes(); const auto & heroes = gameState->players.at(humanPlayer).getHeroes();
if (chosenBonus->info1 == HeroTypeID::CAMP_STRONGEST) //most powerful if (chosenBonus->info1 == HeroTypeID::CAMP_STRONGEST) //most powerful
{ {
@@ -607,7 +607,7 @@ void CGameStateCampaign::initStartingResources()
//increasing resource quantity //increasing resource quantity
for (auto & re : res) for (auto & re : res)
{ {
gameState->players[ps->color].resources[re] += chosenBonus->info2; gameState->players.at(ps->color).resources[re] += chosenBonus->info2;
} }
} }
} }

View File

@@ -903,7 +903,7 @@ void SetCommanderProperty::applyGs(CGameState *gs)
void AddQuest::applyGs(CGameState *gs) void AddQuest::applyGs(CGameState *gs)
{ {
assert (vstd::contains(gs->players, player)); assert (vstd::contains(gs->players, player));
auto * vec = &gs->players[player].quests; auto * vec = &gs->players.at(player).quests;
if (!vstd::contains(*vec, quest)) if (!vstd::contains(*vec, quest))
vec->push_back (quest); vec->push_back (quest);
else else