aboutsummaryrefslogtreecommitdiffstats
path: root/src/world/tectonicphase.cpp
blob: 5e8ce96564547e707fbd779860ebc1d7dee61550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "world/tectonicphase.h"
#include "boxii/input.h"

int TectonicPhase::m_plateCount = 10;

TectonicPhase::TectonicPhase(std::shared_ptr<GenerationPhase> prev, std::shared_ptr<WorldMap> map) :
    GenerationPhase(prev, map)
{
    std::uniform_real_distribution<float> xDist(0.f, 1.f);
    std::uniform_real_distribution<float> yDist(0.f, 1.f);
    std::uniform_int_distribution<int> cDist(0, 1);

    m_plates.reserve(m_plateCount);

    for(int i = 0; i < m_plateCount; i++){
        m_plates.push_back(Plate());
        m_plates[i].x = xDist(map->getRandom()) * map->getWidth();
        m_plates[i].y = yDist(map->getRandom()) * map->getHeight();
        m_plates[i].continental = cDist(map->getRandom());
    }

    rasterize(map);
}

static float
dist(float x1, float y1, float x2, float y2)
{
    return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

void
TectonicPhase::rasterize(std::shared_ptr<WorldMap> map)
{
    for(int y = 0; y < map->getHeight(); y++){
        for(int x = 0; x < map->getWidth(); x++){

            int closest = 0;
            float cdist = INT_MAX;

            //Iterate through plates
            for(int i = 0; i < m_plateCount; i++){
                float d = dist(static_cast<float>(x), static_cast<float>(y), m_plates[i].x, m_plates[i].y);
            
                if(d < cdist){
                    closest = i;
                    cdist = d;
                }
            }
            
            if(m_plates[closest].continental){
                map->setProperties(x, y, 600, 0, 0);
            }else{
                map->setProperties(x, y, 512, 88, 0);
            }
        }
    }
}