private void GenerateRivers()
{
int attempts = 0;
int rivercount = RiverCount;
Rivers = new List <River>();
// Generate some rivers
while (rivercount > 0 && attempts < MaxRiverAttempts)
{
// Get a random tile
int x = Random.Next(0, Width);
int y = Random.Next(0, Height);
Tile tile = Tiles[x, y];
// validate the tile
if (!tile.Collidable)
{
continue;
}
if (tile.Rivers.Count > 0)
{
continue;
}
if (tile.HeightValue > MinRiverHeight)
{
// Tile is good to start river from
River river = new River(rivercount);
// Figure out the direction this river will try to flow
river.CurrentDirection = tile.GetLowestNeighbor();
// Recursively find a path to water
FindPathToWater(tile, river.CurrentDirection, ref river);
// Validate the generated river
if (river.TurnCount < MinRiverTurns || river.Tiles.Count < MinRiverLength || river.Intersections > MaxRiverIntersections)
{
//Validation failed - remove this river
for (int i = 0; i < river.Tiles.Count; i++)
{
Tile t = river.Tiles[i];
t.Rivers.Remove(river);
}
}
else if (river.Tiles.Count >= MinRiverLength)
{
//Validation passed - Add river to list
Rivers.Add(river);
tile.Rivers.Add(river);
rivercount--;
}
}
attempts++;
}
}