RiverGenerator.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public class RiverGenerator
  4. {
  5. public void GenerateRivers(MapData mapData)
  6. {
  7. // Find water bodies to connect
  8. var waterBodies = FindWaterBodies(mapData);
  9. // Only show debug info if a MapMaker2 with debugMode enabled exists
  10. var mapMaker = Object.FindFirstObjectByType<MapMaker2>();
  11. bool debugMode = mapMaker != null && mapMaker.debugMode;
  12. if (debugMode) Debug.Log($"Found {waterBodies.Count} water body tiles for river generation");
  13. // Generate fewer, thinner rivers between some water bodies
  14. int riverCount = Mathf.Min(3, waterBodies.Count - 1); // Maximum 3 rivers
  15. riverCount = Mathf.Max(1, riverCount); // Force at least 1 river if possible
  16. if (debugMode) Debug.Log($"Attempting to generate {riverCount} rivers");
  17. for (int i = 0; i < riverCount && i < waterBodies.Count - 1; i++)
  18. {
  19. CreateRiver(mapData, waterBodies[i], waterBodies[i + 1]);
  20. if (debugMode) Debug.Log($"Created river {i + 1} from {waterBodies[i]} to {waterBodies[i + 1]}");
  21. }
  22. }
  23. private List<Vector2Int> FindWaterBodies(MapData mapData)
  24. {
  25. List<Vector2Int> waterBodyCenters = new List<Vector2Int>();
  26. bool[,] visited = new bool[mapData.Width, mapData.Height];
  27. // Find distinct water bodies (groups of connected water tiles)
  28. for (int x = 0; x < mapData.Width; x++)
  29. {
  30. for (int y = 0; y < mapData.Height; y++)
  31. {
  32. MapTile tile = mapData.GetTile(x, y);
  33. if (!visited[x, y] && (tile.terrainType == TerrainType.Ocean || tile.terrainType == TerrainType.Lake))
  34. {
  35. // Found a new water body, find its center
  36. var waterTiles = FloodFillWaterBody(mapData, visited, x, y);
  37. if (waterTiles.Count > 5) // Only consider substantial water bodies
  38. {
  39. Vector2Int center = CalculateCenter(waterTiles);
  40. waterBodyCenters.Add(center);
  41. var mapMaker = Object.FindFirstObjectByType<MapMaker2>();
  42. bool debugMode = mapMaker != null && mapMaker.debugMode;
  43. if (debugMode) Debug.Log($"Found water body center at {center} with {waterTiles.Count} tiles");
  44. }
  45. }
  46. }
  47. }
  48. return waterBodyCenters;
  49. }
  50. private List<Vector2Int> FloodFillWaterBody(MapData mapData, bool[,] visited, int startX, int startY)
  51. {
  52. List<Vector2Int> waterTiles = new List<Vector2Int>();
  53. Queue<Vector2Int> queue = new Queue<Vector2Int>();
  54. queue.Enqueue(new Vector2Int(startX, startY));
  55. while (queue.Count > 0)
  56. {
  57. Vector2Int current = queue.Dequeue();
  58. if (visited[current.x, current.y]) continue;
  59. visited[current.x, current.y] = true;
  60. waterTiles.Add(current);
  61. // Check neighbors
  62. for (int dx = -1; dx <= 1; dx++)
  63. {
  64. for (int dy = -1; dy <= 1; dy++)
  65. {
  66. if (dx == 0 && dy == 0) continue;
  67. int newX = current.x + dx;
  68. int newY = current.y + dy;
  69. if (mapData.IsValidPosition(newX, newY) && !visited[newX, newY])
  70. {
  71. MapTile tile = mapData.GetTile(newX, newY);
  72. if (tile.terrainType == TerrainType.Ocean || tile.terrainType == TerrainType.Lake)
  73. {
  74. queue.Enqueue(new Vector2Int(newX, newY));
  75. }
  76. }
  77. }
  78. }
  79. }
  80. return waterTiles;
  81. }
  82. private Vector2Int CalculateCenter(List<Vector2Int> tiles)
  83. {
  84. if (tiles.Count == 0) return Vector2Int.zero;
  85. float avgX = 0f;
  86. float avgY = 0f;
  87. foreach (var tile in tiles)
  88. {
  89. avgX += tile.x;
  90. avgY += tile.y;
  91. }
  92. return new Vector2Int(Mathf.RoundToInt(avgX / tiles.Count), Mathf.RoundToInt(avgY / tiles.Count));
  93. }
  94. private void CreateRiver(MapData mapData, Vector2Int start, Vector2Int end)
  95. {
  96. var riverPath = FindRiverPath(mapData, start, end);
  97. foreach (var point in riverPath)
  98. {
  99. if (mapData.IsValidPosition(point.x, point.y))
  100. {
  101. MapTile tile = mapData.GetTile(point.x, point.y);
  102. if (tile.terrainType == TerrainType.Plains || tile.terrainType == TerrainType.Shore)
  103. {
  104. tile.terrainType = TerrainType.River;
  105. tile.isWalkable = false;
  106. }
  107. }
  108. }
  109. // Smart diagonal blocking - only fill specific diagonal gaps
  110. BlockDiagonalCrossings(mapData, riverPath);
  111. }
  112. private void BlockDiagonalCrossings(MapData mapData, List<Vector2Int> riverPath)
  113. {
  114. foreach (var riverPoint in riverPath)
  115. {
  116. // Check for diagonal gaps that would allow diagonal crossing
  117. for (int dx = -1; dx <= 1; dx += 2) // -1 and 1
  118. {
  119. for (int dy = -1; dy <= 1; dy += 2) // -1 and 1
  120. {
  121. Vector2Int diagonal = new Vector2Int(riverPoint.x + dx, riverPoint.y + dy);
  122. Vector2Int horizontal = new Vector2Int(riverPoint.x + dx, riverPoint.y);
  123. Vector2Int vertical = new Vector2Int(riverPoint.x, riverPoint.y + dy);
  124. if (mapData.IsValidPosition(diagonal.x, diagonal.y) &&
  125. mapData.IsValidPosition(horizontal.x, horizontal.y) &&
  126. mapData.IsValidPosition(vertical.x, vertical.y))
  127. {
  128. // If diagonal tile is river and both adjacent tiles are not river,
  129. // there's a crossable diagonal gap
  130. if (mapData.GetTile(diagonal.x, diagonal.y).terrainType == TerrainType.River &&
  131. mapData.GetTile(horizontal.x, horizontal.y).terrainType != TerrainType.River &&
  132. mapData.GetTile(vertical.x, vertical.y).terrainType != TerrainType.River)
  133. {
  134. // Fill one of the adjacent tiles to block diagonal crossing
  135. MapTile fillTile = mapData.GetTile(horizontal.x, horizontal.y);
  136. if (fillTile.terrainType == TerrainType.Plains || fillTile.terrainType == TerrainType.Shore)
  137. {
  138. fillTile.terrainType = TerrainType.River;
  139. fillTile.isWalkable = false;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. private List<Vector2Int> FindRiverPath(MapData mapData, Vector2Int start, Vector2Int end)
  148. {
  149. List<Vector2Int> path = new List<Vector2Int>();
  150. Vector2Int current = start;
  151. while (Vector2Int.Distance(current, end) > 1)
  152. {
  153. path.Add(current);
  154. // Move towards end with some meandering
  155. Vector2Int direction = new Vector2Int(
  156. end.x > current.x ? 1 : (end.x < current.x ? -1 : 0),
  157. end.y > current.y ? 1 : (end.y < current.y ? -1 : 0)
  158. );
  159. // Add meandering
  160. if (Random.value < 0.4f)
  161. {
  162. direction.x += Random.Range(-1, 2);
  163. direction.y += Random.Range(-1, 2);
  164. direction.x = Mathf.Clamp(direction.x, -1, 1);
  165. direction.y = Mathf.Clamp(direction.y, -1, 1);
  166. }
  167. Vector2Int next = current + direction;
  168. if (mapData.IsValidPosition(next.x, next.y))
  169. {
  170. current = next;
  171. }
  172. else
  173. {
  174. break;
  175. }
  176. }
  177. return path;
  178. }
  179. private void FillRiverDiagonals(MapData mapData, int x, int y)
  180. {
  181. // Check and fill diagonal gaps to prevent crossing without a bridge
  182. for (int dx = -1; dx <= 1; dx++)
  183. {
  184. for (int dy = -1; dy <= 1; dy++)
  185. {
  186. if (dx == 0 || dy == 0) continue; // Skip cardinal directions
  187. int checkX = x + dx;
  188. int checkY = y + dy;
  189. if (mapData.IsValidPosition(checkX, checkY))
  190. {
  191. MapTile neighbor = mapData.GetTile(checkX, checkY);
  192. if (neighbor.terrainType == TerrainType.River)
  193. {
  194. // Fill the gap
  195. int fillX = x + (dx > 0 ? 1 : 0);
  196. int fillY = y + (dy > 0 ? 1 : 0);
  197. if (mapData.IsValidPosition(fillX, fillY))
  198. {
  199. MapTile fillTile = mapData.GetTile(fillX, fillY);
  200. if (fillTile.terrainType == TerrainType.Plains)
  201. {
  202. fillTile.terrainType = TerrainType.River;
  203. fillTile.isWalkable = false;
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }