Fixed Rule::findTarget() not finding good or even valid targets

This commit is contained in:
trotFunky 2019-06-10 16:04:12 +02:00
parent c94fc5a0be
commit 293a564a29
3 changed files with 22 additions and 12 deletions

View file

@ -66,19 +66,26 @@ pro_maat::GridPos Rule::findTarget()
// Get the smallest, non-occupied, inside the map square // Get the smallest, non-occupied, inside the map square
auto bestTarget = [this](const pro_maat::GridPos& leftHandSide, const pro_maat::GridPos& rightHandSide){ 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 the left hand side operand is not in the map or occupied, it is not valid
if(!pro_maat::isInMap(leftHandSide,mapSize) || if(!pro_maat::isInMap(leftHandSide,mapSize))
std::find(occupiedSquares.begin(),occupiedSquares.end(),leftHandSide) != occupiedSquares.end())
{ {
return(false); return(false);
} }
else if(!pro_maat::isInMap(rightHandSide,mapSize) || else if(std::find(occupiedSquares.begin(),occupiedSquares.end(),leftHandSide) != occupiedSquares.end())
std::find(occupiedSquares.begin(),occupiedSquares.end(),rightHandSide) != 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); return(true);
} }
else 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); 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(); pro_maat::GridPos topLeftCorner = processingEntity->getPosition();
topLeftCorner.first -= entityWidth*0.5 - 1; topLeftCorner.first -= std::floor(entityWidth*0.5) + 1;
topLeftCorner.second -= entityHeight*0.5 - 1; topLeftCorner.second -= std::floor(entityHeight*0.5) + 1;
// Get all the top and bottom adjacent squares // Get all the top and bottom adjacent squares
for(int i = 0;i<entityWidth+2;i++) for(int i = 0;i<entityWidth+2;i++)
{ {
potentialTargets.emplace_back(topLeftCorner.first+i,topLeftCorner.second); potentialTargets.emplace_back(topLeftCorner.first+i,topLeftCorner.second);
potentialTargets.emplace_back(topLeftCorner.first+i,topLeftCorner.second+entityHeight+2); potentialTargets.emplace_back(topLeftCorner.first+i,topLeftCorner.second+entityHeight+1);
} }
// Get the missing adjacent squares from the sides // Get the missing adjacent squares from the sides
for(int i = 1;i<=entityHeight;i++) for(int i = 1;i<=entityHeight;i++)
{ {
potentialTargets.emplace_back(topLeftCorner.first,topLeftCorner.second+i); potentialTargets.emplace_back(topLeftCorner.first,topLeftCorner.second+i);
potentialTargets.emplace_back(topLeftCorner.first+entityWidth+2,topLeftCorner.second+i); potentialTargets.emplace_back(topLeftCorner.first+entityWidth+1,topLeftCorner.second+i);
} }
@ -121,6 +128,7 @@ pro_maat::GridPos Rule::findTarget()
if(target != potentialTargets.end()) if(target != potentialTargets.end())
{ {
std::cout << "Target : (" << (*target).first << ","<< (*target).second << ")" << std::endl;
return (*target); return (*target);
} }
} }

View file

@ -7,6 +7,8 @@
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <math.h>
#include <iostream>
#include "Entity.h" #include "Entity.h"

View file

@ -39,8 +39,8 @@ namespace pro_maat
bool isInMap(const GridPos& square, const GridPos& gridSize) bool isInMap(const GridPos& square, const GridPos& gridSize)
{ {
return (square.first < 0 || square.second < 0 || return !(square.first < 0 || square.second < 0 ||
square.first >= gridSize.first || square.second >= gridSize.second); square.first > gridSize.first || square.second > gridSize.second);
} }
float manhattanDistance(const GridPos& leftHandSide, const GridPos& rightHandSide) float manhattanDistance(const GridPos& leftHandSide, const GridPos& rightHandSide)