summaryrefslogtreecommitdiffstats
path: root/game/shared/world/dimension.cc
blob: 2d193fcb25f197f206b725c883fa39ca1fe31480 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "shared/pch.hh"

#include "shared/world/dimension.hh"

#include "shared/world/chunk.hh"

#include "shared/coord.hh"
#include "shared/globals.hh"

world::Dimension::Dimension(std::string_view name, float gravity)
{
    m_name = name;
    m_gravity = gravity;
}

world::Dimension::~Dimension(void)
{
    for(const auto it : m_chunkmap)
        delete it.second;
    entities.clear();
    chunks.clear();
}

std::string_view world::Dimension::get_name(void) const
{
    return m_name;
}

float world::Dimension::get_gravity(void) const
{
    return m_gravity;
}

world::Chunk* world::Dimension::create_chunk(const chunk_pos& cpos)
{
    auto it = m_chunkmap.find(cpos);

    if(it != m_chunkmap.cend()) {
        // Chunk already exists
        return it->second;
    }

    auto entity = chunks.create();
    auto chunk = new Chunk(entity, this);

    auto& component = chunks.emplace<ChunkComponent>(entity);
    component.chunk = chunk;
    component.cpos = cpos;

    ChunkCreateEvent event;
    event.dimension = this;
    event.chunk = chunk;
    event.cpos = cpos;

    globals::dispatcher.trigger(event);

    return m_chunkmap.insert_or_assign(cpos, std::move(chunk)).first->second;
}

world::Chunk* world::Dimension::find_chunk(entt::entity entity) const
{
    if(chunks.valid(entity)) {
        return chunks.get<ChunkComponent>(entity).chunk;
    }
    else {
        return nullptr;
    }
}

world::Chunk* world::Dimension::find_chunk(const chunk_pos& cpos) const
{
    auto it = m_chunkmap.find(cpos);

    if(it != m_chunkmap.cend()) {
        return it->second;
    }
    else {
        return nullptr;
    }
}

void world::Dimension::remove_chunk(entt::entity entity)
{
    if(chunks.valid(entity)) {
        auto& component = chunks.get<ChunkComponent>(entity);
        m_chunkmap.erase(component.cpos);
        chunks.destroy(entity);
    }
}

void world::Dimension::remove_chunk(const chunk_pos& cpos)
{
    auto it = m_chunkmap.find(cpos);

    if(it != m_chunkmap.cend()) {
        chunks.destroy(it->second->get_entity());
        m_chunkmap.erase(it);
    }
}

void world::Dimension::remove_chunk(Chunk* chunk)
{
    if(chunk) {
        const auto& component = chunks.get<ChunkComponent>(chunk->get_entity());
        m_chunkmap.erase(component.cpos);
        chunks.destroy(chunk->get_entity());
    }
}

voxel_id world::Dimension::get_voxel(const voxel_pos& vpos) const
{
    auto cpos = coord::to_chunk(vpos);
    auto lpos = coord::to_local(vpos);

    if(auto chunk = find_chunk(cpos)) {
        return chunk->get_voxel(lpos);
    }
    else {
        return NULL_VOXEL_ID;
    }
}

voxel_id world::Dimension::get_voxel(const chunk_pos& cpos, const local_pos& lpos) const
{
    // This allows accessing get_voxel with negative
    // local coordinates that usually would result in an
    // out-of-range values; this is useful for per-voxel update logic
    return get_voxel(coord::to_voxel(cpos, lpos));
}

bool world::Dimension::set_voxel(voxel_id voxel, const voxel_pos& vpos)
{
    auto cpos = coord::to_chunk(vpos);
    auto lpos = coord::to_local(vpos);

    if(auto chunk = find_chunk(cpos)) {
        chunk->set_voxel(voxel, lpos);

        VoxelSetEvent event;
        event.dimension = this;
        event.cpos = cpos;
        event.lpos = lpos;
        event.voxel = voxel;
        event.chunk = chunk;

        globals::dispatcher.trigger(event);

        return true;
    }

    return false;
}

bool world::Dimension::set_voxel(voxel_id voxel, const chunk_pos& cpos, const local_pos& lpos)
{
    // This allows accessing set_voxel with negative
    // local coordinates that usually would result in an
    // out-of-range values; this is useful for per-voxel update logic
    return set_voxel(voxel, coord::to_voxel(cpos, lpos));
}

void world::Dimension::init(io::ConfigMap& config)
{
}

void world::Dimension::init_late(std::uint64_t global_seed)
{
}

bool world::Dimension::generate(const chunk_pos& cpos, VoxelStorage& voxels)
{
    return false;
}