Skip to content

Commit

Permalink
Add actors sorted by CRC in Bmsld entity groups
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanatosGit committed Oct 16, 2023
1 parent 10e24a2 commit 8cf1ed1
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/mercury_engine_data_structures/formats/bmsld.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
from bisect import bisect
from typing import Iterator, Tuple

import construct
from construct import Const, Construct, Container, Flag, Float32l, Hex, Int32ul, Struct, Switch

from mercury_engine_data_structures.common_types import CVector3D, Float, StrId, make_dict, make_vector
from mercury_engine_data_structures.construct_extensions.misc import ErrorWithMessage
from mercury_engine_data_structures.crc import crc32
from mercury_engine_data_structures.formats import BaseResource
from mercury_engine_data_structures.formats.collision import collision_formats
from mercury_engine_data_structures.game_check import Game
Expand Down Expand Up @@ -187,6 +189,16 @@ def remove_actor_from_all_groups(self, actor_name: str):
self.remove_actor_from_group(group_name, actor_name)

def add_actor_to_entity_groups(self, collision_camera_name: str, actor_name: str, all_groups: bool = False):
"""
Adds an actor to either all entity groups or one entity group, which follow the name pattern of eg_SubArea_NAME.
In case an actor needs to be added to an entity group not following this name pattern
use `insert_into_sub_area`.
collision_camera_name: Name of the collision camera to find the entity groups for
actor_name: Actor name to add to the entity group
all_groups: A boolean which defines if the actor should be added to all entity groups starting with the name
pattern or just to one entity group matching the name pattern exactly
"""
def compare_func(first: str, second: str) -> bool:
if all_groups:
return first.startswith(f"eg_SubArea_{second}")
Expand All @@ -199,4 +211,14 @@ def compare_func(first: str, second: str) -> bool:
raise Exception(f"No entity group found for {collision_camera_name}")
for group in collision_camera_groups:
logger.debug("Add actor %s to group %s", actor_name, group.name)
group.names.append(actor_name)
self.insert_into_entity_group(group, actor_name)

def insert_into_entity_group(self, sub_area: Container, name_to_add: str) -> None:
# MSR requires to have the names in the sub area list sorted by their crc32 value
crcs_list = [
crc32(name)
for name in sub_area.names
]
crc_to_add = crc32(name_to_add)
index = bisect(crcs_list, crc_to_add)
sub_area.names.insert(index, name_to_add)

0 comments on commit 8cf1ed1

Please sign in to comment.