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
//! This library contains the shared functions and data types between
//! [server](../server/index.html) and [client](../client/index.html).

use serde::{Deserialize, Serialize};

mod chunk;
mod effect;
pub mod protocol;
mod symmetric_crypto;
mod world;

pub use chunk::*;
pub use effect::*;
pub use world::*;

pub type WorldInstant = u64;
pub type WorldDuration = u64;

/// The amount of timesteps in one second. The "FPS" of the game logic.
pub const SECOND: WorldDuration = 30;
/// The amount of columns of chunks in the world.
pub const CHUNKS_X: usize = 8;
/// The amount of rows of chunks in the world.
pub const CHUNKS_Y: usize = 8;
/// The width of one chunk in tiles.
pub const CHUNK_WIDTH: usize = 8;
/// The height of one chunk in tiles.
pub const CHUNK_HEIGHT: usize = 8;

/// Client-side actions that are sent to the server.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum ClientAction {
    /// No-op to send to avoid getting marked as disconnected because of the
    /// timeout.
    KeepAlive,
    /// The first message to send to the server. The server will respond to this
    /// with a [ServerAction::Welcome].
    Hello,
    /// Attempt to move to the left by one tile.
    MoveLeft,
    /// Attempt to move to the right by one tile.
    MoveRight,
    /// Attempt to move to the up by one tile.
    MoveUp,
    /// Attempt to move to the down by one tile.
    MoveDown,
}

/// Messages sent back to clients by the server.
#[derive(Clone, Serialize, Deserialize)]
pub enum ServerAction {
    /// Game state updates for a specific time, to sync up the client with the
    /// server's [World] state.
    Effects {
        /// The time at which the world should be before applying the effects.
        /// If the receiver World's time is behind, it should be updated until
        /// it reaches this time.
        time: WorldInstant,
        /// Effects or entire chunks to apply or replace, respectively.
        chunk_updates: Vec<(usize, ChunkUpdate)>,
    },
    /// Response to [ClientAction::Hello], tells the peer which
    /// [PlayerCharacter] they're controlling. [PcHandle] is used to index into
    /// [Chunk::pcs].
    Welcome(PcHandle),
    /// Hint for the client to unload the chunks, as effects for the chunks
    /// won't be sent anymore, and they will drift out of sync with the server.
    UnloadChunks(Vec<usize>),
}