R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
simulation.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/core/clock.hpp>
5#include <aegis2d/core/loop.hpp>
6#include <aegis2d/core/profiler.hpp>
7
9#include <rtype/tuning.hpp>
10
11#include <atomic>
12#include <chrono>
13#include <cstdint>
14#include <functional>
15#include <mutex>
16#include <thread>
17#include <vector>
18
19namespace rtype {
20
31public:
33 using Command = std::function<void(GameInstance& instance)>;
34
36 using TickHandler = std::function<void(GameInstance& instance)>;
37
39 static constexpr std::chrono::seconds report_period{10};
40
47 explicit Simulation(GameInstance& instance, std::uint32_t rate = tuning::tick_rate,
48 TickHandler after_tick = {}, bool trace = false);
49
50 Simulation(const Simulation&) = delete;
51 Simulation& operator=(const Simulation&) = delete;
56
61 void post(Command command);
62
65 void stop();
66
69 [[nodiscard]] std::uint64_t ticks() const noexcept;
70
75 [[nodiscard]] const aegis2d::core::Profiler& profiler() const noexcept;
76
79 [[nodiscard]] std::uint32_t rate() const noexcept;
80
81private:
82 void run();
83 void step(aegis2d::core::Duration step);
84 void run_commands();
85
86 GameInstance& instance_;
87 std::uint32_t rate_;
88 TickHandler after_tick_;
89 aegis2d::core::MainLoop loop_;
90 std::mutex mutex_;
91 std::vector<Command> commands_;
92 std::atomic<std::uint64_t> ticks_{0};
93 std::atomic<bool> stopping_{false};
94 // Used by the thread only.
95 std::chrono::steady_clock::time_point report_since_;
96 std::uint64_t report_ticks_ = 0;
97 aegis2d::core::Profiler profiler_;
98 // Last, so the thread starts once every other member is ready.
99 std::thread thread_;
100};
101
102} // namespace rtype
The one game of the server, with up to tuning::max_players players.
Definition game_instance.hpp:37
Runs a GameInstance on its own thread, tuning::tick_rate times per second.
Definition simulation.hpp:30
~Simulation()
Stops the thread and waits for it.
Simulation(GameInstance &instance, std::uint32_t rate=tuning::tick_rate, TickHandler after_tick={}, bool trace=false)
Starts the thread.
Simulation & operator=(const Simulation &)=delete
const aegis2d::core::Profiler & profiler() const noexcept
The timings of the ticks, recorded with trace: a frame per tick, the tick and its systems,...
void stop()
Stops the thread after its current tick and waits for it. Thread-safe, but not from a command; callin...
Simulation(Simulation &&)=delete
std::function< void(GameInstance &instance)> TickHandler
Called on the simulation thread after each tick, to read the game.
Definition simulation.hpp:36
Simulation & operator=(Simulation &&)=delete
std::uint32_t rate() const noexcept
Ticks per second the thread aims at.
std::uint64_t ticks() const noexcept
Number of ticks run. Thread-safe.
Simulation(const Simulation &)=delete
static constexpr std::chrono::seconds report_period
How often the measured rate is logged.
Definition simulation.hpp:39
std::function< void(GameInstance &instance)> Command
A change to make to the game, run on the simulation thread.
Definition simulation.hpp:33
void post(Command command)
Queues a command for the simulation thread. Thread-safe.
The game run by the server, and the players of the sessions in it.
constexpr std::uint32_t tick_rate
Simulation rate of the server, in ticks per second.
Definition tuning.hpp:15
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
Gameplay values of R-Type: play area, speeds, sizes, health, lives and scores.