diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 4098acb9..0f2ff69f 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -33,6 +33,7 @@ namespace Glest{ namespace Game{ Faction::Faction() { texture = NULL; + lastResourceTargettListPurge = 0; } Faction::~Faction() { @@ -669,15 +670,21 @@ void Faction::resetResourceAmount(const ResourceType *rt){ assert(false); } -void Faction::addResourceTargetToCache(const Vec2i &pos) { - bool duplicateEntry = false; +bool Faction::isResourceTargetInCache(const Vec2i &pos, bool incrementUseCounter) { + bool result = false; if(cacheResourceTargetList.size() > 0) { std::map::iterator iter = cacheResourceTargetList.find(pos); - if(iter != cacheResourceTargetList.end()) { + result = (iter != cacheResourceTargetList.end()); + if(result == true && incrementUseCounter == true) { iter->second++; - duplicateEntry = true; } } + + return result; +} + +void Faction::addResourceTargetToCache(const Vec2i &pos) { + bool duplicateEntry = isResourceTargetInCache(pos,true); if(duplicateEntry == false) { cacheResourceTargetList[pos] = 1; } @@ -698,10 +705,12 @@ void Faction::addCloseResourceTargetToCache(const Vec2i &pos) { for(int j = -harvestDistance; j <= harvestDistance; ++j) { for(int k = -harvestDistance; k <= harvestDistance; ++k) { Vec2i newPos = pos + Vec2i(j,k); - if(map->isInside(newPos.x, newPos.y)) { - Resource *r= map->getSurfaceCell(map->toSurfCoords(newPos))->getResource(); - if(r != NULL) { - addResourceTargetToCache(newPos); + if(isResourceTargetInCache(newPos) == false) { + if(map->isInside(newPos.x, newPos.y)) { + Resource *r= map->getSurfaceCell(map->toSurfCoords(newPos))->getResource(); + if(r != NULL) { + addResourceTargetToCache(newPos); + } } } } @@ -744,29 +753,33 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceT void Faction::cleanupResourceTypeTargetCache(std::vector *deleteListPtr) { if(cacheResourceTargetList.size() > 0) { - std::vector deleteList; - if(deleteListPtr != NULL) { - deleteList = *deleteListPtr; - } - else { - for(std::map::iterator iter = cacheResourceTargetList.begin(); - iter != cacheResourceTargetList.end(); ++iter) { - const Vec2i &cache = iter->first; + if(deleteListPtr != NULL || difftime(time(NULL),lastResourceTargettListPurge) >= 120) { + lastResourceTargettListPurge = time(NULL); + std::vector deleteList; - if(world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache)) != NULL) { - Resource *resource = world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache))->getResource(); - if(resource == NULL) { + if(deleteListPtr != NULL) { + deleteList = *deleteListPtr; + } + else { + for(std::map::iterator iter = cacheResourceTargetList.begin(); + iter != cacheResourceTargetList.end(); ++iter) { + const Vec2i &cache = iter->first; + + if(world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache)) != NULL) { + Resource *resource = world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache))->getResource(); + if(resource == NULL) { + deleteList.push_back(cache); + } + } + else { deleteList.push_back(cache); } } - else { - deleteList.push_back(cache); - } } - } - for(int i = 0; i < deleteList.size(); ++i) { - Vec2i &cache = deleteList[i]; - cacheResourceTargetList.erase(cache); + for(int i = 0; i < deleteList.size(); ++i) { + Vec2i &cache = deleteList[i]; + cacheResourceTargetList.erase(cache); + } } } } diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index 9d6b920a..b7e032ab 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -85,6 +85,7 @@ private: std::map > successfulPathFinderTargetList; std::map cacheResourceTargetList; + time_t lastResourceTargettListPurge; public: Faction(); @@ -156,6 +157,7 @@ public: std::vector findCachedPath(const Vec2i &target, Unit *unit); void addCachedPath(const Vec2i &target, Unit *unit); + bool isResourceTargetInCache(const Vec2i &pos,bool incrementUseCounter=false); void addResourceTargetToCache(const Vec2i &pos); void removeResourceTargetFromCache(const Vec2i &pos); void addCloseResourceTargetToCache(const Vec2i &pos); diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index ef1c876c..c999b371 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -184,6 +184,7 @@ Unit::Unit(int id, UnitPathInterface *unitpath, const Vec2i &pos, const UnitType this->screenPos = Vec3f(0.0); this->inBailOutAttempt = false; this->lastHarvestResourceTarget.first = Vec2i(0); + this->lastBadHarvestListPurge = 0; level= NULL; loadType= NULL; @@ -1686,11 +1687,19 @@ void Unit::logSynchData(string source) { void Unit::addBadHarvestPos(const Vec2i &value) { Chrono chron; chron.start(); - badHarvestPosList.push_back(std::pair(value,chron)); + //badHarvestPosList.push_back(std::pair(value,chron)); + badHarvestPosList[value] = chron; cleanupOldBadHarvestPos(); } void Unit::removeBadHarvestPos(const Vec2i &value) { + std::map::iterator iter = badHarvestPosList.find(value); + if(iter != badHarvestPosList.end()) { + badHarvestPosList.erase(value); + } + cleanupOldBadHarvestPos(); + +/* for(int i = 0; i < badHarvestPosList.size(); ++i) { const std::pair &item = badHarvestPosList[i]; if(item.first == value) { @@ -1699,12 +1708,21 @@ void Unit::removeBadHarvestPos(const Vec2i &value) { } } cleanupOldBadHarvestPos(); +*/ + } bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const { //cleanupOldBadHarvestPos(); bool result = false; + + std::map::const_iterator iter = badHarvestPosList.find(value); + if(iter != badHarvestPosList.end()) { + result = true; + } + +/* for(int i = 0; i < badHarvestPosList.size(); ++i) { const std::pair &item = badHarvestPosList[i]; if(item.first == value) { @@ -1712,7 +1730,7 @@ bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const { break; } } - +*/ if(result == false && checkPeerUnits == true) { // Check if any other units of similar type have this position tagged // as bad? @@ -1732,6 +1750,21 @@ bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const { } void Unit::cleanupOldBadHarvestPos() { + if(difftime(time(NULL),lastBadHarvestListPurge) >= 240) { + lastBadHarvestListPurge = time(NULL); + std::vector purgeList; + for(std::map::iterator iter = badHarvestPosList.begin(); iter != badHarvestPosList.end(); iter++) { + if(iter->second.getMillis() >= 2400000) { + purgeList.push_back(iter->first); + } + } + for(int i = 0; i < purgeList.size(); ++i) { + const Vec2i &item = purgeList[i]; + badHarvestPosList.erase(item); + } + } + +/* for(int i = badHarvestPosList.size() - 1; i >= 0; --i) { const std::pair &item = badHarvestPosList[i]; @@ -1741,6 +1774,7 @@ void Unit::cleanupOldBadHarvestPos() { badHarvestPosList.erase(badHarvestPosList.begin() + i); } } +*/ } void Unit::setLastHarvestResourceTarget(const Vec2i *pos) { diff --git a/source/glest_game/type_instances/unit.h b/source/glest_game/type_instances/unit.h index 629bd07a..adf25528 100644 --- a/source/glest_game/type_instances/unit.h +++ b/source/glest_game/type_instances/unit.h @@ -300,7 +300,9 @@ private: // constantly getting blocked from getting to the resource so this // list may be used to tell areas of the game to ignore those cells for a // period of time - std::vector > badHarvestPosList; + //std::vector > badHarvestPosList; + std::map badHarvestPosList; + time_t lastBadHarvestListPurge; std::pair lastHarvestResourceTarget; std::pair > currentTargetPathTaken; @@ -449,8 +451,8 @@ public: bool getInBailOutAttempt() const { return inBailOutAttempt; } void setInBailOutAttempt(bool value) { inBailOutAttempt = value; } - std::vector > getBadHarvestPosList() const { return badHarvestPosList; } - void setBadHarvestPosList(std::vector > value) { badHarvestPosList = value; } + //std::vector > getBadHarvestPosList() const { return badHarvestPosList; } + //void setBadHarvestPosList(std::vector > value) { badHarvestPosList = value; } void addBadHarvestPos(const Vec2i &value); void removeBadHarvestPos(const Vec2i &value); bool isBadHarvestPos(const Vec2i &value,bool checkPeerUnits=true) const;