diff options
Diffstat (limited to 'apworld/player_logic.py')
| -rw-r--r-- | apworld/player_logic.py | 155 |
1 files changed, 134 insertions, 21 deletions
| diff --git a/apworld/player_logic.py b/apworld/player_logic.py index c94b809..8e2a523 100644 --- a/apworld/player_logic.py +++ b/apworld/player_logic.py | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | from enum import IntEnum, auto | 1 | from enum import IntEnum, auto |
| 2 | 2 | ||
| 3 | from .generated import data_pb2 as data_pb2 | 3 | from .generated import data_pb2 as data_pb2 |
| 4 | from .items import SYMBOL_ITEMS | ||
| 4 | from typing import TYPE_CHECKING, NamedTuple | 5 | from typing import TYPE_CHECKING, NamedTuple |
| 5 | 6 | ||
| 6 | from .options import VictoryCondition, ShuffleLetters, CyanDoorBehavior | 7 | from .options import VictoryCondition, ShuffleLetters, CyanDoorBehavior |
| @@ -23,21 +24,38 @@ class AccessRequirements: | |||
| 23 | items: set[str] | 24 | items: set[str] |
| 24 | progressives: dict[str, int] | 25 | progressives: dict[str, int] |
| 25 | rooms: set[str] | 26 | rooms: set[str] |
| 26 | symbols: set[str] | ||
| 27 | letters: dict[str, int] | 27 | letters: dict[str, int] |
| 28 | cyans: bool | 28 | cyans: bool |
| 29 | 29 | ||
| 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() |
| 36 | self.rooms = set() | 41 | self.rooms = set() |
| 37 | self.symbols = set() | ||
| 38 | self.letters = dict() | 42 | self.letters = dict() |
| 39 | self.cyans = False | 43 | self.cyans = False |
| 40 | 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 | ||
| 41 | 59 | ||
| 42 | def merge(self, other: "AccessRequirements"): | 60 | def merge(self, other: "AccessRequirements"): |
| 43 | for item in other.items: | 61 | for item in other.items: |
| @@ -49,9 +67,6 @@ class AccessRequirements: | |||
| 49 | for room in other.rooms: | 67 | for room in other.rooms: |
| 50 | self.rooms.add(room) | 68 | self.rooms.add(room) |
| 51 | 69 | ||
| 52 | for symbol in other.symbols: | ||
| 53 | self.symbols.add(symbol) | ||
| 54 | |||
| 55 | for letter, level in other.letters.items(): | 70 | for letter, level in other.letters.items(): |
| 56 | self.letters[letter] = max(self.letters.get(letter, 0), level) | 71 | self.letters[letter] = max(self.letters.get(letter, 0), level) |
| 57 | 72 | ||
| @@ -60,6 +75,70 @@ class AccessRequirements: | |||
| 60 | for disjunction in other.or_logic: | 75 | for disjunction in other.or_logic: |
| 61 | self.or_logic.append(disjunction) | 76 | self.or_logic.append(disjunction) |
| 62 | 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 | |||
| 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 | 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() | ||
| 141 | |||
| 63 | def __repr__(self): | 142 | def __repr__(self): |
| 64 | parts = [] | 143 | parts = [] |
| 65 | if len(self.items) > 0: | 144 | if len(self.items) > 0: |
| @@ -68,14 +147,16 @@ class AccessRequirements: | |||
| 68 | parts.append(f"progressives={self.progressives}") | 147 | parts.append(f"progressives={self.progressives}") |
| 69 | if len(self.rooms) > 0: | 148 | if len(self.rooms) > 0: |
| 70 | parts.append(f"rooms={self.rooms}") | 149 | parts.append(f"rooms={self.rooms}") |
| 71 | if len(self.symbols) > 0: | ||
| 72 | parts.append(f"symbols={self.symbols}") | ||
| 73 | if len(self.letters) > 0: | 150 | if len(self.letters) > 0: |
| 74 | parts.append(f"letters={self.letters}") | 151 | parts.append(f"letters={self.letters}") |
| 75 | if self.cyans: | 152 | if self.cyans: |
| 76 | parts.append(f"cyans=True") | 153 | parts.append(f"cyans=True") |
| 77 | if len(self.or_logic) > 0: | 154 | if len(self.or_logic) > 0: |
| 78 | parts.append(f"or_logic={self.or_logic}") | 155 | parts.append(f"or_logic={self.or_logic}") |
| 156 | if self.complete_at is not None: | ||
| 157 | parts.append(f"complete_at={self.complete_at}") | ||
| 158 | if len(self.possibilities) > 0: | ||
| 159 | parts.append(f"possibilities={self.possibilities}") | ||
| 79 | return f"AccessRequirements({", ".join(parts)})" | 160 | return f"AccessRequirements({", ".join(parts)})" |
| 80 | 161 | ||
| 81 | 162 | ||
| @@ -158,6 +239,9 @@ class Lingo2PlayerLogic: | |||
| 158 | not self.world.options.shuffle_control_center_colors): | 239 | not self.world.options.shuffle_control_center_colors): |
| 159 | continue | 240 | continue |
| 160 | 241 | ||
| 242 | if door.type == data_pb2.DoorType.GALLERY_PAINTING and not self.world.options.shuffle_gallery_paintings: | ||
| 243 | continue | ||
| 244 | |||
| 161 | door_item_name = self.world.static_logic.get_door_item_name(door) | 245 | door_item_name = self.world.static_logic.get_door_item_name(door) |
| 162 | self.item_by_door[door.id] = (door_item_name, 1) | 246 | self.item_by_door[door.id] = (door_item_name, 1) |
| 163 | self.real_items.append(door_item_name) | 247 | self.real_items.append(door_item_name) |
| @@ -231,6 +315,10 @@ class Lingo2PlayerLogic: | |||
| 231 | self.locations_by_room.setdefault(keyholder.room_id, []).append(PlayerLocation(keyholder.ap_id, | 315 | self.locations_by_room.setdefault(keyholder.room_id, []).append(PlayerLocation(keyholder.ap_id, |
| 232 | reqs)) | 316 | reqs)) |
| 233 | 317 | ||
| 318 | if self.world.options.shuffle_symbols: | ||
| 319 | for symbol_name in SYMBOL_ITEMS.values(): | ||
| 320 | self.real_items.append(symbol_name) | ||
| 321 | |||
| 234 | def get_panel_reqs(self, panel_id: int, answer: str | None) -> AccessRequirements: | 322 | def get_panel_reqs(self, panel_id: int, answer: str | None) -> AccessRequirements: |
| 235 | if answer is None: | 323 | if answer is None: |
| 236 | if panel_id not in self.panel_reqs: | 324 | if panel_id not in self.panel_reqs: |
| @@ -253,25 +341,35 @@ class Lingo2PlayerLogic: | |||
| 253 | self.add_solution_reqs(reqs, answer) | 341 | self.add_solution_reqs(reqs, answer) |
| 254 | elif len(panel.proxies) > 0: | 342 | elif len(panel.proxies) > 0: |
| 255 | possibilities = [] | 343 | possibilities = [] |
| 344 | already_filled = False | ||
| 256 | 345 | ||
| 257 | for proxy in panel.proxies: | 346 | for proxy in panel.proxies: |
| 258 | proxy_reqs = AccessRequirements() | 347 | proxy_reqs = AccessRequirements() |
| 259 | self.add_solution_reqs(proxy_reqs, proxy.answer) | 348 | self.add_solution_reqs(proxy_reqs, proxy.answer) |
| 260 | 349 | ||
| 261 | possibilities.append(proxy_reqs) | 350 | if not proxy_reqs.is_empty(): |
| 351 | possibilities.append(proxy_reqs) | ||
| 352 | else: | ||
| 353 | already_filled = True | ||
| 354 | break | ||
| 262 | 355 | ||
| 263 | if not any(proxy.answer == panel.answer for proxy in panel.proxies): | 356 | if not already_filled and not any(proxy.answer == panel.answer for proxy in panel.proxies): |
| 264 | proxy_reqs = AccessRequirements() | 357 | proxy_reqs = AccessRequirements() |
| 265 | self.add_solution_reqs(proxy_reqs, panel.answer) | 358 | self.add_solution_reqs(proxy_reqs, panel.answer) |
| 266 | 359 | ||
| 267 | possibilities.append(proxy_reqs) | 360 | if not proxy_reqs.is_empty(): |
| 361 | possibilities.append(proxy_reqs) | ||
| 362 | else: | ||
| 363 | already_filled = True | ||
| 268 | 364 | ||
| 269 | reqs.or_logic.append(possibilities) | 365 | if not already_filled: |
| 366 | reqs.or_logic.append(possibilities) | ||
| 270 | else: | 367 | else: |
| 271 | self.add_solution_reqs(reqs, panel.answer) | 368 | self.add_solution_reqs(reqs, panel.answer) |
| 272 | 369 | ||
| 273 | for symbol in panel.symbols: | 370 | if self.world.options.shuffle_symbols: |
| 274 | reqs.symbols.add(symbol) | 371 | for symbol in panel.symbols: |
| 372 | reqs.items.add(SYMBOL_ITEMS.get(symbol)) | ||
| 275 | 373 | ||
| 276 | if panel.HasField("required_door"): | 374 | if panel.HasField("required_door"): |
| 277 | door_reqs = self.get_door_open_reqs(panel.required_door) | 375 | door_reqs = self.get_door_open_reqs(panel.required_door) |
| @@ -294,18 +392,26 @@ class Lingo2PlayerLogic: | |||
| 294 | door = self.world.static_logic.objects.doors[door_id] | 392 | door = self.world.static_logic.objects.doors[door_id] |
| 295 | reqs = AccessRequirements() | 393 | reqs = AccessRequirements() |
| 296 | 394 | ||
| 297 | # TODO: lavender_cubes, endings | ||
| 298 | if not door.HasField("complete_at") or door.complete_at == 0: | 395 | if not door.HasField("complete_at") or door.complete_at == 0: |
| 299 | for proxy in door.panels: | 396 | for proxy in door.panels: |
| 300 | panel_reqs = self.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None) | 397 | panel_reqs = self.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None) |
| 301 | reqs.merge(panel_reqs) | 398 | reqs.merge(panel_reqs) |
| 302 | elif door.complete_at == 1: | 399 | elif door.complete_at == 1: |
| 303 | reqs.or_logic.append([self.get_panel_reqs(proxy.panel, | 400 | disjunction = [] |
| 304 | proxy.answer if proxy.HasField("answer") else None) | 401 | for proxy in door.panels: |
| 305 | for proxy in door.panels]) | 402 | proxy_reqs = self.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None) |
| 403 | if proxy_reqs.is_empty(): | ||
| 404 | disjunction.clear() | ||
| 405 | break | ||
| 406 | else: | ||
| 407 | disjunction.append(proxy_reqs) | ||
| 408 | if len(disjunction) > 0: | ||
| 409 | reqs.or_logic.append(disjunction) | ||
| 306 | else: | 410 | else: |
| 307 | # TODO: Handle complete_at > 1 | 411 | reqs.complete_at = door.complete_at |
| 308 | pass | 412 | for proxy in door.panels: |
| 413 | panel_reqs = self.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None) | ||
| 414 | reqs.possibilities.append(panel_reqs) | ||
| 309 | 415 | ||
| 310 | if door.HasField("control_center_color"): | 416 | if door.HasField("control_center_color"): |
| 311 | # TODO: Logic for ensuring two CC states aren't needed at once. | 417 | # TODO: Logic for ensuring two CC states aren't needed at once. |
| @@ -316,7 +422,8 @@ class Lingo2PlayerLogic: | |||
| 316 | if self.world.options.cyan_door_behavior == CyanDoorBehavior.option_collect_h2: | 422 | if self.world.options.cyan_door_behavior == CyanDoorBehavior.option_collect_h2: |
| 317 | reqs.rooms.add("The Repetitive - Main Room") | 423 | reqs.rooms.add("The Repetitive - Main Room") |
| 318 | elif self.world.options.cyan_door_behavior == CyanDoorBehavior.option_any_double_letter: | 424 | elif self.world.options.cyan_door_behavior == CyanDoorBehavior.option_any_double_letter: |
| 319 | reqs.cyans = True | 425 | if self.world.options.shuffle_letters != ShuffleLetters.option_unlocked: |
| 426 | reqs.cyans = True | ||
| 320 | elif self.world.options.cyan_door_behavior == CyanDoorBehavior.option_item: | 427 | elif self.world.options.cyan_door_behavior == CyanDoorBehavior.option_item: |
| 321 | # There shouldn't be any locations that are cyan doors. | 428 | # There shouldn't be any locations that are cyan doors. |
| 322 | pass | 429 | pass |
| @@ -335,12 +442,18 @@ class Lingo2PlayerLogic: | |||
| 335 | 442 | ||
| 336 | for ending_id in door.endings: | 443 | for ending_id in door.endings: |
| 337 | ending = self.world.static_logic.objects.endings[ending_id] | 444 | ending = self.world.static_logic.objects.endings[ending_id] |
| 338 | reqs.items.add(f"{ending.name.capitalize()} Ending (Achieved)") | 445 | |
| 446 | if self.world.options.victory_condition.current_key.removesuffix("_ending").upper() == ending.name: | ||
| 447 | reqs.items.add("Victory") | ||
| 448 | else: | ||
| 449 | reqs.items.add(f"{ending.name.capitalize()} Ending (Achieved)") | ||
| 339 | 450 | ||
| 340 | for sub_door_id in door.doors: | 451 | for sub_door_id in door.doors: |
| 341 | sub_reqs = self.get_door_open_reqs(sub_door_id) | 452 | sub_reqs = self.get_door_open_reqs(sub_door_id) |
| 342 | reqs.merge(sub_reqs) | 453 | reqs.merge(sub_reqs) |
| 343 | 454 | ||
| 455 | reqs.simplify() | ||
| 456 | |||
| 344 | return reqs | 457 | return reqs |
| 345 | 458 | ||
| 346 | # 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 |
