diff options
Diffstat (limited to 'apworld/player_logic.py')
| -rw-r--r-- | apworld/player_logic.py | 524 |
1 files changed, 521 insertions, 3 deletions
| diff --git a/apworld/player_logic.py b/apworld/player_logic.py index 675c6ae..4aa481d 100644 --- a/apworld/player_logic.py +++ b/apworld/player_logic.py | |||
| @@ -1,20 +1,538 @@ | |||
| 1 | from .generated import common_pb2 as common_pb2 | 1 | from enum import IntEnum, auto |
| 2 | |||
| 3 | from .generated import data_pb2 as data_pb2 | ||
| 4 | from .items import SYMBOL_ITEMS | ||
| 2 | from typing import TYPE_CHECKING, NamedTuple | 5 | from typing import TYPE_CHECKING, NamedTuple |
| 3 | 6 | ||
| 7 | from .options import VictoryCondition, ShuffleLetters, CyanDoorBehavior | ||
| 8 | |||
| 4 | if TYPE_CHECKING: | 9 | if TYPE_CHECKING: |
| 5 | from . import Lingo2World | 10 | from . import Lingo2World |
| 6 | 11 | ||
| 7 | 12 | ||
| 13 | def calculate_letter_histogram(solution: str) -> dict[str, int]: | ||
| 14 | histogram = dict() | ||
| 15 | for l in solution: | ||
| 16 | if l.isalpha(): | ||
| 17 | real_l = l.upper() | ||
| 18 | histogram[real_l] = min(histogram.get(real_l, 0) + 1, 2) | ||
| 19 | |||
| 20 | return histogram | ||
| 21 | |||
| 22 | |||
| 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(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 = 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 | 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 | |||
| 8 | class PlayerLocation(NamedTuple): | 191 | class PlayerLocation(NamedTuple): |
| 9 | code: int | None | 192 | code: int | None |
| 193 | reqs: AccessRequirements | ||
| 194 | |||
| 195 | |||
| 196 | class LetterBehavior(IntEnum): | ||
| 197 | VANILLA = auto() | ||
| 198 | ITEM = auto() | ||
| 199 | UNLOCKED = auto() | ||
| 10 | 200 | ||
| 11 | 201 | ||
| 12 | class Lingo2PlayerLogic: | 202 | class Lingo2PlayerLogic: |
| 203 | world: "Lingo2World" | ||
| 204 | |||
| 13 | locations_by_room: dict[int, list[PlayerLocation]] | 205 | locations_by_room: dict[int, list[PlayerLocation]] |
| 206 | event_loc_item_by_room: dict[int, dict[str, str]] | ||
| 207 | |||
| 208 | item_by_door: dict[int, tuple[str, int]] | ||
| 209 | |||
| 210 | panel_reqs: dict[int, AccessRequirements] | ||
| 211 | proxy_reqs: dict[int, dict[str, AccessRequirements]] | ||
| 212 | door_reqs: dict[int, AccessRequirements] | ||
| 213 | |||
| 214 | real_items: list[str] | ||
| 215 | |||
| 216 | double_letter_amount: dict[str, int] | ||
| 14 | 217 | ||
| 15 | def __init__(self, world: "Lingo2World"): | 218 | def __init__(self, world: "Lingo2World"): |
| 219 | self.world = world | ||
| 16 | self.locations_by_room = {} | 220 | self.locations_by_room = {} |
| 221 | self.event_loc_item_by_room = {} | ||
| 222 | self.item_by_door = {} | ||
| 223 | self.panel_reqs = dict() | ||
| 224 | self.proxy_reqs = dict() | ||
| 225 | self.door_reqs = dict() | ||
| 226 | self.real_items = list() | ||
| 227 | self.double_letter_amount = dict() | ||
| 228 | |||
| 229 | if self.world.options.shuffle_doors: | ||
| 230 | for progressive in world.static_logic.objects.progressives: | ||
| 231 | for i in range(0, len(progressive.doors)): | ||
| 232 | self.item_by_door[progressive.doors[i]] = (progressive.name, i + 1) | ||
| 233 | self.real_items.append(progressive.name) | ||
| 234 | |||
| 235 | for door_group in world.static_logic.objects.door_groups: | ||
| 236 | if door_group.type == data_pb2.DoorGroupType.CONNECTOR: | ||
| 237 | if not self.world.options.shuffle_doors: | ||
| 238 | continue | ||
| 239 | elif door_group.type == data_pb2.DoorGroupType.COLOR_CONNECTOR: | ||
| 240 | if not self.world.options.shuffle_control_center_colors: | ||
| 241 | continue | ||
| 242 | elif door_group.type == data_pb2.DoorGroupType.SHUFFLE_GROUP: | ||
| 243 | if not self.world.options.shuffle_doors: | ||
| 244 | continue | ||
| 245 | else: | ||
| 246 | continue | ||
| 247 | |||
| 248 | for door in door_group.doors: | ||
| 249 | self.item_by_door[door] = (door_group.name, 1) | ||
| 17 | 250 | ||
| 251 | self.real_items.append(door_group.name) | ||
| 252 | |||
| 253 | # We iterate through the doors in two parts because it is essential that we determine which doors are shuffled | ||
| 254 | # before we calculate any access requirements. | ||
| 18 | for door in world.static_logic.objects.doors: | 255 | for door in world.static_logic.objects.doors: |
| 19 | if door.type == common_pb2.DoorType.STANDARD: | 256 | if door.type in [data_pb2.DoorType.EVENT, data_pb2.DoorType.LOCATION_ONLY, data_pb2.DoorType.GRAVESTONE]: |
| 20 | self.locations_by_room.setdefault(door.room_id, []).append(PlayerLocation(door.ap_id)) | 257 | continue |
| 258 | |||
| 259 | if door.id in self.item_by_door: | ||
| 260 | continue | ||
| 261 | |||
| 262 | if (door.type in [data_pb2.DoorType.STANDARD, data_pb2.DoorType.ITEM_ONLY] and | ||
| 263 | not self.world.options.shuffle_doors): | ||
| 264 | continue | ||
| 265 | |||
| 266 | if (door.type == data_pb2.DoorType.CONTROL_CENTER_COLOR and | ||
| 267 | not self.world.options.shuffle_control_center_colors): | ||
| 268 | continue | ||
| 269 | |||
| 270 | if door.type == data_pb2.DoorType.GALLERY_PAINTING and not self.world.options.shuffle_gallery_paintings: | ||
| 271 | continue | ||
| 272 | |||
| 273 | door_item_name = self.world.static_logic.get_door_item_name(door) | ||
| 274 | self.item_by_door[door.id] = (door_item_name, 1) | ||
| 275 | self.real_items.append(door_item_name) | ||
| 276 | |||
| 277 | # We handle cyan_door_behavior = Item after door shuffle, because cyan doors that are impacted by door shuffle | ||
| 278 | # should be exempt from cyan_door_behavior. | ||
| 279 | if world.options.cyan_door_behavior == CyanDoorBehavior.option_item: | ||
| 280 | for door_group in world.static_logic.objects.door_groups: | ||
| 281 | if door_group.type != data_pb2.DoorGroupType.CYAN_DOORS: | ||
| 282 | continue | ||
| 283 | |||
| 284 | for door in door_group.doors: | ||
| 285 | if not door in self.item_by_door: | ||
| 286 | self.item_by_door[door] = (door_group.name, 1) | ||
| 287 | |||
| 288 | self.real_items.append(door_group.name) | ||
| 289 | |||
| 290 | for door in world.static_logic.objects.doors: | ||
| 291 | if door.type in [data_pb2.DoorType.STANDARD, data_pb2.DoorType.LOCATION_ONLY, data_pb2.DoorType.GRAVESTONE]: | ||
| 292 | self.locations_by_room.setdefault(door.room_id, []).append(PlayerLocation(door.ap_id, | ||
| 293 | self.get_door_reqs(door.id))) | ||
| 294 | |||
| 295 | for letter in world.static_logic.objects.letters: | ||
| 296 | self.locations_by_room.setdefault(letter.room_id, []).append(PlayerLocation(letter.ap_id, | ||
| 297 | AccessRequirements())) | ||
| 298 | behavior = self.get_letter_behavior(letter.key, letter.level2) | ||
| 299 | if behavior == LetterBehavior.VANILLA: | ||
| 300 | letter_name = f"{letter.key.upper()}{'2' if letter.level2 else '1'}" | ||
| 301 | event_name = f"{letter_name} (Collected)" | ||
| 302 | self.event_loc_item_by_room.setdefault(letter.room_id, {})[event_name] = letter.key.upper() | ||
| 303 | |||
| 304 | if letter.level2: | ||
| 305 | event_name = f"{letter_name} (Double Collected)" | ||
| 306 | self.event_loc_item_by_room.setdefault(letter.room_id, {})[event_name] = letter.key.upper() | ||
| 307 | elif behavior == LetterBehavior.ITEM: | ||
| 308 | self.real_items.append(letter.key.upper()) | ||
| 309 | |||
| 310 | if behavior != LetterBehavior.UNLOCKED: | ||
| 311 | self.double_letter_amount[letter.key.upper()] = self.double_letter_amount.get(letter.key.upper(), 0) + 1 | ||
| 312 | |||
| 313 | for mastery in world.static_logic.objects.masteries: | ||
| 314 | self.locations_by_room.setdefault(mastery.room_id, []).append(PlayerLocation(mastery.ap_id, | ||
| 315 | AccessRequirements())) | ||
| 316 | |||
| 317 | for ending in world.static_logic.objects.endings: | ||
| 318 | # Don't ever create a location for White Ending. Don't even make an event for it if it's not the victory | ||
| 319 | # condition, since it is necessarily going to be in the postgame. | ||
| 320 | if ending.name == "WHITE": | ||
| 321 | if self.world.options.victory_condition != VictoryCondition.option_white_ending: | ||
| 322 | continue | ||
| 323 | else: | ||
| 324 | self.locations_by_room.setdefault(ending.room_id, []).append(PlayerLocation(ending.ap_id, | ||
| 325 | AccessRequirements())) | ||
| 326 | |||
| 327 | event_name = f"{ending.name.capitalize()} Ending (Achieved)" | ||
| 328 | item_name = event_name | ||
| 329 | |||
| 330 | if world.options.victory_condition.current_key.removesuffix("_ending").upper() == ending.name: | ||
| 331 | item_name = "Victory" | ||
| 332 | |||
| 333 | self.event_loc_item_by_room.setdefault(ending.room_id, {})[event_name] = item_name | ||
| 334 | |||
| 335 | if self.world.options.keyholder_sanity: | ||
| 336 | for keyholder in world.static_logic.objects.keyholders: | ||
| 337 | if keyholder.HasField("key"): | ||
| 338 | reqs = AccessRequirements() | ||
| 339 | |||
| 340 | if self.get_letter_behavior(keyholder.key, False) != LetterBehavior.UNLOCKED: | ||
| 341 | reqs.letters[keyholder.key.upper()] = 1 | ||
| 342 | |||
| 343 | self.locations_by_room.setdefault(keyholder.room_id, []).append(PlayerLocation(keyholder.ap_id, | ||
| 344 | reqs)) | ||
| 345 | |||
| 346 | if self.world.options.shuffle_symbols: | ||
| 347 | for symbol_name in SYMBOL_ITEMS.values(): | ||
| 348 | self.real_items.append(symbol_name) | ||
| 349 | |||
| 350 | def get_panel_reqs(self, panel_id: int, answer: str | None) -> AccessRequirements: | ||
| 351 | if answer is None: | ||
| 352 | if panel_id not in self.panel_reqs: | ||
| 353 | self.panel_reqs[panel_id] = self.calculate_panel_reqs(panel_id, answer) | ||
| 354 | |||
| 355 | return self.panel_reqs.get(panel_id) | ||
| 356 | else: | ||
| 357 | if panel_id not in self.proxy_reqs or answer not in self.proxy_reqs.get(panel_id): | ||
| 358 | self.proxy_reqs.setdefault(panel_id, {})[answer] = self.calculate_panel_reqs(panel_id, answer) | ||
| 359 | |||
| 360 | return self.proxy_reqs.get(panel_id).get(answer) | ||
| 361 | |||
| 362 | def calculate_panel_reqs(self, panel_id: int, answer: str | None) -> AccessRequirements: | ||
| 363 | panel = self.world.static_logic.objects.panels[panel_id] | ||
| 364 | reqs = AccessRequirements() | ||
| 365 | |||
| 366 | reqs.rooms.add(self.world.static_logic.get_room_region_name(panel.room_id)) | ||
| 367 | |||
| 368 | if answer is not None: | ||
| 369 | self.add_solution_reqs(reqs, answer) | ||
| 370 | elif len(panel.proxies) > 0: | ||
| 371 | possibilities = [] | ||
| 372 | already_filled = False | ||
| 373 | |||
| 374 | for proxy in panel.proxies: | ||
| 375 | proxy_reqs = AccessRequirements() | ||
| 376 | self.add_solution_reqs(proxy_reqs, proxy.answer) | ||
| 377 | |||
| 378 | if not proxy_reqs.is_empty(): | ||
| 379 | possibilities.append(proxy_reqs) | ||
| 380 | else: | ||
| 381 | already_filled = True | ||
| 382 | break | ||
| 383 | |||
| 384 | if not already_filled and not any(proxy.answer == panel.answer for proxy in panel.proxies): | ||
| 385 | proxy_reqs = AccessRequirements() | ||
| 386 | self.add_solution_reqs(proxy_reqs, panel.answer) | ||
| 387 | |||
| 388 | if not proxy_reqs.is_empty(): | ||
| 389 | possibilities.append(proxy_reqs) | ||
| 390 | else: | ||
| 391 | already_filled = True | ||
| 392 | |||
| 393 | if not already_filled: | ||
| 394 | reqs.or_logic.append(possibilities) | ||
| 395 | else: | ||
| 396 | self.add_solution_reqs(reqs, panel.answer) | ||
| 397 | |||
| 398 | if self.world.options.shuffle_symbols: | ||
| 399 | for symbol in panel.symbols: | ||
| 400 | reqs.items.add(SYMBOL_ITEMS.get(symbol)) | ||
| 401 | |||
| 402 | if panel.HasField("required_door"): | ||
| 403 | door_reqs = self.get_door_open_reqs(panel.required_door) | ||
| 404 | reqs.merge(door_reqs) | ||
| 405 | |||
| 406 | if panel.HasField("required_room"): | ||
| 407 | reqs.rooms.add(self.world.static_logic.get_room_region_name(panel.required_room)) | ||
| 408 | |||
| 409 | return reqs | ||
| 410 | |||
| 411 | # This gets/calculates the requirements described by the door object. This is most notably used as the requirements | ||
| 412 | # for clearing a location, or opening a door when the door is not shuffled. | ||
| 413 | def get_door_reqs(self, door_id: int) -> AccessRequirements: | ||
| 414 | if door_id not in self.door_reqs: | ||
| 415 | self.door_reqs[door_id] = self.calculate_door_reqs(door_id) | ||
| 416 | |||
| 417 | return self.door_reqs.get(door_id) | ||
| 418 | |||
| 419 | def calculate_door_reqs(self, door_id: int) -> AccessRequirements: | ||
| 420 | door = self.world.static_logic.objects.doors[door_id] | ||
| 421 | reqs = AccessRequirements() | ||
| 422 | |||
| 423 | if not door.HasField("complete_at") or door.complete_at == 0: | ||
| 424 | for proxy in door.panels: | ||
| 425 | panel_reqs = self.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None) | ||
| 426 | reqs.merge(panel_reqs) | ||
| 427 | elif door.complete_at == 1: | ||
| 428 | disjunction = [] | ||
| 429 | for proxy in door.panels: | ||
| 430 | proxy_reqs = self.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None) | ||
| 431 | if proxy_reqs.is_empty(): | ||
| 432 | disjunction.clear() | ||
| 433 | break | ||
| 434 | else: | ||
| 435 | disjunction.append(proxy_reqs) | ||
| 436 | if len(disjunction) > 0: | ||
| 437 | reqs.or_logic.append(disjunction) | ||
| 438 | else: | ||
| 439 | reqs.complete_at = door.complete_at | ||
| 440 | for proxy in door.panels: | ||
| 441 | panel_reqs = self.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None) | ||
| 442 | reqs.possibilities.append(panel_reqs) | ||
| 443 | |||
| 444 | if door.HasField("control_center_color"): | ||
| 445 | # TODO: Logic for ensuring two CC states aren't needed at once. | ||
| 446 | reqs.rooms.add("Control Center - Main Area") | ||
| 447 | self.add_solution_reqs(reqs, door.control_center_color) | ||
| 448 | |||
| 449 | if door.double_letters: | ||
| 450 | if self.world.options.cyan_door_behavior == CyanDoorBehavior.option_collect_h2: | ||
| 451 | reqs.rooms.add("The Repetitive - Main Room") | ||
| 452 | elif self.world.options.cyan_door_behavior == CyanDoorBehavior.option_any_double_letter: | ||
| 453 | if self.world.options.shuffle_letters != ShuffleLetters.option_unlocked: | ||
| 454 | reqs.cyans = True | ||
| 455 | elif self.world.options.cyan_door_behavior == CyanDoorBehavior.option_item: | ||
| 456 | # There shouldn't be any locations that are cyan doors. | ||
| 457 | pass | ||
| 458 | |||
| 459 | for keyholder_uses in door.keyholders: | ||
| 460 | key_name = keyholder_uses.key.upper() | ||
| 461 | if (self.get_letter_behavior(keyholder_uses.key, False) != LetterBehavior.UNLOCKED | ||
| 462 | and key_name not in reqs.letters): | ||
| 463 | reqs.letters[key_name] = 1 | ||
| 464 | |||
| 465 | keyholder = self.world.static_logic.objects.keyholders[keyholder_uses.keyholder] | ||
| 466 | reqs.rooms.add(self.world.static_logic.get_room_region_name(keyholder.room_id)) | ||
| 467 | |||
| 468 | for room in door.rooms: | ||
| 469 | reqs.rooms.add(self.world.static_logic.get_room_region_name(room)) | ||
| 470 | |||
| 471 | for ending_id in door.endings: | ||
| 472 | ending = self.world.static_logic.objects.endings[ending_id] | ||
| 473 | |||
| 474 | if self.world.options.victory_condition.current_key.removesuffix("_ending").upper() == ending.name: | ||
| 475 | reqs.items.add("Victory") | ||
| 476 | else: | ||
| 477 | reqs.items.add(f"{ending.name.capitalize()} Ending (Achieved)") | ||
| 478 | |||
| 479 | for sub_door_id in door.doors: | ||
| 480 | sub_reqs = self.get_door_open_reqs(sub_door_id) | ||
| 481 | reqs.merge(sub_reqs) | ||
| 482 | |||
| 483 | reqs.simplify() | ||
| 484 | |||
| 485 | return reqs | ||
| 486 | |||
| 487 | # This gets the requirements to open a door within the world. When a door is shuffled, this means having the item | ||
| 488 | # that acts as the door's key. | ||
| 489 | def get_door_open_reqs(self, door_id: int) -> AccessRequirements: | ||
| 490 | if door_id in self.item_by_door: | ||
| 491 | reqs = AccessRequirements() | ||
| 492 | |||
| 493 | item_name, amount = self.item_by_door.get(door_id) | ||
| 494 | if amount == 1: | ||
| 495 | reqs.items.add(item_name) | ||
| 496 | else: | ||
| 497 | reqs.progressives[item_name] = amount | ||
| 498 | |||
| 499 | return reqs | ||
| 500 | else: | ||
| 501 | return self.get_door_reqs(door_id) | ||
| 502 | |||
| 503 | def get_letter_behavior(self, letter: str, level2: bool) -> LetterBehavior: | ||
| 504 | if self.world.options.shuffle_letters == ShuffleLetters.option_unlocked: | ||
| 505 | return LetterBehavior.UNLOCKED | ||
| 506 | |||
| 507 | if self.world.options.shuffle_letters in [ShuffleLetters.option_vanilla_cyan, ShuffleLetters.option_item_cyan]: | ||
| 508 | if level2: | ||
| 509 | if self.world.options.shuffle_letters == ShuffleLetters.option_vanilla_cyan: | ||
| 510 | return LetterBehavior.VANILLA | ||
| 511 | else: | ||
| 512 | return LetterBehavior.ITEM | ||
| 513 | else: | ||
| 514 | return LetterBehavior.UNLOCKED | ||
| 515 | |||
| 516 | if not level2 and letter in ["h", "i", "n", "t"]: | ||
| 517 | return LetterBehavior.UNLOCKED | ||
| 518 | |||
| 519 | if self.world.options.shuffle_letters == ShuffleLetters.option_progressive: | ||
| 520 | return LetterBehavior.ITEM | ||
| 521 | |||
| 522 | return LetterBehavior.VANILLA | ||
| 523 | |||
| 524 | def add_solution_reqs(self, reqs: AccessRequirements, solution: str): | ||
| 525 | histogram = calculate_letter_histogram(solution) | ||
| 526 | |||
| 527 | for l, a in histogram.items(): | ||
| 528 | needed = min(a, 2) | ||
| 529 | level2 = (needed == 2) | ||
| 530 | |||
| 531 | if level2 and self.get_letter_behavior(l.lower(), True) == LetterBehavior.UNLOCKED: | ||
| 532 | needed = 1 | ||
| 533 | |||
| 534 | if self.get_letter_behavior(l.lower(), False) == LetterBehavior.UNLOCKED: | ||
| 535 | needed = needed - 1 | ||
| 536 | |||
| 537 | if needed > 0: | ||
| 538 | reqs.letters[l] = max(reqs.letters.get(l, 0), needed) | ||
