Make superflat option per client rather than per world

This commit is contained in:
Ryan 2022-08-09 15:08:42 -07:00
parent a5a560220c
commit 3649a8ca99
7 changed files with 28 additions and 32 deletions

View file

@ -87,7 +87,6 @@ impl Config for Game {
fn init(&self, server: &mut Server<Self>) { fn init(&self, server: &mut Server<Self>) {
let (_, world) = server.worlds.insert(DimensionId::default(), ()); let (_, world) = server.worlds.insert(DimensionId::default(), ());
server.state = Some(server.player_lists.insert(()).0); server.state = Some(server.player_lists.insert(()).0);
world.meta.set_flat(true);
let min_y = server.shared.dimension(DimensionId::default()).min_y; let min_y = server.shared.dimension(DimensionId::default()).min_y;
@ -117,7 +116,7 @@ impl Config for Game {
} }
fn update(&self, server: &mut Server<Self>) { fn update(&self, server: &mut Server<Self>) {
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(); let current_tick = server.shared.current_tick();
@ -152,6 +151,7 @@ impl Config for Game {
client.state.extra_knockback = true; client.state.extra_knockback = true;
client.spawn(world_id); client.spawn(world_id);
client.set_flat(true);
client.set_game_mode(GameMode::Survival); client.set_game_mode(GameMode::Survival);
client.teleport( client.teleport(
[ [

View file

@ -101,7 +101,6 @@ impl Config for Game {
fn init(&self, server: &mut Server<Self>) { fn init(&self, server: &mut Server<Self>) {
let world = server.worlds.insert(DimensionId::default(), ()).1; let world = server.worlds.insert(DimensionId::default(), ()).1;
server.state.player_list = Some(server.player_lists.insert(()).0); 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_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 { 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.spawn(world_id);
client.set_flat(true);
client.teleport(spawn_pos, 0.0, 0.0); client.teleport(spawn_pos, 0.0, 0.0);
client.set_player_list(server.state.player_list.clone()); client.set_player_list(server.state.player_list.clone());

View file

@ -81,7 +81,6 @@ impl Config for Game {
fn init(&self, server: &mut Server<Self>) { fn init(&self, server: &mut Server<Self>) {
let (world_id, world) = server.worlds.insert(DimensionId::default(), ()); let (world_id, world) = server.worlds.insert(DimensionId::default(), ());
server.state.player_list = Some(server.player_lists.insert(()).0); server.state.player_list = Some(server.player_lists.insert(()).0);
world.meta.set_flat(true);
let size = 5; let size = 5;
for z in -size..size { for z in -size..size {
@ -127,6 +126,7 @@ impl Config for Game {
} }
client.spawn(world_id); client.spawn(world_id);
client.set_flat(true);
client.set_game_mode(GameMode::Creative); client.set_game_mode(GameMode::Creative);
client.teleport( client.teleport(
[ [

View file

@ -76,7 +76,6 @@ impl Config for Game {
fn init(&self, server: &mut Server<Self>) { fn init(&self, server: &mut Server<Self>) {
let (world_id, world) = server.worlds.insert(DimensionId::default(), ()); let (world_id, world) = server.worlds.insert(DimensionId::default(), ());
server.state = Some(server.player_lists.insert(()).0); server.state = Some(server.player_lists.insert(()).0);
world.meta.set_flat(true);
let size = 5; let size = 5;
for z in -size..size { for z in -size..size {
@ -127,6 +126,7 @@ impl Config for Game {
} }
client.spawn(world_id); client.spawn(world_id);
client.set_flat(true);
client.set_game_mode(GameMode::Creative); client.set_game_mode(GameMode::Creative);
client.teleport( client.teleport(
[ [

View file

@ -84,9 +84,8 @@ impl Config for Game {
} }
fn init(&self, server: &mut Server<Self>) { fn init(&self, server: &mut Server<Self>) {
let (_, world) = server.worlds.insert(DimensionId::default(), ()); server.worlds.insert(DimensionId::default(), ());
server.state = Some(server.player_lists.insert(()).0); server.state = Some(server.player_lists.insert(()).0);
world.meta.set_flat(true);
} }
fn update(&self, server: &mut Server<Self>) { fn update(&self, server: &mut Server<Self>) {
@ -119,6 +118,7 @@ impl Config for Game {
} }
client.spawn(world_id); client.spawn(world_id);
client.set_flat(true);
client.set_game_mode(GameMode::Creative); client.set_game_mode(GameMode::Creative);
client.teleport([0.0, 200.0, 0.0], 0.0, 0.0); client.teleport([0.0, 200.0, 0.0], 0.0, 0.0);
client.set_player_list(server.state.clone()); client.set_player_list(server.state.clone());

View file

@ -227,6 +227,7 @@ pub struct Client<C: Config> {
#[bitfield(u16)] #[bitfield(u16)]
struct ClientBits { struct ClientBits {
spawn: bool, spawn: bool,
flat: bool,
teleported_this_tick: bool, teleported_this_tick: bool,
/// If spawn_position or spawn_position_yaw were modified this tick. /// If spawn_position or spawn_position_yaw were modified this tick.
modified_spawn_position: bool, modified_spawn_position: bool,
@ -238,7 +239,7 @@ struct ClientBits {
velocity_modified: bool, velocity_modified: bool,
created_this_tick: bool, created_this_tick: bool,
view_distance_modified: bool, view_distance_modified: bool,
#[bits(6)] #[bits(5)]
_pad: u8, _pad: u8,
} }
@ -325,6 +326,20 @@ impl<C: Config> Client<C> {
mem::replace(&mut self.new_player_list, id) 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. /// Changes the world this client is located in and respawns the client.
/// This can be used to respawn the client after death. /// This can be used to respawn the client after death.
/// ///
@ -862,7 +877,7 @@ impl<C: Config> Client<C> {
let current_tick = shared.current_tick(); let current_tick = shared.current_tick();
// Send the join game packet and other initial packets. We defer this until now // 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 self.created_this_tick() {
if let Some(id) = &self.new_player_list { if let Some(id) = &self.new_player_list {
player_lists player_lists
@ -899,7 +914,7 @@ impl<C: Config> Client<C> {
reduced_debug_info: false, reduced_debug_info: false,
enable_respawn_screen: false, enable_respawn_screen: false,
is_debug: false, is_debug: false,
is_flat: world.meta.is_flat(), is_flat: self.bits.flat(),
last_death_location: self last_death_location: self
.death_location .death_location
.map(|(id, pos)| (ident!("{LIBRARY_NAMESPACE}:dimension_{}", id.0), pos)), .map(|(id, pos)| (ident!("{LIBRARY_NAMESPACE}:dimension_{}", id.0), pos)),
@ -921,7 +936,7 @@ impl<C: Config> Client<C> {
game_mode: self.game_mode(), game_mode: self.game_mode(),
previous_game_mode: self.game_mode(), previous_game_mode: self.game_mode(),
is_debug: false, is_debug: false,
is_flat: false, is_flat: self.bits.flat(),
copy_metadata: true, copy_metadata: true,
last_death_location: None, last_death_location: None,
}); });
@ -939,7 +954,7 @@ impl<C: Config> Client<C> {
game_mode: self.game_mode(), game_mode: self.game_mode(),
previous_game_mode: self.game_mode(), previous_game_mode: self.game_mode(),
is_debug: false, is_debug: false,
is_flat: world.meta.is_flat(), is_flat: self.bits.flat(),
copy_metadata: true, copy_metadata: true,
last_death_location: self last_death_location: self
.death_location .death_location

View file

@ -49,10 +49,7 @@ impl<C: Config> Worlds<C> {
state, state,
spatial_index: SpatialIndex::new(), spatial_index: SpatialIndex::new(),
chunks: Chunks::new(self.server.clone(), dim), chunks: Chunks::new(self.server.clone(), dim),
meta: WorldMeta { meta: WorldMeta { dimension: dim },
dimension: dim,
is_flat: false,
},
}); });
(WorldId(id), world) (WorldId(id), world)
@ -136,8 +133,6 @@ pub struct World<C: Config> {
/// Contains miscellaneous world state. /// Contains miscellaneous world state.
pub struct WorldMeta { pub struct WorldMeta {
dimension: DimensionId, dimension: DimensionId,
is_flat: bool,
// TODO: time, weather
} }
impl WorldMeta { impl WorldMeta {
@ -145,18 +140,4 @@ impl WorldMeta {
pub fn dimension(&self) -> DimensionId { pub fn dimension(&self) -> DimensionId {
self.dimension 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;
}
} }