aboutsummaryrefslogtreecommitdiff
path: root/codegen/lib/code/shapes.py
diff options
context:
space:
mode:
Diffstat (limited to 'codegen/lib/code/shapes.py')
-rwxr-xr-xcodegen/lib/code/shapes.py65
1 files changed, 43 insertions, 22 deletions
diff --git a/codegen/lib/code/shapes.py b/codegen/lib/code/shapes.py
index 83521dac..06170552 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, block_states_report, block_datas_burger, mappings: Mappings):
- blocks, shapes = simplify_shapes(blocks, shapes)
+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)
code = generate_block_shapes_code(
blocks, shapes, block_states_report, block_datas_burger, mappings)
@@ -16,33 +16,54 @@ def generate_block_shapes(blocks: dict, shapes: dict, block_states_report, block
f.write(code)
-def simplify_shapes(blocks: dict, shapes: dict):
- shape_to_new_id = {}
+def simplify_shapes(blocks: dict, shapes: dict, aabbs: dict):
new_id_increment = 0
new_shapes = {}
old_id_to_new_id = {}
- for shape_id, shape in sorted(shapes.items(), key=lambda shape: int(shape[0])):
- # tuples are hashable
- shape_as_tuple = tuple(map(tuple, shape))
- if shape_as_tuple not in shape_to_new_id:
- shape_to_new_id[shape_as_tuple] = new_id_increment
- old_id_to_new_id[shape_id] = new_id_increment
- new_shapes[new_id_increment] = shape
- new_id_increment += 1
- else:
- old_id_to_new_id[shape_id] = shape_to_new_id[shape_as_tuple]
+ old_id_to_new_id[None] = 0
+ new_shapes[0] = ()
+ new_id_increment += 1
+
+ used_shape_ids = set()
+ # determine the used shape ids
+ for block_id, block_data in blocks.items():
+ block_id = block_id.split(':')[-1]
+ block_shapes = [state.get('collision_shape')
+ for state in block_data['states'].values()]
+ for s in block_shapes:
+ used_shape_ids.add(s)
+
+ for shape_id, shape in enumerate(shapes):
+ if shape_id not in used_shape_ids: continue
+ # pixlyzer gives us shapes as an index or list of indexes into the
+ # aabbs list
+ # and aabbs look like { "from": number or [x, y, z], "to": (number or vec3) }
+ # convert them to [x1, y1, z1, x2, y2, z2]
+ shape = [shape] if isinstance(shape, int) else shape
+ shape = [aabbs[shape_aabb] for shape_aabb in shape]
+ shape = tuple([(
+ (tuple(part['from']) if isinstance(
+ part['from'], list) else ((part['from'],)*3))
+ + (tuple(part['to']) if isinstance(part['to'], list)
+ else ((part['to'],)*3))
+ ) for part in shape])
+
+ old_id_to_new_id[shape_id] = new_id_increment
+ new_shapes[new_id_increment] = shape
+ new_id_increment += 1
# now map the blocks to the new shape ids
- for block_id, shape_ids in blocks.items():
- if isinstance(shape_ids, int):
- blocks[block_id] = old_id_to_new_id[str(shape_ids)]
- else:
- blocks[block_id] = [old_id_to_new_id[str(shape_id)]
- for shape_id in shape_ids]
+ new_blocks = {}
+ for block_id, block_data in blocks.items():
+ block_id = block_id.split(':')[-1]
+ block_shapes = [state.get('collision_shape')
+ for state in block_data['states'].values()]
+ new_blocks[block_id] = [old_id_to_new_id[shape_id]
+ for shape_id in block_shapes]
- return blocks, new_shapes
+ return new_blocks, new_shapes
def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report, block_datas_burger, mappings: Mappings):
@@ -116,7 +137,7 @@ def generate_code_for_shape(shape_id: str, parts: list[list[float]]):
code = ''
code += f'static SHAPE{shape_id}: Lazy<VoxelShape> = Lazy::new(|| {{'
steps = []
- if parts == []:
+ if parts == ():
steps.append('collision::empty_shape()')
else:
steps.append(f'collision::box_shape({make_arguments(parts[0])})')