|
R-Type
Networked shoot'em up built on Aegis2D
|
One R-Type game on the server: the world, its systems in order, and the players. More...
#include <rtype/server_game.hpp>
Public Member Functions | |
| 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. | |
| ServerGame (const ServerGame &)=delete | |
| ServerGame & | operator= (const ServerGame &)=delete |
| ServerGame (ServerGame &&)=delete | |
| ServerGame & | operator= (ServerGame &&)=delete |
| ~ServerGame ()=default | |
| aegis2d::ecs::Entity | add_player () |
| Adds a player in the first free slot. | |
| aegis2d::ecs::Entity | add_player (std::size_t slot) |
| Adds a player in a given slot, the player number of its session. | |
| bool | remove_player (aegis2d::ecs::Entity player) |
| Removes a player who left: its ship is destroyed and its slot is free. | |
| std::optional< std::size_t > | slot_of (aegis2d::ecs::Entity player) const noexcept |
| Slot of a player. | |
| std::size_t | players () const |
| Players still in the game. | |
| aegis2d::ecs::Entity | spawn_enemy (std::string_view type, aegis2d::Vec2 position) |
| Creates an enemy of a type its script gives (EnemyScripts::spawn()). | |
| EnemyScripts & | enemies () noexcept |
| Enemy types of the game and the scripts of their behaviors. | |
| void | play_waves (aegis2d::ecs::WaveSchedule waves) |
| Plays enemy waves from now on, replacing the ones played so far. | |
| const aegis2d::ecs::WaveSchedule & | waves () const noexcept |
| Enemy waves being played. | |
| bool | set_input (aegis2d::ecs::Entity player, aegis2d::ecs::PlayerInput input) |
| Sets the actions a player holds, as received from its client. | |
| 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(), from its round trip time. | |
| std::uint32_t | rewind (aegis2d::ecs::Entity player) const |
| How many ticks back a player's missiles meet the enemies. | |
| void | tick (aegis2d::core::Duration step) |
| Runs one simulation step: every system, in order. | |
| bool | game_over () const |
| Tells whether every player who joined is out. | |
| std::uint64_t | ticks () const noexcept |
| Number of ticks run. | |
| const PositionHistory & | history () const noexcept |
| Where the enemies were at each of the last 500 ms of ticks. | |
| aegis2d::ecs::Registry & | registry () noexcept |
| World of the game, to spawn entities and to build snapshots. | |
| const aegis2d::ecs::Registry & | registry () const noexcept |
| World of the game, to spawn entities and to build snapshots. | |
| void | set_profiler (aegis2d::core::Profiler *profiler) noexcept |
Times each system of the game with a profiler (ecs::Scheduler::set_profiler()). | |
| const aegis2d::ecs::Scheduler & | systems () const noexcept |
| Systems of the game, in update order. | |
| const std::vector< aegis2d::ecs::Entity > & | fired () const noexcept |
| Missiles fired by the players during the last tick. | |
| const std::vector< aegis2d::ecs::Entity > & | enemy_fired () const noexcept |
| Missiles fired by the enemies 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. | |
| const GameRules & | rules () const noexcept |
| How the game plays. | |
| std::optional< std::uint64_t > | seed () const noexcept |
| Seed of the random spawns. | |
| const aegis2d::ecs::DamageReport & | damage () const noexcept |
| Hits, kills and spent missiles of the last tick. | |
| const std::vector< aegis2d::ecs::Award > & | awards () const noexcept |
| Points given during the last tick. | |
| const std::vector< aegis2d::ecs::LifeLost > & | lives_lost () const noexcept |
| Lives lost during the last tick. | |
| const std::vector< aegis2d::ecs::Entity > & | despawned () const noexcept |
| Missiles and enemies destroyed during the last tick because they left the screen. | |
Static Public Member Functions | |
| static aegis2d::ecs::Aabb | play_area () noexcept |
| Play area: from the origin to the logical resolution. | |
One R-Type game on the server: the world, its systems in order, and the players.
The constructor registers the server systems in the order of a tick:
steering: player ships take the velocity of their held directions (steer_ships());movement: every entity moves by its velocity;behaviors: the script of each enemy's type moves it (EnemyScripts::update());play_area: player ships are kept on screen (keep_ships_in_area());weapons: players holding fire with a reloaded weapon fire a missile;enemy_fire: enemies on screen with a reloaded weapon fire a missile to the left;spawn: enemies of type tuning::random_enemy appear at random on the right edge (only with a seed);waves: the enemies of the waves given to play_waves() appear when due;collision: the grid and the layer masks give the contacts of the tick;damage: contacts remove health, consume missiles and make kills;score: each kill is credited to the player who made it;lives: killed players lose a life and respawn, or are out;cleanup: the dead and the spent missiles are destroyed;despawn: missiles and enemies that left the screen for good are destroyed.Systems 1, 2 and 4 move a player ship as step_ship() does, which the client runs to predict its own ship.
The network layer (#112, #113, #114) creates it with a seed, gives it the waves of the level (play_waves(load_waves(...))), calls set_input() with each client's input, tick() at tuning::tick_rate, then sends the world and the events of the tick (fired(), enemy_fired(), spawned(), damage(), awards(), lives_lost(), despawned(), game_over()). Not thread-safe: use it from the simulation thread only.
|
explicit |
Creates an empty game with its systems registered.
| seed | Seed of the random Bydos spawns: the same seed gives the same spawns, so a game can be replayed from its logged seed. Without one, the spawn system is disabled and only what the caller creates is in the world (tests, scripted levels). |
| enemies | Directory of the enemy scripts (EnemyScripts). |
| hot_reload | true to reload a script when its file changes, checked at most every 250 ms by tick(); by default in debug builds (core::hot_reload_by_default). |
| rules | How the game plays: lives, enemies and friendly fire. |
| aegis2d::core::ConfigError | If a script is wrong, or, with a seed, none gives tuning::random_enemy. |
|
delete |
|
delete |
|
default |
| aegis2d::ecs::Entity rtype::ServerGame::add_player | ( | ) |
Adds a player in the first free slot.
A slot is free when it never had a player or its player is out.
| std::length_error | If tuning::max_players players are in the game. |
| aegis2d::ecs::Entity rtype::ServerGame::add_player | ( | std::size_t | slot | ) |
Adds a player in a given slot, the player number of its session.
| slot | Slot, 0 to tuning::max_players - 1; it picks the spawn point. |
| std::out_of_range | If slot is not below tuning::max_players. |
| std::logic_error | If the slot has a player still in the game. |
|
noexcept |
Points given during the last tick.
|
noexcept |
Hits, kills and spent missiles of the last tick.
|
noexcept |
Missiles and enemies destroyed during the last tick because they left the screen.
|
noexcept |
Enemy types of the game and the scripts of their behaviors.
|
noexcept |
Missiles fired by the enemies during the last tick.
|
noexcept |
Missiles fired by the players during the last tick.
| bool rtype::ServerGame::game_over | ( | ) | const |
Tells whether every player who joined is out.
false before the first player joins.
|
noexcept |
|
noexcept |
Lives lost during the last tick.
|
delete |
|
delete |
|
staticnoexcept |
Play area: from the origin to the logical resolution.
| void rtype::ServerGame::play_waves | ( | aegis2d::ecs::WaveSchedule | waves | ) |
Plays enemy waves from now on, replacing the ones played so far.
| waves | Schedule read by load_waves(), at its start. |
| std::size_t rtype::ServerGame::players | ( | ) | const |
Players still in the game.
|
noexcept |
World of the game, to spawn entities and to build snapshots.
|
noexcept |
World of the game, to spawn entities and to build snapshots.
| bool rtype::ServerGame::remove_player | ( | aegis2d::ecs::Entity | player | ) |
Removes a player who left: its ship is destroyed and its slot is free.
The game goes on for the others; once every player has left or is out, game_over() is true.
| player | Ship returned by add_player(). |
false if it is not a player of this game any more. | std::uint32_t rtype::ServerGame::rewind | ( | aegis2d::ecs::Entity | player | ) | const |
How many ticks back a player's missiles meet the enemies.
| player | Ship returned by add_player(). |
|
noexcept |
How the game plays.
|
noexcept |
Seed of the random spawns.
std::nullopt without random spawns. | bool rtype::ServerGame::set_input | ( | aegis2d::ecs::Entity | player, |
| aegis2d::ecs::PlayerInput | input | ||
| ) |
Sets the actions a player holds, as received from its client.
| player | Ship returned by add_player(). |
| input | Held actions; the latest one received is used at every tick. |
false if the player is not in the game any more (a late packet).
|
noexcept |
Times each system of the game with a profiler (ecs::Scheduler::set_profiler()).
| profiler | Profiler of the simulation thread, or nullptr for none. |
| bool rtype::ServerGame::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(), from its round trip time.
| player | Ship returned by add_player(). |
| ticks | 0 for none, at most PositionHistory::capacity. |
false if the player is out.
|
noexcept |
Slot of a player.
| player | Ship returned by add_player(). |
std::nullopt if it is not a player of this game any more. | aegis2d::ecs::Entity rtype::ServerGame::spawn_enemy | ( | std::string_view | type, |
| aegis2d::Vec2 | position | ||
| ) |
Creates an enemy of a type its script gives (EnemyScripts::spawn()).
| type | Name of the type, as waves files name it. |
| position | Where it appears. |
spawn of its type failed. | std::invalid_argument | If no script gives type. |
|
noexcept |
Enemies spawned during the last tick, at random or by the waves.
|
noexcept |
Systems of the game, in update order.
| void rtype::ServerGame::tick | ( | aegis2d::core::Duration | step | ) |
Runs one simulation step: every system, in order.
With hot reload, the enemy scripts whose file changed are read again first (EnemyScripts::reload()); a wrong one is logged and the type keeps the one it had.
| step | Duration of the step, core::step_from_rate(tuning::tick_rate). |
|
noexcept |
Number of ticks run.
|
noexcept |
Enemy waves being played.