from typing import TYPE_CHECKING import BaseClasses from BaseClasses import Region, ItemClassification, Entrance from entrance_rando import randomize_entrances from .items import Lingo2Item from .locations import Lingo2Location from .player_logic import AccessRequirements from .rules import make_location_lambda if TYPE_CHECKING: from . import Lingo2World def create_region(room, world: "Lingo2World") -> Region: return Region(world.static_logic.get_room_region_name(room.id), world.player, world.multiworld) def create_locations(room, new_region: Region, world: "Lingo2World", regions: dict[str, Region]): for location in world.player_logic.locations_by_room.get(room.id, {}): reqs = location.reqs.copy() reqs.remove_room(new_region.name) new_location = Lingo2Location(world.player, world.static_logic.location_id_to_name[location.code], location.code, new_region) new_location.access_rule = make_location_lambda(reqs, world, regions) new_region.locations.append(new_location) for event_name, item_name in world.player_logic.event_loc_item_by_room.get(room.id, {}).items(): new_location = Lingo2Location(world.player, event_name, None, new_region) if world.for_tracker and item_name == "Victory": new_location.goal = True event_item = Lingo2Item(item_name, ItemClassification.progression, None, world.player) new_location.place_locked_item(event_item) new_region.locations.append(new_location) if world.for_tracker and world.options.shuffle_worldports: for port_id in room.ports: port = world.static_logic.objects.ports[port_id] if port.no_shuffle: continue new_location = Lingo2Location(world.player, f"Worldport {port.id} Entered", None, new_region) new_location.port_id = port.id if port.HasField("required_door"): new_location.access_rule = \ make_location_lambda(world.player_logic.get_door_open_reqs(port.required_door), world, regions) new_region.locations.append(new_location) def create_regions(world: "Lingo2World"): regions = { "Menu": Region("Menu", world.player, world.multiworld) } region_and_room = [] # Create the regions in two stages. First, make the actual region objects and memoize them. Then, add all of the # locations. This allows us to reference the actual region objects in the access rules for the locations, which is # faster than having to look them up during access checking. for room in world.static_logic.objects.rooms: if room.map_id not in world.player_logic.shuffled_maps: continue region = create_region(room, world) regions[region.name] = region region_and_room.append((region, room)) for (region, room) in region_and_room: create_locations(room, region, world, regions) regions["Menu"].connect(regions["The Entry - Starting Room"], "Start Game") for connection in world.static_logic.objects.connections: if connection.roof_access and not world.options.daedalus_roof_access: continue if connection.vanilla_only and world.options.shuffle_doors: continue from_region = world.static_logic.get_room_region_name(connection.from_room) to_region = world.static_logic.get_room_region_name(connection.to_room) if from_region not in regions or to_region not in regions: continue connection_name = f"{from_region} -> {to_region}" reqs = AccessRequirements() if connection.HasField("required_door"): reqs.merge(world.player_logic.get_door_open_reqs(connection.required_door)) door = world.static_logic.objects.doors[connection.required_door] wmap = world.static_logic.objects.maps[door.map_id] connection_name = f"{connection_name} (using {wmap.name} - {door.name})" if connection.HasField("port"): port = world.static_logic.objects.ports[connection.port] connection_name = f"{connection_name} (via {port.display_name})" if world.options.shuffle_worldports and not port.no_shuffle: continue if port.HasField("required_door"): reqs.merge(world.player_logic.get_door_open_reqs(port.required_door)) if connection.HasField("painting"): painting = world.static_logic.objects.paintings[connection.painting] connection_name = f"{connection_name} (via painting {painting.name})" if painting.HasField("required_door"): reqs.merge(world.player_logic.get_door_open_reqs(painting.required_door)) if connection.HasField("panel"): proxy = connection.panel reqs.merge(world.player_logic.get_panel_reqs(proxy.panel, proxy.answer if proxy.HasField("answer") else None)) panel = world.static_logic.objects.panels[proxy.panel] if proxy.HasField("answer"): connection_name = f"{connection_name} (via panel {panel.name}/{proxy.answer})" else: connection_name = f"{connection_name} (via panel {panel.name})" if connection.HasField("purple_ending") and connection.purple_ending and world.options.strict_purple_ending: world.player_logic.add_solution_reqs(reqs, "abcdefghijklmnopqrstuvwxyz") if connection.HasField("cyan_ending") and connection.cyan_ending and world.options.strict_cyan_ending: world.player_logic.add_solution_reqs(reqs, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz") reqs.simplify() reqs.remove_room(from_region) if to_region in reqs.rooms: # This connection can't ever increase access because you're required to have access to the other side in # order for it to be usable. We will just not create the connection at all, in order to help GER figure out # what regions are dead ends. continue connection = Entrance(world.player, connection_name, regions[from_region]) connection.access_rule = make_location_lambda(reqs, world, regions) regions[from_region].exits.append(connection) connection.connect(regions[to_region]) for region in reqs.get_referenced_rooms(): world.multiworld.register_indirect_condition(regions[region], connection) world.multiworld.regions += regions.values() def shuffle_entrances(world: "Lingo2World"): er_entrances: list[Entrance] = [] er_exits: list[Entrance] = [] port_id_by_name: dict[str, int] = {} shuffleable_ports = [port for port in world.static_logic.objects.ports if not port.no_shuffle and world.static_logic.get_room_object_map_id(port) in world.player_logic.shuffled_maps] if len(shuffleable_ports) % 2 == 1: # We have an odd number of shuffleable ports! Pick a port from a room that has more than one, and make it a # redundant warp to another port. redundant_rooms = set(room.id for room in world.static_logic.objects.rooms if len(room.ports) > 1) redundant_ports = [port for port in shuffleable_ports if port.room_id in redundant_rooms] chosen_port = world.random.choice(redundant_ports) shuffleable_ports.remove(chosen_port) chosen_destination = world.random.choice(shuffleable_ports) world.port_pairings[chosen_port.id] = chosen_destination.id from_region_name = world.static_logic.get_room_region_name(chosen_port.room_id) to_region_name = world.static_logic.get_room_region_name(chosen_destination.room_id) from_region = world.multiworld.get_region(from_region_name, world.player) to_region = world.multiworld.get_region(to_region_name, world.player) connection = Entrance(world.player, f"{from_region_name} - {chosen_port.display_name}", from_region) from_region.exits.append(connection) connection.connect(to_region) if chosen_port.HasField("required_door"): door_reqs = world.player_logic.get_door_open_reqs(chosen_port.required_door) connection.access_rule = make_location_lambda(door_reqs, world, None) for region in door_reqs.get_referenced_rooms(): world.multiworld.register_indirect_condition(world.multiworld.get_region(region, world.player), connection) for port in shuffleable_ports: port_region_name = world.static_logic.get_room_region_name(port.room_id) port_region = world.multiworld.get_region(port_region_name, world.player) connection_name = f"{port_region_name} - {port.display_name}"
from .generated import data_pb2 as data_pb2
from .items import SYMBOL_ITEMS, ANTI_COLLECTABLE_TRAPS
import pkgutil
class Lingo2StaticLogic:
item_id_to_name: dict[int, str]
location_id_to_name: dict[int, str]
item_name_to_id: dict[str, int]
location_name_to_id: dict[str, int]
item_name_groups: dict[str, list[str]]
location_name_groups: dict[str, list[str]]
letter_weights: dict[str, int]
def __init__(self):
self.item_id_to_name = {}
self.location_id_to_name = {}
self.item_name_groups = {}
self.location_name_groups = {}
self.letter_weights = {}
file = pkgutil.get_data(__name__, "generated/data.binpb")
self.objects = data_pb2.AllObjects()
self.objects.ParseFromString(bytearray(file))
for door in self.objects.doors:
if door.type in [data_pb2.DoorType.STANDARD, data_pb2.DoorType.LOCATION_ONLY, data_pb2.DoorType.GRAVESTONE]:
location_name = self.get_door_location_name(door)
self.location_id_to_name[door.ap_id] = location_name
if door.type not in [data_pb2.DoorType.EVENT, data_pb2.DoorType.LOCATION_ONLY, data_pb2.DoorType.GRAVESTONE]:
item_name = self.get_door_item_name(door)
self.item_id_to_name[door.ap_id] = item_name
for letter in self.objects.letters:
letter_name = f"{letter.key.upper()}{'2' if letter.level2 else '1'}"
location_name = f"{self.get_room_object_map_name(letter)} - {letter_name}"
self.location_id_to_name[letter.ap_id] = location_name
self.location_name_groups.setdefault("Letters", []).append(location_name)
if not letter.level2:
self.item_id_to_name[letter.ap_id] = letter.key.upper()
self.item_name_groups.setdefault("Letters", []).append(letter.key.upper())
for mastery in self.objects.masteries:
location_name = f"{self.get_room_object_map_name(mastery)} - Mastery"
self.location_id_to_name[mastery.ap_id] = location_name
self.location_name_groups.setdefault("Masteries", []).append(location_name)
for ending in self.objects.endings:
location_name = f"{self.get_room_object_map_name(ending)} - {ending.name.title()} Ending"
self.location_id_to_name[ending.ap_id] = location_name
self.location_name_groups.setdefault("Endings", []).append(location_name)
for progressive in self.objects.progressives:
self.item_id_to_name[progressive.ap_id] = progressive.name
for door_group in self.objects.door_groups:
self.item_id_to_name[door_group.ap_id] = door_group.name
for keyholder in self.objects.keyholders:
if keyholder.HasField("key"):
location_name = f"{self.get_room_object_location_prefix(keyholder)} - {keyholder.key.upper()} Keyholder"
self.location_id_to_name[keyholder.ap_id] = location_name
self.location_name_groups.setdefault("Keyholders", []).append(location_name)
self.item_id_to_name[self.objects.special_ids["A Job Well Done"]] = "A Job Well Done"
for symbol_name in SYMBOL_ITEMS.values():
self.item_id_to_name[self.objects.special_ids[symbol_name]] = symbol_name
for trap_name in ANTI_COLLECTABLE_TRAPS:
self.item_id_to_name[self.objects.special_ids[trap_name]] = trap_name
self.item_name_to_id = {name: ap_id for ap_id, name in self.item_id_to_name.items()}
self.location_name_to_id = {name: ap_id for ap_id, name in self.location_id_to_name.items()}
for panel in self.objects.panels:
for letter in panel.answer.upper():
self.letter_weights[letter] = self.letter_weights.get(letter, 0) + 1
def get_door_item_name(self, door: data_pb2.Door) -> str:
return f"{self.get_map_object_map_name(door)} - {door.name}"
def get_door_item_name_by_id(self, door_id: int) -> str:
door = self.objects.doors[door_id]
return self.get_door_item_name(door_id)
def get_door_location_name(self, door: data_pb2.Door) -> str:
map_part = self.get_room_object_location_prefix(door)
if door.HasField("location_name"):
return f"{map_part} - {door.location_name}"
generated_location_name = self.get_generated_door_location_name(door)
if generated_location_name is not None:
return generated_location_name
return f"{map_part} - {door.name}"
def get_generated_door_location_name(self, door: data_pb2.Door) -> str | None:
if door.type != data_pb2.DoorType.STANDARD:
return None
if len(door.keyholders) > 0 or len(door.endings) > 0 or door.HasField("complete_at"):
return None
if len(door.panels) > 4:
return None
map_areas = set()
for panel_id in door.panels:
panel = self.objects.panels[panel_id.panel]
panel_room = self.objects.rooms[panel.room_id]
# It's okay if panel_display_name is not present because then it's coalesced with other unnamed areas.
map_areas.add(panel_room.panel_display_name)
if len(map_areas) > 1:
return None
game_map = self.objects.maps[door.map_id]
map_area = map_areas.pop()
if map_area == "":
map_part = game_map.display_name
else:
map_part = f"{game_map.display_name} ({map_area})"
def get_panel_display_name(panel: data_pb2.ProxyIdentifier) -> str:
panel_data = self.objects.panels[panel.panel]
panel_name = panel_data.display_name if panel_data.HasField("display_name") else panel_data.name
if panel.HasField("answer"):
return f"{panel_name}/{panel.answer.upper()}"
else:
return panel_name
panel_names = [get_panel_display_name(panel_id)
for panel_id in door.panels]
panel_names.sort()
return map_part + " - " + ", ".join(panel_names)
def get_door_location_name_by_id(self, door_id: int) -> str:
door = self.objects.doors[door_id]
return self.get_door_location_name(door)
def get_room_region_name(self, room_id: int) -> str:
room = self.objects.rooms[room_id]
return f"{self.get_map_object_map_name(room)} - {room.name}"
def get_map_object_map_name(self, obj) -> str:
return self.objects.maps[obj.map_id].display_name
def get_room_object_map_name(self, obj) -> str:
return self.get_map_object_map_name(self.objects.rooms[obj.room_id])
def get_room_object_location_prefix(self, obj) -> str:
room = self.objects.rooms[obj.room_id]
game_map = self.objects.maps[room.map_id]
if room.HasField("panel_display_name"):
return f"{game_map.display_name} ({room.panel_display_name})"
else:
return game_map.display_name
def get_room_object_map_id(self, obj) -> int:
return self.objects.rooms[obj.room_id].map_id
def get_data_version(self) -> list[int]:
version = self.objects.version
return [version.major, version.minor, version.patch]