1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 23:44:38 +00:00
This commit is contained in:
Ubuntu 2022-08-30 19:56:14 +00:00
commit a6c5017e38
7 changed files with 145 additions and 153 deletions

View file

@ -14,6 +14,19 @@ pub struct AABB {
pub max_z: f64, pub max_z: f64,
} }
pub struct ClipPointOpts<'a> {
pub t: &'a mut [f64],
pub approach_dir: Option<Direction>,
pub delta: &'a Vec3,
pub begin: f64,
pub min_x: f64,
pub max_x: f64,
pub min_z: f64,
pub max_z: f64,
pub result_dir: Direction,
pub start: &'a Vec3,
}
impl AABB { impl AABB {
pub fn contract(&self, x: f64, y: f64, z: f64) -> AABB { pub fn contract(&self, x: f64, y: f64, z: f64) -> AABB {
let mut min_x = self.min_x; let mut min_x = self.min_x;
@ -216,10 +229,7 @@ impl AABB {
let x = max.x - min.x; let x = max.x - min.x;
let y = max.y - min.y; let y = max.y - min.y;
let z = max.z - min.z; let z = max.z - min.z;
let dir = self.get_direction(self, min, &mut t, None, &Vec3 { x, y, z }); let _dir = self.get_direction(self, min, &mut t, None, &Vec3 { x, y, z })?;
if dir.is_none() {
return None;
}
let t = t[0]; let t = t[0];
Some(min.add(t * x, t * y, t * z)) Some(min.add(t * x, t * y, t * z))
} }
@ -240,13 +250,11 @@ impl AABB {
for aabb in boxes { for aabb in boxes {
dir = self.get_direction(aabb, from, &mut t, dir, &Vec3 { x, y, z }); dir = self.get_direction(aabb, from, &mut t, dir, &Vec3 { x, y, z });
} }
if dir.is_none() { let dir = dir?;
return None;
}
let t = t[0]; let t = t[0];
Some(BlockHitResult { Some(BlockHitResult {
location: from.add(t * x, t * y, t * z), location: from.add(t * x, t * y, t * z),
direction: dir.unwrap(), direction: dir,
block_pos: *pos, block_pos: *pos,
inside: false, inside: false,
miss: false, miss: false,
@ -262,151 +270,139 @@ impl AABB {
delta: &Vec3, delta: &Vec3,
) -> Option<Direction> { ) -> Option<Direction> {
if delta.x > EPSILON { if delta.x > EPSILON {
return self.clip_point( return self.clip_point(ClipPointOpts {
t, t,
dir, approach_dir: dir,
delta, delta,
aabb.min_x, begin: aabb.min_x,
aabb.min_y, min_x: aabb.min_y,
aabb.max_y, max_x: aabb.max_y,
aabb.min_z, min_z: aabb.min_z,
aabb.max_z, max_z: aabb.max_z,
Direction::West, result_dir: Direction::West,
from, start: from,
); });
} else if delta.x < -EPSILON { } else if delta.x < -EPSILON {
return self.clip_point( return self.clip_point(ClipPointOpts {
t, t,
dir, approach_dir: dir,
delta, delta,
aabb.max_x, begin: aabb.max_x,
aabb.min_y, min_x: aabb.min_y,
aabb.max_y, max_x: aabb.max_y,
aabb.min_z, min_z: aabb.min_z,
aabb.max_z, max_z: aabb.max_z,
Direction::East, result_dir: Direction::East,
from, start: from,
); });
} }
if delta.y > EPSILON { if delta.y > EPSILON {
return self.clip_point( return self.clip_point(ClipPointOpts {
t, t,
dir, approach_dir: dir,
&Vec3 { delta: &Vec3 {
x: delta.y, x: delta.y,
y: delta.z, y: delta.z,
z: delta.x, z: delta.x,
}, },
aabb.min_y, begin: aabb.min_y,
aabb.min_z, min_x: aabb.min_z,
aabb.max_z, max_x: aabb.max_z,
aabb.min_x, min_z: aabb.min_x,
aabb.max_x, max_z: aabb.max_x,
Direction::Down, result_dir: Direction::Down,
&Vec3 { start: &Vec3 {
x: from.y, x: from.y,
y: from.z, y: from.z,
z: from.x, z: from.x,
}, },
); });
} else if delta.y < -EPSILON { } else if delta.y < -EPSILON {
return self.clip_point( return self.clip_point(ClipPointOpts {
t, t,
dir, approach_dir: dir,
&Vec3 { delta: &Vec3 {
x: delta.y, x: delta.y,
y: delta.z, y: delta.z,
z: delta.x, z: delta.x,
}, },
aabb.max_y, begin: aabb.max_y,
aabb.min_z, min_x: aabb.min_z,
aabb.max_z, max_x: aabb.max_z,
aabb.min_x, min_z: aabb.min_x,
aabb.max_x, max_z: aabb.max_x,
Direction::Up, result_dir: Direction::Up,
&Vec3 { start: &Vec3 {
x: from.y, x: from.y,
y: from.z, y: from.z,
z: from.x, z: from.x,
}, },
); });
} }
if delta.z > EPSILON { if delta.z > EPSILON {
return self.clip_point( return self.clip_point(ClipPointOpts {
t, t,
dir, approach_dir: dir,
&Vec3 { delta: &Vec3 {
x: delta.z, x: delta.z,
y: delta.x, y: delta.x,
z: delta.y, z: delta.y,
}, },
aabb.min_z, begin: aabb.min_z,
aabb.min_x, min_x: aabb.min_x,
aabb.max_x, max_x: aabb.max_x,
aabb.min_y, min_z: aabb.min_y,
aabb.max_y, max_z: aabb.max_y,
Direction::North, result_dir: Direction::North,
&Vec3 { start: &Vec3 {
x: from.z, x: from.z,
y: from.x, y: from.x,
z: from.y, z: from.y,
}, },
); });
} else if delta.z < -EPSILON { } else if delta.z < -EPSILON {
return self.clip_point( return self.clip_point(ClipPointOpts {
t, t,
dir, approach_dir: dir,
&Vec3 { delta: &Vec3 {
x: delta.z, x: delta.z,
y: delta.x, y: delta.x,
z: delta.y, z: delta.y,
}, },
aabb.max_z, begin: aabb.max_z,
aabb.min_x, min_x: aabb.min_x,
aabb.max_x, max_x: aabb.max_x,
aabb.min_y, min_z: aabb.min_y,
aabb.max_y, max_z: aabb.max_y,
Direction::South, result_dir: Direction::South,
&Vec3 { start: &Vec3 {
x: from.z, x: from.z,
y: from.x, y: from.x,
z: from.y, z: from.y,
}, },
); });
} }
dir dir
} }
fn clip_point( fn clip_point(&self, opts: ClipPointOpts) -> Option<Direction> {
&self, let t_x = (opts.begin - opts.start.x) / opts.delta.x;
t: &mut [f64], let t_y = (opts.start.y + t_x) / opts.delta.y;
approach_dir: Option<Direction>, let t_z = (opts.start.z + t_x) / opts.delta.z;
delta: &Vec3,
begin: f64,
min_x: f64,
max_x: f64,
min_z: f64,
max_z: f64,
result_dir: Direction,
start: &Vec3,
) -> Option<Direction> {
let t_x = (begin - start.x) / delta.x;
let t_y = (start.y + t_x) / delta.y;
let t_z = (start.z + t_x) / delta.z;
if 0.0 < t_x if 0.0 < t_x
&& t_x < t[0] && t_x < opts.t[0]
&& min_x - EPSILON < t_y && opts.min_x - EPSILON < t_y
&& t_y < max_x + EPSILON && t_y < opts.max_x + EPSILON
&& min_z - EPSILON < t_z && opts.min_z - EPSILON < t_z
&& t_z < max_z + EPSILON && t_z < opts.max_z + EPSILON
{ {
t[0] = t_x; opts.t[0] = t_x;
Some(result_dir) Some(opts.result_dir)
} else { } else {
approach_dir opts.approach_dir
} }
} }

View file

@ -304,9 +304,9 @@ impl McBufReadable for BlockPos {
fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
let val = u64::read_from(buf)?; let val = u64::read_from(buf)?;
println!("reading blockpos from {}", val); println!("reading blockpos from {}", val);
let x = (val << 64 - X_OFFSET - PACKED_X_LENGTH >> 64 - PACKED_X_LENGTH) as i32; let x = (val << (64 - X_OFFSET - PACKED_X_LENGTH) >> (64 - PACKED_X_LENGTH)) as i32;
let y = (val << 64 - PACKED_Y_LENGTH >> 64 - PACKED_Y_LENGTH) as i32; let y = (val << (64 - PACKED_Y_LENGTH) >> (64 - PACKED_Y_LENGTH)) as i32;
let z = (val << 64 - Z_OFFSET - PACKED_Z_LENGTH >> 64 - PACKED_Z_LENGTH) as i32; let z = (val << (64 - Z_OFFSET - PACKED_Z_LENGTH) >> (64 - PACKED_Z_LENGTH)) as i32;
Ok(BlockPos { x, y, z }) Ok(BlockPos { x, y, z })
} }
} }
@ -335,7 +335,7 @@ impl McBufWritable for BlockPos {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut val: u64 = 0; let mut val: u64 = 0;
val |= ((self.x as u64) & PACKED_X_MASK) << X_OFFSET; val |= ((self.x as u64) & PACKED_X_MASK) << X_OFFSET;
val |= ((self.y as u64) & PACKED_Y_MASK) << 0; val |= (self.y as u64) & PACKED_Y_MASK;
val |= ((self.z as u64) & PACKED_Z_MASK) << Z_OFFSET; val |= ((self.z as u64) & PACKED_Z_MASK) << Z_OFFSET;
val.write_into(buf) val.write_into(buf)
} }

View file

@ -2,6 +2,7 @@ mod data;
mod dimensions; mod dimensions;
use crate::Dimension; use crate::Dimension;
use azalea_block::BlockState;
use azalea_core::{BlockPos, Vec3, AABB}; use azalea_core::{BlockPos, Vec3, AABB};
pub use data::*; pub use data::*;
pub use dimensions::*; pub use dimensions::*;
@ -36,7 +37,7 @@ impl<'d> EntityRef<'d> {
} }
pub fn make_bounding_box(&self) -> AABB { pub fn make_bounding_box(&self) -> AABB {
self.dimensions.make_bounding_box(&self.pos()) self.dimensions.make_bounding_box(self.pos())
} }
/// Get the position of the block below the entity, but a little lower. /// Get the position of the block below the entity, but a little lower.
@ -63,20 +64,20 @@ impl<'d> EntityRef<'d> {
let pos = BlockPos { x, y, z }; let pos = BlockPos { x, y, z };
// TODO: check if block below is a fence, wall, or fence gate // TODO: check if block below is a fence, wall, or fence gate
// let block_pos = pos.below(); let block_pos = pos.below();
// let block_state = dimension.get_block_state(&block_pos); let block_state = self.dimension.get_block_state(&block_pos);
// if block_state == Some(BlockState::Air) { if block_state == Some(BlockState::Air) {
// let block_pos_below = block_pos.below(); let block_pos_below = block_pos.below();
// let block_state_below = dimension.get_block_state(&block_pos_below); let block_state_below = self.dimension.get_block_state(&block_pos_below);
// if let Some(block_state_below) = block_state_below { if let Some(_block_state_below) = block_state_below {
// if block_state_below.is_fence() // if block_state_below.is_fence()
// || block_state_below.is_wall() // || block_state_below.is_wall()
// || block_state_below.is_fence_gate() // || block_state_below.is_fence_gate()
// { // {
// return block_pos_below; // return block_pos_below;
// } // }
// } }
// } }
pos pos
} }
@ -102,6 +103,9 @@ impl<'d> EntityMut<'d> {
/// Sets the position of the entity. This doesn't update the cache in /// Sets the position of the entity. This doesn't update the cache in
/// azalea-world, and should only be used within azalea-world! /// azalea-world, and should only be used within azalea-world!
///
/// # Safety
/// Cached position in the dimension must be updated.
pub unsafe fn move_unchecked(&mut self, new_pos: Vec3) { pub unsafe fn move_unchecked(&mut self, new_pos: Vec3) {
self.pos = new_pos; self.pos = new_pos;
let bounding_box = self.make_bounding_box(); let bounding_box = self.make_bounding_box();
@ -147,7 +151,7 @@ impl<'d> EntityMut<'d> {
} }
pub fn make_bounding_box(&self) -> AABB { pub fn make_bounding_box(&self) -> AABB {
self.dimensions.make_bounding_box(&self.pos()) self.dimensions.make_bounding_box(self.pos())
} }
/// Get the position of the block below the entity, but a little lower. /// Get the position of the block below the entity, but a little lower.
@ -174,20 +178,20 @@ impl<'d> EntityMut<'d> {
let pos = BlockPos { x, y, z }; let pos = BlockPos { x, y, z };
// TODO: check if block below is a fence, wall, or fence gate // TODO: check if block below is a fence, wall, or fence gate
// let block_pos = pos.below(); let block_pos = pos.below();
// let block_state = dimension.get_block_state(&block_pos); let block_state = self.dimension.get_block_state(&block_pos);
// if block_state == Some(BlockState::Air) { if block_state == Some(BlockState::Air) {
// let block_pos_below = block_pos.below(); let block_pos_below = block_pos.below();
// let block_state_below = dimension.get_block_state(&block_pos_below); let block_state_below = self.dimension.get_block_state(&block_pos_below);
// if let Some(block_state_below) = block_state_below { if let Some(_block_state_below) = block_state_below {
// if block_state_below.is_fence() // if block_state_below.is_fence()
// || block_state_below.is_wall() // || block_state_below.is_wall()
// || block_state_below.is_fence_gate() // || block_state_below.is_fence_gate()
// { // {
// return block_pos_below; // return block_pos_below;
// } // }
// } }
// } }
pos pos
} }

View file

@ -63,7 +63,7 @@ impl EntityStorage {
/// Get a mutable reference to an entity by its id. /// Get a mutable reference to an entity by its id.
#[inline] #[inline]
pub fn get_mut_by_id<'d>(&'d mut self, id: u32) -> Option<&'d mut EntityData> { pub fn get_mut_by_id(&mut self, id: u32) -> Option<&mut EntityData> {
self.data_by_id.get_mut(&id) self.data_by_id.get_mut(&id)
} }

View file

@ -123,23 +123,16 @@ impl Dimension {
self.entity_storage.get_mut_by_id(id) self.entity_storage.get_mut_by_id(id)
} }
pub fn entity<'d>(&'d self, id: u32) -> Option<EntityRef<'d>> { pub fn entity(&self, id: u32) -> Option<EntityRef> {
let entity_data = self.entity_storage.get_by_id(id); let entity_data = self.entity_storage.get_by_id(id)?;
if let Some(entity_data) = entity_data { Some(EntityRef::new(self, id, entity_data))
Some(EntityRef::new(self, id, entity_data))
} else {
None
}
} }
pub fn entity_mut<'d>(&'d mut self, id: u32) -> Option<EntityMut<'d>> { pub fn entity_mut(&mut self, id: u32) -> Option<EntityMut> {
let entity_data = self.entity_storage.get_mut_by_id(id); let entity_data = self.entity_storage.get_mut_by_id(id)?;
if let Some(entity_data) = entity_data {
let entity_ptr = unsafe { entity_data.as_ptr() }; let entity_ptr = unsafe { entity_data.as_ptr() };
Some(EntityMut::new(self, id, entity_ptr)) Some(EntityMut::new(self, id, entity_ptr))
} else {
None
}
} }
pub fn entity_by_uuid(&self, uuid: &Uuid) -> Option<&EntityData> { pub fn entity_by_uuid(&self, uuid: &Uuid) -> Option<&EntityData> {

View file

@ -112,8 +112,8 @@ impl PalettedContainer {
// sanity check // sanity check
debug_assert_eq!(storage.size(), self.container_type.size()); debug_assert_eq!(storage.size(), self.container_type.size());
// let palette = new_palette_type.into_empty_palette(1usize << (bits_per_entry as usize)); // let palette = new_palette_type.as_empty_palette(1usize << (bits_per_entry as usize));
let palette = new_palette_type.into_empty_palette(); let palette = new_palette_type.as_empty_palette();
PalettedContainer { PalettedContainer {
bits_per_entry, bits_per_entry,
palette, palette,
@ -129,8 +129,7 @@ impl PalettedContainer {
let mut new_data = self.create_or_reuse_data(bits_per_entry); let mut new_data = self.create_or_reuse_data(bits_per_entry);
new_data.copy_from(&self.palette, &self.storage); new_data.copy_from(&self.palette, &self.storage);
*self = new_data; *self = new_data;
let id = self.id_for(value); self.id_for(value)
id
} }
fn copy_from(&mut self, palette: &Palette, storage: &BitStorage) { fn copy_from(&mut self, palette: &Palette, storage: &BitStorage) {
@ -268,7 +267,7 @@ impl PaletteType {
}) })
} }
pub fn into_empty_palette(&self) -> Palette { pub fn as_empty_palette(&self) -> Palette {
match self { match self {
PaletteType::SingleValue => Palette::SingleValue(0), PaletteType::SingleValue => Palette::SingleValue(0),
PaletteType::Linear => Palette::Linear(Vec::new()), PaletteType::Linear => Palette::Linear(Vec::new()),
@ -298,7 +297,7 @@ impl PalettedContainerType {
} }
fn size(&self) -> usize { fn size(&self) -> usize {
1 << self.size_bits() * 3 1 << (self.size_bits() * 3)
} }
} }

View file

@ -15,7 +15,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// println!("{}", response.description.to_ansi(None)); // println!("{}", response.description.to_ansi(None));
let account = Account::offline("bot"); let account = Account::offline("bot");
let (mut client, mut rx) = account.join(&address.try_into().unwrap()).await.unwrap(); let (client, mut rx) = account.join(&address.try_into().unwrap()).await.unwrap();
println!("connected"); println!("connected");
while let Some(e) = &rx.recv().await { while let Some(e) = &rx.recv().await {