Skip to content

Commit

Permalink
Add Pickups on Metroids
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanatosGit committed Sep 30, 2023
1 parent e987ece commit 58f18ca
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 879 deletions.
75 changes: 59 additions & 16 deletions open_samus_returns_rando/files/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,15 @@
"type": "object",
"properties": {
"pickup_type": {
"type": "string"
"type": "string",
"enum": [
"actor",
"metroid"
]
},
"caption": {
"type": "string"
},
"pickup_actor": {
"$ref": "#/$defs/actor_reference_with_layer"
},
"model": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"description": "A list of model identifiers used by this pickup. Non-progressive items will only have one model identifier."
},
"resources": {
"type": "array",
"items": {
Expand Down Expand Up @@ -78,10 +71,45 @@
"required": [
"pickup_type",
"caption",
"resources",
"pickup_actor",
"model"
]
"resources"
],
"if": {
"properties": {
"pickup_type": {
"const": "actor"
}
}
},
"then": {
"properties": {
"pickup_actor": {
"$ref": "#/$defs/actor_reference_with_layer"
},
"model": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"description": "A list of model identifiers used by this pickup. Non-progressive items will only have one model identifier."
}
},
"required": [
"pickup_actor",
"model"
]
},
"else": {
"properties": {
"metroid_callback": {
"$ref": "#/$defs/metroid_callback"
}
},
"required": [
"metroid_callback"
]
}

}
},
"elevators": {
Expand Down Expand Up @@ -336,6 +364,21 @@
"y",
"z"
]
},
"metroid_callback": {
"type": "object",
"properties": {
"scenario": {
"$ref": "#/$defs/scenario_name"
},
"spawngroup": {
"type": "string"
}
},
"required": [
"scenario",
"spawngroup"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ function Metroid.RemoveMetroid(_ARG_0_)

CurrentScenario.currentMetroidSpawngroup = nil

Metroid.Pickups[Scenario.CurrentScenarioID][spawnGroupName].OnPickedUp()

-- TODO: This is only for tests
-- Game.SaveGame("checkpoint", "Bla", "ST_SG_Alpha_004_Out", true)
else
GUI.LaunchMessage("Oops 2", "Metroid.Dummy", "")
end
end

Metroid.Pickups = TEMPLATE("mapping")
12 changes: 12 additions & 0 deletions open_samus_returns_rando/lua_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def _read_level_lua(level_id: str) -> str:
class LuaEditor:
def __init__(self):
self._item_classes = {}
self._metroid_dict = {}
self._powerup_script = _read_powerup_lua()
self._custom_level_scripts: dict[str, dict] = self._read_levels()

Expand Down Expand Up @@ -124,8 +125,19 @@ def add_progressive_models(self, pickup: dict, actordef_name: str):
self._powerup_script += models.encode("utf-8")

def save_modifications(self, editor: PatcherEditor):
final_metroid_script = lua_util.replace_lua_template("metroid_template.lua", {"mapping": self._metroid_dict})

editor.replace_asset("actors/items/randomizer_powerup/scripts/randomizer_powerup.lc", self._powerup_script)
editor.replace_asset("actors/scripts/metroid.lc", final_metroid_script.encode("utf-8"))
editor.replace_asset("actors/props/heatzone/scripts/heatzone.lc",
files_path().joinpath("heatzone.lua").read_bytes())
for scenario, script in self._custom_level_scripts.items():
editor.replace_asset(path_for_level(scenario) + ".lc", script["script"].encode("utf-8"))

def add_metroid_pickup(self, metroid_callback: dict, lua_class: str) -> None:
scenario = metroid_callback["scenario"]
spawngroup = metroid_callback["spawngroup"]
if scenario not in self._metroid_dict:
self._metroid_dict[scenario] = {}
scenario_list = self._metroid_dict[scenario]
scenario_list[spawngroup] = lua_class
13 changes: 11 additions & 2 deletions open_samus_returns_rando/pickup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def _read_template_powerup():

class PickupType(Enum):
ACTOR = "actor"
METROID = "metroid"

class BasePickup:
def __init__(self, lua_editor: LuaEditor, pickup: dict, pickup_id: int, configuration: dict):
Expand Down Expand Up @@ -101,6 +102,7 @@ def patch_multi_item_pickup(self, bmsad: dict) -> dict:
)
return bmsad


def patch_model(self, model_names: list[str], new_template: dict) -> None:
if len(model_names) == 1:
model_data = get_data(model_names[0])
Expand Down Expand Up @@ -187,12 +189,19 @@ def patch(self, editor: PatcherEditor):
# json.dumps(new_template, indent=4)
# )

class MetroidPickup(BasePickup):
def patch(self, editor: PatcherEditor):
lua_class = self.lua_editor.get_script_class(self.pickup)
self.lua_editor.add_metroid_pickup(self.pickup["metroid_callback"], lua_class)


def ensure_base_models(editor: PatcherEditor) -> None:
for level_pkg in editor.get_all_level_pkgs():
# ensure base stuff
editor.ensure_present(level_pkg, "system/animtrees/base.bmsat")
editor.ensure_present(level_pkg, "sounds/generic/obtencion.bcwav")
editor.ensure_present(level_pkg, "actors/items/randomizer_powerup/scripts/randomizer_powerup.lc")
editor.ensure_present(level_pkg, "actors/scripts/metroid.lc")

# ensure itemsphere stuff (base for many majors)
editor.ensure_present(level_pkg, "actors/items/itemsphere/animations/relax.bcskla")
Expand All @@ -207,9 +216,9 @@ def ensure_base_models(editor: PatcherEditor) -> None:
editor.ensure_present(level_pkg, dep)



def patch_pickups(editor: PatcherEditor, lua_scripts: LuaEditor, pickups_config: list[dict], configuration: dict):
editor.add_new_asset("actors/items/randomizer_powerup/scripts/randomizer_powerup.lc", b'', [])
editor.add_new_asset("actors/scripts/metroid.lc", b'', [])
ensure_base_models(editor)

for i, pickup in enumerate(pickups_config):
Expand All @@ -219,9 +228,9 @@ def patch_pickups(editor: PatcherEditor, lua_scripts: LuaEditor, pickups_config:
except NotImplementedError as e:
LOG.warning(e)


_PICKUP_TYPE_TO_CLASS: dict[PickupType, type[BasePickup]] = {
PickupType.ACTOR: ActorPickup,
PickupType.METROID: MetroidPickup,
}

def pickup_object_for(lua_scripts: LuaEditor, pickup: dict, pickup_id: int, configuration: dict) -> "BasePickup":
Expand Down
2 changes: 0 additions & 2 deletions open_samus_returns_rando/specific_patches/metroid_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from mercury_engine_data_structures.formats import Bmsad

from open_samus_returns_rando.constants import ALL_AREAS
from open_samus_returns_rando.files import files_path
from open_samus_returns_rando.patcher_editor import PatcherEditor

LOG = logging.getLogger("metroid_patches")
Expand Down Expand Up @@ -59,7 +58,6 @@ def patch_metroids(editor: PatcherEditor):
},
]

editor.add_new_asset("actors/scripts/metroid.lc", files_path().joinpath("custom_metroid.lua").read_bytes(), [])
for area_name in ALL_AREAS:
editor.ensure_present_in_scenario(area_name, "actors/scripts/metroid.lc")

Expand Down
Loading

0 comments on commit 58f18ca

Please sign in to comment.