From 3649a8ca9981e4ccc7922338e0e222c6dfc684dd Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 9 Aug 2022 15:08:42 -0700 Subject: [PATCH] Make superflat option per client rather than per world --- examples/combat.rs | 4 ++-- examples/conway.rs | 2 +- examples/cow_sphere.rs | 2 +- examples/raycast.rs | 2 +- examples/terrain.rs | 4 ++-- src/client.rs | 25 ++++++++++++++++++++----- src/world.rs | 21 +-------------------- 7 files changed, 28 insertions(+), 32 deletions(-) diff --git a/examples/combat.rs b/examples/combat.rs index 2ead62b..a498e0c 100644 --- a/examples/combat.rs +++ b/examples/combat.rs @@ -87,7 +87,6 @@ impl Config for Game { fn init(&self, server: &mut Server) { let (_, world) = server.worlds.insert(DimensionId::default(), ()); server.state = Some(server.player_lists.insert(()).0); - world.meta.set_flat(true); let min_y = server.shared.dimension(DimensionId::default()).min_y; @@ -117,7 +116,7 @@ impl Config for Game { } fn update(&self, server: &mut Server) { - let (world_id, world) = server.worlds.iter_mut().next().unwrap(); + let (world_id, _) = server.worlds.iter_mut().next().unwrap(); let current_tick = server.shared.current_tick(); @@ -152,6 +151,7 @@ impl Config for Game { client.state.extra_knockback = true; client.spawn(world_id); + client.set_flat(true); client.set_game_mode(GameMode::Survival); client.teleport( [ diff --git a/examples/conway.rs b/examples/conway.rs index f1402f1..e6bbfc9 100644 --- a/examples/conway.rs +++ b/examples/conway.rs @@ -101,7 +101,6 @@ impl Config for Game { fn init(&self, server: &mut Server) { let world = server.worlds.insert(DimensionId::default(), ()).1; server.state.player_list = Some(server.player_lists.insert(()).0); - world.meta.set_flat(true); for chunk_z in -2..Integer::div_ceil(&(SIZE_X as i32), &16) + 2 { for chunk_x in -2..Integer::div_ceil(&(SIZE_Z as i32), &16) + 2 { @@ -144,6 +143,7 @@ impl Config for Game { } client.spawn(world_id); + client.set_flat(true); client.teleport(spawn_pos, 0.0, 0.0); client.set_player_list(server.state.player_list.clone()); diff --git a/examples/cow_sphere.rs b/examples/cow_sphere.rs index 09c70ee..e17a5d7 100644 --- a/examples/cow_sphere.rs +++ b/examples/cow_sphere.rs @@ -81,7 +81,6 @@ impl Config for Game { fn init(&self, server: &mut Server) { let (world_id, world) = server.worlds.insert(DimensionId::default(), ()); server.state.player_list = Some(server.player_lists.insert(()).0); - world.meta.set_flat(true); let size = 5; for z in -size..size { @@ -127,6 +126,7 @@ impl Config for Game { } client.spawn(world_id); + client.set_flat(true); client.set_game_mode(GameMode::Creative); client.teleport( [ diff --git a/examples/raycast.rs b/examples/raycast.rs index c56621f..97c4969 100644 --- a/examples/raycast.rs +++ b/examples/raycast.rs @@ -76,7 +76,6 @@ impl Config for Game { fn init(&self, server: &mut Server) { let (world_id, world) = server.worlds.insert(DimensionId::default(), ()); server.state = Some(server.player_lists.insert(()).0); - world.meta.set_flat(true); let size = 5; for z in -size..size { @@ -127,6 +126,7 @@ impl Config for Game { } client.spawn(world_id); + client.set_flat(true); client.set_game_mode(GameMode::Creative); client.teleport( [ diff --git a/examples/terrain.rs b/examples/terrain.rs index 7cd0365..309ef55 100644 --- a/examples/terrain.rs +++ b/examples/terrain.rs @@ -84,9 +84,8 @@ impl Config for Game { } fn init(&self, server: &mut Server) { - let (_, world) = server.worlds.insert(DimensionId::default(), ()); + server.worlds.insert(DimensionId::default(), ()); server.state = Some(server.player_lists.insert(()).0); - world.meta.set_flat(true); } fn update(&self, server: &mut Server) { @@ -119,6 +118,7 @@ impl Config for Game { } client.spawn(world_id); + client.set_flat(true); client.set_game_mode(GameMode::Creative); client.teleport([0.0, 200.0, 0.0], 0.0, 0.0); client.set_player_list(server.state.clone()); diff --git a/src/client.rs b/src/client.rs index 409c714..7547972 100644 --- a/src/client.rs +++ b/src/client.rs @@ -227,6 +227,7 @@ pub struct Client { #[bitfield(u16)] struct ClientBits { spawn: bool, + flat: bool, teleported_this_tick: bool, /// If spawn_position or spawn_position_yaw were modified this tick. modified_spawn_position: bool, @@ -238,7 +239,7 @@ struct ClientBits { velocity_modified: bool, created_this_tick: bool, view_distance_modified: bool, - #[bits(6)] + #[bits(5)] _pad: u8, } @@ -325,6 +326,20 @@ impl Client { mem::replace(&mut self.new_player_list, id) } + /// Sets if this client sees the world as superflat. Superflat worlds have + /// a horizon line lower than normal worlds. + /// + /// The player must be spawned for changes to take effect. + pub fn set_flat(&mut self, flat: bool) { + self.bits.set_flat(flat); + } + + /// Gets if this client sees the world as superflat. Superflat worlds have + /// a horizon line lower than normal worlds. + pub fn is_flat(&self) -> bool { + self.bits.flat() + } + /// Changes the world this client is located in and respawns the client. /// This can be used to respawn the client after death. /// @@ -862,7 +877,7 @@ impl Client { let current_tick = shared.current_tick(); // Send the join game packet and other initial packets. We defer this until now - // so that the user can set the client's location, game mode, etc. + // so that the user can set the client's initial location, game mode, etc. if self.created_this_tick() { if let Some(id) = &self.new_player_list { player_lists @@ -899,7 +914,7 @@ impl Client { reduced_debug_info: false, enable_respawn_screen: false, is_debug: false, - is_flat: world.meta.is_flat(), + is_flat: self.bits.flat(), last_death_location: self .death_location .map(|(id, pos)| (ident!("{LIBRARY_NAMESPACE}:dimension_{}", id.0), pos)), @@ -921,7 +936,7 @@ impl Client { game_mode: self.game_mode(), previous_game_mode: self.game_mode(), is_debug: false, - is_flat: false, + is_flat: self.bits.flat(), copy_metadata: true, last_death_location: None, }); @@ -939,7 +954,7 @@ impl Client { game_mode: self.game_mode(), previous_game_mode: self.game_mode(), is_debug: false, - is_flat: world.meta.is_flat(), + is_flat: self.bits.flat(), copy_metadata: true, last_death_location: self .death_location diff --git a/src/world.rs b/src/world.rs index 4aef41f..a5be196 100644 --- a/src/world.rs +++ b/src/world.rs @@ -49,10 +49,7 @@ impl Worlds { state, spatial_index: SpatialIndex::new(), chunks: Chunks::new(self.server.clone(), dim), - meta: WorldMeta { - dimension: dim, - is_flat: false, - }, + meta: WorldMeta { dimension: dim }, }); (WorldId(id), world) @@ -136,8 +133,6 @@ pub struct World { /// Contains miscellaneous world state. pub struct WorldMeta { dimension: DimensionId, - is_flat: bool, - // TODO: time, weather } impl WorldMeta { @@ -145,18 +140,4 @@ impl WorldMeta { pub fn dimension(&self) -> DimensionId { self.dimension } - - /// Gets if this world is considered a superflat world. Superflat worlds - /// have a horizon line at y=0. - pub fn is_flat(&self) -> bool { - self.is_flat - } - - /// Sets if this world is considered a superflat world. Superflat worlds - /// have a horizon line at y=0. - /// - /// Clients already in the world must be respawned to see any changes. - pub fn set_flat(&mut self, flat: bool) { - self.is_flat = flat; - } }