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.py151
1 files changed, 144 insertions, 7 deletions
diff --git a/apworld/regions.py b/apworld/regions.py index 1c8e672..3735858 100644 --- a/apworld/regions.py +++ b/apworld/regions.py
@@ -1,6 +1,9 @@
1from typing import TYPE_CHECKING 1from typing import TYPE_CHECKING
2 2
3from BaseClasses import Region 3import BaseClasses
4from BaseClasses import Region, ItemClassification, Entrance
5from entrance_rando import randomize_entrances
6from .items import Lingo2Item
4from .locations import Lingo2Location 7from .locations import Lingo2Location
5from .player_logic import AccessRequirements 8from .player_logic import AccessRequirements
6from .rules import make_location_lambda 9from .rules import make_location_lambda
@@ -10,15 +13,42 @@ if TYPE_CHECKING:
10 13
11 14
12def create_region(room, world: "Lingo2World") -> Region: 15def create_region(room, world: "Lingo2World") -> Region:
13 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)
14 17
18
19def create_locations(room, new_region: Region, world: "Lingo2World", regions: dict[str, Region]):
15 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
16 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],
17 location.code, new_region) 25 location.code, new_region)
18 new_location.access_rule = make_location_lambda(location.reqs, world) 26 new_location.access_rule = make_location_lambda(reqs, world, regions)
27 new_region.locations.append(new_location)
28
29 for event_name, item_name in world.player_logic.event_loc_item_by_room.get(room.id, {}).items():
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
34 event_item = Lingo2Item(item_name, ItemClassification.progression, None, world.player)
35 new_location.place_locked_item(event_item)
19 new_region.locations.append(new_location) 36 new_region.locations.append(new_location)
20 37
21 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)
22 52
23 53
24def create_regions(world: "Lingo2World"): 54def create_regions(world: "Lingo2World"):
@@ -26,16 +56,34 @@ def create_regions(world: "Lingo2World"):
26 "Menu": Region("Menu", world.player, world.multiworld) 56 "Menu": Region("Menu", world.player, world.multiworld)
27 } 57 }
28 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.
29 for room in world.static_logic.objects.rooms: 64 for room in world.static_logic.objects.rooms:
30 region = create_region(room, world) 65 region = create_region(room, world)
31 regions[region.name] = region 66 regions[region.name] = region
67 region_and_room.append((region, room))
68
69 for (region, room) in region_and_room:
70 create_locations(room, region, world, regions)
32 71
33 regions["Menu"].connect(regions["The Entry - Starting Room"], "Start Game") 72 regions["Menu"].connect(regions["The Entry - Starting Room"], "Start Game")
34 73
35 # TODO: The requirements of the opposite trigger also matter.
36 for connection in world.static_logic.objects.connections: 74 for connection in world.static_logic.objects.connections:
75 if connection.roof_access and not world.options.daedalus_roof_access:
76 continue
77
78 if connection.vanilla_only and world.options.shuffle_doors:
79 continue
80
37 from_region = world.static_logic.get_room_region_name(connection.from_room) 81 from_region = world.static_logic.get_room_region_name(connection.from_room)
38 to_region = world.static_logic.get_room_region_name(connection.to_room) 82 to_region = world.static_logic.get_room_region_name(connection.to_room)
83
84 if from_region not in regions or to_region not in regions:
85 continue
86
39 connection_name = f"{from_region} -> {to_region}" 87 connection_name = f"{from_region} -> {to_region}"
40 88
41 reqs = AccessRequirements() 89 reqs = AccessRequirements()
@@ -51,6 +99,9 @@ def create_regions(world: "Lingo2World"):
51 port = world.static_logic.objects.ports[connection.port] 99 port = world.static_logic.objects.ports[connection.port]
52 connection_name = f"{connection_name} (via port {port.name})" 100 connection_name = f"{connection_name} (via port {port.name})"
53 101
102 if world.options.shuffle_worldports and not port.no_shuffle:
103 continue
104
54 if port.HasField("required_door"): 105 if port.HasField("required_door"):
55 reqs.merge(world.player_logic.get_door_open_reqs(port.required_door)) 106 reqs.merge(world.player_logic.get_door_open_reqs(port.required_door))
56 107
@@ -72,7 +123,93 @@ def create_regions(world: "Lingo2World"):
72 else: 123 else:
73 connection_name = f"{connection_name} (via panel {panel.name})" 124 connection_name = f"{connection_name} (via panel {panel.name})"
74 125
75 if from_region in regions and to_region in regions: 126 if connection.HasField("purple_ending") and connection.purple_ending and world.options.strict_purple_ending:
76 regions[from_region].connect(regions[to_region], connection_name, make_location_lambda(reqs, world)) 127 world.player_logic.add_solution_reqs(reqs, "abcdefghijklmnopqrstuvwxyz")
128
129 if connection.HasField("cyan_ending") and connection.cyan_ending and world.options.strict_cyan_ending:
130 world.player_logic.add_solution_reqs(reqs, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
131
132 reqs.simplify()
133 reqs.remove_room(from_region)
134
135 connection = Entrance(world.player, connection_name, regions[from_region])
136 connection.access_rule = make_location_lambda(reqs, world, regions)
137
138 regions[from_region].exits.append(connection)
139 connection.connect(regions[to_region])
140
141 for region in reqs.get_referenced_rooms():
142 world.multiworld.register_indirect_condition(regions[region], connection)
77 143
78 world.multiworld.regions += regions.values() 144 world.multiworld.regions += regions.values()
145
146
147def shuffle_entrances(world: "Lingo2World"):
148 er_entrances: list[Entrance] = []
149 er_exits: list[Entrance] = []
150
151 port_id_by_name: dict[str, int] = {}
152
153 for port in world.static_logic.objects.ports:
154 if port.no_shuffle:
155 continue
156
157 port_region_name = world.static_logic.get_room_region_name(port.room_id)
158 port_region = world.multiworld.get_region(port_region_name, world.player)
159
160 connection_name = f"{port_region_name} - {port.name}"
161 port_id_by_name[connection_name] = port.id
162
163 entrance = port_region.create_er_target(connection_name)
164 entrance.randomization_type = BaseClasses.EntranceType.TWO_WAY
165
166 er_exit = port_region.create_exit(connection_name)
167 er_exit.randomization_type = BaseClasses.EntranceType.TWO_WAY
168
169 if port.HasField("required_door"):
170 door_reqs = world.player_logic.get_door_open_reqs(port.required_door)
171 er_exit.access_rule = make_location_lambda(door_reqs, world, None)
172
173 for region in door_reqs.get_referenced_rooms():
174 world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player),
175 er_exit)
176
177 er_entrances.append(entrance)
178 er_exits.append(er_exit)
179
180 result = randomize_entrances(world, True, {0:[0]}, False, er_entrances,
181 er_exits)
182
183 for (f, to) in result.pairings:
184 world.port_pairings[port_id_by_name[f]] = port_id_by_name[to]
185
186
187def connect_ports_from_ut(port_pairings: dict[int, int], world: "Lingo2World"):
188 for fpid, tpid in port_pairings.items():
189 from_port = world.static_logic.objects.ports[fpid]
190 to_port = world.static_logic.objects.ports[tpid]
191
192 from_region_name = world.static_logic.get_room_region_name(from_port.room_id)
193 to_region_name = world.static_logic.get_room_region_name(to_port.room_id)
194
195 from_region = world.multiworld.get_region(from_region_name, world.player)
196 to_region = world.multiworld.get_region(to_region_name, world.player)
197
198 connection = Entrance(world.player, f"{from_region_name} - {from_port.name}", from_region)
199
200 reqs = AccessRequirements()
201 if from_port.HasField("required_door"):
202 reqs = world.player_logic.get_door_open_reqs(from_port.required_door).copy()
203
204 if world.for_tracker:
205 reqs.items.add(f"Worldport {fpid} Entered")
206
207 if not reqs.is_empty():
208 connection.access_rule = make_location_lambda(reqs, world, None)
209
210 for region in reqs.get_referenced_rooms():
211 world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player),
212 connection)
213
214 from_region.exits.append(connection)
215 connection.connect(to_region)