2019-06-06 17:07:24 +02:00
|
|
|
//
|
|
|
|
// Created by trotfunky on 06/06/19.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Level.h"
|
2019-06-07 02:59:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
Level::Level(const pugi::xml_document& xmlDoc, const TextureStore& textureStore)
|
2019-06-09 14:59:17 +02:00
|
|
|
: size(xmlDoc.child("Level").attribute("w").as_int(),xmlDoc.child("Level").attribute("h").as_int()),
|
|
|
|
textures(textureStore)
|
2019-06-07 02:59:27 +02:00
|
|
|
{
|
|
|
|
pugi::xml_node levelNode = xmlDoc.child("Level");
|
|
|
|
for(const pugi::xml_node& child : levelNode.children())
|
|
|
|
{
|
|
|
|
if(!strncmp(child.name(),"Entity",6))
|
|
|
|
{
|
2019-06-07 22:20:34 +02:00
|
|
|
entities.emplace_back(child,textures.at(child.attribute("textureId").as_int(0)).get());
|
2019-06-08 02:32:06 +02:00
|
|
|
|
|
|
|
// Initialize the occupied squares vector with the new entity's squares
|
2019-06-09 05:18:28 +02:00
|
|
|
std::vector<pro_maat::GridPos> entitySquares = entities.rbegin()->getOccupiedSquares();
|
2019-06-08 02:32:06 +02:00
|
|
|
std::move(entitySquares.begin(),entitySquares.end(),std::back_inserter(occupiedSquares));
|
2019-06-07 02:59:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Level::render(sf::RenderWindow& renderWindow) const
|
|
|
|
{
|
2019-06-07 22:20:34 +02:00
|
|
|
for(const Entity& entity : entities)
|
|
|
|
{
|
|
|
|
renderWindow.draw(entity.getShape());
|
|
|
|
}
|
2019-06-07 02:59:27 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 22:20:34 +02:00
|
|
|
void Level::runStep()
|
2019-06-07 02:59:27 +02:00
|
|
|
{
|
2019-06-08 16:11:29 +02:00
|
|
|
std::vector<pro_maat::GridPos> newOccupiedSquares{};
|
2019-06-08 02:32:06 +02:00
|
|
|
newOccupiedSquares.reserve(occupiedSquares.size());
|
|
|
|
|
2019-06-07 22:20:34 +02:00
|
|
|
for(Entity& entity: entities)
|
|
|
|
{
|
2019-06-08 02:32:06 +02:00
|
|
|
entity.update();
|
|
|
|
int heuristicSign = 0;
|
|
|
|
switch (entity.getState())
|
|
|
|
{
|
|
|
|
case State::Moving:
|
|
|
|
{
|
|
|
|
heuristicSign = 1;
|
2019-06-09 14:59:17 +02:00
|
|
|
[[fallthrough]];
|
2019-06-08 02:32:06 +02:00
|
|
|
}
|
|
|
|
case State::Fleeing:
|
|
|
|
{
|
|
|
|
if(heuristicSign == 0)
|
|
|
|
{
|
|
|
|
heuristicSign = -1;
|
|
|
|
}
|
|
|
|
if(entity.target != entity.getPosition())
|
|
|
|
{
|
|
|
|
entity.move(findPath(entity.getPosition(),entity.target,heuristicSign));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case State::Waiting:break;
|
|
|
|
case State::Idle:break;
|
|
|
|
}
|
|
|
|
|
2019-06-09 05:18:28 +02:00
|
|
|
std::vector<pro_maat::GridPos> entitySquares = entity.getOccupiedSquares();
|
|
|
|
|
|
|
|
// FIXME : Very heavy memory usage and a lot of duplicates, slows down occupiedSquares.find() calls too.
|
|
|
|
// Copy the squares to the currently occupied squares : prevents moving into an entity that just moved
|
|
|
|
occupiedSquares.insert(occupiedSquares.end(),entitySquares.begin(),entitySquares.end());
|
|
|
|
std::sort(occupiedSquares.begin(),occupiedSquares.end());
|
|
|
|
|
2019-06-08 02:32:06 +02:00
|
|
|
// Moves the occupied squares from the entity to the new occupied squares vector
|
|
|
|
std::move(entitySquares.begin(),entitySquares.end(),std::back_inserter(newOccupiedSquares));
|
2019-06-07 22:20:34 +02:00
|
|
|
}
|
2019-06-08 02:32:06 +02:00
|
|
|
|
|
|
|
occupiedSquares.swap(newOccupiedSquares);
|
|
|
|
// Sort the vector as to get O(ln(n)) complexity when searching for a square
|
|
|
|
std::sort(occupiedSquares.begin(),occupiedSquares.end());
|
|
|
|
}
|
|
|
|
|
2019-06-09 05:18:28 +02:00
|
|
|
Orientation Level::findPath(pro_maat::GridPos start, pro_maat::GridPos goal, int sign)
|
2019-06-08 02:32:06 +02:00
|
|
|
{
|
2019-06-09 05:18:28 +02:00
|
|
|
std::set<pro_maat::GridPos> openNodes{start};
|
|
|
|
std::set<pro_maat::GridPos> closedNodes{};
|
|
|
|
|
|
|
|
std::map<pro_maat::GridPos,float> estimatedCosts{{start,pro_maat::manhattanDistance(start,goal)*sign}};
|
|
|
|
std::map<pro_maat::GridPos,float> pathCosts{{start,0}};
|
|
|
|
std::map<pro_maat::GridPos,pro_maat::GridPos> paths{};
|
|
|
|
|
|
|
|
|
|
|
|
const std::vector<pro_maat::GridPos> goalNeighbours = pro_maat::getNeighbours(goal,size);
|
|
|
|
// Save the iterators : vector is const and .begin() and .end() might get called a lot
|
|
|
|
auto goalNeighboursBeginIterator = goalNeighbours.begin();
|
|
|
|
auto goalNeighboursEndIterator = goalNeighbours.end();
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME : Find an efficient way to get rid of openNodes.find calls
|
|
|
|
// Lambda checking if the current element is also in the open nodes set
|
|
|
|
auto compWithOpen = [&openNodes](const std::pair<pro_maat::GridPos,float>& leftHandSide,
|
|
|
|
const std::pair<pro_maat::GridPos,float>& rightHandSide){
|
|
|
|
if(openNodes.find(leftHandSide.first) == openNodes.end())
|
|
|
|
{
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
else if(openNodes.find(rightHandSide.first) == openNodes.end())
|
|
|
|
{
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (leftHandSide.second < rightHandSide.second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
while(!openNodes.empty())
|
|
|
|
{
|
|
|
|
// Expand from the open node with the smallest estimated cost
|
|
|
|
pro_maat::GridPos currentNode = std::min_element(estimatedCosts.begin(),estimatedCosts.end(),compWithOpen)->first;
|
|
|
|
|
|
|
|
if(std::find(goalNeighboursBeginIterator,goalNeighboursEndIterator,currentNode) != goalNeighboursEndIterator)
|
|
|
|
{
|
2019-06-09 05:37:08 +02:00
|
|
|
if(currentNode == start)
|
|
|
|
{
|
|
|
|
return(Orientation::None);
|
|
|
|
}
|
|
|
|
|
2019-06-09 05:18:28 +02:00
|
|
|
// Trace back to the start
|
2019-06-09 05:37:08 +02:00
|
|
|
pro_maat::GridPos& previousNode = currentNode;
|
2019-06-09 05:18:28 +02:00
|
|
|
for(;paths[previousNode]!=start;previousNode = paths[previousNode])
|
|
|
|
{}
|
|
|
|
|
|
|
|
pro_maat::GridUnit dx = previousNode.first - start.first;
|
|
|
|
pro_maat::GridUnit dy = previousNode.second - start.second;
|
|
|
|
|
|
|
|
if(dx < 0)
|
|
|
|
{
|
|
|
|
return(Orientation::West);
|
|
|
|
}
|
|
|
|
else if(dx > 0)
|
|
|
|
{
|
|
|
|
return(Orientation::East);
|
|
|
|
}
|
|
|
|
else if(dy < 0)
|
|
|
|
{
|
|
|
|
return(Orientation::North);
|
|
|
|
}
|
|
|
|
else if(dy > 0 )
|
|
|
|
{
|
|
|
|
return(Orientation::South);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return(Orientation::None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openNodes.erase(currentNode);
|
|
|
|
closedNodes.insert(currentNode);
|
|
|
|
|
|
|
|
for(pro_maat::GridPos neighbour : pro_maat::getNeighbours(currentNode,size))
|
|
|
|
{
|
|
|
|
// Checks if node has been closed or is an obstacle
|
|
|
|
if(std::find(occupiedSquares.begin(),occupiedSquares.end(),neighbour) != occupiedSquares.end() ||
|
|
|
|
closedNodes.find(neighbour) != closedNodes.end())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// As the neighbours are adjacent squares, distance from them is always +-1
|
|
|
|
float newPathCost = pathCosts[currentNode] + sign;
|
|
|
|
|
|
|
|
if(openNodes.find(neighbour) == openNodes.end())
|
|
|
|
{
|
|
|
|
openNodes.insert(neighbour);
|
|
|
|
}
|
|
|
|
else if(newPathCost >= pathCosts[neighbour]) // If the node is open but this path is longer, ignore it
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pathCosts.insert_or_assign(neighbour,newPathCost);
|
|
|
|
estimatedCosts.insert_or_assign(neighbour,newPathCost + pro_maat::manhattanDistance(neighbour,goal));
|
2019-06-09 14:59:17 +02:00
|
|
|
paths.insert_or_assign(neighbour,currentNode);
|
2019-06-09 05:18:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we did not find a path, do not move
|
|
|
|
return Orientation::None;
|
2019-06-07 02:59:27 +02:00
|
|
|
}
|