R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
server_game.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/core/clock.hpp>
5#include <aegis2d/core/hot_reload.hpp>
6#include <aegis2d/core/math.hpp>
7#include <aegis2d/ecs/collision.hpp>
8#include <aegis2d/ecs/components.hpp>
9#include <aegis2d/ecs/damage.hpp>
10#include <aegis2d/ecs/entity.hpp>
11#include <aegis2d/ecs/lives.hpp>
12#include <aegis2d/ecs/registry.hpp>
13#include <aegis2d/ecs/score.hpp>
14#include <aegis2d/ecs/spawner.hpp>
15#include <aegis2d/ecs/system.hpp>
16#include <aegis2d/ecs/waves.hpp>
17
20#include <rtype/tuning.hpp>
21
22#include <array>
23#include <cstddef>
24#include <cstdint>
25#include <filesystem>
26#include <optional>
27#include <string_view>
28#include <vector>
29
30namespace rtype {
31
35struct GameRules {
37 std::uint32_t lives = tuning::player_lives;
40 std::int32_t enemy_health_factor = 1;
45 bool friendly_fire = false;
46};
47
76public:
87 explicit ServerGame(std::optional<std::uint64_t> seed = std::nullopt,
88 const std::filesystem::path& enemies = default_enemies_directory(),
89 bool hot_reload = aegis2d::core::hot_reload_by_default,
90 GameRules rules = {});
91 ServerGame(const ServerGame&) = delete;
92 ServerGame& operator=(const ServerGame&) = delete;
95 ~ServerGame() = default;
96
101 aegis2d::ecs::Entity add_player();
102
108 aegis2d::ecs::Entity add_player(std::size_t slot);
109
115 bool remove_player(aegis2d::ecs::Entity player);
116
120 [[nodiscard]] std::optional<std::size_t> slot_of(aegis2d::ecs::Entity player) const noexcept;
121
124 [[nodiscard]] std::size_t players() const;
125
131 aegis2d::ecs::Entity spawn_enemy(std::string_view type, aegis2d::Vec2 position);
132
135 [[nodiscard]] EnemyScripts& enemies() noexcept;
136
139 void play_waves(aegis2d::ecs::WaveSchedule waves);
140
143 [[nodiscard]] const aegis2d::ecs::WaveSchedule& waves() const noexcept;
144
149 bool set_input(aegis2d::ecs::Entity player, aegis2d::ecs::PlayerInput input);
150
156 bool set_rewind(aegis2d::ecs::Entity player, std::uint32_t ticks);
157
161 [[nodiscard]] std::uint32_t rewind(aegis2d::ecs::Entity player) const;
162
167 void tick(aegis2d::core::Duration step);
168
171 [[nodiscard]] bool game_over() const;
172
175 [[nodiscard]] std::uint64_t ticks() const noexcept;
176
180 [[nodiscard]] const PositionHistory& history() const noexcept;
181
184 [[nodiscard]] aegis2d::ecs::Registry& registry() noexcept;
185
187 [[nodiscard]] const aegis2d::ecs::Registry& registry() const noexcept;
188
191 void set_profiler(aegis2d::core::Profiler* profiler) noexcept;
192
195 [[nodiscard]] const aegis2d::ecs::Scheduler& systems() const noexcept;
196
199 [[nodiscard]] const std::vector<aegis2d::ecs::Entity>& fired() const noexcept;
200
203 [[nodiscard]] const std::vector<aegis2d::ecs::Entity>& enemy_fired() const noexcept;
204
207 [[nodiscard]] const std::vector<aegis2d::ecs::Entity>& spawned() const noexcept;
208
211 [[nodiscard]] const GameRules& rules() const noexcept;
212
215 [[nodiscard]] std::optional<std::uint64_t> seed() const noexcept;
216
219 [[nodiscard]] const aegis2d::ecs::DamageReport& damage() const noexcept;
220
223 [[nodiscard]] const std::vector<aegis2d::ecs::Award>& awards() const noexcept;
224
227 [[nodiscard]] const std::vector<aegis2d::ecs::LifeLost>& lives_lost() const noexcept;
228
231 [[nodiscard]] const std::vector<aegis2d::ecs::Entity>& despawned() const noexcept;
232
235 [[nodiscard]] static aegis2d::ecs::Aabb play_area() noexcept;
236
237private:
238 void register_systems();
239
240 aegis2d::ecs::Registry registry_;
241 EnemyScripts enemies_;
242 aegis2d::core::HotReload hot_;
243 aegis2d::ecs::Scheduler systems_;
244 aegis2d::ecs::CollisionGrid grid_;
245 std::vector<aegis2d::ecs::Entity> shooters_;
246 std::vector<aegis2d::ecs::Entity> fired_;
247 std::vector<aegis2d::ecs::Entity> gunners_;
248 std::vector<aegis2d::ecs::Entity> enemy_fired_;
249 std::optional<std::uint64_t> seed_;
250 GameRules rules_;
251 aegis2d::ecs::RandomSpawner spawner_;
252 std::vector<aegis2d::Vec2> spawn_points_;
253 std::vector<aegis2d::ecs::Entity> spawned_;
254 aegis2d::ecs::WaveSchedule waves_;
255 std::vector<aegis2d::ecs::WaveSpawn> due_;
256 std::vector<aegis2d::ecs::Contact> contacts_;
257 aegis2d::ecs::DamageReport damage_;
258 std::vector<aegis2d::ecs::Award> awards_;
259 std::vector<aegis2d::ecs::LifeLost> lives_lost_;
260 std::vector<aegis2d::ecs::Entity> despawned_;
261 std::array<aegis2d::ecs::Entity, tuning::max_players> slots_{};
262 PositionHistory history_;
263 std::array<std::uint32_t, tuning::max_players> rewinds_{};
264 bool started_ = false;
265};
266
267} // namespace rtype
The enemy types of a directory of Lua scripts, and the behaviors of their enemies.
Definition enemy_scripts.hpp:57
The positions of the enemies over the last tuning::position_history_ticks ticks.
Definition position_history.hpp:25
One R-Type game on the server: the world, its systems in order, and the players.
Definition server_game.hpp:75
std::size_t players() const
Players still in the game.
aegis2d::ecs::Entity add_player()
Adds a player in the first free slot.
bool remove_player(aegis2d::ecs::Entity player)
Removes a player who left: its ship is destroyed and its slot is free.
EnemyScripts & enemies() noexcept
Enemy types of the game and the scripts of their behaviors.
ServerGame(const ServerGame &)=delete
const std::vector< aegis2d::ecs::LifeLost > & lives_lost() const noexcept
Lives lost during the last tick.
aegis2d::ecs::Entity add_player(std::size_t slot)
Adds a player in a given slot, the player number of its session.
const std::vector< aegis2d::ecs::Entity > & enemy_fired() const noexcept
Missiles fired by the enemies during the last tick.
const aegis2d::ecs::Scheduler & systems() const noexcept
Systems of the game, in update order.
ServerGame & operator=(const ServerGame &)=delete
static aegis2d::ecs::Aabb play_area() noexcept
Play area: from the origin to the logical resolution.
const aegis2d::ecs::DamageReport & damage() const noexcept
Hits, kills and spent missiles of the last tick.
const aegis2d::ecs::WaveSchedule & waves() const noexcept
Enemy waves being played.
ServerGame(ServerGame &&)=delete
const std::vector< aegis2d::ecs::Entity > & fired() const noexcept
Missiles fired by the players during the last tick.
ServerGame(std::optional< std::uint64_t > seed=std::nullopt, const std::filesystem::path &enemies=default_enemies_directory(), bool hot_reload=aegis2d::core::hot_reload_by_default, GameRules rules={})
Creates an empty game with its systems registered.
void set_profiler(aegis2d::core::Profiler *profiler) noexcept
Times each system of the game with a profiler (ecs::Scheduler::set_profiler()).
aegis2d::ecs::Registry & registry() noexcept
World of the game, to spawn entities and to build snapshots.
bool game_over() const
Tells whether every player who joined is out.
bool set_rewind(aegis2d::ecs::Entity player, std::uint32_t ticks)
Sets how many ticks back a player's missiles meet the enemies (lag compensation): its rewind_ticks(),...
ServerGame & operator=(ServerGame &&)=delete
std::optional< std::uint64_t > seed() const noexcept
Seed of the random spawns.
void tick(aegis2d::core::Duration step)
Runs one simulation step: every system, in order.
const GameRules & rules() const noexcept
How the game plays.
const std::vector< aegis2d::ecs::Entity > & despawned() const noexcept
Missiles and enemies destroyed during the last tick because they left the screen.
bool set_input(aegis2d::ecs::Entity player, aegis2d::ecs::PlayerInput input)
Sets the actions a player holds, as received from its client.
~ServerGame()=default
std::uint64_t ticks() const noexcept
Number of ticks run.
std::optional< std::size_t > slot_of(aegis2d::ecs::Entity player) const noexcept
Slot of a player.
aegis2d::ecs::Entity spawn_enemy(std::string_view type, aegis2d::Vec2 position)
Creates an enemy of a type its script gives (EnemyScripts::spawn()).
std::uint32_t rewind(aegis2d::ecs::Entity player) const
How many ticks back a player's missiles meet the enemies.
const PositionHistory & history() const noexcept
Where the enemies were at each of the last 500 ms of ticks.
const std::vector< aegis2d::ecs::Award > & awards() const noexcept
Points given during the last tick.
const std::vector< aegis2d::ecs::Entity > & spawned() const noexcept
Enemies spawned during the last tick, at random or by the waves.
void play_waves(aegis2d::ecs::WaveSchedule waves)
Plays enemy waves from now on, replacing the ones played so far.
Enemy types written in Lua: their values and their behavior, run by the server.
constexpr std::uint32_t player_lives
Lives of a player at the start of a game.
Definition tuning.hpp:44
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
std::filesystem::path default_enemies_directory()
Directory of the enemy scripts the server plays.
Where the enemies were at each of the last ticks, for lag compensation.
How a game plays: the rules of its room, as the gameplay reads them.
Definition server_game.hpp:35
float spawn_interval_scale
Factor of the intervals between two random Bydos spawns: above 1, fewer enemies; below 1,...
Definition server_game.hpp:43
bool friendly_fire
Whether the players' missiles hit the other players.
Definition server_game.hpp:45
std::int32_t enemy_health_factor
Factor of the health of each enemy that appears, at random or by the waves, over the health its scrip...
Definition server_game.hpp:40
std::uint32_t lives
Lives of each player at the start, at least 1.
Definition server_game.hpp:37
Gameplay values of R-Type: play area, speeds, sizes, health, lives and scores.