Files
zzz_REBIRTH__Utils/Harmony/Harmony_SpawnPointList.cs
2025-06-04 16:44:53 +09:30

200 lines
8.9 KiB
C#

using Audio;
using System.Collections.Generic;
using XMLEditing;
using static EntityDrone;
namespace Harmony.SpawnPointListPatches
{
[HarmonyPatch(typeof(SpawnPointList))]
[HarmonyPatch("GetRandomSpawnPosition")]
public class GetRandomSpawnPositionPatch
{
public static bool Prefix(SpawnPointList __instance, ref SpawnPosition __result, World _world, Vector3? _refPosition = null, int _minDistance = 0, int _maxDistance = 0)
{
//Log.Out("===================================================================");
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition START");
//Log.Out("===================================================================");
if (_world.IsEditor() || GameUtils.IsWorldEditor() || GameUtils.IsPlaytesting())
{
return true;
}
if (__instance.Count > 0)
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition 1, __instance.Count: " + __instance.Count);
bool bFound = false;
string biome = RebirthVariables.customBiomeSpawn;
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition biome: " + biome);
if (RebirthUtilities.ScenarioSkip())
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition HIVE");
List<PrefabInstance> prefabInstances = new List<PrefabInstance> ();
foreach (string prefabName in RebirthVariables.hivePrefab)
{
var matchingInstances = GameManager.Instance.World.ChunkClusters[0].ChunkProvider
.GetDynamicPrefabDecorator()
.GetDynamicPrefabs()
.FindAll(instance => instance.name.Contains(prefabName));
prefabInstances.AddRange(matchingInstances);
}
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition prefabInstances.Count: " + prefabInstances.Count);
if (prefabInstances.Count > 0)
{
List<PrefabInstance> validPrefabs = new List<PrefabInstance>();
foreach (PrefabInstance prefab in prefabInstances)
{
Vector2 prefabPosition = new Vector2((float)prefab.boundingBoxPosition.x + (float)prefab.boundingBoxSize.x / 2f, (float)prefab.boundingBoxPosition.z + (float)prefab.boundingBoxSize.z / 2f);
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition prefabPosition: " + prefabPosition);
BiomeDefinition biomeAt = GameManager.Instance.World.ChunkCache.ChunkProvider.GetBiomeProvider().GetBiomeAt((int)prefabPosition.x, (int)prefabPosition.y);
if (biomeAt != null && biomeAt.m_sBiomeName == "pine_forest")
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition biomeAt.m_sBiomeName: " + biomeAt.m_sBiomeName);
validPrefabs.Add(prefab);
}
}
if (validPrefabs.Count > 0)
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition PARSING VALID PREFABS: " + validPrefabs.Count);
int random = Manager.random.RandomRange(0, validPrefabs.Count);
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition random: " + random);
PrefabInstance selectedPrefab = validPrefabs[random];
Vector3 prefabPosition = selectedPrefab.boundingBoxPosition;
if (selectedPrefab != null)
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition selectedPrefab.boundingBoxPosition: " + selectedPrefab.boundingBoxPosition);
__result = new SpawnPosition(selectedPrefab.boundingBoxPosition, 0f);
return false;
}
}
}
}
if (biome == "random")
{
GameRandom gameRandom = _world.GetGameRandom();
if (_refPosition == null)
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition RANDOM BIOME 1");
__result = __instance[gameRandom.RandomRange(__instance.Count)].spawnPosition;
return false;
}
Vector3 value = _refPosition.Value;
for (int i = 0; i < 100; i++)
{
int index = gameRandom.RandomRange(__instance.Count);
SpawnPosition spawnPosition = __instance[index].spawnPosition;
float magnitude = (spawnPosition.position - value).magnitude;
if (magnitude >= (float)_minDistance && magnitude <= (float)_maxDistance)
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition RANDOM BIOME 2");
__result = spawnPosition;
return false;
}
}
float num = float.MaxValue;
int num2 = -1;
for (int j = 0; j < __instance.Count; j++)
{
float sqrMagnitude = (__instance[j].spawnPosition.position - value).sqrMagnitude;
if (sqrMagnitude < num)
{
num = sqrMagnitude;
num2 = j;
}
}
if (num2 != -1)
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition RANDOM BIOME 3");
__result = __instance[num2].spawnPosition;
return false;
}
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition RANDOM BIOME 4");
__result = SpawnPosition.Undef;
return false;
}
if (!GameUtils.IsPlaytesting() && biome != "randomtrader")
{
for (int j = 0; j < __instance.Count; j++)
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition 2, j: " + j + " / position: " + __instance[j].spawnPosition);
string biomeName = RebirthUtilities.GetBiomeName(__instance[j].spawnPosition.ToBlockPos());
if (biomeName != "")
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition A, biomeAt.m_Id: " + biomeAt.m_Id);
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition A, biomeName: " + biomeName);
bool isValidBiome = false;
if (biomeName == biome)
{
isValidBiome = true;
}
else if (biomeName == "pine_forest" && biome == "forest")
{
isValidBiome = true;
}
else if (biomeName == "burnt_forest" && biome == "burnt")
{
isValidBiome = true;
}
if (isValidBiome)
{
__result = __instance[j].spawnPosition;
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition B, __result.position: " + __result.position);
return false;
}
}
}
}
if (!bFound)
{
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition 5");
int index = UnityEngine.Random.Range(0, __instance.Count - 1);
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition 6, index: " + index);
__result = __instance[index].spawnPosition;
int height = (int)GameManager.Instance.World.GetHeightAt(__result.position.x, __result.position.y);
__result.position.y = height;
//Log.Out("SpawnPointListPatches-GetRandomSpawnPosition 7, __result.position: " + __result.position);
return false;
}
}
__result = SpawnPosition.Undef;
return false;
}
}
}