Skip to content

Commit

Permalink
poi
Browse files Browse the repository at this point in the history
  • Loading branch information
averrin committed Sep 12, 2017
1 parent 8061cb3 commit eecdf74
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 3 deletions.
3 changes: 3 additions & 0 deletions include/mapgen/MapGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class MapGenerator {
void calcHumidity();
void calcTemp();
void simplifyRivers();
void makeBorders();
void makeMinerals();
int _seed;
VoronoiDiagramGenerator _vdg;
int _pointsCount;
Expand All @@ -75,6 +77,7 @@ class MapGenerator {

module::Perlin _perlin;
utils::NoiseMap _heightMap;
utils::NoiseMap _mineralsMap;
std::vector<Region*>* _regions;
std::string _terrainType;

Expand Down
6 changes: 6 additions & 0 deletions include/mapgen/Region.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Region {
float humidity;
Cell* cell;
float temperature;
float minerals;
float nice;
private:
PointList _verticies;
HeightMap _heights;
Expand All @@ -40,6 +42,10 @@ struct Cluster {
Biom biom;
bool hasRiver;
bool isLand;
Point* center;
PointList border;
std::vector<Region*> resourcePoints;
std::vector<Region*> goodPoints;
};

#endif
80 changes: 79 additions & 1 deletion src/MapGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,53 @@ void MapGenerator::update() {
calcHumidity();
calcTemp();

makeMinerals();
makeBorders();
makeFinalRegions();

makeClusters();

ready = true;
}

void MapGenerator::makeMinerals() {
currentOperation = "Search for minerals...";
utils::NoiseMapBuilderPlane heightMapBuilder;
heightMapBuilder.SetDestNoiseMap(_mineralsMap);

module::Billow minerals;
minerals.SetSeed(_seed+5);
heightMapBuilder.SetSourceModule(minerals);

heightMapBuilder.SetDestSize(_w, _h);
heightMapBuilder.SetBounds(10.0, 20.0, 10.0, 20.0);
heightMapBuilder.Build();
}

void MapGenerator::makeBorders() {
for (auto c: megaClusters) {
for (auto r: c->regions) {
if (!r->border) {
continue;
}

Cell* c = r->cell;
for (auto n: c->getNeighbors()) {
Region* rn = _cells[n];
if (rn->biom.name != r->biom.name){
for (auto e: n->getEdges()) {
if (c->pointIntersection(e->startPoint()->x, e->startPoint()->y) == 0) {
r->megaCluster->border.push_back(e->startPoint());
}
}
}
}
}
//TODO: sort points by distance;
//TODO: do something with inner points
// std::sort(r->megaCluster->border.begin(), r->megaCluster->border.end(), borderOrdered);
}
}

void MapGenerator::setMapTemplate(const char* templateName) {
//TODO: make enum
_terrainType = std::string(templateName);
Expand Down Expand Up @@ -355,8 +396,11 @@ void MapGenerator::makeFinalRegions() {
currentOperation = "Making forrests and deserts...";
for (auto r : *_regions) {
if (r->biom.name == LAKE.name) {
r->minerals = 0;
continue;
}
r->minerals = _mineralsMap.GetValue(r->site->x, r->site->y);
r->minerals = r->minerals > 0 ? r->minerals : 0;
float ht = r->getHeight(r->site);
Biom b = BIOMS[0];
for (int i = 0; i < int(BIOMS.size()); i++)
Expand All @@ -372,6 +416,40 @@ void MapGenerator::makeFinalRegions() {
}
}
r->biom = b;
float hc = (1.f-std::abs(r->humidity-0.8f));
hc = hc <= 0 ? 0 : hc/3.f;
float hic = (1.f-std::abs(r->getHeight(r->site)-0.7f));
hic = hic <= 0 ? 0 : hic/3.f;
float tc = (1.f-std::abs(r->temperature-temperature*2.f/3.f));
tc = tc <= 0 ? 0 : tc/3.f;

r->nice = hc + hic + tc;
}

for (auto cluster : megaClusters){
if (!cluster->isLand) {
continue;
}
for (auto r : cluster->regions) {
Cell* c = r->cell;
if(c == nullptr) {
continue;
}
auto ns = c->getNeighbors();
if (std::count_if(ns.begin(), ns.end(), [&](Cell* oc){
Region* reg = _cells[oc];
return reg->minerals > r->minerals;
})==0 && r->minerals != 0){
cluster->resourcePoints.push_back(r);
}

if (std::count_if(ns.begin(), ns.end(), [&](Cell* oc){
Region* reg = _cells[oc];
return reg->nice >= r->nice;
})==0 && r->biom.name != LAKE.name){
cluster->goodPoints.push_back(r);
}
}
}
}

Expand Down
67 changes: 65 additions & 2 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

class Application {
std::vector<sf::ConvexShape> polygons;
std::vector<sf::CircleShape> poi;
std::vector<sf::ConvexShape> infoPolygons;
std::vector<sf::Vertex> verticies;
sf::Color bgColor;
Expand Down Expand Up @@ -51,6 +52,7 @@ class Application {
bool getScreenshot = false;
float temperature;
bool temp = false;
bool minerals = false;

public:
Application() {
Expand Down Expand Up @@ -133,6 +135,10 @@ class Application {
temp = !temp;
updateVisuals();
break;
case sf::Keyboard::M:
minerals = !minerals;
updateVisuals();
break;
case sf::Keyboard::I:
info = !info;
break;
Expand Down Expand Up @@ -242,6 +248,11 @@ class Application {
updateVisuals();
}
ImGui::SameLine(200);
if (ImGui::Checkbox("Minerals", &minerals)) {
infoPolygons.clear();
updateVisuals();
}

if (ImGui::Checkbox("Simplify rivers", &simplifyRivers)) {
mapgen->simpleRivers = simplifyRivers;
regen();
Expand Down Expand Up @@ -336,6 +347,16 @@ class Application {
Cluster *cluster = currentRegion->cluster;

int i = 0;
// sf::ConvexShape polygon;
// polygon.setPointCount(cluster->megaCluster->border.size());
// for (auto p: cluster->megaCluster->border) {
// polygon.setPoint(i, sf::Vector2f(p->x, p->y));
// i++;
// }
// polygon.setFillColor(sf::Color::Transparent);
// polygon.setOutlineColor(sf::Color::Red);
// polygon.setOutlineThickness(3);
// infoPolygons.push_back(polygon);
for (std::vector<Region *>::iterator
it = cluster->megaCluster->regions.begin();
it < cluster->megaCluster->regions.end(); it++, i++) {
Expand Down Expand Up @@ -444,6 +465,7 @@ class Application {
window->draw(polygons[i]);
}

drawRivers();

sf::Vector2u windowSize = window->getSize();
cachedMap.create(windowSize.x, windowSize.y);
Expand Down Expand Up @@ -494,7 +516,12 @@ class Application {
window->clear(bgColor); // fill background with color

drawMap();
drawRivers();

if(info){
for (auto p: poi) {
window->draw(p);
}
}

sf::Vector2u windowSize = window->getSize();
sf::Text mark("Mapgen by Averrin", sffont);
Expand Down Expand Up @@ -527,7 +554,13 @@ class Application {
texture.update(*window);
sf::Image screenshot = texture.copyToImage();
char s[100];
sprintf(s, "%d.png", seed);
if (!hum && !temp) {
sprintf(s, "%d.png", seed);
} else if (hum) {
sprintf(s, "%d-hum.png", seed);
} else {
sprintf(s, "%d-temp.png", seed);
}
screenshot.saveToFile(s);
char l[255];
sprintf(l, "Screenshot created: %s\n", s);
Expand All @@ -544,7 +577,26 @@ class Application {
void updateVisuals() {
log.AddLog("Update geometry\n");
polygons.clear();
poi.clear();
verticies.clear();

for (auto mc: mapgen->megaClusters) {
for (auto p: mc->resourcePoints) {
float rad = p->minerals * 3 + 1;
sf::CircleShape poiShape(rad);
poiShape.setFillColor(sf::Color::Blue);
poiShape.setPosition(sf::Vector2f(p->site->x - rad/2.f, p->site->y - rad/2.f));
poi.push_back(poiShape);
}
for (auto p: mc->goodPoints) {
float rad = p->minerals * 3 + 1;
sf::CircleShape poiShape(rad);
poiShape.setFillColor(sf::Color::Red);
poiShape.setPosition(sf::Vector2f(p->site->x - rad/2.f, p->site->y - rad/2.f));
poi.push_back(poiShape);
}
}

int i = 0;
std::vector<Region *> *regions = mapgen->getRegions();
polygons.reserve(regions->size());
Expand All @@ -553,6 +605,7 @@ class Application {
it < regions->end(); it++, i++) {

Region *region = (*regions)[i];

sf::ConvexShape polygon;
PointList points = region->getPoints();
polygon.setPointCount(points.size());
Expand Down Expand Up @@ -592,6 +645,16 @@ class Application {
color[2] = 1.f;
}

if (minerals) {
sf::Color col(region->biom.color);
col.g = 255 * (region->minerals) / 1.2;
// col.a = 20 + 255 * (region->minerals + 1.6) / 3.2;
col.b = col.b / 3;
col.r = col.g / 3;
polygon.setFillColor(col);
color[0] = 1.f;
}

if (hum && region->humidity != 1) {
sf::Color col(region->biom.color);
col.b = 255 * region->humidity;
Expand Down
2 changes: 2 additions & 0 deletions src/infoWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ void infoWindow(sf::RenderWindow* window, Region* currentRegion) {
}

ImGui::Text("Region: %p", currentRegion);
ImGui::Text("Minerals: %f", currentRegion->minerals);
ImGui::Text("Goodness: %f", currentRegion->nice);
ImGui::Text("Cluster: %p", cluster);
ImGui::Text("Cluster size: %zu", cluster->regions.size());
ImGui::Text("Mega Cluster: %s", currentRegion->megaCluster->name.c_str());
Expand Down

0 comments on commit eecdf74

Please sign in to comment.