about summary refs log tree commit diff stats
path: root/apworld/player_logic.py
diff options
context:
space:
mode:
Diffstat (limited to 'apworld/player_logic.py')
-rw-r--r--apworld/player_logic.py127
1 files changed, 122 insertions, 5 deletions
diff --git a/apworld/player_logic.py b/apworld/player_logic.py index 42b36e6..4aa481d 100644 --- a/apworld/player_logic.py +++ b/apworld/player_logic.py
@@ -30,6 +30,11 @@ class AccessRequirements:
30 # This is an AND of ORs. 30 # This is an AND of ORs.
31 or_logic: list[list["AccessRequirements"]] 31 or_logic: list[list["AccessRequirements"]]
32 32
33 # When complete_at is set, at least that many of the requirements in possibilities must be accessible. This should
34 # only be used for doors with complete_at > 1, as or_logic is more efficient for complete_at == 1.
35 complete_at: int | None
36 possibilities: list["AccessRequirements"]
37
33 def __init__(self): 38 def __init__(self):
34 self.items = set() 39 self.items = set()
35 self.progressives = dict() 40 self.progressives = dict()
@@ -37,6 +42,20 @@ class AccessRequirements:
37 self.letters = dict() 42 self.letters = dict()
38 self.cyans = False 43 self.cyans = False
39 self.or_logic = list() 44 self.or_logic = list()
45 self.complete_at = None
46 self.possibilities = list()
47
48 def copy(self) -> "AccessRequirements":
49 reqs = AccessRequirements()
50 reqs.items = self.items.copy()
51 reqs.progressives = self.progressives.copy()
52 reqs.rooms = self.rooms.copy()
53 reqs.letters = self.letters.copy()
54 reqs.cyans = self.cyans
55 reqs.or_logic = [[other_req.copy() for other_req in disjunction] for disjunction in self.or_logic]
56 reqs.complete_at = self.complete_at
57 reqs.possibilities = self.possibilities.copy()
58 return reqs
40 59
41 def merge(self, other: "AccessRequirements"): 60 def merge(self, other: "AccessRequirements"):
42 for item in other.items: 61 for item in other.items:
@@ -56,9 +75,97 @@ class AccessRequirements:
56 for disjunction in other.or_logic: 75 for disjunction in other.or_logic:
57 self.or_logic.append(disjunction) 76 self.or_logic.append(disjunction)
58 77
78 if other.complete_at is not None:
79 # Merging multiple requirements that use complete_at sucks, and is part of why we want to minimize use of
80 # it. If both requirements use complete_at, we will cheat by using the or_logic field, which supports
81 # conjunctions of requirements.
82 if self.complete_at is not None:
83 print("Merging requirements with complete_at > 1. This is messy and should be avoided!")
84
85 left_req = AccessRequirements()
86 left_req.complete_at = self.complete_at
87 left_req.possibilities = self.possibilities
88 self.or_logic.append([left_req])
89
90 self.complete_at = None
91 self.possibilities = list()
92
93 right_req = AccessRequirements()
94 right_req.complete_at = other.complete_at
95 right_req.possibilities = other.possibilities
96 self.or_logic.append([right_req])
97 else:
98 self.complete_at = other.complete_at
99 self.possibilities = other.possibilities
100
59 def is_empty(self) -> bool: 101 def is_empty(self) -> bool:
60 return (len(self.items) == 0 and len(self.progressives) == 0 and len(self.rooms) == 0 and len(self.letters) == 0 102 return (len(self.items) == 0 and len(self.progressives) == 0 and len(self.rooms) == 0 and len(self.letters) == 0
61 and not self.cyans and len(self.or_logic) == 0) 103 and not self.cyans and len(self.or_logic) == 0 and self.complete_at is None)
104
105 def __eq__(self, other: "AccessRequirements"):
106 return (self.items == other.items and self.progressives == other.progressives and self.rooms == other.rooms and
107 self.letters == other.letters and self.cyans == other.cyans and self.or_logic == other.or_logic and
108 self.complete_at == other.complete_at and self.possibilities == other.possibilities)
109
110 def simplify(self):
111 resimplify = False
112
113 if len(self.or_logic) > 0:
114 old_or_logic = self.or_logic
115
116 def remove_redundant(sub_reqs: "AccessRequirements"):
117 new_reqs = sub_reqs.copy()
118 new_reqs.letters = {l: v for l, v in new_reqs.letters.items() if self.letters.get(l, 0) < v}
119 if new_reqs != sub_reqs:
120 return new_reqs
121 else:
122 return sub_reqs
123
124 self.or_logic = []
125 for disjunction in old_or_logic:
126 new_disjunction = []
127 for ssr in disjunction:
128 new_ssr = remove_redundant(ssr)
129 if not new_ssr.is_empty():
130 new_disjunction.append(new_ssr)
131 else:
132 new_disjunction.clear()
133 break
134 if len(new_disjunction) == 1:
135 self.merge(new_disjunction[0])
136 resimplify = True
137 elif len(new_disjunction) > 1:
138 if all(cjr == new_disjunction[0] for cjr in new_disjunction):
139 self.merge(new_disjunction[0])
140 resimplify = True
141 else:
142 self.or_logic.append(new_disjunction)
143
144 if resimplify:
145 self.simplify()
146
147 def get_referenced_rooms(self):
148 result = set(self.rooms)
149
150 for disjunction in self.or_logic:
151 for sub_req in disjunction:
152 result = result.union(sub_req.get_referenced_rooms())
153
154 for sub_req in self.possibilities:
155 result = result.union(sub_req.get_referenced_rooms())
156
157 return result
158
159 def remove_room(self, room: str):
160 if room in self.rooms:
161 self.rooms.remove(room)
162
163 for disjunction in self.or_logic:
164 for sub_req in disjunction:
165 sub_req.remove_room(room)
166
167 for sub_req in self.possibilities:
168 sub_req.remove_room(room)
62 169
63 def __repr__(self): 170 def __repr__(self):
64 parts = [] 171 parts = []
@@ -74,7 +181,11 @@ class AccessRequirements:
74 parts.append(f"cyans=True") 181 parts.append(f"cyans=True")
75 if len(self.or_logic) > 0: 182 if len(self.or_logic) > 0:
76 parts.append(f"or_logic={self.or_logic}") 183 parts.append(f"or_logic={self.or_logic}")
77 return f"AccessRequirements({", ".join(parts)})" 184 if self.complete_at is not None:
185 parts.append(f"complete_at={self.complete_at}")
186 if len(self.possibilities) > 0:
187 parts.append(f"possibilities={self.possibilities}")
188 return "AccessRequirements(" + ", ".join(parts) + ")"
78 189
79 190
80class PlayerLocation(NamedTuple): 191class PlayerLocation(NamedTuple):
@@ -156,6 +267,9 @@ class Lingo2PlayerLogic:
156 not self.world.options.shuffle_control_center_colors): 267 not self.world.options.shuffle_control_center_colors):
157 continue 268 continue
158 269
270 if door.type == data_pb2.DoorType.GALLERY_PAINTING and not self.world.options.shuffle_gallery_paintings:
271 continue
272
159 door_item_name = self.world.static_logic.get_door_item_name(door) 273 door_item_name = self.world.static_logic.get_door_item_name(door)
160 self.item_by_door[door.id] = (door_item_name, 1) 274 self.item_by_door[door.id] = (door_item_name, 1)
161 self.real_items.append(door_item_name) 275 self.real_items.append(door_item_name)
@@ -306,7 +420,6 @@ class Lingo2PlayerLogic:
306 door = self.world.static_logic.objects.doors[door_id] 420 door = self.world.static_logic.objects.doors[door_id]
307 reqs = AccessRequirements() 421 reqs = AccessRequirements()
308 422
309 # TODO: lavender_cubes, endings
310 if not door.HasField("complete_at") or door.complete_at == 0: 423 if not door.HasField("complete_at") or door.complete_at == 0:
311 for proxy in door.panels: 424 for proxy in door.panels:
312 panel_reqs = self.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None) 425 panel_reqs = self.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None)
@@ -323,8 +436,10 @@ class Lingo2PlayerLogic:
323 if len(disjunction) > 0: 436 if len(disjunction) > 0:
324 reqs.or_logic.append(disjunction) 437 reqs.or_logic.append(disjunction)
325 else: 438 else:
326 # TODO: Handle complete_at > 1 439 reqs.complete_at = door.complete_at
327 pass 440 for proxy in door.panels:
441 panel_reqs = self.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None)
442 reqs.possibilities.append(panel_reqs)
328 443
329 if door.HasField("control_center_color"): 444 if door.HasField("control_center_color"):
330 # TODO: Logic for ensuring two CC states aren't needed at once. 445 # TODO: Logic for ensuring two CC states aren't needed at once.
@@ -365,6 +480,8 @@ class Lingo2PlayerLogic:
365 sub_reqs = self.get_door_open_reqs(sub_door_id) 480 sub_reqs = self.get_door_open_reqs(sub_door_id)
366 reqs.merge(sub_reqs) 481 reqs.merge(sub_reqs)
367 482
483 reqs.simplify()
484
368 return reqs 485 return reqs
369 486
370 # This gets the requirements to open a door within the world. When a door is shuffled, this means having the item 487 # This gets the requirements to open a door within the world. When a door is shuffled, this means having the item