R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
rtype::ServerGame Class Reference

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.
 

Detailed Description

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:

  1. steering: player ships take the velocity of their held directions (steer_ships());
  2. movement: every entity moves by its velocity;
  3. behaviors: the script of each enemy's type moves it (EnemyScripts::update());
  4. play_area: player ships are kept on screen (keep_ships_in_area());
  5. weapons: players holding fire with a reloaded weapon fire a missile;
  6. enemy_fire: enemies on screen with a reloaded weapon fire a missile to the left;
  7. spawn: enemies of type tuning::random_enemy appear at random on the right edge (only with a seed);
  8. waves: the enemies of the waves given to play_waves() appear when due;
  9. collision: the grid and the layer masks give the contacts of the tick;
  10. damage: contacts remove health, consume missiles and make kills;
  11. score: each kill is credited to the player who made it;
  12. lives: killed players lose a life and respawn, or are out;
  13. cleanup: the dead and the spent missiles are destroyed;
  14. 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.

Constructor & Destructor Documentation

◆ ServerGame() [1/3]

rtype::ServerGame::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 = {} 
)
explicit

Creates an empty game with its systems registered.

Parameters
seedSeed 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).
enemiesDirectory of the enemy scripts (EnemyScripts).
hot_reloadtrue 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).
rulesHow the game plays: lives, enemies and friendly fire.
Exceptions
aegis2d::core::ConfigErrorIf a script is wrong, or, with a seed, none gives tuning::random_enemy.

◆ ServerGame() [2/3]

rtype::ServerGame::ServerGame ( const ServerGame &  )
delete

◆ ServerGame() [3/3]

rtype::ServerGame::ServerGame ( ServerGame &&  )
delete

◆ ~ServerGame()

rtype::ServerGame::~ServerGame ( )
default

Member Function Documentation

◆ add_player() [1/2]

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.

Returns
The player's ship.
Exceptions
std::length_errorIf tuning::max_players players are in the game.

◆ add_player() [2/2]

aegis2d::ecs::Entity rtype::ServerGame::add_player ( std::size_t  slot)

Adds a player in a given slot, the player number of its session.

Parameters
slotSlot, 0 to tuning::max_players - 1; it picks the spawn point.
Returns
The player's ship.
Exceptions
std::out_of_rangeIf slot is not below tuning::max_players.
std::logic_errorIf the slot has a player still in the game.

◆ awards()

const std::vector< aegis2d::ecs::Award > & rtype::ServerGame::awards ( ) const
noexcept

Points given during the last tick.

Returns
One award per credited kill.

◆ damage()

const aegis2d::ecs::DamageReport & rtype::ServerGame::damage ( ) const
noexcept

Hits, kills and spent missiles of the last tick.

Returns
The damage report.

◆ despawned()

const std::vector< aegis2d::ecs::Entity > & rtype::ServerGame::despawned ( ) const
noexcept

Missiles and enemies destroyed during the last tick because they left the screen.

Returns
The despawned entities; their handles are no longer alive.

◆ enemies()

EnemyScripts & rtype::ServerGame::enemies ( )
noexcept

Enemy types of the game and the scripts of their behaviors.

Returns
The scripts.

◆ enemy_fired()

const std::vector< aegis2d::ecs::Entity > & rtype::ServerGame::enemy_fired ( ) const
noexcept

Missiles fired by the enemies during the last tick.

Returns
The new missiles, in the order of their shooters.

◆ fired()

const std::vector< aegis2d::ecs::Entity > & rtype::ServerGame::fired ( ) const
noexcept

Missiles fired by the players during the last tick.

Returns
The new missiles, in the order of their shooters.

◆ game_over()

bool rtype::ServerGame::game_over ( ) const

Tells whether every player who joined is out.

Returns
false before the first player joins.

◆ history()

const PositionHistory & rtype::ServerGame::history ( ) const
noexcept

Where the enemies were at each of the last 500 ms of ticks.

Recorded at the end of each tick(), under the number ticks() then has.

Returns
The history.

◆ lives_lost()

const std::vector< aegis2d::ecs::LifeLost > & rtype::ServerGame::lives_lost ( ) const
noexcept

Lives lost during the last tick.

Returns
One event per player death.

◆ operator=() [1/2]

ServerGame & rtype::ServerGame::operator= ( const ServerGame &  )
delete

◆ operator=() [2/2]

ServerGame & rtype::ServerGame::operator= ( ServerGame &&  )
delete

◆ play_area()

static aegis2d::ecs::Aabb rtype::ServerGame::play_area ( )
staticnoexcept

Play area: from the origin to the logical resolution.

Returns
The area player ships are kept in.

◆ play_waves()

void rtype::ServerGame::play_waves ( aegis2d::ecs::WaveSchedule  waves)

Plays enemy waves from now on, replacing the ones played so far.

Parameters
wavesSchedule read by load_waves(), at its start.

◆ players()

std::size_t rtype::ServerGame::players ( ) const

Players still in the game.

Returns
Ships that have lives left.

◆ registry() [1/2]

const aegis2d::ecs::Registry & rtype::ServerGame::registry ( ) const
noexcept

World of the game, to spawn entities and to build snapshots.

Returns
The registry.

◆ registry() [2/2]

aegis2d::ecs::Registry & rtype::ServerGame::registry ( )
noexcept

World of the game, to spawn entities and to build snapshots.

Returns
The registry.

◆ remove_player()

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.

Parameters
playerShip returned by add_player().
Returns
false if it is not a player of this game any more.

◆ rewind()

std::uint32_t rtype::ServerGame::rewind ( aegis2d::ecs::Entity  player) const

How many ticks back a player's missiles meet the enemies.

Parameters
playerShip returned by add_player().
Returns
The rewind, 0 for a player out or never set.

◆ rules()

const GameRules & rtype::ServerGame::rules ( ) const
noexcept

How the game plays.

Returns
The rules given to the constructor.

◆ seed()

std::optional< std::uint64_t > rtype::ServerGame::seed ( ) const
noexcept

Seed of the random spawns.

Returns
The seed given to the constructor, or std::nullopt without random spawns.

◆ set_input()

bool rtype::ServerGame::set_input ( aegis2d::ecs::Entity  player,
aegis2d::ecs::PlayerInput  input 
)

Sets the actions a player holds, as received from its client.

Parameters
playerShip returned by add_player().
inputHeld actions; the latest one received is used at every tick.
Returns
false if the player is not in the game any more (a late packet).

◆ set_profiler()

void rtype::ServerGame::set_profiler ( aegis2d::core::Profiler *  profiler)
noexcept

Times each system of the game with a profiler (ecs::Scheduler::set_profiler()).

Parameters
profilerProfiler of the simulation thread, or nullptr for none.

◆ set_rewind()

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.

Parameters
playerShip returned by add_player().
ticks0 for none, at most PositionHistory::capacity.
Returns
false if the player is out.

◆ slot_of()

std::optional< std::size_t > rtype::ServerGame::slot_of ( aegis2d::ecs::Entity  player) const
noexcept

Slot of a player.

Parameters
playerShip returned by add_player().
Returns
Its slot, or std::nullopt if it is not a player of this game any more.

◆ spawn_enemy()

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()).

Parameters
typeName of the type, as waves files name it.
positionWhere it appears.
Returns
The enemy, or a null handle if the spawn of its type failed.
Exceptions
std::invalid_argumentIf no script gives type.

◆ spawned()

const std::vector< aegis2d::ecs::Entity > & rtype::ServerGame::spawned ( ) const
noexcept

Enemies spawned during the last tick, at random or by the waves.

Returns
The new enemies, random ones first, in spawn order.

◆ systems()

const aegis2d::ecs::Scheduler & rtype::ServerGame::systems ( ) const
noexcept

Systems of the game, in update order.

Returns
The scheduler.

◆ tick()

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.

Parameters
stepDuration of the step, core::step_from_rate(tuning::tick_rate).

◆ ticks()

std::uint64_t rtype::ServerGame::ticks ( ) const
noexcept

Number of ticks run.

Returns
Completed calls to tick().

◆ waves()

const aegis2d::ecs::WaveSchedule & rtype::ServerGame::waves ( ) const
noexcept

Enemy waves being played.

Returns
The schedule given to play_waves(), or an empty (finished) one.

The documentation for this class was generated from the following file: