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