- disabled the recently introduced pathfinding cache as its too slow and not sure that it really does any good. This should help performance to be better.

This commit is contained in:
Mark Vejvoda 2010-10-24 06:53:30 +00:00
parent bbc8f96327
commit 117521a8d4
2 changed files with 66 additions and 24 deletions

View File

@ -96,7 +96,7 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStu
unit->setCurrentUnitTitle(szBuf); unit->setCurrentUnitTitle(szBuf);
} }
unit->getFaction()->addCachedPath(finalPos,unit); //unit->getFaction()->addCachedPath(finalPos,unit);
return tsArrived; return tsArrived;
} }
else { else {
@ -131,6 +131,7 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStu
//route cache miss //route cache miss
ts = aStar(unit, finalPos, false); ts = aStar(unit, finalPos, false);
/*
if(ts == tsBlocked) { if(ts == tsBlocked) {
std::vector<Vec2i> cachedPath = unit->getFaction()->findCachedPath(finalPos, unit); std::vector<Vec2i> cachedPath = unit->getFaction()->findCachedPath(finalPos, unit);
if(cachedPath.size() > 0) { if(cachedPath.size() > 0) {
@ -144,15 +145,17 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStu
unit->addCurrentTargetPathTakenCell(Vec2i(-1),Vec2i(-1)); unit->addCurrentTargetPathTakenCell(Vec2i(-1),Vec2i(-1));
} }
} }
*/
//post actions //post actions
switch(ts) { switch(ts) {
case tsBlocked: case tsBlocked:
case tsArrived: case tsArrived:
if(ts == tsArrived) { //if(ts == tsArrived) {
unit->getFaction()->addCachedPath(finalPos,unit); // unit->getFaction()->addCachedPath(finalPos,unit);
} //}
// The unit is stuck (not only blocked but unable to go anywhere for a while) // The unit is stuck (not only blocked but unable to go anywhere for a while)
// We will try to bail out of the immediate area // We will try to bail out of the immediate area
if( ts == tsBlocked && unit->getInBailOutAttempt() == false && if( ts == tsBlocked && unit->getInBailOutAttempt() == false &&
@ -269,6 +272,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
const Vec2i finalPos= computeNearestFreePos(unit, targetPos); const Vec2i finalPos= computeNearestFreePos(unit, targetPos);
//if arrived //if arrived
/*
if(finalPos == unit->getPos()) { if(finalPos == unit->getPos()) {
Command *command= unit->getCurrCommand(); Command *command= unit->getCurrCommand();
if(command == NULL || command->getPos() != unit->getPos()) { if(command == NULL || command->getPos() != unit->getPos()) {
@ -285,6 +289,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
return tsArrived; return tsArrived;
} }
} }
*/
//path find algorithm //path find algorithm

View File

@ -707,9 +707,10 @@ void Faction::addCloseResourceTargetToCache(const Vec2i &pos) {
Vec2i newPos = pos + Vec2i(j,k); Vec2i newPos = pos + Vec2i(j,k);
if(isResourceTargetInCache(newPos) == false) { if(isResourceTargetInCache(newPos) == false) {
if(map->isInside(newPos.x, newPos.y)) { if(map->isInside(newPos.x, newPos.y)) {
Resource *r= map->getSurfaceCell(map->toSurfCoords(newPos))->getResource(); Resource *r = map->getSurfaceCell(map->toSurfCoords(newPos))->getResource();
if(r != NULL) { if(r != NULL) {
addResourceTargetToCache(newPos); //addResourceTargetToCache(newPos);
cacheResourceTargetList[newPos] = 1;
} }
} }
} }
@ -717,35 +718,71 @@ void Faction::addCloseResourceTargetToCache(const Vec2i &pos) {
} }
} }
Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type) { Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type) {
Vec2i result(-1); Vec2i result(-1);
if(cacheResourceTargetList.size() > 0) { if(cacheResourceTargetList.size() > 0) {
std::vector<Vec2i> deleteList; std::vector<Vec2i> deleteList;
const int harvestDistance = 5;
const Map *map = world->getMap(); const Map *map = world->getMap();
for(std::map<Vec2i,int>::iterator iter = cacheResourceTargetList.begin(); Vec2i pos = unit->getPos();
iter != cacheResourceTargetList.end(); ++iter) {
const Vec2i &cache = iter->first; bool foundCloseResource = false;
const SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(cache)); // First look immediately around the unit's position
if( sc != NULL && sc->getResource() != NULL) { for(int j = -harvestDistance; j <= harvestDistance && foundCloseResource == false; ++j) {
const Resource *resource = sc->getResource(); for(int k = -harvestDistance; k <= harvestDistance && foundCloseResource == false; ++k) {
if(resource->getType() != NULL && resource->getType() == type) { Vec2i newPos = pos + Vec2i(j,k);
if(result.x < 0 || unit->getPos().dist(cache) < unit->getPos().dist(result)) { if(isResourceTargetInCache(newPos) == false) {
if(unit->isBadHarvestPos(cache) == false) { const SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(newPos));
result = cache; if( sc != NULL && sc->getResource() != NULL) {
// Close enough to our position, no more looking const Resource *resource = sc->getResource();
if(unit->getPos().dist(result) <= 5) { if(resource->getType() != NULL && resource->getType() == type) {
break; if(result.x < 0 || unit->getPos().dist(newPos) < unit->getPos().dist(result)) {
if(unit->isBadHarvestPos(newPos) == false) {
result = newPos;
foundCloseResource = true;
break;
}
}
}
}
else {
deleteList.push_back(newPos);
}
}
}
}
if(foundCloseResource == false) {
// Now check the whole cache
for(std::map<Vec2i,int>::iterator iter = cacheResourceTargetList.begin();
iter != cacheResourceTargetList.end() && foundCloseResource == false;
++iter) {
const Vec2i &cache = iter->first;
const SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(cache));
if( sc != NULL && sc->getResource() != NULL) {
const Resource *resource = sc->getResource();
if(resource->getType() != NULL && resource->getType() == type) {
if(result.x < 0 || unit->getPos().dist(cache) < unit->getPos().dist(result)) {
if(unit->isBadHarvestPos(cache) == false) {
result = cache;
// Close enough to our position, no more looking
if(unit->getPos().dist(result) <= (harvestDistance * 2)) {
foundCloseResource = true;
break;
}
} }
} }
} }
} }
} else {
else { deleteList.push_back(cache);
deleteList.push_back(cache); }
} }
} }
cleanupResourceTypeTargetCache(&deleteList); if(deleteList.size() > 0) {
cleanupResourceTypeTargetCache(&deleteList);
}
} }
return result; return result;