Changed Generator to use enum instead of bool.

This commit is contained in:
Felix Ableitner 2013-04-28 23:18:17 +02:00
parent 5be4a65653
commit 39cfb89f5e
3 changed files with 34 additions and 33 deletions

View file

@ -33,8 +33,6 @@ Generator::Generator() {
/** /**
* Fill TileManager with procedurally generated tiles. * Fill TileManager with procedurally generated tiles.
* *
* True means wall, false means floor.
*
* @param tm TileManager instance to set tiles in. * @param tm TileManager instance to set tiles in.
* @param area Size and position of area to generate tiles for. Must be * @param area Size and position of area to generate tiles for. Must be
* power of two. * power of two.
@ -46,16 +44,20 @@ Generator::generateTiles(TileManager& tm, Pathfinder& pathfinder,
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)));
std::vector<std::vector<bool> > std::vector<std::vector<TileManager::Type> >
noise(area.width, std::vector<bool>(area.height)); noise(area.width, std::vector<TileManager::Type>(area.height));
std::vector<std::vector<bool> > std::vector<std::vector<TileManager::Type> >
filtered(area.width, std::vector<bool>(area.height, false)); filtered(area.width, std::vector<TileManager::Type>(
area.height, TileManager::Type::FLOOR));
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++) {
noise[x-area.left][y-area.top] = noise[x-area.left][y-area.top] =
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) (scaled_octave_noise_2d(2, 2, 0.05f, 0.5f, -0.5f, x, y) +
< -0.1f; scaled_octave_noise_2d(2, 2, 0.5f, 0.15f, -0.15f, x, y)
< -0.1f)
? TileManager::Type::WALL
: TileManager::Type::FLOOR;
} }
} }
for (int x = 0; x < (int) noise.size(); x++) { for (int x = 0; x < (int) noise.size(); x++) {
@ -67,11 +69,8 @@ Generator::generateTiles(TileManager& tm, Pathfinder& pathfinder,
} }
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]) tm.insertTile(TileManager::TilePosition(x, y),
? tm.insertTile(TileManager::TilePosition(x, y), filtered[x-area.left][y-area.top]);
TileManager::Type::WALL)
: tm.insertTile(TileManager::TilePosition(x, y),
TileManager::Type::FLOOR);
} }
} }
generateAreas(pathfinder, filtered, area, generateAreas(pathfinder, filtered, area,
@ -87,8 +86,8 @@ Generator::generateTiles(TileManager& tm, Pathfinder& pathfinder,
* @param value The value to set. * @param value The value to set.
*/ */
void void
Generator::fill(std::vector<std::vector<bool> >& image, Generator::fill(std::vector<std::vector<TileManager::Type> >& image,
const sf::IntRect& area, bool value) { const sf::IntRect& area, TileManager::Type value) {
for (int x = area.left; for (int x = area.left;
x < area.left + area.width && x < (int) image.size(); x++) { x < area.left + area.width && x < (int) image.size(); x++) {
for (int y = area.top; for (int y = area.top;
@ -99,17 +98,17 @@ Generator::fill(std::vector<std::vector<bool> >& image,
} }
/** /**
* Returns the number of walls (fields with value true) in the area in tiles. * Returns the number of walls in the area in tiles.
* *
* @param area The area to count in. * @param area The area to count in.
* @param tiles Array of tile values (walls). * @param tiles Array of tile values.
*/ */
int Generator::countWalls(const sf::IntRect& area, int Generator::countWalls(const sf::IntRect& area,
std::vector<std::vector<bool> >& tiles) { std::vector<std::vector<TileManager::Type> >& tiles) {
int count = 0; int count = 0;
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++)
count += (int) tiles[x][y]; count += (int) (tiles[x][y] == TileManager::Type::WALL);
} }
return count; return count;
} }
@ -128,8 +127,8 @@ int Generator::countWalls(const sf::IntRect& area,
* tiles is not walls (tilecount >= longside * shortside - subtract). * tiles is not walls (tilecount >= longside * shortside - subtract).
*/ */
void void
Generator::filterWalls(std::vector<std::vector<bool> >& in, Generator::filterWalls(std::vector<std::vector<TileManager::Type> >& in,
std::vector<std::vector<bool> >& out, std::vector<std::vector<TileManager::Type> >& out,
int x, int y, int longside, int shortside, int subtract) { int x, int y, int longside, int shortside, int subtract) {
// Skip if we would go out of range. // Skip if we would go out of range.
if ((x + longside >= (int) in.size()) || if ((x + longside >= (int) in.size()) ||
@ -139,11 +138,11 @@ Generator::filterWalls(std::vector<std::vector<bool> >& in,
// Filter in horizontal direction. // Filter in horizontal direction.
if (countWalls(sf::IntRect(x, y, longside, shortside), in) >= if (countWalls(sf::IntRect(x, y, longside, shortside), in) >=
shortside * longside - subtract) shortside * longside - subtract)
fill(out, sf::IntRect(x, y, longside, shortside), true); fill(out, sf::IntRect(x, y, longside, shortside), TileManager::Type::WALL);
// Filter in vertical direction. // Filter in vertical direction.
if (countWalls(sf::IntRect(x, y, shortside, longside), in) >= if (countWalls(sf::IntRect(x, y, shortside, longside), in) >=
shortside * longside - subtract) shortside * longside - subtract)
fill(out, sf::IntRect(x, y, shortside, longside), true); fill(out, sf::IntRect(x, y, shortside, longside), TileManager::Type::WALL);
} }
/** /**
@ -151,12 +150,13 @@ Generator::filterWalls(std::vector<std::vector<bool> >& in,
* into four and continues recursively. * into four and continues recursively.
* *
* @param tm World to insert areas into. * @param tm World to insert areas into.
* @param tiles Array of tile values (walls). * @param tiles Array of tile values.
* @param area The area to generate areas for. * @param area The area to generate areas for.
* @param offset Offset of tiles[0][0] from World coordinate (0, 0). * @param offset Offset of tiles[0][0] from World coordinate (0, 0).
*/ */
void void
Generator::generateAreas(Pathfinder& pathfinder, std::vector<std::vector<bool> >& tiles, Generator::generateAreas(Pathfinder& pathfinder,
std::vector<std::vector<TileManager::Type> >& tiles,
const sf::IntRect& area, const sf::Vector2f& offset) { const sf::IntRect& area, const sf::Vector2f& offset) {
assert(area.width > 0 && area.height > 0); assert(area.width > 0 && area.height > 0);
int count = countWalls(sf::IntRect(area.left - offset.y, area.top - offset.x, int count = countWalls(sf::IntRect(area.left - offset.y, area.top - offset.x,

View file

@ -10,8 +10,9 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "../sprites/TileManager.h"
class Pathfinder; class Pathfinder;
class TileManager;
class Generator { class Generator {
public: public:
@ -22,15 +23,15 @@ public:
sf::Vector2f getPlayerSpawn() const; sf::Vector2f getPlayerSpawn() const;
private: private:
static void fill(std::vector<std::vector<bool> >& image, static void fill(std::vector<std::vector<TileManager::Type> >& image,
const sf::IntRect& area, bool value); const sf::IntRect& area, TileManager::Type value);
static void filterWalls(std::vector<std::vector<bool> >& in, static void filterWalls(std::vector<std::vector<TileManager::Type> >& in,
std::vector<std::vector<bool> >& out, std::vector<std::vector<TileManager::Type> >& out,
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<TileManager::Type> >& tiles);
static void generateAreas(Pathfinder& pathfinder, static void generateAreas(Pathfinder& pathfinder,
std::vector<std::vector<bool> >& tiles, std::vector<std::vector<TileManager::Type> >& tiles,
const sf::IntRect& area, const sf::Vector2f& offset); const sf::IntRect& area, const sf::Vector2f& offset);
}; };

View file

@ -14,7 +14,7 @@ class World;
class TileManager { class TileManager {
public: public:
enum class Type { enum class Type : char {
FLOOR, FLOOR,
WALL WALL
}; };