1
0
mirror of https://github.com/vcmi/vcmi.git synced 2026-05-16 09:28:24 +02:00
Files

168 lines
3.5 KiB
C++
Raw Permalink Normal View History

2018-03-17 17:58:30 +03:00
/*
* CPlayerState.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CPlayerState.h"
2025-03-26 15:24:34 +00:00
#include "GameLibrary.h"
#include "callback/IGameInfoCallback.h"
#include "mapObjects/CGHeroInstance.h"
2025-03-26 15:24:34 +00:00
#include "mapObjects/CGTownInstance.h"
#include "gameState/CGameState.h"
#include "gameState/QuestInfo.h"
#include "texts/CGeneralTextHandler.h"
2025-03-26 15:24:34 +00:00
#include "json/JsonNode.h"
2018-03-17 17:58:30 +03:00
VCMI_LIB_NAMESPACE_BEGIN
PlayerState::PlayerState(IGameInfoCallback *cb)
: CBonusSystemNode(BonusNodeType::PLAYER)
2025-03-26 15:24:34 +00:00
, GameCallbackHolder(cb)
, color(-1)
, human(false)
, cheated(false)
, playerLocalSettings(std::make_unique<JsonNode>())
, enteredWinningCheatCode(false)
, enteredLosingCheatCode(false)
, status(EPlayerStatus::INGAME)
2018-03-17 17:58:30 +03:00
{
}
2023-06-26 17:25:29 +03:00
PlayerState::~PlayerState() = default;
2018-03-17 17:58:30 +03:00
std::string PlayerState::nodeName() const
{
return "Player " + color.toString();
2018-03-17 17:58:30 +03:00
}
2023-04-04 17:52:33 +03:00
PlayerColor PlayerState::getId() const
2018-03-17 17:58:30 +03:00
{
return color;
}
2023-04-04 17:52:33 +03:00
int32_t PlayerState::getIndex() const
{
return color.getNum();
}
int32_t PlayerState::getIconIndex() const
{
return color.getNum();
}
2023-04-04 17:52:33 +03:00
std::string PlayerState::getJsonKey() const
{
return color.toString();
2023-04-04 17:52:33 +03:00
}
std::string PlayerState::getModScope() const
{
return "core";
}
2023-04-04 17:52:33 +03:00
std::string PlayerState::getNameTranslated() const
{
return LIBRARY->generaltexth->translate(getNameTextID());
2023-04-04 17:52:33 +03:00
}
2023-04-04 17:52:33 +03:00
std::string PlayerState::getNameTextID() const
{
return TextIdentifier("core.plcolors", color.getNum()).get();
2023-04-04 17:52:33 +03:00
}
2023-04-04 17:52:33 +03:00
void PlayerState::registerIcons(const IconRegistar & cb) const
{
//We cannot register new icons for players
}
2018-03-17 17:58:30 +03:00
TeamID PlayerState::getTeam() const
{
return team;
}
bool PlayerState::isHuman() const
{
return human;
}
2023-04-05 03:26:29 +03:00
const IBonusBearer * PlayerState::getBonusBearer() const
2018-03-17 17:58:30 +03:00
{
return this;
}
int PlayerState::getResourceAmount(int type) const
{
2025-09-15 23:48:55 +02:00
return resources[type];
2018-03-17 17:58:30 +03:00
}
template<typename T>
std::vector<T> PlayerState::getObjectsOfType() const
{
std::vector<T> result;
2025-03-26 15:24:34 +00:00
for (const ObjectInstanceID & objectID : ownedObjects)
{
auto objectPtr = cb->gameState().getObjInstance(objectID);
2025-03-26 15:24:34 +00:00
auto casted = dynamic_cast<T>(objectPtr);
if (casted)
result.push_back(casted);
}
return result;
}
2025-03-26 15:24:34 +00:00
std::vector<const CGHeroInstance *> PlayerState::getHeroes() const
{
2025-03-26 15:24:34 +00:00
return getObjectsOfType<const CGHeroInstance *>();
}
2025-03-26 15:24:34 +00:00
std::vector<const CGTownInstance *> PlayerState::getTowns() const
{
// optimized due to numerous AI access
using T = const CGTownInstance *;
std::vector<T> result;
for (const ObjectInstanceID & objectID : ownedObjects)
{
const auto * objectPtr = cb->gameState().getObjInstance(objectID);
if (objectPtr->ID != MapObjectID::TOWN)
continue;
assert(dynamic_cast<T>(objectPtr) != nullptr);
auto casted = static_cast<T>(objectPtr);
result.push_back(casted);
}
return result;
}
2025-03-26 15:24:34 +00:00
std::vector<CGHeroInstance *> PlayerState::getHeroes()
{
2025-03-26 15:24:34 +00:00
return getObjectsOfType<CGHeroInstance *>();
}
2025-03-26 15:24:34 +00:00
std::vector<CGTownInstance *> PlayerState::getTowns()
{
2025-03-26 15:24:34 +00:00
return getObjectsOfType<CGTownInstance *>();
}
std::vector<const CGObjectInstance *> PlayerState::getOwnedObjects() const
{
2025-03-26 15:24:34 +00:00
return getObjectsOfType<const CGObjectInstance *>();
}
void PlayerState::addOwnedObject(CGObjectInstance * object)
{
assert(object->asOwnable() != nullptr);
2025-03-26 15:24:34 +00:00
ownedObjects.push_back(object->id);
}
void PlayerState::removeOwnedObject(CGObjectInstance * object)
{
2025-03-26 15:24:34 +00:00
vstd::erase(ownedObjects, object->id);
}
VCMI_LIB_NAMESPACE_END