aboutsummaryrefslogtreecommitdiffstats
path: root/2
blob: cc0ebef22ede574e70361c46ad3a202730654f83 (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
58
59
60
61
62
63
64
#include "world/tectonicphase.h"
#include "boxii/input.h"

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);

    for(int i = 0; i < TECTONIC_PLATES; i++){
        m_plates[i].x = xDist(map->getRandom()) * map->getWidth();
        m_plates[i].y = yDist(map->getRandom()) * map->getHeight();
        m_plates[i].continental = rand() % 2;

        printf("Plate %i : %i %i (%i)\n\r", i, m_plates[i].x, m_plates[i].y, m_plates[i].continental);
        Boxii::Input::getch(true);
    }

    rasterize(map);
}

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

void
TectonicPhase::rasterize(std::shared_ptr<WorldMap> map)
{
    int w = map->getWidth();
    int h = map->getHeight();

    for(int y = 0; y < h; y++)
    for(int x = 0; x < w; x++)
    {
        Plate *closest = nullptr;
        float cdist = INT_MAX;

        //Iterate through plates
        for(int i = 0; i < TECTONIC_PLATES; i++){
            Plate *plate = &m_plates[i];

            float d = dist(x, y, plate->x, plate->y);

            if(d < cdist){
                closest = plate;
                cdist = d;
            }
        }

        if(closest == nullptr) {
            printf("Skipping tile with no clear closest?\n");
            Boxii::Input::getch(true);
            continue;
        }

        if(closest->continental) {
            map->setProperties(x, y, 1024, 0, 0);
        }else{
            map->setProperties(x, y, 0, 0, 0);
        }
    }
}