From 4bc00cbf73f3cd15243333bc34377bb3302d0165 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 28 Aug 2010 03:43:14 +0000 Subject: [PATCH] - added new calculation method when building units, builder units use best approach to build it instead of top left. --- source/glest_game/world/map.cpp | 22 +++++++++++++++++ source/glest_game/world/map.h | 1 + source/glest_game/world/unit_updater.cpp | 31 +++++++++++++++++++++++- source/glest_game/world/unit_updater.h | 1 + 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/source/glest_game/world/map.cpp b/source/glest_game/world/map.cpp index b0009157..77225ede 100644 --- a/source/glest_game/world/map.cpp +++ b/source/glest_game/world/map.cpp @@ -439,6 +439,28 @@ bool Map::aproxCanMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2) c } } +//put a units into the cells +bool Map::isInUnitTypeCells(const UnitType *ut, const Vec2i &pos,const Vec2i &testPos) { + + assert(ut!=NULL); + + Cell *testCell = getCell(testPos); + for(int i=0; i < ut->getSize(); ++i){ + for(int j = 0; j < ut->getSize(); ++j) { + Vec2i currPos = pos + Vec2i(i, j); + if(isInside(currPos) == true) { + //if(ut->hasCellMap() == false || ut->getCellMapCell(i, j, facing)) { + Cell *unitCell = getCell(currPos); + + if(unitCell == testCell) { + return true; + } + } + } + } + return false; +} + //put a units into the cells void Map::putUnitCells(Unit *unit, const Vec2i &pos){ diff --git a/source/glest_game/world/map.h b/source/glest_game/world/map.h index 9b9825ac..914497c6 100755 --- a/source/glest_game/world/map.h +++ b/source/glest_game/world/map.h @@ -214,6 +214,7 @@ public: bool canMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2) const; void putUnitCells(Unit *unit, const Vec2i &pos); void clearUnitCells(Unit *unit, const Vec2i &pos); + bool isInUnitTypeCells(const UnitType *ut, const Vec2i &pos,const Vec2i &testPos); //misc bool isNextTo(const Vec2i &pos, const Unit *unit) const; diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 38406afc..6d4ed92a 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -385,6 +385,30 @@ void UnitUpdater::updateAttackStopped(Unit *unit){ // ==================== updateBuild ==================== +Vec2i UnitUpdater::findBestBuildApproach(Vec2i unitBuilderPos, Vec2i originalBuildPos, const UnitType *ut) { + Vec2i pos = originalBuildPos; + + float bestRange = -1; + + Vec2i start = pos - Vec2i(1); + Vec2i end = pos + Vec2i(ut->getSize()); + + for(int i = start.x; i <= end.x; ++i) { + for(int j = start.y; j <= end.y; ++j){ + Vec2i testPos(i,j); + if(map->isInUnitTypeCells(ut, originalBuildPos,testPos) == false) { + float distance = unitBuilderPos.dist(testPos); + if(bestRange < 0 || bestRange > distance) { + bestRange = distance; + pos = testPos; + } + } + } + } + + return pos; +} + void UnitUpdater::updateBuild(Unit *unit){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -403,7 +427,12 @@ void UnitUpdater::updateBuild(Unit *unit){ TravelState tsValue = tsImpossible; switch(this->game->getGameSettings()->getPathFinderType()) { case pfBasic: - tsValue = pathFinder->findPath(unit, command->getPos()-Vec2i(1)); + { + //Vec2i buildPos = (command->getPos()-Vec2i(1)); + Vec2i buildPos = findBestBuildApproach(unit->getPos(), command->getPos(), ut); + //Vec2i buildPos = (command->getPos() + Vec2i(ut->getSize() / 2)); + tsValue = pathFinder->findPath(unit, buildPos); + } break; case pfRoutePlanner: tsValue = routePlanner->findPathToBuildSite(unit, ut, command->getPos(), command->getFacing()); diff --git a/source/glest_game/world/unit_updater.h b/source/glest_game/world/unit_updater.h index 598c0e03..38bf292b 100644 --- a/source/glest_game/world/unit_updater.h +++ b/source/glest_game/world/unit_updater.h @@ -118,6 +118,7 @@ private: Unit * findPeerUnitBuilder(Unit *unit); void SwapActiveCommand(Unit *unitSrc, Unit *unitDest); + Vec2i findBestBuildApproach(Vec2i unitBuilderPos, Vec2i originalBuildPos, const UnitType *ut); }; // =====================================================