Skip to main content

borger/
lib.rs

1use crate::diff_ser::DiffSerializer;
2use crate::multiplayer_tradeoff::{Immediate, WaitForConsensus};
3use crate::networked_types::primitive::usize32;
4use crate::simulation::{Input, State};
5use crate::simulation_controller::GameContext;
6use crate::tick::TickID;
7
8pub mod multiplayer_tradeoff;
9pub mod physics;
10
11///Drives the simulation tick; controls pretty
12///much everything from atop its throne
13pub mod simulation_controller;
14
15///Defines bidirectional, RPC-like communication
16///channels between threads and they data they
17///can carry
18#[cfg_attr(not(any(feature = "server", feature = "client")), doc(hidden))]
19pub mod thread_comms;
20
21///Time-keeping
22pub mod tick;
23
24///Serdes strategies for all networked state types
25pub mod networked_types;
26
27///Random small snippets of code that didn't seem
28///to belong anywhere else
29mod misc;
30pub(crate) use misc::*;
31
32mod handwritten {
33	pub(crate) mod constructors;
34	pub(crate) mod diff_des;
35	pub(crate) mod diff_ser;
36	pub(crate) mod interpolation;
37	pub(crate) mod simulation;
38	pub(crate) mod snapshot_serdes;
39	pub(crate) mod untracked;
40
41	#[cfg(feature = "client")]
42	pub(crate) mod presentation;
43}
44
45#[allow(unused, dead_code)]
46mod generated {
47	pub(crate) mod constructors;
48	pub(crate) mod diff_des;
49	pub(crate) mod diff_ser;
50	pub(crate) mod interpolation;
51	pub(crate) mod simulation;
52	pub(crate) mod snapshot_serdes;
53	pub(crate) mod untracked;
54
55	#[cfg(feature = "client")]
56	pub(crate) mod presentation;
57}
58
59///Struct definitions of state objects
60#[cfg_attr(not(any(feature = "server", feature = "client")), doc(hidden))]
61pub mod simulation {
62	pub use super::generated::simulation::*;
63	pub use super::handwritten::simulation::*;
64}
65
66///Constructors for state objects
67pub(crate) mod constructors {
68	pub use super::handwritten::constructors::*;
69}
70
71///Any changes to state during the simulation tick
72///are recorded by this system as they're
73///happening. Rollback and rx systems use this
74///data to make multiplayer happen
75pub mod diff_ser {
76	pub use super::handwritten::diff_ser::*;
77
78	#[cfg(feature = "client")]
79	pub(crate) use super::generated::diff_ser::*;
80}
81
82///Parses and executes operations record by
83///diff_ser. Rollback system undoes changes to
84///predicted state in order to account for the rx
85///system receiving new information (server
86///receiving late inputs, client receiving
87///authoritative state). rx system applies state
88///changes that were received
89pub(crate) mod diff_des {
90	pub use super::handwritten::diff_des::*;
91
92	#[cfg(feature = "server")]
93	pub use super::generated::diff_des::*;
94}
95
96///Take a snapshot (ser/des) of all or part of the
97///state. Used for sending server's current state
98///to a newly connected client. Also used to predict
99///the removal of a collection element. This is a
100///complicated+expensive task because it requires
101///creating a backup of the entire struct being
102///removed in order to be able to roll back to
103///before it was deleted
104pub(crate) mod snapshot_serdes {
105	pub use super::handwritten::snapshot_serdes::*;
106}
107
108///Responsible for resetting fields with netVisibility: "Untracked"
109///back to their default values in between ticks. This is important
110///for maintaining deterministic behavior; otherwise, the data in
111///each untracked field is stale/left over from any arbitrary tick,
112///forward or backward in time. It'd be the same effect as reading
113///from uninitialized memory
114pub(crate) mod untracked {
115	pub use super::handwritten::untracked::*;
116}
117
118///Stripped down version of state (only fields with
119///with presentation enabled), cloned and shipped to
120///the presentation thread at the end of each client
121///sided tick
122#[cfg(feature = "client")]
123pub mod presentation {
124	pub use super::generated::presentation::*;
125	pub use super::handwritten::presentation::*;
126}
127
128///Interpolation and presentation of entities
129pub mod interpolation {
130	#[cfg(feature = "client")]
131	pub use super::generated::interpolation::*;
132
133	pub use super::handwritten::interpolation::*;
134}
135
136///Helpful types and macros when writing simulation logic
137pub mod prelude {
138	pub use crate::SimulationInitOptions;
139	pub use crate::diff_ser::DiffSerializer;
140	pub use crate::multiplayer_tradeoff; //macro
141	pub use crate::multiplayer_tradeoff::*;
142	pub use crate::networked_types::primitive::usize32;
143	pub use crate::simulation::*;
144	pub use crate::simulation_controller::GameContext;
145	pub use crate::tick::{TickID, TickInfo};
146	pub use borger_procmac::server;
147	pub use log::{debug, error, info, warn};
148}
149
150pub struct SimulationInitOptions {
151	//pipeline
152	pub init_static_level_geom: Option<fn(/*state*/ &mut State)>,
153	pub simulation_loop: fn(/*ctx*/ &mut GameContext<Immediate>),
154
155	//input operations
156	pub input_merge: fn(/*combined*/ &Input, /*new*/ &Input) -> Input,
157	pub input_validate: fn(/*sus*/ &Input) -> Input,
158	pub input_server_predict_late: fn(
159		/*prv*/ &Input,
160		/*state*/ &State,
161		/*client_id*/ usize32,
162		/*is_timed_out*/ bool,
163	) -> Input,
164	pub input_client_predict_late:
165		fn(/*prv*/ &Input, /*state*/ &State, /*client_id*/ usize32) -> Input,
166
167	//server_events
168	pub on_server_start: fn(/*state*/ &mut State, /*diff*/ &mut DiffSerializer<WaitForConsensus>), //goes without saying tick id is 0
169	pub on_client_connect: fn(
170		/*state*/ &mut State,
171		/*client id*/ usize32,
172		/*tick id*/ TickID,
173		/*diff*/ &mut DiffSerializer<WaitForConsensus>,
174	),
175	pub on_client_disconnect: fn(
176		/*state*/ &mut State,
177		/*id*/ usize32,
178		/*tick id*/ TickID,
179		/*diff*/ &mut DiffSerializer<WaitForConsensus>,
180	),
181}