Skip to content

Commit

Permalink
background generating and terrain type selection (test)
Browse files Browse the repository at this point in the history
  • Loading branch information
averrin committed Sep 9, 2017
1 parent 60ffdbe commit d4b2bb0
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 55 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ add_executable(${EXECUTABLE_NAME}
src/Region.cpp
)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-reorder")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-variable")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-but-set-variable")
Expand Down Expand Up @@ -76,7 +79,7 @@ if (OPENGL_FOUND)
target_link_libraries(${EXECUTABLE_NAME} ${OPENGL_LIBRARIES})
endif()

target_link_libraries(${EXECUTABLE_NAME} voronoi noise imgui sw)
target_link_libraries(${EXECUTABLE_NAME} voronoi noise imgui sw Threads::Threads)

target_compile_features(mapgen PRIVATE cxx_delegating_constructors)

Expand Down
5 changes: 5 additions & 0 deletions include/mapgen/MapGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ class MapGenerator {
std::vector<sf::ConvexShape>* getPolygons();
void seed();
std::vector<Region*>* getRegions();
void setMapTemplate(const char* t);

std::vector<River*> rivers;

std::vector<Cluster*> clusters;
std::vector<MegaCluster*> megaClusters;
bool simpleRivers;
bool ready;
std::string currentOperation;

private:
void regenHeight();
Expand All @@ -49,6 +52,7 @@ class MapGenerator {
void makeRiver(Cell* c);
void calcHumidity();
void simplifyRivers();
// module::Module& getSourceModule();
int _seed;
VoronoiDiagramGenerator _vdg;
int _pointsCount;
Expand All @@ -68,6 +72,7 @@ class MapGenerator {
module::Perlin _perlin;
utils::NoiseMap _heightMap;
std::vector<Region*>* _regions;
std::string _terrainType;

template<typename Iter>
Iter select_randomly(Iter start, Iter end);
Expand Down
67 changes: 58 additions & 9 deletions src/MapGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ Iter MapGenerator::select_randomly(Iter start, Iter end, int s) {
MapGenerator::MapGenerator(int w, int h): _w(w), _h(h) {
_vdg = VoronoiDiagramGenerator();
_pointsCount = 10000;
_octaves = 3;
_octaves = 4;
_freq = 0.3;
_relax = DEFAULT_RELAX;
_regions = new std::vector<Region*>();
simpleRivers = true;
_terrainType = "basic";
currentOperation = "";
}

void MapGenerator::build() {
Expand All @@ -79,6 +81,7 @@ void MapGenerator::relax() {
}

void MapGenerator::simplifyRivers() {
currentOperation = "Simplify rivers...";
for (auto r : rivers) {
PointList* rvr = r->points;
PointList sr;
Expand Down Expand Up @@ -180,6 +183,7 @@ void MapGenerator::setPointCount(int c) {


void MapGenerator::update() {
ready = false;
regenHeight();
regenDiagram();
regenRegions();
Expand All @@ -190,40 +194,78 @@ void MapGenerator::update() {
}
calcHumidity();
regenMegaClusters();
ready = true;
}

void MapGenerator::forceUpdate() {
// regenHeight();
ready = false;
regenDiagram();
regenRegions();
regenClusters();
regenRivers();
simplifyRivers();
calcHumidity();
regenMegaClusters();
ready = true;
}

void MapGenerator::setMapTemplate(const char* templateName) {
_terrainType = std::string(templateName);
}

void MapGenerator::regenHeight() {
currentOperation = "Making mountains and seas...";
utils::NoiseMapBuilderPlane heightMapBuilder;
heightMapBuilder.SetDestNoiseMap(_heightMap);

_perlin.SetSeed(_seed);
_perlin.SetOctaveCount(_octaves);
_perlin.SetFrequency(_freq);
utils::NoiseMapBuilderPlane heightMapBuilder;
heightMapBuilder.SetSourceModule(_perlin);
heightMapBuilder.SetDestNoiseMap(_heightMap);

module::Perlin terrainType;
module::RidgedMulti mountainTerrain;
module::Select finalTerrain;

if (_terrainType == "archipelago") {

terrainType.SetFrequency (0.8);
terrainType.SetPersistence (0.5);

terrainType.SetSeed(_seed);
mountainTerrain.SetSeed(_seed);

// module::ScaleBias flatTerrain;
// flatTerrain.SetSourceModule (0, _perlin);
// flatTerrain.SetScale (0.025);
// flatTerrain.SetBias (-0.75);

finalTerrain.SetSourceModule (0, _perlin);
finalTerrain.SetSourceModule (1, mountainTerrain);
finalTerrain.SetControlModule (terrainType);
finalTerrain.SetBounds (0.0, 100.0);
finalTerrain.SetEdgeFalloff (0.125);
heightMapBuilder.SetSourceModule(terrainType);

} else {
heightMapBuilder.SetSourceModule(_perlin);
}

heightMapBuilder.SetDestSize(_w, _h);
heightMapBuilder.SetBounds(0.0, 10.0, 0.0, 10.0);
heightMapBuilder.Build();
std::cout << "Height generation finished\n" << std::flush;
}

void MapGenerator::makeRiver(Cell* c) {
currentOperation = "Making rivers...";
std::vector<Cell*> visited;
printf("First cell: %p\n", c);
// printf("First cell: %p\n", c);
Region* r = _cells[c];
printf("First biom: %s\n", r->biom.name.c_str());
// printf("First biom: %s\n", r->biom.name.c_str());
// r->biom = MARK;
float z = r->getHeight(r->site);
printf("First vert: %f\n", z);
// printf("First vert: %f\n", z);
River *rvr = new River();

std::string fn = *select_randomly(river_first_names.begin(), river_first_names.end(), std::clock());
Expand All @@ -244,7 +286,7 @@ void MapGenerator::makeRiver(Cell* c) {
}

int count = 0;
while (z > 0.f && count < 100) {
while (z >= -0.01 && count < 100) {
std::vector<Cell*> n = c->getNeighbors();
Cell* end;
for (Cell* c2 : n) {
Expand Down Expand Up @@ -288,9 +330,10 @@ void MapGenerator::makeRiver(Cell* c) {
}

void MapGenerator::regenRivers() {
currentOperation = "Making rivers...";
rivers.clear();
for (auto cluster : clusters){
if (cluster->biom.name == "Snow" || cluster->biom.name == "Rock") {
if ((cluster->biom.name == "Snow" || cluster->biom.name == "Rock") && cluster->regions.size() > 10) {
Cell* c = cellsMap[*select_randomly(cluster->regions.begin(), cluster->regions.end())];
if (c != nullptr) {
makeRiver(c);
Expand All @@ -300,6 +343,7 @@ void MapGenerator::regenRivers() {
}

void MapGenerator::regenRegions() {
currentOperation = "Splitting nothing...";
_cells.clear();
_regions->clear();
_regions->reserve(_diagram->cells.size());
Expand Down Expand Up @@ -356,6 +400,7 @@ bool isDiscard(const Cluster* c)
}

void MapGenerator::calcHumidity() {
currentOperation = "Making world moist...";
for (auto r : *_regions) {
if(r->hasRiver) {
r->humidity += 0.1;
Expand All @@ -376,6 +421,7 @@ void MapGenerator::calcHumidity() {
}

void MapGenerator::regenMegaClusters() {
currentOperation = "Finding far lands...";
megaClusters.clear();
std::map<Cluster*,MegaCluster*> _megaClusters;
for (auto c : clusters) {
Expand Down Expand Up @@ -461,6 +507,7 @@ void MapGenerator::regenMegaClusters() {
}

void MapGenerator::regenClusters() {
currentOperation = "Meeting with neighbors...";
clusters.clear();
cellsMap.clear();
std::map<Cell*,Cluster*> _clusters;
Expand Down Expand Up @@ -568,11 +615,13 @@ int MapGenerator::getRelax() {
}

void MapGenerator::regenDiagram() {
currentOperation = "Making nothing...";
_bbox = sf::Rect<double>(0,0,_w, _h);
_sites = new std::vector<sf::Vector2<double>>();
genRandomSites(*_sites, _bbox, _w, _h, _pointsCount);
_diagram.reset(_vdg.compute(*_sites, _bbox));
for (int n = 0; n < _relax; n++) {
currentOperation = "Relaxing...";
makeRelax();
}
delete _sites;
Expand Down
Loading

0 comments on commit d4b2bb0

Please sign in to comment.