From e187853a7cd3fbbfdf99d23a306841e63121e1d8 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 12 Sep 2025 13:20:39 -0400 Subject: [Apworld] Some access checking optimizations Letter requirements in OR logic (which is the main thing OR logic is used for) is simplified now. Any requirement within the OR logic that is redundant with the top level requirement now has the redundant letters removed. If a clause in a disjunction becomes empty due to this, the disjunction can be removed. Additionally, if all of the clauses in a disjunction are identical, then they can be merged into the top level requirement. I manually verified that every requirement that is affected by this simplification looks correct. Region objects are also now used in access checking instead of looking up the regions by name during access checking. This is a little faster for access checks that involve a lot of rooms, such as the Maze Gravestone. Finally, locations no longer check for access to the region the location is in, and connections no longer check for access to the source region, because these are both implied by how the graph works. --- apworld/regions.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'apworld/regions.py') diff --git a/apworld/regions.py b/apworld/regions.py index e30493c..4f1dd55 100644 --- a/apworld/regions.py +++ b/apworld/regions.py @@ -11,12 +11,18 @@ if TYPE_CHECKING: def create_region(room, world: "Lingo2World") -> Region: - new_region = Region(world.static_logic.get_room_region_name(room.id), world.player, world.multiworld) + return Region(world.static_logic.get_room_region_name(room.id), world.player, world.multiworld) + +def create_locations(room, new_region: Region, world: "Lingo2World", regions: dict[str, Region]): for location in world.player_logic.locations_by_room.get(room.id, {}): + reqs = location.reqs.copy() + if new_region.name in reqs.rooms: + reqs.rooms.remove(new_region.name) + new_location = Lingo2Location(world.player, world.static_logic.location_id_to_name[location.code], location.code, new_region) - new_location.access_rule = make_location_lambda(location.reqs, world) + new_location.access_rule = make_location_lambda(reqs, world, regions) new_region.locations.append(new_location) for event_name, item_name in world.player_logic.event_loc_item_by_room.get(room.id, {}).items(): @@ -25,17 +31,23 @@ def create_region(room, world: "Lingo2World") -> Region: new_location.place_locked_item(event_item) new_region.locations.append(new_location) - return new_region - - def create_regions(world: "Lingo2World"): regions = { "Menu": Region("Menu", world.player, world.multiworld) } + region_and_room = [] + + # Create the regions in two stages. First, make the actual region objects and memoize them. Then, add all of the + # locations. This allows us to reference the actual region objects in the access rules for the locations, which is + # faster than having to look them up during access checking. for room in world.static_logic.objects.rooms: region = create_region(room, world) regions[region.name] = region + region_and_room.append((region, room)) + + for (region, room) in region_and_room: + create_locations(room, region, world, regions) regions["Menu"].connect(regions["The Entry - Starting Room"], "Start Game") @@ -82,14 +94,18 @@ def create_regions(world: "Lingo2World"): else: connection_name = f"{connection_name} (via panel {panel.name})" + reqs.simplify() + if from_region in regions and to_region in regions: connection = Entrance(world.player, connection_name, regions[from_region]) - connection.access_rule = make_location_lambda(reqs, world) + connection.access_rule = make_location_lambda(reqs, world, regions) regions[from_region].exits.append(connection) connection.connect(regions[to_region]) for region in reqs.rooms: + if region == from_region: + continue world.multiworld.register_indirect_condition(regions[region], connection) world.multiworld.regions += regions.values() -- cgit 1.4.1