diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 23ffeee9..aa81b3cc 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -602,6 +602,19 @@ void Faction::addResourceTargetToCache(const Vec2i &pos) { cleanupResourceTypeTargetCache(); } +void Faction::removeResourceTargetFromCache(const Vec2i &pos) { + if(cacheResourceTargetList.size() > 0) { + for(int i = 0; i < cacheResourceTargetList.size(); ++i) { + const Vec2i &cache = cacheResourceTargetList[i]; + if(cache == pos) { + cacheResourceTargetList.erase(cacheResourceTargetList.begin() + i); + break; + } + } + } + cleanupResourceTypeTargetCache(); +} + Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type) { Vec2i result(-1); if(cacheResourceTargetList.size() > 0) { @@ -614,7 +627,29 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceT const Resource *resource = sc->getResource(); if(resource->getType() != NULL && resource->getType() == type) { if(result.x < 0 || unit->getPos().dist(cache) < unit->getPos().dist(result)) { - result = cache; + if(unit->isBadHarvestPos(cache) == false) { + result = cache; + } + else { + const int harvestDistance = 5; + int size = unit->getType()->getSize(); + for(int j = -harvestDistance; j <= size; ++j) { + for(int k = -harvestDistance; k <= size; ++k) { + Vec2i newPos = unit->getPos() + Vec2i(j,k); + if(map->isInside(newPos.x, newPos.y)) { + Resource *r= map->getSurfaceCell(map->toSurfCoords(newPos))->getResource(); + if(r != NULL) { + if(r->getType() == type) { + if(unit->isBadHarvestPos(newPos) == false) { + result= newPos; + } + } + } + } + } + } + + } } } } diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index d7d8e979..90646333 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -157,6 +157,7 @@ public: void addCachedPath(const Vec2i &target, Unit *unit); void addResourceTargetToCache(const Vec2i &pos); + void removeResourceTargetFromCache(const Vec2i &pos); Vec2i getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type); void cleanupResourceTypeTargetCache(); diff --git a/source/glest_game/world/map.cpp b/source/glest_game/world/map.cpp index 33f30c26..88a93742 100644 --- a/source/glest_game/world/map.cpp +++ b/source/glest_game/world/map.cpp @@ -272,6 +272,9 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour if(fallbackToPeersHarvestingSameResource == true && unit != NULL) { // Look for another unit that is currently harvesting the same resource // type right now + + /* This should not be needed due to the check below + * for(int i = 0; i < unit->getFaction()->getUnitCount(); ++i) { Unit *peerUnit = unit->getFaction()->getUnit(i); if( peerUnit != NULL && peerUnit->getId() != unit->getId() && @@ -293,6 +296,7 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour } } } + */ // Check the faction cache for a known position where we can harvest diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 2d6d5c70..71e835c1 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -771,7 +771,7 @@ void UnitUpdater::updateHarvest(Unit *unit) { } //world->changePosCells(unit,unit->getPos()+unit->getDest()); - if(map->isNextTo(unit->getPos(), store)){ + if(map->isNextTo(unit->getPos(), store)) { //update resources int resourceAmount= unit->getLoadCount(); @@ -800,7 +800,8 @@ void UnitUpdater::updateHarvest(Unit *unit) { //if working //unit->setLastHarvestResourceTarget(NULL); - SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(unit->getTargetPos())); + const Vec2i unitTargetPos = unit->getTargetPos(); + SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(unitTargetPos)); Resource *r= sc->getResource(); if (r != NULL) { @@ -821,6 +822,7 @@ void UnitUpdater::updateHarvest(Unit *unit) { if (r->decAmount(1)) { const ResourceType *rt = r->getType(); sc->deleteResource(); + unit->getFaction()->removeResourceTargetFromCache(unitTargetPos); switch(this->game->getGameSettings()->getPathFinderType()) { case pfBasic: