diff options
Diffstat (limited to 'codegen/lib/code')
| -rw-r--r-- | codegen/lib/code/entity.py | 2 | ||||
| -rwxr-xr-x | codegen/lib/code/shapes.py | 58 |
2 files changed, 35 insertions, 25 deletions
diff --git a/codegen/lib/code/entity.py b/codegen/lib/code/entity.py index 844793f6..750b7ca3 100644 --- a/codegen/lib/code/entity.py +++ b/codegen/lib/code/entity.py @@ -367,7 +367,7 @@ impl From<EntityDataValue> for UpdateMetadataError { elif type_name == 'ItemStack': default = f'Slot::Present({default})' if default != 'Empty' else 'Slot::Empty' elif type_name == 'BlockState': - default = f'{default}' if default != 'Empty' else 'BlockState::Air' + default = f'{default}' if default != 'Empty' else 'BlockState::AIR' elif type_name == 'OptionalFormattedText': default = f'Some({default})' if default != 'Empty' else 'None' elif type_name == 'CompoundTag': diff --git a/codegen/lib/code/shapes.py b/codegen/lib/code/shapes.py index 06170552..7682fe53 100755 --- a/codegen/lib/code/shapes.py +++ b/codegen/lib/code/shapes.py @@ -7,8 +7,8 @@ COLLISION_BLOCKS_RS_DIR = get_dir_location( '../azalea-physics/src/collision/blocks.rs') -def generate_block_shapes(blocks: dict, shapes: dict, aabbs: dict, block_states_report, block_datas_burger, mappings: Mappings): - blocks, shapes = simplify_shapes(blocks, shapes, aabbs) +def generate_block_shapes(blocks_pixlyzer: dict, shapes: dict, aabbs: dict, block_states_report, block_datas_burger, mappings: Mappings): + blocks, shapes = simplify_shapes(blocks_pixlyzer, shapes, aabbs) code = generate_block_shapes_code( blocks, shapes, block_states_report, block_datas_burger, mappings) @@ -28,8 +28,7 @@ def simplify_shapes(blocks: dict, shapes: dict, aabbs: dict): used_shape_ids = set() # determine the used shape ids - for block_id, block_data in blocks.items(): - block_id = block_id.split(':')[-1] + for _block_id, block_data in blocks.items(): block_shapes = [state.get('collision_shape') for state in block_data['states'].values()] for s in block_shapes: @@ -73,9 +72,9 @@ def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report, for (shape_id, shape) in sorted(shapes.items(), key=lambda shape: int(shape[0])): generated_shape_code += generate_code_for_shape(shape_id, shape) - # BlockState::PurpurStairs_NorthTopStraightTrue => &SHAPE24, + # 1..100 | 200..300 => &SHAPE1, generated_match_inner_code = '' - shape_ids_to_variants = {} + shape_ids_to_block_state_ids = {} for block_id, shape_ids in blocks.items(): if isinstance(shape_ids, int): shape_ids = [shape_ids] @@ -83,23 +82,34 @@ def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report, block_data_burger = block_datas_burger[block_id] for possible_state, shape_id in zip(block_report_data['states'], shape_ids): - variant_values = [] - for value in tuple(possible_state.get('properties', {}).values()): - variant_values.append(to_camel_case(value)) - - if variant_values == []: - variant_name = to_camel_case(block_id) - else: - variant_name = f'{to_camel_case(block_id)}_{"".join(variant_values)}' - - if shape_id not in shape_ids_to_variants: - shape_ids_to_variants[shape_id] = [] - shape_ids_to_variants[shape_id].append( - f'BlockState::{variant_name}') + block_state_id = possible_state['id'] + + if shape_id not in shape_ids_to_block_state_ids: + shape_ids_to_block_state_ids[shape_id] = [] + shape_ids_to_block_state_ids[shape_id].append(block_state_id) # shape 1 is the most common so we have a _ => &SHAPE1 at the end - del shape_ids_to_variants[1] - for shape_id, variants in shape_ids_to_variants.items(): - generated_match_inner_code += f'{"|".join(variants)} => &SHAPE{shape_id},\n' + del shape_ids_to_block_state_ids[1] + for shape_id, block_state_ids in shape_ids_to_block_state_ids.items(): + + # convert them into ranges (so like 1|2|3 is 1..=3 instead) + block_state_ids_ranges = [] + range_start_block_state_id = None + last_block_state_id = None + for block_state_id in sorted(block_state_ids): + if range_start_block_state_id is None: + range_start_block_state_id = block_state_id + + if last_block_state_id is not None: + # check if the range is done + if block_state_id - 1 != last_block_state_id: + block_state_ids_ranges.append(f'{range_start_block_state_id}..={last_block_state_id}' if range_start_block_state_id != last_block_state_id else str(range_start_block_state_id)) + range_start_block_state_id = block_state_id + + last_block_state_id = block_state_id + + block_state_ids_ranges.append(f'{range_start_block_state_id}..={last_block_state_id}' if range_start_block_state_id != last_block_state_id else str(range_start_block_state_id)) + generated_match_inner_code += f'{"|".join(block_state_ids_ranges)} => &SHAPE{shape_id},\n' + generated_match_inner_code += '_ => &SHAPE1' return f''' //! Autogenerated block collisions for every block @@ -123,8 +133,8 @@ pub trait BlockWithShape {{ impl BlockWithShape for BlockState {{ fn shape(&self) -> &'static VoxelShape {{ - match self {{ - {generated_match_inner_code}_ => &SHAPE1 + match self.id {{ + {generated_match_inner_code} }} }} }} |
