Skip to content

Commit

Permalink
More packet writers for layers
Browse files Browse the repository at this point in the history
  • Loading branch information
rj00a committed Jul 26, 2023
1 parent b305165 commit 43a181d
Show file tree
Hide file tree
Showing 9 changed files with 540 additions and 72 deletions.
54 changes: 51 additions & 3 deletions crates/valence_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ pub struct View {

impl ViewItem<'_> {
pub fn get(&self) -> ChunkView {
ChunkView::new(self.pos.chunk_pos(), self.view_dist.0)
ChunkView::new(self.pos.to_chunk_pos(), self.view_dist.0)
}
}

Expand Down Expand Up @@ -692,8 +692,16 @@ fn handle_layer_messages(
mut visible_entity_layers,
old_visible_entity_layers,
)| {
let block_pos = BlockPos::from_pos(old_view.old_pos.get());
let old_view = old_view.get();

fn in_radius(p0: BlockPos, p1: BlockPos, radius_squared: u32) -> bool {
let dist_squared =
(p1.x - p0.x).pow(2) + (p1.y - p0.y).pow(2) + (p1.z - p0.z).pow(2);

dist_squared as u32 <= radius_squared
}

// Chunk layer messages
if let Ok(chunk_layer) = chunk_layers.get(old_visible_chunk_layer.get()) {
let messages = chunk_layer.messages();
Expand All @@ -720,6 +728,28 @@ fn handle_layer_messages(
valence_layer::chunk::LocalMsg::PacketAt { .. } => {
client.write_packet_bytes(&bytes[range]);
}
valence_layer::chunk::LocalMsg::PacketAtExcept { except, .. } => {
if self_entity != except {
client.write_packet_bytes(&bytes[range]);
}
}
valence_layer::chunk::LocalMsg::RadiusAt {
center,
radius_squared,
} => {
if in_radius(block_pos, center, radius_squared) {
client.write_packet_bytes(&bytes[range]);
}
}
valence_layer::chunk::LocalMsg::RadiusAtExcept {
center,
radius_squared,
except,
} => {
if self_entity != except && in_radius(block_pos, center, radius_squared) {
client.write_packet_bytes(&bytes[range]);
}
}
valence_layer::chunk::LocalMsg::ChangeBiome { pos } => {
chunk_biome_buf.push(ChunkBiome {
pos,
Expand Down Expand Up @@ -791,6 +821,24 @@ fn handle_layer_messages(
client.write_packet_bytes(&bytes[range]);
}
}
valence_layer::entity::LocalMsg::RadiusAt {
center,
radius_squared,
} => {
if in_radius(block_pos, center, radius_squared) {
client.write_packet_bytes(&bytes[range]);
}
}
valence_layer::entity::LocalMsg::RadiusAtExcept {
center,
radius_squared,
except,
} => {
if self_entity != except && in_radius(block_pos, center, radius_squared)
{
client.write_packet_bytes(&bytes[range]);
}
}
valence_layer::entity::LocalMsg::SpawnEntity { pos: _, src_layer } => {
if !old_visible_entity_layers.0.contains(&src_layer) {
let mut bytes = &bytes[range];
Expand Down Expand Up @@ -903,8 +951,8 @@ fn update_view_and_layers(
view_dist,
old_view_dist,
)| {
let view = ChunkView::new(ChunkPos::from_dvec3(pos.0), view_dist.0);
let old_view = ChunkView::new(ChunkPos::from_dvec3(old_pos.get()), old_view_dist.0);
let view = ChunkView::new(ChunkPos::from_pos(pos.0), view_dist.0);
let old_view = ChunkView::new(ChunkPos::from_pos(old_pos.get()), old_view_dist.0);

// Make sure the center chunk is set before loading chunks! Otherwise the client
// may ignore the chunk.
Expand Down
20 changes: 15 additions & 5 deletions crates/valence_core/src/block_pos.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::io::Write;

use anyhow::bail;
use glam::DVec3;

use crate::chunk_pos::ChunkPos;
use crate::direction::Direction;
use crate::protocol::{Decode, Encode};

/// Represents an absolute block position in world space.
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, Debug)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct BlockPos {
pub x: i32,
pub y: i32,
Expand All @@ -19,9 +21,17 @@ impl BlockPos {
Self { x, y, z }
}

/// Returns the block position a point is contained within.
pub fn at(pos: impl Into<[f64; 3]>) -> Self {
pos.into().map(|a| a.floor() as i32).into()
/// Returns the block position a point in world space is contained within.
pub fn from_pos(pos: DVec3) -> Self {
Self {
x: pos.x.floor() as i32,
y: pos.y.floor() as i32,
z: pos.z.floor() as i32,
}
}

pub const fn to_chunk_pos(self) -> ChunkPos {
ChunkPos::from_block_pos(self)
}

/// Get a new [`BlockPos`] that is adjacent to this position in `dir`
Expand All @@ -35,7 +45,7 @@ impl BlockPos {
/// let adj = pos.get_in_direction(Direction::South);
/// assert_eq!(adj, BlockPos::new(0, 0, 1));
/// ```
pub fn get_in_direction(self, dir: Direction) -> BlockPos {
pub const fn get_in_direction(self, dir: Direction) -> BlockPos {
match dir {
Direction::Down => BlockPos::new(self.x, self.y - 1, self.z),
Direction::Up => BlockPos::new(self.x, self.y + 1, self.z),
Expand Down
12 changes: 3 additions & 9 deletions crates/valence_core/src/chunk_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@ impl ChunkPos {

/// Constructs a chunk position from a position in world space. Only the `x`
/// and `z` components are used.
pub fn from_dvec3(pos: DVec3) -> Self {
Self::at(pos.x, pos.z)
pub fn from_pos(pos: DVec3) -> Self {
Self::new((pos.x / 16.0).floor() as i32, (pos.z / 16.0).floor() as i32)
}

pub fn from_block_pos(pos: BlockPos) -> Self {
pub const fn from_block_pos(pos: BlockPos) -> Self {
Self::new(pos.x.div_euclid(16), pos.z.div_euclid(16))
}

/// Takes an X and Z position in world space and returns the chunk position
/// containing the point.
pub fn at(x: f64, z: f64) -> Self {
Self::new((x / 16.0).floor() as i32, (z / 16.0).floor() as i32)
}

pub const fn distance_squared(self, other: Self) -> u64 {
let diff_x = other.x as i64 - self.x as i64;
let diff_z = other.z as i64 - self.z as i64;
Expand Down
21 changes: 15 additions & 6 deletions crates/valence_entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub use manager::EntityManager;
use paste::paste;
use tracing::warn;
use tracked_data::TrackedData;
use valence_core::block_pos::BlockPos;
use valence_core::chunk_pos::ChunkPos;
use valence_core::despawn::Despawned;
use valence_core::protocol::var_int::VarInt;
Expand Down Expand Up @@ -227,8 +228,12 @@ impl Position {
Self(pos.into())
}

pub fn chunk_pos(&self) -> ChunkPos {
ChunkPos::from_dvec3(self.0)
pub fn to_chunk_pos(self) -> ChunkPos {
ChunkPos::from_pos(self.0)
}

pub fn to_block_pos(self) -> BlockPos {
BlockPos::from_pos(self.0)
}

pub fn get(self) -> DVec3 {
Expand All @@ -249,20 +254,24 @@ impl PartialEq<OldPosition> for Position {
/// The value of [`Position`] from the end of the previous tick.
///
/// **NOTE**: You should not modify this component after the entity is spawned.
#[derive(Component, Copy, Clone, PartialEq, Default, Debug)]
#[derive(Component, Clone, PartialEq, Default, Debug)]
pub struct OldPosition(DVec3);

impl OldPosition {
pub fn new(pos: impl Into<DVec3>) -> Self {
Self(pos.into())
}

pub fn get(self) -> DVec3 {
pub fn get(&self) -> DVec3 {
self.0
}

pub fn chunk_pos(self) -> ChunkPos {
ChunkPos::from_dvec3(self.0)
pub fn chunk_pos(&self) -> ChunkPos {
ChunkPos::from_pos(self.0)
}

pub fn to_block_pos(&self) -> BlockPos {
BlockPos::from_pos(self.0)
}
}

Expand Down
Loading

0 comments on commit 43a181d

Please sign in to comment.