|
@@ -14,6 +14,7 @@
|
|
|
#include "../../CCallback.h"
|
|
#include "../../CCallback.h"
|
|
|
#include "../../lib/CCreatureHandler.h"
|
|
#include "../../lib/CCreatureHandler.h"
|
|
|
#include "../../lib/battle/BattleAction.h"
|
|
#include "../../lib/battle/BattleAction.h"
|
|
|
|
|
+#include "../../lib/battle/BattleInfo.h"
|
|
|
|
|
|
|
|
static std::shared_ptr<CBattleCallback> cbc;
|
|
static std::shared_ptr<CBattleCallback> cbc;
|
|
|
|
|
|
|
@@ -53,12 +54,12 @@ void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::share
|
|
|
initBattleInterface(ENV, CB);
|
|
initBattleInterface(ENV, CB);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::actionFinished(const BattleAction &action)
|
|
|
|
|
|
|
+void CStupidAI::actionFinished(const BattleID & battleID, const BattleAction &action)
|
|
|
{
|
|
{
|
|
|
print("actionFinished called");
|
|
print("actionFinished called");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::actionStarted(const BattleAction &action)
|
|
|
|
|
|
|
+void CStupidAI::actionStarted(const BattleID & battleID, const BattleAction &action)
|
|
|
{
|
|
{
|
|
|
print("actionStarted called");
|
|
print("actionStarted called");
|
|
|
}
|
|
}
|
|
@@ -71,11 +72,11 @@ public:
|
|
|
std::vector<BattleHex> attackFrom; //for melee fight
|
|
std::vector<BattleHex> attackFrom; //for melee fight
|
|
|
EnemyInfo(const CStack * _s) : s(_s), adi(0), adr(0)
|
|
EnemyInfo(const CStack * _s) : s(_s), adi(0), adr(0)
|
|
|
{}
|
|
{}
|
|
|
- void calcDmg(const CStack * ourStack)
|
|
|
|
|
|
|
+ void calcDmg(const BattleID & battleID, const CStack * ourStack)
|
|
|
{
|
|
{
|
|
|
// FIXME: provide distance info for Jousting bonus
|
|
// FIXME: provide distance info for Jousting bonus
|
|
|
DamageEstimation retal;
|
|
DamageEstimation retal;
|
|
|
- DamageEstimation dmg = cbc->battleEstimateDamage(ourStack, s, 0, &retal);
|
|
|
|
|
|
|
+ DamageEstimation dmg = cbc->getBattle(battleID)->battleEstimateDamage(ourStack, s, 0, &retal);
|
|
|
adi = static_cast<int>((dmg.damage.min + dmg.damage.max) / 2);
|
|
adi = static_cast<int>((dmg.damage.min + dmg.damage.max) / 2);
|
|
|
adr = static_cast<int>((retal.damage.min + retal.damage.max) / 2);
|
|
adr = static_cast<int>((retal.damage.min + retal.damage.max) / 2);
|
|
|
}
|
|
}
|
|
@@ -91,14 +92,14 @@ bool isMoreProfitable(const EnemyInfo &ei1, const EnemyInfo& ei2)
|
|
|
return (ei1.adi-ei1.adr) < (ei2.adi - ei2.adr);
|
|
return (ei1.adi-ei1.adr) < (ei2.adi - ei2.adr);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static bool willSecondHexBlockMoreEnemyShooters(const BattleHex &h1, const BattleHex &h2)
|
|
|
|
|
|
|
+static bool willSecondHexBlockMoreEnemyShooters(const BattleID & battleID, const BattleHex &h1, const BattleHex &h2)
|
|
|
{
|
|
{
|
|
|
int shooters[2] = {0}; //count of shooters on hexes
|
|
int shooters[2] = {0}; //count of shooters on hexes
|
|
|
|
|
|
|
|
for(int i = 0; i < 2; i++)
|
|
for(int i = 0; i < 2; i++)
|
|
|
{
|
|
{
|
|
|
for (auto & neighbour : (i ? h2 : h1).neighbouringTiles())
|
|
for (auto & neighbour : (i ? h2 : h1).neighbouringTiles())
|
|
|
- if(const auto * s = cbc->battleGetUnitByPos(neighbour))
|
|
|
|
|
|
|
+ if(const auto * s = cbc->getBattle(battleID)->battleGetUnitByPos(neighbour))
|
|
|
if(s->isShooter())
|
|
if(s->isShooter())
|
|
|
shooters[i]++;
|
|
shooters[i]++;
|
|
|
}
|
|
}
|
|
@@ -106,16 +107,16 @@ static bool willSecondHexBlockMoreEnemyShooters(const BattleHex &h1, const Battl
|
|
|
return shooters[0] < shooters[1];
|
|
return shooters[0] < shooters[1];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::yourTacticPhase(int distance)
|
|
|
|
|
|
|
+void CStupidAI::yourTacticPhase(const BattleID & battleID, int distance)
|
|
|
{
|
|
{
|
|
|
- cb->battleMakeTacticAction(BattleAction::makeEndOFTacticPhase(cb->battleGetTacticsSide()));
|
|
|
|
|
|
|
+ cb->battleMakeTacticAction(battleID, BattleAction::makeEndOFTacticPhase(cb->getBattle(battleID)->battleGetTacticsSide()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::activeStack( const CStack * stack )
|
|
|
|
|
|
|
+void CStupidAI::activeStack(const BattleID & battleID, const CStack * stack)
|
|
|
{
|
|
{
|
|
|
//boost::this_thread::sleep_for(boost::chrono::seconds(2));
|
|
//boost::this_thread::sleep_for(boost::chrono::seconds(2));
|
|
|
print("activeStack called for " + stack->nodeName());
|
|
print("activeStack called for " + stack->nodeName());
|
|
|
- ReachabilityInfo dists = cb->getReachability(stack);
|
|
|
|
|
|
|
+ ReachabilityInfo dists = cb->getBattle(battleID)->getReachability(stack);
|
|
|
std::vector<EnemyInfo> enemiesShootable, enemiesReachable, enemiesUnreachable;
|
|
std::vector<EnemyInfo> enemiesShootable, enemiesReachable, enemiesUnreachable;
|
|
|
|
|
|
|
|
if(stack->creatureId() == CreatureID::CATAPULT)
|
|
if(stack->creatureId() == CreatureID::CATAPULT)
|
|
@@ -128,24 +129,24 @@ void CStupidAI::activeStack( const CStack * stack )
|
|
|
attack.side = side;
|
|
attack.side = side;
|
|
|
attack.stackNumber = stack->unitId();
|
|
attack.stackNumber = stack->unitId();
|
|
|
|
|
|
|
|
- cb->battleMakeUnitAction(attack);
|
|
|
|
|
|
|
+ cb->battleMakeUnitAction(battleID, attack);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
else if(stack->hasBonusOfType(BonusType::SIEGE_WEAPON))
|
|
else if(stack->hasBonusOfType(BonusType::SIEGE_WEAPON))
|
|
|
{
|
|
{
|
|
|
- cb->battleMakeUnitAction(BattleAction::makeDefend(stack));
|
|
|
|
|
|
|
+ cb->battleMakeUnitAction(battleID, BattleAction::makeDefend(stack));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- for (const CStack *s : cb->battleGetStacks(CBattleCallback::ONLY_ENEMY))
|
|
|
|
|
|
|
+ for (const CStack *s : cb->getBattle(battleID)->battleGetStacks(CBattleInfoEssentials::ONLY_ENEMY))
|
|
|
{
|
|
{
|
|
|
- if(cb->battleCanShoot(stack, s->getPosition()))
|
|
|
|
|
|
|
+ if(cb->getBattle(battleID)->battleCanShoot(stack, s->getPosition()))
|
|
|
{
|
|
{
|
|
|
enemiesShootable.push_back(s);
|
|
enemiesShootable.push_back(s);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- std::vector<BattleHex> avHexes = cb->battleGetAvailableHexes(stack, false);
|
|
|
|
|
|
|
+ std::vector<BattleHex> avHexes = cb->getBattle(battleID)->battleGetAvailableHexes(stack, false);
|
|
|
|
|
|
|
|
for (BattleHex hex : avHexes)
|
|
for (BattleHex hex : avHexes)
|
|
|
{
|
|
{
|
|
@@ -168,21 +169,23 @@ void CStupidAI::activeStack( const CStack * stack )
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
for ( auto & enemy : enemiesReachable )
|
|
for ( auto & enemy : enemiesReachable )
|
|
|
- enemy.calcDmg( stack );
|
|
|
|
|
|
|
+ enemy.calcDmg(battleID, stack);
|
|
|
|
|
|
|
|
for ( auto & enemy : enemiesShootable )
|
|
for ( auto & enemy : enemiesShootable )
|
|
|
- enemy.calcDmg( stack );
|
|
|
|
|
|
|
+ enemy.calcDmg(battleID, stack);
|
|
|
|
|
|
|
|
if(enemiesShootable.size())
|
|
if(enemiesShootable.size())
|
|
|
{
|
|
{
|
|
|
const EnemyInfo &ei= *std::max_element(enemiesShootable.begin(), enemiesShootable.end(), isMoreProfitable);
|
|
const EnemyInfo &ei= *std::max_element(enemiesShootable.begin(), enemiesShootable.end(), isMoreProfitable);
|
|
|
- cb->battleMakeUnitAction(BattleAction::makeShotAttack(stack, ei.s));
|
|
|
|
|
|
|
+ cb->battleMakeUnitAction(battleID, BattleAction::makeShotAttack(stack, ei.s));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
else if(enemiesReachable.size())
|
|
else if(enemiesReachable.size())
|
|
|
{
|
|
{
|
|
|
const EnemyInfo &ei= *std::max_element(enemiesReachable.begin(), enemiesReachable.end(), &isMoreProfitable);
|
|
const EnemyInfo &ei= *std::max_element(enemiesReachable.begin(), enemiesReachable.end(), &isMoreProfitable);
|
|
|
- cb->battleMakeUnitAction(BattleAction::makeMeleeAttack(stack, ei.s->getPosition(), *std::max_element(ei.attackFrom.begin(), ei.attackFrom.end(), &willSecondHexBlockMoreEnemyShooters)));
|
|
|
|
|
|
|
+ BattleHex targetHex = *std::max_element(ei.attackFrom.begin(), ei.attackFrom.end(), [&](auto a, auto b) { return willSecondHexBlockMoreEnemyShooters(battleID, a, b);});
|
|
|
|
|
+
|
|
|
|
|
+ cb->battleMakeUnitAction(battleID, BattleAction::makeMeleeAttack(stack, ei.s->getPosition(), targetHex));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
else if(enemiesUnreachable.size()) //due to #955 - a buggy battle may occur when there are no enemies
|
|
else if(enemiesUnreachable.size()) //due to #955 - a buggy battle may occur when there are no enemies
|
|
@@ -194,26 +197,26 @@ void CStupidAI::activeStack( const CStack * stack )
|
|
|
|
|
|
|
|
if(dists.distToNearestNeighbour(stack, closestEnemy->s) < GameConstants::BFIELD_SIZE)
|
|
if(dists.distToNearestNeighbour(stack, closestEnemy->s) < GameConstants::BFIELD_SIZE)
|
|
|
{
|
|
{
|
|
|
- cb->battleMakeUnitAction(goTowards(stack, closestEnemy->s->getAttackableHexes(stack)));
|
|
|
|
|
|
|
+ cb->battleMakeUnitAction(battleID, goTowards(battleID, stack, closestEnemy->s->getAttackableHexes(stack)));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- cb->battleMakeUnitAction(BattleAction::makeDefend(stack));
|
|
|
|
|
|
|
+ cb->battleMakeUnitAction(battleID, BattleAction::makeDefend(stack));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::battleAttack(const BattleAttack *ba)
|
|
|
|
|
|
|
+void CStupidAI::battleAttack(const BattleID & battleID, const BattleAttack *ba)
|
|
|
{
|
|
{
|
|
|
print("battleAttack called");
|
|
print("battleAttack called");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa, bool ranged)
|
|
|
|
|
|
|
+void CStupidAI::battleStacksAttacked(const BattleID & battleID, const std::vector<BattleStackAttacked> & bsa, bool ranged)
|
|
|
{
|
|
{
|
|
|
print("battleStacksAttacked called");
|
|
print("battleStacksAttacked called");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::battleEnd(const BattleResult *br, QueryID queryID)
|
|
|
|
|
|
|
+void CStupidAI::battleEnd(const BattleID & battleID, const BattleResult *br, QueryID queryID)
|
|
|
{
|
|
{
|
|
|
print("battleEnd called");
|
|
print("battleEnd called");
|
|
|
}
|
|
}
|
|
@@ -223,38 +226,38 @@ void CStupidAI::battleEnd(const BattleResult *br, QueryID queryID)
|
|
|
// print("battleResultsApplied called");
|
|
// print("battleResultsApplied called");
|
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
-void CStupidAI::battleNewRoundFirst(int round)
|
|
|
|
|
|
|
+void CStupidAI::battleNewRoundFirst(const BattleID & battleID, int round)
|
|
|
{
|
|
{
|
|
|
print("battleNewRoundFirst called");
|
|
print("battleNewRoundFirst called");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::battleNewRound(int round)
|
|
|
|
|
|
|
+void CStupidAI::battleNewRound(const BattleID & battleID, int round)
|
|
|
{
|
|
{
|
|
|
print("battleNewRound called");
|
|
print("battleNewRound called");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance, bool teleport)
|
|
|
|
|
|
|
+void CStupidAI::battleStackMoved(const BattleID & battleID, const CStack * stack, std::vector<BattleHex> dest, int distance, bool teleport)
|
|
|
{
|
|
{
|
|
|
print("battleStackMoved called");
|
|
print("battleStackMoved called");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::battleSpellCast(const BattleSpellCast *sc)
|
|
|
|
|
|
|
+void CStupidAI::battleSpellCast(const BattleID & battleID, const BattleSpellCast *sc)
|
|
|
{
|
|
{
|
|
|
print("battleSpellCast called");
|
|
print("battleSpellCast called");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::battleStacksEffectsSet(const SetStackEffect & sse)
|
|
|
|
|
|
|
+void CStupidAI::battleStacksEffectsSet(const BattleID & battleID, const SetStackEffect & sse)
|
|
|
{
|
|
{
|
|
|
print("battleStacksEffectsSet called");
|
|
print("battleStacksEffectsSet called");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side, bool replayAllowed)
|
|
|
|
|
|
|
+void CStupidAI::battleStart(const BattleID & battleID, const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side, bool replayAllowed)
|
|
|
{
|
|
{
|
|
|
print("battleStart called");
|
|
print("battleStart called");
|
|
|
side = Side;
|
|
side = Side;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void CStupidAI::battleCatapultAttacked(const CatapultAttack & ca)
|
|
|
|
|
|
|
+void CStupidAI::battleCatapultAttacked(const BattleID & battleID, const CatapultAttack & ca)
|
|
|
{
|
|
{
|
|
|
print("battleCatapultAttacked called");
|
|
print("battleCatapultAttacked called");
|
|
|
}
|
|
}
|
|
@@ -264,10 +267,10 @@ void CStupidAI::print(const std::string &text) const
|
|
|
logAi->trace("CStupidAI [%p]: %s", this, text);
|
|
logAi->trace("CStupidAI [%p]: %s", this, text);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-BattleAction CStupidAI::goTowards(const CStack * stack, std::vector<BattleHex> hexes) const
|
|
|
|
|
|
|
+BattleAction CStupidAI::goTowards(const BattleID & battleID, const CStack * stack, std::vector<BattleHex> hexes) const
|
|
|
{
|
|
{
|
|
|
- auto reachability = cb->getReachability(stack);
|
|
|
|
|
- auto avHexes = cb->battleGetAvailableHexes(reachability, stack, false);
|
|
|
|
|
|
|
+ auto reachability = cb->getBattle(battleID)->getReachability(stack);
|
|
|
|
|
+ auto avHexes = cb->getBattle(battleID)->battleGetAvailableHexes(reachability, stack, false);
|
|
|
|
|
|
|
|
if(!avHexes.size() || !hexes.size()) //we are blocked or dest is blocked
|
|
if(!avHexes.size() || !hexes.size()) //we are blocked or dest is blocked
|
|
|
{
|
|
{
|