Added automatic area generation so path finding works on random maps.

This commit is contained in:
Felix Ableitner 2013-04-27 14:04:23 +02:00
parent adfb49b493
commit ca6943f37d
6 changed files with 179 additions and 77 deletions

View file

@ -12,8 +12,6 @@
#include "sprites/Player.h" #include "sprites/Player.h"
#include "util/Yaml.h" #include "util/Yaml.h"
#include "util/Log.h"
const int Game::FPS_GOAL = 60; const int Game::FPS_GOAL = 60;
/** /**
@ -29,7 +27,8 @@ Game::Game(sf::RenderWindow& window) :
mWindow.setKeyRepeatEnabled(true); mWindow.setKeyRepeatEnabled(true);
Generator generator; Generator generator;
generator.generateTiles(mTileManager, sf::IntRect(-16, -16, 32, 32)); generator.generateTiles(mTileManager, mWorld,
sf::IntRect(-16, -16, 32, 32));
mPlayer = std::shared_ptr<Player>(new Player(mWorld, mTileManager, mPlayer = std::shared_ptr<Player>(new Player(mWorld, mTileManager,
sf::Vector2f(0.0f, 0.0f), Yaml("player.yaml"))); sf::Vector2f(0.0f, 0.0f), Yaml("player.yaml")));
mWorld.insertCharacter(mPlayer); mWorld.insertCharacter(mPlayer);
@ -161,8 +160,8 @@ Game::mouseDown(const sf::Event& event) {
mPlayer->pullTrigger(); mPlayer->pullTrigger();
break; break;
case sf::Mouse::Right: case sf::Mouse::Right:
mPlayer->setDestination(convertCoordinates(event.mouseMove.x, mPlayer->setDestination(convertCoordinates(event.mouseButton.x,
event.mouseMove.y)); event.mouseButton.y));
default: default:
break; break;
} }

View file

@ -7,12 +7,14 @@
#include "World.h" #include "World.h"
#include <algorithm>
#include <unordered_set> #include <unordered_set>
#include <unordered_map> #include <unordered_map>
#include <Thor/Vectors.hpp> #include <Thor/Vectors.hpp>
#include "util/Interval.h" #include "util/Interval.h"
#include "sprites/TileManager.h"
const float World::WALL_DISTANCE_MULTIPLIER = 1.5f; const float World::WALL_DISTANCE_MULTIPLIER = 1.5f;
/** /**
@ -69,66 +71,19 @@ World::removeCharacter(std::shared_ptr<Character> character) {
remove(character); remove(character);
} }
/**
* Generate path finding base data.
*
* Hardcoded as heuristic may be unnecessary with proper map generation.
*
* @warning Must not be run while getPath() is running (raw pointers).
*/
void
World::generateAreas() {
Area a;
a.area = sf::FloatRect(50, 50, 900, 300);
a.center = sf::Vector2f(500, 200);
mAreas.push_back(a);
a.area = sf::FloatRect(450, 350, 450, 100);
a.center = sf::Vector2f(675, 400);
mAreas.push_back(a);
a.area = sf::FloatRect(50, 450, 900, 500);
a.center = sf::Vector2f(500, 700);
mAreas.push_back(a);
Portal p1;
Portal p2;
std::vector<Portal> vp;
p1.start = sf::Vector2f(450, 350);
p1.end = sf::Vector2f(950, 350);
p1.area = &mAreas[1];
vp.push_back(p1);
mAreas[0].portals = vp;
vp.clear();
p2.start = sf::Vector2f(450, 450);
p2.end = sf::Vector2f(950, 450);
p2.area = &mAreas[1];
vp.push_back(p2);
mAreas[2].portals = vp;
vp.clear();
p1.area = &mAreas[0];
vp.push_back(p1);
p2.area = &mAreas[2];
vp.push_back(p2);
mAreas[1].portals = vp;
}
/** /**
* Runs the A* path finding algorithm with areas as nodes and portals as edges. * Runs the A* path finding algorithm with areas as nodes and portals as edges.
* *
* @warning Areas and portals must not be changed while this is running. * @warning Areas and portals must not be changed while this is running.
* *
* @param start The area to start the path finding from. Must not be null. * @param start The area to start the path finding from. Must not be null.
* @param end The goal to reach. May be null. * @param end The goal to reach. Must not be null.
* @return Path in reverse order (start being the last item and end the first). * @return Path in reverse order (start being the last item and end the first).
*/ */
std::vector<World::Portal*> std::vector<World::Portal*>
World::astarArea(Area* start, Area* end) const { World::astarArea(Area* start, Area* end) const {
assert(start); assert(start);
if (!end) assert(end);
return std::vector<World::Portal*>();
std::unordered_set<Area*> closed; std::unordered_set<Area*> closed;
std::unordered_map<Area*, float> openAreasEstimatedCost; std::unordered_map<Area*, float> openAreasEstimatedCost;
@ -190,30 +145,32 @@ World::astarArea(Area* start, Area* end) const {
std::vector<sf::Vector2f> std::vector<sf::Vector2f>
World::getPath(const sf::Vector2f& start, const sf::Vector2f& end, World::getPath(const sf::Vector2f& start, const sf::Vector2f& end,
float radius) const { float radius) const {
if (!getArea(end))
return std::vector<sf::Vector2f>();
std::vector<Portal*> portals = astarArea(getArea(start), getArea(end)); std::vector<Portal*> portals = astarArea(getArea(start), getArea(end));
std::vector<sf::Vector2f> path; std::vector<sf::Vector2f> path;
path.push_back(end); path.push_back(end);
for (auto p : portals) { for (auto p : portals) {
// Find the point on the line of the portal closest to the previous point. // Find the point on the line of the portal closest to the previous point.
sf::Vector2f startToEnd = p->end - p->start; sf::Vector2f startToEnd = sf::Vector2f(p->end - p->start);
float percentage = thor::dotProduct(startToEnd, path.back() - p->start) / float percentage = thor::dotProduct(startToEnd, path.back() - sf::Vector2f(p->start)) /
thor::squaredLength(startToEnd); thor::squaredLength(startToEnd);
sf::Vector2f point; sf::Vector2f point;
if (percentage < 0 || percentage > 1.0f) { if (percentage < 0 || percentage > 1.0f) {
if (thor::squaredLength(p->start - path.back()) < if (thor::squaredLength(sf::Vector2f(p->start) - path.back()) <
thor::squaredLength(p->end - path.back())) { thor::squaredLength(sf::Vector2f(p->end) - path.back())) {
thor::setLength(startToEnd, WALL_DISTANCE_MULTIPLIER * radius); thor::setLength(startToEnd, WALL_DISTANCE_MULTIPLIER * radius);
point = p->start + startToEnd; point = sf::Vector2f(p->start) + startToEnd;
} }
else { else {
thor::setLength(startToEnd, WALL_DISTANCE_MULTIPLIER * radius); thor::setLength(startToEnd, WALL_DISTANCE_MULTIPLIER * radius);
point = p->end - startToEnd; point = sf::Vector2f(p->end) - startToEnd;
} }
} }
else else
point = p->start + startToEnd * percentage; point = sf::Vector2f(p->start) + startToEnd * percentage;
// Take two points on a line orthogonal to the portal. // Take two points on a line orthogonal to the portal.
thor::setLength(startToEnd, radius); thor::setLength(startToEnd, radius);
@ -243,12 +200,22 @@ std::vector<std::shared_ptr<Character> >
} }
return visible; return visible;
} }
/**
* Initializes start and end of an area, sets area to null.
*/
World::Portal::Portal(const sf::Vector2i& start, const sf::Vector2i& end) :
start(start),
end(end),
area(nullptr) {
}
/** /**
* Returns the linear distance between two areas (using their center). * Returns the linear distance between two areas (using their center).
*/ */
float float
World::heuristic_cost_estimate(Area* start, Area* end) const { World::heuristic_cost_estimate(Area* start, Area* end) const {
return thor::length(end->center - start->center); return thor::length(sf::Vector2f(end->center - start->center));
} }
/** /**
@ -296,7 +263,7 @@ World::step(int elapsed) {
/** /**
* Calls Character::onThink for each character. Must be called * Calls Character::onThink for each character. Must be called
* before step (due to character removal). * before step (so Characters get removed asap).
* *
* @param elapsed Time since last call. * @param elapsed Time since last call.
*/ */
@ -312,6 +279,90 @@ World::think(int elapsed) {
} }
} }
/**
* Inserts an area used for path finding.
*
* @parm rect Rectangle the area covers.
*/
void
World::insertArea(const sf::IntRect& rect) {
Area a;
// Not sure why the offset of -50 is required, but with it, areas align
// with tiles perfectly.
a.area = sf::IntRect(rect.left * TileManager::TILE_SIZE.x - 50,
rect.top * TileManager::TILE_SIZE.y - 50,
rect.width * TileManager::TILE_SIZE.x,
rect.height * TileManager::TILE_SIZE.y);
a.center = sf::Vector2i(a.area.left + a.area.width / 2,
a.area.top + a.area.height / 2);
mAreas.push_back(a);
}
/**
* Generates portals that connect areas. Needs to be run after insertArea for
* path finding to work.
*
* Could be improved by only checking nearby areas.
*/
void
World::generatePortals() {
for (Area& it : mAreas) {
// We currently recreate portals for all existing areas, so we have
// to clear in case this was already generated.
it.portals.clear();
for (Area& other : mAreas) {
if (&it == &other)
continue;
Portal portal;
portal.area = &other;
if (it.area.left + it.area.width == other.area.left) {
Interval overlap = Interval::IntervalFromPoints(it.area.top,
it.area.top + it.area.height)
.getOverlap(Interval::IntervalFromPoints(other.area.top,
other.area.top + other.area.height));
if (overlap.getLength() > 0) {
portal.start = sf::Vector2i(other.area.left, overlap.start);
portal.end = sf::Vector2i(other.area.left, overlap.end);
it.portals.push_back(portal);
}
}
if (other.area.left + other.area.width == it.area.left) {
Interval overlap = Interval::IntervalFromPoints(it.area.top,
it.area.top + it.area.height)
.getOverlap(Interval::IntervalFromPoints(other.area.top,
other.area.top + other.area.height));
if (overlap.getLength() > 0) {
portal.start = sf::Vector2i(it.area.left, overlap.start);
portal.end = sf::Vector2i(it.area.left, overlap.end);
it.portals.push_back(portal);
}
}
else if (it.area.top + it.area.height == other.area.top) {
Interval overlap = Interval::IntervalFromPoints(it.area.left,
it.area.left + it.area.width)
.getOverlap(Interval::IntervalFromPoints(other.area.left,
other.area.left + other.area.width));
if (overlap.getLength() > 0) {
portal.start = sf::Vector2i(overlap.start, other.area.top);
portal.end = sf::Vector2i(overlap.end, other.area.top);
it.portals.push_back(portal);
}
}
else if (other.area.top + other.area.height == it.area.top) {
Interval overlap = Interval::IntervalFromPoints(it.area.left,
it.area.left + it.area.width)
.getOverlap(Interval::IntervalFromPoints(other.area.left,
other.area.left + other.area.width));
if (overlap.getLength() > 0) {
portal.start = sf::Vector2i(overlap.start, it.area.top);
portal.end = sf::Vector2i(overlap.end, it.area.top);
it.portals.push_back(portal);
}
}
}
}
}
/** /**
* Tests for collisions using Seperating Axis Theorem (SAT). * Tests for collisions using Seperating Axis Theorem (SAT).
* *
@ -426,7 +477,7 @@ World::testCollision(std::shared_ptr<Sprite> spriteA,
World::Area* World::Area*
World::getArea(const sf::Vector2f& point) const { World::getArea(const sf::Vector2f& point) const {
for (auto area = mAreas.begin(); area != mAreas.end(); area++) { for (auto area = mAreas.begin(); area != mAreas.end(); area++) {
if (area->area.contains(point)) if (sf::FloatRect(area->area).contains(point))
// Make the return value non-const for convenience. // Make the return value non-const for convenience.
return &const_cast<Area&>(*area); return &const_cast<Area&>(*area);
} }

View file

@ -28,7 +28,8 @@ public:
void removeCharacter(std::shared_ptr<Character> character); void removeCharacter(std::shared_ptr<Character> character);
void step(int elapsed); void step(int elapsed);
void think(int elapsed); void think(int elapsed);
void generateAreas(); void insertArea(const sf::IntRect& rect);
void generatePortals();
std::vector<sf::Vector2f> getPath(const sf::Vector2f& start, std::vector<sf::Vector2f> getPath(const sf::Vector2f& start,
const sf::Vector2f& end, float radius) const; const sf::Vector2f& end, float radius) const;
std::vector<std::shared_ptr<Character> > std::vector<std::shared_ptr<Character> >
@ -42,8 +43,13 @@ private:
* Redundant data as portals are saved twice. * Redundant data as portals are saved twice.
*/ */
struct Portal { struct Portal {
sf::Vector2f start; Portal() = default;
sf::Vector2f end; Portal(const sf::Vector2i& start, const sf::Vector2i& end);
bool operator==(const Portal& p) {
return start == p.start && end == p.end && area == p.area;
}
sf::Vector2i start;
sf::Vector2i end;
Area* area; Area* area;
}; };
@ -51,8 +57,8 @@ private:
* Nodes * Nodes
*/ */
struct Area { struct Area {
sf::FloatRect area; sf::IntRect area;
sf::Vector2f center; sf::Vector2i center;
std::vector<Portal> portals; std::vector<Portal> portals;
}; };

View file

@ -13,6 +13,7 @@
#include "simplexnoise.h" #include "simplexnoise.h"
#include "../sprites/TileManager.h" #include "../sprites/TileManager.h"
#include "../util/Log.h" #include "../util/Log.h"
#include "../World.h"
/// For usage with simplexnoise.h /// For usage with simplexnoise.h
uint8_t perm[512]; uint8_t perm[512];
@ -39,7 +40,8 @@ Generator::Generator() {
* power of two. * power of two.
*/ */
void void
Generator::generateTiles(TileManager& tm, const sf::IntRect& area) const { Generator::generateTiles(TileManager& tm, World& world,
const sf::IntRect& area) const {
// Check if width and height are power of two. // Check if width and height are power of two.
assert(area.width && !(area.width & (area.width - 1))); assert(area.width && !(area.width & (area.width - 1)));
assert(area.height && !(area.height & (area.height - 1))); assert(area.height && !(area.height & (area.height - 1)));
@ -66,10 +68,14 @@ Generator::generateTiles(TileManager& tm, const sf::IntRect& area) const {
for (int x = area.left; x < area.left + area.width; x++) { for (int x = area.left; x < area.left + area.width; x++) {
for (int y = area.top; y < area.top + area.height; y++) { for (int y = area.top; y < area.top + area.height; y++) {
(filtered[x-area.left][y-area.top]) (filtered[x-area.left][y-area.top])
? tm.insertTile(TileManager::TilePosition(x, y), TileManager::Type::WALL) ? tm.insertTile(TileManager::TilePosition(x, y),
: tm.insertTile(TileManager::TilePosition(x, y), TileManager::Type::FLOOR); TileManager::Type::WALL)
: tm.insertTile(TileManager::TilePosition(x, y),
TileManager::Type::FLOOR);
} }
} }
generateAreas(world, filtered, area, sf::Vector2f(area.left, area.top));
world.generatePortals();
} }
/** /**
@ -138,3 +144,38 @@ Generator::filterWalls(std::vector<std::vector<bool> >& in,
shortside * longside - subtract) shortside * longside - subtract)
fill(out, sf::IntRect(x, y, shortside, longside), true); fill(out, sf::IntRect(x, y, shortside, longside), true);
} }
/**
* Inserts tile if all values within area are the same, otherwise divides area
* into four and continues recursively.
*
* @param tm World to insert areas into.
* @param tiles Array of tile values (walls).
* @param area The area to generate areas for.
* @param offset Offset of tiles[0][0] from World coordinate (0, 0).
*/
void
Generator::generateAreas(World& world, std::vector<std::vector<bool> >& tiles,
const sf::IntRect& area, const sf::Vector2f& offset) {
assert(area.width > 0 && area.height > 0);
int count = countWalls(sf::IntRect(area.left - offset.y, area.top - offset.x,
area.width, area.height), tiles);
if (count == 0) {
world.insertArea(sf::IntRect(area));
}
else if (count == area.width * area.height) {
return;
}
else {
int halfWidth = area.width / 2.0f;
int halfHeight = area.height / 2.0f;
generateAreas(world, tiles, sf::IntRect(area.left,
area.top, halfWidth, halfHeight), offset);
generateAreas(world, tiles, sf::IntRect(area.left + halfWidth,
area.top, halfWidth, halfHeight), offset);
generateAreas(world, tiles, sf::IntRect(area.left,
area.top + halfHeight, halfWidth, halfHeight), offset);
generateAreas(world, tiles, sf::IntRect(area.left + halfWidth,
area.top + halfHeight, halfWidth, halfHeight), offset);
}
}

View file

@ -11,11 +11,13 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
class TileManager; class TileManager;
class World;
class Generator { class Generator {
public: public:
explicit Generator(); explicit Generator();
void generateTiles(TileManager& tm, const sf::IntRect& area) const; void generateTiles(TileManager& tm, World& world,
const sf::IntRect& area) const;
//void generateCharacters(World& world, const sf::IntRect& area) const; //void generateCharacters(World& world, const sf::IntRect& area) const;
sf::Vector2f getPlayerSpawn() const; sf::Vector2f getPlayerSpawn() const;
@ -27,6 +29,9 @@ private:
int x, int y, int longside, int shortside, int subtract); int x, int y, int longside, int shortside, int subtract);
static int countWalls(const sf::IntRect& area, static int countWalls(const sf::IntRect& area,
std::vector<std::vector<bool> >& tiles); std::vector<std::vector<bool> >& tiles);
static void generateAreas(World& world,
std::vector<std::vector<bool> >& tiles,
const sf::IntRect& area, const sf::Vector2f& offset);
}; };
#endif /* DG_GENERATOR_H_ */ #endif /* DG_GENERATOR_H_ */

View file

@ -16,12 +16,12 @@ public:
bool isInside(float point) const; bool isInside(float point) const;
float getLength(); float getLength();
private: public:
Interval(float start, float end);
private:
float start; float start;
float end; float end;
private:
Interval(float start, float end);
}; };
#endif /* DG_INTERVAL_H_ */ #endif /* DG_INTERVAL_H_ */