fixed index out of bounds error on quantum states group

This commit is contained in:
FreezeDriedMangoes 2022-06-16 18:01:04 -04:00
parent 7124834232
commit 8ef12530d9
2 changed files with 90 additions and 3 deletions

View File

@ -52,10 +52,12 @@ namespace NewHorizons.Builder.Props
foreach(var prop in propsInGroup) foreach(var prop in propsInGroup)
{ {
prop.SetActive(false);
var quantumObject = prop.AddComponent<SocketedQuantumObject>(); var quantumObject = prop.AddComponent<SocketedQuantumObject>();
quantumObject._socketRoot = groupRoot; quantumObject._socketRoot = groupRoot;
quantumObject._socketList = sockets; quantumObject._socketList = sockets;
prop.SetActive(true);
if (prop.GetComponentInChildren<ShapeVisibilityTracker>() != null) continue; if (prop.GetComponentInChildren<ShapeVisibilityTracker>() != null) continue;
var boxBounds = GetBoundsOfSelfAndChildMeshes(prop); var boxBounds = GetBoundsOfSelfAndChildMeshes(prop);
@ -109,22 +111,28 @@ namespace NewHorizons.Builder.Props
empty.AddComponent<ShapeVisibilityTracker>(); empty.AddComponent<ShapeVisibilityTracker>();
} }
groupRoot.SetActive(false);
var multiState = groupRoot.AddComponent<MultiStateQuantumObject>(); var multiState = groupRoot.AddComponent<MultiStateQuantumObject>();
multiState._loop = quantumGroup.loop; multiState._loop = quantumGroup.loop;
multiState._sequential = quantumGroup.sequential; multiState._sequential = quantumGroup.sequential;
multiState._states = states.ToArray(); multiState._states = states.ToArray();
multiState._prerequisiteObjects = new MultiStateQuantumObject[0]; // TODO: support this
multiState._initialState = 0;
groupRoot.SetActive(true);
} }
public static void MakeShuffleGroup(GameObject go, Sector sector, PlanetConfig config, IModBehaviour mod, PropModule.QuantumGroupInfo quantumGroup, GameObject[] propsInGroup) public static void MakeShuffleGroup(GameObject go, Sector sector, PlanetConfig config, IModBehaviour mod, PropModule.QuantumGroupInfo quantumGroup, GameObject[] propsInGroup)
{ {
//var averagePosition = propsInGroup.Aggregate(Vector3.zero, (avg, prop) => avg + prop.transform.position) / propsInGroup.Count(); //var averagePosition = propsInGroup.Aggregate(Vector3.zero, (avg, prop) => avg + prop.transform.position) / propsInGroup.Count();
GameObject shuffleParent = new GameObject("Quantum Shuffle - " + quantumGroup.id); GameObject shuffleParent = new GameObject("Quantum Shuffle - " + quantumGroup.id);
shuffleParent.SetActive(false);
shuffleParent.transform.parent = sector.transform; shuffleParent.transform.parent = sector.transform;
shuffleParent.transform.localPosition = Vector3.zero; shuffleParent.transform.localPosition = Vector3.zero;
propsInGroup.ToList().ForEach(p => p.transform.parent = shuffleParent.transform); propsInGroup.ToList().ForEach(p => p.transform.parent = shuffleParent.transform);
var shuffle = shuffleParent.AddComponent<QuantumShuffleObject>(); var shuffle = shuffleParent.AddComponent<QuantumShuffleObject>();
shuffle._shuffledObjects = propsInGroup.Select(p => p.transform).ToArray(); shuffle._shuffledObjects = propsInGroup.Select(p => p.transform).ToArray();
NewHorizons.Utility.Logger.Log($"Shuffled objects {shuffle._shuffledObjects}, propsInGroup count {propsInGroup.Count()}");
shuffle.Awake(); // this doesn't get called on its own for some reason shuffle.Awake(); // this doesn't get called on its own for some reason
var boxBounds = GetBoundsOfSelfAndChildMeshes(shuffleParent); // TODO: add a box shape to each prop instead of to the parent var boxBounds = GetBoundsOfSelfAndChildMeshes(shuffleParent); // TODO: add a box shape to each prop instead of to the parent
@ -133,6 +141,7 @@ namespace NewHorizons.Builder.Props
boxShape.extents = boxBounds.size; boxShape.extents = boxBounds.size;
shuffleParent.AddComponent<ShapeVisibilityTracker>(); shuffleParent.AddComponent<ShapeVisibilityTracker>();
shuffleParent.SetActive(true);
} }
public static Bounds GetBoundsOfSelfAndChildMeshes(GameObject g) public static Bounds GetBoundsOfSelfAndChildMeshes(GameObject g)

View File

@ -14,8 +14,8 @@ namespace NewHorizons.Patches
// This is some dark magic // This is some dark magic
// this creates a method called base_DropItem that basically just calls OWItem.PickUpItem whenever it (VisionTorchItemPatches.base_PickUpItem) is called // this creates a method called base_DropItem that basically just calls OWItem.PickUpItem whenever it (VisionTorchItemPatches.base_PickUpItem) is called
[HarmonyReversePatch] [HarmonyReversePatch]
[HarmonyPatch(typeof(QuantumObject), nameof(QuantumObject.Awake))] [HarmonyPatch(typeof(VisibilityObject), nameof(VisibilityObject.Awake))]
private static void base_Awake(QuantumObject __instance) { } private static void base_Awake(VisibilityObject __instance) { }
// Make the vision torch droppable. In the base game you can only drop it if you're in the dream world. // Make the vision torch droppable. In the base game you can only drop it if you're in the dream world.
@ -33,6 +33,84 @@ namespace NewHorizons.Patches
return false; return false;
} }
[HarmonyPrefix]
[HarmonyPatch(typeof(MultiStateQuantumObject), nameof(MultiStateQuantumObject.ChangeQuantumState))]
public static bool MultiStateQuantumObject_ChangeQuantumState(MultiStateQuantumObject __instance, bool skipInstantVisibilityCheck)
{
for (int i = 0; i < __instance._prerequisiteObjects.Length; i++)
{
if (!__instance._prerequisiteObjects[i].HasCollapsed())
{
return false;
}
}
int stateIndex = __instance._stateIndex;
if (__instance._stateIndex == -1 && __instance._initialState != -1)
{
__instance._stateIndex = __instance._initialState;
}
else if (__instance._sequential)
{
__instance._stateIndex = (__instance._reverse ? (__instance._stateIndex - 1) : (__instance._stateIndex + 1));
if (__instance._loop)
{
if (__instance._stateIndex < 0)
{
__instance._stateIndex = __instance._states.Length - 1;
}
else if (__instance._stateIndex > __instance._states.Length - 1)
{
__instance._stateIndex = 0;
}
}
else
{
__instance._stateIndex = Mathf.Clamp(__instance._stateIndex, 0, __instance._states.Length - 1);
}
}
else
{
int num = 0;
for (int j = 0; j < __instance._states.Length; j++)
{
if (j != stateIndex)
{
__instance._probabilities[j] = __instance._states[j].GetProbability();
num += __instance._probabilities[j];
}
}
int num2 = UnityEngine.Random.Range(0, num);
int num3 = 0;
int num4 = 0;
for (int k = 0; k < __instance._states.Length; k++)
{
if (k != stateIndex)
{
num3 = num4;
num4 += __instance._probabilities[k];
if (__instance._probabilities[k] > 0 && num2 >= num3 && num2 < num4)
{
__instance._stateIndex = k;
break;
}
}
}
}
if (stateIndex != __instance._stateIndex && stateIndex >= 0 && stateIndex < __instance._states.Length)
{
__instance._states[stateIndex].SetVisible(visible: false);
}
NewHorizons.Utility.Logger.Log($"states length {__instance._states.Length} index {__instance._stateIndex}");
__instance._states[__instance._stateIndex].SetVisible(visible: true);
if (__instance._sequential && !__instance._loop && __instance._stateIndex == __instance._states.Length - 1)
{
__instance.SetActivation(active: false);
}
return true;
}
} }
} }