summaryrefslogtreecommitdiffstats
path: root/src/game/server/main.cc
blob: 7c70ae1c92ae391677a5081025478201cb5aef85 (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
// SPDX-License-Identifier: BSD-2-Clause
// Copyright (c) 2025 Kirill Dmitrievich
// File: main.cc
// Description: Entry point

#include "server/pch.hh"

#include "core/config/number.hh"

#include "core/io/cmdline.hh"
#include "core/io/config_map.hh"

#include "core/math/constexpr.hh"

#include "core/resource/image.hh"
#include "core/resource/resource.hh"

#include "core/utils/epoch.hh"

#include "core/threading.hh"
#include "core/version.hh"

#include "shared/game.hh"
#include "shared/protocol.hh"

#include "server/game.hh"
#include "server/globals.hh"

static config::Unsigned server_tickrate(protocol::TICKRATE, 10U, 300U);

static void on_termination_signal(int)
{
    spdlog::warn("server: received termination signal");
    globals::is_running = false;
}

int main(int argc, char** argv)
{
    cmdline::create(argc, argv);

    shared_game::init(argc, argv, "voxelius-server.log");

    spdlog::info("Voxelius Server {}", version::full);

    globals::fixed_frametime = 0.0f;
    globals::fixed_frametime_avg = 0.0f;
    globals::fixed_frametime_us = 0;
    globals::fixed_framecount = 0;

    globals::curtime = utils::unix_microseconds();

    globals::is_running = true;

    std::signal(SIGINT, &on_termination_signal);
    std::signal(SIGTERM, &on_termination_signal);

    Image::register_resource();

    server_game::init();

    threading::init();

    globals::server_config.add_value("server.tickrate", server_tickrate);
    globals::server_config.load_file("server.conf");
    globals::server_config.load_cmdline();

    globals::tickrate = server_tickrate.get_value();
    globals::tickrate_dt = static_cast<std::uint64_t>(1000000.0f / static_cast<float>(globals::tickrate));

    server_game::init_late();

    std::uint64_t last_curtime = globals::curtime;

    while(globals::is_running) {
        globals::curtime = utils::unix_microseconds();

        globals::fixed_frametime_us = globals::curtime - last_curtime;
        globals::fixed_frametime = static_cast<float>(globals::fixed_frametime_us) / 1000000.0f;
        globals::fixed_frametime_avg += globals::fixed_frametime;
        globals::fixed_frametime_avg *= 0.5f;

        last_curtime = globals::curtime;

        server_game::fixed_update();
        server_game::fixed_update_late();

        globals::dispatcher.update();

        globals::fixed_framecount += 1;

        std::this_thread::sleep_for(std::chrono::microseconds(globals::tickrate_dt));

        resource::soft_cleanup();

        threading::update();
    }

    server_game::shutdown();

    resource::hard_cleanup();

    threading::shutdown();

    spdlog::info("server: shutdown after {} frames", globals::fixed_framecount);
    spdlog::info("server: average framerate: {:.03f} TPS", 1.0f / globals::fixed_frametime_avg);
    spdlog::info("server: average frametime: {:.03f} MSPT", 1000.0f * globals::fixed_frametime_avg);

    globals::server_config.save_file("server.conf");

    shared_game::shutdown();

    return EXIT_SUCCESS;
}