mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 23:44:38 +00:00
implement BlockState::outline_shape
This commit is contained in:
parent
6ccd44e28d
commit
04036b6e4a
6 changed files with 9549 additions and 3048 deletions
|
@ -1,4 +1,4 @@
|
||||||
use azalea_block::BlockState;
|
use azalea_block::{BlockState, FluidState};
|
||||||
use azalea_core::{
|
use azalea_core::{
|
||||||
block_hit_result::BlockHitResult,
|
block_hit_result::BlockHitResult,
|
||||||
direction::Direction,
|
direction::Direction,
|
||||||
|
@ -9,7 +9,7 @@ use azalea_inventory::ItemStack;
|
||||||
use azalea_world::ChunkStorage;
|
use azalea_world::ChunkStorage;
|
||||||
use bevy_ecs::entity::Entity;
|
use bevy_ecs::entity::Entity;
|
||||||
|
|
||||||
use crate::collision::{BlockWithShape, VoxelShape};
|
use crate::collision::{BlockWithShape, VoxelShape, EMPTY_SHAPE};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ClipContext {
|
pub struct ClipContext {
|
||||||
|
@ -22,22 +22,37 @@ pub struct ClipContext {
|
||||||
impl ClipContext {
|
impl ClipContext {
|
||||||
// minecraft passes in the world and blockpos here... but it doesn't actually
|
// minecraft passes in the world and blockpos here... but it doesn't actually
|
||||||
// seem necessary?
|
// seem necessary?
|
||||||
|
|
||||||
|
/// Get the shape of given block, using the type of shape set in
|
||||||
|
/// [`Self::block_shape_type`].
|
||||||
pub fn block_shape(&self, block_state: BlockState) -> &VoxelShape {
|
pub fn block_shape(&self, block_state: BlockState) -> &VoxelShape {
|
||||||
// TODO: implement the other shape getters
|
|
||||||
// (see the ClipContext.Block class in the vanilla source)
|
|
||||||
match self.block_shape_type {
|
match self.block_shape_type {
|
||||||
BlockShapeType::Collider => block_state.shape(),
|
BlockShapeType::Collider => block_state.collision_shape(),
|
||||||
BlockShapeType::Outline => block_state.shape(),
|
BlockShapeType::Outline => block_state.outline_shape(),
|
||||||
BlockShapeType::Visual => block_state.shape(),
|
BlockShapeType::Visual => block_state.collision_shape(),
|
||||||
BlockShapeType::FallDamageResetting => block_state.shape(),
|
BlockShapeType::FallDamageResetting => {
|
||||||
|
if azalea_registry::tags::blocks::FALL_DAMAGE_RESETTING
|
||||||
|
.contains(&azalea_registry::Block::from(block_state))
|
||||||
|
{
|
||||||
|
block_state.collision_shape()
|
||||||
|
} else {
|
||||||
|
&EMPTY_SHAPE
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub enum BlockShapeType {
|
pub enum BlockShapeType {
|
||||||
|
/// The shape that's used for collision.
|
||||||
Collider,
|
Collider,
|
||||||
|
/// The block outline that renders when your cursor is over a block.
|
||||||
Outline,
|
Outline,
|
||||||
|
/// Used by entities when considering their line of sight.
|
||||||
|
///
|
||||||
|
/// TODO: visual block shape isn't implemented (it'll just return the
|
||||||
|
/// collider shape), that's correct for most blocks though
|
||||||
Visual,
|
Visual,
|
||||||
FallDamageResetting,
|
FallDamageResetting,
|
||||||
}
|
}
|
||||||
|
@ -64,8 +79,11 @@ pub fn clip(chunk_storage: &ChunkStorage, context: ClipContext) -> BlockHitResul
|
||||||
context,
|
context,
|
||||||
|ctx, block_pos| {
|
|ctx, block_pos| {
|
||||||
let block_state = chunk_storage.get_block_state(block_pos).unwrap_or_default();
|
let block_state = chunk_storage.get_block_state(block_pos).unwrap_or_default();
|
||||||
|
let fluid_state = FluidState::from(block_state);
|
||||||
|
|
||||||
// TODO: add fluid stuff to this (see getFluidState in vanilla source)
|
// TODO: add fluid stuff to this (see getFluidState in vanilla source)
|
||||||
let block_shape = ctx.block_shape(block_state);
|
let block_shape = ctx.block_shape(block_state);
|
||||||
|
|
||||||
clip_with_interaction_override(&ctx.from, &ctx.to, block_pos, block_shape, &block_state)
|
clip_with_interaction_override(&ctx.from, &ctx.to, block_pos, block_shape, &block_state)
|
||||||
// let block_distance = if let Some(block_hit_result) =
|
// let block_distance = if let Some(block_hit_result) =
|
||||||
// block_hit_result { context.from.distance_squared_to(&
|
// block_hit_result { context.from.distance_squared_to(&
|
||||||
|
@ -94,10 +112,9 @@ fn clip_with_interaction_override(
|
||||||
let block_hit_result = block_shape.clip(from, to, block_pos);
|
let block_hit_result = block_shape.clip(from, to, block_pos);
|
||||||
if let Some(block_hit_result) = block_hit_result {
|
if let Some(block_hit_result) = block_hit_result {
|
||||||
// TODO: minecraft calls .getInteractionShape here
|
// TODO: minecraft calls .getInteractionShape here
|
||||||
// some blocks (like tall grass) have a physics shape that's different from the
|
// getInteractionShape is empty for almost every shape except cauldons,
|
||||||
// interaction shape, so we need to implement BlockState::interaction_shape. lol
|
// compostors, hoppers, and scaffolding.
|
||||||
// have fun
|
let interaction_shape = &*EMPTY_SHAPE;
|
||||||
let interaction_shape = block_state.shape();
|
|
||||||
let interaction_hit_result = interaction_shape.clip(from, to, block_pos);
|
let interaction_hit_result = interaction_shape.clip(from, to, block_pos);
|
||||||
if let Some(interaction_hit_result) = interaction_hit_result {
|
if let Some(interaction_hit_result) = interaction_hit_result {
|
||||||
if interaction_hit_result.location.distance_squared_to(from)
|
if interaction_hit_result.location.distance_squared_to(from)
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -47,7 +47,7 @@ pub fn get_block_collisions(world: &Instance, aabb: AABB) -> Vec<VoxelShape> {
|
||||||
// suffocating
|
// suffocating
|
||||||
|
|
||||||
// if it's a full block do a faster collision check
|
// if it's a full block do a faster collision check
|
||||||
if block_state.is_shape_full() {
|
if block_state.is_collision_shape_full() {
|
||||||
if !state.aabb.intersects_aabb(&AABB {
|
if !state.aabb.intersects_aabb(&AABB {
|
||||||
min_x: item.pos.x as f64,
|
min_x: item.pos.x as f64,
|
||||||
min_y: item.pos.y as f64,
|
min_y: item.pos.y as f64,
|
||||||
|
@ -186,7 +186,7 @@ impl<'a> BlockCollisionsState<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let shape = block_state.shape();
|
let shape = block_state.collision_shape();
|
||||||
self.cached_block_shapes.push((block_state, shape));
|
self.cached_block_shapes.push((block_state, shape));
|
||||||
|
|
||||||
shape
|
shape
|
||||||
|
|
|
@ -7,7 +7,7 @@ use std::{
|
||||||
sync::{Arc, Weak},
|
sync::{Arc, Weak},
|
||||||
};
|
};
|
||||||
|
|
||||||
use azalea_block::{BlockState, BlockStateIntegerRepr};
|
use azalea_block::{BlockState, BlockStateIntegerRepr, FluidState};
|
||||||
use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError};
|
use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError};
|
||||||
use azalea_core::position::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos};
|
use azalea_core::position::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos};
|
||||||
use nohash_hasher::IntMap;
|
use nohash_hasher::IntMap;
|
||||||
|
@ -302,6 +302,11 @@ impl ChunkStorage {
|
||||||
chunk.get(&ChunkBlockPos::from(pos), self.min_y)
|
chunk.get(&ChunkBlockPos::from(pos), self.min_y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_fluid_state(&self, pos: &BlockPos) -> Option<FluidState> {
|
||||||
|
let block_state = self.get_block_state(pos)?;
|
||||||
|
Some(FluidState::from(block_state))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_block_state(&self, pos: &BlockPos, state: BlockState) -> Option<BlockState> {
|
pub fn set_block_state(&self, pos: &BlockPos, state: BlockState) -> Option<BlockState> {
|
||||||
if pos.y < self.min_y || pos.y >= (self.min_y + self.height as i32) {
|
if pos.y < self.min_y || pos.y >= (self.min_y + self.height as i32) {
|
||||||
return None;
|
return None;
|
||||||
|
|
|
@ -485,7 +485,7 @@ pub fn is_block_state_passable(block: BlockState) -> bool {
|
||||||
// fast path
|
// fast path
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if !block.is_shape_empty() {
|
if !block.is_collision_shape_empty() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
let registry_block = azalea_registry::Block::from(block);
|
let registry_block = azalea_registry::Block::from(block);
|
||||||
|
@ -523,7 +523,7 @@ pub fn is_block_state_solid(block: BlockState) -> bool {
|
||||||
// fast path
|
// fast path
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
block.is_shape_full()
|
block.is_collision_shape_full()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -8,8 +8,7 @@ COLLISION_BLOCKS_RS_DIR = get_dir_location(
|
||||||
def generate_block_shapes(blocks_pixlyzer: dict, shapes: dict, aabbs: dict, block_states_report):
|
def generate_block_shapes(blocks_pixlyzer: dict, shapes: dict, aabbs: dict, block_states_report):
|
||||||
blocks, shapes = simplify_shapes(blocks_pixlyzer, shapes, aabbs)
|
blocks, shapes = simplify_shapes(blocks_pixlyzer, shapes, aabbs)
|
||||||
|
|
||||||
code = generate_block_shapes_code(
|
code = generate_block_shapes_code(blocks, shapes, block_states_report)
|
||||||
blocks, shapes, block_states_report)
|
|
||||||
with open(COLLISION_BLOCKS_RS_DIR, 'w') as f:
|
with open(COLLISION_BLOCKS_RS_DIR, 'w') as f:
|
||||||
f.write(code)
|
f.write(code)
|
||||||
|
|
||||||
|
@ -27,8 +26,8 @@ def simplify_shapes(blocks: dict, shapes: dict, aabbs: dict):
|
||||||
used_shape_ids = set()
|
used_shape_ids = set()
|
||||||
# determine the used shape ids
|
# determine the used shape ids
|
||||||
for _block_id, block_data in blocks.items():
|
for _block_id, block_data in blocks.items():
|
||||||
block_shapes = [state.get('collision_shape')
|
block_shapes = {state.get('collision_shape') for state in block_data['states'].values()}
|
||||||
for state in block_data['states'].values()]
|
block_shapes.update({state.get('outline_shape') for state in block_data['states'].values()})
|
||||||
for s in block_shapes:
|
for s in block_shapes:
|
||||||
used_shape_ids.add(s)
|
used_shape_ids.add(s)
|
||||||
|
|
||||||
|
@ -55,10 +54,14 @@ def simplify_shapes(blocks: dict, shapes: dict, aabbs: dict):
|
||||||
new_blocks = {}
|
new_blocks = {}
|
||||||
for block_id, block_data in blocks.items():
|
for block_id, block_data in blocks.items():
|
||||||
block_id = block_id.split(':')[-1]
|
block_id = block_id.split(':')[-1]
|
||||||
block_shapes = [state.get('collision_shape')
|
|
||||||
for state in block_data['states'].values()]
|
block_collision_shapes = [state.get('collision_shape') for state in block_data['states'].values()]
|
||||||
new_blocks[block_id] = [old_id_to_new_id[shape_id]
|
block_outline_shapes = [state.get('outline_shape') for state in block_data['states'].values()]
|
||||||
for shape_id in block_shapes]
|
|
||||||
|
new_blocks[block_id] = {
|
||||||
|
'collision': [old_id_to_new_id[shape_id] for shape_id in block_collision_shapes],
|
||||||
|
'outline': [old_id_to_new_id[shape_id] for shape_id in block_outline_shapes]
|
||||||
|
}
|
||||||
|
|
||||||
return new_blocks, new_shapes
|
return new_blocks, new_shapes
|
||||||
|
|
||||||
|
@ -71,41 +74,49 @@ def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report):
|
||||||
generated_shape_code += generate_code_for_shape(shape_id, shape)
|
generated_shape_code += generate_code_for_shape(shape_id, shape)
|
||||||
|
|
||||||
|
|
||||||
# static SHAPES_MAP: [&LazyLock<VoxelShape>; 26644] = [&SHAPE0, &SHAPE1, &SHAPE1, ...]
|
# static COLLISION_SHAPES_MAP: [&LazyLock<VoxelShape>; 26644] = [&SHAPE0, &SHAPE1, &SHAPE1, ...]
|
||||||
empty_shapes = []
|
empty_shapes = []
|
||||||
full_shapes = []
|
full_shapes = []
|
||||||
|
|
||||||
# the index into this list is the block state id
|
# the index into this list is the block state id
|
||||||
shapes_map = []
|
collision_shapes_map = []
|
||||||
|
outline_shapes_map = []
|
||||||
|
|
||||||
|
for block_id, shape_datas in blocks.items():
|
||||||
|
collision_shapes = shape_datas['collision']
|
||||||
|
outline_shapes = shape_datas['outline']
|
||||||
|
|
||||||
|
if isinstance(collision_shapes, int): collision_shapes = [collision_shapes]
|
||||||
|
if isinstance(outline_shapes, int): outline_shapes = [outline_shapes]
|
||||||
|
|
||||||
for block_id, shape_ids in blocks.items():
|
|
||||||
if isinstance(shape_ids, int):
|
|
||||||
shape_ids = [shape_ids]
|
|
||||||
block_report_data = block_states_report['minecraft:' + block_id]
|
block_report_data = block_states_report['minecraft:' + block_id]
|
||||||
|
|
||||||
for possible_state, shape_id in zip(block_report_data['states'], shape_ids):
|
for possible_state, shape_id in zip(block_report_data['states'], collision_shapes):
|
||||||
block_state_id = possible_state['id']
|
block_state_id = possible_state['id']
|
||||||
|
if shape_id == 0: empty_shapes.append(block_state_id)
|
||||||
if shape_id == 0 :
|
elif shape_id == 1: full_shapes.append(block_state_id)
|
||||||
empty_shapes.append(block_state_id)
|
while len(collision_shapes_map) <= block_state_id:
|
||||||
elif shape_id == 1 :
|
|
||||||
full_shapes.append(block_state_id)
|
|
||||||
|
|
||||||
while len(shapes_map) <= block_state_id:
|
|
||||||
# default to shape 1 for missing shapes (full block)
|
# default to shape 1 for missing shapes (full block)
|
||||||
shapes_map.append(1)
|
collision_shapes_map.append(1)
|
||||||
shapes_map[block_state_id] = shape_id
|
collision_shapes_map[block_state_id] = shape_id
|
||||||
|
for possible_state, shape_id in zip(block_report_data['states'], outline_shapes):
|
||||||
|
block_state_id = possible_state['id']
|
||||||
|
while len(outline_shapes_map) <= block_state_id:
|
||||||
generated_map_code = f'static SHAPES_MAP: [&LazyLock<VoxelShape>; {len(shapes_map)}] = ['
|
# default to shape 1 for missing shapes (full block)
|
||||||
|
outline_shapes_map.append(1)
|
||||||
|
outline_shapes_map[block_state_id] = shape_id
|
||||||
|
|
||||||
|
generated_map_code = f'static COLLISION_SHAPES_MAP: [&LazyLock<VoxelShape>; {len(collision_shapes_map)}] = ['
|
||||||
empty_shape_match_code = convert_ints_to_rust_ranges(empty_shapes)
|
empty_shape_match_code = convert_ints_to_rust_ranges(empty_shapes)
|
||||||
block_shape_match_code = convert_ints_to_rust_ranges(full_shapes)
|
block_shape_match_code = convert_ints_to_rust_ranges(full_shapes)
|
||||||
|
for block_state_id, shape_id in enumerate(collision_shapes_map):
|
||||||
for block_state_id, shape_id in enumerate(shapes_map):
|
|
||||||
generated_map_code += f'&SHAPE{shape_id},\n'
|
generated_map_code += f'&SHAPE{shape_id},\n'
|
||||||
generated_map_code += '];'
|
generated_map_code += '];\n'
|
||||||
|
|
||||||
|
generated_map_code += f'static OUTLINE_SHAPES_MAP: [&LazyLock<VoxelShape>; {len(outline_shapes_map)}] = ['
|
||||||
|
for block_state_id, shape_id in enumerate(outline_shapes_map):
|
||||||
|
generated_map_code += f'&SHAPE{shape_id},\n'
|
||||||
|
generated_map_code += '];\n'
|
||||||
|
|
||||||
if empty_shape_match_code == '':
|
if empty_shape_match_code == '':
|
||||||
print('Error: shape 0 was not found')
|
print('Error: shape 0 was not found')
|
||||||
|
@ -126,27 +137,31 @@ use crate::collision::{{self, Shapes}};
|
||||||
use azalea_block::*;
|
use azalea_block::*;
|
||||||
|
|
||||||
pub trait BlockWithShape {{
|
pub trait BlockWithShape {{
|
||||||
fn shape(&self) -> &'static VoxelShape;
|
fn collision_shape(&self) -> &'static VoxelShape;
|
||||||
|
fn outline_shape(&self) -> &'static VoxelShape;
|
||||||
/// Tells you whether the block has an empty shape.
|
/// Tells you whether the block has an empty shape.
|
||||||
///
|
///
|
||||||
/// This is slightly more efficient than calling `shape()` and comparing against `EMPTY_SHAPE`.
|
/// This is slightly more efficient than calling `shape()` and comparing against `EMPTY_SHAPE`.
|
||||||
fn is_shape_empty(&self) -> bool;
|
fn is_collision_shape_empty(&self) -> bool;
|
||||||
fn is_shape_full(&self) -> bool;
|
fn is_collision_shape_full(&self) -> bool;
|
||||||
}}
|
}}
|
||||||
|
|
||||||
{generated_shape_code}
|
{generated_shape_code}
|
||||||
|
|
||||||
|
|
||||||
impl BlockWithShape for BlockState {{
|
impl BlockWithShape for BlockState {{
|
||||||
fn shape(&self) -> &'static VoxelShape {{
|
fn collision_shape(&self) -> &'static VoxelShape {{
|
||||||
SHAPES_MAP.get(self.id as usize).unwrap_or(&&SHAPE1)
|
COLLISION_SHAPES_MAP.get(self.id as usize).unwrap_or(&&SHAPE1)
|
||||||
|
}}
|
||||||
|
fn outline_shape(&self) -> &'static VoxelShape {{
|
||||||
|
OUTLINE_SHAPES_MAP.get(self.id as usize).unwrap_or(&&SHAPE1)
|
||||||
}}
|
}}
|
||||||
|
|
||||||
fn is_shape_empty(&self) -> bool {{
|
fn is_collision_shape_empty(&self) -> bool {{
|
||||||
matches!(self.id, {empty_shape_match_code})
|
matches!(self.id, {empty_shape_match_code})
|
||||||
}}
|
}}
|
||||||
|
|
||||||
fn is_shape_full(&self) -> bool {{
|
fn is_collision_shape_full(&self) -> bool {{
|
||||||
matches!(self.id, {block_shape_match_code})
|
matches!(self.id, {block_shape_match_code})
|
||||||
}}
|
}}
|
||||||
}}
|
}}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue