2013-04-11 18:44:00 +00:00
|
|
|
/*
|
|
|
|
* Generator.cpp
|
|
|
|
*
|
|
|
|
* Created on: 07.04.2013
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Generator.h"
|
|
|
|
|
2013-05-20 21:01:01 +00:00
|
|
|
#include <algorithm>
|
2013-04-18 08:05:17 +00:00
|
|
|
#include <assert.h>
|
2013-04-28 22:41:00 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include <SFML/System.hpp>
|
|
|
|
|
|
|
|
#include <Thor/Vectors.hpp>
|
2013-04-11 18:44:00 +00:00
|
|
|
|
2013-04-28 16:11:39 +00:00
|
|
|
#include "../Pathfinder.h"
|
2013-04-29 14:49:16 +00:00
|
|
|
#include "../World.h"
|
2013-06-21 15:40:39 +00:00
|
|
|
#include "../sprites/Enemy.h"
|
2013-04-11 18:44:00 +00:00
|
|
|
|
2013-05-20 21:01:01 +00:00
|
|
|
const int Generator::GENERATE_AREA_SIZE = 4;
|
|
|
|
const float Generator::GENERATE_AREA_RANGE = 4.0f;
|
|
|
|
|
2013-04-11 18:44:00 +00:00
|
|
|
/**
|
|
|
|
* Generates new random seed.
|
|
|
|
*/
|
2013-04-29 14:49:16 +00:00
|
|
|
Generator::Generator(World& world, Pathfinder& pathfinder) :
|
|
|
|
mWorld(world),
|
|
|
|
mPathfinder(pathfinder) {
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 21:01:01 +00:00
|
|
|
/**
|
2013-06-21 15:40:39 +00:00
|
|
|
* Generates tiles near player position (maximum distance is determined by
|
2013-05-20 21:01:01 +00:00
|
|
|
* GENERATE_AREA_SIZE and GENERATE_AREA_RANGE).
|
|
|
|
*/
|
|
|
|
void
|
2013-06-21 15:40:39 +00:00
|
|
|
Generator::generateCurrentAreaIfNeeded(const sf::Vector2f& playerPosition) {
|
2013-05-20 21:01:01 +00:00
|
|
|
auto compare = [](const sf::Vector2i& a, const sf::Vector2i& b) {
|
|
|
|
return a.x < b.x || (a.x == b.x && a.y < b.y);
|
|
|
|
};
|
|
|
|
std::map<sf::Vector2i, float, decltype(compare)> open(compare);
|
|
|
|
std::set<sf::Vector2i, decltype(compare)> closed(compare);
|
|
|
|
|
2013-06-21 15:40:39 +00:00
|
|
|
sf::Vector2i start((int) floor(playerPosition.x / Tile::TILE_SIZE.x),
|
|
|
|
(int) floor(playerPosition.y / Tile::TILE_SIZE.y));
|
2013-05-20 21:01:01 +00:00
|
|
|
start /= GENERATE_AREA_SIZE;
|
|
|
|
auto makePair = [&start](const sf::Vector2i& point) {
|
|
|
|
return std::make_pair(point, thor::length(sf::Vector2f(point - start)));
|
|
|
|
};
|
|
|
|
|
|
|
|
open.insert(makePair(start));
|
|
|
|
while (!open.empty()) {
|
|
|
|
auto intComp = [](const std::pair<sf::Vector2i, float>& left,
|
|
|
|
const std::pair<sf::Vector2i, float>& right) {
|
|
|
|
return left.second < right.second;
|
|
|
|
};
|
|
|
|
sf::Vector2i current =
|
|
|
|
std::min_element(open.begin(), open.end(), intComp)->first;
|
|
|
|
float distance = open[current];
|
|
|
|
open.erase(current);
|
|
|
|
closed.insert(current);
|
|
|
|
if (!mGenerated[current.x][current.y] && distance <= GENERATE_AREA_RANGE) {
|
|
|
|
mGenerated[current.x][current.y] = true;
|
2013-05-26 18:57:37 +00:00
|
|
|
sf::IntRect area(current * GENERATE_AREA_SIZE -
|
2013-05-20 21:01:01 +00:00
|
|
|
sf::Vector2i(GENERATE_AREA_SIZE, GENERATE_AREA_SIZE) / 2,
|
2013-05-26 18:57:37 +00:00
|
|
|
sf::Vector2i(GENERATE_AREA_SIZE, GENERATE_AREA_SIZE));
|
|
|
|
generateTiles(area);
|
2013-06-21 15:40:39 +00:00
|
|
|
for (const auto& enemyPosition : getEnemySpawns(area)) {
|
|
|
|
float distance = thor::length(enemyPosition - playerPosition);
|
|
|
|
if (distance > Character::VISION_DISTANCE) {
|
|
|
|
mWorld.insertCharacter(std::shared_ptr<Enemy>(new Enemy(mWorld, mPathfinder, enemyPosition)));
|
|
|
|
}
|
|
|
|
}
|
2013-05-20 21:01:01 +00:00
|
|
|
}
|
|
|
|
if (mGenerated[current.x][current.y] && distance <= GENERATE_AREA_RANGE) {
|
|
|
|
if (closed.find(sf::Vector2i(current.x + 1, current.y)) == closed.end())
|
|
|
|
open.insert(makePair(sf::Vector2i(current.x + 1, current.y)));
|
|
|
|
if (closed.find(sf::Vector2i(current.x, current.y + 1)) == closed.end())
|
|
|
|
open.insert(makePair(sf::Vector2i(current.x, current.y + 1)));
|
|
|
|
if (closed.find(sf::Vector2i(current.x - 1, current.y)) == closed.end())
|
|
|
|
open.insert(makePair(sf::Vector2i(current.x - 1, current.y)));
|
|
|
|
if (closed.find(sf::Vector2i(current.x, current.y - 1)) == closed.end())
|
|
|
|
open.insert(makePair(sf::Vector2i(current.x, current.y - 1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-11 18:44:00 +00:00
|
|
|
/**
|
2013-05-01 19:32:03 +00:00
|
|
|
* Fill world with procedurally generated tiles.
|
2013-04-11 18:44:00 +00:00
|
|
|
*
|
2013-05-01 19:32:03 +00:00
|
|
|
* @param area Size and position of area to generate tiles for. Width and
|
|
|
|
* height must each be a power of two.
|
2013-04-11 18:44:00 +00:00
|
|
|
*/
|
|
|
|
void
|
2013-04-29 14:49:16 +00:00
|
|
|
Generator::generateTiles(const sf::IntRect& area) {
|
2013-04-18 08:05:17 +00:00
|
|
|
// Check if width and height are power of two.
|
|
|
|
assert(area.width && !(area.width & (area.width - 1)));
|
|
|
|
assert(area.height && !(area.height & (area.height - 1)));
|
|
|
|
|
2013-06-15 15:49:01 +00:00
|
|
|
array generatedTiles;
|
|
|
|
fill(generatedTiles, area, type::FLOOR);
|
2013-05-01 19:32:03 +00:00
|
|
|
|
|
|
|
for (int x = area.left; x < area.left + area.width; x++) {
|
|
|
|
for (int y = area.top; y < area.top + area.height; y++) {
|
2013-06-15 15:49:01 +00:00
|
|
|
filterWalls(generatedTiles, x, y, 2, 1, 0);
|
|
|
|
filterWalls(generatedTiles, x, y, 6, 1, 2);
|
|
|
|
filterWalls(generatedTiles, x, y, 10, 1, 4);
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-01 19:32:03 +00:00
|
|
|
|
2013-06-15 15:49:01 +00:00
|
|
|
for (int x = area.left; x < area.left + area.width; x++)
|
2013-04-18 08:05:17 +00:00
|
|
|
for (int y = area.top; y < area.top + area.height; y++) {
|
2013-06-15 15:49:01 +00:00
|
|
|
// Merge map that we just generated with stored map.
|
|
|
|
mTiles[x][y] = generatedTiles[x][y];
|
|
|
|
// Actually generate physical tiles.
|
2013-04-29 14:49:16 +00:00
|
|
|
mWorld.insert(std::shared_ptr<Sprite>(
|
2013-06-15 15:49:01 +00:00
|
|
|
new Tile(generatedTiles.at(x).at(y), x, y)));
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
2013-06-15 15:49:01 +00:00
|
|
|
|
2013-06-21 15:40:39 +00:00
|
|
|
generateAreas(area);
|
2013-04-29 14:49:16 +00:00
|
|
|
mPathfinder.generatePortals();
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 15:40:39 +00:00
|
|
|
/**
|
|
|
|
* Returns coordinates where enemies should spawn.
|
|
|
|
*/
|
2013-05-08 09:19:21 +00:00
|
|
|
std::vector<sf::Vector2f>
|
2013-06-15 15:49:01 +00:00
|
|
|
Generator::getEnemySpawns(const sf::IntRect& area) {
|
2013-05-08 09:19:21 +00:00
|
|
|
auto compare = [](const sf::Vector2f& a, const sf::Vector2f& b) {
|
|
|
|
return a.x < b.x || (a.x == b.x && a.y < b.y);
|
|
|
|
};
|
|
|
|
std::set<sf::Vector2f, decltype(compare)> ret(compare);
|
|
|
|
for (int x = area.left; x < area.left + area.width; x++) {
|
|
|
|
for (int y = area.top; y < area.top + area.height; y++) {
|
2013-06-21 15:40:39 +00:00
|
|
|
if (mCharacterNoise.getNoise(x, y) < -0.85f) {
|
|
|
|
sf::Vector2i tilePosition = findClosestFloor(sf::Vector2i(x, y));
|
|
|
|
ret.insert(sf::Vector2f(tilePosition.x * Tile::TILE_SIZE.x,
|
|
|
|
tilePosition.y * Tile::TILE_SIZE.y));
|
2013-05-08 09:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::vector<sf::Vector2f>(ret.begin(), ret.end());
|
|
|
|
}
|
|
|
|
|
2013-04-11 18:44:00 +00:00
|
|
|
/**
|
|
|
|
* Fills a rectangular area with the specified value.
|
|
|
|
*
|
2013-05-01 19:32:03 +00:00
|
|
|
* @param[in,out] Array to set values to.
|
2013-04-11 18:44:00 +00:00
|
|
|
* @param area The area to fill.
|
|
|
|
* @param value The value to set.
|
|
|
|
*/
|
|
|
|
void
|
2013-06-15 15:49:01 +00:00
|
|
|
Generator::fill(array& tiles, const sf::IntRect& area, Tile::Type value) {
|
2013-05-01 19:32:03 +00:00
|
|
|
for (int x = area.left; x < area.left + area.width; x++) {
|
|
|
|
for (int y = area.top; y < area.top + area.height; y++)
|
2013-06-15 15:49:01 +00:00
|
|
|
tiles[x][y] = value;
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-01 19:32:03 +00:00
|
|
|
* Counts and returns the number of walls within the area.
|
2013-04-11 18:44:00 +00:00
|
|
|
*
|
2013-04-18 08:05:17 +00:00
|
|
|
* @param area The area to count in.
|
|
|
|
*/
|
2013-05-01 19:32:03 +00:00
|
|
|
int
|
2013-06-15 15:49:01 +00:00
|
|
|
Generator::countWalls(const sf::IntRect& area) {
|
2013-04-18 08:05:17 +00:00
|
|
|
int count = 0;
|
|
|
|
for (int x = area.left; x < area.left + area.width; x++) {
|
|
|
|
for (int y = area.top; y < area.top + area.height; y++)
|
2013-06-21 15:40:39 +00:00
|
|
|
count += (int) (getTileType(mTileNoise.getNoise(x, y)) ==
|
2013-06-15 15:49:01 +00:00
|
|
|
type::WALL);
|
2013-04-18 08:05:17 +00:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-15 15:49:01 +00:00
|
|
|
* Finds rectangles of specific size with mTileNoise and
|
2013-04-18 08:05:17 +00:00
|
|
|
* puts them into vector out.
|
2013-04-11 18:44:00 +00:00
|
|
|
*
|
2013-06-15 15:49:01 +00:00
|
|
|
* @param[in,out] tiles Tiles to be placed. Does not explicitly set floor values
|
2013-05-01 19:32:03 +00:00
|
|
|
* (keeps previous values).
|
2013-04-11 18:44:00 +00:00
|
|
|
* @param x Position to check from (top left corner for rectangle).
|
|
|
|
* @param y Position to check from (top left corner for rectangle).
|
|
|
|
* @param longside Length of the longer side of the rectangle.
|
|
|
|
* @param shortside Length of the shorter side of the rectangle.
|
|
|
|
* @param subtract Still accepts rectangle if at least this amount of
|
|
|
|
* tiles is not walls (tilecount >= longside * shortside - subtract).
|
|
|
|
*/
|
|
|
|
void
|
2013-06-15 15:49:01 +00:00
|
|
|
Generator::filterWalls(array& tiles, int x, int y, int longside,
|
2013-05-01 19:32:03 +00:00
|
|
|
int shortside, int subtract) {
|
2013-04-11 18:44:00 +00:00
|
|
|
// Filter in horizontal direction.
|
2013-06-15 15:49:01 +00:00
|
|
|
if (countWalls(sf::IntRect(x, y, longside, shortside)) >=
|
2013-04-18 08:05:17 +00:00
|
|
|
shortside * longside - subtract)
|
2013-06-15 15:49:01 +00:00
|
|
|
fill(tiles, sf::IntRect(x, y, longside, shortside), type::WALL);
|
2013-04-11 18:44:00 +00:00
|
|
|
// Filter in vertical direction.
|
2013-06-15 15:49:01 +00:00
|
|
|
if (countWalls(sf::IntRect(x, y, shortside, longside)) >=
|
2013-04-18 08:05:17 +00:00
|
|
|
shortside * longside - subtract)
|
2013-06-15 15:49:01 +00:00
|
|
|
fill(tiles, sf::IntRect(x, y, shortside, longside), type::WALL);
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
2013-04-27 12:04:23 +00:00
|
|
|
|
|
|
|
/**
|
2013-06-15 15:49:01 +00:00
|
|
|
* Inserts floor tiles into path finder, using a quadtree approach to group
|
|
|
|
* tiles where possible.
|
2013-04-27 12:04:23 +00:00
|
|
|
*
|
|
|
|
* @param area The area to generate areas for.
|
|
|
|
*/
|
|
|
|
void
|
2013-06-21 15:40:39 +00:00
|
|
|
Generator::generateAreas(const sf::IntRect& area) {
|
2013-04-27 12:04:23 +00:00
|
|
|
assert(area.width > 0 && area.height > 0);
|
2013-06-21 15:40:39 +00:00
|
|
|
|
|
|
|
int wallCount = 0;
|
|
|
|
for (int x = area.left; x < area.left + area.width; x++)
|
|
|
|
for (int y = area.top; y < area.top + area.height; y++)
|
|
|
|
wallCount += (int) (mTiles[x][y] == type::WALL);
|
|
|
|
|
|
|
|
if (wallCount == 0)
|
|
|
|
mPathfinder.insertArea(sf::FloatRect(area.left, area.top, area.width, area.height));
|
|
|
|
else if (wallCount == area.width * area.height)
|
2013-04-27 12:04:23 +00:00
|
|
|
return;
|
|
|
|
else {
|
2013-06-21 15:40:39 +00:00
|
|
|
int halfWidth = area.width / 2;
|
|
|
|
int halfHeight = area.height / 2;
|
2013-06-15 15:49:01 +00:00
|
|
|
generateAreas(sf::IntRect(area.left,
|
2013-06-21 15:40:39 +00:00
|
|
|
area.top, halfWidth, halfHeight));
|
2013-06-15 15:49:01 +00:00
|
|
|
generateAreas(sf::IntRect(area.left + halfWidth,
|
2013-06-21 15:40:39 +00:00
|
|
|
area.top, halfWidth, halfHeight));
|
2013-06-15 15:49:01 +00:00
|
|
|
generateAreas(sf::IntRect(area.left,
|
2013-06-21 15:40:39 +00:00
|
|
|
area.top + halfHeight, halfWidth, halfHeight));
|
2013-06-15 15:49:01 +00:00
|
|
|
generateAreas(sf::IntRect(area.left + halfWidth,
|
2013-06-21 15:40:39 +00:00
|
|
|
area.top + halfHeight, halfWidth, halfHeight));
|
2013-04-27 12:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-28 22:41:00 +00:00
|
|
|
|
2013-06-15 15:49:01 +00:00
|
|
|
/**
|
|
|
|
* Defines if a perlin noise result value is converted to a wall or floor tile.
|
|
|
|
*
|
|
|
|
* @param value Perlin noise value within [-1, 1]
|
|
|
|
*/
|
|
|
|
Generator::type
|
|
|
|
Generator::getTileType(float value) {
|
|
|
|
return (value < -0.2f)
|
|
|
|
? type::WALL
|
|
|
|
: type::FLOOR;
|
|
|
|
}
|
|
|
|
|
2013-04-28 22:41:00 +00:00
|
|
|
/**
|
|
|
|
* Returns a valid position (floor) for the player to spawn at.
|
|
|
|
*/
|
|
|
|
sf::Vector2f
|
|
|
|
Generator::getPlayerSpawn() const {
|
2013-05-01 19:32:03 +00:00
|
|
|
sf::Vector2i spawn = findClosestFloor(sf::Vector2i(0, 0));
|
|
|
|
return sf::Vector2f(spawn.x * Tile::TILE_SIZE.x,
|
|
|
|
spawn.y * Tile::TILE_SIZE.y);
|
2013-04-28 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the point array index closest to position which has a floor tile.
|
2013-05-01 19:32:03 +00:00
|
|
|
*
|
2013-07-21 09:02:14 +00:00
|
|
|
* @warn Will fail if no floor tile has been generated yet.
|
2013-05-01 19:32:03 +00:00
|
|
|
* @position Point to start search for a floor tile from.
|
2013-04-28 22:41:00 +00:00
|
|
|
*/
|
|
|
|
sf::Vector2i
|
|
|
|
Generator::findClosestFloor(const sf::Vector2i& position) const {
|
|
|
|
auto compare = [](const sf::Vector2i& a, const sf::Vector2i& b) {
|
|
|
|
return a.x < b.x || (a.x == b.x && a.y < b.y);
|
|
|
|
};
|
|
|
|
std::map<sf::Vector2i, float, decltype(compare)> open(compare);
|
|
|
|
std::set<sf::Vector2i, decltype(compare)> closed(compare);
|
|
|
|
sf::Vector2i start = position;
|
|
|
|
auto makePair = [&start](const sf::Vector2i& point) {
|
|
|
|
return std::make_pair(point, thor::length(sf::Vector2f(point - start)));
|
|
|
|
};
|
|
|
|
|
|
|
|
open.insert(makePair(start));
|
|
|
|
while (!open.empty()) {
|
2013-05-20 21:01:01 +00:00
|
|
|
auto intComp = [](const std::pair<sf::Vector2i, float>& left,
|
|
|
|
const std::pair<sf::Vector2i, float>& right) {
|
|
|
|
return left.second < right.second;
|
|
|
|
};
|
|
|
|
sf::Vector2i current = std::min_element(open.begin(), open.end(), intComp)->first;
|
2013-04-28 22:41:00 +00:00
|
|
|
open.erase(current);
|
|
|
|
closed.insert(current);
|
2013-05-20 21:01:01 +00:00
|
|
|
if (mTiles.count(current.x) != 0 &&
|
|
|
|
mTiles.at(current.x).count(current.y) != 0 &&
|
2013-06-21 15:40:39 +00:00
|
|
|
mTiles.at(current.x).at(current.y) == type::FLOOR) {
|
2013-04-28 22:41:00 +00:00
|
|
|
return current;
|
2013-06-21 15:40:39 +00:00
|
|
|
}
|
2013-04-28 22:41:00 +00:00
|
|
|
else {
|
|
|
|
if (closed.find(sf::Vector2i(current.x + 1, current.y)) == closed.end())
|
|
|
|
open.insert(makePair(sf::Vector2i(current.x + 1, current.y)));
|
|
|
|
if (closed.find(sf::Vector2i(current.x, current.y + 1)) == closed.end())
|
|
|
|
open.insert(makePair(sf::Vector2i(current.x, current.y + 1)));
|
|
|
|
if (closed.find(sf::Vector2i(current.x - 1, current.y)) == closed.end())
|
|
|
|
open.insert(makePair(sf::Vector2i(current.x - 1, current.y)));
|
|
|
|
if (closed.find(sf::Vector2i(current.x, current.y - 1)) == closed.end())
|
|
|
|
open.insert(makePair(sf::Vector2i(current.x, current.y - 1)));
|
|
|
|
}
|
|
|
|
}
|
2013-07-21 09:02:14 +00:00
|
|
|
// No floor tile found in the entire world.
|
2013-04-28 22:41:00 +00:00
|
|
|
assert(false);
|
|
|
|
return sf::Vector2i();
|
|
|
|
}
|