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"
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
CPlayerBattleCallback * cbc;
|
2010-12-22 22:14:40 +02:00
|
|
|
|
|
|
|
CStupidAI::CStupidAI(void)
|
2010-12-25 03:43:40 +02:00
|
|
|
: side(-1), cb(NULL)
|
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
|
|
|
}
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
void CStupidAI::init( 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);
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
int distToNearestNeighbour(BattleHex hex, const ReachabilityInfo::TDistances& dists, BattleHex *chosenHex = NULL)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
int ret = 1000000;
|
2011-12-22 16:05:19 +03:00
|
|
|
BOOST_FOREACH(BattleHex 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);
|
|
|
|
}
|
|
|
|
|
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++)
|
2011-12-22 16:05:19 +03:00
|
|
|
BOOST_FOREACH(BattleHex 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;
|
|
|
|
static const int wallHexes[] = {50, 183, 182, 130, 62, 29, 12, 95};
|
|
|
|
attack.destinationTile = wallHexes[ rand()%ARRAY_COUNT(wallHexes) ];
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-03 06:14:18 +03:00
|
|
|
BOOST_FOREACH(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);
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
BOOST_FOREACH(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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());
|
2011-01-08 20:33:40 +02:00
|
|
|
const EnemyInfo &ei= *std::min_element(enemiesUnreachable.begin(), enemiesUnreachable.end(), boost::bind(isCloser, _1, _2, boost::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
|
|
|
|
{
|
2013-04-10 19:28:14 +03: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
|
|
|
{
|
2013-04-10 19:28:14 +03: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 14:09:23 +03:00
|
|
|
void CStupidAI::saveGame(COSer<CSaveFile> &h, const int version)
|
|
|
|
{
|
|
|
|
//TODO to be implemented with saving/loading during the battles
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::loadGame(CISer<CLoadFile> &h, const int version)
|
|
|
|
{
|
|
|
|
//TODO to be implemented with saving/loading during the battles
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|