private void FindPathToWater(Tile tile, Direction direction, ref River river) { if (tile.rivers.Contains(river)) { return; } if (tile.rivers.Count > 0) { river.intersections++; } river.AddTile(tile); // Get neighbours Tile right = GetRight(tile); Tile top = GetTop(tile); Tile left = GetLeft(tile); Tile bottom = GetBottom(tile); float rightValue = float.MaxValue; float topValue = float.MaxValue; float leftValue = float.MaxValue; float bottomValue = float.MaxValue; // Check height value of neighbours if (right.GetRiverNeighbourCount(river) < 2 && !river.tiles.Contains(right)) { rightValue = right.heightValue; } if (top.GetRiverNeighbourCount(river) < 2 && !river.tiles.Contains(top)) { topValue = top.heightValue; } if (left.GetRiverNeighbourCount(river) < 2 && !river.tiles.Contains(left)) { leftValue = left.heightValue; } if (bottom.GetRiverNeighbourCount(river) < 2 && !river.tiles.Contains(bottom)) { bottomValue = bottom.heightValue; } // If the neighbour tile has a river that is not this one, flow into it if (right.rivers.Count == 0 && !right.isCollidable) { rightValue = 0; } if (top.rivers.Count == 0 && !top.isCollidable) { topValue = 0; } if (left.rivers.Count == 0 && !left.isCollidable) { leftValue = 0; } if (bottom.rivers.Count == 0 && !bottom.isCollidable) { bottomValue = 0; } // Override flow direction if the neighbour tile is significantly lower if (direction == Direction.Right && Mathf.Abs(rightValue - leftValue) < 0.1f) { rightValue = int.MaxValue; } if (direction == Direction.Top && Mathf.Abs(topValue - bottomValue) < 0.1f) { topValue = int.MaxValue; } if (direction == Direction.Left && Mathf.Abs(rightValue - leftValue) < 0.1f) { leftValue = int.MaxValue; } if (direction == Direction.Bottom && Mathf.Abs(topValue - bottomValue) < 0.1f) { bottomValue = int.MaxValue; } // Find local miniumum float min = Mathf.Min(Mathf.Min(Mathf.Min(rightValue, topValue), leftValue), bottomValue); // If no minumum found, break if (min == int.MaxValue) { return; } // Move to next neighbour if (min == rightValue) { if (right.isCollidable) { if (river.CurrentDirection != Direction.Right) { river.turnCount++; river.CurrentDirection = Direction.Right; } FindPathToWater(right, direction, ref river); } } else if (min == topValue) { if (top.isCollidable) { if (river.CurrentDirection != Direction.Top) { river.turnCount++; river.CurrentDirection = Direction.Top; } FindPathToWater(top, direction, ref river); } } else if (min == leftValue) { if (left.isCollidable) { if (river.CurrentDirection != Direction.Left) { river.turnCount++; river.CurrentDirection = Direction.Left; } FindPathToWater(left, direction, ref river); } } else if (min == bottomValue) { if (bottom.isCollidable) { if (river.CurrentDirection != Direction.Bottom) { river.turnCount++; river.CurrentDirection = Direction.Bottom; } FindPathToWater(bottom, direction, ref river); } } }
private void FindPathToWater(Tile tile, Direction direction, ref River river) { if (tile.Rivers.Contains(river)) { return; } // check if there is already a river on this tile if (tile.Rivers.Count > 0) { river.Intersections++; } river.AddTile(tile); // get neighbors Tile left = GetLeft(tile); Tile right = GetRight(tile); Tile top = GetTop(tile); Tile bottom = GetBottom(tile); float leftValue = int.MaxValue; float rightValue = int.MaxValue; float topValue = int.MaxValue; float bottomValue = int.MaxValue; // query height values of neighbors if (left.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(left)) { leftValue = left.HeightValue; } if (right.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(right)) { rightValue = right.HeightValue; } if (top.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(top)) { topValue = top.HeightValue; } if (bottom.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(bottom)) { bottomValue = bottom.HeightValue; } // if neighbor is existing river that is not this one, flow into it if (bottom.Rivers.Count == 0 && !bottom.Collidable) { bottomValue = 0; } if (top.Rivers.Count == 0 && !top.Collidable) { topValue = 0; } if (left.Rivers.Count == 0 && !left.Collidable) { leftValue = 0; } if (right.Rivers.Count == 0 && !right.Collidable) { rightValue = 0; } // override flow direction if a tile is significantly lower if (direction == Direction.Left) { if (Mathf.Abs(rightValue - leftValue) < 0.1f) { rightValue = int.MaxValue; } } if (direction == Direction.Right) { if (Mathf.Abs(rightValue - leftValue) < 0.1f) { leftValue = int.MaxValue; } } if (direction == Direction.Top) { if (Mathf.Abs(topValue - bottomValue) < 0.1f) { bottomValue = int.MaxValue; } } if (direction == Direction.Bottom) { if (Mathf.Abs(topValue - bottomValue) < 0.1f) { topValue = int.MaxValue; } } // find mininum float min = Mathf.Min(Mathf.Min(Mathf.Min(leftValue, rightValue), topValue), bottomValue); // if no minimum found - exit if (min == int.MaxValue) { return; } //Move to next neighbor if (min == leftValue) { if (left.Collidable) { if (river.CurrentDirection != Direction.Left) { river.TurnCount++; river.CurrentDirection = Direction.Left; } FindPathToWater(left, direction, ref river); } } else if (min == rightValue) { if (right.Collidable) { if (river.CurrentDirection != Direction.Right) { river.TurnCount++; river.CurrentDirection = Direction.Right; } FindPathToWater(right, direction, ref river); } } else if (min == bottomValue) { if (bottom.Collidable) { if (river.CurrentDirection != Direction.Bottom) { river.TurnCount++; river.CurrentDirection = Direction.Bottom; } FindPathToWater(bottom, direction, ref river); } } else if (min == topValue) { if (top.Collidable) { if (river.CurrentDirection != Direction.Top) { river.TurnCount++; river.CurrentDirection = Direction.Top; } FindPathToWater(top, direction, ref river); } } }
private void FindPathToWater(Tile tile, Direction direction, ref River river) { if (tile.Rivers.Contains (river)) return; // check if there is already a river on this tile if (tile.Rivers.Count > 0) river.Intersections++; river.AddTile (tile); // get neighbors Tile left = GetLeft (tile); Tile right = GetRight (tile); Tile top = GetTop (tile); Tile bottom = GetBottom (tile); float leftValue = int.MaxValue; float rightValue = int.MaxValue; float topValue = int.MaxValue; float bottomValue = int.MaxValue; // query height values of neighbors if (left != null && left.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(left)) leftValue = left.HeightValue; if (right != null && right.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(right)) rightValue = right.HeightValue; if (top != null && top.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(top)) topValue = top.HeightValue; if (bottom != null && bottom.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(bottom)) bottomValue = bottom.HeightValue; // if neighbor is existing river that is not this one, flow into it if (bottom != null && bottom.Rivers.Count == 0 && !bottom.Collidable) bottomValue = 0; if (top != null && top.Rivers.Count == 0 && !top.Collidable) topValue = 0; if (left != null && left.Rivers.Count == 0 && !left.Collidable) leftValue = 0; if (right != null && right.Rivers.Count == 0 && !right.Collidable) rightValue = 0; // override flow direction if a tile is significantly lower if (direction == Direction.Left) if (Mathf.Abs (rightValue - leftValue) < 0.1f) rightValue = int.MaxValue; if (direction == Direction.Right) if (Mathf.Abs (rightValue - leftValue) < 0.1f) leftValue = int.MaxValue; if (direction == Direction.Top) if (Mathf.Abs (topValue - bottomValue) < 0.1f) bottomValue = int.MaxValue; if (direction == Direction.Bottom) if (Mathf.Abs (topValue - bottomValue) < 0.1f) topValue = int.MaxValue; // find mininum float min = Mathf.Min (Mathf.Min (Mathf.Min (leftValue, rightValue), topValue), bottomValue); // if no minimum found - exit if (min == int.MaxValue) return; //Move to next neighbor if (min == leftValue) { if (left != null && left.Collidable) { if (river.CurrentDirection != Direction.Left){ river.TurnCount++; river.CurrentDirection = Direction.Left; } FindPathToWater (left, direction, ref river); } } else if (min == rightValue) { if (right != null && right.Collidable) { if (river.CurrentDirection != Direction.Right){ river.TurnCount++; river.CurrentDirection = Direction.Right; } FindPathToWater (right, direction, ref river); } } else if (min == bottomValue) { if (bottom != null && bottom.Collidable) { if (river.CurrentDirection != Direction.Bottom){ river.TurnCount++; river.CurrentDirection = Direction.Bottom; } FindPathToWater (bottom, direction, ref river); } } else if (min == topValue) { if (top != null && top.Collidable) { if (river.CurrentDirection != Direction.Top){ river.TurnCount++; river.CurrentDirection = Direction.Top; } FindPathToWater (top, direction, ref river); } } }