2018-12-01 10:30:37 +02:00
|
|
|
/*
|
|
|
|
* GatherArmy.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 "Goals.h"
|
|
|
|
#include "../VCAI.h"
|
|
|
|
#include "../AIUtility.h"
|
|
|
|
#include "../AIhelper.h"
|
|
|
|
#include "../FuzzyHelper.h"
|
|
|
|
#include "../ResourceManager.h"
|
|
|
|
#include "../BuildingManager.h"
|
|
|
|
#include "../../../lib/mapping/CMap.h" //for victory conditions
|
|
|
|
#include "../../../lib/CPathfinder.h"
|
|
|
|
#include "../../../lib/StringConstants.h"
|
|
|
|
|
|
|
|
|
|
|
|
extern boost::thread_specific_ptr<CCallback> cb;
|
|
|
|
extern boost::thread_specific_ptr<VCAI> ai;
|
|
|
|
extern FuzzyHelper * fh;
|
|
|
|
|
|
|
|
using namespace Goals;
|
|
|
|
|
|
|
|
bool GatherArmy::operator==(const GatherArmy & other) const
|
|
|
|
{
|
|
|
|
return other.hero.h == hero.h || town == other.town;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GatherArmy::completeMessage() const
|
|
|
|
{
|
2023-03-09 15:36:46 +02:00
|
|
|
return "Hero " + hero.get()->getNameTranslated() + " gathered army of value " + std::to_string(value);
|
2018-12-01 10:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal GatherArmy::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
//TODO: find hero if none set
|
|
|
|
assert(hero.h);
|
|
|
|
|
|
|
|
return fh->chooseSolution(getAllPossibleSubgoals()); //find dwelling. use current hero to prevent him from doing nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec GatherArmy::getAllPossibleSubgoals()
|
|
|
|
{
|
|
|
|
//get all possible towns, heroes and dwellings we may use
|
|
|
|
TGoalVec ret;
|
|
|
|
|
|
|
|
if(!hero.validAndSet())
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: include evaluation of monsters gather in calculation
|
|
|
|
for(auto t : cb->getTownsInfo())
|
|
|
|
{
|
2019-01-07 23:33:31 +02:00
|
|
|
auto waysToVisit = ai->ah->howToVisitObj(hero, t);
|
|
|
|
|
|
|
|
if(waysToVisit.size())
|
2018-12-01 10:30:37 +02:00
|
|
|
{
|
|
|
|
//grab army from town
|
2019-03-13 15:21:24 +02:00
|
|
|
if(!t->visitingHero && ai->ah->howManyReinforcementsCanGet(hero.get(), t))
|
2018-12-01 10:30:37 +02:00
|
|
|
{
|
|
|
|
if(!vstd::contains(ai->townVisitsThisWeek[hero], t))
|
2019-01-07 23:33:31 +02:00
|
|
|
vstd::concatenate(ret, waysToVisit);
|
2018-12-01 10:30:37 +02:00
|
|
|
}
|
2019-01-07 23:33:31 +02:00
|
|
|
|
2018-12-01 10:30:37 +02:00
|
|
|
//buy army in town
|
|
|
|
if (!t->visitingHero || t->visitingHero == hero.get(true))
|
|
|
|
{
|
2018-12-30 16:40:05 +02:00
|
|
|
std::vector<int> values = {
|
|
|
|
value,
|
2019-03-13 15:21:24 +02:00
|
|
|
(int)ai->ah->howManyReinforcementsCanBuy(t->getUpperArmy(), t),
|
|
|
|
(int)ai->ah->howManyReinforcementsCanBuy(hero.get(), t) };
|
2018-12-30 16:40:05 +02:00
|
|
|
|
|
|
|
int val = *std::min_element(values.begin(), values.end());
|
|
|
|
|
2018-12-01 10:30:37 +02:00
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
auto goal = sptr(BuyArmy(t, val).sethero(hero));
|
2018-12-30 16:40:05 +02:00
|
|
|
|
2018-12-01 10:30:37 +02:00
|
|
|
if(!ai->ah->containsObjective(goal)) //avoid loops caused by reserving same objective twice
|
|
|
|
ret.push_back(goal);
|
|
|
|
else
|
|
|
|
logAi->debug("Can not buy army, because of ai->ah->containsObjective");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//build dwelling
|
|
|
|
//TODO: plan building over multiple turns?
|
|
|
|
//auto bid = ah->canBuildAnyStructure(t, std::vector<BuildingID>(unitsSource, unitsSource + ARRAY_COUNT(unitsSource)), 8 - cb->getDate(Date::DAY_OF_WEEK));
|
|
|
|
|
|
|
|
//Do not use below code for now, rely on generic Build. Code below needs to know a lot of town/resource context to do more good than harm
|
|
|
|
/*auto bid = ai->ah->canBuildAnyStructure(t, std::vector<BuildingID>(unitsSource, unitsSource + ARRAY_COUNT(unitsSource)), 1);
|
|
|
|
if (bid.is_initialized())
|
|
|
|
{
|
|
|
|
auto goal = sptr(BuildThis(bid.get(), t).setpriority(priority));
|
|
|
|
if (!ai->ah->containsObjective(goal)) //avoid loops caused by reserving same objective twice
|
|
|
|
ret.push_back(goal);
|
|
|
|
else
|
|
|
|
logAi->debug("Can not build a structure, because of ai->ah->containsObjective");
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto otherHeroes = cb->getHeroesInfo();
|
|
|
|
auto heroDummy = hero;
|
|
|
|
vstd::erase_if(otherHeroes, [heroDummy](const CGHeroInstance * h)
|
|
|
|
{
|
|
|
|
if(h == heroDummy.h)
|
|
|
|
return true;
|
|
|
|
else if(!ai->isAccessibleForHero(heroDummy->visitablePos(), h, true))
|
|
|
|
return true;
|
2019-03-13 15:21:24 +02:00
|
|
|
else if(!ai->ah->canGetArmy(heroDummy.h, h)) //TODO: return actual aiValue
|
2018-12-01 10:30:37 +02:00
|
|
|
return true;
|
|
|
|
else if(ai->getGoal(h)->goalType == GATHER_ARMY)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
});
|
2019-01-07 23:33:31 +02:00
|
|
|
|
2018-12-01 10:30:37 +02:00
|
|
|
for(auto h : otherHeroes)
|
|
|
|
{
|
|
|
|
// Go to the other hero if we are faster
|
2019-01-07 23:33:31 +02:00
|
|
|
if(!vstd::contains(ai->visitedHeroes[hero], h))
|
|
|
|
{
|
2019-02-11 13:46:47 +02:00
|
|
|
vstd::concatenate(ret, ai->ah->howToVisitObj(hero, h, false));
|
2019-01-07 23:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go to the other hero if we are faster
|
|
|
|
if(!vstd::contains(ai->visitedHeroes[h], hero))
|
|
|
|
{
|
2019-02-11 13:46:47 +02:00
|
|
|
vstd::concatenate(ret, ai->ah->howToVisitObj(h, hero.get(), false));
|
2019-01-07 23:33:31 +02:00
|
|
|
}
|
2018-12-01 10:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const CGObjectInstance *> objs;
|
|
|
|
for(auto obj : ai->visitableObjs)
|
|
|
|
{
|
|
|
|
if(obj->ID == Obj::CREATURE_GENERATOR1)
|
|
|
|
{
|
|
|
|
auto relationToOwner = cb->getPlayerRelations(obj->getOwner(), ai->playerID);
|
|
|
|
|
|
|
|
//Use flagged dwellings only when there are available creatures that we can afford
|
|
|
|
if(relationToOwner == PlayerRelations::SAME_PLAYER)
|
|
|
|
{
|
|
|
|
auto dwelling = dynamic_cast<const CGDwelling *>(obj);
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
ui32 val = std::min((ui32)value, (ui32)ai->ah->howManyReinforcementsCanBuy(hero.get(), dwelling));
|
2018-12-01 10:30:37 +02:00
|
|
|
|
|
|
|
if(val)
|
|
|
|
{
|
|
|
|
for(auto & creLevel : dwelling->creatures)
|
|
|
|
{
|
|
|
|
if(creLevel.first)
|
|
|
|
{
|
|
|
|
for(auto & creatureID : creLevel.second)
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
auto creature = VLC->creh->objects[creatureID];
|
2023-04-05 02:26:29 +02:00
|
|
|
if(ai->ah->freeResources().canAfford(creature->getFullRecruitCost()))
|
2018-12-01 10:30:37 +02:00
|
|
|
objs.push_back(obj); //TODO: reserve resources?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 23:33:31 +02:00
|
|
|
|
2018-12-01 10:30:37 +02:00
|
|
|
for(auto h : cb->getHeroesInfo())
|
|
|
|
{
|
|
|
|
for(auto obj : objs)
|
|
|
|
{
|
|
|
|
//find safe dwelling
|
|
|
|
if(ai->isGoodForVisit(obj, h))
|
|
|
|
{
|
|
|
|
vstd::concatenate(ret, ai->ah->howToVisitObj(h, obj));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ai->canRecruitAnyHero() && ai->ah->freeGold() > GameConstants::HERO_GOLD_COST) //this is not stupid in early phase of game
|
|
|
|
{
|
|
|
|
if(auto t = ai->findTownWithTavern())
|
|
|
|
{
|
|
|
|
for(auto h : cb->getAvailableHeroes(t)) //we assume that all towns have same set of heroes
|
|
|
|
{
|
|
|
|
if(h && h->getTotalStrength() > 500) //do not buy heroes with single creatures for GatherArmy
|
|
|
|
{
|
|
|
|
ret.push_back(sptr(RecruitHero()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret.empty())
|
|
|
|
{
|
2019-01-02 22:51:26 +02:00
|
|
|
const bool allowGatherArmy = false;
|
|
|
|
|
2018-12-01 10:30:37 +02:00
|
|
|
if(hero == ai->primaryHero())
|
2019-01-02 22:51:26 +02:00
|
|
|
ret.push_back(sptr(Explore(allowGatherArmy)));
|
2018-12-01 10:30:37 +02:00
|
|
|
else
|
|
|
|
throw cannotFulfillGoalException("No ways to gather army");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|