forked from billw2012/BetterContinents
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BetterContinents.ZoneSystemPatch.cs
79 lines (74 loc) · 3.1 KB
/
BetterContinents.ZoneSystemPatch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using UnityEngine;
namespace BetterContinents;
public partial class BetterContinents
{
[HarmonyPatch(typeof(ZoneSystem))]
private class ZoneSystemPatch
{
[HarmonyPostfix, HarmonyPatch(nameof(ZoneSystem.Load))]
private static void LoadPostfix(ZoneSystem __instance)
{
if (Settings.EnabledForThisWorld)
{
if (!__instance.m_locationsGenerated && __instance.m_locationInstances.Count > 0)
{
LogWarning("Skipping automatic genloc, use the command manually if needed.");
__instance.m_locationsGenerated = true;
}
}
}
// Changes to location type spawn placement (this is the functional part of the mod)
[HarmonyPostfix, HarmonyPatch(nameof(ZoneSystem.ClearNonPlacedLocations), []), HarmonyPriority(Priority.Low)]
private static void ClearNonPlacedLocationsPostfix(ZoneSystem __instance)
{
if (!Settings.EnabledForThisWorld) return;
if (!Settings.HasLocationMap && !Settings.OverrideStartPosition) return;
List<ZoneSystem.ZoneLocation> locs = [.. __instance.m_locations.Where(loc => loc.m_enable && loc.m_quantity != 0).OrderByDescending(x => x.m_prioritized)];
if (Settings.HasLocationMap)
{
foreach (var loc in locs)
HandleLocation(loc);
}
if (Settings.OverrideStartPosition)
{
var startLoc = locs.FirstOrDefault(loc => loc.m_prefabName == "StartTemple");
if (startLoc != null)
{
var y = WorldGenerator.instance.GetHeight(Settings.StartPositionX, Settings.StartPositionY);
Vector3 position = new(Settings.StartPositionX, y, Settings.StartPositionY);
__instance.RegisterLocation(startLoc, position, false);
Log($"Start position overriden: set to {position}");
}
}
}
private static void HandleLocation(ZoneSystem.ZoneLocation loc)
{
var groupName = string.IsNullOrEmpty(loc.m_group) ? "<unnamed>" : loc.m_group;
Log($"Generating location of group {groupName}, required {loc.m_quantity}, unique {loc.m_unique}, name {loc.m_prefabName}");
// Place all locations specified by the spawn map, ignoring counts specified in the prefab
int placed = 0;
foreach (var normalizedPosition in Settings.GetAllSpawns(loc.m_prefabName))
{
var worldPos = NormalizedToWorld(normalizedPosition);
var position = new Vector3(
worldPos.x,
WorldGenerator.instance.GetHeight(worldPos.x, worldPos.y),
worldPos.y
);
ZoneSystem.instance.RegisterLocation(loc, position, false);
Log($"Position of {loc.m_prefabName} ({++placed}/{loc.m_quantity}) overriden: set to {position}");
}
}
[HarmonyPrefix, HarmonyPatch(nameof(ZoneSystem.CountNrOfLocation))]
private static bool CountNrOfLocation(ZoneSystem.ZoneLocation location, ref int __result)
{
if (!ConfigDebugSkipDefaultLocationPlacement.Value) return true;
if (location.m_prefabName == "StartTemple") return true;
__result = location.m_quantity;
return false;
}
}
}