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-11 18:44:00 +00:00
|
|
|
#include <bitset>
|
|
|
|
|
|
|
|
#include "simplexnoise.h"
|
2013-04-12 18:34:48 +00:00
|
|
|
#include "../sprites/TileManager.h"
|
2013-04-18 08:05:17 +00:00
|
|
|
#include "../util/Log.h"
|
2013-04-28 16:11:39 +00:00
|
|
|
#include "../Pathfinder.h"
|
2013-04-11 18:44:00 +00:00
|
|
|
|
|
|
|
/// For usage with simplexnoise.h
|
|
|
|
uint8_t perm[512];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates new random seed.
|
|
|
|
*/
|
|
|
|
Generator::Generator() {
|
|
|
|
std::mt19937 mersenne(time(nullptr));
|
|
|
|
std::uniform_int_distribution<int> distribution(0, 255);
|
|
|
|
|
|
|
|
for (int i = 0; i < 512; i++) {
|
|
|
|
perm[i] = distribution(mersenne);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill TileManager with procedurally generated tiles.
|
|
|
|
*
|
|
|
|
* @param tm TileManager instance to set tiles in.
|
2013-04-18 08:05:17 +00:00
|
|
|
* @param area Size and position of area to generate tiles for. Must be
|
|
|
|
* power of two.
|
2013-04-11 18:44:00 +00:00
|
|
|
*/
|
|
|
|
void
|
2013-04-28 16:11:39 +00:00
|
|
|
Generator::generateTiles(TileManager& tm, Pathfinder& pathfinder,
|
2013-04-27 12:04:23 +00:00
|
|
|
const sf::IntRect& area) const {
|
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-04-28 21:18:17 +00:00
|
|
|
std::vector<std::vector<TileManager::Type> >
|
|
|
|
noise(area.width, std::vector<TileManager::Type>(area.height));
|
|
|
|
std::vector<std::vector<TileManager::Type> >
|
|
|
|
filtered(area.width, std::vector<TileManager::Type>(
|
|
|
|
area.height, TileManager::Type::FLOOR));
|
2013-04-11 18:44:00 +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-11 18:44:00 +00:00
|
|
|
noise[x-area.left][y-area.top] =
|
2013-04-28 21:18:17 +00:00
|
|
|
(scaled_octave_noise_2d(2, 2, 0.05f, 0.5f, -0.5f, x, y) +
|
|
|
|
scaled_octave_noise_2d(2, 2, 0.5f, 0.15f, -0.15f, x, y)
|
|
|
|
< -0.1f)
|
|
|
|
? TileManager::Type::WALL
|
|
|
|
: TileManager::Type::FLOOR;
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-27 17:42:44 +00:00
|
|
|
for (int x = 0; x < (int) noise.size(); x++) {
|
|
|
|
for (int y = 0; y < (int) noise[x].size(); y++) {
|
|
|
|
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-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-28 21:18:17 +00:00
|
|
|
tm.insertTile(TileManager::TilePosition(x, y),
|
|
|
|
filtered[x-area.left][y-area.top]);
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-28 16:11:39 +00:00
|
|
|
generateAreas(pathfinder, filtered, area,
|
|
|
|
sf::Vector2f(area.left, area.top));
|
|
|
|
pathfinder.generatePortals();
|
2013-04-11 18:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fills a rectangular area with the specified value.
|
|
|
|
*
|
|
|
|
* @param[in] Rectangular map.
|
|
|
|
* @param area The area to fill.
|
|
|
|
* @param value The value to set.
|
|
|
|
*/
|
|
|
|
void
|
2013-04-28 21:18:17 +00:00
|
|
|
Generator::fill(std::vector<std::vector<TileManager::Type> >& image,
|
|
|
|
const sf::IntRect& area, TileManager::Type value) {
|
2013-04-11 18:44:00 +00:00
|
|
|
for (int x = area.left;
|
|
|
|
x < area.left + area.width && x < (int) image.size(); x++) {
|
|
|
|
for (int y = area.top;
|
|
|
|
y < area.top + area.height && y < (int) image[x].size(); y++) {
|
|
|
|
image[x][y] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-28 21:18:17 +00:00
|
|
|
* Returns the number of walls in the area in tiles.
|
2013-04-11 18:44:00 +00:00
|
|
|
*
|
2013-04-18 08:05:17 +00:00
|
|
|
* @param area The area to count in.
|
2013-04-28 21:18:17 +00:00
|
|
|
* @param tiles Array of tile values.
|
2013-04-18 08:05:17 +00:00
|
|
|
*/
|
|
|
|
int Generator::countWalls(const sf::IntRect& area,
|
2013-04-28 21:18:17 +00:00
|
|
|
std::vector<std::vector<TileManager::Type> >& tiles) {
|
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-04-28 21:18:17 +00:00
|
|
|
count += (int) (tiles[x][y] == TileManager::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-04-18 08:05:17 +00:00
|
|
|
* @param[in] in Rectangular map of tiles.
|
|
|
|
* @param[out] out Rectangular map of tiles.
|
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-04-28 21:18:17 +00:00
|
|
|
Generator::filterWalls(std::vector<std::vector<TileManager::Type> >& in,
|
|
|
|
std::vector<std::vector<TileManager::Type> >& out,
|
2013-04-11 18:44:00 +00:00
|
|
|
int x, int y, int longside, int shortside, int subtract) {
|
|
|
|
// Skip if we would go out of range.
|
|
|
|
if ((x + longside >= (int) in.size()) ||
|
|
|
|
(y + longside >= (int) in[0].size()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Filter in horizontal direction.
|
2013-04-18 08:05:17 +00:00
|
|
|
if (countWalls(sf::IntRect(x, y, longside, shortside), in) >=
|
|
|
|
shortside * longside - subtract)
|
2013-04-28 21:18:17 +00:00
|
|
|
fill(out, sf::IntRect(x, y, longside, shortside), TileManager::Type::WALL);
|
2013-04-11 18:44:00 +00:00
|
|
|
// Filter in vertical direction.
|
2013-04-18 08:05:17 +00:00
|
|
|
if (countWalls(sf::IntRect(x, y, shortside, longside), in) >=
|
|
|
|
shortside * longside - subtract)
|
2013-04-28 21:18:17 +00:00
|
|
|
fill(out, sf::IntRect(x, y, shortside, longside), TileManager::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.
|
|
|
|
*
|
|
|
|
* @param tm World to insert areas into.
|
2013-04-28 21:18:17 +00:00
|
|
|
* @param tiles 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-04-28 21:18:17 +00:00
|
|
|
Generator::generateAreas(Pathfinder& pathfinder,
|
|
|
|
std::vector<std::vector<TileManager::Type> >& tiles,
|
2013-04-27 12:04:23 +00:00
|
|
|
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) {
|
2013-04-28 16:11:39 +00:00
|
|
|
pathfinder.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-04-28 16:11:39 +00:00
|
|
|
generateAreas(pathfinder, tiles, sf::IntRect(area.left,
|
2013-04-27 12:04:23 +00:00
|
|
|
area.top, halfWidth, halfHeight), offset);
|
2013-04-28 16:11:39 +00:00
|
|
|
generateAreas(pathfinder, tiles, sf::IntRect(area.left + halfWidth,
|
2013-04-27 12:04:23 +00:00
|
|
|
area.top, halfWidth, halfHeight), offset);
|
2013-04-28 16:11:39 +00:00
|
|
|
generateAreas(pathfinder, tiles, sf::IntRect(area.left,
|
2013-04-27 12:04:23 +00:00
|
|
|
area.top + halfHeight, halfWidth, halfHeight), offset);
|
2013-04-28 16:11:39 +00:00
|
|
|
generateAreas(pathfinder, tiles, sf::IntRect(area.left + halfWidth,
|
2013-04-27 12:04:23 +00:00
|
|
|
area.top + halfHeight, halfWidth, halfHeight), offset);
|
|
|
|
}
|
|
|
|
}
|