From 614d0c25dbec68515c24087ae5a5028fdcaaa79c Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 29 Mar 2011 04:57:36 +0000 Subject: [PATCH] - attempt to improve harvesting a bit --- source/glest_game/world/map.cpp | 44 +++++++++++++++++++++--- source/glest_game/world/unit_updater.cpp | 12 +++++-- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/source/glest_game/world/map.cpp b/source/glest_game/world/map.cpp index d3a795b8..456eb44e 100644 --- a/source/glest_game/world/map.cpp +++ b/source/glest_game/world/map.cpp @@ -343,10 +343,18 @@ bool Map::isInsideSurface(const Vec2i &sPos) const { return isInsideSurface(sPos.x, sPos.y); } +class FindBestPos { +public: + float distanceFromUnitNoAdjustment; + float distanceFromClickNoAdjustment; + Vec2i resourcePosNoAdjustment; +}; + //returns if there is a resource next to a unit, in "resourcePos" is stored the relative position of the resource bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resourcePos, int size, Unit *unit, bool fallbackToPeersHarvestingSameResource, Vec2i *resourceClickPos) const { + bool resourceNear = false; float distanceFromUnit=-1; float distanceFromClick=-1; @@ -440,6 +448,8 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour } if(resourceNear == false && resourceClickPos != NULL) { + std::vector bestPosList; + if(resourceClickPos) { //printf("^^^^^ unit [%s - %d]\n",unit->getFullName().c_str(),unit->getId()); } @@ -455,9 +465,21 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour if(r->getType() == rt) { //printf("^^^^^^ unit [%s - %d] resPos = [%s] resourceClickPos->dist(resPos) [%f] distanceFromClick [%f] unit->getCenteredPos().dist(resPos) [%f] distanceFromUnit [%f]\n",unit->getFullName().c_str(),unit->getId(),resPos.getString().c_str(),resourceClickPos->dist(resPos),distanceFromClick,unit->getCenteredPos().dist(resPos),distanceFromUnit); - //if(distanceFromClick < 0 || resourceClickPos->dist(resPos) <= distanceFromClick) { - if(unit == NULL || - (distanceFromUnit < 0 || unit->getCenteredPos().dist(resPos) <= distanceFromUnit)) { + if(unit == NULL || + (distanceFromUnit < 0 || unit->getCenteredPos().dist(resPos) <= (distanceFromUnit + 2.0))) { + + if(resourceClickPos->dist(resPos) <= 1.0) { + FindBestPos bestPosItem; + + bestPosItem.distanceFromUnitNoAdjustment = unit->getCenteredPos().dist(resPos); + bestPosItem.distanceFromClickNoAdjustment =distanceFromClick = resourceClickPos->dist(resPos); + bestPosItem.resourcePosNoAdjustment = resPos; + + bestPosList.push_back(bestPosItem); + } + + //printf("!!!! unit [%s - %d] resPos = [%s] resourceClickPos->dist(resPos) [%f] distanceFromClick [%f] unit->getCenteredPos().dist(resPos) [%f] distanceFromUnit [%f]\n",unit->getFullName().c_str(),unit->getId(),resPos.getString().c_str(),resourceClickPos->dist(resPos),distanceFromClick,unit->getCenteredPos().dist(resPos),distanceFromUnit); + if(distanceFromClick < 0 || resourceClickPos->dist(resPos) <= distanceFromClick) { if(resourceClickPos != NULL) { distanceFromClick = resourceClickPos->dist(resPos); } @@ -473,12 +495,26 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour //printf("%%----------- unit [%s - %d] resPos = [%s] resourceClickPos->dist(resPos) [%f] distanceFromClick [%f] unit->getCenteredPos().dist(resPos) [%f] distanceFromUnit [%f]\n",unit->getFullName().c_str(),unit->getId(),resPos.getString().c_str(),resourceClickPos->dist(resPos),distanceFromClick,unit->getCenteredPos().dist(resPos),distanceFromUnit); } } - //} + } } } } } } + + float bestUnitDist = distanceFromUnit; + for(unsigned int i = 0; i < bestPosList.size(); ++i) { + FindBestPos &bestPosItem = bestPosList[i]; + + if(bestPosItem.distanceFromUnitNoAdjustment < bestUnitDist) { + bestUnitDist = bestPosItem.distanceFromUnitNoAdjustment; + *resourceClickPos = bestPosItem.resourcePosNoAdjustment; + + if(unit == NULL || unit->isBadHarvestPos(*resourceClickPos) == false) { + //printf("%%----------- unit [%s - %d] resourceClickPos [%s] bestUnitDist [%f]\n",unit->getFullName().c_str(),unit->getId(),resourceClickPos->getString().c_str(),bestUnitDist); + } + } + } } return resourceNear; diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 0081904a..ac3db0c0 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -832,15 +832,21 @@ void UnitUpdater::updateHarvest(Unit *unit, int frameIndex) { switch(this->game->getGameSettings()->getPathFinderType()) { case pfBasic: { + const bool newHarvestPath = true; + bool isNearResource = false; Vec2i clickPos = command->getOriginalPos(); - bool isNearResource = map->isResourceNear(unit->getPos(), r->getType(), targetPos,unit->getType()->getSize(),unit, false,&clickPos); - //bool isNearResource = map->isResourceNear(unit->getPos(), r->getType(), targetPos,unit->getType()->getSize(),unit); + if(newHarvestPath == true) { + isNearResource = map->isResourceNear(unit->getPos(), r->getType(), targetPos,unit->getType()->getSize(),unit, false,&clickPos); + } + else { + isNearResource = map->isResourceNear(unit->getPos(), r->getType(), targetPos,unit->getType()->getSize(),unit); + } if(isNearResource == true) { if((unit->getPos().dist(command->getPos()) < harvestDistance || unit->getPos().dist(targetPos) < harvestDistance) && isNearResource == true) { canHarvestDestPos = true; } } - else { + else if(newHarvestPath == true) { if(clickPos != command->getOriginalPos()) { //printf("%%----------- unit [%s - %d] CHANGING RESOURCE POS from [%s] to [%s]\n",unit->getFullName().c_str(),unit->getId(),command->getOriginalPos().getString().c_str(),clickPos.getString().c_str());