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

vcmi: specialize native terrain entity

Specialize native terrain entity for all object that have
native terrain. Allow creatures to take global bonuses into
account when checking for native terrain.
This commit is contained in:
Konstantin
2023-04-05 18:56:28 +03:00
parent 6d9859932b
commit 0f5f4c69ec
14 changed files with 122 additions and 8 deletions

View File

@ -18,6 +18,9 @@
#include "../serializer/JsonDeserializer.h"
#include "../serializer/JsonSerializer.h"
#include <vcmi/Faction.h>
#include <vcmi/FactionService.h>
VCMI_LIB_NAMESPACE_BEGIN
namespace battle
@ -43,6 +46,24 @@ std::string Unit::getDescription() const
return fmt.str();
}
//TODO: deduplicate these functions
const IBonusBearer* Unit::getBonusBearer() const
{
return this;
}
TerrainId Unit::getNativeTerrain() const
{
const std::string cachingStringNoTerrainPenalty = "type_NO_TERRAIN_PENALTY_sANY";
static const auto selectorNoTerrainPenalty = Selector::typeSubtype(Bonus::NO_TERRAIN_PENALTY, static_cast<int>(ETerrainId::ANY_TERRAIN));
//this code is used in the CreatureTerrainLimiter::limit to setup battle bonuses
//and in the CGHeroInstance::getNativeTerrain() to setup movement bonuses or/and penalties.
return getBonusBearer()->hasBonus(selectorNoTerrainPenalty, cachingStringNoTerrainPenalty)
? TerrainId(ETerrainId::ANY_TERRAIN)
: VLC->factions()->getById(getFaction())->getNativeTerrain();
}
std::vector<BattleHex> Unit::getSurroundingHexes(BattleHex assumedPosition) const
{
BattleHex hex = (assumedPosition != BattleHex::INVALID) ? assumedPosition : getPosition(); //use hypothetical position