diff options
Diffstat (limited to 'apworld/regions.py')
| -rw-r--r-- | apworld/regions.py | 227 |
1 files changed, 210 insertions, 17 deletions
| diff --git a/apworld/regions.py b/apworld/regions.py index fe2c99b..313fd02 100644 --- a/apworld/regions.py +++ b/apworld/regions.py | |||
| @@ -1,31 +1,57 @@ | |||
| 1 | from typing import TYPE_CHECKING | 1 | from typing import TYPE_CHECKING |
| 2 | 2 | ||
| 3 | import BaseClasses | ||
| 3 | from BaseClasses import Region, ItemClassification, Entrance | 4 | from BaseClasses import Region, ItemClassification, Entrance |
| 5 | from entrance_rando import randomize_entrances | ||
| 4 | from .items import Lingo2Item | 6 | from .items import Lingo2Item |
| 5 | from .locations import Lingo2Location | 7 | from .locations import Lingo2Location, LetterPlacementType, Lingo2Entrance |
| 8 | from .options import FastTravelAccess | ||
| 6 | from .player_logic import AccessRequirements | 9 | from .player_logic import AccessRequirements |
| 7 | from .rules import make_location_lambda | ||
| 8 | 10 | ||
| 9 | if TYPE_CHECKING: | 11 | if TYPE_CHECKING: |
| 10 | from . import Lingo2World | 12 | from . import Lingo2World |
| 11 | 13 | ||
| 12 | 14 | ||
| 13 | def create_region(room, world: "Lingo2World") -> Region: | 15 | def create_region(room, world: "Lingo2World") -> Region: |
| 14 | new_region = Region(world.static_logic.get_room_region_name(room.id), world.player, world.multiworld) | 16 | return Region(world.static_logic.get_room_region_name(room.id), world.player, world.multiworld) |
| 15 | 17 | ||
| 18 | |||
| 19 | def create_locations(room, new_region: Region, world: "Lingo2World", regions: dict[str, Region]): | ||
| 16 | for location in world.player_logic.locations_by_room.get(room.id, {}): | 20 | for location in world.player_logic.locations_by_room.get(room.id, {}): |
| 17 | new_location = Lingo2Location(world.player, world.static_logic.location_id_to_name[location.code], | 21 | reqs = location.reqs.copy() |
| 18 | location.code, new_region) | 22 | reqs.remove_room(new_region.name) |
| 19 | new_location.access_rule = make_location_lambda(location.reqs, world) | 23 | |
| 24 | new_location = Lingo2Location.non_event_location(world, location.code, new_region) | ||
| 25 | new_location.set_access_rule(reqs, regions) | ||
| 26 | if world.options.restrict_letter_placements: | ||
| 27 | if location.is_letter: | ||
| 28 | new_location.set_up_letter_rule(LetterPlacementType.FORCE) | ||
| 29 | else: | ||
| 30 | new_location.set_up_letter_rule(LetterPlacementType.DISALLOW) | ||
| 20 | new_region.locations.append(new_location) | 31 | new_region.locations.append(new_location) |
| 21 | 32 | ||
| 22 | for event_name, item_name in world.player_logic.event_loc_item_by_room.get(room.id, {}).items(): | 33 | for event_name, item_name in world.player_logic.event_loc_item_by_room.get(room.id, {}).items(): |
| 23 | new_location = Lingo2Location(world.player, event_name, None, new_region) | 34 | new_location = Lingo2Location.event_location(world, event_name, new_region) |
| 35 | if world.for_tracker and item_name == "Victory": | ||
| 36 | new_location.goal = True | ||
| 37 | |||
| 24 | event_item = Lingo2Item(item_name, ItemClassification.progression, None, world.player) | 38 | event_item = Lingo2Item(item_name, ItemClassification.progression, None, world.player) |
| 25 | new_location.place_locked_item(event_item) | 39 | new_location.place_locked_item(event_item) |
| 26 | new_region.locations.append(new_location) | 40 | new_region.locations.append(new_location) |
| 27 | 41 | ||
| 28 | return new_region | 42 | if world.for_tracker and world.options.shuffle_worldports: |
| 43 | for port_id in room.ports: | ||
| 44 | port = world.static_logic.objects.ports[port_id] | ||
| 45 | if port.no_shuffle: | ||
| 46 | continue | ||
| 47 | |||
| 48 | new_location = Lingo2Location.event_location(world, f"Worldport {port.id} Entered", new_region) | ||
| 49 | new_location.port_id = port.id | ||
| 50 | |||
| 51 | if port.HasField("required_door"): | ||
| 52 | new_location.set_access_rule(world.player_logic.get_door_open_reqs(port.required_door), regions) | ||
| 53 | |||
| 54 | new_region.locations.append(new_location) | ||
| 29 | 55 | ||
| 30 | 56 | ||
| 31 | def create_regions(world: "Lingo2World"): | 57 | def create_regions(world: "Lingo2World"): |
| @@ -33,16 +59,40 @@ def create_regions(world: "Lingo2World"): | |||
| 33 | "Menu": Region("Menu", world.player, world.multiworld) | 59 | "Menu": Region("Menu", world.player, world.multiworld) |
| 34 | } | 60 | } |
| 35 | 61 | ||
| 62 | region_and_room = [] | ||
| 63 | |||
| 64 | # Create the regions in two stages. First, make the actual region objects and memoize them. Then, add all of the | ||
| 65 | # locations. This allows us to reference the actual region objects in the access rules for the locations, which is | ||
| 66 | # faster than having to look them up during access checking. | ||
| 36 | for room in world.static_logic.objects.rooms: | 67 | for room in world.static_logic.objects.rooms: |
| 68 | if not world.player_logic.should_shuffle_room(room.id): | ||
| 69 | continue | ||
| 70 | |||
| 37 | region = create_region(room, world) | 71 | region = create_region(room, world) |
| 38 | regions[region.name] = region | 72 | regions[region.name] = region |
| 73 | region_and_room.append((region, room)) | ||
| 39 | 74 | ||
| 40 | regions["Menu"].connect(regions["The Entry - Starting Room"], "Start Game") | 75 | for (region, room) in region_and_room: |
| 76 | create_locations(room, region, world, regions) | ||
| 77 | |||
| 78 | if world.options.daedalus_only: | ||
| 79 | regions["Menu"].connect(regions["Daedalus - Starting Room"], "Start Game") | ||
| 80 | else: | ||
| 81 | regions["Menu"].connect(regions["The Entry - Starting Room"], "Start Game") | ||
| 41 | 82 | ||
| 42 | # TODO: The requirements of the opposite trigger also matter. | ||
| 43 | for connection in world.static_logic.objects.connections: | 83 | for connection in world.static_logic.objects.connections: |
| 84 | if connection.roof_access and not world.options.daedalus_roof_access: | ||
| 85 | continue | ||
| 86 | |||
| 87 | if connection.vanilla_only and world.options.shuffle_doors: | ||
| 88 | continue | ||
| 89 | |||
| 44 | from_region = world.static_logic.get_room_region_name(connection.from_room) | 90 | from_region = world.static_logic.get_room_region_name(connection.from_room) |
| 45 | to_region = world.static_logic.get_room_region_name(connection.to_room) | 91 | to_region = world.static_logic.get_room_region_name(connection.to_room) |
| 92 | |||
| 93 | if from_region not in regions or to_region not in regions: | ||
| 94 | continue | ||
| 95 | |||
| 46 | connection_name = f"{from_region} -> {to_region}" | 96 | connection_name = f"{from_region} -> {to_region}" |
| 47 | 97 | ||
| 48 | reqs = AccessRequirements() | 98 | reqs = AccessRequirements() |
| @@ -56,7 +106,10 @@ def create_regions(world: "Lingo2World"): | |||
| 56 | 106 | ||
| 57 | if connection.HasField("port"): | 107 | if connection.HasField("port"): |
| 58 | port = world.static_logic.objects.ports[connection.port] | 108 | port = world.static_logic.objects.ports[connection.port] |
| 59 | connection_name = f"{connection_name} (via port {port.name})" | 109 | connection_name = f"{connection_name} (via {port.display_name})" |
| 110 | |||
| 111 | if world.options.shuffle_worldports and not port.no_shuffle: | ||
| 112 | continue | ||
| 60 | 113 | ||
| 61 | if port.HasField("required_door"): | 114 | if port.HasField("required_door"): |
| 62 | reqs.merge(world.player_logic.get_door_open_reqs(port.required_door)) | 115 | reqs.merge(world.player_logic.get_door_open_reqs(port.required_door)) |
| @@ -79,14 +132,154 @@ def create_regions(world: "Lingo2World"): | |||
| 79 | else: | 132 | else: |
| 80 | connection_name = f"{connection_name} (via panel {panel.name})" | 133 | connection_name = f"{connection_name} (via panel {panel.name})" |
| 81 | 134 | ||
| 82 | if from_region in regions and to_region in regions: | 135 | if connection.HasField("purple_ending") and connection.purple_ending and world.options.strict_purple_ending: |
| 83 | connection = Entrance(world.player, connection_name, regions[from_region]) | 136 | world.player_logic.add_solution_reqs(reqs, "abcdefghijklmnopqrstuvwxyz") |
| 84 | connection.access_rule = make_location_lambda(reqs, world) | 137 | |
| 138 | if connection.HasField("cyan_ending") and connection.cyan_ending and world.options.strict_cyan_ending: | ||
| 139 | world.player_logic.add_solution_reqs(reqs, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz") | ||
| 140 | |||
| 141 | if (connection.HasField("mint_ending") and connection.mint_ending | ||
| 142 | and world.player_logic.custom_mint_ending is not None): | ||
| 143 | world.player_logic.add_solution_reqs(reqs, world.player_logic.custom_mint_ending) | ||
| 144 | |||
| 145 | reqs.simplify() | ||
| 146 | reqs.remove_room(from_region) | ||
| 147 | |||
| 148 | if to_region in reqs.rooms: | ||
| 149 | # This connection can't ever increase access because you're required to have access to the other side in | ||
| 150 | # order for it to be usable. We will just not create the connection at all, in order to help GER figure out | ||
| 151 | # what regions are dead ends. | ||
| 152 | continue | ||
| 153 | |||
| 154 | connection = Lingo2Entrance(world, connection_name, regions[from_region]) | ||
| 155 | connection.set_access_rule(reqs, regions) | ||
| 156 | |||
| 157 | regions[from_region].exits.append(connection) | ||
| 158 | connection.connect(regions[to_region]) | ||
| 159 | |||
| 160 | for region in reqs.get_referenced_rooms(): | ||
| 161 | world.multiworld.register_indirect_condition(regions[region], connection) | ||
| 85 | 162 | ||
| 86 | regions[from_region].exits.append(connection) | 163 | if world.options.fast_travel_access != FastTravelAccess.option_vanilla: |
| 164 | for rte_map_id in world.player_logic.rte_mapping: | ||
| 165 | rte_map = world.static_logic.objects.maps[rte_map_id] | ||
| 166 | to_region = world.static_logic.get_room_region_name(rte_map.rte_room) | ||
| 167 | |||
| 168 | if to_region not in regions: | ||
| 169 | continue | ||
| 170 | |||
| 171 | connection_name = f"Return to {to_region}" | ||
| 172 | connection = Lingo2Entrance(world, connection_name, regions["Menu"]) | ||
| 173 | regions["Menu"].exits.append(connection) | ||
| 87 | connection.connect(regions[to_region]) | 174 | connection.connect(regions[to_region]) |
| 88 | 175 | ||
| 89 | for region in reqs.rooms: | 176 | if world.options.fast_travel_access == FastTravelAccess.option_items: |
| 90 | world.multiworld.register_indirect_condition(regions[region], connection) | 177 | reqs = AccessRequirements() |
| 178 | reqs.items.add(world.static_logic.get_map_rte_item_name(rte_map_id)) | ||
| 179 | |||
| 180 | connection.set_access_rule(reqs, regions) | ||
| 91 | 181 | ||
| 92 | world.multiworld.regions += regions.values() | 182 | world.multiworld.regions += regions.values() |
| 183 | |||
| 184 | |||
| 185 | def shuffle_entrances(world: "Lingo2World"): | ||
| 186 | er_entrances: list[Entrance] = [] | ||
| 187 | er_exits: list[Entrance] = [] | ||
| 188 | |||
| 189 | port_id_by_name: dict[str, int] = {} | ||
| 190 | |||
| 191 | shuffleable_ports = [port for port in world.static_logic.objects.ports | ||
| 192 | if not port.no_shuffle and world.player_logic.should_shuffle_room(port.room_id)] | ||
| 193 | |||
| 194 | if len(shuffleable_ports) % 2 == 1: | ||
| 195 | # We have an odd number of shuffleable ports! Pick a port from a room that has more than one, and make it a | ||
| 196 | # redundant warp to another port. | ||
| 197 | redundant_rooms = set(room.id for room in world.static_logic.objects.rooms if len(room.ports) > 1) | ||
| 198 | redundant_ports = [port for port in shuffleable_ports if port.room_id in redundant_rooms] | ||
| 199 | chosen_port = world.random.choice(redundant_ports) | ||
| 200 | |||
| 201 | shuffleable_ports.remove(chosen_port) | ||
| 202 | |||
| 203 | chosen_destination = world.random.choice(shuffleable_ports) | ||
| 204 | |||
| 205 | world.port_pairings[chosen_port.id] = chosen_destination.id | ||
| 206 | |||
| 207 | from_region_name = world.static_logic.get_room_region_name(chosen_port.room_id) | ||
| 208 | to_region_name = world.static_logic.get_room_region_name(chosen_destination.room_id) | ||
| 209 | |||
| 210 | from_region = world.multiworld.get_region(from_region_name, world.player) | ||
| 211 | to_region = world.multiworld.get_region(to_region_name, world.player) | ||
| 212 | |||
| 213 | connection = Lingo2Entrance(world, f"{from_region_name} - {chosen_port.display_name}", from_region) | ||
| 214 | from_region.exits.append(connection) | ||
| 215 | connection.connect(to_region) | ||
| 216 | |||
| 217 | if chosen_port.HasField("required_door"): | ||
| 218 | door_reqs = world.player_logic.get_door_open_reqs(chosen_port.required_door) | ||
| 219 | connection.set_access_rule(door_reqs, None) | ||
| 220 | |||
| 221 | for region in door_reqs.get_referenced_rooms(): | ||
| 222 | world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player), | ||
| 223 | connection) | ||
| 224 | |||
| 225 | for port in shuffleable_ports: | ||
| 226 | port_region_name = world.static_logic.get_room_region_name(port.room_id) | ||
| 227 | port_region = world.multiworld.get_region(port_region_name, world.player) | ||
| 228 | |||
| 229 | connection_name = f"{port_region_name} - {port.display_name}" | ||
| 230 | port_id_by_name[connection_name] = port.id | ||
| 231 | |||
| 232 | entrance = port_region.create_er_target(connection_name) | ||
| 233 | entrance.randomization_type = BaseClasses.EntranceType.TWO_WAY | ||
| 234 | |||
| 235 | er_exit = Lingo2Entrance(world, connection_name, port_region) | ||
| 236 | port_region.exits.append(er_exit) | ||
| 237 | er_exit.randomization_type = BaseClasses.EntranceType.TWO_WAY | ||
| 238 | |||
| 239 | if port.HasField("required_door"): | ||
| 240 | door_reqs = world.player_logic.get_door_open_reqs(port.required_door) | ||
| 241 | er_exit.set_access_rule(door_reqs, None) | ||
| 242 | |||
| 243 | for region in door_reqs.get_referenced_rooms(): | ||
| 244 | world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player), | ||
| 245 | er_exit) | ||
| 246 | |||
| 247 | er_entrances.append(entrance) | ||
| 248 | er_exits.append(er_exit) | ||
| 249 | |||
| 250 | result = randomize_entrances(world, True, {0:[0]}, False, er_entrances, | ||
| 251 | er_exits) | ||
| 252 | |||
| 253 | for (f, to) in result.pairings: | ||
| 254 | world.port_pairings[port_id_by_name[f]] = port_id_by_name[to] | ||
| 255 | |||
| 256 | |||
| 257 | def connect_ports_from_ut(port_pairings: dict[int, int], world: "Lingo2World"): | ||
| 258 | for fpid, tpid in port_pairings.items(): | ||
| 259 | from_port = world.static_logic.objects.ports[fpid] | ||
| 260 | to_port = world.static_logic.objects.ports[tpid] | ||
| 261 | |||
| 262 | from_region_name = world.static_logic.get_room_region_name(from_port.room_id) | ||
| 263 | to_region_name = world.static_logic.get_room_region_name(to_port.room_id) | ||
| 264 | |||
| 265 | from_region = world.multiworld.get_region(from_region_name, world.player) | ||
| 266 | to_region = world.multiworld.get_region(to_region_name, world.player) | ||
| 267 | |||
| 268 | connection = Lingo2Entrance(world, f"{from_region_name} - {from_port.display_name}", from_region) | ||
| 269 | |||
| 270 | reqs = AccessRequirements() | ||
| 271 | if from_port.HasField("required_door"): | ||
| 272 | reqs = world.player_logic.get_door_open_reqs(from_port.required_door).copy() | ||
| 273 | |||
| 274 | if world.for_tracker: | ||
| 275 | reqs.items.add(f"Worldport {fpid} Entered") | ||
| 276 | |||
| 277 | if not reqs.is_empty(): | ||
| 278 | connection.set_access_rule(reqs, None) | ||
| 279 | |||
| 280 | for region in reqs.get_referenced_rooms(): | ||
| 281 | world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player), | ||
| 282 | connection) | ||
| 283 | |||
| 284 | from_region.exits.append(connection) | ||
| 285 | connection.connect(to_region) | ||
