Compare commits
5 Commits
b05c3415f2
...
739eba8289
| Author | SHA1 | Date | |
|---|---|---|---|
| 739eba8289 | |||
| 1e6f09df18 | |||
| 15cad47b52 | |||
| c074a86423 | |||
| 1035f06884 |
50
Main.cs
50
Main.cs
@@ -1109,6 +1109,56 @@ namespace KCM
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HarmonyPatch(typeof(Villager), "Update")]
|
||||||
|
public class VillagerMovementSync
|
||||||
|
{
|
||||||
|
private struct MovementSnapshot
|
||||||
|
{
|
||||||
|
public Vector3 position;
|
||||||
|
public float timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static readonly Dictionary<Guid, MovementSnapshot> lastSnapshots = new Dictionary<Guid, MovementSnapshot>();
|
||||||
|
private const float MovementThreshold = 0.3f;
|
||||||
|
private const float ForceSendInterval = 0.5f;
|
||||||
|
|
||||||
|
public static void Postfix(Villager __instance)
|
||||||
|
{
|
||||||
|
if (!KCServer.IsRunning || KCServer.server == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (__instance == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Guid guid = __instance.guid;
|
||||||
|
Component villagerComponent = (Component)(object)__instance;
|
||||||
|
if (villagerComponent == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Vector3 currentPosition = villagerComponent.transform.position;
|
||||||
|
float now = Time.time;
|
||||||
|
|
||||||
|
if (lastSnapshots.TryGetValue(guid, out MovementSnapshot snapshot))
|
||||||
|
{
|
||||||
|
float distance = Vector3.Distance(snapshot.position, currentPosition);
|
||||||
|
if (distance < MovementThreshold && (now - snapshot.timestamp) < ForceSendInterval)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastSnapshots[guid] = new MovementSnapshot
|
||||||
|
{
|
||||||
|
position = currentPosition,
|
||||||
|
timestamp = now
|
||||||
|
};
|
||||||
|
|
||||||
|
new VillagerTeleportTo()
|
||||||
|
{
|
||||||
|
guid = guid,
|
||||||
|
pos = currentPosition
|
||||||
|
}.SendToAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region "Job Hooks"
|
#region "Job Hooks"
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
@@ -12,15 +11,27 @@ namespace KCM
|
|||||||
private static MethodInfo isPrivateMethod;
|
private static MethodInfo isPrivateMethod;
|
||||||
private static MethodInfo addResourceStorageMethod;
|
private static MethodInfo addResourceStorageMethod;
|
||||||
|
|
||||||
|
private static readonly Assembly[] candidateAssemblies = new[]
|
||||||
|
{
|
||||||
|
typeof(Player).Assembly,
|
||||||
|
typeof(World).Assembly,
|
||||||
|
typeof(FreeResourceManager).Assembly,
|
||||||
|
};
|
||||||
|
|
||||||
private static void EnsureInitialized()
|
private static void EnsureInitialized()
|
||||||
{
|
{
|
||||||
if (resourceStorageType != null)
|
if (resourceStorageType != null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
resourceStorageType = AppDomain.CurrentDomain
|
foreach (var assembly in candidateAssemblies)
|
||||||
.GetAssemblies()
|
{
|
||||||
.Select(a => a.GetType("Assets.Interface.IResourceStorage", false))
|
if (assembly == null)
|
||||||
.FirstOrDefault(t => t != null);
|
continue;
|
||||||
|
|
||||||
|
resourceStorageType = assembly.GetType("Assets.Interface.IResourceStorage", false);
|
||||||
|
if (resourceStorageType != null)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (resourceStorageType == null)
|
if (resourceStorageType == null)
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user