Skip to content

Commit

Permalink
bubble columns
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Jan 10, 2025
1 parent 63b4406 commit 579aefd
Show file tree
Hide file tree
Showing 18 changed files with 1,266 additions and 840 deletions.
119 changes: 61 additions & 58 deletions azalea-block/src/fluid_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

#[derive(Clone, Debug)]
pub struct FluidState {
pub fluid: azalea_registry::Fluid,
pub kind: FluidKind,
/// 0 = empty, 8 = full, 9 = max.
///
/// 9 is meant to be used when there's another fluid block of the same type
Expand All @@ -23,42 +23,44 @@ pub struct FluidState {
/// set (see FlowingFluid.getFlowing)
pub falling: bool,
}
#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub enum FluidKind {
#[default]
Empty,
Water,
Lava,
}
impl FluidState {
pub fn new_source_block(kind: FluidKind, falling: bool) -> Self {
Self {
kind,
amount: 8,
falling,
}
}

/// A floating point number in between 0 and 1 representing the height (as a
/// percentage of a full block) of the fluid.
pub fn height(&self) -> f32 {
self.amount as f32 / 9.
}
pub fn is_empty(&self) -> bool {
self.amount == 0
}

pub fn affects_flow(&self, other: &FluidState) -> bool {
other.amount == 0 || self.is_same_kind(other)
}

pub fn is_same_kind(&self, other: &FluidState) -> bool {
(other.is_water() && self.is_water())
|| (other.is_lava() && self.is_lava())
|| (self.amount == 0 && other.amount == 0)
}

pub fn is_water(&self) -> bool {
matches!(
self.fluid,
azalea_registry::Fluid::Water | azalea_registry::Fluid::FlowingWater
)
}

pub fn is_lava(&self) -> bool {
matches!(
self.fluid,
azalea_registry::Fluid::Lava | azalea_registry::Fluid::FlowingLava
)
(other.kind == self.kind) || (self.amount == 0 && other.amount == 0)
}
}

impl Default for FluidState {
fn default() -> Self {
Self {
fluid: azalea_registry::Fluid::Empty,
kind: FluidKind::Empty,
amount: 0,
falling: false,
}
Expand All @@ -74,38 +76,47 @@ impl From<BlockState> for FluidState {
.property::<crate::properties::Waterlogged>()
.unwrap_or_default()
{
Self {
fluid: azalea_registry::Fluid::Water,
return Self {
kind: FluidKind::Water,
amount: 8,
falling: false,
}
} else {
let block = Box::<dyn Block>::from(state);
if let Some(water) = block.downcast_ref::<crate::blocks::Water>() {
Self {
fluid: azalea_registry::Fluid::Water,
amount: to_or_from_legacy_fluid_level(water.level as u8),
falling: false,
}
} else if let Some(lava) = block.downcast_ref::<crate::blocks::Lava>() {
Self {
fluid: azalea_registry::Fluid::Lava,
amount: to_or_from_legacy_fluid_level(lava.level as u8),
};
}

let registry_block = azalea_registry::Block::from(state);
match registry_block {
azalea_registry::Block::Water => {
let level = state
.property::<crate::properties::WaterLevel>()
.expect("water block should always have WaterLevel");
return Self {
kind: FluidKind::Water,
amount: to_or_from_legacy_fluid_level(level as u8),
falling: false,
}
} else {
Self {
fluid: azalea_registry::Fluid::Empty,
amount: 0,
};
}
azalea_registry::Block::Lava => {
let level = state
.property::<crate::properties::LavaLevel>()
.expect("lava block should always have LavaLevel");
return Self {
kind: FluidKind::Lava,
amount: to_or_from_legacy_fluid_level(level as u8),
falling: false,
}
};
}
azalea_registry::Block::BubbleColumn => {
return Self::new_source_block(FluidKind::Water, false);
}
_ => {}
}

Self::default()
}
}

/// Sometimes Minecraft represents fluids with 0 being the empty and 8 being
/// full, and sometimes it's the opposite. You can use this function to convert
/// Sometimes Minecraft represents fluids with 0 being empty and 8 being full,
/// and sometimes it's the opposite. You can use this function to convert
/// in between those two representations.
///
/// You usually don't need to call this yourself, see [`FluidState`].
Expand All @@ -116,22 +127,14 @@ pub fn to_or_from_legacy_fluid_level(level: u8) -> u8 {

impl From<FluidState> for BlockState {
fn from(state: FluidState) -> Self {
match state.fluid {
azalea_registry::Fluid::Empty => BlockState::AIR,
azalea_registry::Fluid::Water | azalea_registry::Fluid::FlowingWater => {
BlockState::from(crate::blocks::Water {
level: crate::properties::WaterLevel::from(
state.amount as BlockStateIntegerRepr,
),
})
}
azalea_registry::Fluid::Lava | azalea_registry::Fluid::FlowingLava => {
BlockState::from(crate::blocks::Lava {
level: crate::properties::LavaLevel::from(
state.amount as BlockStateIntegerRepr,
),
})
}
match state.kind {
FluidKind::Empty => BlockState::AIR,
FluidKind::Water => BlockState::from(crate::blocks::Water {
level: crate::properties::WaterLevel::from(state.amount as BlockStateIntegerRepr),
}),
FluidKind::Lava => BlockState::from(crate::blocks::Lava {
level: crate::properties::LavaLevel::from(state.amount as BlockStateIntegerRepr),
}),
}
}
}
7 changes: 7 additions & 0 deletions azalea-client/src/packet_handling/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ pub fn process_packet_events(ecs: &mut World) {
**position = new_pos;
}

// old_pos is set to the current position when we're teleported
physics.set_old_pos(&position);

// send the relevant packets

send_packet_events.send(SendPacketEvent::new(
Expand Down Expand Up @@ -853,10 +856,14 @@ pub fn process_packet_events(ecs: &mut World) {
if new_pos != **position {
**position = new_pos;
}
let position = position.clone();

Check warning on line 859 in azalea-client/src/packet_handling/game.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `Position` which implements the `Copy` trait

warning: using `clone` on type `Position` which implements the `Copy` trait --> azalea-client/src/packet_handling/game.rs:859:40 | 859 | let position = position.clone(); | ^^^^^^^^^^^^^^^^ help: try dereferencing it: `*position` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy = note: `#[warn(clippy::clone_on_copy)]` on by default
let mut look_direction = entity.get_mut::<LookDirection>().unwrap();
if new_look_direction != *look_direction {
*look_direction = new_look_direction;
}
// old_pos is set to the current position when we're teleported
let mut physics = entity.get_mut::<Physics>().unwrap();
physics.set_old_pos(&position);
}),
});

Expand Down
Loading

0 comments on commit 579aefd

Please sign in to comment.