aboutsummaryrefslogtreecommitdiffstats
path: root/src/world/map.cpp
blob: 48701d8e661962a5dab86029f9745a5f3381f3e5 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "world/map.h"
#include "world/tectonicphase.h"

#include "boxii/input.h"

WorldMap::WorldMap(int w, int h, int seed) :
    m_width(w), m_height(h), m_seed(seed),
    m_random(seed)
{
    m_landElevation = new float[w * h];
    m_waterElevation = new float[w * h];
    m_vegetation = new float[w * h];
}

WorldMap::~WorldMap()
{
    delete[] m_landElevation;
    delete[] m_waterElevation;
    delete[] m_vegetation;
}

void
WorldMap::begin(std::shared_ptr<WorldMap> self)
{
    std::shared_ptr<GenerationPhase> phase1 = std::make_shared<TectonicPhase>(nullptr, self);
}

void
WorldMap::draw(std::shared_ptr<Boxii::Window> window, int zoom)
{
    //TODO: Zoom
    (void)zoom;

    int cx, cy;
    int sx, sy;

    window->getCenter(cx, cy);
    sx = cx - (m_width / 2);
    sy = cy - (m_height / 2);

    for(int y = 0; y < m_height; y++)
    for(int x = 0; x < m_width; x++)
    {
        int index = x + (y * m_width);

        unsigned int r = 256;
        unsigned int g = 256;
        unsigned int b = 256;
        unsigned int color;

        float landElevation = m_landElevation[index];
        float waterElevation = m_waterElevation[index];

        float landScale = std::clamp(landElevation / 1024.f, 0.f, 1.f);
        float waterScale = std::clamp(waterElevation / 6.f, 0.f, 1.f);

        r *= 1.f - waterScale;
        g *= 1.f - waterScale;

        r *= landScale;
        g *= landScale;
        b *= landScale;

        color = ((r & 0xFF) << 16) + ((g & 0xFF) << 8) + (b & 0xFF);
        //window->plot(sx + x, sy + y, ' ', color, color);
        window->plot(x, y, ' ', color, color);
    }
}

void
WorldMap::getProperties(int x, int y, float &le, float &we, float &v)
{
    int index = x + (y * m_width);
    if(index >= m_width * m_height) return;

    le = m_landElevation[index];
    we = m_waterElevation[index];
    v = m_vegetation[index];
}

void 
WorldMap::setProperties(int x, int y, float le, float we, float v)
{
    int index = x + (y * m_width);
    if(index >= m_width * m_height)
        return;

    m_landElevation[index] = le;
    m_waterElevation[index] = we;
    m_vegetation[index] = v;
}