From c99fb68f5b61251fdfa5b39b406137083a58a8b9 Mon Sep 17 00:00:00 2001 From: Titus Tscharntke Date: Tue, 29 Mar 2011 21:44:36 +0000 Subject: [PATCH] selecting mining positions (and by this mining itself ) should be much better now --- source/glest_game/gui/gui.cpp | 103 +++++++++++++++++++++++++++++--- source/glest_game/world/map.cpp | 21 +++---- 2 files changed, 106 insertions(+), 18 deletions(-) diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index 3ea100bf..a1ee31bd 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -925,31 +925,118 @@ void Gui::computeSelected(bool doubleClick, bool force){ } } -bool Gui::computeTarget(const Vec2i &screenPos, Vec2i &targetPos, const Unit *&targetUnit) { +bool Gui::computeTarget(const Vec2i &screenPos, Vec2i &targetPos, const Unit *&targetUnit){ Selection::UnitContainer uc; Renderer &renderer= Renderer::getInstance(); - const Object* obj=NULL; + const Object* obj= NULL; renderer.computeSelected(uc, obj, true, screenPos, screenPos); validPosObjWorld= false; - if(uc.empty() == false) { - targetUnit=getRelevantObjectFromSelection(&uc); + if(uc.empty() == false){ + targetUnit= getRelevantObjectFromSelection(&uc); targetPos= targetUnit->getPos(); return true; } - else if(obj!=NULL) { + else if(obj != NULL){ targetUnit= NULL; + + // get real click pos + renderer.computePosition(screenPos, targetPos); + + printf("c.x=%d c.y=%d o.x=%d o.y=%d\n", targetPos.x, targetPos.y, obj->getMapPos().x, obj->getMapPos().y); + validPosObjWorld= true; + //posObjWorld = targetPos; + + int tx= targetPos.x; + int ty= targetPos.y; + + int ox= obj->getMapPos().x; + int oy= obj->getMapPos().y; + + Resource* clickedRecource= world->getMap()->getSurfaceCell(Map::toSurfCoords(obj->getMapPos()))->getResource(); + + // lets see if the click had the same Resource + if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(targetPos))->getResource()){ + // same ressource is meant, so use the user selected position + return true; + } + else{// calculate a valid resource position which is as near as possible to the selected position + Vec2i testIt= Vec2i(obj->getMapPos()); + /////////////// + // test both // + /////////////// + if(ty < oy){ + testIt.y--; + } + else if(ty > oy){ + testIt.y++; + } + if(tx < ox){ + testIt.x--; + } + else if(tx > ox){ + testIt.x++; + } + + if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){ + // same ressource is meant, so use this position + targetPos= testIt; + //posObjWorld= targetPos; + return true; + } + else{ + testIt= Vec2i(obj->getMapPos()); + } + + ///////////////// + // test y-only // + ///////////////// + if(ty < oy){ + testIt.y--; + } + else if(ty > oy){ + testIt.y++; + } + if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){ + // same ressource is meant, so use this position + targetPos= testIt; + //posObjWorld= targetPos; + return true; + } + else{ + testIt= Vec2i(obj->getMapPos()); + } + + ///////////////// + // test x-only // + ///////////////// + if(tx < ox){ + testIt.x--; + } + else if(tx > ox){ + testIt.x++; + } + if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){ + // same ressource is meant, so use this position + targetPos= testIt; + //posObjWorld= targetPos; + return true; + } + } + // give up and use the object position; targetPos= obj->getMapPos(); + posObjWorld= targetPos; return true; + } - else { + else{ targetUnit= NULL; - if(renderer.computePosition(screenPos, targetPos)) { + if(renderer.computePosition(screenPos, targetPos)){ validPosObjWorld= true; posObjWorld= targetPos; return true; } - else { + else{ return false; } } diff --git a/source/glest_game/world/map.cpp b/source/glest_game/world/map.cpp index 456eb44e..58602e8c 100644 --- a/source/glest_game/world/map.cpp +++ b/source/glest_game/world/map.cpp @@ -849,17 +849,18 @@ Vec2i Map::computeRefPos(const Selection *selection) const { Vec2i Map::computeDestPos( const Vec2i &refUnitPos, const Vec2i &unitPos, const Vec2i &commandPos) const { Vec2i pos; - Vec2i posDiff = unitPos - refUnitPos; +// no more random needed +// Vec2i posDiff = unitPos - refUnitPos; +// +// if(abs(posDiff.x) >= 3){ +// posDiff.x = posDiff.x % 3; +// } +// +// if(abs(posDiff.y) >= 3){ +// posDiff.y = posDiff.y % 3; +// } - if(abs(posDiff.x) >= 3){ - posDiff.x = posDiff.x % 3; - } - - if(abs(posDiff.y) >= 3){ - posDiff.y = posDiff.y % 3; - } - - pos = commandPos + posDiff; + pos = commandPos; //+ posDiff; clampPos(pos); return pos; }