From f7481f2bc9898155c9a149feb5c16ae38a99940a Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Sat, 8 Jun 2019 16:11:29 +0200 Subject: [PATCH] Added type alias for std::pair for grid coordinates as sf::Vector2 cannot be used in std::map or std::set --- src/Entity.cpp | 8 ++++---- src/Entity.h | 8 ++++---- src/Level.cpp | 4 ++-- src/Level.h | 6 ++++-- src/Utils.h | 10 ++++++++++ 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Entity.cpp b/src/Entity.cpp index 921df1f..df05975 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -71,17 +71,17 @@ const State Entity::getState() const return currentState; } -const sf::Vector2i Entity::getPosition() const +const pro_maat::GridPos Entity::getPosition() const { // Safe : size is a multiple of pro_maat::pixelsPerUnit uint8_t x = (shape.getPosition().x-0.5*shape.getSize().x)/pro_maat::pixelsPerUnit; uint8_t y = (shape.getPosition().y-0.5*shape.getSize().y)/pro_maat::pixelsPerUnit; - return sf::Vector2i(x,y); + return pro_maat::GridPos(x,y); } -const std::vector> Entity::getOccupiedSquares() const +const std::vector Entity::getOccupiedSquares() const { - std::vector> occupiedSquares; + std::vector occupiedSquares; // Safe : size is a multiple of pro_maat::pixelsPerUnit uint8_t w = shape.getSize().x/pro_maat::pixelsPerUnit; uint8_t h = shape.getSize().y/pro_maat::pixelsPerUnit; diff --git a/src/Entity.h b/src/Entity.h index f729123..1d0d49c 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -49,13 +49,13 @@ public: const sf::RectangleShape& getShape() const; const State getState() const; - const sf::Vector2i getPosition() const; + const pro_maat::GridPos getPosition() const; // Don't like it : iterates over every square every tick - const std::vector> getOccupiedSquares() const; + const std::vector getOccupiedSquares() const; /// Position of the target of the current action on the map - sf::Vector2i target; + pro_maat::GridPos target; private: static const std::map entityTypeLookup; @@ -69,7 +69,7 @@ private: State nextState; /// Used with rules : last to update has priority - sf::Vector2i nextTarget; + pro_maat::GridPos nextTarget; }; diff --git a/src/Level.cpp b/src/Level.cpp index e5c4e22..470c84e 100644 --- a/src/Level.cpp +++ b/src/Level.cpp @@ -33,7 +33,7 @@ void Level::render(sf::RenderWindow& renderWindow) const void Level::runStep() { - std::vector> newOccupiedSquares{}; + std::vector newOccupiedSquares{}; newOccupiedSquares.reserve(occupiedSquares.size()); for(Entity& entity: entities) @@ -72,7 +72,7 @@ void Level::runStep() std::sort(occupiedSquares.begin(),occupiedSquares.end()); } -Orientation Level::findPath(sf::Vector2i start, sf::Vector2i end, int sign) +Orientation Level::findPath(pro_maat::GridPos start, pro_maat::GridPos end, int sign) { // TODO : A* which returns the next move return Orientation::East; diff --git a/src/Level.h b/src/Level.h index 681a6a6..a975c84 100644 --- a/src/Level.h +++ b/src/Level.h @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "Utils.h" #include "Entity.h" @@ -33,9 +35,9 @@ private: // Pathfinding // - std::vector> occupiedSquares; + std::vector occupiedSquares; - Orientation findPath(sf::Vector2i start, sf::Vector2i end, int sign); + Orientation findPath(pro_maat::GridPos start, pro_maat::GridPos end, int sign); }; diff --git a/src/Utils.h b/src/Utils.h index 4632cb0..431ed1a 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -12,6 +12,8 @@ namespace pro_maat { +// Used with positions on the map grid +using GridPos = std::pair; static constexpr uint8_t pixelsPerUnit = 50; static constexpr char levelFolder[] = "resources/"; @@ -20,6 +22,14 @@ static constexpr char fontFolder[] = "resources/"; void errorWindow(const std::string& error); +// Good heuristic on 4-way grids +double manhattanDistance(pro_maat::GridPos leftHandSide, pro_maat::GridPos rightHandSide) +{ + // The *0.01 helps with breaking ties and minimizing exploration + // As per http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html#breaking-ties + return (std::abs(rightHandSide.first-leftHandSide.first)+std::abs(rightHandSide.second-leftHandSide.second))*1.01; +} + } #endif //PROJECT_MAAT_UTILS_H