1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

VCAI::wander: map object selection refactoring

Avoid doing extra steps that's not needed. Related to issue 2454.
This commit is contained in:
Arseniy Shestakov 2016-08-11 16:53:05 +03:00
parent 8533ee3256
commit b83dea2008
2 changed files with 13 additions and 27 deletions

View File

@ -1408,24 +1408,6 @@ bool VCAI::isGoodForVisit(const CGObjectInstance *obj, HeroPtr h, SectorMap &sm)
return false;
}
std::vector<const CGObjectInstance *> VCAI::getPossibleDestinations(HeroPtr h)
{
validateVisitableObjs();
std::vector<const CGObjectInstance *> possibleDestinations;
auto sm = getCachedSectorMap(h);
for(const CGObjectInstance *obj : visitableObjs)
{
if (isGoodForVisit(obj, h, *sm))
{
possibleDestinations.push_back(obj);
}
}
boost::sort(possibleDestinations, CDistanceSorter(h.get()));
return possibleDestinations;
}
bool VCAI::isTileNotReserved(const CGHeroInstance * h, int3 t)
{
if (t.valid())
@ -1468,20 +1450,25 @@ void VCAI::wander(HeroPtr h)
while (h->movement)
{
validateVisitableObjs();
std::vector <ObjectIdRef> dests, tmp;
std::vector <ObjectIdRef> dests;
auto sm = getCachedSectorMap(h);
range::copy(reservedHeroesMap[h], std::back_inserter(tmp)); //also visit our reserved objects - but they are not prioritized to avoid running back and forth
for (auto obj : tmp)
//also visit our reserved objects - but they are not prioritized to avoid running back and forth
vstd::copy_if(reservedHeroesMap[h], std::back_inserter(dests), [&](ObjectIdRef obj) -> bool
{
int3 pos = sm->firstTileToGet(h, obj->visitablePos());
if (pos.valid())
if (isAccessibleForHero (pos, h)) //even nearby objects could be blocked by other heroes :(
dests.push_back(obj); //can't use lambda for member function :(
}
if(pos.valid() && isAccessibleForHero(pos, h)) //even nearby objects could be blocked by other heroes :(
return true;
return false;
});
vstd::copy_if(visitableObjs, std::back_inserter(dests), [&](ObjectIdRef obj) -> bool
{
return isGoodForVisit(obj, h, *sm);
});
range::copy(getPossibleDestinations(h), std::back_inserter(dests));
vstd::erase_if(dests, [&](ObjectIdRef obj) -> bool
{
return !isSafeToVisit(h, sm->firstTileToGet(h, obj->visitablePos()));

View File

@ -259,7 +259,6 @@ public:
void recruitHero(const CGTownInstance * t, bool throwing = false);
bool isGoodForVisit(const CGObjectInstance *obj, HeroPtr h, SectorMap &sm);
std::vector<const CGObjectInstance *> getPossibleDestinations(HeroPtr h);
void buildStructure(const CGTownInstance * t);
//void recruitCreatures(const CGTownInstance * t);
void recruitCreatures(const CGDwelling * d, const CArmedInstance * recruiter);