2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "../../lib/AI_Base.h"
|
2010-12-22 22:14:40 +02:00
|
|
|
#include "StupidAI.h"
|
2010-12-25 21:23:30 +02:00
|
|
|
#include "../../lib/BattleState.h"
|
2011-01-07 12:48:31 +02:00
|
|
|
#include "../../CCallback.h"
|
2011-01-08 20:33:40 +02:00
|
|
|
#include "../../lib/CCreatureHandler.h"
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
static std::shared_ptr<CBattleCallback> cbc;
|
2010-12-22 22:14:40 +02:00
|
|
|
|
|
|
|
CStupidAI::CStupidAI(void)
|
2013-06-23 22:35:54 +03:00
|
|
|
: side(-1)
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("created");
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CStupidAI::~CStupidAI(void)
|
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("destroyed");
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
void CStupidAI::init(std::shared_ptr<CBattleCallback> CB)
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("init called, saving ptr to IBattleCallback");
|
2011-01-08 20:33:40 +02:00
|
|
|
cbc = cb = CB;
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
2013-05-09 14:09:23 +03:00
|
|
|
void CStupidAI::actionFinished(const BattleAction &action)
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("actionFinished called");
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
2013-05-09 14:09:23 +03:00
|
|
|
void CStupidAI::actionStarted(const BattleAction &action)
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("actionStarted called");
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
2011-01-08 20:33:40 +02:00
|
|
|
struct EnemyInfo
|
|
|
|
{
|
|
|
|
const CStack * s;
|
|
|
|
int adi, adr;
|
2011-12-22 16:05:19 +03:00
|
|
|
std::vector<BattleHex> attackFrom; //for melee fight
|
2011-01-08 20:33:40 +02:00
|
|
|
EnemyInfo(const CStack * _s) : s(_s)
|
|
|
|
{}
|
|
|
|
void calcDmg(const CStack * ourStack)
|
|
|
|
{
|
|
|
|
TDmgRange retal, dmg = cbc->battleEstimateDamage(ourStack, s, &retal);
|
|
|
|
adi = (dmg.first + dmg.second) / 2;
|
|
|
|
adr = (retal.first + retal.second) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const EnemyInfo& ei) const
|
|
|
|
{
|
|
|
|
return s == ei.s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
bool isMoreProfitable(const EnemyInfo &ei1, const EnemyInfo& ei2)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
return (ei1.adi-ei1.adr) < (ei2.adi - ei2.adr);
|
|
|
|
}
|
|
|
|
|
2014-02-20 21:53:18 +03:00
|
|
|
namespace {
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
int distToNearestNeighbour(BattleHex hex, const ReachabilityInfo::TDistances& dists, BattleHex *chosenHex = nullptr)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
int ret = 1000000;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & n: hex.neighbouringTiles())
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
if(dists[n] >= 0 && dists[n] < ret)
|
|
|
|
{
|
|
|
|
ret = dists[n];
|
|
|
|
if(chosenHex)
|
|
|
|
*chosenHex = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
bool isCloser(const EnemyInfo & ei1, const EnemyInfo & ei2, const ReachabilityInfo::TDistances & dists)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
return distToNearestNeighbour(ei1.s->position, dists) < distToNearestNeighbour(ei2.s->position, dists);
|
|
|
|
}
|
|
|
|
|
2014-02-20 21:53:18 +03:00
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
static bool willSecondHexBlockMoreEnemyShooters(const BattleHex &h1, const BattleHex &h2)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
int shooters[2] = {0}; //count of shooters on hexes
|
|
|
|
|
|
|
|
for(int i = 0; i < 2; i++)
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & neighbour : (i ? h2 : h1).neighbouringTiles())
|
2011-01-08 20:33:40 +02:00
|
|
|
if(const CStack *s = cbc->battleGetStackByPos(neighbour))
|
|
|
|
if(s->getCreature()->isShooting())
|
|
|
|
shooters[i]++;
|
|
|
|
|
|
|
|
return shooters[0] < shooters[1];
|
|
|
|
}
|
|
|
|
|
2010-12-23 22:18:10 +02:00
|
|
|
BattleAction CStupidAI::activeStack( const CStack * stack )
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2011-01-08 21:38:42 +02:00
|
|
|
//boost::this_thread::sleep(boost::posix_time::seconds(2));
|
2012-08-26 12:07:48 +03:00
|
|
|
print("activeStack called for " + stack->nodeName());
|
|
|
|
auto dists = cb->battleGetDistances(stack);
|
2011-01-08 20:33:40 +02:00
|
|
|
std::vector<EnemyInfo> enemiesShootable, enemiesReachable, enemiesUnreachable;
|
|
|
|
|
2013-02-07 20:34:50 +03:00
|
|
|
if(stack->type->idNumber == CreatureID::CATAPULT)
|
2012-01-03 04:55:26 +03:00
|
|
|
{
|
|
|
|
BattleAction attack;
|
2016-02-14 13:38:24 +02:00
|
|
|
static const std::vector<int> wallHexes = {50, 183, 182, 130, 78, 29, 12, 95};
|
2014-04-10 20:11:09 +03:00
|
|
|
|
|
|
|
attack.destinationTile = *RandomGeneratorUtil::nextItem(wallHexes, CRandomGenerator::getDefault());
|
2013-02-04 00:05:44 +03:00
|
|
|
attack.actionType = Battle::CATAPULT;
|
2012-01-03 04:55:26 +03:00
|
|
|
attack.additionalInfo = 0;
|
|
|
|
attack.side = side;
|
|
|
|
attack.stackNumber = stack->ID;
|
|
|
|
|
|
|
|
return attack;
|
|
|
|
}
|
2013-08-25 16:03:29 +03:00
|
|
|
else if(stack->hasBonusOfType(Bonus::SIEGE_WEAPON))
|
|
|
|
{
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
2012-01-03 04:55:26 +03:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for (const CStack *s : cb->battleGetStacks(CBattleCallback::ONLY_ENEMY))
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2011-02-14 22:31:53 +02:00
|
|
|
if(cb->battleCanShoot(stack, s->position))
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2011-02-14 22:31:53 +02:00
|
|
|
enemiesShootable.push_back(s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-26 22:13:57 +03:00
|
|
|
std::vector<BattleHex> avHexes = cb->battleGetAvailableHexes(stack, false);
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for (BattleHex hex : avHexes)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2011-02-14 22:31:53 +02:00
|
|
|
if(CStack::isMeleeAttackPossible(stack, s, hex))
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2011-02-14 22:31:53 +02:00
|
|
|
std::vector<EnemyInfo>::iterator i = std::find(enemiesReachable.begin(), enemiesReachable.end(), s);
|
|
|
|
if(i == enemiesReachable.end())
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2011-02-14 22:31:53 +02:00
|
|
|
enemiesReachable.push_back(s);
|
|
|
|
i = enemiesReachable.begin() + (enemiesReachable.size() - 1);
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
|
2011-02-14 22:31:53 +02:00
|
|
|
i->attackFrom.push_back(hex);
|
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
2011-02-14 22:31:53 +02:00
|
|
|
|
2012-01-03 04:55:26 +03:00
|
|
|
if(!vstd::contains(enemiesReachable, s) && s->position.isValid())
|
2011-02-14 22:31:53 +02:00
|
|
|
enemiesUnreachable.push_back(s);
|
2011-01-07 12:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 21:28:01 +02:00
|
|
|
for ( auto & enemy : enemiesReachable )
|
|
|
|
enemy.calcDmg( stack );
|
|
|
|
|
|
|
|
for ( auto & enemy : enemiesShootable )
|
|
|
|
enemy.calcDmg( stack );
|
|
|
|
|
2011-01-08 20:33:40 +02:00
|
|
|
if(enemiesShootable.size())
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
2011-01-08 20:33:40 +02:00
|
|
|
const EnemyInfo &ei= *std::max_element(enemiesShootable.begin(), enemiesShootable.end(), isMoreProfitable);
|
|
|
|
return BattleAction::makeShotAttack(stack, ei.s);
|
2010-12-25 03:43:40 +02:00
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
else if(enemiesReachable.size())
|
|
|
|
{
|
|
|
|
const EnemyInfo &ei= *std::max_element(enemiesReachable.begin(), enemiesReachable.end(), &isMoreProfitable);
|
|
|
|
return BattleAction::makeMeleeAttack(stack, ei.s, *std::max_element(ei.attackFrom.begin(), ei.attackFrom.end(), &willSecondHexBlockMoreEnemyShooters));
|
|
|
|
}
|
2012-09-28 19:48:29 +03:00
|
|
|
else if(enemiesUnreachable.size()) //due to #955 - a buggy battle may occur when there are no enemies
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2012-09-28 12:28:12 +03:00
|
|
|
assert(enemiesUnreachable.size());
|
2014-08-04 21:33:59 +03:00
|
|
|
const EnemyInfo &ei= *std::min_element(enemiesUnreachable.begin(), enemiesUnreachable.end(), std::bind(isCloser, _1, _2, std::ref(dists)));
|
2012-09-28 12:28:12 +03:00
|
|
|
assert(ei.s);
|
2012-08-26 22:13:57 +03:00
|
|
|
if(distToNearestNeighbour(ei.s->position, dists) < GameConstants::BFIELD_SIZE)
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2011-01-08 20:33:40 +02:00
|
|
|
return goTowards(stack, ei.s->position);
|
2011-01-07 12:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-25 03:48:02 +02:00
|
|
|
return BattleAction::makeDefend(stack);
|
2010-12-25 03:43:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleAttack(const BattleAttack *ba)
|
|
|
|
{
|
|
|
|
print("battleAttack called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
|
|
|
|
{
|
|
|
|
print("battleStacksAttacked called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleEnd(const BattleResult *br)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleEnd called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
// void CStupidAI::battleResultsApplied()
|
2011-05-10 01:20:47 +03:00
|
|
|
// {
|
|
|
|
// print("battleResultsApplied called");
|
|
|
|
// }
|
2010-12-25 03:43:40 +02:00
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleNewRoundFirst(int round)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleNewRoundFirst called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleNewRound(int round)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleNewRound called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleStackMoved called");;
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleSpellCast(const BattleSpellCast *sc)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleSpellCast called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleStacksEffectsSet(const SetStackEffect & sse)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleStacksEffectsSet called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleStart called");
|
|
|
|
side = Side;
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleStacksHealedRes called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleNewStackAppeared(const CStack * stack)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleNewStackAppeared called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleObstaclesRemoved called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleCatapultAttacked(const CatapultAttack & ca)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleCatapultAttacked called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
void CStupidAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleStacksRemoved called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::print(const std::string &text) const
|
|
|
|
{
|
2016-03-12 03:41:27 +02:00
|
|
|
logAi->traceStream() << "CStupidAI [" << this <<"]: " << text;
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
BattleAction CStupidAI::goTowards(const CStack * stack, BattleHex destination)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2012-08-26 12:07:48 +03:00
|
|
|
assert(destination.isValid());
|
|
|
|
auto avHexes = cb->battleGetAvailableHexes(stack, false);
|
|
|
|
auto reachability = cb->getReachability(stack);
|
|
|
|
|
|
|
|
if(vstd::contains(avHexes, destination))
|
|
|
|
return BattleAction::makeMove(stack, destination);
|
2012-08-26 12:59:07 +03:00
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
auto destNeighbours = destination.neighbouringTiles();
|
|
|
|
if(vstd::contains_if(destNeighbours, [&](BattleHex n) { return stack->coversPos(destination); }))
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2016-03-12 03:41:27 +02:00
|
|
|
logAi->warnStream() << "Warning: already standing on neighbouring tile!";
|
2012-08-26 12:07:48 +03:00
|
|
|
//We shouldn't even be here...
|
2011-01-08 20:33:40 +02:00
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
vstd::erase_if(destNeighbours, [&](BattleHex hex){ return !reachability.accessibility.accessible(hex, stack); });
|
2011-01-08 20:33:40 +02:00
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
if(!avHexes.size() || !destNeighbours.size()) //we are blocked or dest is blocked
|
2011-09-06 16:59:26 +03:00
|
|
|
{
|
|
|
|
print("goTowards: Stack cannot move! That's " + stack->nodeName());
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
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)
|
2012-08-26 12:59:07 +03:00
|
|
|
{
|
|
|
|
return BattleHex::getDistance(a, hex);
|
2012-08-26 12:07:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return BattleHex::getDistance(*nearestNeighbourToHex, hex);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto nearestAvailableHex = vstd::minElementByFun(avHexes, distToDestNeighbour);
|
|
|
|
return BattleAction::makeMove(stack, *nearestAvailableHex);
|
|
|
|
}
|
|
|
|
else
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2012-08-26 12:07:48 +03:00
|
|
|
BattleHex bestNeighbor = destination;
|
|
|
|
if(distToNearestNeighbour(destination, reachability.distances, &bestNeighbor) > GameConstants::BFIELD_SIZE)
|
|
|
|
{
|
|
|
|
print("goTowards: Cannot reach");
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
BattleHex currentDest = bestNeighbor;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
assert(currentDest.isValid());
|
|
|
|
if(vstd::contains(avHexes, currentDest))
|
|
|
|
return BattleAction::makeMove(stack, currentDest);
|
|
|
|
|
|
|
|
currentDest = reachability.predecessors[currentDest];
|
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-21 16:27:50 +02:00
|
|
|
void CStupidAI::saveGame(COSer & h, const int version)
|
2013-05-09 14:09:23 +03:00
|
|
|
{
|
|
|
|
//TODO to be implemented with saving/loading during the battles
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2014-12-21 16:27:50 +02:00
|
|
|
void CStupidAI::loadGame(CISer & h, const int version)
|
2013-05-09 14:09:23 +03:00
|
|
|
{
|
|
|
|
//TODO to be implemented with saving/loading during the battles
|
|
|
|
assert(0);
|
|
|
|
}
|