SadConsole.Game.WorldGeneration.Tile.GetRiverNeighborCount C# (CSharp) Method

GetRiverNeighborCount() public method

public GetRiverNeighborCount ( River river ) : int
river River
return int
        public int GetRiverNeighborCount(River river)
        {
            int count = 0;
            if (Left != null && Left.Rivers.Count > 0 && Left.Rivers.Contains(river))
                count++;
            if (Right != null && Right.Rivers.Count > 0 && Right.Rivers.Contains(river))
                count++;
            if (Top != null && Top.Rivers.Count > 0 && Top.Rivers.Contains(river))
                count++;
            if (Bottom != null && Bottom.Rivers.Count > 0 && Bottom.Rivers.Contains(river))
                count++;
            return count;
        }

Usage Example

Esempio n. 1
0
        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 (Math.Abs(rightValue - leftValue) < 0.1f)
                {
                    rightValue = int.MaxValue;
                }
            }
            if (direction == Direction.Right)
            {
                if (Math.Abs(rightValue - leftValue) < 0.1f)
                {
                    leftValue = int.MaxValue;
                }
            }
            if (direction == Direction.Top)
            {
                if (Math.Abs(topValue - bottomValue) < 0.1f)
                {
                    bottomValue = int.MaxValue;
                }
            }
            if (direction == Direction.Bottom)
            {
                if (Math.Abs(topValue - bottomValue) < 0.1f)
                {
                    topValue = int.MaxValue;
                }
            }

            // find mininum
            float min = Math.Min(Math.Min(Math.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);
                }
            }
        }