2016-10-28 16:16:46 +02:00
|
|
|
/*
|
2017-07-13 10:26:03 +02:00
|
|
|
* BattleAI.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
|
|
|
|
*
|
|
|
|
*/
|
2012-09-14 02:42:11 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "BattleAI.h"
|
2016-10-28 16:16:46 +02:00
|
|
|
#include "StackWithBonuses.h"
|
|
|
|
#include "EnemyInfo.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "../../lib/spells/CSpellHandler.h"
|
2012-09-14 02:42:11 +03:00
|
|
|
|
2016-10-28 16:16:46 +02:00
|
|
|
#define LOGL(text) print(text)
|
|
|
|
#define LOGFL(text, formattingEl) print(boost::str(boost::format(text) % formattingEl))
|
2012-09-14 02:42:11 +03:00
|
|
|
|
2013-06-21 23:59:32 +03:00
|
|
|
CBattleAI::CBattleAI(void)
|
2016-11-27 16:48:18 +02:00
|
|
|
: side(-1), wasWaitingForRealize(false), wasUnlockingGs(false)
|
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
|
|
|
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
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
void CBattleAI::init(std::shared_ptr<CBattleCallback> CB)
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
setCbc(CB);
|
|
|
|
cb = CB;
|
2017-07-12 21:01:10 +02:00
|
|
|
playerID = *CB->getPlayerID(); //TODO should be sth in callback
|
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
|
|
|
|
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()) ;
|
2016-10-28 16:16:46 +02:00
|
|
|
setCbc(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
|
|
|
|
{
|
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);
|
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)
|
2017-07-04 13:24:46 +02:00
|
|
|
if(auto woundHp = stack->MaxHealth() - stack->getFirstHPleft())
|
2013-08-25 16:03:29 +03:00
|
|
|
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
|
|
|
|
2016-11-28 02:51:32 +02:00
|
|
|
attemptCastingSpell();
|
|
|
|
|
2016-10-28 16:16:46 +02:00
|
|
|
if(auto ret = getCbc()->battleIsFinished())
|
2016-09-22 19:51:13 +02:00
|
|
|
{
|
|
|
|
//spellcast may finish battle
|
|
|
|
//send special preudo-action
|
|
|
|
BattleAction cancel;
|
|
|
|
cancel.actionType = Battle::CANCEL;
|
|
|
|
return cancel;
|
|
|
|
}
|
2013-06-21 23:59:32 +03:00
|
|
|
|
2013-06-17 18:45:55 +03:00
|
|
|
if(auto action = considerFleeingOrSurrendering())
|
|
|
|
return *action;
|
2012-09-20 19:55:21 +03:00
|
|
|
PotentialTargets targets(stack);
|
|
|
|
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())
|
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
//ThreatMap threatsToUs(stack); // These lines may be usefull but they are't used in the code.
|
|
|
|
auto dists = getCbc()->battleGetDistances(stack);
|
2016-03-12 03:41:27 +02:00
|
|
|
const EnemyInfo &ei= *range::min_element(targets.unreachableEnemies, std::bind(isCloser, _1, _2, std::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
|
|
|
}
|
|
|
|
}
|
2017-11-16 13:15:43 +02:00
|
|
|
catch(boost::thread_interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2012-09-20 19:55:21 +03:00
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
2016-08-13 16:44:37 +02:00
|
|
|
logAi->error("Exception occurred in %s %s",__FUNCTION__, e.what());
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
2012-09-14 02:42:11 +03:00
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
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); }))
|
|
|
|
{
|
2016-08-12 07:13:11 +02:00
|
|
|
logAi->warn("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
|
|
|
|
{
|
|
|
|
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)
|
2016-10-28 16:16:46 +02:00
|
|
|
{return BattleHex::getDistance(a, hex);});
|
2012-09-14 02:42:11 +03:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-28 16:16:46 +02: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-12-16 19:54:20 +03:00
|
|
|
}
|
2012-09-20 19:55:21 +03:00
|
|
|
|
|
|
|
void CBattleAI::attemptCastingSpell()
|
|
|
|
{
|
2016-11-28 02:51:32 +02:00
|
|
|
auto hero = cb->battleGetMyHero();
|
|
|
|
if(!hero)
|
|
|
|
return;
|
|
|
|
|
2017-06-05 21:16:12 +02:00
|
|
|
if(cb->battleCanCastSpell(hero, ECastingMode::HERO_CASTING) != ESpellCastProblem::OK)
|
2016-11-28 02:51:32 +02:00
|
|
|
return;
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
LOGL("Casting spells sounds like fun. Let's see...");
|
|
|
|
//Get all spells we can cast
|
|
|
|
std::vector<const CSpell*> possibleSpells;
|
2017-06-05 21:53:42 +02:00
|
|
|
vstd::copy_if(VLC->spellh->objects, std::back_inserter(possibleSpells), [this, hero] (const CSpell *s) -> bool
|
2014-03-07 16:21:09 +03:00
|
|
|
{
|
2017-06-05 22:46:55 +02:00
|
|
|
return s->canBeCast(getCbc().get(), ECastingMode::HERO_CASTING, hero) == 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
|
|
|
{
|
2016-03-01 07:49:29 +02:00
|
|
|
for(auto hex : getTargetsToConsider(spell, hero))
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2016-09-22 21:29:48 +02:00
|
|
|
PossibleSpellcast ps = {spell, hex, 0};
|
2012-09-20 19:55:21 +03:00
|
|
|
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:
|
2016-10-28 16:16:46 +02:00
|
|
|
{
|
|
|
|
int damageDealt = 0, damageReceived = 0;
|
|
|
|
auto stacksSuffering = ps.spell->getAffectedStacks(cb.get(), ECastingMode::HERO_CASTING, hero, skillLevel, ps.dest);
|
|
|
|
if(stacksSuffering.empty())
|
|
|
|
return -1;
|
|
|
|
for(auto stack : stacksSuffering)
|
2012-09-24 02:10:56 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
const int dmg = ps.spell->calculateDamage(hero, stack, skillLevel, spellPower);
|
|
|
|
if(stack->owner == playerID)
|
|
|
|
damageReceived += dmg;
|
|
|
|
else
|
|
|
|
damageDealt += dmg;
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
2016-10-28 16:16:46 +02:00
|
|
|
const int damageDiff = damageDealt - damageReceived * 10;
|
|
|
|
LOGFL("Casting %s on hex %d would deal { %d %d } damage points among %d stacks.",
|
|
|
|
ps.spell->name % ps.dest % damageDealt % damageReceived % stacksSuffering.size());
|
|
|
|
//TODO tactic effect too
|
|
|
|
return damageDiff;
|
|
|
|
}
|
2012-09-24 02:10:56 +03:00
|
|
|
case TIMED_EFFECT:
|
2016-10-28 16:16:46 +02:00
|
|
|
{
|
|
|
|
auto stacksAffected = ps.spell->getAffectedStacks(cb.get(), ECastingMode::HERO_CASTING, hero, skillLevel, ps.dest);
|
|
|
|
if(stacksAffected.empty())
|
|
|
|
return -1;
|
|
|
|
int totalGain = 0;
|
|
|
|
for(const CStack * sta : stacksAffected)
|
2012-09-24 02:10:56 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
StackWithBonuses swb;
|
|
|
|
swb.stack = sta;
|
2016-11-02 19:11:01 +02:00
|
|
|
//todo: handle effect actualization in HypotheticChangesToBattleState
|
|
|
|
ps.spell->getEffects(swb.bonusesToAdd, skillLevel, false, hero->getEnchantPower(ps.spell));
|
|
|
|
ps.spell->getEffects(swb.bonusesToAdd, skillLevel, true, hero->getEnchantPower(ps.spell));
|
2016-10-28 16:16:46 +02:00
|
|
|
HypotheticChangesToBattleState state;
|
|
|
|
state.bonusesOfStacks[swb.stack] = &swb;
|
|
|
|
PotentialTargets pt(swb.stack, state);
|
|
|
|
auto newValue = pt.bestActionValue();
|
|
|
|
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 % sta->nodeName() % (gain) % (oldValue) % (newValue));
|
|
|
|
totalGain += gain;
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
2016-10-28 16:16:46 +02:00
|
|
|
|
|
|
|
LOGFL("Total gain of cast %s at hex %d is %d", ps.spell->name % (ps.dest.hex) % (totalGain));
|
|
|
|
return totalGain;
|
|
|
|
}
|
2012-09-24 02:10:56 +03:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-09-22 21:29:48 +02:00
|
|
|
for(PossibleSpellcast & psc : possibleCasts)
|
|
|
|
psc.value = evaluateSpellcast(psc);
|
|
|
|
auto pscValue = [] (const PossibleSpellcast &ps) -> int
|
|
|
|
{
|
|
|
|
return ps.value;
|
|
|
|
};
|
|
|
|
auto castToPerform = *vstd::maxElementByFun(possibleCasts, pscValue);
|
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
|
|
|
}
|
|
|
|
|
2016-03-01 07:49:29 +02:00
|
|
|
std::vector<BattleHex> CBattleAI::getTargetsToConsider(const CSpell * spell, const ISpellCaster * caster) const
|
2012-09-24 02:10:56 +03:00
|
|
|
{
|
2016-03-01 07:49:29 +02:00
|
|
|
const CSpell::TargetInfo targetInfo(spell, caster->getSpellSchoolLevel(spell));
|
|
|
|
std::vector<BattleHex> ret;
|
|
|
|
if(targetInfo.massive || targetInfo.type == CSpell::NO_TARGET)
|
|
|
|
{
|
|
|
|
ret.push_back(BattleHex());
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-01 07:49:29 +02:00
|
|
|
switch(targetInfo.type)
|
|
|
|
{
|
|
|
|
case CSpell::CREATURE:
|
2016-10-28 16:16:46 +02:00
|
|
|
{
|
|
|
|
for(const CStack * stack : getCbc()->battleAliveStacks())
|
2016-03-01 07:49:29 +02:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
bool immune = ESpellCastProblem::OK != spell->isImmuneByStack(caster, stack);
|
|
|
|
bool casterStack = stack->owner == caster->getOwner();
|
|
|
|
|
|
|
|
if(!immune)
|
|
|
|
switch (spell->positiveness)
|
|
|
|
{
|
|
|
|
case CSpell::POSITIVE:
|
|
|
|
if(casterStack || targetInfo.smart)
|
2016-03-01 07:49:29 +02:00
|
|
|
ret.push_back(stack->position);
|
2016-10-28 16:16:46 +02:00
|
|
|
break;
|
|
|
|
case CSpell::NEUTRAL:
|
|
|
|
ret.push_back(stack->position);
|
|
|
|
break;
|
|
|
|
case CSpell::NEGATIVE:
|
|
|
|
if(!casterStack || targetInfo.smart)
|
|
|
|
ret.push_back(stack->position);
|
|
|
|
break;
|
|
|
|
}
|
2016-03-01 07:49:29 +02:00
|
|
|
}
|
2016-10-28 16:16:46 +02:00
|
|
|
}
|
2016-03-01 07:49:29 +02:00
|
|
|
break;
|
2016-03-01 08:07:51 +02:00
|
|
|
case CSpell::LOCATION:
|
2016-10-28 16:16:46 +02:00
|
|
|
{
|
|
|
|
for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
|
|
|
|
if(BattleHex(i).isAvailable())
|
|
|
|
ret.push_back(i);
|
|
|
|
}
|
2016-03-01 08:07:51 +02:00
|
|
|
break;
|
|
|
|
|
2016-03-01 07:49:29 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
2016-03-01 07:49:29 +02:00
|
|
|
return ret;
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
2013-06-17 18:45:55 +03:00
|
|
|
|
2016-10-28 16:16:46 +02:00
|
|
|
int CBattleAI::distToNearestNeighbour(BattleHex hex, const ReachabilityInfo::TDistances &dists, BattleHex *chosenHex)
|
2013-06-17 18:45:55 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
int ret = 1000000;
|
|
|
|
for(BattleHex n : hex.neighbouringTiles())
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
if(dists[n] >= 0 && dists[n] < ret)
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
ret = dists[n];
|
|
|
|
if(chosenHex)
|
|
|
|
*chosenHex = n;
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 16:16:46 +02:00
|
|
|
return ret;
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
|
|
|
|
2016-10-28 16:16:46 +02:00
|
|
|
void CBattleAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
print("battleStart called");
|
|
|
|
side = Side;
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
|
|
|
|
2016-10-28 16:16:46 +02:00
|
|
|
bool CBattleAI::isCloser(const EnemyInfo &ei1, const EnemyInfo &ei2, const ReachabilityInfo::TDistances &dists)
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
return distToNearestNeighbour(ei1.s->position, dists) < distToNearestNeighbour(ei2.s->position, dists);
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
|
|
|
|
2016-10-28 16:16:46 +02:00
|
|
|
void CBattleAI::print(const std::string &text) const
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
logAi->trace("CBattleAI [%p]: %s", this, text);
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
|
|
|
|
2016-10-28 16:16:46 +02:00
|
|
|
boost::optional<BattleAction> CBattleAI::considerFleeingOrSurrendering()
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
if(cb->battleCanSurrender(playerID))
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
|
|
|
}
|
2016-10-28 16:16:46 +02:00
|
|
|
if(cb->battleCanFlee())
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
|
|
|
}
|
2016-10-28 16:16:46 +02:00
|
|
|
return boost::none;
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|