Переглянути джерело

Nullkiller: temporary hacks to allow army buying

Andrii Danylchenko 4 роки тому
батько
коміт
04bf6f536d

+ 73 - 0
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<CCallback> cb;
+extern boost::thread_specific_ptr<VCAI> 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;
+}

+ 26 - 0
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;
+};
+

+ 2 - 0
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
 )
 

+ 2 - 0
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<CCallback> cb;
@@ -74,6 +75,7 @@ void Nullkiller::makeTurn()
 		updateAiState();
 
 		Goals::TGoalVec bestTasks = {
+			choseBestTask(BuyArmyBehavior()),
 			choseBestTask(CaptureObjectsBehavior()),
 			choseBestTask(RecruitHeroBehavior())
 		};

+ 6 - 1
AI/Nullkiller/VCAI.cpp

@@ -2177,7 +2177,7 @@ void VCAI::tryRealize(Goals::BuyArmy & g)
 		auto res = ah->allResources();
 		std::vector<creInfo> 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
 }