diff options
| author | Ubuntu <github@matdoes.dev> | 2023-02-10 01:56:45 +0000 |
|---|---|---|
| committer | Ubuntu <github@matdoes.dev> | 2023-02-10 01:56:45 +0000 |
| commit | 9d4f738d4e66adf0796e163d1c9368aaba906bba (patch) | |
| tree | ae7b3c7fca0ff054eb67c1311955e9321a37e559 /codegen/lib | |
| parent | 48b2a37aa09f0302b40d0678cdde2703f919ed18 (diff) | |
| download | azalea-drasl-9d4f738d4e66adf0796e163d1c9368aaba906bba.tar.xz | |
make blockstate good
Diffstat (limited to 'codegen/lib')
| -rw-r--r-- | codegen/lib/code/entity.py | 2 | ||||
| -rwxr-xr-x | codegen/lib/code/shapes.py | 58 | ||||
| -rwxr-xr-x | codegen/lib/extract.py | 17 |
3 files changed, 50 insertions, 27 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} }} }} }} diff --git a/codegen/lib/extract.py b/codegen/lib/extract.py index 7cbeff30..09035e86 100755 --- a/codegen/lib/extract.py +++ b/codegen/lib/extract.py @@ -4,6 +4,7 @@ from lib.download import get_server_jar, get_burger, get_client_jar, get_pixlyze from lib.utils import get_dir_location from zipfile import ZipFile import subprocess +import requests import json import sys import re @@ -114,7 +115,20 @@ def get_pixlyzer_data(version_id: str, category: str): target_dir = get_dir_location(f'downloads/pixlyzer-{version_id}') - if not os.path.exists(get_dir_location(target_dir)): + # TODO: right now this False is hard-coded, it should retry with this + # enabled if # initially getting the data fails + if False or (os.path.exists(target_dir) and not os.path.exists(f'{target_dir}/{category}.min.json')): + print('Downloading', category, 'from pixlyzer-data.') + data = requests.get(f'https://gitlab.com/Bixilon/pixlyzer-data/-/raw/master/version/{version_id}/{category}.min.json?inline=false').text + try: + os.mkdir(target_dir) + except: + pass + with open(f'{target_dir}/{category}.min.json', 'w') as f: + f.write(data) + return json.loads(data) + + if not os.path.exists(target_dir): pixlyzer_dir = get_pixlyzer() # for some reason pixlyzer doesn't work right unless the mvn clean @@ -231,7 +245,6 @@ def get_pixlyzer_data(version_id: str, category: str): with open(f'{target_dir}/{category}.min.json', 'r') as f: return json.load(f) - def get_file_from_jar(version_id: str, file_dir: str): get_client_jar(version_id) with ZipFile(get_dir_location(f'downloads/client-{version_id}.jar')) as z: |
