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
|
||||
|
||||
#region "Job Hooks"
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -12,15 +11,27 @@ namespace KCM
|
||||
private static MethodInfo isPrivateMethod;
|
||||
private static MethodInfo addResourceStorageMethod;
|
||||
|
||||
private static readonly Assembly[] candidateAssemblies = new[]
|
||||
{
|
||||
typeof(Player).Assembly,
|
||||
typeof(World).Assembly,
|
||||
typeof(FreeResourceManager).Assembly,
|
||||
};
|
||||
|
||||
private static void EnsureInitialized()
|
||||
{
|
||||
if (resourceStorageType != null)
|
||||
return;
|
||||
|
||||
resourceStorageType = AppDomain.CurrentDomain
|
||||
.GetAssemblies()
|
||||
.Select(a => a.GetType("Assets.Interface.IResourceStorage", false))
|
||||
.FirstOrDefault(t => t != null);
|
||||
foreach (var assembly in candidateAssemblies)
|
||||
{
|
||||
if (assembly == null)
|
||||
continue;
|
||||
|
||||
resourceStorageType = assembly.GetType("Assets.Interface.IResourceStorage", false);
|
||||
if (resourceStorageType != null)
|
||||
break;
|
||||
}
|
||||
|
||||
if (resourceStorageType == null)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user