diff --git a/AI/Nullkiller/Behaviors/BuyArmyBehavior.cpp b/AI/Nullkiller/Behaviors/BuyArmyBehavior.cpp new file mode 100644 index 000000000..97af8ea1d --- /dev/null +++ b/AI/Nullkiller/Behaviors/BuyArmyBehavior.cpp @@ -0,0 +1,73 @@ +/* +* Goals.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 "BuyArmyBehavior.h" +#include "../VCAI.h" +#include "../AIhelper.h" +#include "../AIUtility.h" +#include "../Goals/BuyArmy.h" +#include "../Goals/VisitTile.h" +#include "lib/mapping/CMap.h" //for victory conditions +#include "lib/CPathfinder.h" + +extern boost::thread_specific_ptr cb; +extern boost::thread_specific_ptr ai; +extern FuzzyHelper * fh; + +using namespace Goals; + +std::string BuyArmyBehavior::toString() const +{ + return "Buy army"; +} + +Goals::TGoalVec BuyArmyBehavior::getTasks() +{ + Goals::TGoalVec tasks; + + if(cb->getDate(Date::DAY) == 1) + return tasks; + + auto heroes = cb->getHeroesInfo(); + + if(heroes.size()) + { + auto mainHero = vstd::maxElementByFun(heroes, [](const CGHeroInstance * hero) -> uint64_t + { + return hero->getFightingStrength(); + }); + + for(auto town : cb->getTownsInfo()) + { + const CGHeroInstance * targetHero = *mainHero; + + /*if(town->visitingHero) + { + targetHero = town->visitingHero.get(); + + if(ai->ah->howManyReinforcementsCanGet(targetHero, town->getUpperArmy())) + { + tasks.push_back(sptr(VisitTile(town->visitablePos()).sethero(targetHero).setpriority(5))); + + continue; + } + }*/ + + auto reinforcement = ai->ah->howManyReinforcementsCanBuy(targetHero, town); + + if(reinforcement) + { + tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(5))); + } + } + } + + return tasks; +} \ No newline at end of file diff --git a/AI/Nullkiller/Behaviors/BuyArmyBehavior.h b/AI/Nullkiller/Behaviors/BuyArmyBehavior.h new file mode 100644 index 000000000..5cb52f6f4 --- /dev/null +++ b/AI/Nullkiller/Behaviors/BuyArmyBehavior.h @@ -0,0 +1,26 @@ +/* +* Goals.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/VCMI_Lib.h" +#include "Behavior.h" +#include "../AIUtility.h" + +class BuyArmyBehavior : public Behavior +{ +public: + BuyArmyBehavior() + { + } + + virtual Goals::TGoalVec getTasks() override; + virtual std::string toString() const override; +}; + diff --git a/AI/Nullkiller/CMakeLists.txt b/AI/Nullkiller/CMakeLists.txt index 790d5e535..37bd936da 100644 --- a/AI/Nullkiller/CMakeLists.txt +++ b/AI/Nullkiller/CMakeLists.txt @@ -58,6 +58,7 @@ set(VCAI_SRCS Behaviors/Behavior.cpp Behaviors/CaptureObjectsBehavior.cpp Behaviors/RecruitHeroBehavior.cpp + Behaviors/BuyArmyBehavior.cpp main.cpp VCAI.cpp ) @@ -119,6 +120,7 @@ set(VCAI_HEADERS Behaviors/Behavior.h Behaviors/CaptureObjectsBehavior.h Behaviors/RecruitHeroBehavior.h + Behaviors/BuyArmyBehavior.h VCAI.h ) diff --git a/AI/Nullkiller/Engine/Nullkiller.cpp b/AI/Nullkiller/Engine/Nullkiller.cpp index 2dcc56bb4..1d576f0bb 100644 --- a/AI/Nullkiller/Engine/Nullkiller.cpp +++ b/AI/Nullkiller/Engine/Nullkiller.cpp @@ -4,6 +4,7 @@ #include "../AIHelper.h" #include "../Behaviors/CaptureObjectsBehavior.h" #include "../Behaviors/RecruitHeroBehavior.h" +#include "../Behaviors/BuyArmyBehavior.h" #include "../Goals/Invalid.h" extern boost::thread_specific_ptr cb; @@ -74,6 +75,7 @@ void Nullkiller::makeTurn() updateAiState(); Goals::TGoalVec bestTasks = { + choseBestTask(BuyArmyBehavior()), choseBestTask(CaptureObjectsBehavior()), choseBestTask(RecruitHeroBehavior()) }; diff --git a/AI/Nullkiller/VCAI.cpp b/AI/Nullkiller/VCAI.cpp index 3d3244701..059f05a29 100644 --- a/AI/Nullkiller/VCAI.cpp +++ b/AI/Nullkiller/VCAI.cpp @@ -2177,7 +2177,7 @@ void VCAI::tryRealize(Goals::BuyArmy & g) auto res = ah->allResources(); std::vector creaturesInDwellings; - for (int i = 0; i < t->creatures.size(); i++) + for (int i = t->creatures.size() - 1; i >= 0; i--) { auto ci = infoFromDC(t->creatures[i]); @@ -2214,6 +2214,11 @@ void VCAI::tryRealize(Goals::BuyArmy & g) valueBought += ci.count * ci.cre->AIValue; } + if(t->visitingHero) + { + moveHeroToTile(t->visitablePos(), t->visitingHero.get()); + } + throw goalFulfilledException(sptr(g)); //we bought as many creatures as we wanted }