From 83194f83a35abeedf9b5edad7eb5a62887ee0272 Mon Sep 17 00:00:00 2001 From: Sam Sleight Date: Mon, 9 Sep 2013 16:41:32 +0100 Subject: [PATCH] Initial noisemap implementation Base noisemap class, with random and combination noisemaps inheriting from. --- premake4.lua | 1 - src/constraint.cpp | 62 ------------------- src/generator.cpp | 23 ------- src/inc/constraint.hpp | 58 ------------------ src/inc/defines.hpp | 2 + src/inc/generator.hpp | 53 ---------------- src/inc/noisemap.hpp | 101 +++++++++++++------------------ src/inc/tiledef.hpp | 60 ------------------- src/inc/world.hpp | 28 ++++----- src/noisemap.cpp | 133 ++++++----------------------------------- src/tiledef.cpp | 55 ----------------- src/world.cpp | 48 ++++----------- test.cpp | 68 ++------------------- 13 files changed, 92 insertions(+), 600 deletions(-) delete mode 100644 src/constraint.cpp delete mode 100644 src/generator.cpp delete mode 100644 src/inc/constraint.hpp delete mode 100644 src/inc/generator.hpp delete mode 100644 src/inc/tiledef.hpp delete mode 100644 src/tiledef.cpp diff --git a/premake4.lua b/premake4.lua index bc3561d..59efeb1 100644 --- a/premake4.lua +++ b/premake4.lua @@ -8,7 +8,6 @@ project "worldgen" links { "noise" } objdir "build/obj" buildoptions { "-std=c++11" } - defines { "WG_BUILD_OPS" } configuration "debug" flags { "Symbols", "ExtraWarnings" } diff --git a/src/constraint.cpp b/src/constraint.cpp deleted file mode 100644 index df48025..0000000 --- a/src/constraint.cpp +++ /dev/null @@ -1,62 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// Copyright 2013 Samuel Sleight -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -///////////////////////////////////////////////////////////////////////////// -// Project: World Generation Library -// File: src/constraint.cpp -// Author: Samuel Sleight -///////////////////////////////////////////////////////////////////////////// - -#include "inc/constraint.hpp" - -#define WG_IMP_NOISEMAP -#include "inc/noisemap.hpp" -#undef WG_IMP_NOISEMAP - -WG_NS - -Constraint::Constraint(std::shared_ptr noiseMap, Type type, double value) - : noiseMap(noiseMap), type(type), value(value) { - -} // Constraint::Constraint(NoiseMap::Ptr noiseMap, Type type, double value); - -bool Constraint::isValid(int chunkWidth, int chunkHeight, int chunkX, int chunkY, int x, int y) { - if(!(noiseMap->isGenerated(chunkWidth, chunkHeight, chunkX, chunkY))) { - noiseMap->generate(chunkWidth, chunkHeight, chunkX, chunkY); - - } // if(!(noiseMap->isGenerated(chunkWidth, chunkHeight, chunkX, chunkY))); - - bool retval = false; - - switch(type) { - case GT: - retval = (noiseMap->noiseVals[y][x] > value); - break; - - case LT: - retval = (noiseMap->noiseVals[y][x] < value); - break; - - } // switch(type); - - return retval; - -} // bool Constraint::isValid(int chunkWidth, int chunkHeight, int chunkX, int chunkY, int x, int y); - -void Constraint::clear() { - noiseMap.reset(); - -} // void Constraint::clear(); - -WG_NS_END diff --git a/src/generator.cpp b/src/generator.cpp deleted file mode 100644 index 22d3504..0000000 --- a/src/generator.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "inc/generator.hpp" - -WG_NS - -Generator::Generator(std::string seed, double gridX, double gridY, - int octaves, double freq, double pers, double lacu) - : gridX(gridX), gridY(gridY) { - - perlinModule.SetSeed(std::hash()(seed)); - perlinModule.SetOctaveCount(octaves); - perlinModule.SetFrequency(freq); - perlinModule.SetPersistence(pers); - perlinModule.SetLacunarity(lacu); - -} // Generator::Generator(std::string seed, double gridX, double gridY, - // int octaves, double freq, double pers, double lacu); - -double Generator::operator()(int x, int y) { - return perlinModule.GetValue(x * gridX, y * gridY, 0); - -} // double Generator::operator()(int x, int y); - -WG_NS_END diff --git a/src/inc/constraint.hpp b/src/inc/constraint.hpp deleted file mode 100644 index 9936636..0000000 --- a/src/inc/constraint.hpp +++ /dev/null @@ -1,58 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// Copyright 2013 Samuel Sleight -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -///////////////////////////////////////////////////////////////////////////// -// Project: World Generation Library -// File: src/inc/constraint.hpp -// Author: Samuel Sleight -///////////////////////////////////////////////////////////////////////////// - -#ifndef WG_CONSTRAINT_HPP -#define WG_CONSTRAINT_HPP - -#include - -#include "defines.hpp" - -WG_NS - -class NoiseMap; -class TileDef; - -class Constraint { -public: - enum Type { - GT, - LT - - }; // enum Type; - - Constraint(std::shared_ptr noiseMap, Type type, double value); - -private: - friend class TileDef; - - std::shared_ptr noiseMap; - Type type; - double value; - - bool isValid(int chunkWidth, int chunkHeight, int chunkX, int chunkY, int x, int y); - - void clear(); - -}; // class Constraint; - -WG_NS_END - -#endif // WG_CONSTRAINT_HPP diff --git a/src/inc/defines.hpp b/src/inc/defines.hpp index 62e937d..d2292d1 100644 --- a/src/inc/defines.hpp +++ b/src/inc/defines.hpp @@ -24,6 +24,8 @@ #define WG_NS namespace wg { #define WG_NS_END } +#define WG_DEF_CHUNK 20 + #define WG_DEF_SEED "0" #define WG_DEF_GRID 1 #define WG_DEF_OCTAVES noise::module::DEFAULT_PERLIN_OCTAVE_COUNT diff --git a/src/inc/generator.hpp b/src/inc/generator.hpp deleted file mode 100644 index 6038c54..0000000 --- a/src/inc/generator.hpp +++ /dev/null @@ -1,53 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// Copyright 2013 Samuel Sleight -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -///////////////////////////////////////////////////////////////////////////// -// Project: World Generation Library -// File: src/inc/generator.hpp -// Author: Samuel Sleight -///////////////////////////////////////////////////////////////////////////// - -#ifndef WG_GENERATOR_HPP -#define WG_GENERATOR_HPP - -#include "defines.hpp" - -#include - -#include - -WG_NS - -class NoiseMap; -class World; - -class Generator { -private: - friend class NoiseMap; - friend class World; - - Generator(std::string seed, double gridX, double gridY, - int octaves, double freq, double pers, double lacu); - - double operator()(int x, int y); - - double gridX, gridY; - - noise::module::Perlin perlinModule; - -}; // class Generator; - -WG_NS_END - -#endif // WG_GENERATOR_HPP diff --git a/src/inc/noisemap.hpp b/src/inc/noisemap.hpp index b244ffb..37d994a 100644 --- a/src/inc/noisemap.hpp +++ b/src/inc/noisemap.hpp @@ -21,35 +21,46 @@ #ifndef WG_NOISEMAP_HPP #define WG_NOISEMAP_HPP -#include -#include #include +#include +#include -#include "defines.hpp" -#include "generator.hpp" +#include -#include +#include "defines.hpp" WG_NS -class NoiseMap : public std::enable_shared_from_this { +class World; + +class NoiseMap { +protected: + NoiseMap(bool combination); + public: - typedef std::shared_ptr Ptr; + bool isCombination() { return combination; } + bool isGenerated() { return generated; } - static Ptr create() { return Ptr(new NoiseMap()); } - static Ptr combination() { return Ptr(new NoiseMap(true)); } + double getValue(int x, int y) { return noiseVals[y][x]; } + std::vector> getValues() { return noiseVals; } + +private: + bool combination, generated; + std::vector> noiseVals; - ~NoiseMap(); +}; // class NoiseMap; - Ptr setSeed(std::string seed) { this->seed = seed; return shared_from_this(); } - Ptr setGridSize(double value) { this->gridSizeX = value; this->gridSizeY = value; return shared_from_this(); } - Ptr setGridSize(double width, double height) { this->gridSizeX = width; this->gridSizeY = height; return shared_from_this(); } - Ptr setGridWidth(double value) { this->gridSizeX = value; return shared_from_this(); } - Ptr setGridHeight(double value) { this->gridSizeY = value; return shared_from_this(); } - Ptr setOctaves(int value) { this->octaves = value; return shared_from_this(); } - Ptr setFrequency(double value) { this->frequency = value; return shared_from_this(); } - Ptr setPersistence(double value) { this->persistence = value; return shared_from_this(); } - Ptr setLacunarity(double value) { this->lacunarity = value; return shared_from_this(); } +class RandomNoiseMap : public NoiseMap { +public: + RandomNoiseMap* setSeed(std::string seed) { this->seed = seed; return this; } + RandomNoiseMap* setGridSize(double value) { this->gridSizeX = value; this->gridSizeY = value; return this; } + RandomNoiseMap* setGridSize(double width, double height) { this->gridSizeX = width; this->gridSizeY = height; return this; } + RandomNoiseMap* setGridWidth(double value) { this->gridSizeX = value; return this; } + RandomNoiseMap* setGridHeight(double value) { this->gridSizeY = value; return this; } + RandomNoiseMap* setOctaves(int value) { this->octaves = value; return this; } + RandomNoiseMap* setFrequency(double value) { this->frequency = value; return this; } + RandomNoiseMap* setPersistence(double value) { this->persistence = value; return this; } + RandomNoiseMap* setLacunarity(double value) { this->lacunarity = value; return this; } std::string getSeed() { return seed; } double getGridWidth() { return gridSizeX; } @@ -58,21 +69,11 @@ class NoiseMap : public std::enable_shared_from_this { double getFrequency() { return frequency; } double getPersistence() { return persistence; } double getLacunarity() { return lacunarity; } - bool isGenerated() { return generated; } - bool isGenerated(int chunkWidth, int chunkHeight, int chunkX, int chunkY); - - Ptr add(Ptr noiseMap, int factor); - - Ptr generate(int width, int height, int chunkX = 0, int chunkY = 0); - - double getValue(int x, int y) { return noiseVals[y][x]; } - std::vector> getValues() { return noiseVals; } private: - friend class Constraint; + friend class World; - NoiseMap(); - NoiseMap(bool combination); + RandomNoiseMap(); std::string seed; double gridSizeX, gridSizeY; @@ -81,41 +82,21 @@ class NoiseMap : public std::enable_shared_from_this { double persistence; double lacunarity; - bool isCombination; - std::vector> combinations; +}; // class RandomNoiseMap : public NoiseMap; - void genNormal(); - void genCombination(); +class CombinationNoiseMap : public NoiseMap { +public: + CombinationNoiseMap* add(NoiseMap* mapToAdd, int factor); - bool generated; +private: + friend class World; - std::vector> noiseVals; + CombinationNoiseMap(); - int chunkX, chunkY, chunkWidth, chunkHeight; + std::vector> combinations; -}; // class NoiseMap; +}; // class CombinationNoiseMap : public NoiseMap; WG_NS_END -#include "constraint.hpp" - -wg::Constraint operator<(wg::NoiseMap::Ptr lhs, double rhs); -wg::Constraint operator>(wg::NoiseMap::Ptr lhs, double rhs); - -#ifdef WG_BUILD_OPS -#ifndef WG_IMP_NOISEMAP - -wg::Constraint operator<(wg::NoiseMap::Ptr lhs, double rhs) { - return wg::Constraint(lhs, wg::Constraint::Type::LT, rhs); - -} // Constraint operator<(NoiseMap::Ptr lhs, double rhs); - -wg::Constraint operator>(wg::NoiseMap::Ptr lhs, double rhs) { - return wg::Constraint(lhs, wg::Constraint::Type::GT, rhs); - -} // Constraint operator<(NoiseMap::Ptr lhs, double rhs); - -#endif // WG_IMP_NOISEMAP -#endif // WG_BUILD_OPS - #endif // WG_NOISEMAP_HPP diff --git a/src/inc/tiledef.hpp b/src/inc/tiledef.hpp deleted file mode 100644 index e67b616..0000000 --- a/src/inc/tiledef.hpp +++ /dev/null @@ -1,60 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// Copyright 2013 Samuel Sleight -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -///////////////////////////////////////////////////////////////////////////// -// Project: World Generation Library -// File: src/inc/tiledef.hpp -// Author: Samuel Sleight -///////////////////////////////////////////////////////////////////////////// - -#ifndef WG_TILEDEF_HPP -#define WG_TILEDEF_HPP - -#include -#include - -#include "defines.hpp" -#include "constraint.hpp" - -WG_NS - -class World; - -class TileDef : public std::enable_shared_from_this { -public: - typedef std::shared_ptr Ptr; - - ~TileDef(); - - Ptr addConstraint(Constraint c) { constraints.push_back(c); return shared_from_this(); } - - unsigned int getId() { return id; } - -private: - friend class World; - - TileDef(); - - static unsigned int nextId; - unsigned int id; - - std::vector constraints; - - bool isValid(int chunkWidth, int chunkHeight, int chunkX, int chunkY, int x, int y); - -}; // class TileDef; - -WG_NS_END - -#endif // WG_TILEDEF_HPP diff --git a/src/inc/world.hpp b/src/inc/world.hpp index 484bc3d..364a0f8 100644 --- a/src/inc/world.hpp +++ b/src/inc/world.hpp @@ -24,24 +24,24 @@ #include #include "defines.hpp" -#include "tiledef.hpp" WG_NS -class World : public std::enable_shared_from_this { -public: - typedef std::shared_ptr Ptr; +class NoiseMap; +class RandomNoiseMap; +class CombinationNoiseMap; - static Ptr create() { return Ptr(new World()); } - ~World(); +class World { +public: + World(); - TileDef::Ptr newTile(); - - Ptr setChunkSize(int width, int height) { chunkWidth = width; chunkHeight = height; return shared_from_this(); } - Ptr setChunkWidth(int value) { chunkWidth = value; return shared_from_this(); } - Ptr setChunkHeight(int value) { chunkHeight = value; return shared_from_this(); } + World* setChunkSize(int width, int height) { chunkWidth = width; chunkHeight = height; return this; } + World* setChunkSize(int value) { chunkWidth = value; chunkHeight = value; return this; } + World* setChunkWidth(int value) { chunkWidth = value; return this; } + World* setChunkHeight(int value) { chunkHeight = value; return this; } - Ptr generate(int chunkX = 0, int chunkY = 0); + RandomNoiseMap* addRandomNoiseMap(); + CombinationNoiseMap* addCombinationNoiseMap(); std::vector> getMap() { return this->mapGrid; } unsigned int getTile(int x, int y) { return mapGrid[y][x]; } @@ -49,11 +49,11 @@ class World : public std::enable_shared_from_this { private: int chunkWidth, chunkHeight; - std::vector tileDefinitions; + std::vector noiseMaps; std::vector> mapGrid; -}; // class World : public std::enable_shared_from_this; +}; // class World; WG_NS_END diff --git a/src/noisemap.cpp b/src/noisemap.cpp index 8d8b7fd..7223ec7 100644 --- a/src/noisemap.cpp +++ b/src/noisemap.cpp @@ -22,130 +22,33 @@ WG_NS -NoiseMap::NoiseMap() - : seed(WG_DEF_SEED), - gridSizeX(WG_DEF_GRID), gridSizeY(WG_DEF_GRID), - octaves(WG_DEF_OCTAVES), - frequency(WG_DEF_FREQ), - persistence(WG_DEF_PERS), - lacunarity(WG_DEF_LACU), - - isCombination(false) { - -} // NoiseMap::NoiseMap(); - -NoiseMap::NoiseMap(bool combination) - : isCombination(true) { +NoiseMap::NoiseMap(bool combination) + : combination(combination), generated(false) { } // NoiseMap::NoiseMap(bool combination); -NoiseMap::~NoiseMap() { - if(isCombination) { - for(auto& p : combinations) { - p.first.reset(); - - } // for(auto& p : combinations); - - } // if(isCombinations); - - combinations.clear(); - noiseVals.clear(); - -} // NoiseMap::~NoiseMap(); - -NoiseMap::Ptr NoiseMap::add(Ptr noiseMap, int factor) { - combinations.push_back(std::make_pair(noiseMap, factor)); - - return shared_from_this(); - -} // Ptr NoiseMap::add(Ptr noiseMap, int factor); - -NoiseMap::Ptr NoiseMap::generate(int width, int height, int chunkX, int chunkY) { - this->chunkX = chunkX; - this->chunkY = chunkY; - chunkWidth = width; - chunkHeight = height; - - if(isCombination) { - genCombination(); - - } // if(isCombination); - else { - genNormal(); - - } // else; - - generated = true; - - return shared_from_this(); - -} // NoiseMap::Ptr NoiseMap::generate(int width, int height); +RandomNoiseMap::RandomNoiseMap() + : NoiseMap(false), -void NoiseMap::genNormal() { - Generator gen(seed, gridSizeX, gridSizeY, octaves, - frequency, persistence, lacunarity); - - int yBase = chunkY * chunkHeight; - int xBase = chunkX * chunkWidth; - - for(int y = yBase; y < (yBase + chunkHeight); ++y) { - noiseVals.push_back(std::vector()); - - for(int x = xBase; x < (xBase + chunkWidth); ++x) { - noiseVals.back().push_back(gen(x, y)); - - } // for(int x = 0; x < width; ++x); - - } // for(int y = 0; y < width; ++y); - -} // void NoiseMap::genNormal(); - -void NoiseMap::genCombination() { - noiseVals.resize(chunkHeight); - for(auto& row : noiseVals) { - row.resize(chunkWidth, 0.0); - - } // for(auto& row : noiseVals); - - int totalFactor = 0; - - for(auto& comb : combinations) { - totalFactor += abs(comb.second); - - if(!(comb.first->isGenerated(chunkWidth, chunkHeight, chunkX, chunkY))) { - comb.first->generate(chunkWidth, chunkHeight, chunkX, chunkY); - - } // if(!(comb.first->isGenerated(chunkWidth, chunkHeight, chunkX, chunkY))); - - int xBase = chunkX * chunkWidth; - int yBase = chunkY * chunkHeight; - - for(int x = xBase; x < (xBase + chunkWidth); ++x) { - for(int y = yBase; y < (yBase + chunkHeight); ++y) { - noiseVals[y][x] += (comb.first->noiseVals[y][x] * comb.second); - - } // for(int y = 0; y < height; ++y); - - } // for(int x = 0; x < width; ++x); - - } // for(auto& comb : combinations); + seed(WG_DEF_SEED), + gridSizeX(WG_DEF_GRID), gridSizeY(WG_DEF_GRID), + octaves(WG_DEF_OCTAVES), + frequency(WG_DEF_FREQ), + persistence(WG_DEF_PERS), + lacunarity(WG_DEF_LACU) { - for(auto& row : noiseVals) { - for(auto& val : row) { - val /= totalFactor; +} // RandomNoiseMap::RandomNoiseMap(); - } // for(auto& val : row); +CombinationNoiseMap::CombinationNoiseMap() + : NoiseMap(true) { - } // for(auto& row : noiseVals); +} // CombinationNoiseMap::CombinationNoiseMap(); -} // void NoiseMap::genCombination(int width, int height); +CombinationNoiseMap* CombinationNoiseMap::add(NoiseMap* mapToAdd, int factor) { + combinations.push_back({mapToAdd, factor}); -bool NoiseMap::isGenerated(int chunkWidth, int chunkHeight, int chunkX, int chunkY) { - return generated && (chunkWidth == this->chunkWidth) - && (chunkHeight == this->chunkHeight) - && (chunkX == this->chunkX) - && (chunkY == this->chunkY); + return this; -} // bool NoiseMap::isGenerated(int chunkWidth, int chunkHeight, int chunkX, int chunkY); +} // CombinationNoiseMap* CombinationNoiseMap::add(NoiseMap* mapToAdd, int factor); WG_NS_END diff --git a/src/tiledef.cpp b/src/tiledef.cpp deleted file mode 100644 index feb8ee8..0000000 --- a/src/tiledef.cpp +++ /dev/null @@ -1,55 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// Copyright 2013 Samuel Sleight -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -///////////////////////////////////////////////////////////////////////////// -// Project: World Generation Library -// File: src/tiledef.cpp -// Author: Samuel Sleight -///////////////////////////////////////////////////////////////////////////// - -#include "inc/tiledef.hpp" - -WG_NS - -unsigned int TileDef::nextId = 0; - -TileDef::TileDef() - : id(nextId++) { - -} // TileDef::TileDef(); - -TileDef::~TileDef() { - for(auto& c : constraints) { - c.clear(); - - } // for(auto& c : constraints); - - constraints.clear(); - -} // TileDef::~TileDef(); - -bool TileDef::isValid(int chunkWidth, int chunkHeight, int chunkX, int chunkY, int x, int y) { - for(auto& c : constraints) { - if(!(c.isValid(chunkWidth, chunkHeight, chunkX, chunkY, x, y))) { - return false; - - } // if(!(c.isValid(chunkX, chunkY, x, y))); - - } // for(auto& c : constraints); - - return true; - -} // bool TileDef::isValid(int chunkX, int chunkY, int x, int y); - -WG_NS_END diff --git a/src/world.cpp b/src/world.cpp index 1d69986..41fb11e 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -19,51 +19,27 @@ ///////////////////////////////////////////////////////////////////////////// #include "inc/world.hpp" +#include "inc/noisemap.hpp" WG_NS -World::~World() { - for(auto& tDef : tileDefinitions) { - tDef.reset(); +World::World() + : chunkWidth(WG_DEF_CHUNK), chunkHeight(WG_DEF_CHUNK) { - } // for(auto& tDef : tileDefinitions); +} // World::World(); - tileDefinitions.clear(); +RandomNoiseMap* World::addRandomNoiseMap() { + noiseMaps.push_back(new RandomNoiseMap); -} // World::~World(); + return static_cast(noiseMaps.back()); -TileDef::Ptr World::newTile() { - tileDefinitions.push_back(TileDef::Ptr(new TileDef())); +} // RandomNoiseMap* World::addRandomNoiseMap(); - return tileDefinitions.back(); +CombinationNoiseMap* World::addCombinationNoiseMap() { + noiseMaps.push_back(new CombinationNoiseMap); -} // TileDef::Ptr World::newTile(); + return static_cast(noiseMaps.back()); -World::Ptr World::generate(int chunkX, int chunkY) { - mapGrid.clear(); - mapGrid.resize(chunkHeight); - for(auto& row : mapGrid) { - row.resize(chunkWidth, TileDef::nextId); - - } // for(auto& row : mapGrid); - - for(int y = 0; y < chunkHeight; ++y) { - for(int x = 0; x < chunkWidth; ++x) { - for(auto& tDef : tileDefinitions) { - if(tDef->isValid(chunkWidth, chunkHeight, chunkX, chunkY, x, y)) { - mapGrid[y][x] = tDef->id; - break; - - } // if(tDef->isValid(chunkX, chunkY, x, y)); - - } // for(auto& tDef : tileDefinitions); - - } // for(int y = 0; y < chunkHeight; ++y); - - } // for(int x = 0; x < chunkWidth; ++x); - - return shared_from_this(); - -} // World::Ptr World::generate(int chunkX, int chunkY); +} // CombinationNoiseMap* World::addCombinationNoiseMap(); WG_NS_END diff --git a/test.cpp b/test.cpp index 8914c08..21c3ad1 100644 --- a/test.cpp +++ b/test.cpp @@ -9,75 +9,17 @@ #include #include "src/inc/noisemap.hpp" -#include "src/inc/tiledef.hpp" #include "src/inc/world.hpp" int main(int argc, char* argv[]) { - // Seed random number gen. srand(time(NULL)); - // Create noise maps, with random seed - wg::NoiseMap::Ptr hmap1 = wg::NoiseMap::create() - ->setSeed(std::to_string(rand())) - ->setGridSize(0.05); + wg::World* w = (new wg::World())->setChunkSize(30); - wg::NoiseMap::Ptr hmap2 = wg::NoiseMap::create() - ->setSeed(std::to_string(rand())) - ->setGridSize(0.5); + wg::RandomNoiseMap* nMap1 = w->addRandomNoiseMap() + ->setSeed(std::to_string(rand())); - wg::NoiseMap::Ptr heightmap = wg::NoiseMap::combination() - ->add(hmap1, 20)->add(hmap2, 5); - - wg::NoiseMap::Ptr rainfall = wg::NoiseMap::create() - ->setSeed(std::to_string(rand())) - ->setGridSize(0.01); - - // Create world - wg::World::Ptr w = wg::World::create() - ->setChunkSize(40, 40); - - // Set tile definitions - wg::TileDef::Ptr tileWater = w->newTile()->addConstraint(heightmap < -0.3); - wg::TileDef::Ptr tileHighMnt = w->newTile()->addConstraint(heightmap > 0.55); - wg::TileDef::Ptr tileMnt = w->newTile()->addConstraint(heightmap > 0.2); - wg::TileDef::Ptr tileDesert = w->newTile()->addConstraint(rainfall < -0.3); - wg::TileDef::Ptr tilePlains = w->newTile(); - - // Generate the world (specifically the chunk of world at 0,0) - w->generate(0, 0); - - // Output world (looks nice on linux, windows will spew codes or something) - for(auto r : w->getMap()) { - for(auto t : r) { - if(t == tileWater->getId()) { - std::cout << ""; - - } // if(t == tileWater); - else if(t == tileMnt->getId()) { - std::cout << ""; - - } // else if(t == tileMnt); - else if(t == tileHighMnt->getId()) { - std::cout << ""; - - } // else if(t == tileHighMnt); - else if(t == tilePlains->getId()) { - std::cout << ""; - - } // else if(t == tilePlains); - else if(t == tileDesert->getId()) { - std::cout << ""; - - } // else if(t == tileDesert); - - std::cout << t << " "; - - } // for(auto t : r); - - std::cout << "\n"; - - } // for(auto r : w->getTileMap()); - - std::cout << ""; + wg::CombinationNoiseMap* nMap2 = w->addCombinationNoiseMap() + ->add(nMap1, 10); } // int main(int argc, char* argv[]);