From e187853a7cd3fbbfdf99d23a306841e63121e1d8 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 12 Sep 2025 13:20:39 -0400 Subject: [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. --- apworld/player_logic.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'apworld/player_logic.py') 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: self.complete_at = None self.possibilities = list() + def copy(self) -> "AccessRequirements": + reqs = AccessRequirements() + reqs.items = self.items.copy() + reqs.progressives = self.progressives.copy() + reqs.rooms = self.rooms.copy() + reqs.letters = self.letters.copy() + reqs.cyans = self.cyans + reqs.or_logic = [[other_req.copy() for other_req in disjunction] for disjunction in self.or_logic] + reqs.complete_at = self.complete_at + reqs.possibilities = self.possibilities.copy() + return reqs + def merge(self, other: "AccessRequirements"): for item in other.items: self.items.add(item) @@ -88,7 +100,44 @@ class AccessRequirements: def is_empty(self) -> bool: return (len(self.items) == 0 and len(self.progressives) == 0 and len(self.rooms) == 0 and len(self.letters) == 0 - and not self.cyans and len(self.or_logic) == 0 and self.complete_at is not None) + and not self.cyans and len(self.or_logic) == 0 and self.complete_at is None) + + def __eq__(self, other: "AccessRequirements"): + return (self.items == other.items and self.progressives == other.progressives and self.rooms == other.rooms and + self.letters == other.letters and self.cyans == other.cyans and self.or_logic == other.or_logic and + self.complete_at == other.complete_at and self.possibilities == other.possibilities) + + def simplify(self): + resimplify = False + + if len(self.or_logic) > 0: + old_or_logic = self.or_logic + + def remove_redundant(sub_reqs: "AccessRequirements"): + sub_reqs.letters = {l: v for l, v in sub_reqs.letters.items() if self.letters.get(l, 0) < v} + + self.or_logic = [] + for disjunction in old_or_logic: + new_disjunction = [] + for ssr in disjunction: + remove_redundant(ssr) + if not ssr.is_empty(): + new_disjunction.append(ssr) + else: + new_disjunction.clear() + break + if len(new_disjunction) == 1: + self.merge(new_disjunction[0]) + resimplify = True + elif len(new_disjunction) > 1: + if all(cjr == new_disjunction[0] for cjr in new_disjunction): + self.merge(new_disjunction[0]) + resimplify = True + else: + self.or_logic.append(new_disjunction) + + if resimplify: + self.simplify() def __repr__(self): parts = [] @@ -403,6 +452,8 @@ class Lingo2PlayerLogic: sub_reqs = self.get_door_open_reqs(sub_door_id) reqs.merge(sub_reqs) + reqs.simplify() + return reqs # This gets the requirements to open a door within the world. When a door is shuffled, this means having the item -- cgit 1.4.1