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"
|
2022-01-27 09:00:27 +02:00
|
|
|
#include "BattleExchangeVariant.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2016-10-28 16:16:46 +02:00
|
|
|
#include "StackWithBonuses.h"
|
|
|
|
#include "EnemyInfo.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "../../lib/CStopWatch.h"
|
|
|
|
#include "../../lib/CThreadHelper.h"
|
2020-11-28 17:11:33 +02:00
|
|
|
#include "../../lib/mapObjects/CGTownInstance.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "../../lib/spells/CSpellHandler.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "../../lib/spells/ISpellMechanics.h"
|
2022-10-14 10:24:29 +02:00
|
|
|
#include "../../lib/battle/BattleStateInfoForRetreat.h"
|
2023-03-20 11:41:36 +02:00
|
|
|
#include "../../lib/battle/CObstacleInstance.h"
|
2020-05-09 13:09:32 +02:00
|
|
|
#include "../../lib/CStack.h" // TODO: remove
|
2021-02-15 14:03:32 +02:00
|
|
|
// Eventually only IBattleInfoCallback and battle::Unit should be used,
|
2020-05-09 13:09:32 +02:00
|
|
|
// CUnitState should be private and CStack should be removed completely
|
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
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
enum class SpellTypes
|
|
|
|
{
|
|
|
|
ADVENTURE, BATTLE, OTHER
|
|
|
|
};
|
|
|
|
|
|
|
|
SpellTypes spellType(const CSpell * spell)
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
if(!spell->isCombat() || spell->isCreatureAbility())
|
2017-07-20 06:08:49 +02:00
|
|
|
return SpellTypes::OTHER;
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
if(spell->isOffensive() || spell->hasEffects() || spell->hasBattleEffects())
|
2017-07-20 06:08:49 +02:00
|
|
|
return SpellTypes::BATTLE;
|
|
|
|
|
|
|
|
return SpellTypes::OTHER;
|
|
|
|
}
|
|
|
|
|
2020-11-28 17:11:33 +02:00
|
|
|
std::vector<BattleHex> CBattleAI::getBrokenWallMoatHexes() const
|
|
|
|
{
|
|
|
|
std::vector<BattleHex> result;
|
2021-02-15 14:03:32 +02:00
|
|
|
|
2023-01-13 00:35:58 +02:00
|
|
|
for(EWallPart wallPart : { EWallPart::BOTTOM_WALL, EWallPart::BELOW_GATE, EWallPart::OVER_GATE, EWallPart::UPPER_WALL })
|
2020-11-28 17:11:33 +02:00
|
|
|
{
|
|
|
|
auto state = cb->battleGetWallState(wallPart);
|
|
|
|
|
|
|
|
if(state != EWallState::DESTROYED)
|
|
|
|
continue;
|
2021-02-15 14:03:32 +02:00
|
|
|
|
2023-01-13 00:35:58 +02:00
|
|
|
auto wallHex = cb->wallPartToBattleHex((EWallPart)wallPart);
|
2020-11-28 17:11:33 +02:00
|
|
|
auto moatHex = wallHex.cloneInDirection(BattleHex::LEFT);
|
|
|
|
|
|
|
|
result.push_back(moatHex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-13 10:43:26 +02:00
|
|
|
CBattleAI::CBattleAI()
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
: side(-1),
|
|
|
|
wasWaitingForRealize(false),
|
|
|
|
wasUnlockingGs(false)
|
2012-09-24 02:10:56 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-13 10:43:26 +02:00
|
|
|
CBattleAI::~CBattleAI()
|
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
|
|
|
|
2022-12-07 21:50:45 +02:00
|
|
|
void CBattleAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB)
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2016-10-28 16:16:46 +02:00
|
|
|
setCbc(CB);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
env = ENV;
|
2016-10-28 16:16:46 +02:00
|
|
|
cb = CB;
|
2017-07-12 21:01:10 +02:00
|
|
|
playerID = *CB->getPlayerID(); //TODO should be sth in callback
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
wasWaitingForRealize = CB->waitTillRealize;
|
2013-06-21 23:59:32 +03:00
|
|
|
wasUnlockingGs = CB->unlockGsWhenWaiting;
|
|
|
|
CB->waitTillRealize = true;
|
|
|
|
CB->unlockGsWhenWaiting = false;
|
2023-04-08 13:05:47 +02:00
|
|
|
movesSkippedByDefense = 0;
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
2012-09-29 17:44:06 +03:00
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
BattleAction CBattleAI::activeStack( const CStack * stack )
|
|
|
|
{
|
2022-01-27 09:00:27 +02:00
|
|
|
LOG_TRACE_PARAMS(logAi, "stack: %s", stack->nodeName());
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
BattleAction result = BattleAction::makeDefend(stack);
|
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)
|
2022-04-11 08:12:41 +02:00
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
try
|
|
|
|
{
|
2023-04-27 19:43:20 +02:00
|
|
|
if(stack->creatureId() == 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();
|
|
|
|
|
2023-02-28 09:14:01 +02:00
|
|
|
if(cb->battleIsFinished() || !stack->alive())
|
2016-09-22 19:51:13 +02:00
|
|
|
{
|
2023-02-28 09:14:01 +02:00
|
|
|
//spellcast may finish battle or kill active stack
|
2016-09-22 19:51:13 +02:00
|
|
|
//send special preudo-action
|
|
|
|
BattleAction cancel;
|
2017-07-20 06:08:49 +02:00
|
|
|
cancel.actionType = EActionType::CANCEL;
|
2016-09-22 19:51:13 +02:00
|
|
|
return cancel;
|
|
|
|
}
|
2013-06-21 23:59:32 +03:00
|
|
|
|
2013-06-17 18:45:55 +03:00
|
|
|
if(auto action = considerFleeingOrSurrendering())
|
|
|
|
return *action;
|
2017-07-20 06:08:49 +02:00
|
|
|
//best action is from effective owner point if view, we are effective owner as we received "activeStack"
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
|
2019-05-04 05:42:55 +02:00
|
|
|
//evaluate casting spell for spellcasting stack
|
2023-04-16 19:42:56 +02:00
|
|
|
std::optional<PossibleSpellcast> bestSpellcast(std::nullopt);
|
2019-05-04 05:42:55 +02:00
|
|
|
//TODO: faerie dragon type spell should be selected by server
|
|
|
|
SpellID creatureSpellToCast = cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), stack, CBattleInfoCallback::RANDOM_AIMED);
|
|
|
|
if(stack->hasBonusOfType(Bonus::SPELLCASTER) && stack->canCast() && creatureSpellToCast != SpellID::NONE)
|
|
|
|
{
|
|
|
|
const CSpell * spell = creatureSpellToCast.toSpell();
|
|
|
|
|
|
|
|
if(spell->canBeCast(getCbc().get(), spells::Mode::CREATURE_ACTIVE, stack))
|
|
|
|
{
|
|
|
|
std::vector<PossibleSpellcast> possibleCasts;
|
|
|
|
spells::BattleCast temp(getCbc().get(), stack, spells::Mode::CREATURE_ACTIVE, spell);
|
|
|
|
for(auto & target : temp.findPotentialTargets())
|
|
|
|
{
|
|
|
|
PossibleSpellcast ps;
|
|
|
|
ps.dest = target;
|
|
|
|
ps.spell = spell;
|
|
|
|
evaluateCreatureSpellcast(stack, ps);
|
|
|
|
possibleCasts.push_back(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(possibleCasts.begin(), possibleCasts.end(), [&](const PossibleSpellcast & lhs, const PossibleSpellcast & rhs) { return lhs.value > rhs.value; });
|
|
|
|
if(!possibleCasts.empty() && possibleCasts.front().value > 0)
|
|
|
|
{
|
2023-04-16 19:42:56 +02:00
|
|
|
bestSpellcast = std::optional<PossibleSpellcast>(possibleCasts.front());
|
2019-05-04 05:42:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
HypotheticBattle hb(env.get(), cb);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
PotentialTargets targets(stack, hb);
|
2022-01-27 09:00:27 +02:00
|
|
|
BattleExchangeEvaluator scoreEvaluator(cb, env);
|
2022-04-11 08:12:41 +02:00
|
|
|
auto moveTarget = scoreEvaluator.findMoveTowardsUnreachable(stack, targets, hb);
|
|
|
|
|
|
|
|
int64_t score = EvaluationResult::INEFFECTIVE_SCORE;
|
2019-06-28 20:05:25 +02:00
|
|
|
|
|
|
|
if(!targets.possibleAttacks.empty())
|
2012-09-14 02:42:11 +03:00
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2022-01-27 09:00:27 +02:00
|
|
|
logAi->trace("Evaluating attack for %s", stack->getDescription());
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2019-05-04 05:42:55 +02:00
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
auto evaluationResult = scoreEvaluator.findBestTarget(stack, targets, hb);
|
|
|
|
auto & bestAttack = evaluationResult.bestAttack;
|
2022-04-11 08:12:41 +02:00
|
|
|
|
2019-05-04 05:42:55 +02:00
|
|
|
//TODO: consider more complex spellcast evaluation, f.e. because "re-retaliation" during enemy move in same turn for melee attack etc.
|
2023-04-16 19:42:56 +02:00
|
|
|
if(bestSpellcast.has_value() && bestSpellcast->value > bestAttack.damageDiff())
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
// return because spellcast value is damage dealt and score is dps reduce
|
2023-04-08 13:05:47 +02:00
|
|
|
movesSkippedByDefense = 0;
|
2022-04-11 08:12:41 +02:00
|
|
|
return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
if(evaluationResult.score > score)
|
2021-05-16 19:53:11 +02:00
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
score = evaluationResult.score;
|
|
|
|
std::string action;
|
2021-05-16 19:53:11 +02:00
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
if(evaluationResult.wait)
|
|
|
|
{
|
|
|
|
result = BattleAction::makeWait(stack);
|
|
|
|
action = "wait";
|
|
|
|
}
|
|
|
|
else if(bestAttack.attack.shooting)
|
|
|
|
{
|
|
|
|
result = BattleAction::makeShotAttack(stack, bestAttack.attack.defender);
|
|
|
|
action = "shot";
|
2023-04-08 13:05:47 +02:00
|
|
|
movesSkippedByDefense = 0;
|
2022-04-11 08:12:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = BattleAction::makeMeleeAttack(stack, bestAttack.attack.defender->getPosition(), bestAttack.from);
|
2022-10-17 18:47:16 +02:00
|
|
|
action = "melee";
|
2023-04-08 13:05:47 +02:00
|
|
|
movesSkippedByDefense = 0;
|
2022-04-11 08:12:41 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
logAi->debug("BattleAI: %s -> %s x %d, %s, from %d curpos %d dist %d speed %d: +%lld -%lld = %lld",
|
2023-01-02 18:00:51 +02:00
|
|
|
bestAttack.attackerState->unitType()->getJsonKey(),
|
|
|
|
bestAttack.affectedUnits[0]->unitType()->getJsonKey(),
|
2022-04-11 08:12:41 +02:00
|
|
|
(int)bestAttack.affectedUnits[0]->getCount(), action, (int)bestAttack.from, (int)bestAttack.attack.attacker->getPosition().hex,
|
2023-01-14 19:03:18 +02:00
|
|
|
bestAttack.attack.chargeDistance, bestAttack.attack.attacker->Speed(0, true),
|
2022-10-17 18:47:16 +02:00
|
|
|
bestAttack.defenderDamageReduce, bestAttack.attackerDamageReduce, bestAttack.attackValue()
|
2019-06-28 20:05:25 +02:00
|
|
|
);
|
2022-04-11 08:12:41 +02:00
|
|
|
}
|
2019-05-04 05:42:55 +02:00
|
|
|
}
|
2023-04-16 19:42:56 +02:00
|
|
|
else if(bestSpellcast.has_value())
|
2019-05-04 05:42:55 +02:00
|
|
|
{
|
2023-04-08 13:05:47 +02:00
|
|
|
movesSkippedByDefense = 0;
|
2019-05-04 05:42:55 +02:00
|
|
|
return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
//ThreatMap threatsToUs(stack); // These lines may be usefull but they are't used in the code.
|
|
|
|
if(moveTarget.score > score)
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
score = moveTarget.score;
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
if(stack->waited())
|
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
result = goTowardsNearest(stack, moveTarget.positions);
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
result = BattleAction::makeWait(stack);
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
2012-09-14 02:42:11 +03:00
|
|
|
}
|
2020-11-28 17:11:33 +02:00
|
|
|
|
2023-04-08 13:05:47 +02:00
|
|
|
if(score <= EvaluationResult::INEFFECTIVE_SCORE
|
|
|
|
&& !stack->hasBonusOfType(Bonus::FLYING)
|
2020-11-28 17:11:33 +02:00
|
|
|
&& stack->unitSide() == BattleSide::ATTACKER
|
|
|
|
&& cb->battleGetSiegeLevel() >= CGTownInstance::CITADEL)
|
|
|
|
{
|
|
|
|
auto brokenWallMoat = getBrokenWallMoatHexes();
|
|
|
|
|
|
|
|
if(brokenWallMoat.size())
|
|
|
|
{
|
2023-04-08 13:05:47 +02:00
|
|
|
movesSkippedByDefense = 0;
|
|
|
|
|
2020-11-28 17:11:33 +02:00
|
|
|
if(stack->doubleWide() && vstd::contains(brokenWallMoat, stack->getPosition()))
|
2023-04-08 13:05:47 +02:00
|
|
|
result = BattleAction::makeMove(stack, stack->getPosition().cloneInDirection(BattleHex::RIGHT));
|
2020-11-28 17:11:33 +02:00
|
|
|
else
|
2023-04-08 13:05:47 +02:00
|
|
|
result = goTowardsNearest(stack, brokenWallMoat);
|
2022-04-11 08:12:41 +02:00
|
|
|
}
|
2020-11-28 17:11:33 +02:00
|
|
|
}
|
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
|
|
|
}
|
2020-11-28 17:11:33 +02:00
|
|
|
|
2023-04-08 13:05:47 +02:00
|
|
|
if(result.actionType == EActionType::DEFEND)
|
|
|
|
{
|
|
|
|
movesSkippedByDefense++;
|
|
|
|
}
|
|
|
|
else if(result.actionType != EActionType::WAIT)
|
|
|
|
{
|
|
|
|
movesSkippedByDefense = 0;
|
|
|
|
}
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
return result;
|
2012-09-14 02:42:11 +03:00
|
|
|
}
|
|
|
|
|
2020-11-28 17:11:33 +02:00
|
|
|
BattleAction CBattleAI::goTowardsNearest(const CStack * stack, std::vector<BattleHex> hexes) const
|
2012-09-14 02:42:11 +03:00
|
|
|
{
|
|
|
|
auto reachability = cb->getReachability(stack);
|
2023-04-20 23:24:37 +02:00
|
|
|
auto avHexes = cb->battleGetAvailableHexes(reachability, stack, false);
|
2020-11-23 08:40:36 +02:00
|
|
|
|
2020-11-28 17:11:33 +02:00
|
|
|
if(!avHexes.size() || !hexes.size()) //we are blocked or dest is blocked
|
2012-09-14 02:42:11 +03:00
|
|
|
{
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
2020-11-23 08:40:36 +02:00
|
|
|
|
2020-11-28 17:11:33 +02:00
|
|
|
std::sort(hexes.begin(), hexes.end(), [&](BattleHex h1, BattleHex h2) -> bool
|
2012-09-14 02:42:11 +03:00
|
|
|
{
|
2020-11-28 17:11:33 +02:00
|
|
|
return reachability.distances[h1] < reachability.distances[h2];
|
|
|
|
});
|
|
|
|
|
|
|
|
for(auto hex : hexes)
|
|
|
|
{
|
|
|
|
if(vstd::contains(avHexes, hex))
|
2023-04-08 13:05:47 +02:00
|
|
|
{
|
2020-11-28 17:11:33 +02:00
|
|
|
return BattleAction::makeMove(stack, hex);
|
2023-04-08 13:05:47 +02:00
|
|
|
}
|
2020-11-28 17:11:33 +02:00
|
|
|
|
|
|
|
if(stack->coversPos(hex))
|
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
logAi->warn("Warning: already standing on neighbouring tile!");
|
|
|
|
//We shouldn't even be here...
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
2012-09-14 02:42:11 +03:00
|
|
|
}
|
2020-11-23 08:40:36 +02:00
|
|
|
|
2020-11-28 17:11:33 +02:00
|
|
|
BattleHex bestNeighbor = hexes.front();
|
|
|
|
|
|
|
|
if(reachability.distances[bestNeighbor] > GameConstants::BFIELD_SIZE)
|
2020-11-23 08:40:36 +02:00
|
|
|
{
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
BattleExchangeEvaluator scoreEvaluator(cb, env);
|
|
|
|
HypotheticBattle hb(env.get(), cb);
|
|
|
|
|
|
|
|
scoreEvaluator.updateReachabilityMap(hb);
|
|
|
|
|
2012-09-14 02:42:11 +03:00
|
|
|
if(stack->hasBonusOfType(Bonus::FLYING))
|
|
|
|
{
|
2023-03-20 11:41:36 +02:00
|
|
|
std::set<BattleHex> obstacleHexes;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-03-30 23:49:51 +02:00
|
|
|
auto insertAffected = [](const CObstacleInstance & spellObst, std::set<BattleHex> obstacleHexes) {
|
|
|
|
auto affectedHexes = spellObst.getAffectedTiles();
|
2023-03-20 11:41:36 +02:00
|
|
|
obstacleHexes.insert(affectedHexes.cbegin(), affectedHexes.cend());
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto & obstacles = hb.battleGetAllObstacles();
|
|
|
|
|
|
|
|
for (const auto & obst: obstacles) {
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-03-30 23:49:51 +02:00
|
|
|
if(obst->triggersEffects())
|
2023-03-20 11:41:36 +02:00
|
|
|
{
|
2023-03-30 23:49:51 +02:00
|
|
|
auto triggerAbility = VLC->spells()->getById(obst->getTrigger());
|
2023-03-20 11:41:36 +02:00
|
|
|
auto triggerIsNegative = triggerAbility->isNegative() || triggerAbility->isDamage();
|
|
|
|
|
|
|
|
if(triggerIsNegative)
|
2023-03-30 23:49:51 +02:00
|
|
|
insertAffected(*obst, obstacleHexes);
|
2023-03-20 11:41:36 +02:00
|
|
|
}
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
2012-09-14 02:42:11 +03:00
|
|
|
// 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.
|
2020-11-23 08:40:36 +02:00
|
|
|
auto nearestAvailableHex = vstd::minElementByFun(avHexes, [&](BattleHex hex) -> int
|
2012-09-14 02:42:11 +03:00
|
|
|
{
|
2023-03-20 11:41:36 +02:00
|
|
|
const int NEGATIVE_OBSTACLE_PENALTY = 100; // avoid landing on negative obstacle (moat, fire wall, etc)
|
2022-10-17 18:47:16 +02:00
|
|
|
const int BLOCKED_STACK_PENALTY = 100; // avoid landing on moat
|
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
auto distance = BattleHex::getDistance(bestNeighbor, hex);
|
|
|
|
|
2023-03-20 11:41:36 +02:00
|
|
|
if(vstd::contains(obstacleHexes, hex))
|
|
|
|
distance += NEGATIVE_OBSTACLE_PENALTY;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
return scoreEvaluator.checkPositionBlocksOurStacks(hb, stack, hex) ? BLOCKED_STACK_PENALTY + distance : distance;
|
2020-11-23 08:40:36 +02:00
|
|
|
});
|
|
|
|
|
2012-09-14 02:42:11 +03:00
|
|
|
return BattleAction::makeMove(stack, *nearestAvailableHex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BattleHex currentDest = bestNeighbor;
|
|
|
|
while(1)
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
if(!currentDest.isValid())
|
|
|
|
{
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
if(vstd::contains(avHexes, currentDest)
|
|
|
|
&& !scoreEvaluator.checkPositionBlocksOurStacks(hb, stack, currentDest))
|
2012-09-14 02:42:11 +03:00
|
|
|
return BattleAction::makeMove(stack, currentDest);
|
2020-11-23 08:40:36 +02:00
|
|
|
|
2012-09-14 02:42:11 +03:00
|
|
|
currentDest = reachability.predecessors[currentDest];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
BattleAction CBattleAI::useCatapult(const CStack * stack)
|
|
|
|
{
|
2020-11-28 17:11:33 +02:00
|
|
|
BattleAction attack;
|
|
|
|
BattleHex targetHex = BattleHex::INVALID;
|
|
|
|
|
|
|
|
if(cb->battleGetGateState() == EGateState::CLOSED)
|
|
|
|
{
|
|
|
|
targetHex = cb->wallPartToBattleHex(EWallPart::GATE);
|
2021-02-15 14:03:32 +02:00
|
|
|
}
|
2020-11-28 17:11:33 +02:00
|
|
|
else
|
|
|
|
{
|
2023-01-13 00:35:58 +02:00
|
|
|
EWallPart wallParts[] = {
|
2020-11-28 17:11:33 +02:00
|
|
|
EWallPart::KEEP,
|
|
|
|
EWallPart::BOTTOM_TOWER,
|
|
|
|
EWallPart::UPPER_TOWER,
|
|
|
|
EWallPart::BELOW_GATE,
|
|
|
|
EWallPart::OVER_GATE,
|
|
|
|
EWallPart::BOTTOM_WALL,
|
|
|
|
EWallPart::UPPER_WALL
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto wallPart : wallParts)
|
|
|
|
{
|
|
|
|
auto wallState = cb->battleGetWallState(wallPart);
|
|
|
|
|
2023-01-13 01:09:24 +02:00
|
|
|
if(wallState == EWallState::REINFORCED || wallState == EWallState::INTACT || wallState == EWallState::DAMAGED)
|
2020-11-28 17:11:33 +02:00
|
|
|
{
|
|
|
|
targetHex = cb->wallPartToBattleHex(wallPart);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!targetHex.isValid())
|
|
|
|
{
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
attack.aimToHex(targetHex);
|
|
|
|
attack.actionType = EActionType::CATAPULT;
|
|
|
|
attack.side = side;
|
2023-04-27 19:43:20 +02:00
|
|
|
attack.stackNumber = stack->unitId();
|
2020-11-28 17:11:33 +02:00
|
|
|
|
2023-04-08 13:05:47 +02:00
|
|
|
movesSkippedByDefense = 0;
|
|
|
|
|
2020-11-28 17:11:33 +02:00
|
|
|
return attack;
|
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-07-20 06:08:49 +02:00
|
|
|
if(cb->battleCanCastSpell(hero, spells::Mode::HERO) != 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;
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
vstd::copy_if(VLC->spellh->objects, std::back_inserter(possibleSpells), [hero, this](const CSpell *s) -> bool
|
2014-03-07 16:21:09 +03:00
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
return s->canBeCast(cb.get(), spells::Mode::HERO, hero);
|
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)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
return spellType(s) != SpellTypes::BATTLE;
|
|
|
|
});
|
|
|
|
|
|
|
|
LOGFL("I know how %d of them works.", possibleSpells.size());
|
2012-09-20 19:55:21 +03:00
|
|
|
|
|
|
|
//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
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
spells::BattleCast temp(cb.get(), hero, spells::Mode::HERO, spell);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
for(auto & target : temp.findPotentialTargets())
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
PossibleSpellcast ps;
|
|
|
|
ps.dest = target;
|
|
|
|
ps.spell = spell;
|
2012-09-20 19:55:21 +03:00
|
|
|
possibleCasts.push_back(ps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOGFL("Found %d spell-target combinations.", possibleCasts.size());
|
|
|
|
if(possibleCasts.empty())
|
|
|
|
return;
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
using ValueMap = PossibleSpellcast::ValueMap;
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
auto evaluateQueue = [&](ValueMap & values, const std::vector<battle::Units> & queue, HypotheticBattle & state, size_t minTurnSpan, bool * enemyHadTurnOut) -> bool
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
bool firstRound = true;
|
|
|
|
bool enemyHadTurn = false;
|
|
|
|
size_t ourTurnSpan = 0;
|
|
|
|
|
|
|
|
bool stop = false;
|
|
|
|
|
|
|
|
for(auto & round : queue)
|
|
|
|
{
|
|
|
|
if(!firstRound)
|
2022-10-17 18:47:16 +02:00
|
|
|
state.nextRound(0);//todo: set actual value?
|
2017-07-20 06:08:49 +02:00
|
|
|
for(auto unit : round)
|
|
|
|
{
|
|
|
|
if(!vstd::contains(values, unit->unitId()))
|
|
|
|
values[unit->unitId()] = 0;
|
|
|
|
|
|
|
|
if(!unit->alive())
|
|
|
|
continue;
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
if(state.battleGetOwner(unit) != playerID)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
enemyHadTurn = true;
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
if(!firstRound || state.battleCastSpells(unit->unitSide()) == 0)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
//enemy could counter our spell at this point
|
|
|
|
//anyway, we do not know what enemy will do
|
|
|
|
//just stop evaluation
|
|
|
|
stop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!enemyHadTurn)
|
|
|
|
{
|
|
|
|
ourTurnSpan++;
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
state.nextTurn(unit->unitId());
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
PotentialTargets pt(unit, state);
|
|
|
|
|
|
|
|
if(!pt.possibleAttacks.empty())
|
|
|
|
{
|
|
|
|
AttackPossibility ap = pt.bestAction();
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
auto swb = state.getForUpdate(unit->unitId());
|
2017-07-20 06:08:49 +02:00
|
|
|
*swb = *ap.attackerState;
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
if(ap.defenderDamageReduce > 0)
|
2017-07-20 06:08:49 +02:00
|
|
|
swb->removeUnitBonus(Bonus::UntilAttack);
|
2022-10-17 18:47:16 +02:00
|
|
|
if(ap.attackerDamageReduce > 0)
|
2017-07-20 06:08:49 +02:00
|
|
|
swb->removeUnitBonus(Bonus::UntilBeingAttacked);
|
|
|
|
|
|
|
|
for(auto affected : ap.affectedUnits)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
swb = state.getForUpdate(affected->unitId());
|
2017-07-20 06:08:49 +02:00
|
|
|
*swb = *affected;
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
if(ap.defenderDamageReduce > 0)
|
2017-07-20 06:08:49 +02:00
|
|
|
swb->removeUnitBonus(Bonus::UntilBeingAttacked);
|
2022-10-17 18:47:16 +02:00
|
|
|
if(ap.attackerDamageReduce > 0 && ap.attack.defender->unitId() == affected->unitId())
|
2017-07-20 06:08:49 +02:00
|
|
|
swb->removeUnitBonus(Bonus::UntilAttack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto bav = pt.bestActionValue();
|
|
|
|
|
|
|
|
//best action is from effective owner`s point if view, we need to convert to our point if view
|
2022-10-17 18:47:16 +02:00
|
|
|
if(state.battleGetOwner(unit) != playerID)
|
2017-07-20 06:08:49 +02:00
|
|
|
bav = -bav;
|
|
|
|
values[unit->unitId()] += bav;
|
|
|
|
}
|
|
|
|
|
|
|
|
firstRound = false;
|
|
|
|
|
|
|
|
if(stop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(enemyHadTurnOut)
|
|
|
|
*enemyHadTurnOut = enemyHadTurn;
|
|
|
|
|
2018-03-01 21:18:01 +02:00
|
|
|
return ourTurnSpan >= minTurnSpan;
|
2017-07-20 06:08:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ValueMap valueOfStack;
|
|
|
|
ValueMap healthOfStack;
|
|
|
|
|
|
|
|
TStacks all = cb->battleGetAllStacks(false);
|
|
|
|
|
2018-03-01 21:18:01 +02:00
|
|
|
size_t ourRemainingTurns = 0;
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
for(auto unit : all)
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
healthOfStack[unit->unitId()] = unit->getAvailableHealth();
|
|
|
|
valueOfStack[unit->unitId()] = 0;
|
2018-03-01 21:18:01 +02:00
|
|
|
|
|
|
|
if(cb->battleGetOwner(unit) == playerID && unit->canMove() && !unit->moved())
|
|
|
|
ourRemainingTurns++;
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 21:18:01 +02:00
|
|
|
LOGFL("I have %d turns left in this round", ourRemainingTurns);
|
|
|
|
|
|
|
|
const bool castNow = ourRemainingTurns <= 1;
|
|
|
|
|
|
|
|
if(castNow)
|
|
|
|
print("I should try to cast a spell now");
|
|
|
|
else
|
|
|
|
print("I could wait better moment to cast a spell");
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
auto amount = all.size();
|
|
|
|
|
|
|
|
std::vector<battle::Units> turnOrder;
|
|
|
|
|
|
|
|
cb->battleGetTurnOrder(turnOrder, amount, 2); //no more than 1 turn after current, each unit at least once
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
bool enemyHadTurn = false;
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
HypotheticBattle state(env.get(), cb);
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
evaluateQueue(valueOfStack, turnOrder, state, 0, &enemyHadTurn);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
if(!enemyHadTurn)
|
2016-10-28 16:16:46 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
auto battleIsFinishedOpt = state.battleIsFinished();
|
|
|
|
|
|
|
|
if(battleIsFinishedOpt)
|
2012-09-24 02:10:56 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
print("No need to cast a spell. Battle will finish soon.");
|
|
|
|
return;
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
2016-10-28 16:16:46 +02:00
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
struct ScriptsCache
|
|
|
|
{
|
|
|
|
//todo: re-implement scripts context cache
|
|
|
|
};
|
|
|
|
|
|
|
|
auto evaluateSpellcast = [&] (PossibleSpellcast * ps, std::shared_ptr<ScriptsCache>)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
HypotheticBattle state(env.get(), cb);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
spells::BattleCast cast(&state, hero, spells::Mode::HERO, ps->spell);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
cast.castEval(state.getServerCallback(), ps->dest);
|
2017-07-20 06:08:49 +02:00
|
|
|
ValueMap newHealthOfStack;
|
|
|
|
ValueMap newValueOfStack;
|
|
|
|
|
|
|
|
size_t ourUnits = 0;
|
|
|
|
|
2023-03-16 20:39:15 +02:00
|
|
|
std::set<uint32_t> unitIds;
|
|
|
|
|
|
|
|
state.battleGetUnitsIf([&](const battle::Unit * u)->bool
|
|
|
|
{
|
|
|
|
if(!u->isGhost() && !u->isTurret())
|
|
|
|
unitIds.insert(u->unitId());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
for(auto unitId : unitIds)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2018-03-01 21:18:01 +02:00
|
|
|
auto localUnit = state.battleGetUnitByID(unitId);
|
|
|
|
|
|
|
|
newHealthOfStack[unitId] = localUnit->getAvailableHealth();
|
|
|
|
newValueOfStack[unitId] = 0;
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2018-03-01 21:18:01 +02:00
|
|
|
if(state.battleGetOwner(localUnit) == playerID && localUnit->alive() && localUnit->willMove())
|
2017-07-20 06:08:49 +02:00
|
|
|
ourUnits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t minTurnSpan = ourUnits/3; //todo: tweak this
|
|
|
|
|
|
|
|
std::vector<battle::Units> newTurnOrder;
|
2021-01-30 14:13:45 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
state.battleGetTurnOrder(newTurnOrder, amount, 2);
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
const bool turnSpanOK = evaluateQueue(newValueOfStack, newTurnOrder, state, minTurnSpan, nullptr);
|
2018-03-01 21:18:01 +02:00
|
|
|
|
|
|
|
if(turnSpanOK || castNow)
|
2016-10-28 16:16:46 +02:00
|
|
|
{
|
2018-03-01 21:18:01 +02:00
|
|
|
int64_t totalGain = 0;
|
|
|
|
|
2023-03-16 20:39:15 +02:00
|
|
|
for(auto unitId : unitIds)
|
2012-09-24 02:10:56 +03:00
|
|
|
{
|
2018-03-01 21:18:01 +02:00
|
|
|
auto localUnit = state.battleGetUnitByID(unitId);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2018-03-01 21:18:01 +02:00
|
|
|
auto newValue = getValOr(newValueOfStack, unitId, 0);
|
|
|
|
auto oldValue = getValOr(valueOfStack, unitId, 0);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2018-03-01 21:18:01 +02:00
|
|
|
auto healthDiff = newHealthOfStack[unitId] - healthOfStack[unitId];
|
|
|
|
|
|
|
|
if(localUnit->unitOwner() != playerID)
|
2017-07-20 06:08:49 +02:00
|
|
|
healthDiff = -healthDiff;
|
|
|
|
|
2018-03-01 21:18:01 +02:00
|
|
|
if(healthDiff < 0)
|
|
|
|
{
|
|
|
|
ps->value = -1;
|
|
|
|
return; //do not damage own units at all
|
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2018-03-01 21:18:01 +02:00
|
|
|
totalGain += (newValue - oldValue + healthDiff);
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
2016-10-28 16:16:46 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
ps->value = totalGain;
|
2016-10-28 16:16:46 +02:00
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ps->value = -1;
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
2012-09-20 19:55:21 +03:00
|
|
|
};
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
using EvalRunner = ThreadPool<ScriptsCache>;
|
|
|
|
|
|
|
|
EvalRunner::Tasks tasks;
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2016-09-22 21:29:48 +02:00
|
|
|
for(PossibleSpellcast & psc : possibleCasts)
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
tasks.push_back(std::bind(evaluateSpellcast, &psc, _1));
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
uint32_t threadCount = boost::thread::hardware_concurrency();
|
|
|
|
|
|
|
|
if(threadCount == 0)
|
|
|
|
{
|
|
|
|
logGlobal->warn("No information of CPU cores available");
|
|
|
|
threadCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CStopWatch timer;
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
std::vector<std::shared_ptr<ScriptsCache>> scriptsPool;
|
|
|
|
|
|
|
|
for(uint32_t idx = 0; idx < threadCount; idx++)
|
|
|
|
{
|
|
|
|
scriptsPool.emplace_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
EvalRunner runner(&tasks, scriptsPool);
|
|
|
|
runner.run();
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
LOGFL("Evaluation took %d ms", timer.getDiff());
|
|
|
|
|
2018-03-01 21:18:01 +02:00
|
|
|
auto pscValue = [](const PossibleSpellcast &ps) -> int64_t
|
2016-09-22 21:29:48 +02:00
|
|
|
{
|
|
|
|
return ps.value;
|
|
|
|
};
|
|
|
|
auto castToPerform = *vstd::maxElementByFun(possibleCasts, pscValue);
|
2012-09-24 02:10:56 +03:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
if(castToPerform.value > 0)
|
2016-03-01 07:49:29 +02:00
|
|
|
{
|
2023-01-02 00:06:42 +02:00
|
|
|
LOGFL("Best spell is %s (value %d). Will cast.", castToPerform.spell->getNameTranslated() % castToPerform.value);
|
2017-07-20 06:08:49 +02:00
|
|
|
BattleAction spellcast;
|
|
|
|
spellcast.actionType = EActionType::HERO_SPELL;
|
|
|
|
spellcast.actionSubtype = castToPerform.spell->id;
|
|
|
|
spellcast.setTarget(castToPerform.dest);
|
|
|
|
spellcast.side = side;
|
|
|
|
spellcast.stackNumber = (!side) ? -1 : -2;
|
|
|
|
cb->battleMakeAction(&spellcast);
|
2023-04-08 13:05:47 +02:00
|
|
|
movesSkippedByDefense = 0;
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-02 00:06:42 +02:00
|
|
|
LOGFL("Best spell is %s. But it is actually useless (value %d).", castToPerform.spell->getNameTranslated() % castToPerform.value);
|
2012-09-24 02:10:56 +03:00
|
|
|
}
|
|
|
|
}
|
2013-06-17 18:45:55 +03:00
|
|
|
|
2019-05-04 05:42:55 +02:00
|
|
|
//Below method works only for offensive spells
|
|
|
|
void CBattleAI::evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps)
|
|
|
|
{
|
|
|
|
using ValueMap = PossibleSpellcast::ValueMap;
|
|
|
|
|
|
|
|
RNGStub rngStub;
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
HypotheticBattle state(env.get(), cb);
|
|
|
|
TStacks all = cb->battleGetAllStacks(false);
|
|
|
|
|
2019-05-04 05:42:55 +02:00
|
|
|
ValueMap healthOfStack;
|
|
|
|
ValueMap newHealthOfStack;
|
|
|
|
|
|
|
|
for(auto unit : all)
|
|
|
|
{
|
|
|
|
healthOfStack[unit->unitId()] = unit->getAvailableHealth();
|
|
|
|
}
|
|
|
|
|
|
|
|
spells::BattleCast cast(&state, stack, spells::Mode::CREATURE_ACTIVE, ps.spell);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
cast.castEval(state.getServerCallback(), ps.dest);
|
2019-05-04 05:42:55 +02:00
|
|
|
|
|
|
|
for(auto unit : all)
|
|
|
|
{
|
|
|
|
auto unitId = unit->unitId();
|
|
|
|
auto localUnit = state.battleGetUnitByID(unitId);
|
|
|
|
newHealthOfStack[unitId] = localUnit->getAvailableHealth();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t totalGain = 0;
|
|
|
|
|
|
|
|
for(auto unit : all)
|
|
|
|
{
|
|
|
|
auto unitId = unit->unitId();
|
|
|
|
auto localUnit = state.battleGetUnitByID(unitId);
|
|
|
|
|
|
|
|
auto healthDiff = newHealthOfStack[unitId] - healthOfStack[unitId];
|
|
|
|
|
|
|
|
if(localUnit->unitOwner() != getCbc()->getPlayerID())
|
|
|
|
healthDiff = -healthDiff;
|
|
|
|
|
|
|
|
if(healthDiff < 0)
|
|
|
|
{
|
|
|
|
ps.value = -1;
|
|
|
|
return; //do not damage own units at all
|
|
|
|
}
|
|
|
|
|
|
|
|
totalGain += healthDiff;
|
|
|
|
}
|
|
|
|
|
|
|
|
ps.value = totalGain;
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
}
|
2019-05-04 05:42:55 +02: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
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
LOG_TRACE(logAi);
|
2016-10-28 16:16:46 +02:00
|
|
|
side = Side;
|
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
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
logAi->trace("%s Battle AI[%p]: %s", playerID.getStr(), this, text);
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
std::optional<BattleAction> CBattleAI::considerFleeingOrSurrendering()
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2022-10-14 10:24:29 +02:00
|
|
|
BattleStateInfoForRetreat bs;
|
|
|
|
|
|
|
|
bs.canFlee = cb->battleCanFlee();
|
|
|
|
bs.canSurrender = cb->battleCanSurrender(playerID);
|
|
|
|
bs.ourSide = cb->battleGetMySide();
|
2022-10-15 14:05:20 +02:00
|
|
|
bs.ourHero = cb->battleGetMyHero();
|
|
|
|
bs.enemyHero = nullptr;
|
2022-10-14 10:24:29 +02:00
|
|
|
|
|
|
|
for(auto stack : cb->battleGetAllStacks(false))
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2022-10-14 10:24:29 +02:00
|
|
|
if(stack->alive())
|
|
|
|
{
|
2023-04-27 19:43:20 +02:00
|
|
|
if(stack->unitSide() == bs.ourSide)
|
2022-10-14 10:24:29 +02:00
|
|
|
bs.ourStacks.push_back(stack);
|
|
|
|
else
|
2022-10-15 14:05:20 +02:00
|
|
|
{
|
2022-10-14 10:24:29 +02:00
|
|
|
bs.enemyStacks.push_back(stack);
|
2022-10-15 14:05:20 +02:00
|
|
|
bs.enemyHero = cb->battleGetOwnerHero(stack);
|
|
|
|
}
|
2022-10-14 10:24:29 +02:00
|
|
|
}
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
2022-10-14 10:24:29 +02:00
|
|
|
|
2023-04-08 13:05:47 +02:00
|
|
|
bs.turnsSkippedByDefense = movesSkippedByDefense / bs.ourStacks.size();
|
|
|
|
|
2022-10-14 10:24:29 +02:00
|
|
|
if(!bs.canFlee || !bs.canSurrender)
|
2013-06-21 23:59:32 +03:00
|
|
|
{
|
2023-04-16 19:42:56 +02:00
|
|
|
return std::nullopt;
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
2022-10-14 10:24:29 +02:00
|
|
|
|
2023-04-08 13:05:47 +02:00
|
|
|
auto result = cb->makeSurrenderRetreatDecision(bs);
|
|
|
|
|
|
|
|
if(!result && bs.canFlee && bs.turnsSkippedByDefense > 30)
|
|
|
|
{
|
|
|
|
return BattleAction::makeRetreat(bs.ourSide);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2013-06-21 23:59:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|