from dataclasses import dataclass from Options import PerGameCommonOptions, Toggle, Choice, DefaultOnToggle class ShuffleDoors(DefaultOnToggle): """If enabled, most doors will open from receiving an item rather than fulfilling the in-game requirements.""" display_name = "Shuffle Doors" class ShuffleControlCenterColors(Toggle): """ Some doors open after solving the COLOR panel in the Control Center. If this option is enabled, these doors will instead open upon receiving an item. """ display_name = "Shuffle Control Center Colors" class ShuffleLetters(Choice): """ Controls how letter unlocks are handled. Note that H1, I1, N1, and T1 will always be present at their vanilla locations in the starting room, even if letters are shuffled remotely. - **Vanilla**: All letters will be present at their vanilla locations. - **Unlocked**: Players will start with their keyboards fully unlocked. - **Progressive**: Two items will be added to the pool for every letter (one for H, I, N, and T). Receiving the first item gives you the corresponding level 1 letter, and the second item gives you the corresponding level 2 letter. - **Vanilla Cyan**: Players will start with all level 1 (purple) letters unlocked. Level 2 (cyan) letters will be present at their vanilla locations. - **Item Cyan**: Players will start with all level 1 (purple) letters unlocked. One item will be added to the pool for every level 2 (cyan) letter. """ display_name = "Shuffle Letters" option_vanilla = 0 option_unlocked = 1 option_progressive = 2 option_vanilla_cyan = 3 option_item_cyan = 4 class ShuffleSymbols(Toggle): """ If enabled, 19 items will be added to the pool, representing the different symbols that can appear on a panel. Players will be prevented from solving puzzles with symbols on them until all of the required symbols are unlocked. """ display_name = "Shuffle Symbols" class KeyholderSanity(Toggle): """ If enabled, 26 locations will be created for placing each key into its respective Green Ending keyholder. NOTE: This does not apply to the two disappearing keyholders in The Congruent, as they are not part of Green Ending. """ display_name = "Keyholder Sanity" class CyanDoorBehavior(Choice): """ Cyan-colored doors usually only open upon unlocking double letters. Some panels also only appear upon unlocking double letters. This option determines how these unlocks should behave. - **Collect H2**: In the base game, H2 is the first double letter you are intended to collect, so cyan doors only open when you collect the H2 pickup in The Repetitive. Collecting the actual pickup is still required even with remote letter shuffle enabled. - **Any Double Letter**: Cyan doors will open when you have unlocked any cyan letter on your keyboard. In letter shuffle, this means receiving a cyan letter, not picking up a cyan letter collectable. - **Item**: Cyan doors will be grouped together in a single item. Note that some cyan doors are impacted by door shuffle (e.g. the entrance to The Tower). When door shuffle is enabled, these doors won't be affected by the value of this option. """ display_name = "Cyan Door Behavior" option_collect_h2 = 0 option_any_double_letter = 1 option_item = 2 class DaedalusRoofAccess(Toggle): """ If enabled, the player will be logically expected to be able to go from the castle entrance to any part of Daedalus that is open to the air. If disabled, the player will only be expected to be able to enter the castle, the moat, Icarus, and the area at the bottom of the stairs. Invisible walls that become opaque as you approach them are added to the level to prevent the player from accidentally breaking logic. """ display_name = "Allow Daedalus Roof Access" class VictoryCondition(Choice): """ This option determines what your goal is. - **Gray Ending** (The Colorful) - **Purple Ending** (The Sun Temple). This ordinarily requires all level 1 (purple) letters. - **Mint Ending** (typing EXIT into the keyholders in Control Center) - **Black Ending** (The Graveyard) - **Blue Ending** (The Words) - **Cyan Ending** (The Parthenon). This ordinarily requires almost all level 2 (cyan) letters. - **Red Ending** (The Tower) - **Plum Ending** (The Wondrous / The Door) - **Orange Ending** (the castle in Daedalus) - **Gold Ending** (The Gold). This involves going through the color rooms in Daedalus. - **Yellow Ending** (The Gallery). This requires unlocking all gallery paintings. - **Green Ending** (The Ancient). This requires filling all keyholders with specific letters. - **White Ending** (Control Center). This combines every other ending. """ display_name = "Victory Condition" option_gray_ending = 0 option_purple_ending = 1 option_mint_ending = 2 option_black_ending = 3 option_blue_ending = 4 option_cyan_ending = 5 option_red_ending = 6 option_plum_ending = 7 option_orange_ending = 8 option_gold_ending = 9 option_yellow_ending = 10 option_green_ending = 11 option_white_ending = 12 @dataclass class Lingo2Options(PerGameCommonOptions): shuffle_doors: ShuffleDoors shuffle_control_center_colors: ShuffleControlCenterColors shuffle_letters: ShuffleLetters shuffle_symbols: ShuffleSymbols keyholder_sanity: KeyholderSanity cyan_door_behavior: CyanDoorBehavior daedalus_roof_access: DaedalusRoofAccess victory_condition: VictoryCondition href='#n63'>63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
extends Node2D


func _ready():
	# Some helpful logging.
	if Steam.isSubscribed():
		global._print("Provisioning successful! Build ID: %d" % Steam.getAppBuildId())
	else:
		global._print("Provisioning failed.")

	# Undo the load screen removing our cursor
	get_tree().get_root().set_disable_input(false)
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

	# Increase the WebSocket input buffer size so that we can download large
	# data packages.
	ProjectSettings.set_setting("network/limits/websocket_client/max_in_buffer_kb", 8192)

	# Create the global AP manager, if it doesn't already exist.
	if not global.has_node("Archipelago"):
		var ap_script = ResourceLoader.load("user://maps/Archipelago/manager.gd")
		var ap_instance = ap_script.new()
		ap_instance.name = "Archipelago"

		ap_instance.SCRIPT_client = load("user://maps/Archipelago/client.gd")
		ap_instance.SCRIPT_keyboard = load("user://maps/Archipelago/keyboard.gd")
		ap_instance.SCRIPT_locationListener = load("user://maps/Archipelago/locationListener.gd")
		ap_instance.SCRIPT_uuid = load("user://maps/Archipelago/vendor/uuid.gd")
		ap_instance.SCRIPT_victoryListener = load("user://maps/Archipelago/victoryListener.gd")

		global.add_child(ap_instance)

		# Let's also inject any scripts we need to inject now.
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/animationListener.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/collectable.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/door.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/keyHolder.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/keyHolderChecker.gd"))
		installScriptExtension(
			ResourceLoader.load("user://maps/Archipelago/keyHolderResetterListener.gd")
		)
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/painting.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/panel.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/pauseMenu.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/player.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/saver.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/teleport.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/teleportListener.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/visibilityListener.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/worldport.gd"))
		installScriptExtension(ResourceLoader.load("user://maps/Archipelago/worldportListener.gd"))

		var proto_script = load("user://maps/Archipelago/generated/proto.gd")
		var gamedata_script = load("user://maps/Archipelago/gamedata.gd")
		var gamedata_instance = gamedata_script.new(proto_script)
		gamedata_instance.load(
			FileAccess.get_file_as_bytes("user://maps/Archipelago/generated/data.binpb")
		)
		gamedata_instance.name = "Gamedata"
		global.add_child(gamedata_instance)

		var messages_script = load("user://maps/Archipelago/messages.gd")
		var messages_instance = messages_script.new()
		messages_instance.name = "Messages"
		global.add_child(messages_instance)

		var textclient_script = load("user://maps/Archipelago/textclient.gd")
		var textclient_instance = textclient_script.new()
		textclient_instance.name = "Textclient"
		global.add_child(textclient_instance)

	var ap = global.get_node("Archipelago")
	var gamedata = global.get_node("Gamedata")
	ap.connect("ap_connected", connectionSuccessful)
	ap.connect("could_not_connect", connectionUnsuccessful)
	ap.connect("connect_status", connectionStatus)

	# Populate textboxes with AP settings.
	$Panel/server_box.text = ap.ap_server
	$Panel/player_box.text = ap.ap_user
	$Panel/password_box.text = ap.ap_pass

	var history_box = $Panel/connection_history
	if ap.connection_history.is_empty():
		history_box.disabled = true
	else:
		history_box.disabled = false

		var i = 0
		for details in ap.connection_history:
			history_box.get_popup().add_item("%s (%s)" % [details[1], details[0]], i)
			i += 1

	history_box.get_popup().connect("id_pressed", historySelected)

	# Show client version.
	$Panel/title.text = "ARCHIPELAGO (%d.%d)" % [gamedata.objects.get_version(), ap.MOD_VERSION]

	# Increase font size in text boxes.
	$Panel/server_box.add_theme_font_size_override("font_size", 36)
	$Panel/player_box.add_theme_font_size_override("font_size", 36)
	$Panel/password_box.add_theme_font_size_override("font_size", 36)

	# Set up version mismatch dialog.
	$Panel/VersionMismatch.connect("confirmed", startGame)
	$Panel/VersionMismatch.get_cancel_button().pressed.connect(versionMismatchDeclined)


# Adapted from https://gitlab.com/Delta-V-Modding/Mods/-/blob/main/game/ModLoader.gd
func installScriptExtension(childScript: Resource):
	# Force Godot to compile the script now.
	# We need to do this here to ensure that the inheritance chain is
	# properly set up, and multiple mods can chain-extend the same
	# class multiple times.
	# This is also needed to make Godot instantiate the extended class
	# when creating singletons.
	# The actual instance is thrown away.
	childScript.new()

	var parentScript = childScript.get_base_script()
	var parentScriptPath = parentScript.resource_path
	global._print("ModLoader: Installing script extension over %s" % parentScriptPath)
	childScript.take_over_path(parentScriptPath)


func connectionStatus(message):
	var popup = self.get_node("Panel/AcceptDialog")
	popup.title = "Connecting to Archipelago"
	popup.dialog_text = message
	popup.exclusive = true
	popup.get_ok_button().visible = false
	popup.popup_centered()


func connectionSuccessful():
	var ap = global.get_node("Archipelago")
	var gamedata = global.get_node("Gamedata")

	# Check for major version mismatch.
	if ap.apworld_version[0] != gamedata.objects.get_version():
		$Panel/AcceptDialog.exclusive = false

		var popup = self.get_node("Panel/VersionMismatch")
		popup.title = "Version Mismatch!"
		popup.dialog_text = (
			"This slot was generated using v%d.%d of the Lingo 2 apworld,\nwhich has a different major version than this client (v%d.%d).\nIt is highly recommended to play using the correct version of the client.\nYou may experience bugs or logic issues if you continue."
			% [
				ap.apworld_version[0],
				ap.apworld_version[1],
				gamedata.objects.get_version(),
				ap.MOD_VERSION
			]
		)
		popup.exclusive = true
		popup.popup_centered()

		return

	startGame()


func startGame():
	var ap = global.get_node("Archipelago")

	# Save connection details
	var connection_details = [ap.ap_server, ap.ap_user, ap.ap_pass]
	if ap.connection_history.has(connection_details):
		ap.connection_history.erase(connection_details)
	ap.connection_history.push_front(connection_details)
	if ap.connection_history.size() > 10:
		ap.connection_history.resize(10)
	ap.saveSettings()

	# Switch to the_entry
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	global.user = ap.getSaveFileName()
	global.universe = "lingo"
	global.map = "the_entry"

	unlocks.resetCollectables()
	unlocks.resetData()

	ap.setup_keys()

	unlocks.loadCollectables()
	unlocks.loadData()
	unlocks.unlockKey("capslock", 1)

	clearResourceCache("res://objects/meshes/gridDoor.tscn")
	clearResourceCache("res://objects/nodes/collectable.tscn")
	clearResourceCache("res://objects/nodes/door.tscn")
	clearResourceCache("res://objects/nodes/keyHolder.tscn")
	clearResourceCache("res://objects/nodes/listeners/animationListener.tscn")
	clearResourceCache("res://objects/nodes/listeners/keyHolderChecker.tscn")
	clearResourceCache("res://objects/nodes/listeners/keyHolderResetterListener.tscn")
	clearResourceCache("res://objects/nodes/listeners/teleportListener.tscn")
	clearResourceCache("res://objects/nodes/listeners/visibilityListener.tscn")
	clearResourceCache("res://objects/nodes/listeners/worldportListener.tscn")
	clearResourceCache("res://objects/nodes/panel.tscn")
	clearResourceCache("res://objects/nodes/player.tscn")
	clearResourceCache("res://objects/nodes/saver.tscn")
	clearResourceCache("res://objects/nodes/teleport.tscn")
	clearResourceCache("res://objects/nodes/worldport.tscn")
	clearResourceCache("res://objects/scenes/menus/pause_menu.tscn")

	var paintings_dir = DirAccess.open("res://objects/meshes/paintings")
	if paintings_dir:
		paintings_dir.list_dir_begin()
		var file_name = paintings_dir.get_next()
		while file_name != "":
			if not paintings_dir.current_is_dir() and file_name.ends_with(".tscn"):
				clearResourceCache("res://objects/meshes/paintings/" + file_name)
			file_name = paintings_dir.get_next()

	switcher.switch_map.call_deferred("res://objects/scenes/the_entry.tscn")


func connectionUnsuccessful(error_message):
	$Panel/connect_button.disabled = false

	var popup = $Panel/AcceptDialog
	popup.title = "Could not connect to Archipelago"
	popup.dialog_text = error_message
	popup.exclusive = true
	popup.get_ok_button().visible = true
	popup.popup_centered()

	$Panel/connect_button.disabled = false


func versionMismatchDeclined():
	$Panel/AcceptDialog.hide()
	$Panel/connect_button.disabled = false


func historySelected(index):
	var ap = global.get_node("Archipelago")
	var details = ap.connection_history[index]

	$Panel/server_box.text = details[0]
	$Panel/player_box.text = details[1]
	$Panel/password_box.text = details[2]


func clearResourceCache(path):
	ResourceLoader.load(path, "", ResourceLoader.CACHE_MODE_REPLACE)