From 6104dedc76f3cc07cd95ceb4ffacd7dad8d24f15 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Wed, 20 Oct 2010 07:28:27 +0000 Subject: [PATCH] - added a resource harvest success cache for stuck units to look at when they cannot harvest. --- source/glest_game/type_instances/faction.cpp | 51 ++++++++++++++++++++ source/glest_game/type_instances/faction.h | 6 +++ source/glest_game/type_instances/unit.cpp | 6 ++- source/glest_game/world/map.cpp | 11 ++++- source/glest_game/world/unit_updater.cpp | 2 + 5 files changed, 73 insertions(+), 3 deletions(-) diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 394073bc..52b4df88 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -584,6 +584,57 @@ void Faction::resetResourceAmount(const ResourceType *rt){ assert(false); } +void Faction::addResourceTypeTargetToCache(const ResourceType *type, const Vec2i &pos) { + bool duplicateEntry = false; + if(cacheResourceTypeTargetList.size() > 0) { + for(int i = 0; i < cacheResourceTypeTargetList.size(); ++i) { + std::pair &cache = cacheResourceTypeTargetList[i]; + if(cache.first == type && cache.second == pos) { + duplicateEntry = true; + break; + } + } + } + if(duplicateEntry == false) { + cacheResourceTypeTargetList.push_back(make_pair(type,pos)); + } + + cleanupResourceTypeTargetCache(); +} + +Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type) { + Vec2i result(-1); + if(cacheResourceTypeTargetList.size() > 0) { + for(int i = 0; i < cacheResourceTypeTargetList.size(); ++i) { + std::pair &cache = cacheResourceTypeTargetList[i]; + + Resource *resource = world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache.second))->getResource(); + if(resource != NULL && cache.first == type) { + if(result.x < 0 || unit->getPos().dist(cache.second) < unit->getPos().dist(result)) { + result = cache.second; + } + } + } + } + + cleanupResourceTypeTargetCache(); + + return result; +} + +void Faction::cleanupResourceTypeTargetCache() { + if(cacheResourceTypeTargetList.size() > 0) { + for(int i = cacheResourceTypeTargetList.size() - 1; i >= 0; --i) { + std::pair &cache = cacheResourceTypeTargetList[i]; + + Resource *resource = world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache.second))->getResource(); + if(resource == NULL) { + cacheResourceTypeTargetList.erase(cacheResourceTypeTargetList.begin() + i); + } + } + } +} + std::vector Faction::findCachedPath(const Vec2i &target, Unit *unit) { std::vector result; if(successfulPathFinderTargetList.find(target) == successfulPathFinderTargetList.end()) { diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index d6352017..46ba68c1 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -84,6 +84,7 @@ private: bool thisFaction; std::map > successfulPathFinderTargetList; + std::vector > cacheResourceTypeTargetList; public: Faction(); @@ -155,6 +156,11 @@ public: std::vector findCachedPath(const Vec2i &target, Unit *unit); void addCachedPath(const Vec2i &target, Unit *unit); + //std::map cacheResourceTypeTargetList; + void addResourceTypeTargetToCache(const ResourceType *type, const Vec2i &pos); + Vec2i getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type); + void cleanupResourceTypeTargetCache(); + std::string toString() const; private: diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index e0537a0e..ef1c876c 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -678,8 +678,10 @@ bool Unit::anyCommand() const{ //return current command, assert that there is always one command Command *Unit::getCurrCommand() const{ - assert(!commands.empty()); - return commands.front(); + if(commands.empty() == false) { + return commands.front(); + } + return NULL; } void Unit::replaceCurrCommand(Command *cmd) { diff --git a/source/glest_game/world/map.cpp b/source/glest_game/world/map.cpp index 9d3fb675..614c1c67 100644 --- a/source/glest_game/world/map.cpp +++ b/source/glest_game/world/map.cpp @@ -278,7 +278,7 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour peerUnit->getLoadType() == rt && peerUnit->getCurrCommand() != NULL) { - if(unit->getPos().dist(peerUnit->getCurrCommand()->getPos()) <= 30) { + if(unit->getPos().dist(peerUnit->getCurrCommand()->getPos()) <= 40) { if(i == 0 || (unit->getPos().dist(peerUnit->getCurrCommand()->getPos()) < unit->getPos().dist(resourcePos))) { resourcePos = peerUnit->getCurrCommand()->getPos(); } @@ -290,6 +290,15 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour } } } + + Vec2i result = unit->getFaction()->getClosestResourceTypeTargetFromCache(unit, rt); + if(result.x >= 0) { + resourcePos = result; + + if(unit->getPos().dist(resourcePos) <= 5) { + return true; + } + } } return false; } diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 622f4319..c479b228 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -616,6 +616,7 @@ void UnitUpdater::updateHarvest(Unit *unit) { unit->setTargetPos(targetPos); command->setPos(targetPos); unit->setLoadCount(0); + unit->getFaction()->addResourceTypeTargetToCache(r->getType(), targetPos); switch(this->game->getGameSettings()->getPathFinderType()) { case pfBasic: @@ -675,6 +676,7 @@ void UnitUpdater::updateHarvest(Unit *unit) { unit->setTargetPos(targetPos); command->setPos(targetPos); unit->setLoadCount(0); + unit->getFaction()->addResourceTypeTargetToCache(r->getType(), targetPos); switch(this->game->getGameSettings()->getPathFinderType()) { case pfBasic: