1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Merge pull request #3198 from IvanSavenko/ai_optimize

AI optimizations
This commit is contained in:
Ivan Savenko 2023-11-20 14:06:25 +02:00 committed by GitHub
commit aaefc07182
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 35 additions and 30 deletions

View File

@ -890,11 +890,12 @@ bool BattleExchangeEvaluator::checkPositionBlocksOurStacks(HypotheticBattle & hb
auto ratio = blockedUnitDamage / (blockedUnitDamage + activeUnitDamage);
auto unitReachability = turnBattle.getReachability(unit);
auto unitSpeed = unit->speed(turn); // Cached value, to avoid performance hit
for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
{
bool enemyUnit = false;
bool reachable = unitReachability.distances[hex] <= unit->speed(turn);
bool reachable = unitReachability.distances[hex] <= unitSpeed;
if(!reachable && unitReachability.accessibility[hex] == EAccessibility::ALIVE_STACK)
{
@ -906,7 +907,7 @@ bool BattleExchangeEvaluator::checkPositionBlocksOurStacks(HypotheticBattle & hb
for(BattleHex neighbor : hex.neighbouringTiles())
{
reachable = unitReachability.distances[neighbor] <= unit->speed(turn);
reachable = unitReachability.distances[neighbor] <= unitSpeed;
if(reachable) break;
}

View File

@ -298,7 +298,7 @@ std::shared_ptr<StackWithBonuses> HypotheticBattle::getForUpdate(uint32_t id)
}
}
battle::Units HypotheticBattle::getUnitsIf(battle::UnitFilter predicate) const
battle::Units HypotheticBattle::getUnitsIf(const battle::UnitFilter & predicate) const
{
battle::Units proxyed = BattleProxy::getUnitsIf(predicate);

View File

@ -114,7 +114,7 @@ public:
int32_t getActiveStackID() const override;
battle::Units getUnitsIf(battle::UnitFilter predicate) const override;
battle::Units getUnitsIf(const battle::UnitFilter & predicate) const override;
void nextRound() override;
void nextTurn(uint32_t unitId) override;

View File

@ -114,16 +114,20 @@ void CGuiHandler::renderFrame()
{
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
if(nullptr != curInt)
if (nullptr != curInt)
curInt->update();
if(settings["video"]["showfps"].Bool())
if (settings["video"]["showfps"].Bool())
drawFPSCounter();
}
SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
SDL_RenderClear(mainRenderer);
SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
SDL_RenderClear(mainRenderer);
SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
{
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
CCS->curh->render();

View File

@ -562,14 +562,14 @@ int32_t BattleInfo::getActiveStackID() const
return activeStack;
}
TStacks BattleInfo::getStacksIf(TStackFilter predicate) const
TStacks BattleInfo::getStacksIf(const TStackFilter & predicate) const
{
TStacks ret;
vstd::copy_if(stacks, std::back_inserter(ret), predicate);
return ret;
}
battle::Units BattleInfo::getUnitsIf(battle::UnitFilter predicate) const
battle::Units BattleInfo::getUnitsIf(const battle::UnitFilter & predicate) const
{
battle::Units ret;
vstd::copy_if(stacks, std::back_inserter(ret), predicate);

View File

@ -85,9 +85,9 @@ public:
int32_t getActiveStackID() const override;
TStacks getStacksIf(TStackFilter predicate) const override;
TStacks getStacksIf(const TStackFilter & predicate) const override;
battle::Units getUnitsIf(battle::UnitFilter predicate) const override;
battle::Units getUnitsIf(const battle::UnitFilter & predicate) const override;
BattleField getBattlefieldType() const override;
TerrainId getTerrainType() const override;

View File

@ -40,12 +40,12 @@ int32_t BattleProxy::getActiveStackID() const
return -1;
}
TStacks BattleProxy::getStacksIf(TStackFilter predicate) const
TStacks BattleProxy::getStacksIf(const TStackFilter & predicate) const
{
return subject->battleGetStacksIf(predicate);
}
battle::Units BattleProxy::getUnitsIf(battle::UnitFilter predicate) const
battle::Units BattleProxy::getUnitsIf(const battle::UnitFilter & predicate) const
{
return subject->battleGetUnitsIf(predicate);
}

View File

@ -29,9 +29,9 @@ public:
int32_t getActiveStackID() const override;
TStacks getStacksIf(TStackFilter predicate) const override;
TStacks getStacksIf(const TStackFilter & predicate) const override;
battle::Units getUnitsIf(battle::UnitFilter predicate) const override;
battle::Units getUnitsIf(const battle::UnitFilter & predicate) const override;
BattleField getBattlefieldType() const override;
TerrainId getTerrainType() const override;

View File

@ -357,7 +357,7 @@ const battle::Unit * CBattleInfoCallback::battleGetUnitByPos(BattleHex pos, bool
auto ret = battleGetUnitsIf([=](const battle::Unit * unit)
{
return !unit->isGhost()
&& vstd::contains(battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()), pos)
&& unit->coversPos(pos)
&& (!onlyAlive || unit->alive());
});

View File

@ -109,13 +109,13 @@ TStacks CBattleInfoEssentials::battleGetAllStacks(bool includeTurrets) const
});
}
TStacks CBattleInfoEssentials::battleGetStacksIf(TStackFilter predicate) const
TStacks CBattleInfoEssentials::battleGetStacksIf(const TStackFilter & predicate) const
{
RETURN_IF_NOT_BATTLE(TStacks());
return getBattle()->getStacksIf(std::move(predicate));
}
battle::Units CBattleInfoEssentials::battleGetUnitsIf(battle::UnitFilter predicate) const
battle::Units CBattleInfoEssentials::battleGetUnitsIf(const battle::UnitFilter & predicate) const
{
RETURN_IF_NOT_BATTLE(battle::Units());
return getBattle()->getUnitsIf(predicate);

View File

@ -62,8 +62,8 @@ public:
* @return filtered stacks
*
*/
TStacks battleGetStacksIf(TStackFilter predicate) const; //deprecated
battle::Units battleGetUnitsIf(battle::UnitFilter predicate) const override;
TStacks battleGetStacksIf(const TStackFilter & predicate) const; //deprecated
battle::Units battleGetUnitsIf(const battle::UnitFilter & predicate) const override;
const battle::Unit * battleGetUnitByID(uint32_t ID) const override;
const battle::Unit * battleActiveUnit() const override;

View File

@ -46,7 +46,7 @@ int32_t CAmmo::available() const
bool CAmmo::canUse(int32_t amount) const
{
return !isLimited() || (available() - amount >= 0);
return (available() - amount >= 0) || !isLimited();
}
bool CAmmo::isLimited() const
@ -99,7 +99,7 @@ CShots & CShots::operator=(const CShots & other)
bool CShots::isLimited() const
{
return !env->unitHasAmmoCart(owner) || !shooter.getHasBonus();
return !shooter.getHasBonus() || !env->unitHasAmmoCart(owner);
}
void CShots::setEnv(const IUnitEnvironment * env_)

View File

@ -72,7 +72,7 @@ public:
virtual uint32_t battleNextUnitId() const = 0;
virtual battle::Units battleGetUnitsIf(battle::UnitFilter predicate) const = 0;
virtual battle::Units battleGetUnitsIf(const battle::UnitFilter & predicate) const = 0;
virtual const battle::Unit * battleGetUnitByID(uint32_t ID) const = 0;
virtual const battle::Unit * battleGetUnitByPos(BattleHex pos, bool onlyAlive = true) const = 0;

View File

@ -42,9 +42,9 @@ public:
virtual int32_t getActiveStackID() const = 0;
virtual TStacks getStacksIf(TStackFilter predicate) const = 0;
virtual TStacks getStacksIf(const TStackFilter & predicate) const = 0;
virtual battle::Units getUnitsIf(battle::UnitFilter predicate) const = 0;
virtual battle::Units getUnitsIf(const battle::UnitFilter & predicate) const = 0;
virtual BattleField getBattlefieldType() const = 0;
virtual TerrainId getTerrainType() const = 0;

View File

@ -29,7 +29,7 @@ public:
MOCK_CONST_METHOD0(battleNextUnitId, uint32_t());
MOCK_CONST_METHOD1(battleGetUnitsIf, battle::Units(battle::UnitFilter));
MOCK_CONST_METHOD1(battleGetUnitsIf, battle::Units(const battle::UnitFilter &));
MOCK_CONST_METHOD1(battleGetUnitByID, const battle::Unit *(uint32_t));
MOCK_CONST_METHOD2(battleGetUnitByPos, const battle::Unit *(BattleHex, bool));

View File

@ -17,8 +17,8 @@ class BattleStateMock : public IBattleState
{
public:
MOCK_CONST_METHOD0(getActiveStackID, int32_t());
MOCK_CONST_METHOD1(getStacksIf, TStacks(TStackFilter));
MOCK_CONST_METHOD1(getUnitsIf, battle::Units(battle::UnitFilter));
MOCK_CONST_METHOD1(getStacksIf, TStacks(const TStackFilter&));
MOCK_CONST_METHOD1(getUnitsIf, battle::Units(const battle::UnitFilter &));
MOCK_CONST_METHOD0(getBattlefieldType, BattleField());
MOCK_CONST_METHOD0(getTerrainType, TerrainId());
MOCK_CONST_METHOD0(getAllObstacles, IBattleInfo::ObstacleCList());