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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
pub mod prelude { pub use batbox::*; #[cfg(feature = "rendering")] pub use geng::{self, prelude::*}; } use prelude::*; #[cfg(feature = "rendering")] mod app; mod player; mod processor; #[cfg(feature = "rendering")] pub use app::*; pub use player::*; pub use processor::*; pub trait PlayerOptions<G: Game>: From<TcpPlayerOptions> + From<EmptyPlayerOptions> { fn get( &self, extra_data: &G::PlayerExtraData, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Player<G>>, PlayerError>>>>; } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct FullOptions<G: Game> { pub seed: Option<u64>, pub options_preset: G::OptionsPreset, pub players: Vec<G::PlayerOptions>, } impl<G: Game> FullOptions<G> { pub fn save(&self, writer: impl Write) -> std::io::Result<()> { Ok(serde_json::to_writer_pretty(writer, self)?) } pub fn load(reader: impl Read) -> std::io::Result<Self> { Ok(serde_json::from_reader(reader)?) } } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct FullResults<G: Game> { players: Vec<PlayerResult>, results: G::Results, seed: Option<u64>, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct PlayerResult { crashed: bool, comment: Option<String>, } pub trait Game: Diff + Schematic { type Options: Serialize + for<'de> Deserialize<'de> + Sync + Send + Clone + 'static; type OptionsPreset: Serialize + for<'de> Deserialize<'de> + Sync + Send + Clone + 'static + Into<Self::Options>; type PlayerOptions: PlayerOptions<Self> + Serialize + for<'de> Deserialize<'de> + Sync + Send + Clone + 'static; type Action: Serialize + for<'de> Deserialize<'de> + Trans + Schematic + Sync + Send + Clone + 'static; type Event: Serialize + for<'de> Deserialize<'de> + Trans + Schematic + Sync + Send + Clone + 'static; type PlayerView: Serialize + for<'de> Deserialize<'de> + Trans + Schematic + Sync + Send + Clone + 'static; type Results: Serialize + for<'de> Deserialize<'de> + Sync + Send + Clone + 'static; type PlayerExtraData; type CustomData: Serialize + for<'de> Deserialize<'de> + Trans + Schematic + Sync + Send + Clone + 'static; fn init(rng: &mut dyn RngCore, player_count: usize, options: Self::Options) -> Self; fn player_view(&self, player_index: usize) -> Self::PlayerView; fn process_turn( &mut self, rng: &mut dyn RngCore, actions: HashMap<usize, Self::Action>, ) -> Vec<Self::Event>; fn finished(&self) -> bool; fn results(&self) -> Self::Results; } #[derive(Serialize, Deserialize, Trans, Schematic)] pub enum PlayerMessage<G: Game> { CustomDataMessage { data: G::CustomData }, ActionMessage { action: G::Action }, } #[derive(Serialize, Deserialize, Trans, Schematic)] pub struct ServerMessage<G: Game> { pub player_view: Option<G::PlayerView>, } #[cfg(feature = "rendering")] pub trait RendererExtraData<G: Game>: Diff { fn new(game: &G) -> Self; fn update(&mut self, events: &[G::Event], game: &G) { #![allow(unused_variables)] *self = Self::new(game); } } #[cfg(feature = "rendering")] pub trait Renderer<G: Game>: 'static { type ExtraData: RendererExtraData<G>; type Preferences: Debug + Clone + Default + Serialize + for<'de> Deserialize<'de> + 'static; fn default_tps(&self) -> f64; fn update(&mut self, delta_time: f64) { #![allow(unused_variables)] } fn draw( &mut self, game: &G, last_events: &[G::Event], extra_data: &Self::ExtraData, custom_data: &HashMap<usize, Vec<G::CustomData>>, framebuffer: &mut ugli::Framebuffer, ); fn process_event(&mut self, event: &G::Event) { #![allow(unused_variables)] } fn handle_event(&mut self, event: &geng::Event) { #![allow(unused_variables)] } } pub fn save_replay_tick_handler<G: Game, T: Write + Send + 'static>( mut writer: T, ) -> Box<dyn FnMut(Option<&Vec<G::Event>>, &G) + Send> { let mut last: Option<G> = None; Box::new(move |events: Option<&Vec<G::Event>>, current: &G| { if let Some(events) = events { events .write_to(&mut writer) .expect("Failed to write replay"); } match &last { None => current.write_to(&mut writer), Some(last) => last.diff(current).write_to(&mut writer), } .expect("Failed to write replay"); last = Some(current.clone()); }) }