2013-10-18 23:17:25 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "Goals.h"
|
|
|
|
#include "VCAI.h"
|
2013-12-19 23:21:21 +03:00
|
|
|
#include "Fuzzy.h"
|
2013-12-29 18:48:56 +03:00
|
|
|
#include "../../lib/mapping/CMap.h" //for victory conditions
|
2013-10-18 23:17:25 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern boost::thread_specific_ptr<CCallback> cb;
|
|
|
|
extern boost::thread_specific_ptr<VCAI> ai;
|
2013-12-20 12:43:12 +03:00
|
|
|
extern FuzzyHelper * fh; //TODO: this logic should be moved inside VCAI
|
2013-10-18 23:17:25 +03:00
|
|
|
|
|
|
|
using namespace vstd;
|
2013-10-19 08:52:30 +03:00
|
|
|
using namespace Goals;
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-11-23 21:16:25 +03:00
|
|
|
TSubgoal Goals::sptr(const AbstractGoal & tmp)
|
|
|
|
{
|
|
|
|
shared_ptr<AbstractGoal> ptr;
|
|
|
|
ptr.reset(tmp.clone());
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2013-11-15 20:30:16 +03:00
|
|
|
std::string Goals::AbstractGoal::name() const //TODO: virtualize
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-23 23:46:01 +03:00
|
|
|
std::string desc;
|
2013-10-18 23:17:25 +03:00
|
|
|
switch (goalType)
|
|
|
|
{
|
|
|
|
case INVALID:
|
|
|
|
return "INVALID";
|
|
|
|
case WIN:
|
|
|
|
return "WIN";
|
|
|
|
case DO_NOT_LOSE:
|
|
|
|
return "DO NOT LOOSE";
|
|
|
|
case CONQUER:
|
|
|
|
return "CONQUER";
|
|
|
|
case BUILD:
|
|
|
|
return "BUILD";
|
|
|
|
case EXPLORE:
|
2013-12-23 23:46:01 +03:00
|
|
|
desc = "EXPLORE";
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
case GATHER_ARMY:
|
2013-12-23 23:46:01 +03:00
|
|
|
desc = "GATHER ARMY";
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
case BOOST_HERO:
|
2013-12-23 23:46:01 +03:00
|
|
|
desc = "BOOST_HERO (unsupported)";
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
case RECRUIT_HERO:
|
|
|
|
return "RECRUIT HERO";
|
|
|
|
case BUILD_STRUCTURE:
|
|
|
|
return "BUILD STRUCTURE";
|
|
|
|
case COLLECT_RES:
|
2013-12-23 23:46:01 +03:00
|
|
|
desc = "COLLECT RESOURCE";
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
case GATHER_TROOPS:
|
2013-12-23 23:46:01 +03:00
|
|
|
desc = "GATHER TROOPS";
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
case GET_OBJ:
|
2013-12-26 12:53:37 +03:00
|
|
|
{
|
|
|
|
auto obj = cb->getObjInstance(ObjectInstanceID(objid));
|
|
|
|
if (obj)
|
|
|
|
desc = "GET OBJ " + obj->getHoverText();
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
case FIND_OBJ:
|
2013-12-23 23:46:01 +03:00
|
|
|
desc = "FIND OBJ " + boost::lexical_cast<std::string>(objid);
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
case VISIT_HERO:
|
2013-12-26 12:53:37 +03:00
|
|
|
{
|
|
|
|
auto obj = cb->getObjInstance(ObjectInstanceID(objid));
|
|
|
|
if (obj)
|
|
|
|
desc = "VISIT HERO " + obj->getHoverText();
|
|
|
|
}
|
2013-12-23 23:46:01 +03:00
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
case GET_ART_TYPE:
|
2013-12-23 23:46:01 +03:00
|
|
|
desc = "GET ARTIFACT OF TYPE " + VLC->arth->artifacts[aid]->Name();
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
case ISSUE_COMMAND:
|
|
|
|
return "ISSUE COMMAND (unsupported)";
|
|
|
|
case VISIT_TILE:
|
2013-12-23 23:46:01 +03:00
|
|
|
desc = "VISIT TILE " + tile();
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
case CLEAR_WAY_TO:
|
2013-12-23 23:46:01 +03:00
|
|
|
desc = "CLEAR WAY TO " + tile();
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
case DIG_AT_TILE:
|
2013-12-23 23:46:01 +03:00
|
|
|
desc = "DIG AT TILE " + tile();
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
default:
|
|
|
|
return boost::lexical_cast<std::string>(goalType);
|
|
|
|
}
|
2013-12-24 00:22:42 +03:00
|
|
|
if (hero.get(true)) //FIXME: used to crash when we lost hero and failed goal
|
2013-12-23 23:46:01 +03:00
|
|
|
desc += " (" + hero->name + ")";
|
|
|
|
return desc;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-11-30 10:52:08 +03:00
|
|
|
//TODO: find out why the following are not generated automatically on MVS?
|
|
|
|
|
|
|
|
namespace Goals
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
void CGoal<Win>::accept (VCAI * ai)
|
|
|
|
{
|
|
|
|
ai->tryRealize(static_cast<Win&>(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void CGoal<Build>::accept (VCAI * ai)
|
|
|
|
{
|
|
|
|
ai->tryRealize(static_cast<Build&>(*this));
|
|
|
|
}
|
2013-12-19 23:21:21 +03:00
|
|
|
template <>
|
|
|
|
float CGoal<Win>::accept (FuzzyHelper * f)
|
|
|
|
{
|
|
|
|
return f->evaluate(static_cast<Win&>(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
float CGoal<Build>::accept (FuzzyHelper * f)
|
|
|
|
{
|
|
|
|
return f->evaluate(static_cast<Build&>(*this));
|
|
|
|
}
|
2013-11-30 10:52:08 +03:00
|
|
|
}
|
|
|
|
|
2013-11-23 21:16:25 +03:00
|
|
|
//TSubgoal AbstractGoal::whatToDoToAchieve()
|
|
|
|
//{
|
|
|
|
// logAi->debugStream() << boost::format("Decomposing goal of type %s") % name();
|
|
|
|
// return sptr (Goals::Explore());
|
|
|
|
//}
|
2013-10-18 23:17:25 +03:00
|
|
|
|
|
|
|
TSubgoal Win::whatToDoToAchieve()
|
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
auto toBool = [=](const EventCondition &)
|
|
|
|
{
|
|
|
|
// TODO: proper implementation
|
|
|
|
// Right now even already fulfilled goals will be included into generated list
|
|
|
|
// Proper check should test if event condition is already fulfilled
|
|
|
|
// Easiest way to do this is to call CGameState::checkForVictory but this function should not be
|
|
|
|
// used on client side or in AI code
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<EventCondition> goals;
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-29 14:27:38 +03:00
|
|
|
for (const TriggeredEvent & event : cb->getMapHeader()->triggeredEvents)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
//TODO: try to eliminate human player(s) using loss conditions that have isHuman element
|
|
|
|
|
|
|
|
if (event.effect.type == EventEffect::VICTORY)
|
|
|
|
{
|
|
|
|
boost::range::copy(event.trigger.getFulfillmentCandidates(toBool), std::back_inserter(goals));
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-12-29 14:27:38 +03:00
|
|
|
//TODO: instead of returning first encountered goal AI should generate list of possible subgoals
|
|
|
|
for (const EventCondition & goal : goals)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
switch(goal.condition)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
case EventCondition::HAVE_ARTIFACT:
|
|
|
|
return sptr (Goals::GetArtOfType(goal.objectType));
|
|
|
|
case EventCondition::DESTROY:
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
if (goal.object)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
return sptr (Goals::GetObj(goal.object->id.getNum()));
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
// TODO: destroy all objects of type goal.objectType
|
|
|
|
// This situation represents "kill all creatures" condition from H3
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case EventCondition::HAVE_BUILDING:
|
|
|
|
{
|
|
|
|
// TODO build other buildings apart from Grail
|
|
|
|
// goal.objectType = buidingID to build
|
|
|
|
// goal.object = optional, town in which building should be built
|
|
|
|
// Represents "Improve town" condition from H3 (but unlike H3 it consists from 2 separate conditions)
|
|
|
|
|
|
|
|
if (goal.objectType == BuildingID::GRAIL)
|
|
|
|
{
|
|
|
|
if(auto h = ai->getHeroWithGrail())
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
//hero is in a town that can host Grail
|
|
|
|
if(h->visitedTown && !vstd::contains(h->visitedTown->forbiddenBuildings, BuildingID::GRAIL))
|
|
|
|
{
|
|
|
|
const CGTownInstance *t = h->visitedTown;
|
|
|
|
return sptr (Goals::BuildThis(BuildingID::GRAIL, t));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto towns = cb->getTownsInfo();
|
|
|
|
towns.erase(boost::remove_if(towns,
|
|
|
|
[](const CGTownInstance *t) -> bool
|
|
|
|
{
|
|
|
|
return vstd::contains(t->forbiddenBuildings, BuildingID::GRAIL);
|
|
|
|
}),
|
|
|
|
towns.end());
|
|
|
|
boost::sort(towns, isCloser);
|
|
|
|
if(towns.size())
|
|
|
|
{
|
|
|
|
return sptr (Goals::VisitTile(towns.front()->visitablePos()).sethero(h));
|
|
|
|
}
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
2013-12-29 14:27:38 +03:00
|
|
|
double ratio = 0;
|
|
|
|
// maybe make this check a bit more complex? For example:
|
|
|
|
// 0.75 -> dig randomly withing 3 tiles radius
|
|
|
|
// 0.85 -> radius now 2 tiles
|
|
|
|
// 0.95 -> 1 tile radius, position is fully known
|
|
|
|
// AFAIK H3 AI does something like this
|
|
|
|
int3 grailPos = cb->getGrailPos(ratio);
|
|
|
|
if(ratio > 0.99)
|
|
|
|
{
|
|
|
|
return sptr (Goals::DigAtTile(grailPos));
|
|
|
|
} //TODO: use FIND_OBJ
|
|
|
|
else if(const CGObjectInstance * obj = ai->getUnvisitedObj(objWithID<Obj::OBELISK>)) //there are unvisited Obelisks
|
|
|
|
return sptr (Goals::GetObj(obj->id.getNum()));
|
|
|
|
else
|
|
|
|
return sptr (Goals::Explore());
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
2013-12-29 14:27:38 +03:00
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
2013-12-29 14:27:38 +03:00
|
|
|
case EventCondition::CONTROL:
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
if (goal.object)
|
|
|
|
{
|
|
|
|
return sptr (Goals::GetObj(goal.object->id.getNum()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//TODO: control all objects of type "goal.objectType"
|
|
|
|
// Represents H3 condition "Flag all mines"
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case EventCondition::HAVE_RESOURCES:
|
|
|
|
//TODO mines? piles? marketplace?
|
|
|
|
//save?
|
|
|
|
return sptr (Goals::CollectRes(static_cast<Res::ERes>(goal.objectType), goal.value));
|
|
|
|
case EventCondition::HAVE_CREATURES:
|
|
|
|
return sptr (Goals::GatherTroops(goal.objectType, goal.value));
|
|
|
|
case EventCondition::TRANSPORT:
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
//TODO. merge with bring Grail to town? So AI will first dig grail, then transport it using this goal and builds it
|
|
|
|
// Represents "transport artifact" condition:
|
|
|
|
// goal.objectType = type of artifact
|
|
|
|
// goal.object = destination-town where artifact should be transported
|
|
|
|
break;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
2013-12-29 14:27:38 +03:00
|
|
|
case EventCondition::STANDARD_WIN:
|
|
|
|
return sptr (Goals::Conquer());
|
|
|
|
|
|
|
|
// Conditions that likely don't need any implementation
|
|
|
|
case EventCondition::DAYS_PASSED:
|
|
|
|
break; // goal.value = number of days for condition to trigger
|
|
|
|
case EventCondition::DAYS_WITHOUT_TOWN:
|
|
|
|
break; // goal.value = number of days to trigger this
|
|
|
|
case EventCondition::IS_HUMAN:
|
|
|
|
break; // Should be only used in calculation of candidates (see toBool lambda)
|
2014-02-09 15:10:02 +03:00
|
|
|
case EventCondition::CONST_VALUE:
|
|
|
|
break;
|
2013-12-29 14:27:38 +03:00
|
|
|
default:
|
|
|
|
assert(0);
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
}
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::Invalid());
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal FindObj::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
const CGObjectInstance * o = nullptr;
|
|
|
|
if (resID > -1) //specified
|
|
|
|
{
|
|
|
|
for(const CGObjectInstance *obj : ai->visitableObjs)
|
|
|
|
{
|
|
|
|
if(obj->ID == objid && obj->subID == resID)
|
|
|
|
{
|
|
|
|
o = obj;
|
|
|
|
break; //TODO: consider multiple objects and choose best
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(const CGObjectInstance *obj : ai->visitableObjs)
|
|
|
|
{
|
|
|
|
if(obj->ID == objid)
|
|
|
|
{
|
|
|
|
o = obj;
|
|
|
|
break; //TODO: consider multiple objects and choose best
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-24 22:15:08 +03:00
|
|
|
if (o && isReachable(o)) //we don't use isAccessibleForHero as we don't know which hero it is
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::GetObj(o->id.getNum()));
|
2013-10-18 23:17:25 +03:00
|
|
|
else
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::Explore());
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
2013-12-20 12:43:12 +03:00
|
|
|
|
2013-11-24 11:21:51 +03:00
|
|
|
std::string GetObj::completeMessage() const
|
|
|
|
{
|
|
|
|
return "hero " + hero.get()->name + " captured Object ID = " + boost::lexical_cast<std::string>(objid);
|
|
|
|
}
|
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
TSubgoal GetObj::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(objid));
|
|
|
|
if(!obj)
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::Explore());
|
2013-10-18 23:17:25 +03:00
|
|
|
int3 pos = obj->visitablePos();
|
2013-12-25 01:01:16 +03:00
|
|
|
if (hero)
|
|
|
|
{
|
|
|
|
if (ai->isAccessibleForHero(pos, hero))
|
|
|
|
return sptr (Goals::VisitTile(pos).sethero(hero));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (isReachable(obj))
|
|
|
|
return sptr (Goals::VisitTile(pos).sethero(hero)); //we must visit object with same hero, if any
|
|
|
|
}
|
|
|
|
return sptr (Goals::ClearWayTo(pos).sethero(hero));
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-12-25 01:01:16 +03:00
|
|
|
bool GetObj::fulfillsMe (TSubgoal goal)
|
2013-11-24 22:15:08 +03:00
|
|
|
{
|
2013-12-25 01:12:12 +03:00
|
|
|
if (goal->goalType == Goals::VISIT_TILE)
|
|
|
|
{
|
|
|
|
auto obj = cb->getObj(ObjectInstanceID(objid));
|
|
|
|
if (obj && obj->visitablePos() == goal->tile) //object could be removed
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-25 16:38:20 +03:00
|
|
|
|
|
|
|
return false;
|
2013-11-24 22:15:08 +03:00
|
|
|
}
|
|
|
|
|
2013-11-24 11:21:51 +03:00
|
|
|
std::string VisitHero::completeMessage() const
|
|
|
|
{
|
|
|
|
return "hero " + hero.get()->name + " visited hero " + boost::lexical_cast<std::string>(objid);
|
|
|
|
}
|
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
TSubgoal VisitHero::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(objid));
|
|
|
|
if(!obj)
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::Explore());
|
2013-10-18 23:17:25 +03:00
|
|
|
int3 pos = obj->visitablePos();
|
|
|
|
|
|
|
|
if (hero && ai->isAccessibleForHero(pos, hero, true) && isSafeToVisit(hero, pos)) //enemy heroes can get reinforcements
|
2013-11-15 20:30:16 +03:00
|
|
|
{
|
2013-12-21 20:34:59 +03:00
|
|
|
if (hero->pos == pos)
|
|
|
|
logAi->errorStream() << "Hero " << hero.name << " tries to visit himself.";
|
|
|
|
else
|
|
|
|
{
|
2013-12-27 16:20:40 +03:00
|
|
|
//can't use VISIT_TILE here as tile appears blocked by target hero
|
|
|
|
//FIXME: elementar goal should not be abstract
|
|
|
|
return sptr (Goals::VisitHero(objid).sethero(hero).settile(pos).setisElementar(true));
|
2013-12-21 20:34:59 +03:00
|
|
|
}
|
2013-11-15 20:30:16 +03:00
|
|
|
}
|
|
|
|
return sptr (Goals::Invalid());
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-12-25 01:01:16 +03:00
|
|
|
bool VisitHero::fulfillsMe (TSubgoal goal)
|
2013-11-24 22:15:08 +03:00
|
|
|
{
|
2013-12-25 01:01:16 +03:00
|
|
|
if (goal->goalType == Goals::VISIT_TILE && cb->getObj(ObjectInstanceID(objid))->visitablePos() == goal->tile)
|
2013-11-24 22:15:08 +03:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
TSubgoal GetArtOfType::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
TSubgoal alternativeWay = CGoal::lookForArtSmart(aid); //TODO: use
|
2013-11-10 00:29:46 +03:00
|
|
|
if(alternativeWay->invalid())
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::FindObj(Obj::ARTIFACT, aid));
|
|
|
|
return sptr (Goals::Invalid());
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal ClearWayTo::whatToDoToAchieve()
|
|
|
|
{
|
2013-12-23 23:46:01 +03:00
|
|
|
assert(cb->isInTheMap(tile)); //set tile
|
2013-10-18 23:17:25 +03:00
|
|
|
if(!cb->isVisible(tile))
|
|
|
|
{
|
|
|
|
logAi->errorStream() << "Clear way should be used with visible tiles!";
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::Explore());
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
return (fh->chooseSolution(getAllPossibleSubgoals()));
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
TGoalVec ClearWayTo::getAllPossibleSubgoals()
|
|
|
|
{
|
|
|
|
TGoalVec ret;
|
|
|
|
for (auto h : cb->getHeroesInfo())
|
|
|
|
{
|
2013-12-26 12:53:37 +03:00
|
|
|
if ((hero && hero->visitablePos() == tile && hero == *h) || //we can't free the way ourselves
|
|
|
|
h->visitablePos() == tile) //we are already on that tile! what does it mean?
|
|
|
|
continue;
|
|
|
|
|
2014-02-15 22:39:03 +03:00
|
|
|
SectorMap sm(h);
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
int3 tileToHit = sm.firstTileToGet(hero ? hero : h, tile);
|
|
|
|
//if our hero is trapped, make sure we request clearing the way from OUR perspective
|
2014-02-15 19:38:51 +03:00
|
|
|
if (!tileToHit.valid())
|
|
|
|
continue;
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
if (isBlockedBorderGate(tileToHit))
|
|
|
|
{ //FIXME: this way we'll not visit gate and activate quest :?
|
|
|
|
ret.push_back (sptr (Goals::FindObj (Obj::KEYMASTER, cb->getTile(tileToHit)->visitableObjects.back()->subID)));
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2014-02-17 20:28:39 +03:00
|
|
|
auto topObj = cb->getTopObj(tileToHit);
|
2013-12-25 01:01:16 +03:00
|
|
|
if(topObj)
|
2013-12-23 23:46:01 +03:00
|
|
|
{
|
2013-12-25 01:01:16 +03:00
|
|
|
if (topObj->ID == Obj::HERO && cb->getPlayerRelations(h->tempOwner, topObj->tempOwner) != PlayerRelations::ENEMIES)
|
2013-12-26 12:53:37 +03:00
|
|
|
if (topObj != hero.get(true)) //the hero we wnat to free
|
|
|
|
logAi->errorStream() << boost::format("%s stands in the way of %s") % topObj->getHoverText() % h->getHoverText();
|
2013-12-25 01:01:16 +03:00
|
|
|
if (topObj->ID == Obj::QUEST_GUARD || topObj->ID == Obj::BORDERGUARD)
|
|
|
|
{
|
|
|
|
if (shouldVisit(h, topObj))
|
|
|
|
{
|
|
|
|
//do NOT use VISIT_TILE, as tile with quets guard can't be visited
|
|
|
|
ret.push_back (sptr (Goals::GetObj(topObj->id.getNum()).sethero(h)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//TODO: we should be able to return apriopriate quest here (VCAI::striveToQuest)
|
|
|
|
logAi->debugStream() << "Quest guard blocks the way to " + tile();
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 23:46:01 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret.push_back (sptr (Goals::VisitTile(tileToHit).sethero(h)));
|
|
|
|
}
|
|
|
|
if (ai->canRecruitAnyHero())
|
|
|
|
ret.push_back (sptr (Goals::RecruitHero()));
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
if (ret.empty())
|
2013-12-25 01:01:16 +03:00
|
|
|
{
|
|
|
|
logAi->warnStream() << "There is no known way to clear the way to tile " + tile();
|
|
|
|
throw goalFulfilledException (sptr(*this)); //make sure asigned hero gets unlocked
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
return ret;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-11-24 11:21:51 +03:00
|
|
|
std::string Explore::completeMessage() const
|
|
|
|
{
|
|
|
|
return "Hero " + hero.get()->name + " completed exploration";
|
2014-01-16 23:24:06 +03:00
|
|
|
}
|
2013-11-24 11:21:51 +03:00
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
TSubgoal Explore::whatToDoToAchieve()
|
|
|
|
{
|
2013-12-20 19:49:51 +03:00
|
|
|
auto ret = fh->chooseSolution(getAllPossibleSubgoals());
|
|
|
|
if (hero) //use best step for this hero
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ret->hero.get(true))
|
|
|
|
return sptr (sethero(ret->hero.h).setisAbstract(true)); //choose this hero and then continue with him
|
|
|
|
else
|
|
|
|
return ret; //other solutions, like buying hero from tavern
|
|
|
|
}
|
2014-01-16 23:24:06 +03:00
|
|
|
}
|
2013-12-20 19:49:51 +03:00
|
|
|
|
|
|
|
TGoalVec Explore::getAllPossibleSubgoals()
|
|
|
|
{
|
|
|
|
TGoalVec ret;
|
2013-12-23 23:46:01 +03:00
|
|
|
std::vector<const CGHeroInstance *> heroes;
|
|
|
|
//std::vector<HeroPtr> heroes;
|
2013-12-20 19:49:51 +03:00
|
|
|
if (hero)
|
2013-12-23 23:46:01 +03:00
|
|
|
//heroes.push_back(hero);
|
|
|
|
heroes.push_back(hero.h);
|
2013-12-20 19:49:51 +03:00
|
|
|
else
|
|
|
|
{
|
2013-12-23 23:46:01 +03:00
|
|
|
//heroes = ai->getUnblockedHeroes();
|
|
|
|
heroes = cb->getHeroesInfo();
|
2013-12-21 00:10:43 +03:00
|
|
|
erase_if (heroes, [](const HeroPtr h)
|
2013-12-20 19:49:51 +03:00
|
|
|
{
|
2014-02-17 10:36:03 +03:00
|
|
|
if (vstd::contains(ai->lockedHeroes, h))
|
|
|
|
if (ai->lockedHeroes[h]->goalType == Goals::EXPLORE) //do not reassign hero who is already explorer
|
|
|
|
return true;
|
|
|
|
|
2013-12-21 00:10:43 +03:00
|
|
|
return !h->movement; //saves time, immobile heroes are useless anyway
|
2013-12-20 19:49:51 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-21 20:34:59 +03:00
|
|
|
//try to use buildings that uncover map
|
|
|
|
std::vector<const CGObjectInstance *> objs;
|
|
|
|
for (auto obj : ai->visitableObjs)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-21 20:34:59 +03:00
|
|
|
if (!vstd::contains(ai->alreadyVisited, obj))
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-21 20:34:59 +03:00
|
|
|
switch (obj->ID.num)
|
|
|
|
{
|
|
|
|
case Obj::REDWOOD_OBSERVATORY:
|
|
|
|
case Obj::PILLAR_OF_FIRE:
|
|
|
|
case Obj::CARTOGRAPHER:
|
|
|
|
case Obj::SUBTERRANEAN_GATE: //TODO: check ai->knownSubterraneanGates
|
|
|
|
objs.push_back (obj);
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
2013-12-21 20:34:59 +03:00
|
|
|
}
|
2013-12-20 19:49:51 +03:00
|
|
|
for (auto h : heroes)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2014-02-15 22:39:03 +03:00
|
|
|
SectorMap sm(h);
|
2013-12-20 19:49:51 +03:00
|
|
|
for (auto obj : objs) //double loop, performance risk?
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2014-02-15 19:38:51 +03:00
|
|
|
auto t = sm.firstTileToGet(h, obj->visitablePos()); //we assume that no more than one tile on the way is guarded
|
2014-02-17 20:28:39 +03:00
|
|
|
ai->whatToDoToReachTile(h, t, ret);
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-12-20 19:49:51 +03:00
|
|
|
int3 t = whereToExplore(h);
|
2014-02-15 19:38:51 +03:00
|
|
|
if (t.valid())
|
2014-02-16 00:32:49 +03:00
|
|
|
{
|
2013-12-21 20:34:59 +03:00
|
|
|
ret.push_back (sptr (Goals::VisitTile(t).sethero(h)));
|
2014-02-16 00:32:49 +03:00
|
|
|
}
|
2014-02-17 10:36:03 +03:00
|
|
|
else if (hero.h == h || (!hero && h == ai->primaryHero().h)) //check this only ONCE, high cost
|
|
|
|
{
|
2014-02-17 20:28:39 +03:00
|
|
|
t = ai->explorationDesperate(h);
|
|
|
|
ai->whatToDoToReachTile(h, t, ret);
|
2014-02-17 10:36:03 +03:00
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
2013-12-23 23:46:01 +03:00
|
|
|
//we either don't have hero yet or none of heroes can explore
|
|
|
|
if ((!hero || ret.empty()) && ai->canRecruitAnyHero())
|
2013-12-20 19:49:51 +03:00
|
|
|
ret.push_back (sptr(Goals::RecruitHero()));
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2014-02-17 10:36:03 +03:00
|
|
|
//if (ret.empty())
|
|
|
|
//{
|
|
|
|
// for (auto h : heroes) //this is costly function, use only when there is no other way
|
|
|
|
// {
|
2014-02-17 20:28:39 +03:00
|
|
|
// auto t = ai->explorationDesperate (h); //we assume that no more than one tile on the way is guarded
|
2014-02-17 10:36:03 +03:00
|
|
|
// if (t.valid())
|
|
|
|
// {
|
|
|
|
// if (isSafeToVisit(h, t))
|
|
|
|
// {
|
|
|
|
// ret.push_back (sptr (Goals::VisitTile(t).sethero(h)));
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// ret.push_back (sptr (Goals::GatherArmy(evaluateDanger(t, h)*SAFE_ATTACK_CONSTANT).
|
|
|
|
// sethero(h).setisAbstract(true)));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2013-12-20 19:49:51 +03:00
|
|
|
if (ret.empty())
|
2013-12-26 12:53:37 +03:00
|
|
|
{
|
2014-02-07 11:23:31 +03:00
|
|
|
throw goalFulfilledException (sptr(Goals::Explore().sethero(hero)));
|
2013-12-26 12:53:37 +03:00
|
|
|
}
|
|
|
|
//throw cannotFulfillGoalException("Cannot explore - no possible ways found!");
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-20 19:49:51 +03:00
|
|
|
return ret;
|
2014-01-16 23:24:06 +03:00
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-26 12:53:37 +03:00
|
|
|
bool Explore::fulfillsMe (TSubgoal goal)
|
|
|
|
{
|
|
|
|
if (goal->goalType == Goals::EXPLORE)
|
|
|
|
{
|
|
|
|
if (goal->hero)
|
|
|
|
return hero == goal->hero;
|
|
|
|
else
|
|
|
|
return true; //cancel ALL exploration
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
TSubgoal RecruitHero::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
const CGTownInstance *t = ai->findTownWithTavern();
|
|
|
|
if(!t)
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::BuildThis(BuildingID::TAVERN));
|
2013-10-18 23:17:25 +03:00
|
|
|
|
|
|
|
if(cb->getResourceAmount(Res::GOLD) < HERO_GOLD_COST)
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::CollectRes(Res::GOLD, HERO_GOLD_COST));
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-11-15 20:30:16 +03:00
|
|
|
return iAmElementar();
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-11-24 11:21:51 +03:00
|
|
|
std::string VisitTile::completeMessage() const
|
|
|
|
{
|
|
|
|
return "Hero " + hero.get()->name + " visited tile " + tile();
|
|
|
|
}
|
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
TSubgoal VisitTile::whatToDoToAchieve()
|
|
|
|
{
|
2013-12-20 16:07:58 +03:00
|
|
|
auto ret = fh->chooseSolution(getAllPossibleSubgoals());
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-20 12:43:12 +03:00
|
|
|
if (ret->hero)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-26 12:53:37 +03:00
|
|
|
if (isSafeToVisit(ret->hero, tile) && ai->isAccessibleForHero(tile, ret->hero))
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-20 12:43:12 +03:00
|
|
|
ret->setisElementar(true);
|
|
|
|
return ret;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 23:46:01 +03:00
|
|
|
return sptr (Goals::GatherArmy(evaluateDanger(tile, *ret->hero) * SAFE_ATTACK_CONSTANT)
|
|
|
|
.sethero(ret->hero).setisAbstract(true));
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
}
|
2013-12-20 12:43:12 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-19 23:21:21 +03:00
|
|
|
TGoalVec VisitTile::getAllPossibleSubgoals()
|
|
|
|
{
|
2014-02-16 00:32:49 +03:00
|
|
|
assert(cb->isInTheMap(tile));
|
|
|
|
|
2013-12-19 23:21:21 +03:00
|
|
|
TGoalVec ret;
|
|
|
|
if (!cb->isVisible(tile))
|
|
|
|
ret.push_back (sptr(Goals::Explore())); //what sense does it make?
|
|
|
|
else
|
|
|
|
{
|
2013-12-21 10:37:23 +03:00
|
|
|
std::vector<const CGHeroInstance *> heroes;
|
|
|
|
if (hero)
|
|
|
|
heroes.push_back(hero.h); //use assigned hero if any
|
|
|
|
else
|
|
|
|
heroes = cb->getHeroesInfo(); //use most convenient hero
|
|
|
|
|
|
|
|
for (auto h : heroes)
|
2013-12-19 23:21:21 +03:00
|
|
|
{
|
2013-12-20 12:43:12 +03:00
|
|
|
if (ai->isAccessibleForHero(tile, h))
|
|
|
|
ret.push_back (sptr(Goals::VisitTile(tile).sethero(h)));
|
2013-12-19 23:21:21 +03:00
|
|
|
}
|
|
|
|
if (ai->canRecruitAnyHero())
|
|
|
|
ret.push_back (sptr(Goals::RecruitHero()));
|
|
|
|
}
|
2013-12-20 12:43:12 +03:00
|
|
|
if (ret.empty())
|
2013-12-26 12:53:37 +03:00
|
|
|
{
|
|
|
|
auto obj = frontOrNull(cb->getVisitableObjs(tile));
|
|
|
|
if (obj && obj->ID == Obj::HERO && obj->tempOwner == ai->playerID) //our own hero stands on that tile
|
|
|
|
ret.push_back (sptr(Goals::VisitTile(tile).sethero(dynamic_cast<const CGHeroInstance *>(obj)).setisElementar(true)));
|
|
|
|
else
|
|
|
|
ret.push_back (sptr(Goals::ClearWayTo(tile)));
|
|
|
|
}
|
2013-12-23 23:46:01 +03:00
|
|
|
|
2013-12-20 12:43:12 +03:00
|
|
|
//important - at least one sub-goal must handle case which is impossible to fulfill (unreachable tile)
|
2013-12-19 23:21:21 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
TSubgoal DigAtTile::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
const CGObjectInstance *firstObj = frontOrNull(cb->getVisitableObjs(tile));
|
|
|
|
if(firstObj && firstObj->ID == Obj::HERO && firstObj->tempOwner == ai->playerID) //we have hero at dest
|
|
|
|
{
|
|
|
|
const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(firstObj);
|
2013-11-15 20:30:16 +03:00
|
|
|
sethero(h).setisElementar(true);
|
|
|
|
return sptr (*this);
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::VisitTile(tile));
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal BuildThis::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
//TODO check res
|
|
|
|
//look for town
|
|
|
|
//prerequisites?
|
2013-11-15 20:30:16 +03:00
|
|
|
return iAmElementar();
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal CollectRes::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
std::vector<const IMarket*> markets;
|
|
|
|
|
|
|
|
std::vector<const CGObjectInstance*> visObjs;
|
|
|
|
ai->retreiveVisitableObjs(visObjs, true);
|
|
|
|
for(const CGObjectInstance *obj : visObjs)
|
|
|
|
{
|
|
|
|
if(const IMarket *m = IMarket::castFrom(obj, false))
|
|
|
|
{
|
|
|
|
if(obj->ID == Obj::TOWN && obj->tempOwner == ai->playerID && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE))
|
|
|
|
markets.push_back(m);
|
|
|
|
else if(obj->ID == Obj::TRADING_POST) //TODO a moze po prostu test na pozwalanie handlu?
|
|
|
|
markets.push_back(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::sort(markets, [](const IMarket *m1, const IMarket *m2) -> bool
|
|
|
|
{
|
|
|
|
return m1->getMarketEfficiency() < m2->getMarketEfficiency();
|
|
|
|
});
|
|
|
|
|
|
|
|
markets.erase(boost::remove_if(markets, [](const IMarket *market) -> bool
|
|
|
|
{
|
|
|
|
return !(market->o->ID == Obj::TOWN && market->o->tempOwner == ai->playerID)
|
|
|
|
&& !ai->isAccessible(market->o->visitablePos());
|
|
|
|
}),markets.end());
|
|
|
|
|
|
|
|
if(!markets.size())
|
|
|
|
{
|
|
|
|
for(const CGTownInstance *t : cb->getTownsInfo())
|
|
|
|
{
|
|
|
|
if(cb->canBuildStructure(t, BuildingID::MARKETPLACE) == EBuildingState::ALLOWED)
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::BuildThis(BuildingID::MARKETPLACE, t));
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const IMarket *m = markets.back();
|
|
|
|
//attempt trade at back (best prices)
|
|
|
|
int howManyCanWeBuy = 0;
|
|
|
|
for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, 1))
|
|
|
|
{
|
|
|
|
if(i == resID) continue;
|
|
|
|
int toGive = -1, toReceive = -1;
|
|
|
|
m->getOffer(i, resID, toGive, toReceive, EMarketMode::RESOURCE_RESOURCE);
|
|
|
|
assert(toGive > 0 && toReceive > 0);
|
|
|
|
howManyCanWeBuy += toReceive * (cb->getResourceAmount(i) / toGive);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(howManyCanWeBuy + cb->getResourceAmount(static_cast<Res::ERes>(resID)) >= value)
|
|
|
|
{
|
2014-02-17 20:28:39 +03:00
|
|
|
auto backObj = cb->getTopObj(m->o->visitablePos()); //it'll be a hero if we have one there; otherwise marketplace
|
2013-10-18 23:17:25 +03:00
|
|
|
assert(backObj);
|
2013-11-15 20:30:16 +03:00
|
|
|
if (backObj->tempOwner != ai->playerID)
|
|
|
|
{
|
|
|
|
return sptr (Goals::GetObj(m->o->id.getNum()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return sptr (Goals::GetObj(m->o->id.getNum()).setisElementar(true));
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
}
|
2013-12-25 01:01:16 +03:00
|
|
|
return sptr (setisElementar(true)); //all the conditions for trade are met
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal GatherTroops::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
std::vector<const CGDwelling *> dwellings;
|
|
|
|
for(const CGTownInstance *t : cb->getTownsInfo())
|
|
|
|
{
|
|
|
|
auto creature = VLC->creh->creatures[objid];
|
|
|
|
if (t->subID == creature->faction) //TODO: how to force AI to build unupgraded creatures? :O
|
|
|
|
{
|
|
|
|
auto creatures = vstd::tryAt(t->town->creatures, creature->level - 1);
|
|
|
|
if(!creatures)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int upgradeNumber = vstd::find_pos(*creatures, creature->idNumber);
|
|
|
|
if(upgradeNumber < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BuildingID bid(BuildingID::DWELL_FIRST + creature->level - 1 + upgradeNumber * GameConstants::CREATURES_PER_TOWN);
|
|
|
|
if (t->hasBuilt(bid)) //this assumes only creatures with dwellings are assigned to faction
|
|
|
|
{
|
|
|
|
dwellings.push_back(t);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::BuildThis(bid, t));
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto obj : ai->visitableObjs)
|
|
|
|
{
|
|
|
|
if (obj->ID != Obj::CREATURE_GENERATOR1) //TODO: what with other creature generators?
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto d = dynamic_cast<const CGDwelling *>(obj);
|
|
|
|
for (auto creature : d->creatures)
|
|
|
|
{
|
|
|
|
if (creature.first) //there are more than 0 creatures avaliabe
|
|
|
|
{
|
|
|
|
for (auto type : creature.second)
|
|
|
|
{
|
|
|
|
if (type == objid && ai->freeResources().canAfford(VLC->creh->creatures[type]->cost))
|
|
|
|
dwellings.push_back(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dwellings.size())
|
|
|
|
{
|
|
|
|
boost::sort(dwellings, isCloser);
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::GetObj(dwellings.front()->id.getNum()));
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
else
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::Explore());
|
2013-10-18 23:17:25 +03:00
|
|
|
//TODO: exchange troops between heroes
|
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal Conquer::whatToDoToAchieve()
|
|
|
|
{
|
2013-12-20 16:01:44 +03:00
|
|
|
return fh->chooseSolution (getAllPossibleSubgoals());
|
|
|
|
}
|
|
|
|
TGoalVec Conquer::getAllPossibleSubgoals()
|
|
|
|
{
|
|
|
|
TGoalVec ret;
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2014-02-17 10:36:03 +03:00
|
|
|
auto conquerable = [](const CGObjectInstance * obj) -> bool
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2014-02-17 10:36:03 +03:00
|
|
|
if (cb->getPlayerRelations(ai->playerID, obj->tempOwner) == PlayerRelations::ENEMIES)
|
2014-02-15 19:38:51 +03:00
|
|
|
{
|
|
|
|
switch (obj->ID.num)
|
|
|
|
{
|
|
|
|
case Obj::TOWN:
|
|
|
|
case Obj::HERO:
|
|
|
|
case Obj::CREATURE_GENERATOR1:
|
|
|
|
case Obj::MINE: //TODO: check ai->knownSubterraneanGates
|
2014-02-17 10:36:03 +03:00
|
|
|
return true;
|
2014-02-15 19:38:51 +03:00
|
|
|
}
|
|
|
|
}
|
2014-02-17 10:36:03 +03:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<const CGObjectInstance *> objs;
|
|
|
|
for (auto obj : ai->visitableObjs)
|
|
|
|
{
|
|
|
|
if (conquerable(obj))
|
|
|
|
objs.push_back (obj);
|
2014-02-15 19:38:51 +03:00
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-20 16:01:44 +03:00
|
|
|
for (auto h : cb->getHeroesInfo())
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2014-02-15 22:39:03 +03:00
|
|
|
SectorMap sm(h);
|
2014-02-17 10:36:03 +03:00
|
|
|
std::vector<const CGObjectInstance *> ourObjs(objs); //copy common objects
|
|
|
|
|
|
|
|
for (auto obj : ai->reservedHeroesMap[h]) //add objects reserved by this hero
|
|
|
|
{
|
|
|
|
if (conquerable(obj))
|
|
|
|
ourObjs.push_back(obj);
|
|
|
|
}
|
|
|
|
for (auto obj : ourObjs) //double loop, performance risk?
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2014-02-15 19:38:51 +03:00
|
|
|
auto t = sm.firstTileToGet(h, obj->visitablePos()); //we assume that no more than one tile on the way is guarded
|
2014-02-17 20:28:39 +03:00
|
|
|
ai->whatToDoToReachTile (h, t, ret);
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
}
|
2013-12-20 16:01:44 +03:00
|
|
|
if (!objs.empty() && ai->canRecruitAnyHero()) //probably no point to recruit hero if we see no objects to capture
|
|
|
|
ret.push_back (sptr(Goals::RecruitHero()));
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-20 16:01:44 +03:00
|
|
|
if (ret.empty())
|
|
|
|
ret.push_back (sptr(Goals::Explore())); //we need to find an enemy
|
|
|
|
return ret;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
2013-12-20 16:01:44 +03:00
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
TSubgoal Build::whatToDoToAchieve()
|
|
|
|
{
|
2013-11-15 20:30:16 +03:00
|
|
|
return iAmElementar();
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal Invalid::whatToDoToAchieve()
|
|
|
|
{
|
2013-11-15 20:30:16 +03:00
|
|
|
return iAmElementar();
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-11-24 11:21:51 +03:00
|
|
|
std::string GatherArmy::completeMessage() const
|
|
|
|
{
|
|
|
|
return "Hero " + hero.get()->name + " gathered army of value " + boost::lexical_cast<std::string>(value);
|
2014-01-16 23:24:06 +03:00
|
|
|
}
|
2013-11-24 11:21:51 +03:00
|
|
|
|
2013-10-18 23:17:25 +03:00
|
|
|
TSubgoal GatherArmy::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
//TODO: find hero if none set
|
2013-12-23 23:46:01 +03:00
|
|
|
assert(hero.h);
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
//TODO: include evaluation of monsters gather in calculation
|
|
|
|
for (auto t : cb->getTownsInfo())
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-23 23:46:01 +03:00
|
|
|
auto pos = t->visitablePos();
|
|
|
|
if (ai->isAccessibleForHero(pos, hero))
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-23 23:46:01 +03:00
|
|
|
if(!t->visitingHero && howManyReinforcementsCanGet(hero,t))
|
|
|
|
{
|
|
|
|
if (!vstd::contains (ai->townVisitsThisWeek[hero], t))
|
|
|
|
ret.push_back (sptr (Goals::VisitTile(pos).sethero(hero)));
|
|
|
|
}
|
|
|
|
auto bid = ai->canBuildAnyStructure(t, std::vector<BuildingID>
|
|
|
|
(unitsSource, unitsSource + ARRAY_COUNT(unitsSource)), 8 - cb->getDate(Date::DAY_OF_WEEK));
|
|
|
|
if (bid != BuildingID::NONE)
|
|
|
|
ret.push_back (sptr(BuildThis(bid, t)));
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
auto otherHeroes = cb->getHeroesInfo();
|
|
|
|
auto heroDummy = hero;
|
|
|
|
erase_if(otherHeroes, [heroDummy](const CGHeroInstance * h)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2014-02-17 10:36:03 +03:00
|
|
|
return (h == heroDummy.h || !ai->isAccessibleForHero(heroDummy->visitablePos(), h, true)
|
|
|
|
|| !ai->canGetArmy(heroDummy.h, h) || ai->getGoal(h)->goalType == Goals::GATHER_ARMY);
|
2013-12-23 23:46:01 +03:00
|
|
|
});
|
|
|
|
for (auto h : otherHeroes)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-12-23 23:46:01 +03:00
|
|
|
ret.push_back (sptr (Goals::VisitHero(h->id.getNum()).setisAbstract(true).sethero(hero)));
|
|
|
|
//go to the other hero if we are faster
|
|
|
|
ret.push_back (sptr (Goals::VisitHero(hero->id.getNum()).setisAbstract(true).sethero(h)));
|
|
|
|
//let the other hero come to us
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
std::vector <const CGObjectInstance *> objs;
|
|
|
|
for (auto obj : ai->visitableObjs)
|
|
|
|
{
|
|
|
|
if(obj->ID == Obj::CREATURE_GENERATOR1)
|
|
|
|
{
|
2013-10-18 23:17:25 +03:00
|
|
|
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);
|
|
|
|
for(auto & creLevel : dwelling->creatures)
|
|
|
|
{
|
|
|
|
if(creLevel.first)
|
|
|
|
{
|
|
|
|
for(auto & creatureID : creLevel.second)
|
|
|
|
{
|
|
|
|
auto creature = VLC->creh->creatures[creatureID];
|
2013-12-23 23:46:01 +03:00
|
|
|
if (ai->freeResources().canAfford(creature->cost))
|
|
|
|
objs.push_back(obj);
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 23:46:01 +03:00
|
|
|
for(auto h : cb->getHeroesInfo())
|
|
|
|
{
|
|
|
|
for (auto obj : objs)
|
|
|
|
{ //find safe dwelling
|
|
|
|
auto pos = obj->visitablePos();
|
2014-02-15 19:38:51 +03:00
|
|
|
if (ai->isGoodForVisit(obj, h))
|
2013-12-23 23:46:01 +03:00
|
|
|
ret.push_back (sptr (Goals::VisitTile(pos).sethero(h)));
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 20:28:39 +03:00
|
|
|
if (ai->canRecruitAnyHero()) //this is not stupid in early phase of game
|
|
|
|
ret.push_back (sptr(Goals::RecruitHero()));
|
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
if (ret.empty())
|
2014-02-17 10:36:03 +03:00
|
|
|
{
|
|
|
|
if (hero == ai->primaryHero() || value >= 1.1f)
|
|
|
|
ret.push_back (sptr(Goals::Explore()));
|
|
|
|
else //workaround to break loop - seemingly there are no ways to explore left
|
|
|
|
throw goalFulfilledException (sptr(Goals::GatherArmy(0).sethero(hero)));
|
|
|
|
}
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-12-23 23:46:01 +03:00
|
|
|
return ret;
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-11-23 21:16:25 +03:00
|
|
|
//TSubgoal AbstractGoal::whatToDoToAchieve()
|
|
|
|
//{
|
|
|
|
// logAi->debugStream() << boost::format("Decomposing goal of type %s") % name();
|
|
|
|
// return sptr (Goals::Explore());
|
|
|
|
//}
|
2013-10-18 23:17:25 +03:00
|
|
|
|
2013-11-15 20:30:16 +03:00
|
|
|
TSubgoal AbstractGoal::goVisitOrLookFor(const CGObjectInstance *obj)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
|
|
|
if(obj)
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::GetObj(obj->id.getNum()));
|
2013-10-18 23:17:25 +03:00
|
|
|
else
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::Explore());
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-11-15 20:30:16 +03:00
|
|
|
TSubgoal AbstractGoal::lookForArtSmart(int aid)
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
2013-11-15 20:30:16 +03:00
|
|
|
return sptr (Goals::Invalid());
|
2013-10-18 23:17:25 +03:00
|
|
|
}
|
|
|
|
|
2013-11-15 20:30:16 +03:00
|
|
|
bool AbstractGoal::invalid() const
|
2013-10-18 23:17:25 +03:00
|
|
|
{
|
|
|
|
return goalType == INVALID;
|
2013-10-20 16:51:05 +03:00
|
|
|
}
|
2013-11-23 21:16:25 +03:00
|
|
|
|
|
|
|
void AbstractGoal::accept (VCAI * ai)
|
|
|
|
{
|
|
|
|
ai->tryRealize(*this);
|
2013-11-24 14:36:51 +03:00
|
|
|
}
|
|
|
|
|
2013-12-19 23:21:21 +03:00
|
|
|
|
2013-11-24 14:36:51 +03:00
|
|
|
template<typename T>
|
|
|
|
void CGoal<T>::accept (VCAI * ai)
|
|
|
|
{
|
|
|
|
ai->tryRealize(static_cast<T&>(*this)); //casting enforces template instantiation
|
|
|
|
}
|
2013-11-24 18:30:17 +03:00
|
|
|
|
2013-12-19 23:21:21 +03:00
|
|
|
float AbstractGoal::accept (FuzzyHelper * f)
|
|
|
|
{
|
|
|
|
return f->evaluate(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
float CGoal<T>::accept (FuzzyHelper * f)
|
|
|
|
{
|
|
|
|
return f->evaluate(static_cast<T&>(*this)); //casting enforces template instantiation
|
|
|
|
}
|