From 170213e6d4f8b308fe466193cd0d770b39214054 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Sun, 20 Feb 2022 19:10:08 +0100 Subject: [PATCH] Core: reduce memory use of "Entrance" class SM64: reduce count of lambda creations (memory/cpu speedup) --- BaseClasses.py | 16 ++++++++++------ worlds/sm64ex/Regions.py | 32 +++++++++++++++++++------------- worlds/sm64ex/Rules.py | 26 +++++++++++++------------- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/BaseClasses.py b/BaseClasses.py index a10a77b7..de2d8e28 100644 --- a/BaseClasses.py +++ b/BaseClasses.py @@ -903,18 +903,22 @@ class Region(object): return self.world.get_name_string_for_object(self) if self.world else f'{self.name} (Player {self.player})' -class Entrance(object): +class Entrance: spot_type = 'Entrance' + access_rule: Callable[[CollectionState], bool] = staticmethod(lambda state: True) + hide_path: bool = False + player: int + name: str + parent_region: Optional[Region] + connected_region: Optional[Region] = None + # LttP specific, TODO: should make a LttPEntrance + addresses = None + target = None def __init__(self, player: int, name: str = '', parent=None): self.name = name self.parent_region = parent - self.connected_region = None - self.target = None - self.addresses = None - self.access_rule = lambda state: True self.player = player - self.hide_path = False def can_reach(self, state: CollectionState) -> bool: if self.parent_region.can_reach(state) and self.access_rule(state): diff --git a/worlds/sm64ex/Regions.py b/worlds/sm64ex/Regions.py index d4d25031..85c08933 100644 --- a/worlds/sm64ex/Regions.py +++ b/worlds/sm64ex/Regions.py @@ -1,16 +1,18 @@ import typing from BaseClasses import MultiWorld, Region, Entrance, Location, RegionType -from .Locations import SM64Location, location_table,locBoB_table,locWhomp_table,locJRB_table,locCCM_table,locBBH_table, \ - locHMC_table,locLLL_table,locSSL_table,locDDD_table,locSL_table, \ - locWDW_table,locTTM_table,locTHI_table,locTTC_table,locRR_table, \ - locBitDW_table, locBitFS_table, locSS_table, locCap_table +from .Locations import SM64Location, location_table, locBoB_table, locWhomp_table, locJRB_table, locCCM_table, \ + locBBH_table, \ + locHMC_table, locLLL_table, locSSL_table, locDDD_table, locSL_table, \ + locWDW_table, locTTM_table, locTHI_table, locTTC_table, locRR_table, \ + locBitDW_table, locBitFS_table, locSS_table, locCap_table sm64courses = ["Bob-omb Battlefield", "Whomp's Fortress", "Jolly Roger Bay", "Cool, Cool Mountain", "Big Boo's Haunt", - "Hazy Maze Cave", "Lethal Lava Land", "Shifting Sand Land", "Dire, Dire Docks", "Snowman's Land", "Wet-Dry World", + "Hazy Maze Cave", "Lethal Lava Land", "Shifting Sand Land", "Dire, Dire Docks", "Snowman's Land", + "Wet-Dry World", "Tall, Tall Mountain", "Tiny-Huge Island", "Tick Tock Clock", "Rainbow Ride"] -def create_regions(world: MultiWorld, player: int): +def create_regions(world: MultiWorld, player: int): regSS = Region("Menu", RegionType.Generic, "Castle Area", player, world) locSS_names = [name for name, id in locSS_table.items()] locSS_names += [name for name, id in locCap_table.items()] @@ -26,7 +28,8 @@ def create_regions(world: MultiWorld, player: int): regWhomp = Region("Whomp's Fortress", RegionType.Generic, "Whomp's Fortress", player, world) locWhomp_names = [name for name, id in locWhomp_table.items()] - regWhomp.locations += [SM64Location(player, loc_name, location_table[loc_name], regWhomp) for loc_name in locWhomp_names] + regWhomp.locations += [SM64Location(player, loc_name, location_table[loc_name], regWhomp) for loc_name in + locWhomp_names] if (world.EnableCoinStars[player].value): regWhomp.locations.append(SM64Location(player, "WF: 100 Coins", location_table["WF: 100 Coins"], regWhomp)) world.regions.append(regWhomp) @@ -54,7 +57,8 @@ def create_regions(world: MultiWorld, player: int): regBitDW = Region("Bowser in the Dark World", RegionType.Generic, "Bowser in the Dark World", player, world) locBitDW_names = [name for name, id in locBitDW_table.items()] - regBitDW.locations += [SM64Location(player, loc_name, location_table[loc_name], regBitDW) for loc_name in locBitDW_names] + regBitDW.locations += [SM64Location(player, loc_name, location_table[loc_name], regBitDW) for loc_name in + locBitDW_names] world.regions.append(regBitDW) regBasement = Region("Basement", RegionType.Generic, "Basement", player, world) @@ -90,7 +94,8 @@ def create_regions(world: MultiWorld, player: int): regBitFS = Region("Bowser in the Fire Sea", RegionType.Generic, "Bowser in the Fire Sea", player, world) locBitFS_names = [name for name, id in locBitFS_table.items()] - regBitFS.locations += [SM64Location(player, loc_name, location_table[loc_name], regBitFS) for loc_name in locBitFS_names] + regBitFS.locations += [SM64Location(player, loc_name, location_table[loc_name], regBitFS) for loc_name in + locBitFS_names] world.regions.append(regBitFS) regFloor2 = Region("Second Floor", RegionType.Generic, "Second Floor", player, world) @@ -142,12 +147,13 @@ def create_regions(world: MultiWorld, player: int): world.regions.append(regRR) -def connect_regions(world: MultiWorld, player: int, source: str, target: str, rule): +def connect_regions(world: MultiWorld, player: int, source: str, target: str, rule=None): sourceRegion = world.get_region(source, player) targetRegion = world.get_region(target, player) - connection = Entrance(player,'', sourceRegion) - connection.access_rule = rule + connection = Entrance(player, '', sourceRegion) + if rule: + connection.access_rule = rule sourceRegion.exits.append(connection) - connection.connect(targetRegion) \ No newline at end of file + connection.connect(targetRegion) diff --git a/worlds/sm64ex/Rules.py b/worlds/sm64ex/Rules.py index 5d6b41b9..f5bb9ff7 100644 --- a/worlds/sm64ex/Rules.py +++ b/worlds/sm64ex/Rules.py @@ -1,14 +1,14 @@ -import typing from ..generic.Rules import add_rule from .Regions import connect_regions, sm64courses -def set_rules(world,player,area_connections): + +def set_rules(world, player: int, area_connections): courseshuffle = list(range(len(sm64courses))) - if (world.AreaRandomizer[player].value): + if world.AreaRandomizer[player].value: world.random.shuffle(courseshuffle) area_connections.update({index: value for index, value in enumerate(courseshuffle)}) - connect_regions(world, player, "Menu", sm64courses[area_connections[0]], lambda state: True) + connect_regions(world, player, "Menu", sm64courses[area_connections[0]]) connect_regions(world, player, "Menu", sm64courses[area_connections[1]], lambda state: state.has("Power Star", player, 1)) connect_regions(world, player, "Menu", sm64courses[area_connections[2]], lambda state: state.has("Power Star", player, 3)) connect_regions(world, player, "Menu", sm64courses[area_connections[3]], lambda state: state.has("Power Star", player, 3)) @@ -17,24 +17,24 @@ def set_rules(world,player,area_connections): connect_regions(world, player, "Menu", "Basement", lambda state: state.has("Basement Key", player) or state.has("Progressive Key", player, 1)) - connect_regions(world, player, "Basement", sm64courses[area_connections[5]], lambda state: True) - connect_regions(world, player, "Basement", sm64courses[area_connections[6]], lambda state: True) - connect_regions(world, player, "Basement", sm64courses[area_connections[7]], lambda state: True) + connect_regions(world, player, "Basement", sm64courses[area_connections[5]]) + connect_regions(world, player, "Basement", sm64courses[area_connections[6]]) + connect_regions(world, player, "Basement", sm64courses[area_connections[7]]) connect_regions(world, player, "Basement", sm64courses[area_connections[8]], lambda state: state.has("Power Star", player, 30)) connect_regions(world, player, "Basement", "Bowser in the Fire Sea", lambda state: state.has("Power Star", player, 30) and state.can_reach("Dire, Dire Docks", 'Region', player)) connect_regions(world, player, "Menu", "Second Floor", lambda state: state.has("Second Floor Key", player) or state.has("Progressive Key", player, 2)) - connect_regions(world, player, "Second Floor", sm64courses[area_connections[9]], lambda state: True) - connect_regions(world, player, "Second Floor", sm64courses[area_connections[10]], lambda state: True) - connect_regions(world, player, "Second Floor", sm64courses[area_connections[11]], lambda state: True) - connect_regions(world, player, "Second Floor", sm64courses[area_connections[12]], lambda state: True) + connect_regions(world, player, "Second Floor", sm64courses[area_connections[9]]) + connect_regions(world, player, "Second Floor", sm64courses[area_connections[10]]) + connect_regions(world, player, "Second Floor", sm64courses[area_connections[11]]) + connect_regions(world, player, "Second Floor", sm64courses[area_connections[12]]) connect_regions(world, player, "Second Floor", "Third Floor", lambda state: state.has("Power Star", player, 50)) - connect_regions(world, player, "Third Floor", sm64courses[area_connections[13]], lambda state: True) - connect_regions(world, player, "Third Floor", sm64courses[area_connections[14]], lambda state: True) + connect_regions(world, player, "Third Floor", sm64courses[area_connections[13]]) + connect_regions(world, player, "Third Floor", sm64courses[area_connections[14]]) #Special Rules for some Locations add_rule(world.get_location("Tower of the Wing Cap Switch", player), lambda state: state.has("Power Star", player, 10))