most elvileg el indul és javítottuk ezueket a szarokat.
This commit is contained in:
@@ -8,6 +8,7 @@ using System.Linq;
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
namespace KCM.LoadSaveOverrides
|
namespace KCM.LoadSaveOverrides
|
||||||
{
|
{
|
||||||
@@ -196,7 +197,17 @@ namespace KCM.LoadSaveOverrides
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Main.helper.Log("Unpacking AI brains before player data");
|
Main.helper.Log("Unpacking AI brains before player data");
|
||||||
this.AIBrainsSaveData.UnpackPrePlayer(AIBrainsContainer.inst);
|
// Use reflection to call UnpackPrePlayer if it exists
|
||||||
|
var aiSaveDataType = this.AIBrainsSaveData.GetType();
|
||||||
|
var unpackPrePlayerMethod = aiSaveDataType.GetMethod("UnpackPrePlayer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
|
||||||
|
if (unpackPrePlayerMethod != null)
|
||||||
|
{
|
||||||
|
unpackPrePlayerMethod.Invoke(this.AIBrainsSaveData, new object[] { AIBrainsContainer.inst });
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Main.helper.Log("UnpackPrePlayer method not found, skipping AI brains pre-unpack");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@@ -272,13 +283,24 @@ namespace KCM.LoadSaveOverrides
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
AIBrainsContainer.inst.ClearAIs();
|
AIBrainsContainer.inst.ClearAIs();
|
||||||
// Force reinitialize AI for all villagers
|
// Force villager system refresh instead of direct brain access
|
||||||
for (int i = 0; i < Villager.villagers.Count; i++)
|
if (VillagerSystem.inst != null)
|
||||||
{
|
{
|
||||||
Villager v = Villager.villagers.data[i];
|
var villagerSystemType = typeof(VillagerSystem);
|
||||||
if (v != null && v.brain != null)
|
var refreshMethods = villagerSystemType.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
|
||||||
|
.Where(m => m.Name.Contains("Refresh") || m.Name.Contains("Update") || m.Name.Contains("Restart"));
|
||||||
|
|
||||||
|
foreach (var method in refreshMethods)
|
||||||
{
|
{
|
||||||
v.brain.Restart();
|
if (method.GetParameters().Length == 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
method.Invoke(VillagerSystem.inst, null);
|
||||||
|
Main.helper.Log($"Called VillagerSystem.{method.Name} for AI reinit");
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Main.helper.Log("AI systems reinitialized");
|
Main.helper.Log("AI systems reinitialized");
|
||||||
@@ -294,13 +316,24 @@ namespace KCM.LoadSaveOverrides
|
|||||||
Main.helper.Log("No AI brains save data found, initializing fresh AI");
|
Main.helper.Log("No AI brains save data found, initializing fresh AI");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Initialize AI for all villagers if no save data
|
// Force villager system refresh for fresh initialization
|
||||||
for (int i = 0; i < Villager.villagers.Count; i++)
|
if (VillagerSystem.inst != null)
|
||||||
{
|
{
|
||||||
Villager v = Villager.villagers.data[i];
|
var villagerSystemType = typeof(VillagerSystem);
|
||||||
if (v != null && v.brain != null)
|
var refreshMethods = villagerSystemType.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
|
||||||
|
.Where(m => m.Name.Contains("Refresh") || m.Name.Contains("Update") || m.Name.Contains("Restart"));
|
||||||
|
|
||||||
|
foreach (var method in refreshMethods)
|
||||||
{
|
{
|
||||||
v.brain.Restart();
|
if (method.GetParameters().Length == 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
method.Invoke(VillagerSystem.inst, null);
|
||||||
|
Main.helper.Log($"Called VillagerSystem.{method.Name} for fresh AI");
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Main.helper.Log("Fresh AI initialization completed");
|
Main.helper.Log("Fresh AI initialization completed");
|
||||||
@@ -377,19 +410,23 @@ namespace KCM.LoadSaveOverrides
|
|||||||
{
|
{
|
||||||
Main.helper.Log("Forcing AI system restart after load");
|
Main.helper.Log("Forcing AI system restart after load");
|
||||||
|
|
||||||
// Restart all villager AI brains
|
// Force villager system refresh instead of direct brain access
|
||||||
for (int i = 0; i < Villager.villagers.Count; i++)
|
if (VillagerSystem.inst != null)
|
||||||
{
|
{
|
||||||
Villager v = Villager.villagers.data[i];
|
var villagerSystemType = typeof(VillagerSystem);
|
||||||
if (v != null && v.brain != null)
|
var refreshMethods = villagerSystemType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
|
||||||
|
.Where(m => m.Name.Contains("Refresh") || m.Name.Contains("Rebuild") || m.Name.Contains("Update") || m.Name.Contains("Restart"));
|
||||||
|
|
||||||
|
foreach (var method in refreshMethods)
|
||||||
|
{
|
||||||
|
if (method.GetParameters().Length == 0)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
v.brain.Restart();
|
method.Invoke(VillagerSystem.inst, null);
|
||||||
|
Main.helper.Log($"Called VillagerSystem.{method.Name}()");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch { }
|
||||||
{
|
|
||||||
Main.helper.Log($"Failed to restart villager AI: {e.Message}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -399,9 +436,8 @@ namespace KCM.LoadSaveOverrides
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Use reflection to call any refresh/rebuild methods
|
var jobSystemRefreshType = typeof(JobSystem);
|
||||||
var jobSystemType = typeof(JobSystem);
|
var refreshMethods = jobSystemRefreshType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
|
||||||
var refreshMethods = jobSystemType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
|
|
||||||
.Where(m => m.Name.Contains("Refresh") || m.Name.Contains("Rebuild") || m.Name.Contains("Update"));
|
.Where(m => m.Name.Contains("Refresh") || m.Name.Contains("Rebuild") || m.Name.Contains("Update"));
|
||||||
|
|
||||||
foreach (var method in refreshMethods)
|
foreach (var method in refreshMethods)
|
||||||
@@ -457,10 +493,70 @@ namespace KCM.LoadSaveOverrides
|
|||||||
Vector3 currentPos = v.Pos;
|
Vector3 currentPos = v.Pos;
|
||||||
v.TeleportTo(currentPos);
|
v.TeleportTo(currentPos);
|
||||||
|
|
||||||
// Restart AI brain if it exists
|
// Use reflection to check and fix villager state
|
||||||
if (v.brain != null)
|
var villagerType = typeof(Villager);
|
||||||
|
var workerJobField = villagerType.GetField("workerJob", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
|
var workerJob = workerJobField?.GetValue(v);
|
||||||
|
|
||||||
|
// Ensure villager is in correct system lists
|
||||||
|
if (workerJob != null && Player.inst != null)
|
||||||
{
|
{
|
||||||
v.brain.Restart();
|
if (!Player.inst.Workers.Contains(v))
|
||||||
|
{
|
||||||
|
Player.inst.Workers.Add(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (workerJob == null && Player.inst != null)
|
||||||
|
{
|
||||||
|
if (!Player.inst.Homeless.Contains(v))
|
||||||
|
{
|
||||||
|
Player.inst.Homeless.Add(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.helper.Log($"Error resyncing villager {i}: {e.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Force job system to re-evaluate all jobs
|
||||||
|
if (JobSystem.inst != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var jobSystemUpdateType = typeof(JobSystem);
|
||||||
|
var updateMethods = jobSystemUpdateType.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
|
||||||
|
.Where(m => m.Name.Contains("Update") || m.Name.Contains("Refresh"));
|
||||||
|
|
||||||
|
foreach (var method in updateMethods)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
method.Invoke(JobSystem.inst, null);
|
||||||
|
Main.helper.Log($"Called JobSystem.{method.Name} for resync");
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.helper.Log($"Error updating job system: {e.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Main.helper.Log("Villager state resync completed");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.helper.Log($"Error in delayed villager resync: {e.Message}");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.helper.Log($"Error starting villager resync: {e.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure villager is in correct system lists
|
// Ensure villager is in correct system lists
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
namespace KCM.Packets.Game
|
namespace KCM.Packets.Game
|
||||||
{
|
{
|
||||||
@@ -43,20 +44,32 @@ namespace KCM.Packets.Game
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Restart villager AI to ensure they continue working
|
// Force villager system refresh to ensure they continue working
|
||||||
for (int i = 0; i < Villager.villagers.Count; i++)
|
if (VillagerSystem.inst != null)
|
||||||
{
|
{
|
||||||
Villager v = Villager.villagers.data[i];
|
// Use reflection to call any refresh methods on VillagerSystem
|
||||||
if (v != null && v.brain != null)
|
var villagerSystemType = typeof(VillagerSystem);
|
||||||
|
var refreshMethods = villagerSystemType.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
|
||||||
|
.Where(m => m.Name.Contains("Refresh") || m.Name.Contains("Update") || m.Name.Contains("Restart"));
|
||||||
|
|
||||||
|
foreach (var method in refreshMethods)
|
||||||
{
|
{
|
||||||
v.brain.Restart();
|
if (method.GetParameters().Length == 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
method.Invoke(VillagerSystem.inst, null);
|
||||||
|
Main.helper.Log($"Called VillagerSystem.{method.Name} for speed change");
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Main.helper.Log($"AI systems restarted for speed change to {speed}");
|
}
|
||||||
|
Main.helper.Log($"AI systems refreshed for speed change to {speed}");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Main.helper.Log("Error restarting AI on speed change: " + e.Message);
|
Main.helper.Log("Error refreshing AI on speed change: " + e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using KCM.Packets;
|
using KCM.Packets;
|
||||||
using KCM.Packets.State;
|
using KCM.Packets.State;
|
||||||
using KCM.StateManagement.Observers;
|
using KCM.StateManagement.Observers;
|
||||||
using System;
|
using System;
|
||||||
@@ -6,6 +6,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine;
|
||||||
using static KCM.StateManagement.Observers.Observer;
|
using static KCM.StateManagement.Observers.Observer;
|
||||||
|
|
||||||
namespace KCM.StateManagement.BuildingState
|
namespace KCM.StateManagement.BuildingState
|
||||||
|
|||||||
@@ -760,48 +760,23 @@ namespace KCM.StateManagement.Sync
|
|||||||
{
|
{
|
||||||
bool needsCorrection = false;
|
bool needsCorrection = false;
|
||||||
|
|
||||||
// Check if villager is stuck (not moving for too long while having a job)
|
|
||||||
if (v.workerJob != null && v.brain != null)
|
|
||||||
{
|
|
||||||
// Check if villager is in invalid state
|
|
||||||
if (v.brain.currentAction == null && v.workerJob.active)
|
|
||||||
{
|
|
||||||
needsCorrection = true;
|
|
||||||
stuckVillagers++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if villager position is invalid
|
// Check if villager position is invalid
|
||||||
if (float.IsNaN(v.Pos.x) || float.IsNaN(v.Pos.y) || float.IsNaN(v.Pos.z))
|
if (float.IsNaN(v.Pos.x) || float.IsNaN(v.Pos.y) || float.IsNaN(v.Pos.z))
|
||||||
{
|
{
|
||||||
needsCorrection = true;
|
needsCorrection = true;
|
||||||
stuckVillagers++;
|
stuckVillagers++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (needsCorrection)
|
if (needsCorrection)
|
||||||
{
|
{
|
||||||
// Correct villager state
|
// Correct villager state
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Restart AI brain
|
|
||||||
if (v.brain != null)
|
|
||||||
{
|
|
||||||
v.brain.Restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure valid position
|
// Ensure valid position
|
||||||
if (float.IsNaN(v.Pos.x) || float.IsNaN(v.Pos.y) || float.IsNaN(v.Pos.z))
|
if (float.IsNaN(v.Pos.x) || float.IsNaN(v.Pos.y) || float.IsNaN(v.Pos.z))
|
||||||
{
|
{
|
||||||
// Teleport to a safe position near their workplace or home
|
// Teleport to a safe position
|
||||||
Vector3 safePos = Vector3.zero;
|
Vector3 safePos = new Vector3(World.inst.GridWidth / 2, 0, World.inst.GridHeight / 2);
|
||||||
if (v.workerJob != null && v.workerJob.employer != null)
|
|
||||||
{
|
|
||||||
safePos = v.workerJob.employer.transform.position;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
safePos = new Vector3(World.inst.GridWidth / 2, 0, World.inst.GridHeight / 2);
|
|
||||||
}
|
|
||||||
v.TeleportTo(safePos);
|
v.TeleportTo(safePos);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -823,6 +798,34 @@ namespace KCM.StateManagement.Sync
|
|||||||
{
|
{
|
||||||
Main.helper.Log($"Villager validation: Found {stuckVillagers} stuck villagers, corrected {correctedVillagers}");
|
Main.helper.Log($"Villager validation: Found {stuckVillagers} stuck villagers, corrected {correctedVillagers}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Force villager system refresh if we found issues
|
||||||
|
if (stuckVillagers > 0 && VillagerSystem.inst != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var villagerSystemType = typeof(VillagerSystem);
|
||||||
|
var refreshMethods = villagerSystemType.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
|
||||||
|
.Where(m => m.Name.Contains("Refresh") || m.Name.Contains("Update") || m.Name.Contains("Restart"));
|
||||||
|
|
||||||
|
foreach (var method in refreshMethods)
|
||||||
|
{
|
||||||
|
if (method.GetParameters().Length == 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
method.Invoke(VillagerSystem.inst, null);
|
||||||
|
Main.helper.Log($"Called VillagerSystem.{method.Name} for validation");
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Main.helper.Log($"Error refreshing villager system: {e.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user