diff --git a/src/Rule.cpp b/src/Rule.cpp index 50b5c1b..b875d8f 100644 --- a/src/Rule.cpp +++ b/src/Rule.cpp @@ -66,19 +66,26 @@ pro_maat::GridPos Rule::findTarget() // Get the smallest, non-occupied, inside the map square auto bestTarget = [this](const pro_maat::GridPos& leftHandSide, const pro_maat::GridPos& rightHandSide){ // If the left hand side operand is not in the map or occupied, it is not valid - if(!pro_maat::isInMap(leftHandSide,mapSize) || - std::find(occupiedSquares.begin(),occupiedSquares.end(),leftHandSide) != occupiedSquares.end()) + if(!pro_maat::isInMap(leftHandSide,mapSize)) { return(false); } - else if(!pro_maat::isInMap(rightHandSide,mapSize) || - std::find(occupiedSquares.begin(),occupiedSquares.end(),rightHandSide) != occupiedSquares.end()) + else if(std::find(occupiedSquares.begin(),occupiedSquares.end(),leftHandSide) != occupiedSquares.end()) + { + return(false); + } + else if(!pro_maat::isInMap(rightHandSide,mapSize)) + { + return(true); + } + else if(std::find(occupiedSquares.begin(),occupiedSquares.end(),rightHandSide) != occupiedSquares.end()) { return(true); } else { - return(leftHandSide < rightHandSide); + return(pro_maat::manhattanDistance(entity->getPosition(),leftHandSide) < + pro_maat::manhattanDistance(entity->getPosition(),rightHandSide)); }}; @@ -96,23 +103,23 @@ pro_maat::GridPos Rule::findTarget() potentialTargets.reserve((entityWidth+2)*2+entityHeight*2); - // Computes the top left corner of the entity + // Computes the top left corner just outside of the entity pro_maat::GridPos topLeftCorner = processingEntity->getPosition(); - topLeftCorner.first -= entityWidth*0.5 - 1; - topLeftCorner.second -= entityHeight*0.5 - 1; + topLeftCorner.first -= std::floor(entityWidth*0.5) + 1; + topLeftCorner.second -= std::floor(entityHeight*0.5) + 1; // Get all the top and bottom adjacent squares for(int i = 0;i #include +#include +#include #include "Entity.h" diff --git a/src/Utils.cpp b/src/Utils.cpp index 23d78d4..320d753 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -39,8 +39,8 @@ namespace pro_maat bool isInMap(const GridPos& square, const GridPos& gridSize) { - return (square.first < 0 || square.second < 0 || - square.first >= gridSize.first || square.second >= gridSize.second); + return !(square.first < 0 || square.second < 0 || + square.first > gridSize.first || square.second > gridSize.second); } float manhattanDistance(const GridPos& leftHandSide, const GridPos& rightHandSide)