From e3fd34c18ce197deb9785b1a3058c311d9ab09e1 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Mon, 21 Mar 2011 01:03:14 +0000 Subject: [PATCH] - attempt to make more randomized cell direction when harvesting using locations in the resource cache --- source/glest_game/type_instances/faction.cpp | 124 ++++++++++++++----- source/glest_game/type_instances/faction.h | 1 + 2 files changed, 94 insertions(+), 31 deletions(-) diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 7f22c2dcf..c14b26699 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -22,9 +22,11 @@ #include "tech_tree.h" #include "game.h" #include "config.h" +#include "randomgen.h" #include "leak_dumper.h" using namespace Shared::Util; +using Shared::Util::RandomGen; namespace Glest { namespace Game { @@ -925,25 +927,56 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceT bool foundCloseResource = false; // First look immediately around the unit's position - for(int j = -harvestDistance; j <= harvestDistance && foundCloseResource == false; ++j) { - for(int k = -harvestDistance; k <= harvestDistance && foundCloseResource == false; ++k) { - Vec2i newPos = pos + Vec2i(j,k); - if(map->isInside(newPos) == true && isResourceTargetInCache(newPos) == false) { - const SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(newPos)); - if( sc != NULL && sc->getResource() != NULL) { - const Resource *resource = sc->getResource(); - if(resource->getType() != NULL && resource->getType() == type) { - if(result.x < 0 || unit->getPos().dist(newPos) < unit->getPos().dist(result)) { - if(unit->isBadHarvestPos(newPos) == false) { - result = newPos; - foundCloseResource = true; - break; + + // 0 means start looking leftbottom to top right + int tryRadius = random.randRange(0,1); + if(tryRadius == 0) { + for(int j = -harvestDistance; j <= harvestDistance && foundCloseResource == false; ++j) { + for(int k = -harvestDistance; k <= harvestDistance && foundCloseResource == false; ++k) { + Vec2i newPos = pos + Vec2i(j,k); + if(map->isInside(newPos) == true && isResourceTargetInCache(newPos) == false) { + const SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(newPos)); + if( sc != NULL && sc->getResource() != NULL) { + const Resource *resource = sc->getResource(); + if(resource->getType() != NULL && resource->getType() == type) { + if(result.x < 0 || unit->getPos().dist(newPos) < unit->getPos().dist(result)) { + if(unit->isBadHarvestPos(newPos) == false) { + result = newPos; + foundCloseResource = true; + break; + } } } } + else { + deleteList.push_back(newPos); + } } - else { - deleteList.push_back(newPos); + } + } + } + // start looking topright to leftbottom + else { + for(int j = harvestDistance; j >= -harvestDistance && foundCloseResource == false; --j) { + for(int k = harvestDistance; k >= -harvestDistance && foundCloseResource == false; --k) { + Vec2i newPos = pos + Vec2i(j,k); + if(map->isInside(newPos) == true && isResourceTargetInCache(newPos) == false) { + const SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(newPos)); + if( sc != NULL && sc->getResource() != NULL) { + const Resource *resource = sc->getResource(); + if(resource->getType() != NULL && resource->getType() == type) { + if(result.x < 0 || unit->getPos().dist(newPos) < unit->getPos().dist(result)) { + if(unit->isBadHarvestPos(newPos) == false) { + result = newPos; + foundCloseResource = true; + break; + } + } + } + } + else { + deleteList.push_back(newPos); + } } } } @@ -1012,25 +1045,54 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(const Vec2i &pos, const Res const Map *map = world->getMap(); bool foundCloseResource = false; - // First look immediately around the given position - for(int j = -harvestDistance; j <= harvestDistance && foundCloseResource == false; ++j) { - for(int k = -harvestDistance; k <= harvestDistance && foundCloseResource == false; ++k) { - Vec2i newPos = pos + Vec2i(j,k); - if(map->isInside(newPos) == true && isResourceTargetInCache(newPos) == false) { - const SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(newPos)); - if( sc != NULL && sc->getResource() != NULL) { - const Resource *resource = sc->getResource(); - if(resource->getType() != NULL && resource->getType() == type) { - if(result.x < 0 || pos.dist(newPos) < pos.dist(result)) { - result = newPos; - foundCloseResource = true; - break; + + // 0 means start looking leftbottom to top right + int tryRadius = random.randRange(0,1); + if(tryRadius == 0) { + // First look immediately around the given position + for(int j = -harvestDistance; j <= harvestDistance && foundCloseResource == false; ++j) { + for(int k = -harvestDistance; k <= harvestDistance && foundCloseResource == false; ++k) { + Vec2i newPos = pos + Vec2i(j,k); + if(map->isInside(newPos) == true && isResourceTargetInCache(newPos) == false) { + const SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(newPos)); + if( sc != NULL && sc->getResource() != NULL) { + const Resource *resource = sc->getResource(); + if(resource->getType() != NULL && resource->getType() == type) { + if(result.x < 0 || pos.dist(newPos) < pos.dist(result)) { + result = newPos; + foundCloseResource = true; + break; + } } } + //else { + // deleteList.push_back(newPos); + //} + } + } + } + } + else { + // First look immediately around the given position + for(int j = harvestDistance; j >= -harvestDistance && foundCloseResource == false; --j) { + for(int k = harvestDistance; k >= -harvestDistance && foundCloseResource == false; --k) { + Vec2i newPos = pos + Vec2i(j,k); + if(map->isInside(newPos) == true && isResourceTargetInCache(newPos) == false) { + const SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(newPos)); + if( sc != NULL && sc->getResource() != NULL) { + const Resource *resource = sc->getResource(); + if(resource->getType() != NULL && resource->getType() == type) { + if(result.x < 0 || pos.dist(newPos) < pos.dist(result)) { + result = newPos; + foundCloseResource = true; + break; + } + } + } + //else { + // deleteList.push_back(newPos); + //} } - //else { - // deleteList.push_back(newPos); - //} } } } diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index 14c657283..17de37e51 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -120,6 +120,7 @@ private: // update of the faction //std::map > > localCacheForUnitCellMovement; + RandomGen random; FactionThread *workerThread; public: