2010-12-22 22:14:40 +02:00
|
|
|
#include "stdafx.h"
|
|
|
|
#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 <boost/foreach.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include "../../lib/CCreatureHandler.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/thread.hpp>
|
|
|
|
|
|
|
|
IBattleCallback * 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
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::init( IBattleCallback * CB )
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::actionFinished( const BattleAction *action )
|
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("actionFinished called");
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::actionStarted( const BattleAction *action )
|
|
|
|
{
|
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;
|
|
|
|
std::vector<THex> attackFrom; //for melee fight
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool isMoreProfitable(const EnemyInfo &ei1, const EnemyInfo& ei2)
|
|
|
|
{
|
|
|
|
return (ei1.adi-ei1.adr) < (ei2.adi - ei2.adr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int distToNearestNeighbour(THex hex, const std::vector<int> & dists, THex *chosenHex = NULL)
|
|
|
|
{
|
|
|
|
int ret = 1000000;
|
|
|
|
BOOST_FOREACH(THex n, hex.neighbouringTiles())
|
|
|
|
{
|
|
|
|
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 std::vector<int> & dists)
|
|
|
|
{
|
|
|
|
return distToNearestNeighbour(ei1.s->position, dists) < distToNearestNeighbour(ei2.s->position, dists);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool willSecondHexBlockMoreEnemyShooters(const THex &h1, const THex &h2)
|
|
|
|
{
|
|
|
|
int shooters[2] = {0}; //count of shooters on hexes
|
|
|
|
|
|
|
|
for(int i = 0; i < 2; i++)
|
|
|
|
BOOST_FOREACH(THex neighbour, (i ? h2 : h1).neighbouringTiles())
|
|
|
|
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));
|
2010-12-25 03:43:40 +02:00
|
|
|
print("activeStack called");
|
2011-02-26 19:34:23 +02:00
|
|
|
std::vector<THex> avHexes = cb->battleGetAvailableHexes(stack, false);
|
2011-01-08 20:33:40 +02:00
|
|
|
std::vector<int> dists = cb->battleGetDistances(stack);
|
|
|
|
std::vector<EnemyInfo> enemiesShootable, enemiesReachable, enemiesUnreachable;
|
|
|
|
|
2011-02-14 22:31:53 +02:00
|
|
|
BOOST_FOREACH(const CStack *s, cb->battleGetStacks(IBattleCallback::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
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(THex 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
|
|
|
|
|
|
|
if(!vstd::contains(enemiesReachable, s))
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
else
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
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)));
|
|
|
|
if(distToNearestNeighbour(ei.s->position, dists) < 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");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleEnd(const BattleResult *br)
|
|
|
|
{
|
|
|
|
print("battleEnd called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleResultsApplied()
|
|
|
|
{
|
|
|
|
print("battleResultsApplied called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleNewRoundFirst(int round)
|
|
|
|
{
|
|
|
|
print("battleNewRoundFirst called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleNewRound(int round)
|
|
|
|
{
|
|
|
|
print("battleNewRound called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleStackMoved(const CStack * stack, THex dest, int distance, bool end)
|
|
|
|
{
|
|
|
|
print("battleStackMoved called");;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleSpellCast(const BattleSpellCast *sc)
|
|
|
|
{
|
|
|
|
print("battleSpellCast called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleStacksEffectsSet(const SetStackEffect & sse)
|
|
|
|
{
|
|
|
|
print("battleStacksEffectsSet called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
|
|
|
|
{
|
|
|
|
print("battleStart called");
|
|
|
|
side = Side;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, si32 lifeDrainFrom)
|
|
|
|
{
|
|
|
|
print("battleStacksHealedRes called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleNewStackAppeared(const CStack * stack)
|
|
|
|
{
|
|
|
|
print("battleNewStackAppeared called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
|
|
|
|
{
|
|
|
|
print("battleObstaclesRemoved called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleCatapultAttacked(const CatapultAttack & ca)
|
|
|
|
{
|
|
|
|
print("battleCatapultAttacked called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
|
|
|
|
{
|
|
|
|
print("battleStacksRemoved called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::print(const std::string &text) const
|
|
|
|
{
|
|
|
|
tlog0 << "CStupidAI [" << this <<"]: " << text << std::endl;
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BattleAction CStupidAI::goTowards(const CStack * stack, THex hex)
|
|
|
|
{
|
|
|
|
THex realDest = hex;
|
2011-01-09 19:41:46 +02:00
|
|
|
THex predecessors[BFIELD_SIZE];
|
2011-01-08 20:33:40 +02:00
|
|
|
std::vector<int> dists = cb->battleGetDistances(stack, hex);
|
|
|
|
if(distToNearestNeighbour(hex, dists, &realDest) > BFIELD_SIZE)
|
|
|
|
{
|
|
|
|
print("goTowards: Cannot reach");
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
dists = cb->battleGetDistances(stack, realDest, predecessors);
|
2011-02-26 19:34:23 +02:00
|
|
|
std::vector<THex> avHexes = cb->battleGetAvailableHexes(stack, false);
|
2011-01-08 20:33:40 +02:00
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
assert(realDest.isValid());
|
|
|
|
if(vstd::contains(avHexes, hex))
|
|
|
|
return BattleAction::makeMove(stack, hex);
|
|
|
|
|
|
|
|
hex = predecessors[hex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|