Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BMSBK - Minor cleanup and add functions #252

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/mercury_engine_data_structures/formats/bmsbk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import functools
from typing import TYPE_CHECKING

from construct import (
import construct
from construct.core import (
Array,
Const,
Construct,
Expand All @@ -13,7 +14,6 @@
Int32ul,
Rebuild,
Struct,
Terminated,
)

from mercury_engine_data_structures.base_resource import BaseResource
Expand Down Expand Up @@ -43,7 +43,7 @@ def _rebuild_types(ctx: Container) -> int:
BlockGroup = Struct(
"_num_blocks" / Rebuild(Int32ul, _rebuild_blocks),
"_num_types" / Rebuild(Int32ul, _rebuild_types),
"unk_bool" / Flag, # always true?
"is_enabled" / Flag, # always true?
"types" / Array(lambda this: this._num_types, Struct(
"block_type" / StrId,
"blocks" / make_vector(Block),
Expand All @@ -52,13 +52,13 @@ def _rebuild_types(ctx: Container) -> int:

BMSBK = Struct(
"magic" / Const(b"MSBK"),
"version" / VersionAdapter(),
"version" / VersionAdapter("1.10.0"),
"block_groups" / make_vector(BlockGroup),
"collision_cameras" / make_vector(Struct(
"name" / StrId,
"entries" / make_vector(Int32ul),
)),
Terminated,
construct.Terminated,
) # fmt: skip


Expand All @@ -67,3 +67,17 @@ class Bmsbk(BaseResource):
@functools.lru_cache
def construct_class(cls, target_game: Game) -> Construct:
return BMSBK

def get_block_group(self, block_group: int) -> Container:
return self.raw.block_groups[block_group]

def set_block_type(self, block_group: int, block_type: str) -> Container:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block_type should certainly be an enum

weakness = self.get_block_group(block_group).types[0]
dyceron marked this conversation as resolved.
Show resolved Hide resolved
duncathan marked this conversation as resolved.
Show resolved Hide resolved
weakness.block_type = block_type

def get_block(self, block_group: int, block_idx: int = 0) -> Container:
return self.get_block_group(block_group).types[0].blocks[block_idx]

def set_respawn_time(self, block_group: int, block_idx: int = 0, respawn_time: float = 0.0) -> Container:
block = self.get_block(block_group)
block.respawn_time = respawn_time
21 changes: 21 additions & 0 deletions tests/formats/test_bmsbk.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@
@pytest.mark.parametrize("bmsbk_path", samus_returns_data.all_files_ending_with(".bmsbk", sr_missing))
def test_bmsbk(samus_returns_tree, bmsbk_path):
parse_build_compare_editor(Bmsbk, samus_returns_tree, bmsbk_path)


@pytest.fixture()
def surface_bmsbk(samus_returns_tree) -> Bmsbk:
return samus_returns_tree.get_parsed_asset("maps/levels/c10_samus/s000_surface/s000_surface.bmsbk", type_hint=Bmsbk)


def test_get_block(surface_bmsbk: Bmsbk):
block = surface_bmsbk.get_block(0, 1)
assert block.respawn_time == 0.0


def test_changing_weakness(surface_bmsbk: Bmsbk):
original_type = surface_bmsbk.get_block_group(1).types[0].block_type
new_type = surface_bmsbk.set_block_type(1, "bomb")
dyceron marked this conversation as resolved.
Show resolved Hide resolved
assert original_type != new_type


def test_respawn_time(surface_bmsbk: Bmsbk):
surface_bmsbk.set_respawn_time(0, 0, 5.0)
assert surface_bmsbk.get_block(0).respawn_time == 5.0
Loading