Disable time loop properly if u remove the warp core and warp away

This commit is contained in:
xen-42 2024-10-05 22:01:31 -04:00
parent fafb0a5df0
commit 2ad32409ab

View File

@ -5,8 +5,8 @@ using NewHorizons.Utility;
using NewHorizons.Utility.OWML; using NewHorizons.Utility.OWML;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
namespace NewHorizons.Handlers; namespace NewHorizons.Handlers;
@ -34,6 +34,8 @@ public static class HeldItemHandler
/// </summary> /// </summary>
private static string _currentStarSystem; private static string _currentStarSystem;
private const string ADVANCED_WARP_CORE = "TowerTwin_Body/Sector_TowerTwin/Sector_TimeLoopInterior/Interactables_TimeLoopInterior/WarpCoreSocket/Prefab_NOM_WarpCoreVessel";
[HarmonyPrefix, HarmonyPatch(typeof(ItemTool), nameof(ItemTool.Awake))] [HarmonyPrefix, HarmonyPatch(typeof(ItemTool), nameof(ItemTool.Awake))]
private static void Init() private static void Init()
{ {
@ -79,6 +81,15 @@ public static class HeldItemHandler
return newObject; return newObject;
} }
private static void TrackPath(string path)
{
if (!_pathOfItemTakenFromSystem.ContainsKey(Main.Instance.CurrentStarSystem))
{
_pathOfItemTakenFromSystem[Main.Instance.CurrentStarSystem] = new();
}
_pathOfItemTakenFromSystem[Main.Instance.CurrentStarSystem].Add(path);
}
private static void OnStarSystemChanging() private static void OnStarSystemChanging()
{ {
if (_currentlyHeldItem != null) if (_currentlyHeldItem != null)
@ -86,11 +97,7 @@ public static class HeldItemHandler
// Track it so that when we return to this system we can delete the original // Track it so that when we return to this system we can delete the original
if (_trackedPaths.TryGetValue(_currentlyHeldItem, out var path)) if (_trackedPaths.TryGetValue(_currentlyHeldItem, out var path))
{ {
if (!_pathOfItemTakenFromSystem.ContainsKey(Main.Instance.CurrentStarSystem)) TrackPath(path);
{
_pathOfItemTakenFromSystem[Main.Instance.CurrentStarSystem] = new();
}
_pathOfItemTakenFromSystem[Main.Instance.CurrentStarSystem].Add(path);
} }
NHLogger.Log($"Scene unloaded, preserved inactive held item {_currentlyHeldItem.name}"); NHLogger.Log($"Scene unloaded, preserved inactive held item {_currentlyHeldItem.name}");
@ -98,6 +105,17 @@ public static class HeldItemHandler
_currentlyHeldItem = MakePerfectCopy(_currentlyHeldItem).DontDestroyOnLoad(); _currentlyHeldItem = MakePerfectCopy(_currentlyHeldItem).DontDestroyOnLoad();
} }
// If warping with a vessel, make sure to also track the path to the advanced warp core (assuming the player actually removed it)
if (Main.Instance.CurrentStarSystem == "SolarSystem" && Main.Instance.IsWarpingFromVessel)
{
// Making sure its actually gone
var warpCoreSocket = GameObject.FindObjectOfType<TimeLoopCoreController>()._warpCoreSocket;
if (!warpCoreSocket.IsSocketOccupied() || warpCoreSocket.GetWarpCoreType() != WarpCoreType.Vessel)
{
TrackPath(ADVANCED_WARP_CORE);
}
}
_trackedPaths.Clear(); _trackedPaths.Clear();
_isSystemReady = false; _isSystemReady = false;
} }
@ -109,14 +127,32 @@ public static class HeldItemHandler
{ {
foreach (var path in paths) foreach (var path in paths)
{ {
NHLogger.Log($"Removing item that was taken from this system at {path}"); Delay.FireInNUpdates(() =>
var item = SearchUtilities.Find(path)?.GetComponent<OWItem>();
// Make sure to update the socket it might be in so that it works
if (item.GetComponentInParent<OWItemSocket>() is OWItemSocket socket)
{ {
socket.RemoveFromSocket(); try
} {
GameObject.Destroy(item.gameObject); NHLogger.Log($"Removing item that was taken from this system at {path}");
var item = SearchUtilities.Find(path)?.GetComponent<OWItem>();
// Make sure to update the socket it might be in so that it works
if (item.GetComponentInParent<OWItemSocket>() is OWItemSocket socket)
{
socket.RemoveFromSocket();
// Time loop core controller doesn't have time to hook up its events yet so we call this manually
if (path == ADVANCED_WARP_CORE)
{
var controller = GameObject.FindObjectOfType<TimeLoopCoreController>();
controller.OpenCore();
controller.OnSocketableRemoved(item);
}
NHLogger.Log($"Unsocketed {item.name}");
}
GameObject.Destroy(item.gameObject);
}
catch (Exception e)
{
NHLogger.LogError($"Failed to remove item at {path}: {e}");
}
}, 2);
} }
} }