diff --git a/source/generator/Generator.cpp b/source/generator/Generator.cpp index d31bc8d..364951e 100644 --- a/source/generator/Generator.cpp +++ b/source/generator/Generator.cpp @@ -33,8 +33,6 @@ Generator::Generator() { /** * Fill TileManager with procedurally generated tiles. * - * True means wall, false means floor. - * * @param tm TileManager instance to set tiles in. * @param area Size and position of area to generate tiles for. Must be * power of two. @@ -46,16 +44,20 @@ Generator::generateTiles(TileManager& tm, Pathfinder& pathfinder, assert(area.width && !(area.width & (area.width - 1))); assert(area.height && !(area.height & (area.height - 1))); - std::vector > - noise(area.width, std::vector(area.height)); - std::vector > - filtered(area.width, std::vector(area.height, false)); + std::vector > + noise(area.width, std::vector(area.height)); + std::vector > + filtered(area.width, std::vector( + area.height, TileManager::Type::FLOOR)); for (int x = area.left; x < area.left + area.width; x++) { for (int y = area.top; y < area.top + area.height; y++) { 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) - < -0.1f; + (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; } } 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 y = area.top; y < area.top + area.height; y++) { - (filtered[x-area.left][y-area.top]) - ? tm.insertTile(TileManager::TilePosition(x, y), - TileManager::Type::WALL) - : tm.insertTile(TileManager::TilePosition(x, y), - TileManager::Type::FLOOR); + tm.insertTile(TileManager::TilePosition(x, y), + filtered[x-area.left][y-area.top]); } } generateAreas(pathfinder, filtered, area, @@ -87,8 +86,8 @@ Generator::generateTiles(TileManager& tm, Pathfinder& pathfinder, * @param value The value to set. */ void -Generator::fill(std::vector >& image, - const sf::IntRect& area, bool value) { +Generator::fill(std::vector >& image, + const sf::IntRect& area, TileManager::Type value) { for (int x = area.left; x < area.left + area.width && x < (int) image.size(); x++) { for (int y = area.top; @@ -99,17 +98,17 @@ Generator::fill(std::vector >& 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 tiles Array of tile values (walls). + * @param tiles Array of tile values. */ int Generator::countWalls(const sf::IntRect& area, - std::vector >& tiles) { + std::vector >& tiles) { 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++) - count += (int) tiles[x][y]; + count += (int) (tiles[x][y] == TileManager::Type::WALL); } return count; } @@ -128,8 +127,8 @@ int Generator::countWalls(const sf::IntRect& area, * tiles is not walls (tilecount >= longside * shortside - subtract). */ void -Generator::filterWalls(std::vector >& in, - std::vector >& out, +Generator::filterWalls(std::vector >& in, + std::vector >& out, int x, int y, int longside, int shortside, int subtract) { // Skip if we would go out of range. if ((x + longside >= (int) in.size()) || @@ -139,11 +138,11 @@ Generator::filterWalls(std::vector >& in, // Filter in horizontal direction. if (countWalls(sf::IntRect(x, y, longside, shortside), in) >= 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. if (countWalls(sf::IntRect(x, y, shortside, longside), in) >= 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 >& in, * into four and continues recursively. * * @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 offset Offset of tiles[0][0] from World coordinate (0, 0). */ void -Generator::generateAreas(Pathfinder& pathfinder, std::vector >& tiles, +Generator::generateAreas(Pathfinder& pathfinder, + std::vector >& 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, diff --git a/source/generator/Generator.h b/source/generator/Generator.h index 35e5d4b..aa4cfb3 100644 --- a/source/generator/Generator.h +++ b/source/generator/Generator.h @@ -10,8 +10,9 @@ #include +#include "../sprites/TileManager.h" + class Pathfinder; -class TileManager; class Generator { public: @@ -22,15 +23,15 @@ public: sf::Vector2f getPlayerSpawn() const; private: - static void fill(std::vector >& image, - const sf::IntRect& area, bool value); - static void filterWalls(std::vector >& in, - std::vector >& out, + static void fill(std::vector >& image, + const sf::IntRect& area, TileManager::Type value); + static void filterWalls(std::vector >& in, + std::vector >& out, int x, int y, int longside, int shortside, int subtract); static int countWalls(const sf::IntRect& area, - std::vector >& tiles); + std::vector >& tiles); static void generateAreas(Pathfinder& pathfinder, - std::vector >& tiles, + std::vector >& tiles, const sf::IntRect& area, const sf::Vector2f& offset); }; diff --git a/source/sprites/TileManager.h b/source/sprites/TileManager.h index 00b97dd..5e38697 100644 --- a/source/sprites/TileManager.h +++ b/source/sprites/TileManager.h @@ -14,7 +14,7 @@ class World; class TileManager { public: - enum class Type { + enum class Type : char { FLOOR, WALL };