diff options
Diffstat (limited to 'apworld/rules.py')
| -rw-r--r-- | apworld/rules.py | 243 |
1 files changed, 196 insertions, 47 deletions
| diff --git a/apworld/rules.py b/apworld/rules.py index f859e75..70a76c0 100644 --- a/apworld/rules.py +++ b/apworld/rules.py | |||
| @@ -1,66 +1,215 @@ | |||
| 1 | from collections.abc import Callable | ||
| 2 | from typing import TYPE_CHECKING | 1 | from typing import TYPE_CHECKING |
| 3 | 2 | ||
| 4 | from BaseClasses import CollectionState, Region | 3 | from BaseClasses import CollectionState |
| 5 | from .player_logic import AccessRequirements | ||
| 6 | 4 | ||
| 7 | if TYPE_CHECKING: | 5 | if TYPE_CHECKING: |
| 8 | from . import Lingo2World | 6 | from . import Lingo2World |
| 9 | 7 | ||
| 10 | 8 | ||
| 11 | def lingo2_can_satisfy_requirements(state: CollectionState, reqs: AccessRequirements, regions: list[Region], | 9 | class AccessRequirements: |
| 12 | world: "Lingo2World") -> bool: | 10 | items: set[str] |
| 13 | if not all(state.has(item, world.player) for item in reqs.items): | 11 | progressives: dict[str, int] |
| 14 | return False | 12 | rooms: set[str] |
| 13 | letters: dict[str, int] | ||
| 14 | cyans: bool | ||
| 15 | 15 | ||
| 16 | if not all(state.has(item, world.player, amount) for item, amount in reqs.progressives.items()): | 16 | # This is an AND of ORs. |
| 17 | return False | 17 | or_logic: list[list["AccessRequirements"]] |
| 18 | 18 | ||
| 19 | if not all(state.can_reach_region(region_name, world.player) for region_name in reqs.rooms): | 19 | # When complete_at is set, at least that many of the requirements in possibilities must be accessible. This should |
| 20 | return False | 20 | # only be used for doors with complete_at > 1, as or_logic is more efficient for complete_at == 1. |
| 21 | complete_at: int | None | ||
| 22 | possibilities: list["AccessRequirements"] | ||
| 21 | 23 | ||
| 22 | if not all(state.can_reach(region) for region in regions): | 24 | def __init__(self): |
| 23 | return False | 25 | self.items = set() |
| 26 | self.progressives = dict() | ||
| 27 | self.rooms = set() | ||
| 28 | self.letters = dict() | ||
| 29 | self.cyans = False | ||
| 30 | self.or_logic = list() | ||
| 31 | self.complete_at = None | ||
| 32 | self.possibilities = list() | ||
| 24 | 33 | ||
| 25 | for letter_key, letter_level in reqs.letters.items(): | 34 | def copy(self) -> "AccessRequirements": |
| 26 | if not state.has(letter_key, world.player, letter_level): | 35 | reqs = AccessRequirements() |
| 36 | reqs.items = self.items.copy() | ||
| 37 | reqs.progressives = self.progressives.copy() | ||
| 38 | reqs.rooms = self.rooms.copy() | ||
| 39 | reqs.letters = self.letters.copy() | ||
| 40 | reqs.cyans = self.cyans | ||
| 41 | reqs.or_logic = [[other_req.copy() for other_req in disjunction] for disjunction in self.or_logic] | ||
| 42 | reqs.complete_at = self.complete_at | ||
| 43 | reqs.possibilities = self.possibilities.copy() | ||
| 44 | return reqs | ||
| 45 | |||
| 46 | def merge(self, other: "AccessRequirements"): | ||
| 47 | for item in other.items: | ||
| 48 | self.items.add(item) | ||
| 49 | |||
| 50 | for item, amount in other.progressives.items(): | ||
| 51 | self.progressives[item] = max(amount, self.progressives.get(item, 0)) | ||
| 52 | |||
| 53 | for room in other.rooms: | ||
| 54 | self.rooms.add(room) | ||
| 55 | |||
| 56 | for letter, level in other.letters.items(): | ||
| 57 | self.letters[letter] = max(self.letters.get(letter, 0), level) | ||
| 58 | |||
| 59 | self.cyans = self.cyans or other.cyans | ||
| 60 | |||
| 61 | for disjunction in other.or_logic: | ||
| 62 | self.or_logic.append([sub_req.copy() for sub_req in disjunction]) | ||
| 63 | |||
| 64 | if other.complete_at is not None: | ||
| 65 | # Merging multiple requirements that use complete_at sucks, and is part of why we want to minimize use of | ||
| 66 | # it. If both requirements use complete_at, we will cheat by using the or_logic field, which supports | ||
| 67 | # conjunctions of requirements. | ||
| 68 | if self.complete_at is not None: | ||
| 69 | print("Merging requirements with complete_at > 1. This is messy and should be avoided!") | ||
| 70 | |||
| 71 | left_req = AccessRequirements() | ||
| 72 | left_req.complete_at = self.complete_at | ||
| 73 | left_req.possibilities = [sub_req.copy() for sub_req in self.possibilities] | ||
| 74 | self.or_logic.append([left_req]) | ||
| 75 | |||
| 76 | self.complete_at = None | ||
| 77 | self.possibilities = list() | ||
| 78 | |||
| 79 | right_req = AccessRequirements() | ||
| 80 | right_req.complete_at = other.complete_at | ||
| 81 | right_req.possibilities = [sub_req.copy() for sub_req in other.possibilities] | ||
| 82 | self.or_logic.append([right_req]) | ||
| 83 | else: | ||
| 84 | self.complete_at = other.complete_at | ||
| 85 | self.possibilities = [sub_req.copy() for sub_req in other.possibilities] | ||
| 86 | |||
| 87 | def is_empty(self) -> bool: | ||
| 88 | return (len(self.items) == 0 and len(self.progressives) == 0 and len(self.rooms) == 0 and len(self.letters) == 0 | ||
| 89 | and not self.cyans and len(self.or_logic) == 0 and self.complete_at is None) | ||
| 90 | |||
| 91 | def __eq__(self, other: "AccessRequirements"): | ||
| 92 | return (self.items == other.items and self.progressives == other.progressives and self.rooms == other.rooms and | ||
| 93 | self.letters == other.letters and self.cyans == other.cyans and self.or_logic == other.or_logic and | ||
| 94 | self.complete_at == other.complete_at and self.possibilities == other.possibilities) | ||
| 95 | |||
| 96 | def simplify(self): | ||
| 97 | resimplify = False | ||
| 98 | |||
| 99 | if len(self.or_logic) > 0: | ||
| 100 | old_or_logic = self.or_logic | ||
| 101 | |||
| 102 | def remove_redundant(sub_reqs: "AccessRequirements"): | ||
| 103 | new_reqs = sub_reqs.copy() | ||
| 104 | new_reqs.letters = {l: v for l, v in new_reqs.letters.items() if self.letters.get(l, 0) < v} | ||
| 105 | if new_reqs != sub_reqs: | ||
| 106 | return new_reqs | ||
| 107 | else: | ||
| 108 | return sub_reqs | ||
| 109 | |||
| 110 | self.or_logic = [] | ||
| 111 | for disjunction in old_or_logic: | ||
| 112 | new_disjunction = [] | ||
| 113 | for ssr in disjunction: | ||
| 114 | new_ssr = remove_redundant(ssr) | ||
| 115 | if not new_ssr.is_empty(): | ||
| 116 | new_disjunction.append(new_ssr) | ||
| 117 | else: | ||
| 118 | new_disjunction.clear() | ||
| 119 | break | ||
| 120 | if len(new_disjunction) == 1: | ||
| 121 | self.merge(new_disjunction[0]) | ||
| 122 | resimplify = True | ||
| 123 | elif len(new_disjunction) > 1: | ||
| 124 | if all(cjr == new_disjunction[0] for cjr in new_disjunction): | ||
| 125 | self.merge(new_disjunction[0]) | ||
| 126 | resimplify = True | ||
| 127 | else: | ||
| 128 | self.or_logic.append(new_disjunction) | ||
| 129 | |||
| 130 | if resimplify: | ||
| 131 | self.simplify() | ||
| 132 | |||
| 133 | def get_referenced_rooms(self): | ||
| 134 | result = set(self.rooms) | ||
| 135 | |||
| 136 | for disjunction in self.or_logic: | ||
| 137 | for sub_req in disjunction: | ||
| 138 | result = result.union(sub_req.get_referenced_rooms()) | ||
| 139 | |||
| 140 | for sub_req in self.possibilities: | ||
| 141 | result = result.union(sub_req.get_referenced_rooms()) | ||
| 142 | |||
| 143 | return result | ||
| 144 | |||
| 145 | def remove_room(self, room: str): | ||
| 146 | if room in self.rooms: | ||
| 147 | self.rooms.remove(room) | ||
| 148 | |||
| 149 | for disjunction in self.or_logic: | ||
| 150 | for sub_req in disjunction: | ||
| 151 | sub_req.remove_room(room) | ||
| 152 | |||
| 153 | for sub_req in self.possibilities: | ||
| 154 | sub_req.remove_room(room) | ||
| 155 | |||
| 156 | def __repr__(self): | ||
| 157 | parts = [] | ||
| 158 | if len(self.items) > 0: | ||
| 159 | parts.append(f"items={self.items}") | ||
| 160 | if len(self.progressives) > 0: | ||
| 161 | parts.append(f"progressives={self.progressives}") | ||
| 162 | if len(self.rooms) > 0: | ||
| 163 | parts.append(f"rooms={self.rooms}") | ||
| 164 | if len(self.letters) > 0: | ||
| 165 | parts.append(f"letters={self.letters}") | ||
| 166 | if self.cyans: | ||
| 167 | parts.append(f"cyans=True") | ||
| 168 | if len(self.or_logic) > 0: | ||
| 169 | parts.append(f"or_logic={self.or_logic}") | ||
| 170 | if self.complete_at is not None: | ||
| 171 | parts.append(f"complete_at={self.complete_at}") | ||
| 172 | if len(self.possibilities) > 0: | ||
| 173 | parts.append(f"possibilities={self.possibilities}") | ||
| 174 | return "AccessRequirements(" + ", ".join(parts) + ")" | ||
| 175 | |||
| 176 | def check_access(self, state: CollectionState, world: "Lingo2World") -> bool: | ||
| 177 | if not all(state.has(item, world.player) for item in self.items): | ||
| 27 | return False | 178 | return False |
| 28 | 179 | ||
| 29 | if reqs.cyans: | 180 | if not all(state.has(item, world.player, amount) for item, amount in self.progressives.items()): |
| 30 | if not any(state.has(letter, world.player, amount) | ||
| 31 | for letter, amount in world.player_logic.double_letter_amount.items()): | ||
| 32 | return False | 181 | return False |
| 33 | 182 | ||
| 34 | if len(reqs.or_logic) > 0: | 183 | if not all(state.can_reach_region(region_name, world.player) for region_name in self.rooms): |
| 35 | if not all(any(lingo2_can_satisfy_requirements(state, sub_reqs, [], world) for sub_reqs in subjunction) | ||
| 36 | for subjunction in reqs.or_logic): | ||
| 37 | return False | 184 | return False |
| 38 | 185 | ||
| 39 | if reqs.complete_at is not None: | 186 | for letter_key, letter_level in self.letters.items(): |
| 40 | completed = 0 | 187 | if not state.has(letter_key, world.player, letter_level): |
| 41 | checked = 0 | 188 | return False |
| 42 | for possibility in reqs.possibilities: | 189 | |
| 43 | checked += 1 | 190 | if self.cyans: |
| 44 | if lingo2_can_satisfy_requirements(state, possibility, [], world): | 191 | if not any(state.has(letter, world.player, amount) |
| 45 | completed += 1 | 192 | for letter, amount in world.player_logic.double_letter_amount.items()): |
| 46 | if completed >= reqs.complete_at: | 193 | return False |
| 47 | break | 194 | |
| 48 | elif len(reqs.possibilities) - checked + completed < reqs.complete_at: | 195 | if len(self.or_logic) > 0: |
| 49 | # There aren't enough remaining possibilities for the check to pass. | 196 | if not all(any(sub_reqs.check_access(state, world) for sub_reqs in subjunction) |
| 197 | for subjunction in self.or_logic): | ||
| 198 | return False | ||
| 199 | |||
| 200 | if self.complete_at is not None: | ||
| 201 | completed = 0 | ||
| 202 | checked = 0 | ||
| 203 | for possibility in self.possibilities: | ||
| 204 | checked += 1 | ||
| 205 | if possibility.check_access(state, world): | ||
| 206 | completed += 1 | ||
| 207 | if completed >= self.complete_at: | ||
| 208 | break | ||
| 209 | elif len(self.possibilities) - checked + completed < self.complete_at: | ||
| 210 | # There aren't enough remaining possibilities for the check to pass. | ||
| 211 | return False | ||
| 212 | if completed < self.complete_at: | ||
| 50 | return False | 213 | return False |
| 51 | if completed < reqs.complete_at: | ||
| 52 | return False | ||
| 53 | 214 | ||
| 54 | return True | 215 | return True |
| 55 | |||
| 56 | def make_location_lambda(reqs: AccessRequirements, world: "Lingo2World", | ||
| 57 | regions: dict[str, Region] | None) -> Callable[[CollectionState], bool]: | ||
| 58 | # Replace required rooms with regions for the top level requirement, which saves looking up the regions during rule | ||
| 59 | # checking. | ||
| 60 | if regions is not None: | ||
| 61 | required_regions = [regions[room_name] for room_name in reqs.rooms] | ||
| 62 | else: | ||
| 63 | required_regions = [world.multiworld.get_region(room_name, world.player) for room_name in reqs.rooms] | ||
| 64 | new_reqs = reqs.copy() | ||
| 65 | new_reqs.rooms.clear() | ||
| 66 | return lambda state: lingo2_can_satisfy_requirements(state, new_reqs, required_regions, world) | ||
