summaryrefslogtreecommitdiffstats
path: root/game/shared/world/voxel_registry.cc
blob: 4c2f360279f88d5b5754614ed58db15f94f90f65 (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
#include "shared/pch.hh"

#include "shared/world/voxel_registry.hh"

static std::uint64_t registry_checksum = 0U;
emhash8::HashMap<std::string, voxel_id> world::voxel_registry::names;
std::vector<std::shared_ptr<world::Voxel>> world::voxel_registry::voxels;

static void recalculate_checksum(void)
{
    registry_checksum = 0U;

    for(const auto& voxel : world::voxel_registry::voxels) {
        registry_checksum = voxel->calculate_checksum(registry_checksum);
    }
}

world::Voxel* world::voxel_registry::register_voxel(const Voxel& voxel_template)
{
    assert(voxel_template.get_name().size());
    assert(nullptr == find(voxel_template.get_name()));

    const auto id = static_cast<voxel_id>(voxels.size());

    auto voxel = voxel_template.clone();
    voxel->set_id(id);

    names.emplace(std::string(voxel_template.get_name()), id);
    voxels.push_back(std::move(voxel));

    recalculate_checksum();

    return voxels.back().get();
}

world::Voxel* world::voxel_registry::find(std::string_view name)
{
    const auto it = names.find(name);

    if(it == names.end()) {
        return nullptr;
    }

    return voxels[it->second].get();
}

world::Voxel* world::voxel_registry::find(voxel_id id)
{
    if(id >= voxels.size()) {
        return nullptr;
    }

    return voxels[id].get();
}

void world::voxel_registry::purge(void)
{
    registry_checksum = 0U;
    voxels.clear();
    names.clear();
}

std::uint64_t world::voxel_registry::get_checksum(void)
{
    return registry_checksum;
}