about summary refs log tree commit diff stats
path: root/apworld/player_logic.py
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2025-09-12 13:20:39 -0400
committerStar Rauchenberger <fefferburbia@gmail.com>2025-09-12 13:20:39 -0400
commite187853a7cd3fbbfdf99d23a306841e63121e1d8 (patch)
tree36799a8f6b9a5e23471b3ca31808cd62687249e0 /apworld/player_logic.py
parent67b721503443274351e0729ac57cbff83d31d753 (diff)
downloadlingo2-archipelago-e187853a7cd3fbbfdf99d23a306841e63121e1d8.tar.gz
lingo2-archipelago-e187853a7cd3fbbfdf99d23a306841e63121e1d8.tar.bz2
lingo2-archipelago-e187853a7cd3fbbfdf99d23a306841e63121e1d8.zip
[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.
Diffstat (limited to 'apworld/player_logic.py')
-rw-r--r--apworld/player_logic.py53
1 files changed, 52 insertions, 1 deletions
diff --git a/apworld/player_logic.py b/apworld/player_logic.py index 2ff7163..8e2a523 100644 --- a/apworld/player_logic.py +++ b/apworld/player_logic.py
@@ -45,6 +45,18 @@ class AccessRequirements:
45 self.complete_at = None 45 self.complete_at = None
46 self.possibilities = list() 46 self.possibilities = list()
47 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
59
48 def merge(self, other: "AccessRequirements"): 60 def merge(self, other: "AccessRequirements"):
49 for item in other.items: 61 for item in other.items:
50 self.items.add(item) 62 self.items.add(item)
@@ -88,7 +100,44 @@ class AccessRequirements:
88 100
89 def is_empty(self) -> bool: 101 def is_empty(self) -> bool:
90 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
91 and not self.cyans and len(self.or_logic) == 0 and self.complete_at is not None) 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 sub_reqs.letters = {l: v for l, v in sub_reqs.letters.items() if self.letters.get(l, 0) < v}
118
119 self.or_logic = []
120 for disjunction in old_or_logic:
121 new_disjunction = []
122 for ssr in disjunction:
123 remove_redundant(ssr)
124 if not ssr.is_empty():
125 new_disjunction.append(ssr)
126 else:
127 new_disjunction.clear()
128 break
129 if len(new_disjunction) == 1:
130 self.merge(new_disjunction[0])
131 resimplify = True
132 elif len(new_disjunction) > 1:
133 if all(cjr == new_disjunction[0] for cjr in new_disjunction):
134 self.merge(new_disjunction[0])
135 resimplify = True
136 else:
137 self.or_logic.append(new_disjunction)
138
139 if resimplify:
140 self.simplify()
92 141
93 def __repr__(self): 142 def __repr__(self):
94 parts = [] 143 parts = []
@@ -403,6 +452,8 @@ class Lingo2PlayerLogic:
403 sub_reqs = self.get_door_open_reqs(sub_door_id) 452 sub_reqs = self.get_door_open_reqs(sub_door_id)
404 reqs.merge(sub_reqs) 453 reqs.merge(sub_reqs)
405 454
455 reqs.simplify()
456
406 return reqs 457 return reqs
407 458
408 # This gets the requirements to open a door within the world. When a door is shuffled, this means having the item 459 # This gets the requirements to open a door within the world. When a door is shuffled, this means having the item