2012-09-14 02:42:11 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "../../lib/AI_Base.h"
|
|
|
|
#include "BattleAI.h"
|
|
|
|
#include "../../lib/BattleState.h"
|
|
|
|
#include "../../CCallback.h"
|
|
|
|
#include "../../lib/CCreatureHandler.h"
|
2012-09-20 19:55:21 +03:00
|
|
|
#include "../../lib/CSpellHandler.h"
|
|
|
|
#include "../../lib/VCMI_Lib.h"
|
2012-09-14 02:42:11 +03:00
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
using boost::optional;
|
2013-06-22 17:47:20 +03:00
|
|
|
shared_ptr<CBattleCallback> cbc;
|
2012-09-20 19:55:21 +03:00
|
|
|
|
|
|
|
#define LOGL(text) print(text)
|
|
|
|
#define LOGFL(text, formattingEl) print(boost::str(boost::format(text) % formattingEl))
|
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
struct Priorities
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
double manaValue;
|
|
|
|
double generalResourceValueModifier;
|
|
|
|
std::vector<double> resourceTypeBaseValues;
|
|
|
|
std::function<double(const CStack *)> stackEvaluator;
|
2012-09-20 19:55:21 +03:00
|
|
|
|
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
Priorities()
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
manaValue = 0.;
|
|
|
|
generalResourceValueModifier = 1.;
|
|
|
|
range::copy(VLC->objh->resVals, std::back_inserter(resourceTypeBaseValues));
|
|
|
|
stackEvaluator = [](const CStack*){ return 1.0; };
|
2013-01-20 15:06:49 +03:00
|
|
|
}
|
2013-06-21 23:59:32 +03:00
|
|
|
} priorities;
|
2012-09-29 17:44:06 +03:00
|
|
|
|
2012-09-14 02:42:11 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
int distToNearestNeighbour(BattleHex hex, const ReachabilityInfo::TDistances& dists, BattleHex *chosenHex = nullptr)
|
2012-09-14 02:42:11 +03:00
|
|
|
{
|
|
|
|
int ret = 1000000;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(BattleHex n : hex.neighbouringTiles())
|
2012-09-14 02:42:11 +03:00
|
|
|
{
|
|
|
|
if(dists[n] >= 0 && dists[n] < ret)
|
|
|
|
{
|
|
|
|
ret = dists[n];
|
|
|
|
if(chosenHex)
|
|
|
|
*chosenHex = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isCloser(const EnemyInfo & ei1, const EnemyInfo & ei2, const ReachabilityInfo::TDistances & dists)
|
|
|
|
{
|
|
|
|
return distToNearestNeighbour(ei1.s->position, dists) < distToNearestNeighbour(ei2.s->position, dists);
|
|
|
|
}
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
template <typename Container, typename Pred>
|
2013-06-29 16:05:48 +03:00
|
|
|
auto sum(const Container & c, Pred p) -> decltype(p(*std::begin(c)))
|
2012-09-14 02:42:11 +03:00
|
|
|
{
|
2012-09-20 19:55:21 +03:00
|
|
|
double ret = 0;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const auto &element : c)
|
2012-09-14 02:42:11 +03:00
|
|
|
{
|
2012-09-20 19:55:21 +03:00
|
|
|
ret += p(element);
|
2012-09-14 02:42:11 +03:00
|
|
|
}
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-14 02:42:11 +03:00
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
CBattleAI::CBattleAI(void)
|
2013-06-23 22:35:54 +03:00
|
|
|
: side(-1)
|
2012-09-24 02:10:56 +03:00
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
print("created");
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
CBattleAI::~CBattleAI(void)
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
print("destroyed");
|
2012-09-24 02:10:56 +03:00
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
if(cb)
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
//Restore previous state of CB - it may be shared with the main AI (like VCAI)
|
|
|
|
cb->waitTillRealize = wasWaitingForRealize;
|
|
|
|
cb->unlockGsWhenWaiting = wasUnlockingGs;
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
2012-09-20 19:55:21 +03:00
|
|
|
|
2013-06-22 17:47:20 +03:00
|
|
|
void CBattleAI::init(shared_ptr<CBattleCallback> CB)
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
|
|
|
print("init called, saving ptr to IBattleCallback");
|
|
|
|
cbc = cb = CB;
|
|
|
|
playerID = *CB->getPlayerID();; //TODO should be sth in callback
|
2012-09-29 17:44:06 +03:00
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
wasWaitingForRealize = cb->waitTillRealize;
|
|
|
|
wasUnlockingGs = CB->unlockGsWhenWaiting;
|
|
|
|
CB->waitTillRealize = true;
|
|
|
|
CB->unlockGsWhenWaiting = false;
|
|
|
|
}
|
2012-09-29 17:44:06 +03:00
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
static bool thereRemainsEnemy()
|
|
|
|
{
|
2013-09-30 00:15:02 +03:00
|
|
|
return !cbc->battleIsFinished();
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
2012-09-20 19:55:21 +03:00
|
|
|
|
|
|
|
BattleAction CBattleAI::activeStack( const CStack * stack )
|
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
LOG_TRACE_PARAMS(logAi, "stack: %s", stack->nodeName()) ;
|
|
|
|
|
2013-01-22 00:34:30 +03:00
|
|
|
cbc = cb; //TODO: make solid sure that AIs always use their callbacks (need to take care of event handlers too)
|
2012-09-20 19:55:21 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
print("activeStack called for " + stack->nodeName());
|
2013-02-07 20:34:50 +03:00
|
|
|
if(stack->type->idNumber == CreatureID::CATAPULT)
|
2012-09-20 19:55:21 +03:00
|
|
|
return useCatapult(stack);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2013-08-25 16:03:29 +03:00
|
|
|
if(stack->hasBonusOfType(Bonus::SIEGE_WEAPON) && stack->hasBonusOfType(Bonus::HEALER))
|
|
|
|
{
|
|
|
|
auto healingTargets = cb->battleGetStacks(CBattleInfoEssentials::ONLY_MINE);
|
|
|
|
std::map<int, const CStack*> woundHpToStack;
|
|
|
|
for(auto stack : healingTargets)
|
|
|
|
if(auto woundHp = stack->MaxHealth() - stack->firstHPleft)
|
|
|
|
woundHpToStack[woundHp] = stack;
|
|
|
|
|
2013-11-07 15:48:41 +03:00
|
|
|
if(woundHpToStack.empty())
|
2013-08-25 16:03:29 +03:00
|
|
|
return BattleAction::makeDefend(stack);
|
2013-11-07 15:48:41 +03:00
|
|
|
else
|
|
|
|
return BattleAction::makeHeal(stack, woundHpToStack.rbegin()->second); //last element of the woundHpToStack is the most wounded stack
|
2013-08-25 16:03:29 +03:00
|
|
|
}
|
2012-09-20 19:55:21 +03:00
|
|
|
|
|
|
|
if(cb->battleCanCastSpell())
|
|
|
|
attemptCastingSpell();
|
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
if(!thereRemainsEnemy())
|
|
|
|
return BattleAction();
|
|
|
|
|
2013-06-17 18:45:55 +03:00
|
|
|
if(auto action = considerFleeingOrSurrendering())
|
|
|
|
return *action;
|
|
|
|
|
2012-09-29 17:44:06 +03:00
|
|
|
if(cb->battleGetStacks(CBattleInfoEssentials::ONLY_ENEMY).empty())
|
|
|
|
{
|
|
|
|
//We apparently won battle by casting spell, return defend... (accessing cb may cause trouble)
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
PotentialTargets targets(stack);
|
2013-06-21 23:59:32 +03:00
|
|
|
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
if(targets.possibleAttacks.size())
|
2012-09-14 02:42:11 +03:00
|
|
|
{
|
2012-09-20 19:55:21 +03:00
|
|
|
auto hlp = targets.bestAction();
|
|
|
|
if(hlp.attack.shooting)
|
|
|
|
return BattleAction::makeShotAttack(stack, hlp.enemy);
|
|
|
|
else
|
|
|
|
return BattleAction::makeMeleeAttack(stack, hlp.enemy, hlp.tile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(stack->waited())
|
|
|
|
{
|
2013-06-17 18:45:55 +03:00
|
|
|
ThreatMap threatsToUs(stack);
|
2012-09-20 19:55:21 +03:00
|
|
|
auto dists = cbc->battleGetDistances(stack);
|
2013-07-02 18:23:32 +03:00
|
|
|
const EnemyInfo &ei= *range::min_element(targets.unreachableEnemies, boost::bind(isCloser, _1, _2, boost::ref(dists)));
|
2012-09-20 19:55:21 +03:00
|
|
|
if(distToNearestNeighbour(ei.s->position, dists) < GameConstants::BFIELD_SIZE)
|
|
|
|
{
|
|
|
|
return goTowards(stack, ei.s->position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return BattleAction::makeWait(stack);
|
|
|
|
}
|
2012-09-14 02:42:11 +03:00
|
|
|
}
|
|
|
|
}
|
2012-09-20 19:55:21 +03:00
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
2013-06-21 23:59:32 +03:00
|
|
|
logAi->errorStream() << "Exception occurred in " << __FUNCTION__ << " " << e.what();
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
2012-09-14 02:42:11 +03:00
|
|
|
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
void CBattleAI::actionFinished(const BattleAction &action)
|
|
|
|
{
|
|
|
|
print("actionFinished called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::actionStarted(const BattleAction &action)
|
|
|
|
{
|
|
|
|
print("actionStarted called");
|
|
|
|
}
|
|
|
|
|
2012-09-14 02:42:11 +03:00
|
|
|
void CBattleAI::battleAttack(const BattleAttack *ba)
|
|
|
|
{
|
|
|
|
print("battleAttack called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
|
|
|
|
{
|
|
|
|
print("battleStacksAttacked called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleEnd(const BattleResult *br)
|
|
|
|
{
|
|
|
|
print("battleEnd called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleNewRoundFirst(int round)
|
|
|
|
{
|
|
|
|
print("battleNewRoundFirst called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleNewRound(int round)
|
|
|
|
{
|
|
|
|
print("battleNewRound called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
|
|
|
|
{
|
|
|
|
print("battleStackMoved called");;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleSpellCast(const BattleSpellCast *sc)
|
|
|
|
{
|
|
|
|
print("battleSpellCast called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleStacksEffectsSet(const SetStackEffect & sse)
|
|
|
|
{
|
|
|
|
print("battleStacksEffectsSet called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
|
|
|
|
{
|
|
|
|
print("battleStart called");
|
|
|
|
side = Side;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
|
|
|
|
{
|
|
|
|
print("battleStacksHealedRes called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleNewStackAppeared(const CStack * stack)
|
|
|
|
{
|
|
|
|
print("battleNewStackAppeared called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
|
|
|
|
{
|
|
|
|
print("battleObstaclesRemoved called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleCatapultAttacked(const CatapultAttack & ca)
|
|
|
|
{
|
|
|
|
print("battleCatapultAttacked called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
|
|
|
|
{
|
|
|
|
print("battleStacksRemoved called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBattleAI::print(const std::string &text) const
|
|
|
|
{
|
2013-04-10 19:28:14 +03:00
|
|
|
logAi->traceStream() << "CBattleAI [" << this <<"]: " << text;
|
2012-09-14 02:42:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BattleAction CBattleAI::goTowards(const CStack * stack, BattleHex destination)
|
|
|
|
{
|
|
|
|
assert(destination.isValid());
|
|
|
|
auto avHexes = cb->battleGetAvailableHexes(stack, false);
|
|
|
|
auto reachability = cb->getReachability(stack);
|
|
|
|
|
|
|
|
if(vstd::contains(avHexes, destination))
|
|
|
|
return BattleAction::makeMove(stack, destination);
|
|
|
|
|
|
|
|
auto destNeighbours = destination.neighbouringTiles();
|
|
|
|
if(vstd::contains_if(destNeighbours, [&](BattleHex n) { return stack->coversPos(destination); }))
|
|
|
|
{
|
2013-04-10 19:28:14 +03:00
|
|
|
logAi->warnStream() << "Warning: already standing on neighbouring tile!";
|
2012-09-14 02:42:11 +03:00
|
|
|
//We shouldn't even be here...
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
vstd::erase_if(destNeighbours, [&](BattleHex hex){ return !reachability.accessibility.accessible(hex, stack); });
|
|
|
|
|
|
|
|
if(!avHexes.size() || !destNeighbours.size()) //we are blocked or dest is blocked
|
|
|
|
{
|
|
|
|
print("goTowards: Stack cannot move! That's " + stack->nodeName());
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stack->hasBonusOfType(Bonus::FLYING))
|
|
|
|
{
|
|
|
|
// Flying stack doesn't go hex by hex, so we can't backtrack using predecessors.
|
|
|
|
// We just check all available hexes and pick the one closest to the target.
|
|
|
|
auto distToDestNeighbour = [&](BattleHex hex) -> int
|
|
|
|
{
|
|
|
|
auto nearestNeighbourToHex = vstd::minElementByFun(destNeighbours, [&](BattleHex a)
|
|
|
|
{
|
|
|
|
return BattleHex::getDistance(a, hex);
|
|
|
|
});
|
|
|
|
|
|
|
|
return BattleHex::getDistance(*nearestNeighbourToHex, hex);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto nearestAvailableHex = vstd::minElementByFun(avHexes, distToDestNeighbour);
|
|
|
|
return BattleAction::makeMove(stack, *nearestAvailableHex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BattleHex bestNeighbor = destination;
|
|
|
|
if(distToNearestNeighbour(destination, reachability.distances, &bestNeighbor) > GameConstants::BFIELD_SIZE)
|
|
|
|
{
|
|
|
|
print("goTowards: Cannot reach");
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
BattleHex currentDest = bestNeighbor;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
assert(currentDest.isValid());
|
|
|
|
if(vstd::contains(avHexes, currentDest))
|
|
|
|
return BattleAction::makeMove(stack, currentDest);
|
|
|
|
|
|
|
|
currentDest = reachability.predecessors[currentDest];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
BattleAction CBattleAI::useCatapult(const CStack * stack)
|
|
|
|
{
|
2012-09-22 18:10:15 +03:00
|
|
|
throw std::runtime_error("The method or operation is not implemented.");
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
|
|
|
|
2012-09-24 02:10:56 +03:00
|
|
|
enum SpellTypes
|
|
|
|
{
|
|
|
|
OFFENSIVE_SPELL, TIMED_EFFECT, OTHER
|
|
|
|
};
|
|
|
|
|
|
|
|
SpellTypes spellType(const CSpell *spell)
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2013-01-16 14:19:04 +03:00
|
|
|
if (spell->isOffensiveSpell())
|
2012-09-24 02:10:56 +03:00
|
|
|
return OFFENSIVE_SPELL;
|
2013-01-16 14:19:04 +03:00
|
|
|
if (spell->hasEffects())
|
2014-03-07 16:21:09 +03:00
|
|
|
return TIMED_EFFECT;
|
2013-01-16 14:19:04 +03:00
|
|
|
return OTHER;
|
2012-09-24 02:10:56 +03:00
|
|
|
|
2012-12-16 19:54:20 +03:00
|
|
|
}
|
2012-09-20 19:55:21 +03:00
|
|
|
|
|
|
|
struct PossibleSpellcast
|
|
|
|
{
|
|
|
|
const CSpell *spell;
|
|
|
|
BattleHex dest;
|
|
|
|
};
|
|
|
|
|
2012-09-24 02:10:56 +03:00
|
|
|
struct CurrentOffensivePotential
|
|
|
|
{
|
|
|
|
std::map<const CStack *, PotentialTargets> ourAttacks;
|
|
|
|
std::map<const CStack *, PotentialTargets> enemyAttacks;
|
|
|
|
|
|
|
|
CurrentOffensivePotential(ui8 side)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto stack : cbc->battleGetStacks())
|
2012-09-24 02:10:56 +03:00
|
|
|
{
|
|
|
|
if(stack->attackerOwned == !side)
|
|
|
|
ourAttacks[stack] = PotentialTargets(stack);
|
|
|
|
else
|
|
|
|
enemyAttacks[stack] = PotentialTargets(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int potentialValue()
|
|
|
|
{
|
|
|
|
int ourPotential = 0, enemyPotential = 0;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto &p : ourAttacks)
|
2012-09-24 02:10:56 +03:00
|
|
|
ourPotential += p.second.bestAction().attackValue();
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto &p : enemyAttacks)
|
2012-09-24 02:10:56 +03:00
|
|
|
enemyPotential += p.second.bestAction().attackValue();
|
|
|
|
|
|
|
|
return ourPotential - enemyPotential;
|
|
|
|
}
|
|
|
|
};
|
2013-06-21 23:59:32 +03:00
|
|
|
|
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
//
|
2012-09-24 02:10:56 +03:00
|
|
|
// //set has its own order, so remove_if won't work. TODO - reuse for map
|
|
|
|
// template<typename Elem, typename Predicate>
|
|
|
|
// void erase_if(std::set<Elem> &setContainer, Predicate pred)
|
|
|
|
// {
|
|
|
|
// auto itr = setContainer.begin();
|
2014-03-07 16:21:09 +03:00
|
|
|
// auto endItr = setContainer.end();
|
2012-09-24 02:10:56 +03:00
|
|
|
// while(itr != endItr)
|
|
|
|
// {
|
|
|
|
// auto tmpItr = itr++;
|
|
|
|
// if(pred(*tmpItr))
|
|
|
|
// setContainer.erase(tmpItr);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
void CBattleAI::attemptCastingSpell()
|
|
|
|
{
|
|
|
|
LOGL("Casting spells sounds like fun. Let's see...");
|
|
|
|
|
2012-09-24 02:10:56 +03:00
|
|
|
auto hero = cb->battleGetMyHero();
|
|
|
|
|
2012-09-22 18:10:15 +03:00
|
|
|
//auto known = cb->battleGetFightingHero(side);
|
2012-09-20 19:55:21 +03:00
|
|
|
|
|
|
|
//Get all spells we can cast
|
|
|
|
std::vector<const CSpell*> possibleSpells;
|
2014-03-07 16:21:09 +03:00
|
|
|
vstd::copy_if(VLC->spellh->objects, std::back_inserter(possibleSpells), [this] (const CSpell *s) -> bool
|
|
|
|
{
|
|
|
|
auto problem = cbc->battleCanCastThisSpell(s);
|
|
|
|
return problem == ESpellCastProblem::OK;
|
2012-09-20 19:55:21 +03:00
|
|
|
});
|
|
|
|
LOGFL("I can cast %d spells.", possibleSpells.size());
|
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
vstd::erase_if(possibleSpells, [](const CSpell *s)
|
2012-09-24 02:10:56 +03:00
|
|
|
{return spellType(s) == OTHER; });
|
2012-09-20 19:55:21 +03:00
|
|
|
LOGFL("I know about workings of %d of them.", possibleSpells.size());
|
|
|
|
|
|
|
|
//Get possible spell-target pairs
|
|
|
|
std::vector<PossibleSpellcast> possibleCasts;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto spell : possibleSpells)
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto hex : getTargetsToConsider(spell))
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
|
|
|
PossibleSpellcast ps = {spell, hex};
|
|
|
|
possibleCasts.push_back(ps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOGFL("Found %d spell-target combinations.", possibleCasts.size());
|
|
|
|
if(possibleCasts.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::map<const CStack*, int> valueOfStack;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto stack : cb->battleGetStacks())
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
|
|
|
PotentialTargets pt(stack);
|
2012-09-29 17:44:06 +03:00
|
|
|
valueOfStack[stack] = pt.bestActionValue();
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto evaluateSpellcast = [&] (const PossibleSpellcast &ps) -> int
|
|
|
|
{
|
2012-09-24 02:10:56 +03:00
|
|
|
const int skillLevel = hero->getSpellSchoolLevel(ps.spell);
|
|
|
|
const int spellPower = hero->getPrimSkillLevel(PrimarySkill::SPELL_POWER);
|
|
|
|
|
|
|
|
switch(spellType(ps.spell))
|
|
|
|
{
|
|
|
|
case OFFENSIVE_SPELL:
|
|
|
|
{
|
|
|
|
int damageDealt = 0, damageReceived = 0;
|
|
|
|
|
|
|
|
auto stacksSuffering = cb->getAffectedCreatures(ps.spell, skillLevel, playerID, ps.dest);
|
|
|
|
vstd::erase_if(stacksSuffering, [&](const CStack *stack) -> bool
|
|
|
|
{
|
|
|
|
return cb->battleIsImmune(hero, ps.spell, ECastingMode::HERO_CASTING, ps.dest);
|
|
|
|
});
|
|
|
|
|
|
|
|
if(stacksSuffering.empty())
|
|
|
|
return -1;
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto stack : stacksSuffering)
|
2012-09-24 02:10:56 +03:00
|
|
|
{
|
|
|
|
const int dmg = cb->calculateSpellDmg(ps.spell, hero, stack, skillLevel, spellPower);
|
|
|
|
if(stack->owner == playerID)
|
|
|
|
damageReceived += dmg;
|
|
|
|
else
|
|
|
|
damageDealt += dmg;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int damageDiff = damageDealt - damageReceived;
|
|
|
|
|
|
|
|
|
|
|
|
LOGFL("Casting %s on hex %d would deal %d damage points among %d stacks.",
|
|
|
|
ps.spell->name % ps.dest % damageDiff % stacksSuffering.size());
|
|
|
|
//TODO tactic effect too
|
|
|
|
return damageDiff;
|
|
|
|
}
|
|
|
|
case TIMED_EFFECT:
|
|
|
|
{
|
|
|
|
StackWithBonuses swb;
|
|
|
|
swb.stack = cb->battleGetStackByPos(ps.dest);
|
|
|
|
if(!swb.stack)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
Bonus pseudoBonus;
|
|
|
|
pseudoBonus.sid = ps.spell->id;
|
|
|
|
pseudoBonus.val = skillLevel;
|
|
|
|
pseudoBonus.turnsRemain = 1; //TODO
|
|
|
|
CStack::stackEffectToFeature(swb.bonusesToAdd, pseudoBonus);
|
|
|
|
|
|
|
|
HypotheticChangesToBattleState state;
|
|
|
|
state.bonusesOfStacks[swb.stack] = &swb;
|
|
|
|
|
|
|
|
PotentialTargets pt(swb.stack, state);
|
2012-09-29 17:44:06 +03:00
|
|
|
auto newValue = pt.bestActionValue();
|
2012-09-24 02:10:56 +03:00
|
|
|
auto oldValue = valueOfStack[swb.stack];
|
|
|
|
auto gain = newValue - oldValue;
|
|
|
|
if(swb.stack->owner != playerID) //enemy
|
|
|
|
gain = -gain;
|
|
|
|
|
|
|
|
LOGFL("Casting %s on %s would improve the stack by %d points (from %d to %d)",
|
|
|
|
ps.spell->name % swb.stack->nodeName() % gain % (oldValue) % (newValue));
|
|
|
|
|
|
|
|
return gain;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(0);
|
2012-10-05 16:11:26 +03:00
|
|
|
return 0;
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
2012-09-20 19:55:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
auto castToPerform = *vstd::maxElementByFun(possibleCasts, evaluateSpellcast);
|
2012-09-24 02:10:56 +03:00
|
|
|
LOGFL("Best spell is %s. Will cast.", castToPerform.spell->name);
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
BattleAction spellcast;
|
2013-02-04 00:05:44 +03:00
|
|
|
spellcast.actionType = Battle::HERO_SPELL;
|
2012-09-20 19:55:21 +03:00
|
|
|
spellcast.additionalInfo = castToPerform.spell->id;
|
|
|
|
spellcast.destinationTile = castToPerform.dest;
|
|
|
|
spellcast.side = side;
|
|
|
|
spellcast.stackNumber = (!side) ? -1 : -2;
|
|
|
|
|
|
|
|
cb->battleMakeAction(&spellcast);
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<BattleHex> CBattleAI::getTargetsToConsider( const CSpell *spell ) const
|
|
|
|
{
|
|
|
|
if(spell->getTargetType() == CSpell::NO_TARGET)
|
|
|
|
{
|
|
|
|
//Spell can be casted anywhere, all hexes are potentially considerable.
|
|
|
|
std::vector<BattleHex> ret;
|
|
|
|
|
|
|
|
for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
|
|
|
|
if(BattleHex(i).isAvailable())
|
|
|
|
ret.push_back(i);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-23 15:59:03 +03:00
|
|
|
//TODO when massive effect -> doesn't matter where cast
|
2012-09-24 02:10:56 +03:00
|
|
|
return cbc->battleGetPossibleTargets(playerID, spell);
|
|
|
|
}
|
|
|
|
}
|
2013-06-17 18:45:55 +03:00
|
|
|
|
|
|
|
boost::optional<BattleAction> CBattleAI::considerFleeingOrSurrendering()
|
|
|
|
{
|
|
|
|
if(cb->battleCanSurrender(playerID))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
if(cb->battleCanFlee())
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
return boost::none;
|
|
|
|
}
|
2013-06-21 23:59:32 +03:00
|
|
|
|
|
|
|
ThreatMap::ThreatMap(const CStack *Endangered) : endangered(Endangered)
|
|
|
|
{
|
|
|
|
sufferedDamage.fill(0);
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const CStack *enemy : cbc->battleGetStacks())
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
|
|
|
//Consider only stacks of different owner
|
|
|
|
if(enemy->attackerOwned == endangered->attackerOwned)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//Look-up which tiles can be melee-attacked
|
|
|
|
std::array<bool, GameConstants::BFIELD_SIZE> meleeAttackable;
|
|
|
|
meleeAttackable.fill(false);
|
|
|
|
auto enemyReachability = cbc->getReachability(enemy);
|
|
|
|
for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
|
|
|
|
{
|
|
|
|
if(enemyReachability.isReachable(i))
|
|
|
|
{
|
|
|
|
meleeAttackable[i] = true;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto n : BattleHex(i).neighbouringTiles())
|
2013-06-21 23:59:32 +03:00
|
|
|
meleeAttackable[n] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Gather possible assaults
|
|
|
|
for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
|
|
|
|
{
|
|
|
|
if(cbc->battleCanShoot(enemy, i))
|
|
|
|
threatMap[i].push_back(BattleAttackInfo(enemy, endangered, true));
|
|
|
|
else if(meleeAttackable[i])
|
|
|
|
{
|
|
|
|
BattleAttackInfo bai(enemy, endangered, false);
|
|
|
|
bai.chargedFields = std::max(BattleHex::getDistance(enemy->position, i) - 1, 0); //TODO check real distance (BFS), not just metric
|
|
|
|
threatMap[i].push_back(BattleAttackInfo(bai));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
|
|
|
|
{
|
|
|
|
sufferedDamage[i] = sum(threatMap[i], [](const BattleAttackInfo &bai) -> int
|
|
|
|
{
|
|
|
|
auto dmg = cbc->calculateDmgRange(bai);
|
|
|
|
return (dmg.first + dmg.second)/2;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
const TBonusListPtr StackWithBonuses::getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= nullptr*/, const std::string &cachingStr /*= ""*/) const
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
|
|
|
TBonusListPtr ret = make_shared<BonusList>();
|
|
|
|
const TBonusListPtr originalList = stack->getAllBonuses(selector, limit, root, cachingStr);
|
2013-06-29 16:05:48 +03:00
|
|
|
range::copy(*originalList, std::back_inserter(*ret));
|
|
|
|
for(auto &bonus : bonusesToAdd)
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
|
|
|
if(selector(&bonus) && (!limit || !limit(&bonus)))
|
|
|
|
ret->push_back(&bonus);
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO limiters?
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AttackPossibility::damageDiff() const
|
|
|
|
{
|
|
|
|
const auto dealtDmgValue = priorities.stackEvaluator(enemy) * damageDealt;
|
|
|
|
const auto receivedDmgValue = priorities.stackEvaluator(attack.attacker) * damageReceived;
|
|
|
|
return dealtDmgValue - receivedDmgValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AttackPossibility::attackValue() const
|
|
|
|
{
|
|
|
|
return damageDiff() + tacticImpact;
|
|
|
|
}
|
|
|
|
|
|
|
|
AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo &AttackInfo, const HypotheticChangesToBattleState &state, BattleHex hex)
|
|
|
|
{
|
|
|
|
auto attacker = AttackInfo.attacker;
|
|
|
|
auto enemy = AttackInfo.defender;
|
|
|
|
|
|
|
|
const int remainingCounterAttacks = getValOr(state.counterAttacksLeft, enemy, enemy->counterAttacks);
|
|
|
|
const bool counterAttacksBlocked = attacker->hasBonusOfType(Bonus::BLOCKS_RETALIATION) || enemy->hasBonusOfType(Bonus::NO_RETALIATION);
|
2013-07-02 15:08:30 +03:00
|
|
|
const int totalAttacks = 1 + AttackInfo.attackerBonuses->getBonuses(Selector::type(Bonus::ADDITIONAL_ATTACK), (Selector::effectRange (Bonus::NO_LIMIT).Or(Selector::effectRange(Bonus::ONLY_MELEE_FIGHT))))->totalValue();
|
2013-06-21 23:59:32 +03:00
|
|
|
|
|
|
|
AttackPossibility ap = {enemy, hex, AttackInfo, 0, 0, 0};
|
|
|
|
|
|
|
|
auto curBai = AttackInfo; //we'll modify here the stack counts
|
|
|
|
for(int i = 0; i < totalAttacks; i++)
|
|
|
|
{
|
|
|
|
std::pair<ui32, ui32> retaliation(0,0);
|
|
|
|
auto attackDmg = cbc->battleEstimateDamage(curBai, &retaliation);
|
|
|
|
ap.damageDealt = (attackDmg.first + attackDmg.second) / 2;
|
|
|
|
ap.damageReceived = (retaliation.first + retaliation.second) / 2;
|
|
|
|
|
|
|
|
if(remainingCounterAttacks <= i || counterAttacksBlocked)
|
|
|
|
ap.damageReceived = 0;
|
|
|
|
|
|
|
|
curBai.attackerCount = attacker->count - attacker->countKilledByAttack(ap.damageReceived).first;
|
|
|
|
curBai.defenderCount = enemy->count - enemy->countKilledByAttack(ap.damageDealt).first;
|
2014-03-07 16:21:09 +03:00
|
|
|
if(!curBai.attackerCount)
|
2013-06-21 23:59:32 +03:00
|
|
|
break;
|
|
|
|
//TODO what about defender? should we break? but in pessimistic scenario defender might be alive
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO other damage related to attack (eg. fire shield and other abilities)
|
|
|
|
|
|
|
|
//Limit damages by total stack health
|
|
|
|
vstd::amin(ap.damageDealt, enemy->count * enemy->MaxHealth() - (enemy->MaxHealth() - enemy->firstHPleft));
|
|
|
|
vstd::amin(ap.damageReceived, attacker->count * attacker->MaxHealth() - (attacker->MaxHealth() - attacker->firstHPleft));
|
|
|
|
|
|
|
|
return ap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PotentialTargets::PotentialTargets(const CStack *attacker, const HypotheticChangesToBattleState &state /*= HypotheticChangesToBattleState()*/)
|
|
|
|
{
|
|
|
|
auto dists = cbc->battleGetDistances(attacker);
|
|
|
|
auto avHexes = cbc->battleGetAvailableHexes(attacker, false);
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const CStack *enemy : cbc->battleGetStacks())
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
|
|
|
//Consider only stacks of different owner
|
|
|
|
if(enemy->attackerOwned == attacker->attackerOwned)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto GenerateAttackInfo = [&](bool shooting, BattleHex hex) -> AttackPossibility
|
|
|
|
{
|
|
|
|
auto bai = BattleAttackInfo(attacker, enemy, shooting);
|
|
|
|
bai.attackerBonuses = getValOr(state.bonusesOfStacks, bai.attacker, bai.attacker);
|
|
|
|
bai.defenderBonuses = getValOr(state.bonusesOfStacks, bai.defender, bai.defender);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
if(hex.isValid())
|
|
|
|
{
|
|
|
|
assert(dists[hex] <= attacker->Speed());
|
|
|
|
bai.chargedFields = dists[hex];
|
|
|
|
}
|
|
|
|
|
|
|
|
return AttackPossibility::evaluate(bai, state, hex);
|
|
|
|
};
|
|
|
|
|
|
|
|
if(cbc->battleCanShoot(attacker, enemy->position))
|
|
|
|
{
|
|
|
|
possibleAttacks.push_back(GenerateAttackInfo(true, BattleHex::INVALID));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(BattleHex hex : avHexes)
|
2013-06-21 23:59:32 +03:00
|
|
|
if(CStack::isMeleeAttackPossible(attacker, enemy, hex))
|
|
|
|
possibleAttacks.push_back(GenerateAttackInfo(false, hex));
|
|
|
|
|
|
|
|
if(!vstd::contains_if(possibleAttacks, [=](const AttackPossibility &pa) { return pa.enemy == enemy; }))
|
|
|
|
unreachableEnemies.push_back(enemy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AttackPossibility PotentialTargets::bestAction() const
|
|
|
|
{
|
|
|
|
if(possibleAttacks.empty())
|
|
|
|
throw std::runtime_error("No best action, since we don't have any actions");
|
|
|
|
|
|
|
|
return *vstd::maxElementByFun(possibleAttacks, [](const AttackPossibility &ap) { return ap.attackValue(); } );
|
|
|
|
}
|
|
|
|
|
|
|
|
int PotentialTargets::bestActionValue() const
|
|
|
|
{
|
|
|
|
if(possibleAttacks.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return bestAction().attackValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnemyInfo::calcDmg(const CStack * ourStack)
|
|
|
|
{
|
|
|
|
TDmgRange retal, dmg = cbc->battleEstimateDamage(ourStack, s, &retal);
|
|
|
|
adi = (dmg.first + dmg.second) / 2;
|
|
|
|
adr = (retal.first + retal.second) / 2;
|
|
|
|
}
|