Fix: Remove TeleportTo calls that break villager AI/movement
- Remove post-load villager TeleportTo refresh (breaks pathfinding) - Remove periodic villager position sync (TeleportTo interrupts movement) - Keep ClearVillagerPositionCache for API compatibility The TeleportTo calls were resetting villager AI state and preventing them from continuing their movement/work. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -295,29 +295,6 @@ namespace KCM.LoadSaveOverrides
|
||||
}
|
||||
}
|
||||
|
||||
// Fix 2: Refresh all villagers to fix stuck AI
|
||||
Main.helper.Log("Refreshing villager states...");
|
||||
foreach (var kcPlayer in Main.kCPlayers.Values)
|
||||
{
|
||||
if (kcPlayer.inst == null) continue;
|
||||
|
||||
for (int i = 0; i < kcPlayer.inst.Workers.Count; i++)
|
||||
{
|
||||
var villager = kcPlayer.inst.Workers.data[i];
|
||||
if (villager == null) continue;
|
||||
|
||||
try
|
||||
{
|
||||
// Teleport to current position to reset pathfinding
|
||||
villager.TeleportTo(villager.Pos);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.helper.Log($"Error refreshing villager: {e.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Main.helper.Log("Post-load multiplayer fixes complete.");
|
||||
|
||||
return obj;
|
||||
|
||||
60
Main.cs
60
Main.cs
@@ -182,75 +182,17 @@ namespace KCM
|
||||
#endregion
|
||||
|
||||
public static int FixedUpdateInterval = 0;
|
||||
private static Dictionary<Guid, Vector3> lastVillagerPositions = new Dictionary<Guid, Vector3>();
|
||||
|
||||
public static void ClearVillagerPositionCache()
|
||||
{
|
||||
lastVillagerPositions.Clear();
|
||||
// Kept for API compatibility with LobbyManager
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
// Periodic villager position sync from server to clients
|
||||
// Sync every 150 frames (~3 seconds at 50 FPS) to reduce bandwidth
|
||||
if (KCServer.IsRunning && KCClient.client.IsConnected && FixedUpdateInterval % 150 == 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
SyncVillagerPositions();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
helper.Log($"Error in villager sync: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
FixedUpdateInterval++;
|
||||
}
|
||||
|
||||
private void SyncVillagerPositions()
|
||||
{
|
||||
// Only sync villagers that have moved significantly
|
||||
const float movementThreshold = 0.5f;
|
||||
|
||||
foreach (var kcPlayer in kCPlayers.Values)
|
||||
{
|
||||
if (kcPlayer.inst == null) continue;
|
||||
|
||||
for (int i = 0; i < kcPlayer.inst.Workers.Count; i++)
|
||||
{
|
||||
var villager = kcPlayer.inst.Workers.data[i];
|
||||
if (villager == null) continue;
|
||||
|
||||
Vector3 currentPos = villager.Pos;
|
||||
Vector3 lastPos;
|
||||
|
||||
bool shouldSync = false;
|
||||
if (!lastVillagerPositions.TryGetValue(villager.guid, out lastPos))
|
||||
{
|
||||
// First time seeing this villager
|
||||
lastVillagerPositions[villager.guid] = currentPos;
|
||||
shouldSync = true;
|
||||
}
|
||||
else if (Vector3.Distance(currentPos, lastPos) > movementThreshold)
|
||||
{
|
||||
// Villager has moved significantly
|
||||
lastVillagerPositions[villager.guid] = currentPos;
|
||||
shouldSync = true;
|
||||
}
|
||||
|
||||
if (shouldSync)
|
||||
{
|
||||
new Packets.Game.GameVillager.VillagerTeleportTo()
|
||||
{
|
||||
guid = villager.guid,
|
||||
pos = currentPos
|
||||
}.SendToAll(KCClient.client.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region "TransitionTo"
|
||||
public static void TransitionTo(MenuState state)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user