diff --git a/source/glest_game/ai/path_finder.cpp b/source/glest_game/ai/path_finder.cpp index b68bace7..b67b519b 100644 --- a/source/glest_game/ai/path_finder.cpp +++ b/source/glest_game/ai/path_finder.cpp @@ -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 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 diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 0f2ff69f..93e93444 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -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 deleteList; + + const int harvestDistance = 5; const Map *map = world->getMap(); - for(std::map::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::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;