| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 | 
							- /*
 
- * StartupBehavior.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 "StartupBehavior.h"
 
- #include "../VCAI.h"
 
- #include "../AIhelper.h"
 
- #include "../AIUtility.h"
 
- #include "../Goals/RecruitHero.h"
 
- #include "../Goals/ExecuteHeroChain.h"
 
- #include "../Goals/ExchangeSwapTownHeroes.h"
 
- #include "lib/mapping/CMap.h" //for victory conditions
 
- #include "lib/mapObjects/MapObjects.h" //for victory conditions
 
- #include "lib/CPathfinder.h"
 
- #include "../Engine/Nullkiller.h"
 
- extern boost::thread_specific_ptr<CCallback> cb;
 
- extern boost::thread_specific_ptr<VCAI> ai;
 
- extern FuzzyHelper * fh;
 
- using namespace Goals;
 
- std::string StartupBehavior::toString() const
 
- {
 
- 	return "Startup";
 
- }
 
- const AIPath getShortestPath(const CGTownInstance * town, const std::vector<AIPath> & paths)
 
- {
 
- 	auto shortestPath = *vstd::minElementByFun(paths, [town](const AIPath & path) -> float
 
- 	{
 
- 		if(town->garrisonHero && path.targetHero == town->garrisonHero.get())
 
- 			return 1;
 
- 		return path.movementCost();
 
- 	});
 
- 	return shortestPath;
 
- }
 
- const CGHeroInstance * getNearestHero(const CGTownInstance * town)
 
- {
 
- 	auto paths = ai->ah->getPathsToTile(town->visitablePos());
 
- 	if(paths.empty())
 
- 		return nullptr;
 
- 	auto shortestPath = getShortestPath(town, paths);
 
- 	if(shortestPath.nodes.size() > 1 
 
- 		|| shortestPath.targetHero->visitablePos().dist2dSQ(town->visitablePos()) > 4
 
- 		|| town->garrisonHero && shortestPath.targetHero == town->garrisonHero.get())
 
- 		return nullptr;
 
- 	return shortestPath.targetHero;
 
- }
 
- bool needToRecruitHero(const CGTownInstance * startupTown)
 
- {
 
- 	if(!ai->canRecruitAnyHero(startupTown))
 
- 		return false;
 
- 	if(!startupTown->garrisonHero && !startupTown->visitingHero)
 
- 		return false;
 
- 	auto heroToCheck = startupTown->garrisonHero ? startupTown->garrisonHero.get() : startupTown->visitingHero.get();
 
- 	auto paths = cb->getPathsInfo(heroToCheck);
 
- 	for(auto obj : ai->visitableObjs)
 
- 	{
 
- 		if(obj->ID == Obj::RESOURCE && obj->subID == Res::GOLD
 
- 			|| obj->ID == Obj::TREASURE_CHEST
 
- 			|| obj->ID == Obj::CAMPFIRE
 
- 			|| obj->ID == Obj::WATER_WHEEL)
 
- 		{
 
- 			auto path = paths->getPathInfo(obj->visitablePos());
 
- 			if((path->accessible == CGPathNode::BLOCKVIS || path->accessible == CGPathNode::VISIT) 
 
- 				&& path->reachable())
 
- 			{
 
- 				return true;
 
- 			}
 
- 		}
 
- 	}
 
- 	return false;
 
- }
 
- Goals::TGoalVec StartupBehavior::getTasks()
 
- {
 
- 	Goals::TGoalVec tasks;
 
- 	auto towns = cb->getTownsInfo();
 
- 	if(!towns.size())
 
- 		return tasks;
 
- 	const CGTownInstance * startupTown = towns.front();
 
- 	bool canRecruitHero = needToRecruitHero(startupTown);
 
- 	if(towns.size() > 1)
 
- 	{
 
- 		startupTown = *vstd::maxElementByFun(towns, [](const CGTownInstance * town) -> float
 
- 		{
 
- 			auto closestHero = getNearestHero(town);
 
- 			if(!closestHero)
 
- 				return 0;
 
- 			return ai->ah->evaluateHero(closestHero);
 
- 		});
 
- 	}
 
- 	auto closestHero = getNearestHero(startupTown);
 
- 	if(closestHero)
 
- 	{
 
- 		if(!startupTown->visitingHero)
 
- 		{
 
- 			if(ai->ah->howManyReinforcementsCanGet(startupTown->getUpperArmy(), closestHero) > 200)
 
- 			{
 
- 				auto paths = ai->ah->getPathsToTile(startupTown->visitablePos());
 
- 				if(paths.size())
 
- 				{
 
- 					auto path = getShortestPath(startupTown, paths);
 
- 					tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path, startupTown).setpriority(100)));
 
- 				}
 
- 			}
 
- 		}
 
- 		else
 
- 		{
 
- 			auto visitingHero = startupTown->visitingHero.get();
 
- 			auto visitingHeroScore = ai->ah->evaluateHero(visitingHero);
 
- 				
 
- 			if(startupTown->garrisonHero)
 
- 			{
 
- 				auto garrisonHero = startupTown->garrisonHero.get();
 
- 				auto garrisonHeroScore = ai->ah->evaluateHero(garrisonHero);
 
- 				if(visitingHeroScore > garrisonHeroScore
 
- 					|| ai->ah->getHeroRole(garrisonHero) == HeroRole::SCOUT && ai->ah->getHeroRole(visitingHero) == HeroRole::MAIN)
 
- 				{
 
- 					if(canRecruitHero || ai->ah->howManyReinforcementsCanGet(visitingHero, garrisonHero) > 200)
 
- 					{
 
- 						tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero, HeroLockedReason::STARTUP).setpriority(100)));
 
- 					}
 
- 				}
 
- 				else if(ai->ah->howManyReinforcementsCanGet(garrisonHero, visitingHero) > 200)
 
- 				{
 
- 					tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, garrisonHero, HeroLockedReason::STARTUP).setpriority(100)));
 
- 				}
 
- 			}
 
- 			else if(canRecruitHero)
 
- 			{
 
- 				tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero, HeroLockedReason::STARTUP).setpriority(100)));
 
- 			}
 
- 		}
 
- 	}
 
- 	if(tasks.empty() && canRecruitHero && !startupTown->visitingHero)
 
- 	{
 
- 		tasks.push_back(Goals::sptr(Goals::RecruitHero()));
 
- 	}
 
- 	if(tasks.empty() && towns.size())
 
- 	{
 
- 		for(const CGTownInstance * town : towns)
 
- 		{
 
- 			if(town->garrisonHero && town->garrisonHero->movement && ai->nullkiller->getHeroLockedReason(town->garrisonHero) != HeroLockedReason::DEFENCE)
 
- 				tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(town, nullptr).setpriority(0.0001f)));
 
- 		}
 
- 	}
 
- 	return tasks;
 
- }
 
 
  |