summaryrefslogtreecommitdiffstats
path: root/src/game/server/world/random_tick.cc
blob: 6e6c65ae98d6d3e5df1121fb7e72ef12d9c001bc (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
// SPDX-License-Identifier: BSD-2-Clause
// Copyright (c) 2025 Kirill Dmitrievich
// File: random_tick.cc
// Description: Voxel random ticking

#include "server/pch.hh"

#include "server/world/random_tick.hh"

#include "core/config/number.hh"

#include "core/io/config_map.hh"

#include "shared/world/chunk.hh"
#include "shared/world/dimension.hh"
#include "shared/world/voxel.hh"

#include "shared/coord.hh"

#include "server/globals.hh"

static config::Int random_tick_speed(2, 1, 1000);
static std::mt19937_64 random_source;

void random_tick::init(void)
{
    globals::server_config.add_value("world.random_tick_speed", random_tick_speed);

    random_source.seed(std::random_device {}());
}

void random_tick::tick(const chunk_pos& cpos, Chunk* chunk)
{
    assert(chunk);

    for(int i = 0; i < random_tick_speed.get_value(); ++i) {
        auto voxel_index = random_source() % CHUNK_VOLUME;
        auto lpos = coord::to_local(voxel_index);
        auto vpos = coord::to_voxel(cpos, lpos);

        if(auto voxel = chunk->get_voxel(lpos)) {
            voxel->on_tick(chunk->get_dimension(), vpos);
        }
    }
}