Compare commits
4 Commits
aa6fb797c2
...
7a14303353
| Author | SHA1 | Date | |
|---|---|---|---|
| 7a14303353 | |||
| a918262d99 | |||
| 87f65320c0 | |||
| 97bbf059a9 |
102
Main.cs
102
Main.cs
@@ -181,6 +181,10 @@ namespace KCM
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public static int FixedUpdateInterval = 0;
|
public static int FixedUpdateInterval = 0;
|
||||||
|
private readonly Dictionary<Guid, Vector3> villagerPositionCache = new Dictionary<Guid, Vector3>();
|
||||||
|
private float villagerBroadcastAccumulator = 0f;
|
||||||
|
private const float VillagerBroadcastInterval = 0.25f;
|
||||||
|
private const float VillagerMovementThreshold = 0.3f;
|
||||||
|
|
||||||
public static void ClearVillagerPositionCache()
|
public static void ClearVillagerPositionCache()
|
||||||
{
|
{
|
||||||
@@ -190,6 +194,46 @@ namespace KCM
|
|||||||
private void FixedUpdate()
|
private void FixedUpdate()
|
||||||
{
|
{
|
||||||
FixedUpdateInterval++;
|
FixedUpdateInterval++;
|
||||||
|
BroadcastVillagerMovements();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void BroadcastVillagerMovements()
|
||||||
|
{
|
||||||
|
if (!KCServer.IsRunning || KCServer.server == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
villagerBroadcastAccumulator += Time.fixedDeltaTime;
|
||||||
|
if (villagerBroadcastAccumulator < VillagerBroadcastInterval)
|
||||||
|
return;
|
||||||
|
|
||||||
|
villagerBroadcastAccumulator = 0f;
|
||||||
|
if (Villager.villagers == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach (var villager in Villager.villagers.data)
|
||||||
|
{
|
||||||
|
if (villager == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Guid guid = villager.guid;
|
||||||
|
Component villagerComponent = (Component)(object)villager;
|
||||||
|
if (villagerComponent == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Vector3 currentPosition = villagerComponent.transform.position;
|
||||||
|
if (villagerPositionCache.TryGetValue(guid, out Vector3 lastPosition)
|
||||||
|
&& Vector3.Distance(lastPosition, currentPosition) < VillagerMovementThreshold)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
villagerPositionCache[guid] = currentPosition;
|
||||||
|
new VillagerTeleportTo()
|
||||||
|
{
|
||||||
|
guid = guid,
|
||||||
|
pos = currentPosition
|
||||||
|
}.SendToAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region "TransitionTo"
|
#region "TransitionTo"
|
||||||
@@ -1109,56 +1153,6 @@ 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"
|
||||||
@@ -1196,18 +1190,14 @@ namespace KCM
|
|||||||
public static bool Prefix(ref string __result)
|
public static bool Prefix(ref string __result)
|
||||||
{
|
{
|
||||||
Main.helper.Log("Get save dir");
|
Main.helper.Log("Get save dir");
|
||||||
if (KCClient.client.IsConnected)
|
if (KCServer.IsRunning || KCClient.client.IsConnected)
|
||||||
{
|
{
|
||||||
if (KCServer.IsRunning)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
__result = Application.persistentDataPath + "/Saves/Multiplayer";
|
__result = Application.persistentDataPath + "/Saves/Multiplayer";
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
__result = Application.persistentDataPath + "/Saves"; ;
|
__result = Application.persistentDataPath + "/Saves";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,16 +21,15 @@ namespace KCM.ServerLobby
|
|||||||
|
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
SetValues();
|
|
||||||
|
|
||||||
InvokeRepeating("SetValues", 0, 0.25f);
|
|
||||||
|
|
||||||
banner = transform.Find("PlayerBanner").GetComponent<RawImage>();
|
banner = transform.Find("PlayerBanner").GetComponent<RawImage>();
|
||||||
|
|
||||||
transform.Find("PlayerBanner").GetComponent<Button>().onClick.AddListener(() =>
|
transform.Find("PlayerBanner").GetComponent<Button>().onClick.AddListener(() =>
|
||||||
{
|
{
|
||||||
Main.TransitionTo(MenuState.NameAndBanner);//ChooseBannerUI Hooks required, as well as townnameui
|
Main.TransitionTo(MenuState.NameAndBanner);//ChooseBannerUI Hooks required, as well as townnameui
|
||||||
});
|
});
|
||||||
|
|
||||||
|
SetValues();
|
||||||
|
InvokeRepeating("SetValues", 0, 0.25f);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetValues()
|
public void SetValues()
|
||||||
|
|||||||
Reference in New Issue
Block a user