| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 | 
							- /*
 
-  * 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"
 
- #include "json/JsonNode.h"
 
- #include "mapObjects/CGDwelling.h"
 
- #include "mapObjects/CGTownInstance.h"
 
- #include "mapObjects/CGHeroInstance.h"
 
- #include "gameState/QuestInfo.h"
 
- #include "texts/CGeneralTextHandler.h"
 
- #include "VCMI_Lib.h"
 
- VCMI_LIB_NAMESPACE_BEGIN
 
- PlayerState::PlayerState()
 
- 	: color(-1)
 
- 	, human(false)
 
- 	, cheated(false)
 
- 	, playerLocalSettings(std::make_unique<JsonNode>())
 
- 	, enteredWinningCheatCode(false)
 
- 	, enteredLosingCheatCode(false)
 
- 	, status(EPlayerStatus::INGAME)
 
- {
 
- 	setNodeType(PLAYER);
 
- }
 
- PlayerState::~PlayerState() = default;
 
- std::string PlayerState::nodeName() const
 
- {
 
- 	return "Player " + color.toString();
 
- }
 
- PlayerColor PlayerState::getId() const
 
- {
 
- 	return color;
 
- }
 
- int32_t PlayerState::getIndex() const
 
- {
 
- 	return color.getNum();
 
- }
 
- int32_t PlayerState::getIconIndex() const 
 
- {
 
- 	return color.getNum();
 
- }
 
- std::string PlayerState::getJsonKey() const
 
- {
 
- 	return color.toString();
 
- }
 
- std::string PlayerState::getModScope() const
 
- {
 
- 	return "core";
 
- }
 
- std::string PlayerState::getNameTranslated() const
 
- {
 
- 	return VLC->generaltexth->translate(getNameTextID());
 
- }
 
- std::string PlayerState::getNameTextID() const
 
- {
 
- 	return TextIdentifier("core.plcolors", color.getNum()).get();
 
- }
 
- void PlayerState::registerIcons(const IconRegistar & cb) const
 
- {
 
- 	//We cannot register new icons for players
 
- }
 
- TeamID PlayerState::getTeam() const
 
- {
 
- 	return team;
 
- }
 
- bool PlayerState::isHuman() const
 
- {
 
- 	return human;
 
- }
 
- const IBonusBearer * PlayerState::getBonusBearer() const
 
- {
 
- 	return this;
 
- }
 
- int PlayerState::getResourceAmount(int type) const
 
- {
 
- 	return vstd::atOrDefault(resources, static_cast<size_t>(type), 0);
 
- }
 
- template<typename T>
 
- std::vector<T> PlayerState::getObjectsOfType() const
 
- {
 
- 	std::vector<T> result;
 
- 	for (auto const & object : ownedObjects)
 
- 	{
 
- 		auto casted = dynamic_cast<T>(object);
 
- 		if (casted)
 
- 			result.push_back(casted);
 
- 	}
 
- 	return result;
 
- }
 
- const std::vector<const CGHeroInstance *> & PlayerState::getHeroes() const
 
- {
 
- 	return constOwnedHeroes;
 
- }
 
- const std::vector<const CGTownInstance *> & PlayerState::getTowns() const
 
- {
 
- 	return constOwnedTowns;
 
- }
 
- const std::vector<CGHeroInstance *> & PlayerState::getHeroes()
 
- {
 
- 	return ownedHeroes;
 
- }
 
- const std::vector<CGTownInstance *> & PlayerState::getTowns()
 
- {
 
- 	return ownedTowns;
 
- }
 
- std::vector<const CGObjectInstance *> PlayerState::getOwnedObjects() const
 
- {
 
- 	return {ownedObjects.begin(), ownedObjects.end()};
 
- }
 
- void PlayerState::addOwnedObject(CGObjectInstance * object)
 
- {
 
- 	assert(object->asOwnable() != nullptr);
 
- 	ownedObjects.push_back(object);
 
- 	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)
 
- {
 
- 	vstd::erase(ownedObjects, object);
 
- 	vstd::erase(ownedTowns, object);
 
- 	vstd::erase(constOwnedTowns, object);
 
- 	vstd::erase(ownedHeroes, object);
 
- 	vstd::erase(constOwnedHeroes, object);
 
- }
 
- VCMI_LIB_NAMESPACE_END
 
 
  |