1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-10-08 23:22:25 +02:00

add initial Battle AI proxy project for neural networks

This commit is contained in:
Andrii Danylchenko
2022-01-15 19:26:20 +02:00
parent b8b330668d
commit 4256792914
23 changed files with 2383 additions and 1 deletions

View File

@@ -0,0 +1,188 @@
/*
* AttackPossibility.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
*
*/
#include "StdInc.h"
#include "AttackPossibility.h"
#include "../../lib/CStack.h" // TODO: remove
// Eventually only IBattleInfoCallback and battle::Unit should be used,
// CUnitState should be private and CStack should be removed completely
AttackPossibility::AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack)
: from(from), dest(dest), attack(attack)
{
}
int64_t AttackPossibility::damageDiff() const
{
return damageDealt - damageReceived - collateralDamage + shootersBlockedDmg;
}
int64_t AttackPossibility::attackValue() const
{
return damageDiff();
}
int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state)
{
int64_t res = 0;
if(attackInfo.shooting)
return 0;
auto attacker = attackInfo.attacker;
auto hexes = attacker->getSurroundingHexes(hex);
for(BattleHex tile : hexes)
{
auto st = state->battleGetUnitByPos(tile, true);
if(!st || !state->battleMatchOwner(st, attacker))
continue;
if(!state->battleCanShoot(st))
continue;
BattleAttackInfo rangeAttackInfo(st, attacker, true);
rangeAttackInfo.defenderPos = hex;
BattleAttackInfo meleeAttackInfo(st, attacker, false);
meleeAttackInfo.defenderPos = hex;
auto rangeDmg = getCbc()->battleEstimateDamage(rangeAttackInfo);
auto meleeDmg = getCbc()->battleEstimateDamage(meleeAttackInfo);
int64_t gain = (rangeDmg.first + rangeDmg.second - meleeDmg.first - meleeDmg.second) / 2 + 1;
res += gain;
}
return res;
}
AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state)
{
auto attacker = attackInfo.attacker;
auto defender = attackInfo.defender;
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
static const auto selectorBlocksRetaliation = Selector::type()(Bonus::BLOCKS_RETALIATION);
const auto attackerSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attacker));
const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
AttackPossibility bestAp(hex, BattleHex::INVALID, attackInfo);
std::vector<BattleHex> defenderHex;
if(attackInfo.shooting)
defenderHex = defender->getHexes();
else
defenderHex = CStack::meleeAttackHexes(attacker, defender, hex);
for(BattleHex defHex : defenderHex)
{
if(defHex == hex) // should be impossible but check anyway
continue;
AttackPossibility ap(hex, defHex, attackInfo);
ap.attackerState = attacker->acquireState();
ap.shootersBlockedDmg = bestAp.shootersBlockedDmg;
const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);
if (!attackInfo.shooting)
ap.attackerState->setPosition(hex);
std::vector<const battle::Unit*> units;
if (attackInfo.shooting)
units = state->getAttackedBattleUnits(attacker, defHex, true, BattleHex::INVALID);
else
units = state->getAttackedBattleUnits(attacker, defHex, false, hex);
// ensure the defender is also affected
bool addDefender = true;
for(auto unit : units)
{
if (unit->unitId() == defender->unitId())
{
addDefender = false;
break;
}
}
if(addDefender)
units.push_back(defender);
for(auto u : units)
{
if(!ap.attackerState->alive())
break;
auto defenderState = u->acquireState();
ap.affectedUnits.push_back(defenderState);
for(int i = 0; i < totalAttacks; i++)
{
si64 damageDealt, damageReceived;
TDmgRange retaliation(0, 0);
auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation);
vstd::amin(attackDmg.first, defenderState->getAvailableHealth());
vstd::amin(attackDmg.second, defenderState->getAvailableHealth());
vstd::amin(retaliation.first, ap.attackerState->getAvailableHealth());
vstd::amin(retaliation.second, ap.attackerState->getAvailableHealth());
damageDealt = (attackDmg.first + attackDmg.second) / 2;
ap.attackerState->afterAttack(attackInfo.shooting, false);
//FIXME: use ranged retaliation
damageReceived = 0;
if (!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
{
damageReceived = (retaliation.first + retaliation.second) / 2;
defenderState->afterAttack(attackInfo.shooting, true);
}
bool isEnemy = state->battleMatchOwner(attacker, u);
// this includes enemy units as well as attacker units under enemy's mind control
if(isEnemy)
ap.damageDealt += damageDealt;
// damaging attacker's units (even those under enemy's mind control) is considered friendly fire
if(attackerSide == u->unitSide())
ap.collateralDamage += damageDealt;
if(u->unitId() == defender->unitId() ||
(!attackInfo.shooting && CStack::isMeleeAttackPossible(u, attacker, hex)))
{
//FIXME: handle RANGED_RETALIATION ?
ap.damageReceived += damageReceived;
}
ap.attackerState->damage(damageReceived);
defenderState->damage(damageDealt);
if (!ap.attackerState->alive() || !defenderState->alive())
break;
}
}
if(!bestAp.dest.isValid() || ap.attackValue() > bestAp.attackValue())
bestAp = ap;
}
// check how much damage we gain from blocking enemy shooters on this hex
bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, state);
logAi->debug("BattleAI best AP: %s -> %s at %d from %d, affects %d units: %lld %lld %lld %lld",
attackInfo.attacker->unitType()->identifier,
attackInfo.defender->unitType()->identifier,
(int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(),
bestAp.damageDealt, bestAp.damageReceived, bestAp.collateralDamage, bestAp.shootersBlockedDmg);
//TODO other damage related to attack (eg. fire shield and other abilities)
return bestAp;
}

View File

@@ -0,0 +1,41 @@
/*
* AttackPossibility.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/battle/CUnitState.h"
#include "../../CCallback.h"
#include "common.h"
#include "StackWithBonuses.h"
class AttackPossibility
{
public:
BattleHex from; //tile from which we attack
BattleHex dest; //tile which we attack
BattleAttackInfo attack;
std::shared_ptr<battle::CUnitState> attackerState;
std::vector<std::shared_ptr<battle::CUnitState>> affectedUnits;
int64_t damageDealt = 0;
int64_t damageReceived = 0; //usually by counter-attack
int64_t collateralDamage = 0; // friendly fire (usually by two-hex attacks)
int64_t shootersBlockedDmg = 0;
AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack_);
int64_t damageDiff() const;
int64_t attackValue() const;
static AttackPossibility evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state);
private:
static int64_t evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state);
};

837
AI/BattleML/BattleAI.cpp Normal file
View File

@@ -0,0 +1,837 @@
/*
* 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
*
*/
#include "StdInc.h"
#include "BattleAI.h"
#include "StackWithBonuses.h"
#include "EnemyInfo.h"
#include "../../lib/CStopWatch.h"
#include "../../lib/CThreadHelper.h"
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../../lib/spells/CSpellHandler.h"
#include "../../lib/spells/ISpellMechanics.h"
#include "../../lib/CStack.h" // TODO: remove
// Eventually only IBattleInfoCallback and battle::Unit should be used,
// CUnitState should be private and CStack should be removed completely
#define LOGL(text) print(text)
#define LOGFL(text, formattingEl) print(boost::str(boost::format(text) % formattingEl))
enum class SpellTypes
{
ADVENTURE, BATTLE, OTHER
};
SpellTypes spellType(const CSpell * spell)
{
if(!spell->isCombat() || spell->isCreatureAbility())
return SpellTypes::OTHER;
if(spell->isOffensive() || spell->hasEffects() || spell->hasBattleEffects())
return SpellTypes::BATTLE;
return SpellTypes::OTHER;
}
std::vector<BattleHex> CBattleAI::getBrokenWallMoatHexes() const
{
std::vector<BattleHex> result;
for(int wallPart = EWallPart::BOTTOM_WALL; wallPart < EWallPart::UPPER_WALL; wallPart++)
{
auto state = cb->battleGetWallState(wallPart);
if(state != EWallState::DESTROYED)
continue;
auto wallHex = cb->wallPartToBattleHex((EWallPart::EWallPart)wallPart);
auto moatHex = wallHex.cloneInDirection(BattleHex::LEFT);
result.push_back(moatHex);
}
return result;
}
CBattleAI::CBattleAI()
: side(-1),
wasWaitingForRealize(false),
wasUnlockingGs(false)
{
}
CBattleAI::~CBattleAI()
{
if(cb)
{
//Restore previous state of CB - it may be shared with the main AI (like VCAI)
cb->waitTillRealize = wasWaitingForRealize;
cb->unlockGsWhenWaiting = wasUnlockingGs;
}
}
void CBattleAI::init(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB)
{
setCbc(CB);
env = ENV;
cb = CB;
playerID = *CB->getPlayerID(); //TODO should be sth in callback
wasWaitingForRealize = CB->waitTillRealize;
wasUnlockingGs = CB->unlockGsWhenWaiting;
CB->waitTillRealize = true;
CB->unlockGsWhenWaiting = false;
}
BattleAction CBattleAI::activeStack( const CStack * stack )
{
LOG_TRACE_PARAMS(logAi, "stack: %s", stack->nodeName()) ;
setCbc(cb); //TODO: make solid sure that AIs always use their callbacks (need to take care of event handlers too)
try
{
if(stack->type->idNumber == CreatureID::CATAPULT)
return useCatapult(stack);
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)
if(auto woundHp = stack->MaxHealth() - stack->getFirstHPleft())
woundHpToStack[woundHp] = stack;
if(woundHpToStack.empty())
return BattleAction::makeDefend(stack);
else
return BattleAction::makeHeal(stack, woundHpToStack.rbegin()->second); //last element of the woundHpToStack is the most wounded stack
}
attemptCastingSpell();
if(auto ret = cb->battleIsFinished())
{
//spellcast may finish battle
//send special preudo-action
BattleAction cancel;
cancel.actionType = EActionType::CANCEL;
return cancel;
}
if(auto action = considerFleeingOrSurrendering())
return *action;
//best action is from effective owner point if view, we are effective owner as we received "activeStack"
//evaluate casting spell for spellcasting stack
boost::optional<PossibleSpellcast> bestSpellcast(boost::none);
//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)
{
bestSpellcast = boost::optional<PossibleSpellcast>(possibleCasts.front());
}
}
}
HypotheticBattle hb(env.get(), cb);
PotentialTargets targets(stack, &hb);
std::vector<battle::Units> turnOrder;
cb->battleGetTurnOrder(turnOrder, 1000, 3);
cb->battleGetAllObstacles(cb->battleGetMySide());
cb->battleGetMyHero();
cb->battleGetEnemyHero();
auto stacks = cb->battleGetAllStacks();
//AccessibilityInfo access = cb->getReachability();
JsonNode root;
root["currentSide"].Integer() = cb->battleGetMySide();
root["activeStackId"].Integer() = stack->unitId();
root["terrain"].Integer() = cb->battleTerrainType();
root["battlefield"].Integer() = cb->battleGetBattlefieldType();
for(auto unit : stacks)
{
JsonNode unitNode;
// state
unitNode["hex"].Integer() = unit->getPosition().hex;
unitNode["id"].Integer() = unit->unitId();
unitNode["canShoot"].Bool() = unit->canShoot();
unitNode["canCast"].Bool() = unit->canCast();
unitNode["shots"].Integer() = unit->shots.total();
unitNode["side"].Integer() = unit->unitSide();
// creature
unitNode["doubleWide"].Bool() = unit->doubleWide();
// stats
unitNode["attack"].Integer() = unit->getAttack(false);
unitNode["defence"].Integer() = unit->getDefense(false);
unitNode["minDamage"].Integer() = unit->getMinDamage(false);
unitNode["maxDamage"].Integer() = unit->getMinDamage(false);
auto & ranged = unitNode["ranged"];
ranged["attack"].Integer() = unit->getAttack(true);
ranged["defence"].Integer() = unit->getDefense(true);
ranged["minDamage"].Integer() = unit->getMinDamage(true);
ranged["maxDamage"].Integer() = unit->getMinDamage(true);
unitNode["morale"].Integer() = unit->MoraleVal();
unitNode["luck"].Integer() = unit->LuckVal();
unitNode["speed"].Integer() = unit->Speed();
unitNode["isClone"].Bool() = unit->isClone();
unitNode["summoned"].Bool() = unit->summoned;
unitNode["isGhost"].Bool() = unit->isGhost();
unitNode["NO_DISTANCE_PENALTY"].Bool() = unit->hasBonusOfType(Bonus::NO_DISTANCE_PENALTY);
unitNode["NO_MELEE_PENALTY"].Bool() = unit->hasBonusOfType(Bonus::NO_MELEE_PENALTY);
unitNode["ADDITIONAL_RETALIATION"].Bool() = unit->hasBonusOfType(Bonus::ADDITIONAL_RETALIATION);
unitNode["NO_RETALIATION"].Bool() = unit->hasBonusOfType(Bonus::NO_RETALIATION);
unitNode["ADDITIONAL_ATTACK"].Bool() = unit->hasBonusOfType(Bonus::ADDITIONAL_ATTACK);
unitNode["TWO_HEX_ATTACK_BREATH"].Bool() = unit->hasBonusOfType(Bonus::TWO_HEX_ATTACK_BREATH);
unitNode["LIFE_DRAIN"].Bool() = unit->hasBonusOfType(Bonus::LIFE_DRAIN);
unitNode["ATTACKS_NEAREST_CREATURE"].Bool() = unit->hasBonusOfType(Bonus::ATTACKS_NEAREST_CREATURE);
unitNode["THREE_HEADED_ATTACK"].Bool() = unit->hasBonusOfType(Bonus::THREE_HEADED_ATTACK);
root["stacks"].Vector().push_back(unitNode);
}
JsonNode & orderNode = root["order"];
int index = 0;
for(auto & turn : turnOrder)
{
JsonNode turnNode;
for(const battle::Unit * unit : turn)
{
JsonNode unitNode;
// state
unitNode["queuePhase"].Integer() = unit->battleQueuePhase(index);
unitNode["canRetalitate"].Bool() = index == 0 ? unit->ableToRetaliate() : true;
unitNode["canMove"].Bool() = unit->canMove(index);
unitNode["defended"].Bool() = unit->defended(index);
unitNode["speed"].Integer() = unit->Speed(index);
unitNode["initiative"].Integer() = unit->getInitiative(index);
unitNode["id"].Integer() = unit->unitId();
turnNode.Vector().push_back(unitNode);
}
orderNode.Vector().push_back(turnNode);
}
std::ofstream file;
file.open("data.json");
file << root.toJson();
file.close();
if(!targets.possibleAttacks.empty())
{
AttackPossibility bestAttack = targets.bestAction();
//TODO: consider more complex spellcast evaluation, f.e. because "re-retaliation" during enemy move in same turn for melee attack etc.
if(bestSpellcast.is_initialized() && bestSpellcast->value > bestAttack.damageDiff())
return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
else if(bestAttack.attack.shooting)
{
auto &target = bestAttack;
logAi->debug("BattleAI: %s -> %s x %d, shot, from %d curpos %d dist %d speed %d: %lld %lld %lld",
target.attackerState->unitType()->identifier,
target.affectedUnits[0]->unitType()->identifier,
(int)target.affectedUnits.size(), (int)target.from, (int)bestAttack.attack.attacker->getPosition().hex,
bestAttack.attack.chargedFields, bestAttack.attack.attacker->Speed(0, true),
target.damageDealt, target.damageReceived, target.attackValue()
);
return BattleAction::makeShotAttack(stack, bestAttack.attack.defender);
}
else
{
auto &target = bestAttack;
logAi->debug("BattleAI: %s -> %s x %d, mellee, from %d curpos %d dist %d speed %d: %lld %lld %lld",
target.attackerState->unitType()->identifier,
target.affectedUnits[0]->unitType()->identifier,
(int)target.affectedUnits.size(), (int)target.from, (int)bestAttack.attack.attacker->getPosition().hex,
bestAttack.attack.chargedFields, bestAttack.attack.attacker->Speed(0, true),
target.damageDealt, target.damageReceived, target.attackValue()
);
return BattleAction::makeMeleeAttack(stack, bestAttack.attack.defender->getPosition(), bestAttack.from);
}
}
else if(bestSpellcast.is_initialized())
{
return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
}
else
{
if(stack->waited())
{
//ThreatMap threatsToUs(stack); // These lines may be usefull but they are't used in the code.
auto dists = cb->getReachability(stack);
if(!targets.unreachableEnemies.empty())
{
auto closestEnemy = vstd::minElementByFun(targets.unreachableEnemies, [&](const battle::Unit * enemy) -> int
{
return dists.distToNearestNeighbour(stack, enemy);
});
if(dists.distToNearestNeighbour(stack, *closestEnemy) < GameConstants::BFIELD_SIZE)
{
return goTowardsNearest(stack, (*closestEnemy)->getAttackableHexes(stack));
}
}
}
else
{
return BattleAction::makeWait(stack);
}
}
if(!stack->hasBonusOfType(Bonus::FLYING)
&& stack->unitSide() == BattleSide::ATTACKER
&& cb->battleGetSiegeLevel() >= CGTownInstance::CITADEL)
{
auto brokenWallMoat = getBrokenWallMoatHexes();
if(brokenWallMoat.size())
{
if(stack->doubleWide() && vstd::contains(brokenWallMoat, stack->getPosition()))
return BattleAction::makeMove(stack, stack->getPosition().cloneInDirection(BattleHex::RIGHT));
else
return goTowardsNearest(stack, brokenWallMoat);
}
}
}
catch(boost::thread_interrupted &)
{
throw;
}
catch(std::exception &e)
{
logAi->error("Exception occurred in %s %s",__FUNCTION__, e.what());
}
return BattleAction::makeDefend(stack);
}
BattleAction CBattleAI::goTowardsNearest(const CStack * stack, std::vector<BattleHex> hexes) const
{
auto reachability = cb->getReachability(stack);
auto avHexes = cb->battleGetAvailableHexes(reachability, stack);
if(!avHexes.size() || !hexes.size()) //we are blocked or dest is blocked
{
return BattleAction::makeDefend(stack);
}
std::sort(hexes.begin(), hexes.end(), [&](BattleHex h1, BattleHex h2) -> bool
{
return reachability.distances[h1] < reachability.distances[h2];
});
for(auto hex : hexes)
{
if(vstd::contains(avHexes, hex))
return BattleAction::makeMove(stack, hex);
if(stack->coversPos(hex))
{
logAi->warn("Warning: already standing on neighbouring tile!");
//We shouldn't even be here...
return BattleAction::makeDefend(stack);
}
}
BattleHex bestNeighbor = hexes.front();
if(reachability.distances[bestNeighbor] > GameConstants::BFIELD_SIZE)
{
return BattleAction::makeDefend(stack);
}
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 nearestAvailableHex = vstd::minElementByFun(avHexes, [&](BattleHex hex) -> int
{
return BattleHex::getDistance(bestNeighbor, hex);
});
return BattleAction::makeMove(stack, *nearestAvailableHex);
}
else
{
BattleHex currentDest = bestNeighbor;
while(1)
{
if(!currentDest.isValid())
{
logAi->error("CBattleAI::goTowards: internal error");
return BattleAction::makeDefend(stack);
}
if(vstd::contains(avHexes, currentDest))
return BattleAction::makeMove(stack, currentDest);
currentDest = reachability.predecessors[currentDest];
}
}
}
BattleAction CBattleAI::useCatapult(const CStack * stack)
{
BattleAction attack;
BattleHex targetHex = BattleHex::INVALID;
if(cb->battleGetGateState() == EGateState::CLOSED)
{
targetHex = cb->wallPartToBattleHex(EWallPart::GATE);
}
else
{
EWallPart::EWallPart wallParts[] = {
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);
if(wallState == EWallState::INTACT || wallState == EWallState::DAMAGED)
{
targetHex = cb->wallPartToBattleHex(wallPart);
break;
}
}
}
if(!targetHex.isValid())
{
return BattleAction::makeDefend(stack);
}
attack.aimToHex(targetHex);
attack.actionType = EActionType::CATAPULT;
attack.side = side;
attack.stackNumber = stack->ID;
return attack;
}
void CBattleAI::attemptCastingSpell()
{
auto hero = cb->battleGetMyHero();
if(!hero)
return;
if(cb->battleCanCastSpell(hero, spells::Mode::HERO) != ESpellCastProblem::OK)
return;
LOGL("Casting spells sounds like fun. Let's see...");
//Get all spells we can cast
std::vector<const CSpell*> possibleSpells;
vstd::copy_if(VLC->spellh->objects, std::back_inserter(possibleSpells), [hero, this](const CSpell *s) -> bool
{
return s->canBeCast(cb.get(), spells::Mode::HERO, hero);
});
LOGFL("I can cast %d spells.", possibleSpells.size());
vstd::erase_if(possibleSpells, [](const CSpell *s)
{
return spellType(s) != SpellTypes::BATTLE;
});
LOGFL("I know how %d of them works.", possibleSpells.size());
//Get possible spell-target pairs
std::vector<PossibleSpellcast> possibleCasts;
for(auto spell : possibleSpells)
{
spells::BattleCast temp(cb.get(), hero, spells::Mode::HERO, spell);
for(auto & target : temp.findPotentialTargets())
{
PossibleSpellcast ps;
ps.dest = target;
ps.spell = spell;
possibleCasts.push_back(ps);
}
}
LOGFL("Found %d spell-target combinations.", possibleCasts.size());
if(possibleCasts.empty())
return;
using ValueMap = PossibleSpellcast::ValueMap;
auto evaluateQueue = [&](ValueMap & values, const std::vector<battle::Units> & queue, HypotheticBattle * state, size_t minTurnSpan, bool * enemyHadTurnOut) -> bool
{
bool firstRound = true;
bool enemyHadTurn = false;
size_t ourTurnSpan = 0;
bool stop = false;
for(auto & round : queue)
{
if(!firstRound)
state->nextRound(0);//todo: set actual value?
for(auto unit : round)
{
if(!vstd::contains(values, unit->unitId()))
values[unit->unitId()] = 0;
if(!unit->alive())
continue;
if(state->battleGetOwner(unit) != playerID)
{
enemyHadTurn = true;
if(!firstRound || state->battleCastSpells(unit->unitSide()) == 0)
{
//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++;
}
state->nextTurn(unit->unitId());
PotentialTargets pt(unit, state);
if(!pt.possibleAttacks.empty())
{
AttackPossibility ap = pt.bestAction();
auto swb = state->getForUpdate(unit->unitId());
*swb = *ap.attackerState;
if(ap.damageDealt > 0)
swb->removeUnitBonus(Bonus::UntilAttack);
if(ap.damageReceived > 0)
swb->removeUnitBonus(Bonus::UntilBeingAttacked);
for(auto affected : ap.affectedUnits)
{
swb = state->getForUpdate(affected->unitId());
*swb = *affected;
if(ap.damageDealt > 0)
swb->removeUnitBonus(Bonus::UntilBeingAttacked);
if(ap.damageReceived > 0 && ap.attack.defender->unitId() == affected->unitId())
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
if(state->battleGetOwner(unit) != playerID)
bav = -bav;
values[unit->unitId()] += bav;
}
firstRound = false;
if(stop)
break;
}
if(enemyHadTurnOut)
*enemyHadTurnOut = enemyHadTurn;
return ourTurnSpan >= minTurnSpan;
};
ValueMap valueOfStack;
ValueMap healthOfStack;
TStacks all = cb->battleGetAllStacks(false);
size_t ourRemainingTurns = 0;
for(auto unit : all)
{
healthOfStack[unit->unitId()] = unit->getAvailableHealth();
valueOfStack[unit->unitId()] = 0;
if(cb->battleGetOwner(unit) == playerID && unit->canMove() && !unit->moved())
ourRemainingTurns++;
}
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");
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
{
bool enemyHadTurn = false;
HypotheticBattle state(env.get(), cb);
evaluateQueue(valueOfStack, turnOrder, &state, 0, &enemyHadTurn);
if(!enemyHadTurn)
{
auto battleIsFinishedOpt = state.battleIsFinished();
if(battleIsFinishedOpt)
{
print("No need to cast a spell. Battle will finish soon.");
return;
}
}
}
struct ScriptsCache
{
//todo: re-implement scripts context cache
};
auto evaluateSpellcast = [&] (PossibleSpellcast * ps, std::shared_ptr<ScriptsCache>)
{
HypotheticBattle state(env.get(), cb);
spells::BattleCast cast(&state, hero, spells::Mode::HERO, ps->spell);
cast.castEval(state.getServerCallback(), ps->dest);
ValueMap newHealthOfStack;
ValueMap newValueOfStack;
size_t ourUnits = 0;
for(auto unit : all)
{
auto unitId = unit->unitId();
auto localUnit = state.battleGetUnitByID(unitId);
newHealthOfStack[unitId] = localUnit->getAvailableHealth();
newValueOfStack[unitId] = 0;
if(state.battleGetOwner(localUnit) == playerID && localUnit->alive() && localUnit->willMove())
ourUnits++;
}
size_t minTurnSpan = ourUnits/3; //todo: tweak this
std::vector<battle::Units> newTurnOrder;
state.battleGetTurnOrder(newTurnOrder, amount, 2);
const bool turnSpanOK = evaluateQueue(newValueOfStack, newTurnOrder, &state, minTurnSpan, nullptr);
if(turnSpanOK || castNow)
{
int64_t totalGain = 0;
for(auto unit : all)
{
auto unitId = unit->unitId();
auto localUnit = state.battleGetUnitByID(unitId);
auto newValue = getValOr(newValueOfStack, unitId, 0);
auto oldValue = getValOr(valueOfStack, unitId, 0);
auto healthDiff = newHealthOfStack[unitId] - healthOfStack[unitId];
if(localUnit->unitOwner() != playerID)
healthDiff = -healthDiff;
if(healthDiff < 0)
{
ps->value = -1;
return; //do not damage own units at all
}
totalGain += (newValue - oldValue + healthDiff);
}
ps->value = totalGain;
}
else
{
ps->value = -1;
}
};
using EvalRunner = ThreadPool<ScriptsCache>;
EvalRunner::Tasks tasks;
for(PossibleSpellcast & psc : possibleCasts)
tasks.push_back(std::bind(evaluateSpellcast, &psc, _1));
uint32_t threadCount = boost::thread::hardware_concurrency();
if(threadCount == 0)
{
logGlobal->warn("No information of CPU cores available");
threadCount = 1;
}
CStopWatch timer;
std::vector<std::shared_ptr<ScriptsCache>> scriptsPool;
for(uint32_t idx = 0; idx < threadCount; idx++)
{
scriptsPool.emplace_back();
}
EvalRunner runner(&tasks, scriptsPool);
runner.run();
LOGFL("Evaluation took %d ms", timer.getDiff());
auto pscValue = [](const PossibleSpellcast &ps) -> int64_t
{
return ps.value;
};
auto castToPerform = *vstd::maxElementByFun(possibleCasts, pscValue);
if(castToPerform.value > 0)
{
LOGFL("Best spell is %s (value %d). Will cast.", castToPerform.spell->name % castToPerform.value);
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);
}
else
{
LOGFL("Best spell is %s. But it is actually useless (value %d).", castToPerform.spell->name % castToPerform.value);
}
}
//Below method works only for offensive spells
void CBattleAI::evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps)
{
using ValueMap = PossibleSpellcast::ValueMap;
RNGStub rngStub;
HypotheticBattle state(env.get(), cb);
TStacks all = cb->battleGetAllStacks(false);
ValueMap healthOfStack;
ValueMap newHealthOfStack;
for(auto unit : all)
{
healthOfStack[unit->unitId()] = unit->getAvailableHealth();
}
spells::BattleCast cast(&state, stack, spells::Mode::CREATURE_ACTIVE, ps.spell);
cast.castEval(state.getServerCallback(), ps.dest);
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;
}
void CBattleAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
{
LOG_TRACE(logAi);
side = Side;
}
void CBattleAI::print(const std::string &text) const
{
logAi->trace("%s Battle AI[%p]: %s", playerID.getStr(), this, text);
}
boost::optional<BattleAction> CBattleAI::considerFleeingOrSurrendering()
{
if(cb->battleCanSurrender(playerID))
{
}
if(cb->battleCanFlee())
{
}
return boost::none;
}

93
AI/BattleML/BattleAI.h Normal file
View File

@@ -0,0 +1,93 @@
/*
* BattleAI.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 "PossibleSpellcast.h"
#include "PotentialTargets.h"
class CSpell;
class EnemyInfo;
/*
struct CurrentOffensivePotential
{
std::map<const CStack *, PotentialTargets> ourAttacks;
std::map<const CStack *, PotentialTargets> enemyAttacks;
CurrentOffensivePotential(ui8 side)
{
for(auto stack : cbc->battleGetStacks())
{
if(stack->side == side)
ourAttacks[stack] = PotentialTargets(stack);
else
enemyAttacks[stack] = PotentialTargets(stack);
}
}
int potentialValue()
{
int ourPotential = 0, enemyPotential = 0;
for(auto &p : ourAttacks)
ourPotential += p.second.bestAction().attackValue();
for(auto &p : enemyAttacks)
enemyPotential += p.second.bestAction().attackValue();
return ourPotential - enemyPotential;
}
};
*/ // These lines may be usefull but they are't used in the code.
class CBattleAI : public CBattleGameInterface
{
int side;
std::shared_ptr<CBattleCallback> cb;
std::shared_ptr<Environment> env;
//Previous setting of cb
bool wasWaitingForRealize, wasUnlockingGs;
public:
CBattleAI();
~CBattleAI();
void init(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB) override;
void attemptCastingSpell();
void evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps); //for offensive damaging spells only
BattleAction activeStack(const CStack * stack) override; //called when it's turn of that stack
boost::optional<BattleAction> considerFleeingOrSurrendering();
void print(const std::string &text) const;
BattleAction useCatapult(const CStack *stack);
void battleStart(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile, const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool Side) override;
//void actionFinished(const BattleAction &action) override;//occurs AFTER every action taken by any stack or by the hero
//void actionStarted(const BattleAction &action) override;//occurs BEFORE every action taken by any stack or by the hero
//void battleAttack(const BattleAttack *ba) override; //called when stack is performing attack
//void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa) override; //called when stack receives damage (after battleAttack())
//void battleEnd(const BattleResult *br) override;
//void battleResultsApplied() override; //called when all effects of last battle are applied
//void battleNewRoundFirst(int round) override; //called at the beginning of each turn before changes are applied;
//void battleNewRound(int round) override; //called at the beginning of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
//void battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance) override;
//void battleSpellCast(const BattleSpellCast *sc) override;
//void battleStacksEffectsSet(const SetStackEffect & sse) override;//called when a specific effect is set to stacks
//void battleTriggerEffect(const BattleTriggerEffect & bte) override;
//void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side) override; //called by engine when battle starts; side=0 - left, side=1 - right
//void battleCatapultAttacked(const CatapultAttack & ca) override; //called when catapult makes an attack
private:
BattleAction goTowardsNearest(const CStack * stack, std::vector<BattleHex> hexes) const;
std::vector<BattleHex> getBrokenWallMoatHexes() const;
};

View File

@@ -0,0 +1,43 @@
set(battleML_SRCS
StdInc.cpp
AttackPossibility.cpp
BattleAI.cpp
common.cpp
EnemyInfo.cpp
main.cpp
PossibleSpellcast.cpp
PotentialTargets.cpp
StackWithBonuses.cpp
ThreatMap.cpp
)
set(battleML_HEADERS
StdInc.h
AttackPossibility.h
BattleAI.h
common.h
EnemyInfo.h
PotentialTargets.h
PossibleSpellcast.h
StackWithBonuses.h
ThreatMap.h
)
assign_source_group(${battleML_SRCS} ${battleML_HEADERS})
if(ANDROID) # android compiles ai libs into main lib directly, so we skip this library and just reuse sources list
return()
endif()
add_library(BattleML SHARED ${battleML_SRCS} ${battleML_HEADERS})
target_include_directories(BattleML PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(BattleML PRIVATE vcmi)
vcmi_set_output_dir(BattleML "AI")
set_target_properties(BattleML PROPERTIES ${PCH_PROPERTIES})
cotire(BattleML)
install(TARGETS BattleML RUNTIME DESTINATION ${AI_LIB_DIR} LIBRARY DESTINATION ${AI_LIB_DIR})

19
AI/BattleML/EnemyInfo.cpp Normal file
View File

@@ -0,0 +1,19 @@
/*
* EnemyInfo.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
*
*/
#include "StdInc.h"
#include "EnemyInfo.h"
#include "../../lib/battle/Unit.h"
bool EnemyInfo::operator==(const EnemyInfo & ei) const
{
return s->unitId() == ei.s->unitId();
}

24
AI/BattleML/EnemyInfo.h Normal file
View File

@@ -0,0 +1,24 @@
/*
* EnemyInfo.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
namespace battle
{
class Unit;
}
class EnemyInfo
{
public:
const battle::Unit * s;
EnemyInfo(const battle::Unit * _s) : s(_s)
{}
bool operator==(const EnemyInfo & ei) const;
};

View File

@@ -0,0 +1,21 @@
/*
* PossibleSpellcast.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
*
*/
#include "StdInc.h"
#include "PossibleSpellcast.h"
PossibleSpellcast::PossibleSpellcast()
: spell(nullptr),
dest(),
value(0)
{
}
PossibleSpellcast::~PossibleSpellcast() = default;

View File

@@ -0,0 +1,30 @@
/*
* PossibleSpellcast.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 <vcmi/spells/Magic.h>
#include "../../lib/battle/Destination.h"
class CSpell;
class PossibleSpellcast
{
public:
using ValueMap = std::map<uint32_t, int64_t>;
const CSpell * spell;
spells::Target dest;
int64_t value;
PossibleSpellcast();
virtual ~PossibleSpellcast();
};

View File

@@ -0,0 +1,121 @@
/*
* PotentialTargets.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
*
*/
#include "StdInc.h"
#include "PotentialTargets.h"
#include "../../lib/CStack.h"//todo: remove
PotentialTargets::PotentialTargets(const battle::Unit * attacker, const HypotheticBattle * state)
{
auto attIter = state->stackStates.find(attacker->unitId());
const battle::Unit * attackerInfo = (attIter == state->stackStates.end()) ? attacker : attIter->second.get();
auto reachability = state->getReachability(attackerInfo);
auto avHexes = state->battleGetAvailableHexes(reachability, attackerInfo);
//FIXME: this should part of battleGetAvailableHexes
bool forceTarget = false;
const battle::Unit * forcedTarget = nullptr;
BattleHex forcedHex;
if(attackerInfo->hasBonusOfType(Bonus::ATTACKS_NEAREST_CREATURE))
{
forceTarget = true;
auto nearest = state->getNearestStack(attackerInfo);
if(nearest.first != nullptr)
{
forcedTarget = nearest.first;
forcedHex = nearest.second;
}
}
auto aliveUnits = state->battleGetUnitsIf([=](const battle::Unit * unit)
{
return unit->isValidTarget() && unit->unitId() != attackerInfo->unitId();
});
for(auto defender : aliveUnits)
{
if(!forceTarget && !state->battleMatchOwner(attackerInfo, defender))
continue;
auto GenerateAttackInfo = [&](bool shooting, BattleHex hex) -> AttackPossibility
{
auto bai = BattleAttackInfo(attackerInfo, defender, shooting);
if(hex.isValid() && !shooting)
bai.chargedFields = reachability.distances[hex];
return AttackPossibility::evaluate(bai, hex, state);
};
if(forceTarget)
{
if(forcedTarget && defender->unitId() == forcedTarget->unitId())
possibleAttacks.push_back(GenerateAttackInfo(false, forcedHex));
else
unreachableEnemies.push_back(defender);
}
else if(state->battleCanShoot(attackerInfo, defender->getPosition()))
{
possibleAttacks.push_back(GenerateAttackInfo(true, BattleHex::INVALID));
}
else
{
for(BattleHex hex : avHexes)
{
if(!CStack::isMeleeAttackPossible(attackerInfo, defender, hex))
continue;
auto bai = GenerateAttackInfo(false, hex);
if(!bai.affectedUnits.empty())
possibleAttacks.push_back(bai);
}
if(!vstd::contains_if(possibleAttacks, [=](const AttackPossibility & pa) { return pa.attack.defender->unitId() == defender->unitId(); }))
unreachableEnemies.push_back(defender);
}
}
boost::sort(possibleAttacks, [](const AttackPossibility & lhs, const AttackPossibility & rhs) -> bool
{
if(lhs.collateralDamage > rhs.collateralDamage)
return false;
if(lhs.collateralDamage < rhs.collateralDamage)
return true;
return (lhs.damageDealt + lhs.shootersBlockedDmg - lhs.damageReceived > rhs.damageDealt + rhs.shootersBlockedDmg - rhs.damageReceived);
});
if (!possibleAttacks.empty())
{
auto &bestAp = possibleAttacks[0];
logGlobal->info("Battle AI best: %s -> %s at %d from %d, affects %d units: %lld %lld %lld %lld",
bestAp.attack.attacker->unitType()->identifier,
state->battleGetUnitByPos(bestAp.dest)->unitType()->identifier,
(int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(),
bestAp.damageDealt, bestAp.damageReceived, bestAp.collateralDamage, bestAp.shootersBlockedDmg);
}
}
int64_t PotentialTargets::bestActionValue() const
{
if(possibleAttacks.empty())
return 0;
return bestAction().attackValue();
}
AttackPossibility PotentialTargets::bestAction() const
{
if(possibleAttacks.empty())
throw std::runtime_error("No best action, since we don't have any actions");
return possibleAttacks[0];
//return *vstd::maxElementByFun(possibleAttacks, [](const AttackPossibility &ap) { return ap.attackValue(); } );
}

View File

@@ -0,0 +1,24 @@
/*
* PotentialTargets.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 "AttackPossibility.h"
class PotentialTargets
{
public:
std::vector<AttackPossibility> possibleAttacks;
std::vector<const battle::Unit *> unreachableEnemies;
PotentialTargets(){};
PotentialTargets(const battle::Unit * attacker, const HypotheticBattle * state);
AttackPossibility bestAction() const;
int64_t bestActionValue() const;
};

View File

@@ -0,0 +1,525 @@
/*
* StackWithBonuses.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
*
*/
#include "StdInc.h"
#include "StackWithBonuses.h"
#include <vcmi/events/EventBus.h>
#include "../../lib/NetPacks.h"
#include "../../lib/CStack.h"
#include "../../lib/ScriptHandler.h"
using scripting::Pool;
void actualizeEffect(TBonusListPtr target, const Bonus & ef)
{
for(auto & bonus : *target) //TODO: optimize
{
if(bonus->source == Bonus::SPELL_EFFECT && bonus->type == ef.type && bonus->subtype == ef.subtype)
{
if(bonus->turnsRemain < ef.turnsRemain)
{
bonus.reset(new Bonus(*bonus));
bonus->turnsRemain = ef.turnsRemain;
}
}
}
}
StackWithBonuses::StackWithBonuses(const HypotheticBattle * Owner, const CStack * Stack)
: battle::CUnitState(),
origBearer(Stack),
owner(Owner),
type(Stack->unitType()),
baseAmount(Stack->unitBaseAmount()),
id(Stack->unitId()),
side(Stack->unitSide()),
player(Stack->unitOwner()),
slot(Stack->unitSlot())
{
localInit(Owner);
battle::CUnitState::operator=(*Stack);
}
StackWithBonuses::StackWithBonuses(const HypotheticBattle * Owner, const battle::UnitInfo & info)
: battle::CUnitState(),
origBearer(nullptr),
owner(Owner),
baseAmount(info.count),
id(info.id),
side(info.side),
slot(SlotID::SUMMONED_SLOT_PLACEHOLDER)
{
type = info.type.toCreature();
origBearer = type;
player = Owner->getSidePlayer(side);
localInit(Owner);
position = info.position;
summoned = info.summoned;
}
StackWithBonuses::~StackWithBonuses() = default;
StackWithBonuses & StackWithBonuses::operator=(const battle::CUnitState & other)
{
battle::CUnitState::operator=(other);
return *this;
}
const CCreature * StackWithBonuses::unitType() const
{
return type;
}
int32_t StackWithBonuses::unitBaseAmount() const
{
return baseAmount;
}
uint32_t StackWithBonuses::unitId() const
{
return id;
}
ui8 StackWithBonuses::unitSide() const
{
return side;
}
PlayerColor StackWithBonuses::unitOwner() const
{
return player;
}
SlotID StackWithBonuses::unitSlot() const
{
return slot;
}
TConstBonusListPtr StackWithBonuses::getAllBonuses(const CSelector & selector, const CSelector & limit,
const CBonusSystemNode * root, const std::string & cachingStr) const
{
TBonusListPtr ret = std::make_shared<BonusList>();
TConstBonusListPtr originalList = origBearer->getAllBonuses(selector, limit, root, cachingStr);
vstd::copy_if(*originalList, std::back_inserter(*ret), [this](const std::shared_ptr<Bonus> & b)
{
return !vstd::contains(bonusesToRemove, b);
});
for(const Bonus & bonus : bonusesToUpdate)
{
if(selector(&bonus) && (!limit || !limit(&bonus)))
{
if(ret->getFirst(Selector::source(Bonus::SPELL_EFFECT, bonus.sid).And(Selector::typeSubtype(bonus.type, bonus.subtype))))
{
actualizeEffect(ret, bonus);
}
else
{
auto b = std::make_shared<Bonus>(bonus);
ret->push_back(b);
}
}
}
for(auto & bonus : bonusesToAdd)
{
auto b = std::make_shared<Bonus>(bonus);
if(selector(b.get()) && (!limit || !limit(b.get())))
ret->push_back(b);
}
//TODO limiters?
return ret;
}
int64_t StackWithBonuses::getTreeVersion() const
{
return owner->getTreeVersion();
}
void StackWithBonuses::addUnitBonus(const std::vector<Bonus> & bonus)
{
vstd::concatenate(bonusesToAdd, bonus);
}
void StackWithBonuses::updateUnitBonus(const std::vector<Bonus> & bonus)
{
//TODO: optimize, actualize to last value
vstd::concatenate(bonusesToUpdate, bonus);
}
void StackWithBonuses::removeUnitBonus(const std::vector<Bonus> & bonus)
{
for(auto & one : bonus)
{
CSelector selector([&one](const Bonus * b) -> bool
{
//compare everything but turnsRemain, limiter and propagator
return one.duration == b->duration
&& one.type == b->type
&& one.subtype == b->subtype
&& one.source == b->source
&& one.val == b->val
&& one.sid == b->sid
&& one.valType == b->valType
&& one.additionalInfo == b->additionalInfo
&& one.effectRange == b->effectRange
&& one.description == b->description;
});
removeUnitBonus(selector);
}
}
void StackWithBonuses::removeUnitBonus(const CSelector & selector)
{
TConstBonusListPtr toRemove = origBearer->getBonuses(selector);
for(auto b : *toRemove)
bonusesToRemove.insert(b);
vstd::erase_if(bonusesToAdd, [&](const Bonus & b){return selector(&b);});
vstd::erase_if(bonusesToUpdate, [&](const Bonus & b){return selector(&b);});
}
void StackWithBonuses::spendMana(ServerCallback * server, const int spellCost) const
{
//TODO: evaluate cast use
}
HypotheticBattle::HypotheticBattle(const Environment * ENV, Subject realBattle)
: BattleProxy(realBattle),
env(ENV),
bonusTreeVersion(1)
{
auto activeUnit = realBattle->battleActiveUnit();
activeUnitId = activeUnit ? activeUnit->unitId() : -1;
nextId = 0x00F00000;
eventBus.reset(new events::EventBus());
localEnvironment.reset(new HypotheticEnvironment(this, env));
serverCallback.reset(new HypotheticServerCallback(this));
pool.reset(new scripting::PoolImpl(localEnvironment.get(), serverCallback.get()));
}
bool HypotheticBattle::unitHasAmmoCart(const battle::Unit * unit) const
{
//FIXME: check ammocart alive state here
return false;
}
PlayerColor HypotheticBattle::unitEffectiveOwner(const battle::Unit * unit) const
{
return battleGetOwner(unit);
}
std::shared_ptr<StackWithBonuses> HypotheticBattle::getForUpdate(uint32_t id)
{
auto iter = stackStates.find(id);
if(iter == stackStates.end())
{
const CStack * s = subject->battleGetStackByID(id, false);
auto ret = std::make_shared<StackWithBonuses>(this, s);
stackStates[id] = ret;
return ret;
}
else
{
return iter->second;
}
}
battle::Units HypotheticBattle::getUnitsIf(battle::UnitFilter predicate) const
{
battle::Units proxyed = BattleProxy::getUnitsIf(predicate);
battle::Units ret;
ret.reserve(proxyed.size());
for(auto unit : proxyed)
{
//unit was not changed, trust proxyed data
if(stackStates.find(unit->unitId()) == stackStates.end())
ret.push_back(unit);
}
for(auto id_unit : stackStates)
{
if(predicate(id_unit.second.get()))
ret.push_back(id_unit.second.get());
}
return ret;
}
int32_t HypotheticBattle::getActiveStackID() const
{
return activeUnitId;
}
void HypotheticBattle::nextRound(int32_t roundNr)
{
//TODO:HypotheticBattle::nextRound
for(auto unit : battleAliveUnits())
{
auto forUpdate = getForUpdate(unit->unitId());
//TODO: update Bonus::NTurns effects
forUpdate->afterNewRound();
}
}
void HypotheticBattle::nextTurn(uint32_t unitId)
{
activeUnitId = unitId;
auto unit = getForUpdate(unitId);
unit->removeUnitBonus(Bonus::UntilGetsTurn);
unit->afterGetsTurn();
}
void HypotheticBattle::addUnit(uint32_t id, const JsonNode & data)
{
battle::UnitInfo info;
info.load(id, data);
std::shared_ptr<StackWithBonuses> newUnit = std::make_shared<StackWithBonuses>(this, info);
stackStates[newUnit->unitId()] = newUnit;
}
void HypotheticBattle::moveUnit(uint32_t id, BattleHex destination)
{
std::shared_ptr<StackWithBonuses> changed = getForUpdate(id);
changed->position = destination;
}
void HypotheticBattle::setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta)
{
std::shared_ptr<StackWithBonuses> changed = getForUpdate(id);
changed->load(data);
if(healthDelta < 0)
{
changed->removeUnitBonus(Bonus::UntilBeingAttacked);
}
}
void HypotheticBattle::removeUnit(uint32_t id)
{
std::set<uint32_t> ids;
ids.insert(id);
while(!ids.empty())
{
auto toRemoveId = *ids.begin();
auto toRemove = getForUpdate(toRemoveId);
if(!toRemove->ghost)
{
toRemove->onRemoved();
//TODO: emulate detachFromAll() somehow
//stack may be removed instantly (not being killed first)
//handle clone remove also here
if(toRemove->cloneID >= 0)
{
ids.insert(toRemove->cloneID);
toRemove->cloneID = -1;
}
//TODO: cleanup remaining clone links if any
// for(auto s : stacks)
// {
// if(s->cloneID == toRemoveId)
// s->cloneID = -1;
// }
}
ids.erase(toRemoveId);
}
}
void HypotheticBattle::updateUnit(uint32_t id, const JsonNode & data)
{
//TODO:
}
void HypotheticBattle::addUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
{
getForUpdate(id)->addUnitBonus(bonus);
bonusTreeVersion++;
}
void HypotheticBattle::updateUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
{
getForUpdate(id)->updateUnitBonus(bonus);
bonusTreeVersion++;
}
void HypotheticBattle::removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus)
{
getForUpdate(id)->removeUnitBonus(bonus);
bonusTreeVersion++;
}
void HypotheticBattle::setWallState(int partOfWall, si8 state)
{
//TODO:HypotheticBattle::setWallState
}
void HypotheticBattle::addObstacle(const ObstacleChanges & changes)
{
//TODO:HypotheticBattle::addObstacle
}
void HypotheticBattle::updateObstacle(const ObstacleChanges& changes)
{
//TODO:HypotheticBattle::updateObstacle
}
void HypotheticBattle::removeObstacle(uint32_t id)
{
//TODO:HypotheticBattle::removeObstacle
}
uint32_t HypotheticBattle::nextUnitId() const
{
return nextId++;
}
int64_t HypotheticBattle::getActualDamage(const TDmgRange & damage, int32_t attackerCount, vstd::RNG & rng) const
{
return (damage.first + damage.second) / 2;
}
int64_t HypotheticBattle::getTreeVersion() const
{
return getBattleNode()->getTreeVersion() + bonusTreeVersion;
}
Pool * HypotheticBattle::getContextPool() const
{
return pool.get();
}
ServerCallback * HypotheticBattle::getServerCallback()
{
return serverCallback.get();
}
HypotheticBattle::HypotheticServerCallback::HypotheticServerCallback(HypotheticBattle * owner_)
:owner(owner_)
{
}
void HypotheticBattle::HypotheticServerCallback::complain(const std::string & problem)
{
logAi->error(problem);
}
bool HypotheticBattle::HypotheticServerCallback::describeChanges() const
{
return false;
}
vstd::RNG * HypotheticBattle::HypotheticServerCallback::getRNG()
{
return &rngStub;
}
void HypotheticBattle::HypotheticServerCallback::apply(CPackForClient * pack)
{
logAi->error("Package of type %s is not allowed in battle evaluation", typeid(pack).name());
}
void HypotheticBattle::HypotheticServerCallback::apply(BattleLogMessage * pack)
{
pack->applyBattle(owner);
}
void HypotheticBattle::HypotheticServerCallback::apply(BattleStackMoved * pack)
{
pack->applyBattle(owner);
}
void HypotheticBattle::HypotheticServerCallback::apply(BattleUnitsChanged * pack)
{
pack->applyBattle(owner);
}
void HypotheticBattle::HypotheticServerCallback::apply(SetStackEffect * pack)
{
pack->applyBattle(owner);
}
void HypotheticBattle::HypotheticServerCallback::apply(StacksInjured * pack)
{
pack->applyBattle(owner);
}
void HypotheticBattle::HypotheticServerCallback::apply(BattleObstaclesChanged * pack)
{
pack->applyBattle(owner);
}
void HypotheticBattle::HypotheticServerCallback::apply(CatapultAttack * pack)
{
pack->applyBattle(owner);
}
HypotheticBattle::HypotheticEnvironment::HypotheticEnvironment(HypotheticBattle * owner_, const Environment * upperEnvironment)
: owner(owner_),
env(upperEnvironment)
{
}
const Services * HypotheticBattle::HypotheticEnvironment::services() const
{
return env->services();
}
const Environment::BattleCb * HypotheticBattle::HypotheticEnvironment::battle() const
{
return owner;
}
const Environment::GameCb * HypotheticBattle::HypotheticEnvironment::game() const
{
return env->game();
}
vstd::CLoggerBase * HypotheticBattle::HypotheticEnvironment::logger() const
{
return env->logger();
}
events::EventBus * HypotheticBattle::HypotheticEnvironment::eventBus() const
{
return owner->eventBus.get();
}

View File

@@ -0,0 +1,194 @@
/*
* StackWithBonuses.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 <vstd/RNG.h>
#include <vcmi/Environment.h>
#include <vcmi/ServerCallback.h>
#include "../../lib/HeroBonus.h"
#include "../../lib/battle/BattleProxy.h"
#include "../../lib/battle/CUnitState.h"
class HypotheticBattle;
class CStack;
///Fake random generator, used by AI to evaluate random server behavior
class RNGStub : public vstd::RNG
{
public:
vstd::TRandI64 getInt64Range(int64_t lower, int64_t upper) override
{
return [=]()->int64_t
{
return (lower + upper)/2;
};
}
vstd::TRand getDoubleRange(double lower, double upper) override
{
return [=]()->double
{
return (lower + upper)/2;
};
}
};
class StackWithBonuses : public battle::CUnitState, public virtual IBonusBearer
{
public:
std::vector<Bonus> bonusesToAdd;
std::vector<Bonus> bonusesToUpdate;
std::set<std::shared_ptr<Bonus>> bonusesToRemove;
StackWithBonuses(const HypotheticBattle * Owner, const CStack * Stack);
StackWithBonuses(const HypotheticBattle * Owner, const battle::UnitInfo & info);
virtual ~StackWithBonuses();
StackWithBonuses & operator= (const battle::CUnitState & other);
///IUnitInfo
const CCreature * unitType() const override;
int32_t unitBaseAmount() const override;
uint32_t unitId() const override;
ui8 unitSide() const override;
PlayerColor unitOwner() const override;
SlotID unitSlot() const override;
///IBonusBearer
TConstBonusListPtr getAllBonuses(const CSelector & selector, const CSelector & limit,
const CBonusSystemNode * root = nullptr, const std::string & cachingStr = "") const override;
int64_t getTreeVersion() const override;
void addUnitBonus(const std::vector<Bonus> & bonus);
void updateUnitBonus(const std::vector<Bonus> & bonus);
void removeUnitBonus(const std::vector<Bonus> & bonus);
void removeUnitBonus(const CSelector & selector);
void spendMana(ServerCallback * server, const int spellCost) const override;
private:
const IBonusBearer * origBearer;
const HypotheticBattle * owner;
const CCreature * type;
ui32 baseAmount;
uint32_t id;
ui8 side;
PlayerColor player;
SlotID slot;
};
class HypotheticBattle : public BattleProxy, public battle::IUnitEnvironment
{
public:
std::map<uint32_t, std::shared_ptr<StackWithBonuses>> stackStates;
const Environment * env;
HypotheticBattle(const Environment * ENV, Subject realBattle);
bool unitHasAmmoCart(const battle::Unit * unit) const override;
PlayerColor unitEffectiveOwner(const battle::Unit * unit) const override;
std::shared_ptr<StackWithBonuses> getForUpdate(uint32_t id);
int32_t getActiveStackID() const override;
battle::Units getUnitsIf(battle::UnitFilter predicate) const override;
void nextRound(int32_t roundNr) override;
void nextTurn(uint32_t unitId) override;
void addUnit(uint32_t id, const JsonNode & data) override;
void setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta) override;
void moveUnit(uint32_t id, BattleHex destination) override;
void removeUnit(uint32_t id) override;
void updateUnit(uint32_t id, const JsonNode & data) override;
void addUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
void updateUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
void removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
void setWallState(int partOfWall, si8 state) override;
void addObstacle(const ObstacleChanges & changes) override;
void updateObstacle(const ObstacleChanges& changes) override;
void removeObstacle(uint32_t id) override;
uint32_t nextUnitId() const override;
int64_t getActualDamage(const TDmgRange & damage, int32_t attackerCount, vstd::RNG & rng) const override;
int64_t getTreeVersion() const;
scripting::Pool * getContextPool() const override;
ServerCallback * getServerCallback();
private:
class HypotheticServerCallback : public ServerCallback
{
public:
HypotheticServerCallback(HypotheticBattle * owner_);
void complain(const std::string & problem) override;
bool describeChanges() const override;
vstd::RNG * getRNG() override;
void apply(CPackForClient * pack) override;
void apply(BattleLogMessage * pack) override;
void apply(BattleStackMoved * pack) override;
void apply(BattleUnitsChanged * pack) override;
void apply(SetStackEffect * pack) override;
void apply(StacksInjured * pack) override;
void apply(BattleObstaclesChanged * pack) override;
void apply(CatapultAttack * pack) override;
private:
HypotheticBattle * owner;
RNGStub rngStub;
};
class HypotheticEnvironment : public Environment
{
public:
HypotheticEnvironment(HypotheticBattle * owner_, const Environment * upperEnvironment);
const Services * services() const override;
const BattleCb * battle() const override;
const GameCb * game() const override;
vstd::CLoggerBase * logger() const override;
events::EventBus * eventBus() const override;
private:
HypotheticBattle * owner;
const Environment * env;
};
int32_t bonusTreeVersion;
int32_t activeUnitId;
mutable uint32_t nextId;
std::unique_ptr<HypotheticServerCallback> serverCallback;
std::unique_ptr<HypotheticEnvironment> localEnvironment;
mutable std::shared_ptr<scripting::Pool> pool;
mutable std::shared_ptr<events::EventBus> eventBus;
};

11
AI/BattleML/StdInc.cpp Normal file
View File

@@ -0,0 +1,11 @@
/*
* StdInc.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
*
*/
// Creates the precompiled header
#include "StdInc.h"

15
AI/BattleML/StdInc.h Normal file
View File

@@ -0,0 +1,15 @@
/*
* StdInc.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 "../../Global.h"
// This header should be treated as a pre compiled header file(PCH) in the compiler building settings.
// Here you can add specific libraries and macros which are specific to this project.

73
AI/BattleML/ThreatMap.cpp Normal file
View File

@@ -0,0 +1,73 @@
/*
* ThreatMap.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
*
*/
#include "StdInc.h"
/*
#include "ThreatMap.h"
#include "StdInc.h"
template <typename Container, typename Pred>
auto sum(const Container & c, Pred p) -> decltype(p(*std::begin(c)))
{
double ret = 0;
for(const auto &element : c)
{
ret += p(element);
}
return ret;
}
ThreatMap::ThreatMap(const CStack *Endangered) : endangered(Endangered)
{
sufferedDamage.fill(0);
for(const CStack *enemy : getCbc()->battleGetStacks())
{
//Consider only stacks of different owner
if(enemy->side == endangered->side)
continue;
//Look-up which tiles can be melee-attacked
std::array<bool, GameConstants::BFIELD_SIZE> meleeAttackable;
meleeAttackable.fill(false);
auto enemyReachability = getCbc()->getReachability(enemy);
for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
{
if(enemyReachability.isReachable(i))
{
meleeAttackable[i] = true;
for(auto n : BattleHex(i).neighbouringTiles())
meleeAttackable[n] = true;
}
}
//Gather possible assaults
for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
{
if(getCbc()->battleCanShoot(enemy, i))
threatMap[i].push_back(BattleAttackInfo(enemy, endangered, true));
else if(meleeAttackable[i])
{
BattleAttackInfo bai(enemy, endangered, false);
bai.chargedFields = std::max(BattleHex::getDistance(enemy->position, i) - 1, 0); //TODO check real distance (BFS), not just metric
threatMap[i].push_back(BattleAttackInfo(bai));
}
}
}
for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
{
sufferedDamage[i] = sum(threatMap[i], [](const BattleAttackInfo &bai) -> int
{
auto dmg = getCbc()->calculateDmgRange(bai);
return (dmg.first + dmg.second)/2;
});
}
}
*/ // These lines may be usefull but they are't used in the code.

25
AI/BattleML/ThreatMap.h Normal file
View File

@@ -0,0 +1,25 @@
/*
* ThreatMap.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 "common.h"
#include "CCallback.h"
/*
class ThreatMap
{
public:
std::array<std::vector<BattleAttackInfo>, GameConstants::BFIELD_SIZE> threatMap; // [hexNr] -> enemies able to strike
const CStack *endangered;
std::array<int, GameConstants::BFIELD_SIZE> sufferedDamage;
ThreatMap(const CStack *Endangered);
};*/ // These lines may be usefull but they are't used in the code.

23
AI/BattleML/common.cpp Normal file
View File

@@ -0,0 +1,23 @@
/*
* common.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
*
*/
#include "StdInc.h"
#include "common.h"
std::shared_ptr<CBattleCallback> cbc;
void setCbc(std::shared_ptr<CBattleCallback> cb)
{
cbc = cb;
}
std::shared_ptr<CBattleCallback> getCbc()
{
return cbc;
}

26
AI/BattleML/common.h Normal file
View File

@@ -0,0 +1,26 @@
/*
* common.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
class CBattleCallback;
template<typename Key, typename Val, typename Val2>
const Val getValOr(const std::map<Key, Val> &Map, const Key &key, const Val2 defaultValue)
{
//returning references here won't work: defaultValue must be converted into Val, creating temporary
auto i = Map.find(key);
if(i != Map.end())
return i->second;
else
return defaultValue;
}
void setCbc(std::shared_ptr<CBattleCallback> cb);
std::shared_ptr<CBattleCallback> getCbc();

33
AI/BattleML/main.cpp Normal file
View File

@@ -0,0 +1,33 @@
/*
* main.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
*
*/
#include "StdInc.h"
#include "../../lib/AI_Base.h"
#include "BattleAI.h"
#ifdef __GNUC__
#define strcpy_s(a, b, c) strncpy(a, c, b)
#endif
static const char *g_cszAiName = "Battle AI";
extern "C" DLL_EXPORT int GetGlobalAiVersion()
{
return AI_INTERFACE_VER;
}
extern "C" DLL_EXPORT void GetAiName(char* name)
{
strcpy_s(name, strlen(g_cszAiName) + 1, g_cszAiName);
}
extern "C" DLL_EXPORT void GetNewBattleAI(std::shared_ptr<CBattleGameInterface> &out)
{
out = std::make_shared<CBattleAI>();
}

View File

@@ -37,6 +37,7 @@ endif()
#######################################
add_subdirectory(BattleAI)
add_subdirectory(BattleML)
add_subdirectory(StupidAI)
add_subdirectory(EmptyAI)
add_subdirectory(VCAI)

View File

@@ -163,7 +163,7 @@ if(ENABLE_DEBUG_CONSOLE)
else()
add_executable(vcmiclient WIN32 ${client_SRCS} ${client_HEADERS} ${client_ICON})
endif(ENABLE_DEBUG_CONSOLE)
add_dependencies(vcmiclient vcmiserver BattleAI StupidAI VCAI Nullkiller)
add_dependencies(vcmiclient vcmiserver BattleAI StupidAI VCAI Nullkiller BattleML)
if(WIN32)
set_target_properties(vcmiclient

View File

@@ -102,6 +102,11 @@
<string>StupidAI</string>
</property>
</item>
<item>
<property name="text">
<string>BattleML</string>
</property>
</item>
</widget>
</item>
<item row="2" column="6">
@@ -298,6 +303,11 @@
<string>StupidAI</string>
</property>
</item>
<item>
<property name="text">
<string>BattleML</string>
</property>
</item>
</widget>
</item>
<item row="2" column="1">
@@ -540,6 +550,11 @@
<string>StupidAI</string>
</property>
</item>
<item>
<property name="text">
<string>BattleML</string>
</property>
</item>
</widget>
</item>
<item row="10" column="7" colspan="3">