- 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->getFaction()->addCachedPath(finalPos,unit);
//unit->getFaction()->addCachedPath(finalPos,unit);
return tsArrived;
}
else {
@ -131,6 +131,7 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStu
//route cache miss
ts = aStar(unit, finalPos, false);
/*
if(ts == tsBlocked) {
std::vector<Vec2i> cachedPath = unit->getFaction()->findCachedPath(finalPos, unit);
if(cachedPath.size() > 0) {
@ -144,15 +145,17 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStu
unit->addCurrentTargetPathTakenCell(Vec2i(-1),Vec2i(-1));
}
}
*/
//post actions
switch(ts) {
case tsBlocked:
case tsArrived:
if(ts == tsArrived) {
unit->getFaction()->addCachedPath(finalPos,unit);
}
//if(ts == tsArrived) {
// unit->getFaction()->addCachedPath(finalPos,unit);
//}
// 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
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);
//if arrived
/*
if(finalPos == unit->getPos()) {
Command *command= unit->getCurrCommand();
if(command == NULL || command->getPos() != unit->getPos()) {
@ -285,6 +289,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
return tsArrived;
}
}
*/
//path find algorithm

View File

@ -707,9 +707,10 @@ void Faction::addCloseResourceTargetToCache(const Vec2i &pos) {
Vec2i newPos = pos + Vec2i(j,k);
if(isResourceTargetInCache(newPos) == false) {
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) {
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 result(-1);
if(cacheResourceTargetList.size() > 0) {
std::vector<Vec2i> deleteList;
const int harvestDistance = 5;
const Map *map = world->getMap();
for(std::map<Vec2i,int>::iterator iter = cacheResourceTargetList.begin();
iter != cacheResourceTargetList.end(); ++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) <= 5) {
break;
Vec2i pos = unit->getPos();
bool foundCloseResource = false;
// First look immediately around the unit's position
for(int j = -harvestDistance; j <= harvestDistance && foundCloseResource == false; ++j) {
for(int k = -harvestDistance; k <= harvestDistance && foundCloseResource == false; ++k) {
Vec2i newPos = pos + Vec2i(j,k);
if(isResourceTargetInCache(newPos) == false) {
const SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(newPos));
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(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 {
deleteList.push_back(cache);
else {
deleteList.push_back(cache);
}
}
}
cleanupResourceTypeTargetCache(&deleteList);
if(deleteList.size() > 0) {
cleanupResourceTypeTargetCache(&deleteList);
}
}
return result;