Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/fheroes2/ai/ai_battle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <functional>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <ostream>
Expand All @@ -39,6 +40,7 @@
#include <utility>
#include <vector>

#include "army_troop.h"
#include "artifact.h"
#include "artifact_info.h"
#include "battle.h"
Expand Down Expand Up @@ -68,6 +70,8 @@
#include "spell_info.h"
#include "spell_storage.h"

class Army;

namespace
{
const std::vector<int32_t> cellsUnderWallsIndexes = { 7, 28, 49, 72, 95 };
Expand Down Expand Up @@ -734,6 +738,45 @@ bool AI::BattlePlanner::isLimitOfTurnsExceeded( const Battle::Arena & arena, Bat
return false;
}

std::optional<const Battle::Unit *> AI::BattlePlanner::findNextUnit( const Battle::Arena & arena, const Battle::Unit & currentUnit )
{
const std::shared_ptr<const Battle::Units> order = arena.getOrderOfUnits();
const Battle::Units * units = order.get();

size_t i = 0;
const size_t count = units->size();
for ( ; i < count; i++ ) {
const Battle::Unit * unit = units->at( i );
if ( *unit == currentUnit ) {
break;
}
}
i++;
if ( i == count ) {
i = 0;
}

const Battle::Unit * next_unit = units->at( i );
assert( next_unit != nullptr );
return { next_unit };
Comment on lines +746 to +761

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this code check units for the next turn?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I could understand, _orderOfUnits is like a circular buffer, hence I tried to find current unit in it and get the next unit by index.

}

bool AI::BattlePlanner::isPostponeRetreat( const Battle::Arena & arena, const Battle::Unit & currentUnit )
{
const std::optional<const Battle::Unit *> maybeNextUnit = findNextUnit( arena, currentUnit );
if ( !maybeNextUnit.has_value() ) {
return false;
}
const Battle::Unit * nextUnit = maybeNextUnit.value();
assert( nextUnit != nullptr );

const Army * currentArmy = currentUnit.GetArmy();
const Army * nextArmy = nextUnit->GetArmy();

const bool isSameArmy = ( currentArmy != nullptr ) && ( currentArmy == nextArmy );

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to compare armies instead of units?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of that as the simplest way to check if two units belong to the same army. Otherwise I can make a function like bool Unit#isSameArmyAs(const Unit& unit) const to make it look more OOP (it would do the same logic inside though).

return isSameArmy && ( nextUnit->GetMorale() >= 0 );
}

Battle::Actions AI::BattlePlanner::planUnitTurn( Battle::Arena & arena, const Battle::Unit & currentUnit )
{
if ( currentUnit.Modes( Battle::SP_BERSERKER ) ) {
Expand All @@ -757,7 +800,7 @@ Battle::Actions AI::BattlePlanner::planUnitTurn( Battle::Arena & arena, const Ba
Surrender
};

const Outcome outcome = [this, &arena, actualHero]() {
const Outcome outcome = [this, &arena, actualHero, &currentUnit]() {
if ( !_considerRetreat ) {
return Outcome::ContinueBattle;
}
Expand Down Expand Up @@ -852,7 +895,7 @@ Battle::Actions AI::BattlePlanner::planUnitTurn( Battle::Arena & arena, const Ba

// If the hero has valuable artifacts, he should retreat so that these artifacts do not end up at the disposal of the enemy, especially in the case of an
// alliance war
if ( hasValuableArtifacts ) {
if ( hasValuableArtifacts && !isPostponeRetreat( arena, currentUnit ) ) {
return Outcome::Retreat;
}

Expand All @@ -862,7 +905,7 @@ Battle::Actions AI::BattlePlanner::planUnitTurn( Battle::Arena & arena, const Ba
}

// Otherwise, if this hero is relatively experienced, then he should retreat so that he can be hired again later
if ( actualHero->getTotalPrimarySkillLevel() >= minHeroTotalPrimarySkillLevelForRetreat ) {
if ( ( actualHero->getTotalPrimarySkillLevel() >= minHeroTotalPrimarySkillLevelForRetreat ) && !( isPostponeRetreat( arena, currentUnit ) ) ) {
return Outcome::Retreat;
}

Expand Down
6 changes: 5 additions & 1 deletion src/fheroes2/ai/ai_battle.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/***************************************************************************
* fheroes2: https://github.com/ihhub/fheroes2 *
* Copyright (C) 2024 - 2025 *
* Copyright (C) 2024 - 2026 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
Expand All @@ -21,6 +21,7 @@
#pragma once

#include <cstdint>
#include <optional>

#include "color.h"

Expand Down Expand Up @@ -120,6 +121,9 @@ namespace AI

static double getMeleeBestOutcome( Battle::Arena & arena, const Battle::Unit & currentUnit, const Battle::Units & enemies, BattleTargetPair & bestTarget );

static std::optional<const Battle::Unit *> findNextUnit( const Battle::Arena & arena, const Battle::Unit & currentUnit );
bool isPostponeRetreat( const Battle::Arena & arena, const Battle::Unit & currentUnit );
Comment thread
tau3 marked this conversation as resolved.

// When this limit of turns without deaths is exceeded for an attacking AI-controlled hero,
// the auto combat should be interrupted (one way or another)
static const uint32_t MAX_TURNS_WITHOUT_DEATHS{ 50 };
Expand Down
7 changes: 6 additions & 1 deletion src/fheroes2/battle/battle_arena.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/***************************************************************************
* fheroes2: https://github.com/ihhub/fheroes2 *
* Copyright (C) 2019 - 2025 *
* Copyright (C) 2019 - 2026 *
* *
* Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 *
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
Expand Down Expand Up @@ -152,6 +152,11 @@ namespace Battle
const Unit * GetTroopBoard( int32_t ) const;

Unit * GetTroopUID( uint32_t );
std::shared_ptr<const Units> getOrderOfUnits() const
{
return _orderOfUnits;
}
Comment on lines +155 to +158

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should revert this change: see my comments above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no objections here, but how to receive order of units otherwise? It's required to find the next unit.


const Unit * GetTroopUID( uint32_t ) const;

const SpellStorage & GetUsedSpells() const;
Expand Down
Loading