2013-04-11 18:44:00 +00:00
|
|
|
/*
|
|
|
|
* Generator.cpp
|
|
|
|
*
|
|
|
|
* Created on: 07.04.2013
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Generator.h"
|
|
|
|
|
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
|
|
|
|
|
|
|
#include "simplexnoise.h"
|
2013-04-28 16:11:39 +00:00
|
|
|
#include "../Pathfinder.h"
|
2013-04-29 14:49:16 +00:00
|
|
|
#include "../World.h"
|
2013-04-11 18:44:00 +00:00
|
|
|
|
2013-05-01 19:32:03 +00:00
|
|
|
/// Seed for usage with simplexnoise.h
|
2013-04-11 18:44:00 +00:00
|
|
|
uint8_t perm[512];
|
|
|
|
|
2013-05-01 19:32:03 +00:00
|
|
|
/**
|
|
|
|
* Amount of tiles extra to generate, to get consistent walls
|
|
|
|
* across multiple generateTiles calls for bordering areas.
|
|
|
|
*/
|
|
|
|
const int Generator::MARGIN = 10;
|
|
|
|
|
2013-05-08 09:19:21 +00:00
|
|
|
// Different layers in 3d noise so we don't use the same noise values.
|
|
|
|
const float Generator::LAYER_TILES = 0;
|
|
|
|
const float Generator::LAYER_ENEMIES = 1.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
|
|
|
std::mt19937 mersenne(time(nullptr));
|
|
|
|
std::uniform_int_distribution<int> distribution(0, 255);
|
|
|
|
|
2013-05-01 19:32:03 +00:00
|
|
|
for (int i = 0; i < 512; i++)
|
2013-04-11 18:44:00 +00:00
|
|
|
perm[i] = distribution(mersenne);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-05-01 19:32:03 +00:00
|
|
|
array noise;
|
|
|
|
array filtered;
|
|
|
|
for (int x = area.left - MARGIN; x < area.left + area.width + MARGIN; x++) {
|
|
|
|
for (int y = area.top - MARGIN; y < area.top + area.height + MARGIN; y++) {
|
|
|
|
noise[x][y] =
|
2013-05-08 09:19:21 +00:00
|
|
|
(scaled_octave_noise_3d(2, 2, 0.05f, 0.5f, -0.5f, x, y, LAYER_TILES) +
|
|
|
|
scaled_octave_noise_3d(2, 2, 0.5f, 0.15f, -0.15f, x, y, LAYER_TILES)
|
2013-04-28 21:18:17 +00:00
|
|
|
< -0.1f)
|
2013-05-01 19:32:03 +00:00
|
|
|
? type::WALL
|
|
|
|
: type::FLOOR;
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-01 19:32:03 +00:00
|
|
|
fill(filtered, area, type::FLOOR);
|
|
|
|
|
|
|
|
for (int x = area.left; x < area.left + area.width; x++) {
|
|
|
|
for (int y = area.top; y < area.top + area.height; y++) {
|
2013-04-27 17:42:44 +00:00
|
|
|
filterWalls(noise, filtered, x, y, 2, 1, 0);
|
|
|
|
filterWalls(noise, filtered, x, y, 6, 1, 2);
|
|
|
|
filterWalls(noise, filtered, x, y, 10, 1, 4);
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-01 19:32:03 +00:00
|
|
|
|
2013-04-18 08:05:17 +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-04-29 14:49:16 +00:00
|
|
|
mWorld.insert(std::shared_ptr<Sprite>(
|
2013-05-01 19:32:03 +00:00
|
|
|
new Tile(filtered.at(x).at(y), x, y)));
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-01 19:32:03 +00:00
|
|
|
generateAreas(filtered, area, sf::Vector2f(area.left, area.top));
|
2013-04-29 14:49:16 +00:00
|
|
|
mPathfinder.generatePortals();
|
2013-04-28 22:41:00 +00:00
|
|
|
mGenerated = filtered;
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 09:19:21 +00:00
|
|
|
std::vector<sf::Vector2f>
|
|
|
|
Generator::getEnemySpawns(const sf::IntRect& area) const {
|
|
|
|
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++) {
|
|
|
|
if (scaled_octave_noise_3d(2, 2, 0.5f, 10.0f, 0, x, y, LAYER_ENEMIES)
|
|
|
|
< 1.0f) {
|
|
|
|
ret.insert(sf::Vector2f(thor::componentwiseProduct(
|
|
|
|
findClosestFloor(sf::Vector2i(x, y)), Tile::TILE_SIZE)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-05-01 19:32:03 +00:00
|
|
|
Generator::fill(array& image, const sf::IntRect& area, Tile::Type value) {
|
|
|
|
for (int x = area.left; x < area.left + area.width; x++) {
|
|
|
|
for (int y = area.top; y < area.top + area.height; y++)
|
2013-04-11 18:44:00 +00:00
|
|
|
image[x][y] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-05-01 19:32:03 +00:00
|
|
|
* @param[in] tiles Array of tile values.
|
2013-04-18 08:05:17 +00:00
|
|
|
* @param area The area to count in.
|
|
|
|
*/
|
2013-05-01 19:32:03 +00:00
|
|
|
int
|
|
|
|
Generator::countWalls(const array& tiles, 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-05-01 19:32:03 +00:00
|
|
|
count += (int) (tiles.at(x).at(y) == type::WALL);
|
2013-04-18 08:05:17 +00:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds rectangles of specific size inside vector in and
|
|
|
|
* puts them into vector out.
|
2013-04-11 18:44:00 +00:00
|
|
|
*
|
2013-05-01 19:32:03 +00:00
|
|
|
* @param[in] in Perlin noise values.
|
|
|
|
* @param[in,out] out Tiles to be placed. Does not explicitly set floor values
|
|
|
|
* (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-05-01 19:32:03 +00:00
|
|
|
Generator::filterWalls(const array& in, array& out, int x, int y, int longside,
|
|
|
|
int shortside, int subtract) {
|
2013-04-11 18:44:00 +00:00
|
|
|
// Filter in horizontal direction.
|
2013-05-01 19:32:03 +00:00
|
|
|
if (countWalls(in, sf::IntRect(x, y, longside, shortside)) >=
|
2013-04-18 08:05:17 +00:00
|
|
|
shortside * longside - subtract)
|
2013-05-01 19:32:03 +00:00
|
|
|
fill(out, sf::IntRect(x, y, longside, shortside), type::WALL);
|
2013-04-11 18:44:00 +00:00
|
|
|
// Filter in vertical direction.
|
2013-05-01 19:32:03 +00:00
|
|
|
if (countWalls(in, sf::IntRect(x, y, shortside, longside)) >=
|
2013-04-18 08:05:17 +00:00
|
|
|
shortside * longside - subtract)
|
2013-05-01 19:32:03 +00:00
|
|
|
fill(out, sf::IntRect(x, y, shortside, longside), type::WALL);
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
2013-04-27 12:04:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts tile if all values within area are the same, otherwise divides area
|
|
|
|
* into four and continues recursively.
|
|
|
|
*
|
2013-05-01 19:32:03 +00:00
|
|
|
* @param in Array of tile values.
|
2013-04-27 12:04:23 +00:00
|
|
|
* @param area The area to generate areas for.
|
|
|
|
* @param offset Offset of tiles[0][0] from World coordinate (0, 0).
|
|
|
|
*/
|
|
|
|
void
|
2013-05-01 19:32:03 +00:00
|
|
|
Generator::generateAreas(const array& in, const sf::IntRect& area,
|
|
|
|
const sf::Vector2f& offset) const {
|
2013-04-27 12:04:23 +00:00
|
|
|
assert(area.width > 0 && area.height > 0);
|
2013-05-01 19:32:03 +00:00
|
|
|
int count = countWalls(in, area);
|
2013-04-27 12:04:23 +00:00
|
|
|
if (count == 0) {
|
2013-04-29 14:49:16 +00:00
|
|
|
mPathfinder.insertArea(sf::IntRect(area));
|
2013-04-27 12:04:23 +00:00
|
|
|
}
|
|
|
|
else if (count == area.width * area.height) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int halfWidth = area.width / 2.0f;
|
|
|
|
int halfHeight = area.height / 2.0f;
|
2013-05-01 19:32:03 +00:00
|
|
|
generateAreas(in, sf::IntRect(area.left,
|
2013-04-27 12:04:23 +00:00
|
|
|
area.top, halfWidth, halfHeight), offset);
|
2013-05-01 19:32:03 +00:00
|
|
|
generateAreas(in, sf::IntRect(area.left + halfWidth,
|
2013-04-27 12:04:23 +00:00
|
|
|
area.top, halfWidth, halfHeight), offset);
|
2013-05-01 19:32:03 +00:00
|
|
|
generateAreas(in, sf::IntRect(area.left,
|
2013-04-27 12:04:23 +00:00
|
|
|
area.top + halfHeight, halfWidth, halfHeight), offset);
|
2013-05-01 19:32:03 +00:00
|
|
|
generateAreas(in, sf::IntRect(area.left + halfWidth,
|
2013-04-27 12:04:23 +00:00
|
|
|
area.top + halfHeight, halfWidth, halfHeight), offset);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* @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()) {
|
|
|
|
const sf::Vector2i& current = open.begin()->first;
|
|
|
|
open.erase(current);
|
|
|
|
closed.insert(current);
|
2013-05-08 09:19:21 +00:00
|
|
|
if (mGenerated.count(current.x) != 0 &&
|
|
|
|
mGenerated.at(current.x).count(current.y) != 0 &&
|
|
|
|
mGenerated.at(current.x).at(current.y) == Tile::Type::FLOOR)
|
2013-04-28 22:41:00 +00:00
|
|
|
return current;
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(false);
|
|
|
|
return sf::Vector2i();
|
|
|
|
}
|