From c58f338d1beb46dec815479378c2419489598642 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 29 Mar 2011 10:01:01 +0000 Subject: [PATCH] - clear pathfinder precache for units when they die to free memory sooner --- source/glest_game/ai/path_finder.cpp | 11 +++++++++++ source/glest_game/ai/path_finder.h | 1 + source/glest_game/type_instances/unit.cpp | 6 ++++-- source/glest_game/world/unit_updater.cpp | 6 ++++++ source/glest_game/world/unit_updater.h | 1 + 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/source/glest_game/ai/path_finder.cpp b/source/glest_game/ai/path_finder.cpp index aa11076a..a54fc1f8 100644 --- a/source/glest_game/ai/path_finder.cpp +++ b/source/glest_game/ai/path_finder.cpp @@ -85,6 +85,17 @@ void PathFinder::clearUnitPrecache(Unit *unit) { factions[unit->getFactionIndex()].precachedPath[unit->getId()].clear(); } +void PathFinder::removeUnitPrecache(Unit *unit) { + if(factions.size() > unit->getFactionIndex()) { + if(factions[unit->getFactionIndex()].precachedTravelState.find(unit->getId()) != factions[unit->getFactionIndex()].precachedTravelState.end()) { + factions[unit->getFactionIndex()].precachedTravelState.erase(unit->getId()); + } + if(factions[unit->getFactionIndex()].precachedPath.find(unit->getId()) != factions[unit->getFactionIndex()].precachedPath.end()) { + factions[unit->getFactionIndex()].precachedPath.erase(unit->getId()); + } + } +} + TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck, int frameIndex) { //printf("PathFinder::findPath...\n"); diff --git a/source/glest_game/ai/path_finder.h b/source/glest_game/ai/path_finder.h index d6d418cc..d483dc40 100644 --- a/source/glest_game/ai/path_finder.h +++ b/source/glest_game/ai/path_finder.h @@ -101,6 +101,7 @@ public: void init(const Map *map); TravelState findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck=NULL,int frameIndex=-1); void clearUnitPrecache(Unit *unit); + void removeUnitPrecache(Unit *unit); private: TravelState aStar(Unit *unit, const Vec2i &finalPos, bool inBailout, int frameIndex); diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 3b578d56..8513f2c4 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -988,14 +988,16 @@ void Unit::kill() { clearCommands(); UnitUpdater *unitUpdater = game->getWorld()->getUnitUpdater(); - unitUpdater->clearUnitPrecache(this); + //unitUpdater->clearUnitPrecache(this); + unitUpdater->removeUnitPrecache(this); } void Unit::undertake() { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to undertake unit id = %d [%s] [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->id, this->getFullName().c_str(),this->getDesc().c_str()); UnitUpdater *unitUpdater = game->getWorld()->getUnitUpdater(); - unitUpdater->clearUnitPrecache(this); + //unitUpdater->clearUnitPrecache(this); + unitUpdater->removeUnitPrecache(this); livingUnits.erase(id); livingUnitsp.erase(this); diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index ac3db0c0..58286dca 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -89,6 +89,12 @@ void UnitUpdater::clearUnitPrecache(Unit *unit) { } } +void UnitUpdater::removeUnitPrecache(Unit *unit) { + if(pathFinder != NULL) { + pathFinder->removeUnitPrecache(unit); + } +} + UnitUpdater::~UnitUpdater() { //UnitRangeCellsLookupItemCache.clear(); diff --git a/source/glest_game/world/unit_updater.h b/source/glest_game/world/unit_updater.h index 0e30c1ff..edc3daff 100644 --- a/source/glest_game/world/unit_updater.h +++ b/source/glest_game/world/unit_updater.h @@ -113,6 +113,7 @@ public: void updateMorph(Unit *unit, int frameIndex); void clearUnitPrecache(Unit *unit); + void removeUnitPrecache(Unit *unit); unsigned int getAttackWarningCount() const { return attackWarnings.size(); } std::pair unitBeingAttacked(const Unit *unit);