mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
Merge branch 'main' into pathfinder-mining
This commit is contained in:
commit
d4b618b4de
4 changed files with 123 additions and 40 deletions
|
@ -214,7 +214,7 @@ impl BlockPos {
|
|||
|
||||
/// Chunk coordinates are used to represent where a chunk is in the world. You
|
||||
/// can convert the x and z to block coordinates by multiplying them by 16.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, McBuf)]
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct ChunkPos {
|
||||
pub x: i32,
|
||||
pub z: i32,
|
||||
|
@ -234,12 +234,38 @@ impl Add<ChunkPos> for ChunkPos {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// reading ChunkPos is done in reverse, so z first and then x
|
||||
// ........
|
||||
// mojang why
|
||||
impl From<ChunkPos> for u64 {
|
||||
#[inline]
|
||||
fn from(pos: ChunkPos) -> Self {
|
||||
((pos.x as u64) << 32) | (pos.z as u64)
|
||||
(pos.x as u64) | ((pos.z as u64) << 32)
|
||||
}
|
||||
}
|
||||
impl From<u64> for ChunkPos {
|
||||
#[inline]
|
||||
fn from(pos: u64) -> Self {
|
||||
ChunkPos {
|
||||
x: (pos) as i32,
|
||||
z: (pos >> 32) as i32,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl McBufReadable for ChunkPos {
|
||||
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
|
||||
let long = u64::read_from(buf)?;
|
||||
Ok(ChunkPos::from(long))
|
||||
}
|
||||
}
|
||||
impl McBufWritable for ChunkPos {
|
||||
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
|
||||
u64::from(*self).write_into(buf)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for ChunkPos {
|
||||
#[inline]
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
|
@ -608,4 +634,13 @@ mod tests {
|
|||
ChunkSectionBlockPos::new(0, 4, 0)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_chunk_pos_from() {
|
||||
let mut buf = Vec::new();
|
||||
ChunkPos::new(2, -1).write_into(&mut buf).unwrap();
|
||||
let mut buf = Cursor::new(&buf[..]);
|
||||
let chunk_pos = ChunkPos::from(u64::read_from(&mut buf).unwrap());
|
||||
assert_eq!(chunk_pos, ChunkPos::new(2, -1));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -123,18 +123,30 @@ impl PartialChunkStorage {
|
|||
self.view_center
|
||||
}
|
||||
|
||||
pub fn view_range(&self) -> u32 {
|
||||
self.view_range
|
||||
}
|
||||
|
||||
pub fn index_from_chunk_pos(&self, chunk_pos: &ChunkPos) -> usize {
|
||||
(i32::rem_euclid(chunk_pos.x, self.view_range as i32) * (self.view_range as i32)
|
||||
+ i32::rem_euclid(chunk_pos.z, self.view_range as i32)) as usize
|
||||
let view_range = self.view_range as i32;
|
||||
|
||||
let x = i32::rem_euclid(chunk_pos.x, view_range) * view_range;
|
||||
let z = i32::rem_euclid(chunk_pos.z, view_range);
|
||||
(x + z) as usize
|
||||
}
|
||||
|
||||
pub fn chunk_pos_from_index(&self, index: usize) -> ChunkPos {
|
||||
let x = index as i32 % self.view_range as i32;
|
||||
let z = index as i32 / self.view_range as i32;
|
||||
ChunkPos::new(
|
||||
x + self.view_center.x - self.chunk_radius as i32,
|
||||
z + self.view_center.z - self.chunk_radius as i32,
|
||||
)
|
||||
let view_range = self.view_range as i32;
|
||||
|
||||
// find the base from the view center
|
||||
let base_x = self.view_center.x.div_euclid(view_range) * view_range;
|
||||
let base_z = self.view_center.z.div_euclid(view_range) * view_range;
|
||||
|
||||
// add the offset from the base
|
||||
let offset_x = index as i32 / view_range;
|
||||
let offset_z = index as i32 % view_range;
|
||||
|
||||
ChunkPos::new(base_x + offset_x, base_z + offset_z)
|
||||
}
|
||||
|
||||
pub fn in_range(&self, chunk_pos: &ChunkPos) -> bool {
|
||||
|
@ -207,6 +219,7 @@ impl PartialChunkStorage {
|
|||
}
|
||||
|
||||
let index = self.index_from_chunk_pos(pos);
|
||||
|
||||
Some(&mut self.chunks[index])
|
||||
}
|
||||
|
||||
|
@ -552,4 +565,16 @@ mod tests {
|
|||
.get_block_state(&BlockPos { x: 0, y: -65, z: 0 })
|
||||
.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_chunk_pos_from_index() {
|
||||
let mut partial_chunk_storage = PartialChunkStorage::new(5);
|
||||
partial_chunk_storage.update_view_center(ChunkPos::new(0, -1));
|
||||
assert_eq!(
|
||||
partial_chunk_storage.chunk_pos_from_index(
|
||||
partial_chunk_storage.index_from_chunk_pos(&ChunkPos::new(2, -1))
|
||||
),
|
||||
ChunkPos::new(2, -1),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -308,43 +308,63 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
|
|||
bot.chat(&format!("block: {block:?}"));
|
||||
}
|
||||
"debugchunks" => {
|
||||
println!("shared:");
|
||||
{
|
||||
println!("shared:");
|
||||
|
||||
let partial_instance_lock = bot.component::<InstanceHolder>().partial_instance;
|
||||
let local_chunk_storage = &partial_instance_lock.read().chunks;
|
||||
let mut ecs = bot.ecs.lock();
|
||||
|
||||
let mut total_loaded_chunks_count = 0;
|
||||
for (chunk_pos, chunk) in &bot.world().read().chunks.map {
|
||||
if let Some(chunk) = chunk.upgrade() {
|
||||
let in_range = local_chunk_storage.in_range(chunk_pos);
|
||||
println!(
|
||||
"{chunk_pos:?} has {} references{}",
|
||||
std::sync::Arc::strong_count(&chunk) - 1,
|
||||
if in_range { "" } else { " (out of range)" }
|
||||
);
|
||||
total_loaded_chunks_count += 1;
|
||||
let instance_holder = bot.query::<&InstanceHolder>(&mut ecs).clone();
|
||||
drop(ecs);
|
||||
let local_chunk_storage = &instance_holder.partial_instance.read().chunks;
|
||||
let shared_chunk_storage = instance_holder.instance.read();
|
||||
|
||||
let mut total_loaded_chunks_count = 0;
|
||||
for (chunk_pos, chunk) in &shared_chunk_storage.chunks.map {
|
||||
if let Some(chunk) = chunk.upgrade() {
|
||||
let in_range = local_chunk_storage.in_range(chunk_pos);
|
||||
println!(
|
||||
"{chunk_pos:?} has {} references{}",
|
||||
std::sync::Arc::strong_count(&chunk) - 1,
|
||||
if in_range { "" } else { " (out of range)" }
|
||||
);
|
||||
total_loaded_chunks_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("local:");
|
||||
println!("local:");
|
||||
println!("view range: {}", local_chunk_storage.view_range());
|
||||
println!("view center: {:?}", local_chunk_storage.view_center());
|
||||
|
||||
let mut local_loaded_chunks_count = 0;
|
||||
for (i, chunk) in local_chunk_storage.chunks().enumerate() {
|
||||
if let Some(chunk) = chunk {
|
||||
let chunk_pos = local_chunk_storage.chunk_pos_from_index(i);
|
||||
println!(
|
||||
"{chunk_pos:?} has {} references",
|
||||
std::sync::Arc::strong_count(&chunk)
|
||||
);
|
||||
local_loaded_chunks_count += 1;
|
||||
let mut local_loaded_chunks_count = 0;
|
||||
for (i, chunk) in local_chunk_storage.chunks().enumerate() {
|
||||
if let Some(chunk) = chunk {
|
||||
let chunk_pos = local_chunk_storage.chunk_pos_from_index(i);
|
||||
println!(
|
||||
"{chunk_pos:?} (#{i}) has {} references",
|
||||
std::sync::Arc::strong_count(&chunk)
|
||||
);
|
||||
local_loaded_chunks_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("total loaded chunks: {total_loaded_chunks_count}");
|
||||
println!(
|
||||
"local loaded chunks: {local_loaded_chunks_count}/{}",
|
||||
local_chunk_storage.chunks().collect::<Vec<_>>().len()
|
||||
);
|
||||
println!("total loaded chunks: {total_loaded_chunks_count}");
|
||||
println!(
|
||||
"local loaded chunks: {local_loaded_chunks_count}/{}",
|
||||
local_chunk_storage.chunks().collect::<Vec<_>>().len()
|
||||
);
|
||||
}
|
||||
{
|
||||
let local_chunk_storage_lock = bot.partial_world();
|
||||
let local_chunk_storage = local_chunk_storage_lock.read();
|
||||
let current_chunk_loaded = local_chunk_storage
|
||||
.chunks
|
||||
.limited_get(&ChunkPos::from(bot.position()));
|
||||
|
||||
bot.chat(&format!(
|
||||
"current chunk loaded: {}",
|
||||
current_chunk_loaded.is_some()
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
@ -229,6 +229,9 @@ where
|
|||
/// Use [`Self::add_account`] to only add one account. If you want the
|
||||
/// clients to have different default states, add them one at a time with
|
||||
/// [`Self::add_account_with_state`].
|
||||
///
|
||||
/// By default every account will join at the same time, you can add a delay
|
||||
/// with [`Self::join_delay`].
|
||||
#[must_use]
|
||||
pub fn add_accounts(mut self, accounts: Vec<Account>) -> Self
|
||||
where
|
||||
|
|
Loading…
Add table
Reference in a new issue