Skip to content

Commit

Permalink
Merge branch 'dev' into archipelago
Browse files Browse the repository at this point in the history
  • Loading branch information
alwaysintreble committed May 9, 2023
2 parents 87e58ef + 93ac272 commit cde2656
Show file tree
Hide file tree
Showing 13 changed files with 1,075 additions and 54 deletions.
36 changes: 28 additions & 8 deletions APRandomizerMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public override void Load()
On.InGameHud.OnGUI += InGameHud_OnGUI;
On.SaveManager.DoActualSaving += SaveManager_DoActualSave;
On.Quarble.OnPlayerDied += Quarble_OnPlayerDied;
// On.MegaTimeShard.NextState += RandoTimeShardManager.NextState;
// On.MegaTimeShard.ReceiveHit += RandoTimeShardManager.ReceiveHit;
On.MegaTimeShard.OnBreakDone += MegaTimeShard_OnBreakDone;
On.DialogSequence.GetDialogList += DialogSequence_GetDialogList;
On.LevelManager.EndLevelLoading += LevelManager_EndLevelLoading;
Expand Down Expand Up @@ -279,6 +281,12 @@ void InventoryManager_AddItem(On.InventoryManager.orig_AddItem orig, InventoryMa
}
if (randoStateManager.IsLocationRandomized(itemId, out var randoItemCheck))
{
if (itemId.Equals(EItems.CANDLE) &&
randoStateManager.IsLocationRandomized(EItems.TEA_SEED, out var seedCheck) &&
!RandomizerStateManager.HasCompletedCheck(seedCheck))
{
ItemsAndLocationsHandler.SendLocationCheck(seedCheck);
}
ItemsAndLocationsHandler.SendLocationCheck(randoItemCheck);
return;
}
Expand Down Expand Up @@ -323,11 +331,19 @@ bool HasItem_IsTrue(On.HasItem.orig_IsTrue orig, HasItem self)
//OLD WAY
//Don't actually check for the item i have, check to see if I have the item that was at it's location.
//int itemQuantity = Manager<InventoryManager>.Instance.GetItemQuantity(randoStateManager.CurrentLocationToItemMapping[check].Item);



//NEW WAY
//Don't actually check for the item I have, check to see if I have done this check before. We'll do this by seeing if the item at its location has been collected yet or not
int itemQuantity = RandomizerStateManager.HasCompletedCheck(check) ? 1 : 0;
if (self.item.Equals(EItems.CANDLE) && itemQuantity == 1)
{
var seed = ItemsAndLocationsHandler.LocationFromEItem(EItems.TEA_SEED);
var leaves = ItemsAndLocationsHandler.LocationFromEItem(EItems.TEA_LEAVES);
itemQuantity = RandomizerStateManager.HasCompletedCheck(seed) &&
RandomizerStateManager.HasCompletedCheck(leaves)
? 1
: 0;
}

switch (self.conditionOperator)
{
Expand Down Expand Up @@ -460,14 +476,13 @@ void SaveGameSelectionScreen_OnLoadGame(On.SaveGameSelectionScreen.orig_OnLoadGa
catch (Exception e)
{
Console.WriteLine(e);
orig(self, slotIndex);
}
//Generate the mappings based on the seed for the game if a seed was generated.
if (!ArchipelagoClient.HasConnected)
{
Console.WriteLine(
$"This file slot ({randoStateManager.CurrentFileSlot}) has no seed generated or is not " +
"a randomized file. Resetting the mappings and putting game items back to normal.");
ArchipelagoClient.ServerData = new ArchipelagoData();
}

orig(self, slotIndex);
Expand Down Expand Up @@ -536,7 +551,10 @@ void MegaTimeShard_OnBreakDone(On.MegaTimeShard.orig_OnBreakDone orig, MegaTimeS
var currentLevel = Manager<LevelManager>.Instance.GetCurrentLevelEnum();
var currentRoom = Manager<Level>.Instance.CurrentRoom.roomKey;
if (randoStateManager.MegaShards)
{
RandoTimeShardManager.BreakShard(new RandoTimeShardManager.MegaShard(currentLevel, currentRoom));
// self.shardsPerHit = 50;
}
orig(self);
}

Expand Down Expand Up @@ -721,9 +739,11 @@ void OnSelectArchipelagoConnect()
{
if (ArchipelagoClient.ServerData == null) return;
if (ArchipelagoClient.ServerData.SlotName == null) return;
if (ArchipelagoClient.ServerData.Uri == null) ArchipelagoClient.ServerData.Uri = "archipelago.gg";
if (ArchipelagoClient.ServerData.Port == 0) ArchipelagoClient.ServerData.Port = 38281;

if (ArchipelagoClient.ServerData.Uri == null)
ArchipelagoClient.ServerData.Uri = "archipelago.gg";
if (ArchipelagoClient.ServerData.Port == 0)
ArchipelagoClient.ServerData.Port = 38281;

ArchipelagoClient.ConnectAsync(archipelagoConnectButton);
}

Expand Down Expand Up @@ -856,14 +876,14 @@ private void SaveManager_DoActualSave(On.SaveManager.orig_DoActualSaving orig, S
{
ArchipelagoClient.UpdateClientStatus(ArchipelagoClientState.ClientGoal);
}
Save.Update();
if (randoStateManager.CurrentFileSlot == 0) return;
var saveSlot = self.GetCurrentSaveGameSlot();
if (!saveSlot.SlotName.Equals(ArchipelagoClient.ServerData.SlotName))
{
saveSlot.SlotName = ArchipelagoClient.ServerData.SlotName;
}
}
Save.Update();
orig(self, applySaveDelay);
}

Expand Down
30 changes: 8 additions & 22 deletions Archipelago/ArchipelagoClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using Archipelago.MultiClient.Net;
Expand Down Expand Up @@ -35,12 +34,16 @@ public static class ArchipelagoClient

public static void ConnectAsync()
{
if (attemptingConnection) return;
attemptingConnection = true;
Debug.Log($"Connecting to {ServerData.Uri}:{ServerData.Port} as {ServerData.SlotName}");
ThreadPool.QueueUserWorkItem(_ => Connect(OnConnected));
}

public static void ConnectAsync(SubMenuButtonInfo connectButton)
{
if (attemptingConnection) return;
attemptingConnection = true;
if (ServerData == null)
ServerData = new ArchipelagoData();
Debug.Log($"Connecting to {ServerData.Uri}:{ServerData.Port} as {ServerData.SlotName}");
Expand Down Expand Up @@ -80,12 +83,9 @@ private static void OnConnected(LoginResult connectResult, SubMenuButtonInfo con
attemptingConnection = false;
}

private static ArchipelagoSession CreateSession(bool secure)
private static ArchipelagoSession CreateSession()
{
var uri = secure ? "wss://" : "ws://";
uri += ServerData.Uri;
Console.WriteLine($"Creating session: {uri}:{ServerData.Port}");
var session = ArchipelagoSessionFactory.CreateSession(uri, ServerData.Port);
var session = ArchipelagoSessionFactory.CreateSession(ServerData.Uri, ServerData.Port);
session.MessageLog.OnMessageReceived += OnMessageReceived;
// session.Items.ItemReceived += ItemReceived;
session.Socket.ErrorReceived += SessionErrorReceived;
Expand All @@ -97,34 +97,20 @@ private static void Connect(OnConnectAttempt attempt)
{
if (Authenticated) return;
if (ItemsAndLocationsHandler.ItemsLookup == null) ItemsAndLocationsHandler.Initialize();
if (attemptingConnection) return;
attemptingConnection = true;

LoginResult result;

try
{
Console.WriteLine("Attempting wss Connection...");
Session = CreateSession(true);
Session = CreateSession();
result = Session.TryConnectAndLogin(
"The Messenger",
ServerData.SlotName,
ItemsHandlingFlags.AllItems,
new Version(ApVersion),
password: ServerData.Password == "" ? null : ServerData.Password
);
if (!result.Successful)
{
Console.WriteLine("Attempting ws Connection...");
Session = CreateSession(false);
result = Session.TryConnectAndLogin(
"The Messenger",
ServerData.SlotName,
ItemsHandlingFlags.AllItems,
new Version(ApVersion),
password: ServerData.Password == "" ? null : ServerData.Password
);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -222,7 +208,7 @@ private static void SessionSocketClosed(string reason)
public static void Disconnect()
{
Console.WriteLine("Disconnecting from server...");
Session?.Socket.DisconnectAsync();
Session?.Socket.Disconnect();
Session = null;
Authenticated = false;
}
Expand Down
6 changes: 3 additions & 3 deletions Archipelago/ItemsAndLocationsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,11 @@ public static void SendLocationCheck(long locationID)
Console.WriteLine($"Checking if we need to modify the location {locationID} before sending");
if (ArchipelagoClient.ServerData.CheckedLocations.Contains(locationID))
{
var locName = LocationsLookup.First(x => x.Value.Equals(locationID)).Key;
if (SpecialNames.TryGetValue(locName.LocationName, out var name))
var loc = LocationFromID(locationID);
if (SpecialNames.TryGetValue(loc.LocationName, out var name))
{
LocationsLookup.TryGetValue(new LocationRO(name,
(EItems)Enum.Parse(typeof(EItems), locName.PrettyLocationName)), out locationID);
(EItems)Enum.Parse(typeof(EItems), loc.PrettyLocationName)), out locationID);
}
else return;
}
Expand Down
166 changes: 164 additions & 2 deletions GameOverrideManagers/RandoLevelManager.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,173 @@
using System;
using MessengerRando.Utils;
using System.Collections.Generic;
using System.Linq;
using MessengerRando.Utils.Constants;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace MessengerRando.GameOverrideManagers
{
public class RandoLevelManager
public static class RandoLevelManager
{
private static bool teleporting;
private static ELevel lastLevel;
private static ELevel currentLevel;
public static readonly List<string> PlayedSpecialCutscenes = new List<string>();

public static Dictionary<LevelConstants.RandoLevel, LevelConstants.RandoLevel> RandoLevelMapping;

public static void LoadLevel(On.LevelManager.orig_LoadLevel orig, LevelManager self, LevelLoadingInfo levelInfo)
{
#if DEBUG
Console.WriteLine($"Current Level: {Manager<LevelManager>.Instance.GetCurrentLevelEnum()}");
Console.WriteLine($"Loading Level: {levelInfo.levelName}");
Console.WriteLine($"Entrance ID: {levelInfo.levelEntranceId}, Dimension: {levelInfo.dimension}");
#endif
orig(self, levelInfo);
if (RandoLevelMapping == null || teleporting) teleporting = false;
else
{
lastLevel = Manager<LevelManager>.Instance.GetCurrentLevelEnum();
currentLevel = Manager<LevelManager>.Instance.GetLevelEnumFromLevelName(levelInfo.levelName);
}
}

static bool WithinRange(float pos1, float pos2)
{
var comparison = pos2 - pos1;
if (comparison < 0) comparison *= -1;
return comparison <= 10;
}

private static LevelConstants.RandoLevel FindEntrance(out string entrance)
{
try
{
currentLevel = Manager<LevelManager>.Instance.GetCurrentLevelEnum();
var playerPos = Manager<PlayerManager>.Instance.Player.transform.position;
if (!LevelConstants.TransitionToEntranceName.TryGetValue(
new LevelConstants.Transition(lastLevel, currentLevel), out entrance))
return new LevelConstants.RandoLevel(ELevel.NONE, new Vector3());
LevelConstants.RandoLevel oldLevel = default;
if (LevelConstants.SpecialEntranceNames.Contains(entrance))
{

Vector3 comparePos;
switch (entrance)
{
case "Howling Grotto - Right":
comparePos = LevelConstants.EntranceNameToRandoLevel["Howling Grotto - Right"].PlayerPos;
if (WithinRange(playerPos.x, comparePos.x))
{
entrance = "Howling Grotto - Right";
oldLevel = LevelConstants.EntranceNameToRandoLevel[entrance];
}
else
{
entrance = "Howling Grotto - Bottom";
oldLevel = LevelConstants.EntranceNameToRandoLevel[entrance];
}

break;
case "Quillshroom Marsh - Left":
comparePos = LevelConstants.EntranceNameToRandoLevel["Quillshroom Marsh - Top Left"].PlayerPos;
if (WithinRange(playerPos.x, comparePos.x))
{
entrance = "Quillshroom Marsh - Top Left";
oldLevel = LevelConstants.EntranceNameToRandoLevel[entrance];
}
else
{
entrance = "Quillshroom Marsh - Bottom Left";
oldLevel = LevelConstants.EntranceNameToRandoLevel[entrance];
}

break;
case "Quillshroom Marsh - Right":
comparePos = LevelConstants.EntranceNameToRandoLevel["Quillshroom Marsh - Top Right"].PlayerPos;
if (WithinRange(playerPos.x, comparePos.x))
{
entrance = "Quillshroom Marsh - Top Right";
oldLevel = LevelConstants.EntranceNameToRandoLevel[entrance];
}
else
{
entrance = "Quillshroom Marsh - Bottom Right";
oldLevel = LevelConstants.EntranceNameToRandoLevel[entrance];
}

break;
case "Searing Crags - Left":
comparePos = LevelConstants.EntranceNameToRandoLevel["Searing Crags - Left"].PlayerPos;
if (WithinRange(playerPos.x, comparePos.x))
{
entrance = "Searing Crags - Left";
oldLevel = LevelConstants.EntranceNameToRandoLevel[entrance];
}
else
{
entrance = "Searing Crags - Bottom";
oldLevel = LevelConstants.EntranceNameToRandoLevel[entrance];
}

break;
}
}
else LevelConstants.EntranceNameToRandoLevel.TryGetValue(entrance, out oldLevel);

return oldLevel;
} catch (Exception e){ Console.WriteLine(e);}

entrance = string.Empty;
return new LevelConstants.RandoLevel(ELevel.NONE, new Vector3());
}

public static void EndLevelLoading(On.LevelManager.orig_EndLevelLoading orig, LevelManager self)
{
orig(self);
// i haven't figured out any way to teleport the player between boss rooms yet still
// this check is specifically for emerald golem since that boss exists on a transition screen
// if (RandoBossManager.OrigToNewBoss != null &&
// RandoRoomManager.IsBossRoom(Manager<Level>.Instance.CurrentRoom.roomKey, out var bossName))
// {
// if (RandoBossManager.OrigToNewBoss.TryGetValue(bossName, out bossName))
// {
// try
// {
// RandoBossManager.AdjustPlayerInBossRoom(bossName);
// }
// catch (Exception e)
// {
// Console.WriteLine(e);
// throw;
// }
// return;
// }
// }
if (teleporting) teleporting = false;

var oldLevel = FindEntrance(out var entrance);
if (RandoLevelMapping == null || !RandoLevelMapping.TryGetValue(oldLevel, out var newLevel)) return;
var actualEntrance = LevelConstants.EntranceNameToRandoLevel
.First(ret => ret.Value.Equals(newLevel)).Key;
EBits newDimension;
if (LevelConstants.Force16.Contains(actualEntrance)) newDimension = EBits.BITS_16;
else if (LevelConstants.Force8.Contains(actualEntrance)) newDimension = EBits.BITS_8;
else newDimension = Manager<DimensionManager>.Instance.currentDimension;

if (newLevel.LevelName.Equals(ELevel.Level_11_B_MusicBox) && RandomizerStateManager.Instance.SkipMusicBox)
SkipMusicBox();
else TeleportInArea(newLevel.LevelName, newLevel.PlayerPos, newDimension);
}

public static void PortalIntoArea(On.TotHQLevelInitializer.orig_InitLevel orig, TotHQLevelInitializer self,
Scene levelScene, ELevelEntranceID levelEntranceID, EBits dimension, bool positionPlayer,
LevelInitializerParams levelInitParams)
{
orig(self, levelScene, levelEntranceID, dimension, positionPlayer, levelInitParams);
}

public static void SkipMusicBox()
{
if (teleporting)
Expand All @@ -23,15 +183,17 @@ public static void SkipMusicBox()
TeleportInArea(ELevel.Level_11_B_MusicBox, playerPosition, EBits.BITS_16);
}

public static void TeleportInArea(ELevel area, Vector2 position, EBits dimension)
public static void TeleportInArea(ELevel area, Vector2 position, EBits dimension = EBits.NONE)
{
if (teleporting)
{
teleporting = false;
return;
}
Console.WriteLine($"Attempting to teleport to {area}, ({position.x}, {position.y}), {dimension}");
Manager<AudioManager>.Instance.StopMusic();
Manager<ProgressionManager>.Instance.checkpointSaveInfo.loadedLevelPlayerPosition = position;
if (dimension.Equals(EBits.NONE)) dimension = Manager<DimensionManager>.Instance.currentDimension;
LevelLoadingInfo levelLoadingInfo = new LevelLoadingInfo(area + "_Build",
true, true, LoadSceneMode.Single,
ELevelEntranceID.NONE, dimension);
Expand Down
Loading

0 comments on commit cde2656

Please sign in to comment.