mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-20 20:23:03 +02:00
Use existing callback method instead of custom one in AI
This commit is contained in:
parent
64db89711e
commit
bb7cf3ffbf
@ -119,50 +119,6 @@ std::vector<BattleHex> BattleEvaluator::getBrokenWallMoatHexes() const
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<BattleHex> BattleEvaluator::getCastleHexes()
|
|
||||||
{
|
|
||||||
std::vector<BattleHex> result;
|
|
||||||
|
|
||||||
// Loop through all wall parts
|
|
||||||
|
|
||||||
std::vector<BattleHex> wallHexes;
|
|
||||||
wallHexes.push_back(50);
|
|
||||||
wallHexes.push_back(183);
|
|
||||||
wallHexes.push_back(182);
|
|
||||||
wallHexes.push_back(130);
|
|
||||||
wallHexes.push_back(78);
|
|
||||||
wallHexes.push_back(29);
|
|
||||||
wallHexes.push_back(12);
|
|
||||||
wallHexes.push_back(97);
|
|
||||||
wallHexes.push_back(45);
|
|
||||||
wallHexes.push_back(62);
|
|
||||||
wallHexes.push_back(112);
|
|
||||||
wallHexes.push_back(147);
|
|
||||||
wallHexes.push_back(165);
|
|
||||||
|
|
||||||
for (BattleHex wallHex : wallHexes) {
|
|
||||||
// Get the starting x-coordinate of the wall hex
|
|
||||||
int startX = wallHex.getX();
|
|
||||||
|
|
||||||
// Initialize current hex with the wall hex
|
|
||||||
BattleHex currentHex = wallHex;
|
|
||||||
while (currentHex.isValid()) {
|
|
||||||
// Check if the x-coordinate has wrapped (smaller than the starting x)
|
|
||||||
if (currentHex.getX() < startX) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the hex to the result
|
|
||||||
result.push_back(currentHex);
|
|
||||||
|
|
||||||
// Move to the next hex to the right
|
|
||||||
currentHex = currentHex.cloneInDirection(BattleHex::RIGHT, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BattleEvaluator::hasWorkingTowers() const
|
bool BattleEvaluator::hasWorkingTowers() const
|
||||||
{
|
{
|
||||||
bool keepIntact = cb->getBattle(battleID)->battleGetWallState(EWallPart::KEEP) != EWallState::NONE && cb->getBattle(battleID)->battleGetWallState(EWallPart::KEEP) != EWallState::DESTROYED;
|
bool keepIntact = cb->getBattle(battleID)->battleGetWallState(EWallPart::KEEP) != EWallState::NONE && cb->getBattle(battleID)->battleGetWallState(EWallPart::KEEP) != EWallState::DESTROYED;
|
||||||
@ -221,11 +177,6 @@ BattleAction BattleEvaluator::selectStackAction(const CStack * stack)
|
|||||||
&& !stack->canShoot()
|
&& !stack->canShoot()
|
||||||
&& hasWorkingTowers()
|
&& hasWorkingTowers()
|
||||||
&& !enemyMellee.empty();
|
&& !enemyMellee.empty();
|
||||||
std::vector<BattleHex> castleHexes = getCastleHexes();
|
|
||||||
for (auto hex : castleHexes)
|
|
||||||
{
|
|
||||||
logAi->trace("Castlehex ID: %d Y: %d X: %d", hex, hex.getY(), hex.getX());
|
|
||||||
}
|
|
||||||
|
|
||||||
if(targets->possibleAttacks.empty() && bestSpellcast.has_value())
|
if(targets->possibleAttacks.empty() && bestSpellcast.has_value())
|
||||||
{
|
{
|
||||||
@ -292,10 +243,7 @@ BattleAction BattleEvaluator::selectStackAction(const CStack * stack)
|
|||||||
return BattleAction::makeDefend(stack);
|
return BattleAction::makeDefend(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isTargetOutsideFort = std::none_of(castleHexes.begin(), castleHexes.end(),
|
bool isTargetOutsideFort = !hb->battleIsInsideWalls(bestAttack.from);
|
||||||
[&](const BattleHex& hex) {
|
|
||||||
return hex == bestAttack.from;
|
|
||||||
});
|
|
||||||
bool siegeDefense = stack->unitSide() == BattleSide::DEFENDER
|
bool siegeDefense = stack->unitSide() == BattleSide::DEFENDER
|
||||||
&& !bestAttack.attack.shooting
|
&& !bestAttack.attack.shooting
|
||||||
&& hasWorkingTowers()
|
&& hasWorkingTowers()
|
||||||
@ -424,14 +372,8 @@ BattleAction BattleEvaluator::goTowardsNearest(const CStack * stack, std::vector
|
|||||||
if (siegeDefense)
|
if (siegeDefense)
|
||||||
{
|
{
|
||||||
vstd::erase_if(avHexes, [&](const BattleHex& hex) {
|
vstd::erase_if(avHexes, [&](const BattleHex& hex) {
|
||||||
std::vector<BattleHex> castleHexes = getCastleHexes();
|
return !cb->getBattle(battleID)->battleIsInsideWalls(hex);
|
||||||
|
});
|
||||||
bool isOutsideWall = std::none_of(castleHexes.begin(), castleHexes.end(),
|
|
||||||
[&](const BattleHex& checkhex) {
|
|
||||||
return checkhex == hex;
|
|
||||||
});
|
|
||||||
return isOutsideWall;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!avHexes.size() || !hexes.size()) //we are blocked or dest is blocked
|
if(!avHexes.size() || !hexes.size()) //we are blocked or dest is blocked
|
||||||
|
@ -53,7 +53,6 @@ public:
|
|||||||
std::optional<PossibleSpellcast> findBestCreatureSpell(const CStack * stack);
|
std::optional<PossibleSpellcast> findBestCreatureSpell(const CStack * stack);
|
||||||
BattleAction goTowardsNearest(const CStack * stack, std::vector<BattleHex> hexes, const PotentialTargets & targets);
|
BattleAction goTowardsNearest(const CStack * stack, std::vector<BattleHex> hexes, const PotentialTargets & targets);
|
||||||
std::vector<BattleHex> getBrokenWallMoatHexes() const;
|
std::vector<BattleHex> getBrokenWallMoatHexes() const;
|
||||||
static std::vector<BattleHex> getCastleHexes();
|
|
||||||
bool hasWorkingTowers() const;
|
bool hasWorkingTowers() const;
|
||||||
void evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps); //for offensive damaging spells only
|
void evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps); //for offensive damaging spells only
|
||||||
void print(const std::string & text) const;
|
void print(const std::string & text) const;
|
||||||
|
@ -218,7 +218,6 @@ EvaluationResult BattleExchangeEvaluator::findBestTarget(
|
|||||||
bool siegeDefense)
|
bool siegeDefense)
|
||||||
{
|
{
|
||||||
EvaluationResult result(targets.bestAction());
|
EvaluationResult result(targets.bestAction());
|
||||||
std::vector<BattleHex> castleHexes = BattleEvaluator::getCastleHexes();
|
|
||||||
|
|
||||||
if(!activeStack->waited() && !activeStack->acquireState()->hadMorale)
|
if(!activeStack->waited() && !activeStack->acquireState()->hadMorale)
|
||||||
{
|
{
|
||||||
@ -234,7 +233,7 @@ EvaluationResult BattleExchangeEvaluator::findBestTarget(
|
|||||||
|
|
||||||
for(auto & ap : targets.possibleAttacks)
|
for(auto & ap : targets.possibleAttacks)
|
||||||
{
|
{
|
||||||
if (siegeDefense && std::find(castleHexes.begin(), castleHexes.end(), ap.from) == castleHexes.end())
|
if (siegeDefense && !hb->battleIsInsideWalls(ap.from))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
float score = evaluateExchange(ap, 0, targets, damageCache, hbWaited);
|
float score = evaluateExchange(ap, 0, targets, damageCache, hbWaited);
|
||||||
@ -269,7 +268,7 @@ EvaluationResult BattleExchangeEvaluator::findBestTarget(
|
|||||||
|
|
||||||
for(auto & ap : targets.possibleAttacks)
|
for(auto & ap : targets.possibleAttacks)
|
||||||
{
|
{
|
||||||
if (siegeDefense && std::find(castleHexes.begin(), castleHexes.end(), ap.from) == castleHexes.end())
|
if (siegeDefense && !hb->battleIsInsideWalls(ap.from))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
float score = evaluateExchange(ap, 0, targets, damageCache, hb);
|
float score = evaluateExchange(ap, 0, targets, damageCache, hb);
|
||||||
|
Loading…
Reference in New Issue
Block a user