Skip to content

Commit

Permalink
Revert AICombatOptions -> AutocombatPreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
smanolloff committed Oct 9, 2024
1 parent e6341ce commit fb3edb6
Show file tree
Hide file tree
Showing 26 changed files with 70 additions and 91 deletions.
12 changes: 8 additions & 4 deletions AI/BattleAI/BattleAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "StackWithBonuses.h"
#include "EnemyInfo.h"
#include "battle/AICombatOptions.h"
#include "tbb/parallel_for.h"
#include "../../lib/CStopWatch.h"
#include "../../lib/CThreadHelper.h"
Expand Down Expand Up @@ -61,11 +60,10 @@ void logHexNumbers()
#endif
}

void CBattleAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AICombatOptions aiCombatOptions_)
void CBattleAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB)
{
env = ENV;
cb = CB;
aiCombatOptions = aiCombatOptions_;
playerID = *CB->getPlayerID();
wasWaitingForRealize = CB->waitTillRealize;
wasUnlockingGs = CB->unlockGsWhenWaiting;
Expand All @@ -76,6 +74,12 @@ void CBattleAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::share
logHexNumbers();
}

void CBattleAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AutocombatPreferences autocombatPreferences)
{
initBattleInterface(ENV, CB);
autobattlePreferences = autocombatPreferences;
}

BattleAction CBattleAI::useHealingTent(const BattleID & battleID, const CStack *stack)
{
auto healingTargets = cb->getBattle(battleID)->battleGetStacks(CBattleInfoEssentials::ONLY_MINE);
Expand Down Expand Up @@ -163,7 +167,7 @@ void CBattleAI::activeStack(const BattleID & battleID, const CStack * stack )

result = evaluator.selectStackAction(stack);

if(aiCombatOptions.enableSpellsUsage && !skipCastUntilNextBattle && evaluator.canCastSpell())
if(autobattlePreferences.enableSpellsUsage && !skipCastUntilNextBattle && evaluator.canCastSpell())
{
auto spelCasted = evaluator.attemptCastingSpell(stack);

Expand Down
6 changes: 3 additions & 3 deletions AI/BattleAI/BattleAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "../../lib/battle/ReachabilityInfo.h"
#include "PossibleSpellcast.h"
#include "PotentialTargets.h"
#include "battle/AICombatOptions.h"

VCMI_LIB_NAMESPACE_BEGIN

Expand Down Expand Up @@ -69,7 +68,8 @@ class CBattleAI : public CBattleGameInterface
CBattleAI();
~CBattleAI();

void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AICombatOptions aiCombatOptions) override;
void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB) override;
void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AutocombatPreferences autocombatPreferences) override;

void activeStack(const BattleID & battleID, const CStack * stack) override; //called when it's turn of that stack
void yourTacticPhase(const BattleID & battleID, int distance) override;
Expand All @@ -95,5 +95,5 @@ class CBattleAI : public CBattleGameInterface
//void battleTriggerEffect(const BattleTriggerEffect & bte) override;
//void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, BattleSide 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
AICombatOptions aiCombatOptions;
AutocombatPreferences autobattlePreferences = AutocombatPreferences();
};
3 changes: 1 addition & 2 deletions AI/EmptyAI/CEmptyAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
#include "../../lib/CStack.h"
#include "../../lib/battle/BattleAction.h"

void CEmptyAI::initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB, AICombatOptions aiCombatOptions_)
void CEmptyAI::initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB)
{
cb = CB;
env = ENV;
aiCombatOptions = aiCombatOptions_;
human=false;
playerID = *cb->getPlayerID();
}
Expand Down
2 changes: 1 addition & 1 deletion AI/EmptyAI/CEmptyAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CEmptyAI : public CGlobalAI
std::shared_ptr<CCallback> cb;

public:
void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB, AICombatOptions aiCombatOptions) override;
void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB) override;
void yourTurn(QueryID queryID) override;
void yourTacticPhase(const BattleID & battleID, int distance) override;
void activeStack(const BattleID & battleID, const CStack * stack) override;
Expand Down
3 changes: 1 addition & 2 deletions AI/Nullkiller/AIGateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,11 @@ std::optional<BattleAction> AIGateway::makeSurrenderRetreatDecision(const Battle
}


void AIGateway::initGameInterface(std::shared_ptr<Environment> env, std::shared_ptr<CCallback> CB, AICombatOptions aiCombatOptions_)
void AIGateway::initGameInterface(std::shared_ptr<Environment> env, std::shared_ptr<CCallback> CB)
{
LOG_TRACE(logAi);
myCb = CB;
cbc = CB;
aiCombatOptions = aiCombatOptions_;
this->env = env;

NET_EVENT_HANDLER;
Expand Down
2 changes: 1 addition & 1 deletion AI/Nullkiller/AIGateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class DLL_EXPORT AIGateway : public CAdventureAI

std::string getBattleAIName() const override;

void initGameInterface(std::shared_ptr<Environment> env, std::shared_ptr<CCallback> CB, AICombatOptions aiCombatOptions) override;
void initGameInterface(std::shared_ptr<Environment> env, std::shared_ptr<CCallback> CB) override;
void yourTurn(QueryID queryID) override;

void heroGotLevel(const CGHeroInstance * hero, PrimarySkill pskill, std::vector<SecondarySkill> & skills, QueryID queryID) override; //pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
Expand Down
7 changes: 6 additions & 1 deletion AI/StupidAI/StupidAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CStupidAI::~CStupidAI()
}
}

void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AICombatOptions _aiCombatOptions)
void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB)
{
print("init called, saving ptr to IBattleCallback");
env = ENV;
Expand All @@ -48,6 +48,11 @@ void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::share
CB->unlockGsWhenWaiting = false;
}

void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AutocombatPreferences autocombatPreferences)
{
initBattleInterface(ENV, CB);
}

void CStupidAI::actionFinished(const BattleID & battleID, const BattleAction &action)
{
print("actionFinished called");
Expand Down
4 changes: 2 additions & 2 deletions AI/StupidAI/StupidAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "../../lib/battle/BattleHex.h"
#include "../../lib/battle/ReachabilityInfo.h"
#include "../../lib/CGameInterface.h"
#include "battle/AICombatOptions.h"

class EnemyInfo;

Expand All @@ -30,7 +29,8 @@ class CStupidAI : public CBattleGameInterface
CStupidAI();
~CStupidAI();

void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AICombatOptions aiCombatOptions) override;
void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB) override;
void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AutocombatPreferences autocombatPreferences) override;

void actionFinished(const BattleID & battleID, const BattleAction &action) override;//occurs AFTER every action taken by any stack or by the hero
void actionStarted(const BattleID & battleID, const BattleAction &action) override;//occurs BEFORE every action taken by any stack or by the hero
Expand Down
4 changes: 2 additions & 2 deletions AI/VCAI/VCAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,13 +608,13 @@ void VCAI::showWorldViewEx(const std::vector<ObjectPosInfo> & objectPositions, b
NET_EVENT_HANDLER;
}

void VCAI::initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB, AICombatOptions aiCombatOptions_)
void VCAI::initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB)
{
LOG_TRACE(logAi);
env = ENV;
myCb = CB;
cbc = CB;
aiCombatOptions = aiCombatOptions_;

ah->init(CB.get());

NET_EVENT_HANDLER; //sets ah->rm->cb
Expand Down
2 changes: 1 addition & 1 deletion AI/VCAI/VCAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class DLL_EXPORT VCAI : public CAdventureAI

std::string getBattleAIName() const override;

void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB, AICombatOptions aiCombatOptions) override;
void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB) override;
void yourTurn(QueryID queryID) override;

void heroGotLevel(const CGHeroInstance * hero, PrimarySkill pskill, std::vector<SecondarySkill> & skills, QueryID queryID) override; //pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
Expand Down
11 changes: 6 additions & 5 deletions client/CPlayerInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "adventureMap/CInGameConsole.h"
#include "adventureMap/CList.h"

#include "battle/AICombatOptions.h"
#include "battle/BattleEffectsController.h"
#include "battle/BattleFieldController.h"
#include "battle/BattleInterface.h"
Expand Down Expand Up @@ -158,11 +157,10 @@ CPlayerInterface::~CPlayerInterface()
if (LOCPLINT == this)
LOCPLINT = nullptr;
}
void CPlayerInterface::initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB, AICombatOptions aiCombatOptions_)
void CPlayerInterface::initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB)
{
cb = CB;
env = ENV;
aiCombatOptions = aiCombatOptions_;

CCS->musich->loadTerrainMusicThemes();
initializeHeroTownList();
Expand Down Expand Up @@ -1795,9 +1793,12 @@ bool CPlayerInterface::capturedAllEvents()

void CPlayerInterface::prepareAutoFightingAI(const BattleID &bid, const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, BattleSide side)
{
aiCombatOptions.enableSpellsUsage = settings["battle"]["enableAutocombatSpells"].Bool();
autofightingAI = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());
autofightingAI->initBattleInterface(env, cb, aiCombatOptions);

AutocombatPreferences autocombatPreferences = AutocombatPreferences();
autocombatPreferences.enableSpellsUsage = settings["battle"]["enableAutocombatSpells"].Bool();

autofightingAI->initBattleInterface(env, cb, autocombatPreferences);
autofightingAI->battleStart(bid, army1, army2, tile, hero1, hero2, side, false);
isAutoFightOn = true;
cb->registerBattleInterface(autofightingAI);
Expand Down
2 changes: 1 addition & 1 deletion client/CPlayerInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class CPlayerInterface : public CGameInterface, public IUpdateable
protected: // Call-ins from server, should not be called directly, but only via GameInterface

void update() override;
void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB, AICombatOptions aiCombatOptions) override;
void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB) override;

void garrisonsChanged(ObjectInstanceID id1, ObjectInstanceID id2) override;
void buildChanged(const CGTownInstance *town, BuildingID buildingID, int what) override; //what: 1 - built, 2 - demolished
Expand Down
6 changes: 1 addition & 5 deletions client/CServerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "ServerRunner.h"
#include "GameChatHandler.h"
#include "CPlayerInterface.h"
#include "battle/AICombatOptions.h"
#include "gui/CGuiHandler.h"
#include "gui/WindowHandler.h"

Expand Down Expand Up @@ -94,7 +93,7 @@ void CServerHandler::endNetwork()
}
}

CServerHandler::CServerHandler(AICombatOptions aiCombatOptions)
CServerHandler::CServerHandler()
: networkHandler(INetworkHandler::createHandler())
, lobbyClient(std::make_unique<GlobalLobbyClient>())
, gameChat(std::make_unique<GameChatHandler>())
Expand All @@ -106,7 +105,6 @@ CServerHandler::CServerHandler(AICombatOptions aiCombatOptions)
, serverMode(EServerMode::NONE)
, loadMode(ELoadMode::NONE)
, client(nullptr)
, aiCombatOptions(aiCombatOptions)
{
uuid = boost::uuids::to_string(boost::uuids::random_generator()());
}
Expand Down Expand Up @@ -614,8 +612,6 @@ void CServerHandler::startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameSta
if(CMM)
CMM->disable();

client->aiCombatOptions = aiCombatOptions;

switch(si->mode)
{
case EStartMode::NEW_GAME:
Expand Down
5 changes: 1 addition & 4 deletions client/CServerHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "../lib/network/NetworkInterface.h"
#include "../lib/StartInfo.h"
#include "../lib/gameState/GameStatistics.h"
#include "../lib/battle/AICombatOptions.h"

VCMI_LIB_NAMESPACE_BEGIN

Expand Down Expand Up @@ -108,8 +107,6 @@ class CServerHandler final : public IServerAPI, public LobbyInfo, public INetwor

std::atomic<EClientState> state;

AICombatOptions aiCombatOptions;

void threadRunNetwork();
void waitForServerShutdown();

Expand Down Expand Up @@ -144,7 +141,7 @@ class CServerHandler final : public IServerAPI, public LobbyInfo, public INetwor
std::unique_ptr<CStopWatch> th;
std::unique_ptr<CClient> client;

CServerHandler(AICombatOptions aiCombatOptions = {});
CServerHandler();
~CServerHandler();

void resetStateForLobby(EStartMode mode, ESelectionScreen screen, EServerMode serverMode, const std::vector<std::string> & playerNames);
Expand Down
4 changes: 2 additions & 2 deletions client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void CClient::installNewPlayerInterface(std::shared_ptr<CGameInterface> gameInte
logGlobal->trace("\tInitializing the interface for player %s", color.toString());
auto cb = std::make_shared<CCallback>(gs, color, this);
battleCallbacks[color] = cb;
gameInterface->initGameInterface(playerEnvironments.at(color), cb, aiCombatOptions);
gameInterface->initGameInterface(playerEnvironments.at(color), cb);

installNewBattleInterface(gameInterface, color, battlecb);
}
Expand All @@ -344,7 +344,7 @@ void CClient::installNewBattleInterface(std::shared_ptr<CBattleGameInterface> ba
logGlobal->trace("\tInitializing the battle interface for player %s", color.toString());
auto cbc = std::make_shared<CBattleCallback>(color, this);
battleCallbacks[color] = cbc;
battleInterface->initBattleInterface(playerEnvironments.at(color), cbc, aiCombatOptions);
battleInterface->initBattleInterface(playerEnvironments.at(color), cbc);
}
}

Expand Down
2 changes: 0 additions & 2 deletions client/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <vcmi/Environment.h>

#include "../lib/IGameCallback.h"
#include "battle/AICombatOptions.h"

VCMI_LIB_NAMESPACE_BEGIN

Expand Down Expand Up @@ -116,7 +115,6 @@ class CClient : public IGameCallback, public Environment
std::map<PlayerColor, std::vector<std::shared_ptr<IBattleEventsReceiver>>> additionalBattleInts;

std::unique_ptr<BattleAction> currentBattleAction;
AICombatOptions aiCombatOptions;

CClient();
~CClient();
Expand Down
14 changes: 1 addition & 13 deletions client/battle/BattleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
#include "../../lib/battle/BattleInfo.h"
#include "../../lib/CPlayerState.h"
#include "../windows/settings/SettingsMainWindow.h"
#include "battle/AICombatOptions.h"

BattleWindow::BattleWindow(BattleInterface & Owner):
owner(Owner),
Expand Down Expand Up @@ -888,18 +887,7 @@ void BattleWindow::endWithAutocombat()
[this]()
{
owner.curInt->isAutoFightEndBattle = true;

auto ai = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());

AICombatOptions aiCombatOptions = owner.curInt->aiCombatOptions;
aiCombatOptions.enableSpellsUsage = settings["battle"]["enableAutocombatSpells"].Bool();

ai->initBattleInterface(owner.curInt->env, owner.curInt->cb, aiCombatOptions);
ai->battleStart(owner.getBattleID(), owner.army1, owner.army2, int3(0,0,0), owner.attackingHeroInstance, owner.defendingHeroInstance, owner.getBattle()->battleGetMySide(), false);

owner.curInt->isAutoFightOn = true;
owner.curInt->cb->registerBattleInterface(ai);
owner.curInt->autofightingAI = ai;
owner.curInt->prepareAutoFightingAI(owner.getBattleID(), owner.army1, owner.army2, int3(0,0,0), owner.attackingHeroInstance, owner.defendingHeroInstance, owner.getBattle()->battleGetMySide());

owner.requestAutofightingAIToTakeAction();

Expand Down
6 changes: 3 additions & 3 deletions launcher/settingsView/csettingsview_moc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ void CSettingsView::loadSettings()
ui->spinBoxFramerateLimit->setDisabled(settings["video"]["vsync"].Bool());
ui->sliderReservedArea->setValue(std::round(settings["video"]["reservedWidth"].Float() * 100));

ui->comboBoxFriendlyAI->setCurrentText(QString::fromStdString(settings["server"]["friendlyAI"].String());
ui->comboBoxNeutralAI->setCurrentText(QString::fromStdString(settings["server"]["neutralAI"].String());
ui->comboBoxEnemyAI->setCurrentText(QString::fromStdString(settings["server"]["enemyAI"].String());
ui->comboBoxFriendlyAI->setCurrentText(QString::fromStdString(settings["server"]["friendlyAI"].String()));
ui->comboBoxNeutralAI->setCurrentText(QString::fromStdString(settings["server"]["neutralAI"].String()));
ui->comboBoxEnemyAI->setCurrentText(QString::fromStdString(settings["server"]["enemyAI"].String()));

ui->comboBoxEnemyPlayerAI->setCurrentText(QString::fromStdString(settings["server"]["playerAI"].String()));
ui->comboBoxAlliedPlayerAI->setCurrentText(QString::fromStdString(settings["server"]["alliedAI"].String()));
Expand Down
2 changes: 1 addition & 1 deletion lib/CGameInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void CAdventureAI::battleStart(const BattleID & battleID, const CCreatureSet * a
assert(!battleAI);
assert(cbc);
battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
battleAI->initBattleInterface(env, cbc, aiCombatOptions);
battleAI->initBattleInterface(env, cbc);
battleAI->battleStart(battleID, army1, army2, tile, hero1, hero2, side, replayAllowed);
}

Expand Down
9 changes: 4 additions & 5 deletions lib/CGameInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
#pragma once

#include "battle/AICombatOptions.h"
#include "battle/AutocombatPreferences.h"
#include "IGameEventsReceiver.h"

#include "spells/ViewSpellInt.h"
Expand Down Expand Up @@ -74,7 +74,8 @@ class DLL_LINKAGE CBattleGameInterface : public IBattleEventsReceiver
std::string dllName;

virtual ~CBattleGameInterface() {};
virtual void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AICombatOptions aiCombatOptions){};
virtual void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB){};
virtual void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AutocombatPreferences autocombatPreferences){};

//battle call-ins
virtual void activeStack(const BattleID & battleID, const CStack * stack)=0; //called when it's turn of that stack
Expand All @@ -85,10 +86,8 @@ class DLL_LINKAGE CBattleGameInterface : public IBattleEventsReceiver
class DLL_LINKAGE CGameInterface : public CBattleGameInterface, public IGameEventsReceiver
{
public:
AICombatOptions aiCombatOptions;

virtual ~CGameInterface() = default;
virtual void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB, AICombatOptions aiCombatOptions){};
virtual void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB){};
virtual void yourTurn(QueryID askID){}; //called AFTER playerStartsTurn(player)

//pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
Expand Down
Loading

0 comments on commit fb3edb6

Please sign in to comment.