1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-05 00:49:09 +02:00

Use small vector for unit list

This commit is contained in:
Ivan Savenko
2025-01-10 18:58:46 +00:00
parent 797b62fd46
commit 40bff74195
26 changed files with 57 additions and 52 deletions

View File

@ -92,8 +92,8 @@ void DamageCache::buildDamageCache(std::shared_ptr<HypotheticBattle> hb, BattleS
return u->isValidTarget();
});
std::vector<const battle::Unit *> ourUnits;
std::vector<const battle::Unit *> enemyUnits;
battle::Units ourUnits;
battle::Units enemyUnits;
for(auto stack : stacks)
{
@ -346,9 +346,9 @@ AttackPossibility AttackPossibility::evaluate(
if (!attackInfo.shooting)
ap.attackerState->setPosition(hex);
std::vector<const battle::Unit *> defenderUnits;
std::vector<const battle::Unit *> retaliatedUnits = {attacker};
std::vector<const battle::Unit *> affectedUnits;
battle::Units defenderUnits;
battle::Units retaliatedUnits = {attacker};
battle::Units affectedUnits;
if (attackInfo.shooting)
defenderUnits = state->getAttackedBattleUnits(attacker, defender, defHex, true, hex, defender->getPosition());

View File

@ -471,10 +471,10 @@ MoveTarget BattleExchangeEvaluator::findMoveTowardsUnreachable(
return result;
}
std::vector<const battle::Unit *> BattleExchangeEvaluator::getAdjacentUnits(const battle::Unit * blockerUnit) const
battle::Units BattleExchangeEvaluator::getAdjacentUnits(const battle::Unit * blockerUnit) const
{
std::queue<const battle::Unit *> queue;
std::vector<const battle::Unit *> checkedStacks;
battle::Units checkedStacks;
queue.push(blockerUnit);
@ -506,7 +506,7 @@ ReachabilityData BattleExchangeEvaluator::getExchangeUnits(
uint8_t turn,
PotentialTargets & targets,
std::shared_ptr<HypotheticBattle> hb,
std::vector<const battle::Unit *> additionalUnits) const
battle::Units additionalUnits) const
{
ReachabilityData result;
@ -515,7 +515,7 @@ ReachabilityData BattleExchangeEvaluator::getExchangeUnits(
if(!ap.attack.shooting)
hexes.insert(ap.from);
std::vector<const battle::Unit *> allReachableUnits = additionalUnits;
battle::Units allReachableUnits = additionalUnits;
for(auto hex : hexes)
{
@ -636,7 +636,7 @@ BattleScore BattleExchangeEvaluator::calculateExchange(
PotentialTargets & targets,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb,
std::vector<const battle::Unit *> additionalUnits) const
battle::Units additionalUnits) const
{
#if BATTLE_TRACE_LEVEL>=1
logAi->trace("Battle exchange at %d", ap.attack.shooting ? ap.dest.hex : ap.from.hex);
@ -649,8 +649,8 @@ BattleScore BattleExchangeEvaluator::calculateExchange(
return BattleScore(EvaluationResult::INEFFECTIVE_SCORE, 0);
}
std::vector<const battle::Unit *> ourStacks;
std::vector<const battle::Unit *> enemyStacks;
battle::Units ourStacks;
battle::Units enemyStacks;
if(hb->battleGetUnitByID(ap.attack.defender->unitId())->alive())
enemyStacks.push_back(ap.attack.defender);

View File

@ -112,13 +112,13 @@ private:
struct ReachabilityData
{
std::map<int, std::vector<const battle::Unit *>> units;
std::map<int, battle::Units> units;
// shooters which are within mellee attack and mellee units
std::vector<const battle::Unit *> melleeAccessible;
battle::Units melleeAccessible;
// far shooters
std::vector<const battle::Unit *> shooters;
battle::Units shooters;
std::set<uint32_t> enemyUnitsReachingAttacker;
};
@ -158,7 +158,7 @@ private:
PotentialTargets & targets,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb,
std::vector<const battle::Unit *> additionalUnits = {}) const;
battle::Units additionalUnits = {}) const;
bool canBeHitThisTurn(const AttackPossibility & ap);
@ -193,7 +193,7 @@ public:
uint8_t turn,
PotentialTargets & targets,
std::shared_ptr<HypotheticBattle> hb,
std::vector<const battle::Unit *> additionalUnits = {}) const;
battle::Units additionalUnits = {}) const;
bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
@ -203,7 +203,7 @@ public:
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb);
std::vector<const battle::Unit *> getAdjacentUnits(const battle::Unit * unit) const;
battle::Units getAdjacentUnits(const battle::Unit * unit) const;
float getPositiveEffectMultiplier() const { return 1; }
float getNegativeEffectMultiplier() const { return negativeEffectMultiplier; }

View File

@ -14,7 +14,7 @@ class PotentialTargets
{
public:
std::vector<AttackPossibility> possibleAttacks;
std::vector<const battle::Unit *> unreachableEnemies;
battle::Units unreachableEnemies;
PotentialTargets(){};
PotentialTargets(

View File

@ -670,15 +670,15 @@ namespace vstd
return false;
}
template<typename T>
void removeDuplicates(std::vector<T> &vec)
template <typename Container>
void removeDuplicates(Container &vec)
{
std::sort(vec.begin(), vec.end());
vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
}
template <typename T>
void concatenate(std::vector<T> &dest, const std::vector<T> &src)
template <typename Container>
void concatenate(Container &dest, const Container &src)
{
dest.reserve(dest.size() + src.size());
dest.insert(dest.end(), src.begin(), src.end());

View File

@ -22,6 +22,7 @@ class SpellSchool;
namespace battle
{
class Unit;
using Units = boost::container::small_vector<const Unit *, 4>;
}
namespace spells
@ -65,7 +66,7 @@ public:
virtual void getCasterName(MetaString & text) const = 0;
///full default text
virtual void getCastDescription(const Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const = 0;
virtual void getCastDescription(const Spell * spell, const battle::Units & attacked, MetaString & text) const = 0;
virtual void spendMana(ServerCallback * server, const int32_t spellCost) const = 0;

View File

@ -27,7 +27,7 @@ BattleStateInfoForRetreat::BattleStateInfoForRetreat():
{
}
uint64_t getFightingStrength(const std::vector<const battle::Unit *> & stacks, const CGHeroInstance * hero = nullptr)
uint64_t getFightingStrength(const battle::Units & stacks, const CGHeroInstance * hero = nullptr)
{
uint64_t result = 0;

View File

@ -16,6 +16,7 @@ VCMI_LIB_NAMESPACE_BEGIN
namespace battle
{
class Unit;
using Units = boost::container::small_vector<const Unit *, 4>;
}
class CGHeroInstance;
@ -27,8 +28,8 @@ public:
bool canSurrender;
bool isLastTurnBeforeDie;
BattleSide ourSide;
std::vector<const battle::Unit *> ourStacks;
std::vector<const battle::Unit *> enemyStacks;
battle::Units ourStacks;
battle::Units enemyStacks;
const CGHeroInstance * ourHero;
const CGHeroInstance * enemyHero;
int turnsSkippedByDefense;

View File

@ -383,11 +383,9 @@ battle::Units CBattleInfoCallback::battleAliveUnits(BattleSide side) const
using namespace battle;
//T is battle::Unit descendant
template <typename T>
const T * takeOneUnit(std::vector<const T*> & allUnits, const int turn, BattleSide & sideThatLastMoved, int phase)
static const battle::Unit * takeOneUnit(battle::Units & allUnits, const int turn, BattleSide & sideThatLastMoved, int phase)
{
const T * returnedUnit = nullptr;
const battle::Unit * returnedUnit = nullptr;
size_t currentUnitIndex = 0;
for(size_t i = 0; i < allUnits.size(); i++)
@ -1168,7 +1166,7 @@ std::pair<const battle::Unit *, BattleHex> CBattleInfoCallback::getNearestStack(
std::vector<DistStack> stackPairs;
std::vector<const battle::Unit *> possible = battleGetUnitsIf([=](const battle::Unit * unit)
battle::Units possible = battleGetUnitsIf([=](const battle::Unit * unit)
{
return unit->isValidTarget(false) && unit != closest;
});
@ -1436,7 +1434,7 @@ AttackableTiles CBattleInfoCallback::getPotentiallyShootableHexes(const battle::
return at;
}
std::vector<const battle::Unit*> CBattleInfoCallback::getAttackedBattleUnits(
battle::Units CBattleInfoCallback::getAttackedBattleUnits(
const battle::Unit * attacker,
const battle::Unit * defender,
BattleHex destinationTile,
@ -1444,7 +1442,7 @@ std::vector<const battle::Unit*> CBattleInfoCallback::getAttackedBattleUnits(
BattleHex attackerPos,
BattleHex defenderPos) const
{
std::vector<const battle::Unit*> units;
battle::Units units;
RETURN_IF_NOT_BATTLE(units);
if(attackerPos == BattleHex::INVALID)

View File

@ -147,7 +147,7 @@ public:
AttackableTiles getPotentiallyShootableHexes(const battle::Unit* attacker, BattleHex destinationTile, BattleHex attackerPos) const;
std::vector<const battle::Unit *> getAttackedBattleUnits(
battle::Units getAttackedBattleUnits(
const battle::Unit* attacker,
const battle::Unit * defender,
BattleHex destinationTile,

View File

@ -464,7 +464,7 @@ void CUnitState::getCasterName(MetaString & text) const
addNameReplacement(text, true);
}
void CUnitState::getCastDescription(const spells::Spell * spell, const std::vector<const Unit *> & attacked, MetaString & text) const
void CUnitState::getCastDescription(const spells::Spell * spell, const battle::Units & attacked, MetaString & text) const
{
text.appendLocalString(EMetaText::GENERAL_TXT, 565);//The %s casts %s
//todo: use text 566 for single creature

View File

@ -183,7 +183,7 @@ public:
PlayerColor getCasterOwner() const override;
const CGHeroInstance * getHeroCaster() const override;
void getCasterName(MetaString & text) const override;
void getCastDescription(const spells::Spell * spell, const std::vector<const Unit *> & attacked, MetaString & text) const override;
void getCastDescription(const spells::Spell * spell, const battle::Units & attacked, MetaString & text) const override;
int32_t manaLimit() const override;
bool ableToRetaliate() const override;

View File

@ -27,7 +27,7 @@ namespace battle
{
class IUnitInfo;
class Unit;
using Units = std::vector<const Unit *>;
using Units = boost::container::small_vector<const Unit *, 4>;
using UnitFilter = std::function<bool(const Unit *)>;
}

View File

@ -856,7 +856,7 @@ void CGHeroInstance::getCasterName(MetaString & text) const
text.replaceRawString(getNameTranslated());
}
void CGHeroInstance::getCastDescription(const spells::Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const
void CGHeroInstance::getCastDescription(const spells::Spell * spell, const battle::Units & attacked, MetaString & text) const
{
const bool singleTarget = attacked.size() == 1;
const int textIndex = singleTarget ? 195 : 196;

View File

@ -309,7 +309,7 @@ public:
const CGHeroInstance * getHeroCaster() const override;
void getCasterName(MetaString & text) const override;
void getCastDescription(const spells::Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const override;
void getCastDescription(const spells::Spell * spell, const battle::Units & attacked, MetaString & text) const override;
void spendMana(ServerCallback * server, const int spellCost) const override;
void attachToBoat(CGBoat* newBoat);

View File

@ -49,7 +49,7 @@ int32_t AbilityCaster::getEffectLevel(const Spell * spell) const
return getSpellSchoolLevel(spell);
}
void AbilityCaster::getCastDescription(const Spell * spell, const std::vector<const battle::Unit*> & attacked, MetaString & text) const
void AbilityCaster::getCastDescription(const Spell * spell, const battle::Units & attacked, MetaString & text) const
{
//do nothing
}

View File

@ -25,7 +25,7 @@ public:
int32_t getSpellSchoolLevel(const Spell * spell, SpellSchool * outSelectedSchool = nullptr) const override;
int32_t getEffectLevel(const Spell * spell) const override;
void getCastDescription(const Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const override;
void getCastDescription(const Spell * spell, const battle::Units & attacked, MetaString & text) const override;
void spendMana(ServerCallback * server, const int32_t spellCost) const override;
private:

View File

@ -473,7 +473,7 @@ std::set<const battle::Unit *> BattleSpellMechanics::collectTargets() const
return result;
}
void BattleSpellMechanics::doRemoveEffects(ServerCallback * server, const std::vector<const battle::Unit *> & targets, const CSelector & selector)
void BattleSpellMechanics::doRemoveEffects(ServerCallback * server, const battle::Units & targets, const CSelector & selector)
{
SetStackEffect sse;
sse.battleID = battle()->getBattle()->getBattleID();

View File

@ -18,6 +18,11 @@ VCMI_LIB_NAMESPACE_BEGIN
struct BattleSpellCast;
namespace battle
{
using Units = boost::container::small_vector<const Unit *, 4>;
}
namespace spells
{
@ -66,14 +71,14 @@ private:
std::shared_ptr<effects::Effects> effects;
std::shared_ptr<IReceptiveCheck> targetCondition;
std::vector<const battle::Unit *> affectedUnits;
battle::Units affectedUnits;
effects::Effects::EffectsToApply effectsToApply;
void beforeCast(BattleSpellCast & sc, vstd::RNG & rng, const Target & target);
std::set<const battle::Unit *> collectTargets() const;
void doRemoveEffects(ServerCallback * server, const std::vector<const battle::Unit *> & targets, const CSelector & selector);
void doRemoveEffects(ServerCallback * server, const battle::Units & targets, const CSelector & selector);
BattleHexArray spellRangeInHexes(BattleHex centralHex) const;

View File

@ -57,7 +57,7 @@ void BonusCaster::getCasterName(MetaString & text) const
}
}
void BonusCaster::getCastDescription(const Spell * spell, const std::vector<const battle::Unit*> & attacked, MetaString & text) const
void BonusCaster::getCastDescription(const Spell * spell, const battle::Units & attacked, MetaString & text) const
{
const bool singleTarget = attacked.size() == 1;
const int textIndex = singleTarget ? 195 : 196;

View File

@ -26,7 +26,7 @@ public:
virtual ~BonusCaster();
void getCasterName(MetaString & text) const override;
void getCastDescription(const Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const override;
void getCastDescription(const Spell * spell, const battle::Units & attacked, MetaString & text) const override;
void spendMana(ServerCallback * server, const int spellCost) const override;
private:

View File

@ -75,7 +75,7 @@ void SilentCaster::getCasterName(MetaString & text) const
logGlobal->debug("Unexpected call to SilentCaster::getCasterName");
}
void SilentCaster::getCastDescription(const Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const
void SilentCaster::getCastDescription(const Spell * spell, const battle::Units & attacked, MetaString & text) const
{
//do nothing
}

View File

@ -25,7 +25,7 @@ public:
SilentCaster(PlayerColor owner_, const Caster * caster);
void getCasterName(MetaString & text) const override;
void getCastDescription(const Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const override;
void getCastDescription(const Spell * spell, const battle::Units & attacked, MetaString & text) const override;
void spendMana(ServerCallback * server, const int spellCost) const override;
PlayerColor getCasterOwner() const override;
int32_t manaLimit() const override;

View File

@ -106,7 +106,7 @@ void ProxyCaster::getCasterName(MetaString & text) const
actualCaster->getCasterName(text);
}
void ProxyCaster::getCastDescription(const Spell * spell, const std::vector<const battle::Unit*> & attacked, MetaString & text) const
void ProxyCaster::getCastDescription(const Spell * spell, const battle::Units & attacked, MetaString & text) const
{
if(actualCaster)
actualCaster->getCastDescription(spell, attacked, text);

View File

@ -33,7 +33,7 @@ public:
int64_t getEffectValue(const Spell * spell) const override;
PlayerColor getCasterOwner() const override;
void getCasterName(MetaString & text) const override;
void getCastDescription(const Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const override;
void getCastDescription(const Spell * spell, const battle::Units & attacked, MetaString & text) const override;
void spendMana(ServerCallback * server, const int32_t spellCost) const override;
const CGHeroInstance * getHeroCaster() const override;
int32_t manaLimit() const override;

View File

@ -28,7 +28,7 @@ public:
MOCK_CONST_METHOD1(getEffectValue, int64_t(const spells::Spell *));
MOCK_CONST_METHOD0(getCasterOwner, PlayerColor());
MOCK_CONST_METHOD1(getCasterName, void(MetaString &));
MOCK_CONST_METHOD3(getCastDescription, void(const spells::Spell *, const std::vector<const battle::Unit *> &, MetaString &));
MOCK_CONST_METHOD3(getCastDescription, void(const spells::Spell *, const battle::Units &, MetaString &));
MOCK_CONST_METHOD2(spendMana, void(ServerCallback *, const int32_t));
MOCK_CONST_METHOD0(manaLimit, int32_t());
MOCK_CONST_METHOD0(getHeroCaster, CGHeroInstance*());