private void CreateRivers(MapSettingsData data) { System.Random prng = new System.Random(data.noiseSetting.seed); var terrainOnlyHexes = Hexes.ToList().Where(h => h.Elevation > 0); for (int i = 0; i < data.riverCount; i++) { int riverLength = prng.Next((int)data.riverLengthMinMax[0], (int)data.riverLengthMinMax[1]); // Start the river HexObject currentHex = terrainOnlyHexes.ToArray()[prng.Next(0, terrainOnlyHexes.Count())]; // Get neighbour to connect to HexObject nextLowestNeighbour = Instance.FindHexObject(Hex.Neighbours(currentHex.Hex).OrderBy(n => Instance.FindHexObject(n.cubeCoords).Elevation).FirstOrDefault().cubeCoords); for (int l = 0; l < riverLength; l++) { if (currentHex.Elevation == 0) { break; } if (currentHex == nextLowestNeighbour) { continue; } currentHex.SetOutgoingRiver(Hex.Direction(currentHex.Hex, nextLowestNeighbour.Hex)); if (nextLowestNeighbour == null) { break; } currentHex = nextLowestNeighbour; if (currentHex == null) { break; } try { var neighbours = Hex.Neighbours(currentHex.Hex); var hexObjectToFind = neighbours.OrderBy(n => Instance.FindHexObject(n.cubeCoords).Elevation).FirstOrDefault(); nextLowestNeighbour = Instance.FindHexObject(hexObjectToFind.cubeCoords); } catch { continue; } } } }