diff options
| author | Star Rauchenberger <fefferburbia@gmail.com> | 2025-12-13 07:10:05 -0500 |
|---|---|---|
| committer | Star Rauchenberger <fefferburbia@gmail.com> | 2025-12-13 07:10:05 -0500 |
| commit | c5a564bfc9bcf422d04c9016f56d65260b007c67 (patch) | |
| tree | 9360c9d81c59418510c9beb1048a8ec93c10bfa2 | |
| parent | b5cff95338c2ce6c35d0cc5a01ac51476885e4de (diff) | |
| download | lingo2-archipelago-c5a564bfc9bcf422d04c9016f56d65260b007c67.tar.gz lingo2-archipelago-c5a564bfc9bcf422d04c9016f56d65260b007c67.tar.bz2 lingo2-archipelago-c5a564bfc9bcf422d04c9016f56d65260b007c67.zip | |
Refactor AccessRequirements loc-refactor
| -rw-r--r-- | apworld/locations.py | 78 | ||||
| -rw-r--r-- | apworld/player_logic.py | 169 | ||||
| -rw-r--r-- | apworld/regions.py | 42 | ||||
| -rw-r--r-- | apworld/rules.py | 243 |
4 files changed, 294 insertions, 238 deletions
| diff --git a/apworld/locations.py b/apworld/locations.py index 3d619dc..fc7ef26 100644 --- a/apworld/locations.py +++ b/apworld/locations.py | |||
| @@ -1,4 +1,20 @@ | |||
| 1 | from BaseClasses import Location | 1 | from typing import TYPE_CHECKING |
| 2 | |||
| 3 | from BaseClasses import Location, Region, CollectionState, Entrance | ||
| 4 | from .rules import AccessRequirements | ||
| 5 | |||
| 6 | if TYPE_CHECKING: | ||
| 7 | from . import Lingo2World | ||
| 8 | |||
| 9 | |||
| 10 | def get_required_regions(reqs: AccessRequirements, world: "Lingo2World", | ||
| 11 | regions: dict[str, Region] | None) -> list[Region]: | ||
| 12 | # Replace required rooms with regions for the top level requirement, which saves looking up the regions during rule | ||
| 13 | # checking. | ||
| 14 | if regions is not None: | ||
| 15 | return [regions[room_name] for room_name in reqs.rooms] | ||
| 16 | else: | ||
| 17 | return [world.multiworld.get_region(room_name, world.player) for room_name in reqs.rooms] | ||
| 2 | 18 | ||
| 3 | 19 | ||
| 4 | class Lingo2Location(Location): | 20 | class Lingo2Location(Location): |
| @@ -6,3 +22,63 @@ class Lingo2Location(Location): | |||
| 6 | 22 | ||
| 7 | port_id: int | 23 | port_id: int |
| 8 | goal: bool | 24 | goal: bool |
| 25 | reqs: AccessRequirements | None | ||
| 26 | world: "Lingo2World" | ||
| 27 | required_regions: list[Region] | ||
| 28 | |||
| 29 | @classmethod | ||
| 30 | def non_event_location(cls, world: "Lingo2World", code: int, region: Region): | ||
| 31 | result = cls(world.player, world.static_logic.location_id_to_name[code], code, region) | ||
| 32 | result.reqs = None | ||
| 33 | result.world = world | ||
| 34 | result.required_regions = [] | ||
| 35 | |||
| 36 | return result | ||
| 37 | |||
| 38 | @classmethod | ||
| 39 | def event_location(cls, world: "Lingo2World", name: str, region: Region): | ||
| 40 | result = cls(world.player, name, None, region) | ||
| 41 | result.reqs = None | ||
| 42 | result.world = world | ||
| 43 | result.required_regions = [] | ||
| 44 | |||
| 45 | return result | ||
| 46 | |||
| 47 | def set_access_rule(self, reqs: AccessRequirements, regions: dict[str, Region] | None): | ||
| 48 | self.reqs = reqs | ||
| 49 | self.required_regions = get_required_regions(reqs, self.world, regions) | ||
| 50 | |||
| 51 | def access_rule(self, state: CollectionState) -> bool: | ||
| 52 | if self.reqs is not None and not self.reqs.check_access(state, self.world): | ||
| 53 | return False | ||
| 54 | |||
| 55 | if not all(state.can_reach(region) for region in self.required_regions): | ||
| 56 | return False | ||
| 57 | |||
| 58 | return True | ||
| 59 | |||
| 60 | |||
| 61 | class Lingo2Entrance(Entrance): | ||
| 62 | reqs: AccessRequirements | None | ||
| 63 | world: "Lingo2World" | ||
| 64 | required_regions: list[Region] | ||
| 65 | |||
| 66 | def __init__(self, world: "Lingo2World", description: str, region: Region): | ||
| 67 | super().__init__(world.player, description, region) | ||
| 68 | |||
| 69 | self.reqs = None | ||
| 70 | self.world = world | ||
| 71 | self.required_regions = [] | ||
| 72 | |||
| 73 | def set_access_rule(self, reqs: AccessRequirements, regions: dict[str, Region] | None): | ||
| 74 | self.reqs = reqs | ||
| 75 | self.required_regions = get_required_regions(reqs, self.world, regions) | ||
| 76 | |||
| 77 | def access_rule(self, state: CollectionState) -> bool: | ||
| 78 | if self.reqs is not None and not self.reqs.check_access(state, self.world): | ||
| 79 | return False | ||
| 80 | |||
| 81 | if not all(state.can_reach(region) for region in self.required_regions): | ||
| 82 | return False | ||
| 83 | |||
| 84 | return True | ||
| diff --git a/apworld/player_logic.py b/apworld/player_logic.py index 3ee8f38..892dac5 100644 --- a/apworld/player_logic.py +++ b/apworld/player_logic.py | |||
| @@ -5,6 +5,7 @@ from .items import SYMBOL_ITEMS | |||
| 5 | from typing import TYPE_CHECKING, NamedTuple | 5 | from typing import TYPE_CHECKING, NamedTuple |
| 6 | 6 | ||
| 7 | from .options import ShuffleLetters, CyanDoorBehavior | 7 | from .options import ShuffleLetters, CyanDoorBehavior |
| 8 | from .rules import AccessRequirements | ||
| 8 | 9 | ||
| 9 | if TYPE_CHECKING: | 10 | if TYPE_CHECKING: |
| 10 | from . import Lingo2World | 11 | from . import Lingo2World |
| @@ -20,174 +21,6 @@ def calculate_letter_histogram(solution: str) -> dict[str, int]: | |||
| 20 | return histogram | 21 | return histogram |
| 21 | 22 | ||
| 22 | 23 | ||
| 23 | class AccessRequirements: | ||
| 24 | items: set[str] | ||
| 25 | progressives: dict[str, int] | ||
| 26 | rooms: set[str] | ||
| 27 | letters: dict[str, int] | ||
| 28 | cyans: bool | ||
| 29 | |||
| 30 | # This is an AND of ORs. | ||
| 31 | or_logic: list[list["AccessRequirements"]] | ||
| 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 | |||
| 38 | def __init__(self): | ||
| 39 | self.items = set() | ||
| 40 | self.progressives = dict() | ||
| 41 | self.rooms = set() | ||
| 42 | self.letters = dict() | ||
| 43 | self.cyans = False | ||
| 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 | ||
| 59 | |||
| 60 | def merge(self, other: "AccessRequirements"): | ||
| 61 | for item in other.items: | ||
| 62 | self.items.add(item) | ||
| 63 | |||
| 64 | for item, amount in other.progressives.items(): | ||
| 65 | self.progressives[item] = max(amount, self.progressives.get(item, 0)) | ||
| 66 | |||
| 67 | for room in other.rooms: | ||
| 68 | self.rooms.add(room) | ||
| 69 | |||
| 70 | for letter, level in other.letters.items(): | ||
| 71 | self.letters[letter] = max(self.letters.get(letter, 0), level) | ||
| 72 | |||
| 73 | self.cyans = self.cyans or other.cyans | ||
| 74 | |||
| 75 | for disjunction in other.or_logic: | ||
| 76 | self.or_logic.append([sub_req.copy() for sub_req in disjunction]) | ||
| 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 = [sub_req.copy() for sub_req in 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 = [sub_req.copy() for sub_req in other.possibilities] | ||
| 96 | self.or_logic.append([right_req]) | ||
| 97 | else: | ||
| 98 | self.complete_at = other.complete_at | ||
| 99 | self.possibilities = [sub_req.copy() for sub_req in other.possibilities] | ||
| 100 | |||
| 101 | def is_empty(self) -> bool: | ||
| 102 | return (len(self.items) == 0 and len(self.progressives) == 0 and len(self.rooms) == 0 and len(self.letters) == 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) | ||
| 169 | |||
| 170 | def __repr__(self): | ||
| 171 | parts = [] | ||
| 172 | if len(self.items) > 0: | ||
| 173 | parts.append(f"items={self.items}") | ||
| 174 | if len(self.progressives) > 0: | ||
| 175 | parts.append(f"progressives={self.progressives}") | ||
| 176 | if len(self.rooms) > 0: | ||
| 177 | parts.append(f"rooms={self.rooms}") | ||
| 178 | if len(self.letters) > 0: | ||
| 179 | parts.append(f"letters={self.letters}") | ||
| 180 | if self.cyans: | ||
| 181 | parts.append(f"cyans=True") | ||
| 182 | if len(self.or_logic) > 0: | ||
| 183 | parts.append(f"or_logic={self.or_logic}") | ||
| 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) + ")" | ||
| 189 | |||
| 190 | |||
| 191 | class PlayerLocation(NamedTuple): | 24 | class PlayerLocation(NamedTuple): |
| 192 | code: int | None | 25 | code: int | None |
| 193 | reqs: AccessRequirements | 26 | reqs: AccessRequirements |
| diff --git a/apworld/regions.py b/apworld/regions.py index 1118603..d5bdd46 100644 --- a/apworld/regions.py +++ b/apworld/regions.py | |||
| @@ -1,12 +1,10 @@ | |||
| 1 | from typing import TYPE_CHECKING | 1 | from typing import TYPE_CHECKING |
| 2 | 2 | ||
| 3 | import BaseClasses | 3 | from BaseClasses import Region, ItemClassification, Entrance, EntranceType |
| 4 | from BaseClasses import Region, ItemClassification, Entrance | ||
| 5 | from entrance_rando import randomize_entrances | 4 | from entrance_rando import randomize_entrances |
| 6 | from .items import Lingo2Item | 5 | from .items import Lingo2Item |
| 7 | from .locations import Lingo2Location | 6 | from .locations import Lingo2Location, Lingo2Entrance |
| 8 | from .player_logic import AccessRequirements | 7 | from .rules import AccessRequirements |
| 9 | from .rules import make_location_lambda | ||
| 10 | 8 | ||
| 11 | if TYPE_CHECKING: | 9 | if TYPE_CHECKING: |
| 12 | from . import Lingo2World | 10 | from . import Lingo2World |
| @@ -21,13 +19,13 @@ def create_locations(room, new_region: Region, world: "Lingo2World", regions: di | |||
| 21 | reqs = location.reqs.copy() | 19 | reqs = location.reqs.copy() |
| 22 | reqs.remove_room(new_region.name) | 20 | reqs.remove_room(new_region.name) |
| 23 | 21 | ||
| 24 | new_location = Lingo2Location(world.player, world.static_logic.location_id_to_name[location.code], | 22 | new_location = Lingo2Location.non_event_location(world, location.code, new_region) |
| 25 | location.code, new_region) | 23 | new_location.set_access_rule(reqs, regions) |
| 26 | new_location.access_rule = make_location_lambda(reqs, world, regions) | ||
| 27 | new_region.locations.append(new_location) | 24 | new_region.locations.append(new_location) |
| 28 | 25 | ||
| 29 | for event_name, item_name in world.player_logic.event_loc_item_by_room.get(room.id, {}).items(): | 26 | 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) | 27 | new_location = Lingo2Location.event_location(world, event_name, new_region) |
| 28 | |||
| 31 | if world.for_tracker and item_name == "Victory": | 29 | if world.for_tracker and item_name == "Victory": |
| 32 | new_location.goal = True | 30 | new_location.goal = True |
| 33 | 31 | ||
| @@ -41,12 +39,11 @@ def create_locations(room, new_region: Region, world: "Lingo2World", regions: di | |||
| 41 | if port.no_shuffle: | 39 | if port.no_shuffle: |
| 42 | continue | 40 | continue |
| 43 | 41 | ||
| 44 | new_location = Lingo2Location(world.player, f"Worldport {port.id} Entered", None, new_region) | 42 | new_location = Lingo2Location.event_location(world, f"Worldport {port.id} Entered", new_region) |
| 45 | new_location.port_id = port.id | 43 | new_location.port_id = port.id |
| 46 | 44 | ||
| 47 | if port.HasField("required_door"): | 45 | if port.HasField("required_door"): |
| 48 | new_location.access_rule = \ | 46 | new_location.set_access_rule(world.player_logic.get_door_open_reqs(port.required_door), regions) |
| 49 | make_location_lambda(world.player_logic.get_door_open_reqs(port.required_door), world, regions) | ||
| 50 | 47 | ||
| 51 | new_region.locations.append(new_location) | 48 | new_region.locations.append(new_location) |
| 52 | 49 | ||
| @@ -141,8 +138,8 @@ def create_regions(world: "Lingo2World"): | |||
| 141 | # what regions are dead ends. | 138 | # what regions are dead ends. |
| 142 | continue | 139 | continue |
| 143 | 140 | ||
| 144 | connection = Entrance(world.player, connection_name, regions[from_region]) | 141 | connection = Lingo2Entrance(world, connection_name, regions[from_region]) |
| 145 | connection.access_rule = make_location_lambda(reqs, world, regions) | 142 | connection.set_access_rule(reqs, regions) |
| 146 | 143 | ||
| 147 | regions[from_region].exits.append(connection) | 144 | regions[from_region].exits.append(connection) |
| 148 | connection.connect(regions[to_region]) | 145 | connection.connect(regions[to_region]) |
| @@ -182,13 +179,13 @@ def shuffle_entrances(world: "Lingo2World"): | |||
| 182 | from_region = world.multiworld.get_region(from_region_name, world.player) | 179 | from_region = world.multiworld.get_region(from_region_name, world.player) |
| 183 | to_region = world.multiworld.get_region(to_region_name, world.player) | 180 | to_region = world.multiworld.get_region(to_region_name, world.player) |
| 184 | 181 | ||
| 185 | connection = Entrance(world.player, f"{from_region_name} - {chosen_port.display_name}", from_region) | 182 | connection = Lingo2Entrance(world, f"{from_region_name} - {chosen_port.display_name}", from_region) |
| 186 | from_region.exits.append(connection) | 183 | from_region.exits.append(connection) |
| 187 | connection.connect(to_region) | 184 | connection.connect(to_region) |
| 188 | 185 | ||
| 189 | if chosen_port.HasField("required_door"): | 186 | if chosen_port.HasField("required_door"): |
| 190 | door_reqs = world.player_logic.get_door_open_reqs(chosen_port.required_door) | 187 | door_reqs = world.player_logic.get_door_open_reqs(chosen_port.required_door) |
| 191 | connection.access_rule = make_location_lambda(door_reqs, world, None) | 188 | connection.set_access_rule(door_reqs, None) |
| 192 | 189 | ||
| 193 | for region in door_reqs.get_referenced_rooms(): | 190 | for region in door_reqs.get_referenced_rooms(): |
| 194 | world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player), | 191 | world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player), |
| @@ -202,14 +199,15 @@ def shuffle_entrances(world: "Lingo2World"): | |||
| 202 | port_id_by_name[connection_name] = port.id | 199 | port_id_by_name[connection_name] = port.id |
| 203 | 200 | ||
| 204 | entrance = port_region.create_er_target(connection_name) | 201 | entrance = port_region.create_er_target(connection_name) |
| 205 | entrance.randomization_type = BaseClasses.EntranceType.TWO_WAY | 202 | entrance.randomization_type = EntranceType.TWO_WAY |
| 206 | 203 | ||
| 207 | er_exit = port_region.create_exit(connection_name) | 204 | er_exit = Lingo2Entrance(world, connection_name, port_region) |
| 208 | er_exit.randomization_type = BaseClasses.EntranceType.TWO_WAY | 205 | port_region.exits.append(er_exit) |
| 206 | er_exit.randomization_type = EntranceType.TWO_WAY | ||
| 209 | 207 | ||
| 210 | if port.HasField("required_door"): | 208 | if port.HasField("required_door"): |
| 211 | door_reqs = world.player_logic.get_door_open_reqs(port.required_door) | 209 | door_reqs = world.player_logic.get_door_open_reqs(port.required_door) |
| 212 | er_exit.access_rule = make_location_lambda(door_reqs, world, None) | 210 | er_exit.set_access_rule(door_reqs, None) |
| 213 | 211 | ||
| 214 | for region in door_reqs.get_referenced_rooms(): | 212 | for region in door_reqs.get_referenced_rooms(): |
| 215 | world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player), | 213 | world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player), |
| @@ -236,7 +234,7 @@ def connect_ports_from_ut(port_pairings: dict[int, int], world: "Lingo2World"): | |||
| 236 | from_region = world.multiworld.get_region(from_region_name, world.player) | 234 | from_region = world.multiworld.get_region(from_region_name, world.player) |
| 237 | to_region = world.multiworld.get_region(to_region_name, world.player) | 235 | to_region = world.multiworld.get_region(to_region_name, world.player) |
| 238 | 236 | ||
| 239 | connection = Entrance(world.player, f"{from_region_name} - {from_port.display_name}", from_region) | 237 | connection = Lingo2Entrance(world, f"{from_region_name} - {from_port.display_name}", from_region) |
| 240 | 238 | ||
| 241 | reqs = AccessRequirements() | 239 | reqs = AccessRequirements() |
| 242 | if from_port.HasField("required_door"): | 240 | if from_port.HasField("required_door"): |
| @@ -246,7 +244,7 @@ def connect_ports_from_ut(port_pairings: dict[int, int], world: "Lingo2World"): | |||
| 246 | reqs.items.add(f"Worldport {fpid} Entered") | 244 | reqs.items.add(f"Worldport {fpid} Entered") |
| 247 | 245 | ||
| 248 | if not reqs.is_empty(): | 246 | if not reqs.is_empty(): |
| 249 | connection.access_rule = make_location_lambda(reqs, world, None) | 247 | connection.set_access_rule(reqs, None) |
| 250 | 248 | ||
| 251 | for region in reqs.get_referenced_rooms(): | 249 | for region in reqs.get_referenced_rooms(): |
| 252 | world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player), | 250 | world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player), |
| 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) | ||
