Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add World::find_block #80

Merged
merged 10 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
BlockStates
  • Loading branch information
mat-1 committed Mar 8, 2023
commit 683de171211f403a120b5600cff10762d8151c3a
10 changes: 5 additions & 5 deletions azalea-block/azalea-block-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
let mut from_state_to_block_match = quote! {};
let mut from_registry_block_to_block_match = quote! {};
let mut from_registry_block_to_blockstate_match = quote! {};
let mut from_registry_block_to_blockstate_range_match = quote! {};
let mut from_registry_block_to_blockstates_match = quote! {};

for block in &input.block_definitions.blocks {
let block_property_names = &block
Expand Down Expand Up @@ -524,8 +524,8 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
from_registry_block_to_blockstate_match.extend(quote! {
azalea_registry::Block::#block_name_pascal_case => BlockState { id: #default_state_id },
});
from_registry_block_to_blockstate_range_match.extend(quote! {
azalea_registry::Block::#block_name_pascal_case => BlockStateRange { id: #first_state_id..=#last_state_id },
from_registry_block_to_blockstates_match.extend(quote! {
azalea_registry::Block::#block_name_pascal_case => BlockStates::from(#first_state_id..=#last_state_id),
});

let mut block_default_fields = quote! {};
Expand Down Expand Up @@ -629,10 +629,10 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
}
}
}
impl From<azalea_registry::Block> for BlockStateRange {
impl From<azalea_registry::Block> for BlockStates {
fn from(block: azalea_registry::Block) -> Self {
match block {
#from_registry_block_to_blockstate_range_match
#from_registry_block_to_blockstates_match
_ => unreachable!("There should always be a block state for every azalea_registry::Block variant")
}
}
Expand Down
2 changes: 1 addition & 1 deletion azalea-block/src/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Block, BlockBehavior, BlockState, BlockStateRange};
use crate::{Block, BlockBehavior, BlockState, BlockStates};
use azalea_block_macros::make_block_states;
use std::fmt::Debug;

Expand Down
2 changes: 1 addition & 1 deletion azalea-block/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod range;
use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable};
pub use behavior::BlockBehavior;
use core::fmt::Debug;
pub use range::BlockStateRange;
pub use range::BlockStates;
use std::{
any::Any,
io::{Cursor, Write},
Expand Down
33 changes: 30 additions & 3 deletions azalea-block/src/range.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
use std::ops::RangeInclusive;
use std::{collections::HashSet, ops::RangeInclusive};

use crate::BlockState;

#[derive(Debug, Clone)]
pub struct BlockStateRange {
pub id: RangeInclusive<u32>,
pub struct BlockStates {
pub set: HashSet<BlockState>,
}

impl From<RangeInclusive<u32>> for BlockStates {
fn from(range: RangeInclusive<u32>) -> Self {
let mut set = HashSet::with_capacity((range.end() - range.start() + 1) as usize);
for id in range {
set.insert(BlockState { id });
}
Self { set }
}
}

impl IntoIterator for BlockStates {
type Item = BlockState;
type IntoIter = std::collections::hash_set::IntoIter<BlockState>;

fn into_iter(self) -> Self::IntoIter {
self.set.into_iter()
}
}

impl BlockStates {
pub fn contains(&self, state: &BlockState) -> bool {
self.set.contains(state)
}
}
5 changes: 2 additions & 3 deletions azalea-world/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
palette::Palette,
ChunkStorage, PartialChunkStorage, WorldContainer,
};
use azalea_block::BlockState;
use azalea_block::{BlockState, BlockStates};
use azalea_core::{BlockPos, ChunkPos};
use azalea_ecs::{
entity::Entity,
Expand Down Expand Up @@ -198,12 +198,11 @@ impl World {
pub fn find_block(
&self,
nearest_to: impl Into<BlockPos>,
block_states: impl Into<HashSet<BlockState>>,
block_states: &BlockStates,
) -> Option<BlockPos> {
// iterate over every chunk in a 3d spiral pattern
// and then check the palette for the block state

let block_states = block_states.into();
let nearest_to: BlockPos = nearest_to.into();
let start_chunk: ChunkPos = (&nearest_to).into();
// todo (correctness): rename this to something like SquareChunkIterator and
Expand Down
2 changes: 1 addition & 1 deletion azalea/examples/testbot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
"findblock" => {
let target_pos = bot.world().read().find_block(
bot.component::<Position>(),
azalea_registry::Block::DiamondBlock.into(),
&azalea_registry::Block::DiamondBlock.into(),
);
bot.chat(&format!("target_pos: {target_pos:?}",));
}
Expand Down