2022-01-27 09:00:27 +02:00
|
|
|
/*
|
|
|
|
* BattleExchangeVariant.h, 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../../lib/AI_Base.h"
|
|
|
|
#include "../../lib/battle/ReachabilityInfo.h"
|
|
|
|
#include "PotentialTargets.h"
|
|
|
|
#include "StackWithBonuses.h"
|
|
|
|
|
|
|
|
struct AttackerValue
|
|
|
|
{
|
2023-08-26 12:06:41 +02:00
|
|
|
float value;
|
2022-01-27 09:00:27 +02:00
|
|
|
bool isRetalitated;
|
|
|
|
BattleHex position;
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
AttackerValue();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MoveTarget
|
|
|
|
{
|
2023-08-26 12:06:41 +02:00
|
|
|
float score;
|
|
|
|
float scorePerTurn;
|
2022-04-11 08:12:41 +02:00
|
|
|
std::vector<BattleHex> positions;
|
2023-08-08 17:37:42 +02:00
|
|
|
std::optional<AttackPossibility> cachedAttack;
|
|
|
|
uint8_t turnsToRich;
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
MoveTarget();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EvaluationResult
|
|
|
|
{
|
2023-08-26 12:06:41 +02:00
|
|
|
static const int64_t INEFFECTIVE_SCORE = -10000;
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
AttackPossibility bestAttack;
|
|
|
|
MoveTarget bestMove;
|
|
|
|
bool wait;
|
2023-08-26 12:06:41 +02:00
|
|
|
float score;
|
2022-04-11 08:12:41 +02:00
|
|
|
bool defend;
|
|
|
|
|
|
|
|
EvaluationResult(const AttackPossibility & ap)
|
2023-04-08 13:05:47 +02:00
|
|
|
:wait(false), score(INEFFECTIVE_SCORE), bestAttack(ap), defend(false)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The class represents evaluation of attack value
|
|
|
|
/// of exchanges between all stacks which can access particular hex
|
|
|
|
/// starting from initial attack represented by AttackPossibility and further according turn order.
|
|
|
|
/// Negative score value means we get more demage than deal
|
|
|
|
/// </summary>
|
2022-01-27 09:00:27 +02:00
|
|
|
class BattleExchangeVariant
|
|
|
|
{
|
|
|
|
public:
|
2023-08-19 09:22:34 +02:00
|
|
|
BattleExchangeVariant(float positiveEffectMultiplier, float negativeEffectMultiplier)
|
|
|
|
: dpsScore(0), positiveEffectMultiplier(positiveEffectMultiplier), negativeEffectMultiplier(negativeEffectMultiplier) {}
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
float trackAttack(
|
|
|
|
const AttackPossibility & ap,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb,
|
|
|
|
DamageCache & damageCache);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
float trackAttack(
|
2022-01-27 09:00:27 +02:00
|
|
|
std::shared_ptr<StackWithBonuses> attacker,
|
|
|
|
std::shared_ptr<StackWithBonuses> defender,
|
|
|
|
bool shooting,
|
|
|
|
bool isOurAttack,
|
2023-08-08 17:37:42 +02:00
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb,
|
2022-01-27 09:00:27 +02:00
|
|
|
bool evaluateOnly = false);
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
float getScore() const { return dpsScore; }
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
void adjustPositions(
|
|
|
|
std::vector<const battle::Unit *> attackers,
|
|
|
|
const AttackPossibility & ap,
|
|
|
|
std::map<BattleHex, battle::Units> & reachabilityMap);
|
|
|
|
|
|
|
|
private:
|
2023-08-19 09:22:34 +02:00
|
|
|
float positiveEffectMultiplier;
|
|
|
|
float negativeEffectMultiplier;
|
2023-08-26 12:06:41 +02:00
|
|
|
float dpsScore;
|
2022-01-27 09:00:27 +02:00
|
|
|
std::map<uint32_t, AttackerValue> attackerValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BattleExchangeEvaluator
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::shared_ptr<CBattleInfoCallback> cb;
|
|
|
|
std::shared_ptr<Environment> env;
|
|
|
|
std::map<BattleHex, std::vector<const battle::Unit *>> reachabilityMap;
|
|
|
|
std::vector<battle::Units> turnOrder;
|
2023-08-19 09:22:34 +02:00
|
|
|
float negativeEffectMultiplier;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
public:
|
2023-08-19 09:22:34 +02:00
|
|
|
BattleExchangeEvaluator(
|
|
|
|
std::shared_ptr<CBattleInfoCallback> cb,
|
|
|
|
std::shared_ptr<Environment> env,
|
|
|
|
float strengthRatio): cb(cb), env(env) {
|
|
|
|
negativeEffectMultiplier = strengthRatio;
|
|
|
|
}
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
EvaluationResult findBestTarget(
|
|
|
|
const battle::Unit * activeStack,
|
|
|
|
PotentialTargets & targets,
|
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb);
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
float calculateExchange(
|
2023-08-08 17:37:42 +02:00
|
|
|
const AttackPossibility & ap,
|
|
|
|
PotentialTargets & targets,
|
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb);
|
|
|
|
|
|
|
|
void updateReachabilityMap(std::shared_ptr<HypotheticBattle> hb);
|
|
|
|
std::vector<const battle::Unit *> getExchangeUnits(const AttackPossibility & ap, PotentialTargets & targets, std::shared_ptr<HypotheticBattle> hb);
|
2022-01-27 09:00:27 +02:00
|
|
|
bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
|
2023-08-08 17:37:42 +02:00
|
|
|
|
|
|
|
MoveTarget findMoveTowardsUnreachable(
|
|
|
|
const battle::Unit * activeStack,
|
|
|
|
PotentialTargets & targets,
|
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb);
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
std::vector<const battle::Unit *> getAdjacentUnits(const battle::Unit * unit);
|
2023-08-19 09:22:34 +02:00
|
|
|
|
|
|
|
float getPositiveEffectMultiplier() { return 1; }
|
|
|
|
float getNegativeEffectMultiplier() { return negativeEffectMultiplier; }
|
2023-08-28 17:59:12 +02:00
|
|
|
};
|