From 831975cf10d4cb7b490da315802d3699ca9c8a4b Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 3 Sep 2013 00:36:38 +0200 Subject: [PATCH] Moved generation parameters to yaml. --- res/yaml/generation.yaml | 15 +++++++++++++++ src/Game.cpp | 2 +- src/generator/Generator.cpp | 28 +++++++++++++++++----------- src/generator/Generator.h | 12 +++++++----- 4 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 res/yaml/generation.yaml diff --git a/res/yaml/generation.yaml b/res/yaml/generation.yaml new file mode 100644 index 0000000..75c2a86 --- /dev/null +++ b/res/yaml/generation.yaml @@ -0,0 +1,15 @@ +# Whenever tiles are generated, rectangles of this side length are created. +generate_area_size: 4 + +# Distance in pixels to which areas should be generated. +generate_area_range: 1200.0 + +# Deciding factor in how big individual rooms will be. Higher value means bigger rooms. +room_size_value: 10.0 + +# Deciding factor in how long connections between rooms may be and how many there may be. +# Higher value means more and/or longer paths. +room_connection_value: 5.0 + +# The chance that an enemy is placed in a certain tile, must be in [0, 1]. Higher value means more enemies. +enemy_generation_chance: 0.075 \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 1476c8a..e9a9374 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -22,7 +22,7 @@ Game::Game(tgui::Window& window) : mWindow(window), mWorldView(Vector2f(0, 0), mWindow.getView().getSize()), - mGenerator(mWorld, mPathfinder) { + mGenerator(mWorld, mPathfinder, Yaml("generation.yaml")) { mWindow.setFramerateLimit(FPS_GOAL); mWindow.setKeyRepeatEnabled(false); srand(time(nullptr)); diff --git a/src/generator/Generator.cpp b/src/generator/Generator.cpp index 392b2fa..c91873a 100644 --- a/src/generator/Generator.cpp +++ b/src/generator/Generator.cpp @@ -19,11 +19,17 @@ #include "../Pathfinder.h" #include "../World.h" #include "../sprites/Enemy.h" +#include "../util/Yaml.h" /** * Generates new random seed. */ -Generator::Generator(World& world, Pathfinder& pathfinder) : +Generator::Generator(World& world, Pathfinder& pathfinder, const Yaml& config) : + mAreaSize(config.get("generate_area_size", 1)), + mMaxRange((config.get("generate_area_range", 1.0f) / mAreaSize) / Tile::TILE_SIZE.x), + mRoomSizeValue(config.get("room_size_value", 1.0f)), + mRoomConnectionValue(config.get("room_connection_value", 1.0f)), + mEnemyGenerationChance(config.get("enemy_generation_chance", 0.0f) * 2 - 1), mWorld(world), mPathfinder(pathfinder) { } @@ -40,7 +46,7 @@ Generator::generateCurrentAreaIfNeeded(const Vector2f& playerPosition, Vector2i start((int) floor(playerPosition.x / Tile::TILE_SIZE.x), (int) floor(playerPosition.y / Tile::TILE_SIZE.y)); - start /= GENERATE_AREA_SIZE; + start /= mAreaSize; auto makePair = [&start](const Vector2i& point) { return std::make_pair(point, thor::length(Vector2f(point - start))); }; @@ -51,11 +57,11 @@ Generator::generateCurrentAreaIfNeeded(const Vector2f& playerPosition, float distance = open[current]; open.erase(current); closed.insert(current); - if (!mGenerated[current.x][current.y] && distance <= GENERATE_AREA_RANGE) { + if (!mGenerated[current.x][current.y] && distance <= mMaxRange) { mGenerated[current.x][current.y] = true; - sf::IntRect area(current * GENERATE_AREA_SIZE - - Vector2i(GENERATE_AREA_SIZE, GENERATE_AREA_SIZE) / 2, - Vector2i(GENERATE_AREA_SIZE, GENERATE_AREA_SIZE)); + sf::IntRect area(current * mAreaSize - + Vector2i(mAreaSize, mAreaSize) / 2, + Vector2i(mAreaSize, mAreaSize)); generateTiles(area); for (const auto& spawn : getEnemySpawns(area)) { float distance = thor::length(spawn - playerPosition); @@ -64,7 +70,7 @@ Generator::generateCurrentAreaIfNeeded(const Vector2f& playerPosition, mWorld, mPathfinder, spawn, playerItems))); } } - if (mGenerated[current.x][current.y] && distance <= GENERATE_AREA_RANGE) { + if (mGenerated[current.x][current.y] && distance <= mMaxRange) { if (closed.find(Vector2i(current.x + 1, current.y)) == closed.end()) open.insert(makePair(Vector2i(current.x + 1, current.y))); if (closed.find(Vector2i(current.x, current.y + 1)) == closed.end()) @@ -158,7 +164,7 @@ Generator::connectRooms(const Vector2i& start) { destinations.insert(current); break; } - if (distance.at(current) < ROOM_CONNECTION_VALUE) { + if (distance.at(current) < mRoomConnectionValue) { process(Vector2i(current.x + 1, current.y), current); process(Vector2i(current.x, current.y + 1), current); process(Vector2i(current.x - 1, current.y), current); @@ -167,7 +173,7 @@ Generator::connectRooms(const Vector2i& start) { } float totalValue = 0.0f; - while (totalValue < ROOM_CONNECTION_VALUE && !destinations.empty()) { + while (totalValue < mRoomConnectionValue && !destinations.empty()) { std::vector path; float pathValue = 0; Vector2i current = *destinations.begin(); @@ -214,7 +220,7 @@ Generator::generateTiles(const sf::IntRect& area) { } } - std::vector selected = createMinimalSpanningTree(start, ROOM_SIZE_VALUE); + std::vector selected = createMinimalSpanningTree(start, mRoomSizeValue); // For rooms, take minimum bounding box of spanning tree. @@ -261,7 +267,7 @@ Generator::getEnemySpawns(const sf::IntRect& area) { for (int x = area.left; x < area.left + area.width; x++) { for (int y = area.top; y < area.top + area.height; y++) { float noise = mCharacterNoise.getNoise(x, y); - if (noise <= -0.85f) { + if (noise <= mEnemyGenerationChance) { Vector2i tilePosition = findClosestFloor(Vector2i(x, y)); spawns.push_back(Vector2f(tilePosition.x * Tile::TILE_SIZE.x, tilePosition.y * Tile::TILE_SIZE.y)); diff --git a/src/generator/Generator.h b/src/generator/Generator.h index fd4c3f6..1d0b757 100644 --- a/src/generator/Generator.h +++ b/src/generator/Generator.h @@ -17,13 +17,14 @@ class World; class Pathfinder; +class Yaml; /** * Procedurally generates tiles, chooses player and enemy spawn positions. */ class Generator : public sf::Drawable { public: - explicit Generator(World& world, Pathfinder& pathfinder); + explicit Generator(World& world, Pathfinder& pathfinder, const Yaml& config); void generateCurrentAreaIfNeeded(const Vector2f& position, const Character::EquippedItems& playerItems); Vector2f getPlayerSpawn() const; @@ -42,10 +43,11 @@ private: void draw(sf::RenderTarget& target, sf::RenderStates states) const; private: - static constexpr int GENERATE_AREA_SIZE = 4; - static constexpr float GENERATE_AREA_RANGE = 4.0f; - static constexpr float ROOM_SIZE_VALUE = 10.0f; - static constexpr float ROOM_CONNECTION_VALUE = 5.0f; + const int mAreaSize; + const float mMaxRange; + const float mRoomSizeValue; + const float mRoomConnectionValue; + const float mEnemyGenerationChance; World& mWorld; Pathfinder& mPathfinder;