SettlementDetectionHotfix.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using UnityEngine;
  2. /// <summary>
  3. /// Temporary fix to force settlement detection to work
  4. /// This patches the coordinate conversion issue
  5. /// </summary>
  6. public class SettlementDetectionHotfix : MonoBehaviour
  7. {
  8. [Header("Settlement Detection Hotfix")]
  9. [Tooltip("Force settlement detection to work by improving coordinate conversion")]
  10. public bool enableHotfix = true;
  11. private SettlementInteractionManager settlementManager;
  12. void Start()
  13. {
  14. settlementManager = FindFirstObjectByType<SettlementInteractionManager>();
  15. if (settlementManager != null && enableHotfix)
  16. {
  17. Debug.Log("[Hotfix] Settlement detection hotfix enabled");
  18. }
  19. }
  20. void Update()
  21. {
  22. if (!enableHotfix || settlementManager == null) return;
  23. // Check if we should manually trigger settlement detection
  24. var teamMarker = GameObject.Find("TeamMarker");
  25. if (teamMarker == null) return;
  26. Vector3 teamPos = teamMarker.transform.position;
  27. Vector2Int mapPos = new Vector2Int(
  28. Mathf.RoundToInt(teamPos.x),
  29. Mathf.RoundToInt(teamPos.z)
  30. );
  31. // Force check the exact coordinate and nearby coordinates
  32. var mapMaker = FindFirstObjectByType<MapMaker2>();
  33. if (mapMaker?.GetMapData() != null)
  34. {
  35. var mapData = mapMaker.GetMapData();
  36. // Check current position and all adjacent positions
  37. Settlement foundSettlement = null;
  38. for (int dx = -1; dx <= 1; dx++)
  39. {
  40. for (int dy = -1; dy <= 1; dy++)
  41. {
  42. Vector2Int checkPos = new Vector2Int(mapPos.x + dx, mapPos.y + dy);
  43. var settlement = mapData.GetSettlementAt(checkPos, 0.1f); // Very tight tolerance
  44. if (settlement != null)
  45. {
  46. foundSettlement = settlement;
  47. break;
  48. }
  49. }
  50. if (foundSettlement != null) break;
  51. }
  52. // If we found a settlement but the manager doesn't detect it, force the detection
  53. var currentDetected = settlementManager.GetCurrentNearbySettlement();
  54. if (foundSettlement != null && currentDetected == null)
  55. {
  56. Debug.Log($"[Hotfix] Forcing settlement detection: {foundSettlement.name} at {foundSettlement.position}");
  57. // Force manual entry when E is pressed
  58. if (Input.GetKeyDown(KeyCode.E))
  59. {
  60. Debug.Log($"[Hotfix] Manually entering settlement: {foundSettlement.name}");
  61. settlementManager.EnterSettlement(foundSettlement);
  62. }
  63. }
  64. }
  65. }
  66. [ContextMenu("Force Settlement Detection Now")]
  67. public void ForceDetectionNow()
  68. {
  69. var teamMarker = GameObject.Find("TeamMarker");
  70. var mapMaker = FindFirstObjectByType<MapMaker2>();
  71. if (teamMarker == null || mapMaker?.GetMapData() == null)
  72. {
  73. Debug.LogError("[Hotfix] Missing components for detection");
  74. return;
  75. }
  76. Vector3 teamPos = teamMarker.transform.position;
  77. Vector2Int mapPos = new Vector2Int(
  78. Mathf.RoundToInt(teamPos.x),
  79. Mathf.RoundToInt(teamPos.z)
  80. );
  81. Debug.Log($"[Hotfix] Checking position {mapPos} (world {teamPos})");
  82. var mapData = mapMaker.GetMapData();
  83. var allSettlements = mapData.GetAllSettlements();
  84. foreach (var settlement in allSettlements)
  85. {
  86. float distance = Vector2Int.Distance(mapPos, settlement.position);
  87. Debug.Log($"[Hotfix] Settlement '{settlement.name}' at {settlement.position} - Distance: {distance:F2}");
  88. if (distance <= 1.5f)
  89. {
  90. Debug.Log($"[Hotfix] 🎯 FOUND NEARBY: {settlement.name}");
  91. if (settlementManager != null)
  92. {
  93. Debug.Log($"[Hotfix] Manually entering settlement: {settlement.name}");
  94. settlementManager.EnterSettlement(settlement);
  95. return;
  96. }
  97. }
  98. }
  99. Debug.Log("[Hotfix] No settlement found within range");
  100. }
  101. void OnGUI()
  102. {
  103. if (!enableHotfix) return;
  104. GUILayout.BeginArea(new Rect(Screen.width - 300, 10, 280, 100));
  105. GUILayout.BeginVertical("box");
  106. GUILayout.Label("Settlement Detection Hotfix", new GUIStyle(GUI.skin.label) { fontSize = 12, fontStyle = FontStyle.Bold });
  107. if (GUILayout.Button("Force Enter Settlement"))
  108. {
  109. ForceDetectionNow();
  110. }
  111. var teamMarker = GameObject.Find("TeamMarker");
  112. if (teamMarker != null)
  113. {
  114. Vector3 pos = teamMarker.transform.position;
  115. GUILayout.Label($"Position: ({pos.x:F0}, {pos.z:F0})", new GUIStyle(GUI.skin.label) { fontSize = 10 });
  116. }
  117. GUILayout.EndVertical();
  118. GUILayout.EndArea();
  119. }
  120. }