1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00

Add new general method for battle stack access

This commit is contained in:
AlexVinS 2014-05-17 13:37:12 +04:00
parent 0e3f59294c
commit 0e93ec28c5
2 changed files with 33 additions and 12 deletions

View File

@ -177,29 +177,36 @@ bool CBattleInfoEssentials::battleHasNativeStack(ui8 side) const
return false; return false;
} }
TStacks CBattleInfoEssentials::battleGetAllStacks(bool includeTurrets /*= false*/) const /*returns all stacks, alive or dead or undead or mechanical :) */ TStacks CBattleInfoEssentials::battleGetAllStacks(bool includeTurrets /*= false*/) const
{
return battleGetStacksIf([](const CStack * s){return true;},includeTurrets);
}
TStacks CBattleInfoEssentials::battleGetStacksIf(TStackFilter predicate, bool includeTurrets /*= false*/) const
{ {
TStacks ret; TStacks ret;
RETURN_IF_NOT_BATTLE(ret); RETURN_IF_NOT_BATTLE(ret);
boost::copy(getBattle()->stacks, std::back_inserter(ret));
if(!includeTurrets) vstd::copy_if(getBattle()->stacks, std::back_inserter(ret), [=](const CStack * s){
vstd::erase_if(ret, [](const CStack *stack) { return stack->type->idNumber == CreatureID::ARROW_TOWERS; }); return predicate(s) && (includeTurrets || !(s->type->idNumber == CreatureID::ARROW_TOWERS));
});
return ret; return ret;
} }
TStacks CBattleInfoEssentials::battleAliveStacks() const TStacks CBattleInfoEssentials::battleAliveStacks() const
{ {
TStacks ret; return battleGetStacksIf([](const CStack * s){
vstd::copy_if(battleGetAllStacks(), std::back_inserter(ret), [](const CStack *s){ return s->alive(); }); return s->alive();
return ret; });
} }
TStacks CBattleInfoEssentials::battleAliveStacks(ui8 side) const TStacks CBattleInfoEssentials::battleAliveStacks(ui8 side) const
{ {
TStacks ret; return battleGetStacksIf([=](const CStack * s){
vstd::copy_if(battleGetAllStacks(), std::back_inserter(ret), [=](const CStack *s){ return s->alive() && s->attackerOwned == !side; }); return s->alive() && s->attackerOwned == !side;
return ret; });
} }
int CBattleInfoEssentials::battleGetMoatDmg() const int CBattleInfoEssentials::battleGetMoatDmg() const

View File

@ -31,6 +31,7 @@ namespace BattleSide
} }
typedef std::vector<const CStack*> TStacks; typedef std::vector<const CStack*> TStacks;
typedef std::function<bool(const CStack *)> TStackFilter;
class CBattleInfoEssentials; class CBattleInfoEssentials;
@ -168,7 +169,15 @@ public:
ETerrainType battleTerrainType() const; ETerrainType battleTerrainType() const;
BFieldType battleGetBattlefieldType() const; BFieldType battleGetBattlefieldType() const;
std::vector<shared_ptr<const CObstacleInstance> > battleGetAllObstacles(boost::optional<BattlePerspective::BattlePerspective> perspective = boost::none) const; //returns all obstacles on the battlefield std::vector<shared_ptr<const CObstacleInstance> > battleGetAllObstacles(boost::optional<BattlePerspective::BattlePerspective> perspective = boost::none) const; //returns all obstacles on the battlefield
TStacks battleGetAllStacks(bool includeTurrets = false) const; //returns all stacks, alive or dead or undead or mechanical :)
/** @brief Main method for getting battle stacks
*
* @param predicate Functor that shall return true for desired stack
* @return filtered stacks
*
*/
TStacks battleGetStacksIf(TStackFilter predicate, bool includeTurrets = false) const;
bool battleHasNativeStack(ui8 side) const; bool battleHasNativeStack(ui8 side) const;
int battleGetMoatDmg() const; //what dmg unit will suffer if ending turn in the moat int battleGetMoatDmg() const; //what dmg unit will suffer if ending turn in the moat
const CGTownInstance * battleGetDefendedTown() const; //returns defended town if current battle is a siege, nullptr instead const CGTownInstance * battleGetDefendedTown() const; //returns defended town if current battle is a siege, nullptr instead
@ -190,7 +199,12 @@ public:
si8 battleGetWallState(int partOfWall) const; si8 battleGetWallState(int partOfWall) const;
//helpers //helpers
///returns all stacks, alive or dead or undead or mechanical :)
TStacks battleGetAllStacks(bool includeTurrets = false) const;
///returns all alive stacks excluding turrets
TStacks battleAliveStacks() const; TStacks battleAliveStacks() const;
///returns all alive stacks from particular side excluding turrets
TStacks battleAliveStacks(ui8 side) const; TStacks battleAliveStacks(ui8 side) const;
const CStack * battleGetStackByID(int ID, bool onlyAlive = true) const; //returns stack info by given ID const CStack * battleGetStackByID(int ID, bool onlyAlive = true) const; //returns stack info by given ID
bool battleIsObstacleVisibleForSide(const CObstacleInstance & coi, BattlePerspective::BattlePerspective side) const; bool battleIsObstacleVisibleForSide(const CObstacleInstance & coi, BattlePerspective::BattlePerspective side) const;