RiverGenerator.cs 8.8 KB

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