private static MapEdge SelectDownhill(MapNode node) { double? dist = node.DistanceToCoast; MapEdge ret = node.Edges.First(); foreach (MapEdge e in node.Edges) { if (e.To.DistanceToCoast < dist) { ret = e; } } return(ret); }
public IEnumerable <MapNode[]> GenerateRivers() { int c = 0; MapNode[] eligibleRivers = map.Polygons .SelectMany(_ => _.Nodes) .Where(_ => _.DistanceToCoast > 0.25 && _.DistanceToCoast < 0.8) .ToArray(); List <MapNode[]> ret = new List <MapNode[]>(); do { MapNode node = eligibleRivers[rand.Next(0, eligibleRivers.Length)]; Stack <MapEdge> edges = new Stack <MapEdge>(); HashSet <MapNode> visited = new HashSet <MapNode>(); edges.Push(SelectDownhill(node)); List <MapNode> nodes = new List <MapNode>(); nodes.Add(node); while (edges.Count > 0) { MapEdge ed = edges.Pop(); nodes.Add(ed.To); MapEdge edge = SelectDownhill(ed.To); if (!edge.To.IsOcean && !visited.Contains(edge.To)) { visited.Add(edge.To); edges.Push(edge); } } ret.Add(nodes.ToArray()); c++; } while (c < 10); return(ret); }