1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-09-16 09:26:28 +02:00

- EXPLORE goal can now be complete soAI won't get stuck

- AI won't try to visit tiles occupied by allied heroes by default
This commit is contained in:
DjWarmonger
2012-05-09 07:56:39 +00:00
parent 05742afd60
commit b597171f91
3 changed files with 14 additions and 3 deletions

View File

@@ -156,7 +156,7 @@ BattleAction CStupidAI::activeStack( const CStack * stack )
else else
{ {
const EnemyInfo &ei= *std::min_element(enemiesUnreachable.begin(), enemiesUnreachable.end(), boost::bind(isCloser, _1, _2, boost::ref(dists))); const EnemyInfo &ei= *std::min_element(enemiesUnreachable.begin(), enemiesUnreachable.end(), boost::bind(isCloser, _1, _2, boost::ref(dists)));
if(distToNearestNeighbour(ei.s->position, dists) < GameConstants::BFIELD_SIZE) if(distToNearestNeighbour(ei.s->position, dists) < GameConstants::BFIELD_SIZE) //FIXME: rare crash when AI attacks banks
{ {
return goTowards(stack, ei.s->position); return goTowards(stack, ei.s->position);
} }

View File

@@ -235,6 +235,7 @@ bool remove_if_present(Container &c, const Item &item)
return false; return false;
} }
template <typename V, typename Item, typename Item2> template <typename V, typename Item, typename Item2>
bool remove_if_present(std::map<Item,V> & c, const Item2 &item) bool remove_if_present(std::map<Item,V> & c, const Item2 &item)
{ {
@@ -1443,9 +1444,17 @@ const CGObjectInstance * VCAI::getUnvisitedObj(const boost::function<bool(const
return NULL; return NULL;
} }
bool VCAI::isAccessibleForHero(const int3 & pos, const CGHeroInstance * h) const bool VCAI::isAccessibleForHero(const int3 & pos, const CGHeroInstance * h, bool includeAllies) const
{ {
cb->setSelection(h); cb->setSelection(h);
if (!includeAllies)
{ //don't visit tile occupied by allied hero
BOOST_FOREACH (auto obj, cb->getVisitableObjs(pos))
{
if (obj->ID == GameConstants::HEROI_TYPE && obj->tempOwner == h->tempOwner && obj != h)
return false;
}
}
return cb->getPathInfo(pos)->reachable(); return cb->getPathInfo(pos)->reachable();
} }
@@ -2904,6 +2913,8 @@ int3 SectorMap::firstTileToGet(const CGHeroInstance *h, crint3 dst)
if(!preds[dst]) if(!preds[dst])
{ {
write("test.txt"); write("test.txt");
ai->completeGoal (CGoal(EXPLORE).sethero(h)); //if we can't find the way, seemingly all tiles were explored
//TODO: more organized way?
throw cannotFulfillGoalException(str(format("Cannot find connection between sectors %d and %d") % src->id % dst->id)); throw cannotFulfillGoalException(str(format("Cannot find connection between sectors %d and %d") % src->id % dst->id));
} }

View File

@@ -305,7 +305,7 @@ public:
const CGHeroInstance *getHeroWithGrail() const; const CGHeroInstance *getHeroWithGrail() const;
const CGObjectInstance *getUnvisitedObj(const boost::function<bool(const CGObjectInstance *)> &predicate); const CGObjectInstance *getUnvisitedObj(const boost::function<bool(const CGObjectInstance *)> &predicate);
bool isAccessibleForHero(const int3 & pos, const CGHeroInstance * h) const; bool isAccessibleForHero(const int3 & pos, const CGHeroInstance * h, bool includeAllies = false) const;
const CGTownInstance *findTownWithTavern() const; const CGTownInstance *findTownWithTavern() const;