Skip to content

Commit de44914

Browse files
committed
New feature: Configurable build range multiplier
1 parent 5bb159b commit de44914

9 files changed

+120
-8
lines changed

Tobey.SnapBuilder.sln

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tobey.SnapBuilder", "Tobey.
77
EndProject
88
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "BepInEx.ConfigurationManager", "BepInEx.ConfigurationManager\BepInEx.ConfigurationManager\BepInEx.ConfigurationManager.shproj", "{F9BC4E11-B6B8-478A-9D3A-D9A42DAF70D1}"
99
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E91209C9-6AF7-41FC-B274-9FD0047A337B}"
11+
ProjectSection(SolutionItems) = preProject
12+
README.md = README.md
13+
EndProjectSection
14+
EndProject
1015
Global
1116
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1217
Debug|Any CPU = Debug|Any CPU

Tobey.SnapBuilder/AimTransform.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ private void OnDestroy()
4949
Destroy(GetComponent<ColliderCache>());
5050
}
5151

52-
private static PlaceTool GetPlaceTool() => Inventory.main?.GetHeld()?.GetComponent<PlaceTool>();
52+
internal static BuilderTool GetBuilderTool() => Inventory.main?.GetHeld()?.GetComponent<BuilderTool>();
53+
internal static PlaceTool GetPlaceTool() => Inventory.main?.GetHeld()?.GetComponent<PlaceTool>();
5354

5455
public static bool Raycast(Vector3 from, Vector3 direction, out RaycastHit hit)
5556
{
@@ -60,7 +61,7 @@ public static bool Raycast(Vector3 from, Vector3 direction, out RaycastHit hit)
6061
}
6162
else
6263
{
63-
int max = UWE.Utils.RaycastIntoSharedBuffer(from, direction, 5f, -5, QueryTriggerInteraction.UseGlobal);
64+
int max = UWE.Utils.RaycastIntoSharedBuffer(from, direction, 5f * General.BuildRangeMultiplier.Value, -5, QueryTriggerInteraction.UseGlobal);
6465
var hits = new ArraySegment<RaycastHit>(UWE.Utils.sharedHitBuffer, 0, max)
6566
.Where(h => !h.collider.isTrigger && !UWE.Utils.SharingHierarchy(placeTool.gameObject, h.collider.gameObject))
6667
.OrderBy(h => h.distance);

Tobey.SnapBuilder/Config.cs

+8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ internal static void Initialise() { }
8080
defaultValue: true,
8181
description: "Whether detailed colliders are rendered while using the Habitat Builder."
8282
);
83+
84+
public static ConfigEntry<float> BuildRangeMultiplier { get; } =
85+
Cfg.Bind(
86+
section: nameof(General),
87+
key: "Build range multiplier",
88+
defaultValue: 1.75f,
89+
description: "The multiplier applied to the build range when using the Habitat Builder."
90+
);
8391
}
8492

8593
public static class Keybinds

Tobey.SnapBuilder/Patches/BuilderPatch.cs

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ public static void BeginHintsPostfix(bool __state)
4545
public static void BeginResetTogglesPostFix() => Toggles.Reset();
4646
#endregion
4747

48+
#region Builder.CreateGhost
49+
[HarmonyPatch(typeof(Builder), nameof(Builder.CreateGhost))]
50+
[HarmonyPostfix, HarmonyWrapSafe]
51+
public static void CreateGhostPostfix()
52+
{
53+
var constructable = Builder.prefab.GetComponent<Constructable>();
54+
if (constructable != null && !ConstructablePatch.constructableDistances.ContainsKey(constructable))
55+
{
56+
ConstructablePatch.constructableDistances.Add(constructable, new(constructable.placeDefaultDistance, constructable.placeMaxDistance));
57+
Builder.placeDefaultDistance = constructable.placeDefaultDistance *= General.BuildRangeMultiplier.Value;
58+
Builder.placeMaxDistance = constructable.placeMaxDistance *= General.BuildRangeMultiplier.Value;
59+
}
60+
else
61+
{
62+
Builder.placeDefaultDistance = ConstructablePatch.constructableDistances[constructable].Item1 * General.BuildRangeMultiplier.Value;
63+
Builder.placeMaxDistance = ConstructablePatch.constructableDistances[constructable].Item2 * General.BuildRangeMultiplier.Value;
64+
}
65+
}
66+
#endregion
67+
4868
#region Builder.GetAimTransform
4969
[HarmonyPatch(typeof(Builder), nameof(Builder.GetAimTransform))]
5070
[HarmonyPostfix, HarmonyWrapSafe]

Tobey.SnapBuilder/Patches/BuilderTool_GetCustomUseText_Patch.cs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Reflection;
65

76
namespace Tobey.SnapBuilder.Patches;
87
using static Config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using HarmonyLib;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace Tobey.SnapBuilder.Patches;
6+
using static Config;
7+
internal static class ConstructablePatch
8+
{
9+
public static Dictionary<Constructable, Tuple<float, float>> constructableDistances = new();
10+
11+
[HarmonyPatch(typeof(Constructable), nameof(Constructable.Awake))]
12+
[HarmonyPostfix, HarmonyWrapSafe]
13+
public static void Awake_Postfix(Constructable __instance)
14+
{
15+
if (!constructableDistances.ContainsKey(__instance))
16+
{
17+
constructableDistances.Add(__instance, new(__instance.placeDefaultDistance, __instance.placeMaxDistance));
18+
__instance.placeDefaultDistance *= General.BuildRangeMultiplier.Value;
19+
__instance.placeMaxDistance *= General.BuildRangeMultiplier.Value;
20+
}
21+
}
22+
23+
[HarmonyPatch(typeof(Constructable), nameof(Constructable.OnDestroy))]
24+
[HarmonyPrefix, HarmonyWrapSafe]
25+
public static void OnDestroy_Prefix(Constructable __instance)
26+
{
27+
if (constructableDistances.ContainsKey(__instance))
28+
{
29+
constructableDistances.Remove(__instance);
30+
}
31+
}
32+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using HarmonyLib;
2+
using System;
3+
using UnityEngine;
4+
5+
namespace Tobey.SnapBuilder.Patches;
6+
using static Config;
7+
internal static class PhysicsPatch
8+
{
9+
[HarmonyPatch(typeof(UWE.Utils), nameof(UWE.Utils.RaycastIntoSharedBuffer))]
10+
[HarmonyPatch(new Type[] { typeof(Vector3), typeof(Vector3), typeof(float), typeof(int), typeof(QueryTriggerInteraction) })]
11+
[HarmonyPrefix, HarmonyWrapSafe]
12+
public static void RaycastIntoSharedBufferPrefix(Vector3 origin, Vector3 direction, ref float maxDistance)
13+
{
14+
if (AimTransform.GetPlaceTool() != null
15+
&& Vector3.Distance(origin, AimTransform.Instance.transform.position) <= Vector3.kEpsilon
16+
&& Vector3.Distance(direction, AimTransform.Instance.transform.forward) <= Vector3.kEpsilon)
17+
{
18+
maxDistance *= General.BuildRangeMultiplier.Value;
19+
}
20+
}
21+
22+
[HarmonyPatch(typeof(Targeting), nameof(Targeting.GetTarget))]
23+
[HarmonyPatch(
24+
argumentTypes: new Type[] { typeof(float), typeof(GameObject), typeof(float) },
25+
argumentVariations: new ArgumentType[] { ArgumentType.Normal, ArgumentType.Out, ArgumentType.Out })]
26+
[HarmonyPrefix, HarmonyWrapSafe]
27+
public static void GetTargetPrefix(ref float maxDistance)
28+
{
29+
if (AimTransform.GetBuilderTool() != null)
30+
{
31+
maxDistance *= General.BuildRangeMultiplier.Value;
32+
}
33+
}
34+
}

Tobey.SnapBuilder/SnapBuilder.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ private void Awake()
3939
}
4040
}
4141

42-
private void Start() => Localisation.Initialise();
43-
4442
private void OnEnable()
4543
{
4644
General.Initialise();
@@ -49,19 +47,34 @@ private void OnEnable()
4947
Toggles.Initialise();
5048
Localisation.Initialise();
5149

50+
General.BuildRangeMultiplier.SettingChanged -= BuildRangeMultiplier_SettingChanged;
51+
General.BuildRangeMultiplier.SettingChanged += BuildRangeMultiplier_SettingChanged;
52+
5253
Toggles.Bind();
5354
ApplyHarmonyPatches();
5455
}
5556

57+
private void BuildRangeMultiplier_SettingChanged(object sender, EventArgs e)
58+
{
59+
foreach (var constructable in ConstructablePatch.constructableDistances)
60+
{
61+
constructable.Key.placeDefaultDistance = constructable.Value.Item1 * General.BuildRangeMultiplier.Value;
62+
constructable.Key.placeMaxDistance = constructable.Value.Item2 * General.BuildRangeMultiplier.Value;
63+
}
64+
}
65+
5666
private void ApplyHarmonyPatches()
5767
{
5868
Harmony.PatchAll(typeof(BuilderPatch));
5969
Harmony.PatchAll(typeof(PlaceToolPatch));
6070
Harmony.PatchAll(typeof(BuilderTool_GetCustomUseText_Patch));
71+
Harmony.PatchAll(typeof(PhysicsPatch));
72+
Harmony.PatchAll(typeof(ConstructablePatch));
6173
}
6274

6375
private void OnDisable()
6476
{
77+
General.BuildRangeMultiplier.SettingChanged -= BuildRangeMultiplier_SettingChanged;
6578
Toggles.Unbind();
6679
Harmony.UnpatchSelf();
6780
Destroy(AimTransform.Instance);

Tobey.SnapBuilder/Tobey.SnapBuilder.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net472</TargetFramework>
55
<AssemblyName>Tobey.SnapBuilder</AssemblyName>
66
<Description>Snap-to-Grid for Subnautica!</Description>
7-
<Version>2.2.1</Version>
7+
<Version>2.3.0</Version>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
99
<LangVersion>10.0</LangVersion>
1010
<Product>SnapBuilder</Product>
@@ -28,11 +28,11 @@
2828

2929
<ItemGroup>
3030
<Reference Include="Assembly-CSharp-firstpass">
31-
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\Subnautica.experimental\Subnautica_Data\Managed\publicized-assemblies\Assembly-CSharp-firstpass-publicized.dll</HintPath>
31+
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\Subnautica\Subnautica_Data\Managed\publicized\Assembly-CSharp-firstpass-publicized.dll</HintPath>
3232
<Private>False</Private>
3333
</Reference>
3434
<Reference Include="Assembly-CSharp">
35-
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\Subnautica.experimental\Subnautica_Data\Managed\publicized-assemblies\Assembly-CSharp-publicized.dll</HintPath>
35+
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\Subnautica\Subnautica_Data\Managed\publicized\Assembly-CSharp-publicized.dll</HintPath>
3636
<Private>False</Private>
3737
</Reference>
3838
</ItemGroup>

0 commit comments

Comments
 (0)