From c3223d5db95b9bf3415cec9899501ba0fd570579 Mon Sep 17 00:00:00 2001 From: devbeni Date: Sat, 13 Dec 2025 23:18:30 +0100 Subject: [PATCH] =?UTF-8?q?na=20most=20elvileg=20a=20grok=20fix=C3=A1lta?= =?UTF-8?q?=20de=20nem=20hinnem=20xd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main.cs | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 140 insertions(+), 10 deletions(-) diff --git a/Main.cs b/Main.cs index 7022717..19dd3c5 100644 --- a/Main.cs +++ b/Main.cs @@ -350,6 +350,47 @@ namespace KCM }*/ FixedUpdateInterval++; + + // Force AI updates in multiplayer every few frames + if (KCClient.client.IsConnected && FixedUpdateInterval % 60 == 0 && Time.timeScale > 0) + { + ForceMultiplayerAIUpdate(); + } + } + + private static void ForceMultiplayerAIUpdate() + { + try + { + foreach (var player in kCPlayers.Values) + { + if (player?.inst == null) continue; + + // Force villager AI updates + for (int i = 0; i < player.inst.Workers.Count; i++) + { + Villager v = player.inst.Workers.data[i]; + if (v != null && v.brain != null) + { + v.brain.Think(); + } + } + + // Force homeless to find jobs + for (int i = 0; i < player.inst.Homeless.Count; i++) + { + Villager v = player.inst.Homeless.data[i]; + if (v != null && v.workerJob == null) + { + JobSystem.inst?.TryAssignJob(v); + } + } + } + } + catch (Exception e) + { + helper?.Log("Error in ForceMultiplayerAIUpdate: " + e.Message); + } } #region "TransitionTo" @@ -900,6 +941,86 @@ namespace KCM } } + // Force AI system restart in multiplayer + [HarmonyPatch(typeof(VillagerSystem), "Update")] + public class VillagerSystemUpdateHook + { + public static void Postfix() + { + if (KCClient.client.IsConnected && Time.timeScale > 0) + { + // Force AI brain updates for all villagers in multiplayer + try + { + for (int i = 0; i < Villager.villagers.Count; i++) + { + Villager v = Villager.villagers.data[i]; + if (v != null && v.brain != null && v.workerJob != null) + { + // Force brain to think and update + v.brain.Think(); + } + } + } + catch (Exception e) + { + helper?.Log("Error forcing AI update: " + e.Message); + } + } + } + } + + // Force job system updates in multiplayer + [HarmonyPatch(typeof(JobSystem), "Update")] + public class JobSystemUpdateHook + { + public static void Postfix() + { + if (KCClient.client.IsConnected && Time.timeScale > 0) + { + // Force job assignments and updates + try + { + // Force homeless villagers to find jobs + if (Player.inst != null && Player.inst.Homeless != null) + { + for (int i = Player.inst.Homeless.Count - 1; i >= 0; i--) + { + Villager v = Player.inst.Homeless.data[i]; + if (v != null && v.workerJob == null) + { + // Try to assign a job + JobSystem.inst.TryAssignJob(v); + } + } + } + } + catch (Exception e) + { + helper?.Log("Error forcing job assignment: " + e.Message); + } + } + } + } + + // Force AI restart when game is unpaused + [HarmonyPatch(typeof(SpeedControlUI), "SetPaused")] + public class SpeedControlUISetPausedHook + { + public static void Postfix(bool paused) + { + if (!KCClient.client.IsConnected) + return; + + if (!paused) + { + // Game is unpaused, force AI restart + helper.Log("Game unpaused, forcing AI restart"); + ForceMultiplayerAIUpdate(); + } + } + } + [HarmonyPatch(typeof(VillagerSystem), "AddVillager")] public class PlayerAddVillagerHook { @@ -1118,18 +1239,27 @@ namespace KCM [HarmonyPatch(typeof(SpeedControlUI), "SetSpeed")] public class SpeedControlUISetSpeedHook { - private static long lastTime = 0; - - public static bool Prefix(ref bool __state) + public static void Postfix(int idx) { - __state = false; - if (KCClient.client.IsConnected) + if (!KCClient.client.IsConnected) + return; + + helper.Log("SpeedControlUI.SetSpeed (local): " + idx); + + // Force AI restart when speed changes from paused to playing + if (idx > 0) { - bool calledFromPacket = false; - try - { - calledFromPacket = new StackFrame(3).GetMethod().Name.Contains("HandlePacket"); - } + ForceMultiplayerAIUpdate(); + } + + bool isPaused = (idx == 0); + new SetSpeed() + { + speed = idx, + isPaused = isPaused + }.Send(); + } + } catch { }