From dda594cf3341419cf7e7ac679aec48d905bcbfb0 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 19 Oct 2010 16:54:48 +0000 Subject: [PATCH] - a slight change in dealing with units getting stuck while harvesting. Trying to keep them busy instead of standing around. --- source/glest_game/ai/path_finder.cpp | 19 +++++++--- source/glest_game/ai/path_finder.h | 2 +- source/glest_game/world/unit_updater.cpp | 45 +++++++++++++++--------- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/source/glest_game/ai/path_finder.cpp b/source/glest_game/ai/path_finder.cpp index 3d6e054e..62d59720 100644 --- a/source/glest_game/ai/path_finder.cpp +++ b/source/glest_game/ai/path_finder.cpp @@ -73,7 +73,7 @@ PathFinder::~PathFinder(){ map=NULL; } -TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos) { +TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck) { if(map == NULL) { throw runtime_error("map == NULL"); @@ -135,6 +135,9 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos) { // We will try to bail out of the immediate area if( ts == tsBlocked && unit->getInBailOutAttempt() == false && path->isStuck() == true) { + if(wasStuck != NULL) { + *wasStuck = true; + } unit->setInBailOutAttempt(true); // Try to bail out up to 20 cells away @@ -143,6 +146,8 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos) { const Vec2i newFinalPos = finalPos + Vec2i(bailoutX,bailoutY); if(map->canMove(unit, unit->getPos(), newFinalPos)) { ts= aStar(unit, newFinalPos, true); + + /* if(ts == tsMoving) { unit->setInBailOutAttempt(false); @@ -173,6 +178,8 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos) { throw runtime_error("unsupported or missing path finder detected!"); } } + */ + //else if(ts == tsArrived) { // ts = aStar(unit, finalPos, true); // break; @@ -182,11 +189,13 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos) { } unit->setInBailOutAttempt(false); - if(ts == tsArrived) { - ts = tsBlocked; - } + //if(ts == tsArrived) { + // ts = tsBlocked; + //} + } + if(ts == tsArrived || ts == tsBlocked) { + unit->setCurrSkill(scStop); } - unit->setCurrSkill(scStop); break; case tsMoving: { diff --git a/source/glest_game/ai/path_finder.h b/source/glest_game/ai/path_finder.h index 98122aa1..175350ef 100644 --- a/source/glest_game/ai/path_finder.h +++ b/source/glest_game/ai/path_finder.h @@ -68,7 +68,7 @@ public: PathFinder(const Map *map); ~PathFinder(); void init(const Map *map); - TravelState findPath(Unit *unit, const Vec2i &finalPos); + TravelState findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck=NULL); private: TravelState aStar(Unit *unit, const Vec2i &finalPos, bool inBailout); diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 63192a55..7b17afe7 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -605,7 +605,7 @@ void UnitUpdater::updateHarvest(Unit *unit) { } if (canHarvestDestPos == true) { - unit->setLastHarvestResourceTarget(&targetPos); + unit->setLastHarvestResourceTarget(NULL); //if it finds resources it starts harvesting unit->setCurrSkill(hct->getHarvestSkillType()); @@ -625,12 +625,14 @@ void UnitUpdater::updateHarvest(Unit *unit) { } } else { + unit->setLastHarvestResourceTarget(&targetPos); //if not continue walking + bool wasStuck = false; TravelState tsValue = tsImpossible; switch(this->game->getGameSettings()->getPathFinderType()) { case pfBasic: - tsValue = pathFinder->findPath(unit, command->getPos()); + tsValue = pathFinder->findPath(unit, command->getPos(), &wasStuck); if (tsValue == tsMoving) { unit->setCurrSkill(hct->getMoveSkillType()); } @@ -644,12 +646,20 @@ void UnitUpdater::updateHarvest(Unit *unit) { default: throw runtime_error("detected unsupported pathfinder type!"); } + + if(wasStuck == true) { + //if can't harvest, search for another resource + unit->setCurrSkill(scStop); + if(searchForResource(unit, hct) == false) { + unit->finishCommand(); + } + } } } - else{ + else { //if can't harvest, search for another resource unit->setCurrSkill(scStop); - if(!searchForResource(unit, hct)){ + if(searchForResource(unit, hct) == false) { unit->finishCommand(); } } @@ -699,7 +709,7 @@ void UnitUpdater::updateHarvest(Unit *unit) { unit->setLoadCount(0); } } - else{ + else { unit->finishCommand(); } } @@ -1358,25 +1368,28 @@ void UnitUpdater::startAttackParticleSystem(Unit *unit){ //looks for a resource of type rt, if rt==NULL looks for any //resource the unit can harvest -bool UnitUpdater::searchForResource(Unit *unit, const HarvestCommandType *hct){ +bool UnitUpdater::searchForResource(Unit *unit, const HarvestCommandType *hct) { Chrono chrono; chrono.start(); Vec2i pos= unit->getCurrCommand()->getPos(); - for(int radius= 0; radiusisInside(i, j)){ + for(int radius= 0; radius < maxResSearchRadius; radius++) { + for(int i = pos.x - radius; i <= pos.x + radius; ++i) { + for(int j=pos.y - radius; j <= pos.y + radius; ++j) { + if(map->isInside(i, j)) { Resource *r= map->getSurfaceCell(Map::toSurfCoords(Vec2i(i, j)))->getResource(); - if(r!=NULL){ - if(hct->canHarvest(r->getType())){ - unit->getCurrCommand()->setPos(Vec2i(i, j)); + if(r != NULL) { + if(hct->canHarvest(r->getType())) { + const Vec2i newPos = Vec2i(i, j); + if(unit->isBadHarvestPos(newPos) == false) { + unit->getCurrCommand()->setPos(newPos); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); - if(chrono.getMillis() > 0) chrono.start(); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); - return true; + return true; + } } } }