Skip to content

Commit

Permalink
fix random
Browse files Browse the repository at this point in the history
  • Loading branch information
averrin committed Sep 20, 2017
1 parent 16d83af commit 32b13de
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
5 changes: 2 additions & 3 deletions include/mapgen/MapGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "noise/noiseutils.h"
#include <memory>
#include <functional>
#include <random>

//TODO: fix it and use header
#include "../../src/Map.cpp"
Expand Down Expand Up @@ -102,10 +103,8 @@ class MapGenerator {
template<typename Iter>
Iter select_randomly(Iter start, Iter end);

template<typename Iter>
Iter select_randomly_seed(Iter start, Iter end, int s);

void genRandomSites(std::vector<sf::Vector2<double> >& sites, sf::Rect<double>& bbox, unsigned int dx, unsigned int dy, unsigned int numSites);
std::mt19937 _gen;
};

#endif
13 changes: 2 additions & 11 deletions src/MapGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ bool clusterOrdered(Cluster *s1, Cluster *s2) {

template <typename Iter>
Iter MapGenerator::select_randomly(Iter start, Iter end) {
std::mt19937 gen(_seed);
std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1);
std::advance(start, dis(gen));
return start;
}

template <typename Iter>
Iter MapGenerator::select_randomly_seed(Iter start, Iter end, int s) {
std::mt19937 gen(s);
std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1);
std::advance(start, dis(gen));
return start;
Expand All @@ -68,6 +59,7 @@ MapGenerator::MapGenerator(int w, int h) : _w(w), _h(h) {
currentOperation = "";
temperature = DEFAULT_TEMPERATURE;
map = nullptr;
_gen = std::mt19937(_seed);
}

double getDistance(Point p, Point p2) {
Expand Down Expand Up @@ -277,8 +269,7 @@ void MapGenerator::makeCaves() {
int n = c->regions.size() / 50 + 1;

while (n != 0) {
Region *r = *select_randomly_seed(c->regions.begin(), c->regions.end(),
std::clock());
Region *r = *select_randomly(c->regions.begin(), c->regions.end());
if (r->location != nullptr) {
continue;
}
Expand Down
14 changes: 7 additions & 7 deletions src/names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ std::vector<std::string> city_first_names (_city_first_names, _city_first_names
std::vector<std::string> city_second_names (_city_second_names, _city_second_names + sizeof(_city_second_names) / sizeof(_city_second_names[0]) );


std::mt19937 gen(std::clock());
template<typename Iter>
Iter select_randomly(Iter start, Iter end, int s) {
std::mt19937 gen(s);
Iter select_randomly(Iter start, Iter end) {
std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1);
std::advance(start, dis(gen));
return start;
}

std::string generateRiverName() {
std::string fn = *select_randomly(river_first_names.begin(), river_first_names.end(), std::clock());
std::string sn = *select_randomly(river_second_names.begin(), river_second_names.end(), std::clock());
std::string fn = *select_randomly(river_first_names.begin(), river_first_names.end());
std::string sn = *select_randomly(river_second_names.begin(), river_second_names.end());
return fn+" "+sn;
}

std::string generateLandName() {
return *select_randomly(island_first_names.begin(), island_first_names.end(), std::clock()) + " " + *select_randomly(island_second_names.begin(), island_second_names.end(), std::clock());
return *select_randomly(island_first_names.begin(), island_first_names.end()) + " " + *select_randomly(island_second_names.begin(), island_second_names.end());
}

std::string generateSeaName() {
return *select_randomly(sea_first_names.begin(), sea_first_names.end(), std::clock()) + " " + *select_randomly(sea_second_names.begin(), sea_second_names.end(), std::clock());
return *select_randomly(sea_first_names.begin(), sea_first_names.end()) + " " + *select_randomly(sea_second_names.begin(), sea_second_names.end());
}

std::string generateCityName() {
auto name = *select_randomly(city_first_names.begin(), city_first_names.end(), std::clock()) + *select_randomly(city_second_names.begin(), city_second_names.end(), std::clock());
auto name = *select_randomly(city_first_names.begin(), city_first_names.end()) + *select_randomly(city_second_names.begin(), city_second_names.end());
name[0] = toupper(name[0]);
return name;
}

0 comments on commit 32b13de

Please sign in to comment.