Ejemplo n.º 1
0
        private static Int3 StepAlongPath(Room room, Int3 currentLocation, Sides[] possibleSides,
            Directions[] possibleDirections, List<Int3> solutionPath, int i, int minPathLength, ref Directions lastDirection)
        {
            var currentSide = room.roomBuffer[currentLocation.X, currentLocation.Y, currentLocation.Z];

            if (lastDirection != Directions.None && UnityEngine.Random.Range(0, 2) == 0)
            {
                var side = currentSide;
                var direction = lastDirection;

                var desirableSides = possibleSides
                    .Where(s => (s & side) == 0 && s != Sides.None &&
                                (
                                    s == Sides.Left && direction != Directions.Right
                                    || s == Sides.Right && direction != Directions.Left
                                    || s == Sides.Top && direction != Directions.Down
                                    || s == Sides.Bottom && direction != Directions.Up
                                    || s == Sides.Front && direction != Directions.Back
                                    || s == Sides.Rear && direction != Directions.Forward
                                    ));

                if (desirableSides.Any())
                {
                    currentSide = desirableSides.SelectRandom();
                    room.roomBuffer[currentLocation.X, currentLocation.Y, currentLocation.Z] |= currentSide;
                }
            }

            var intendedLocation = DetermineNextLocation(room, currentLocation, possibleDirections, ref lastDirection);

            room.roomBuffer[intendedLocation.X, intendedLocation.Y, intendedLocation.Z] |= currentSide;
            currentLocation = intendedLocation;
            solutionPath.Add(currentLocation);

            return currentLocation;
        }
Ejemplo n.º 2
0
        private static Int3 DetermineNextLocation(Room room, Int3 currentLocation, Directions[] possibleDirections, ref Directions lastDirection)
        {
            Directions invalidDirections = Directions.None;
            var foundValidDirection = false;
            var intendedLocation = currentLocation;
            var currentSide = room.roomBuffer[currentLocation.X, currentLocation.Y, currentLocation.Z];

            if (lastDirection == Directions.Left)
            {
                invalidDirections |= Directions.Right;
            }

            if (lastDirection == Directions.Right)
            {
                invalidDirections |= Directions.Left;
            }

            if (lastDirection == Directions.Up)
            {
                invalidDirections |= Directions.Down;
            }

            if (lastDirection == Directions.Down)
            {
                invalidDirections |= Directions.Up;
            }

            if (lastDirection == Directions.Forward)
            {
                invalidDirections |= Directions.Back;
            }

            if (lastDirection == Directions.Back)
            {
                invalidDirections |= Directions.Forward;
            }

            if ((currentSide & Sides.Left) > 0)
            {
                invalidDirections |= Directions.Left;
                invalidDirections |= Directions.Right;
            }
            if ((currentSide & Sides.Right) > 0)
            {
                invalidDirections |= Directions.Left;
                invalidDirections |= Directions.Right;
            }
            if ((currentSide & Sides.Top) > 0)
            {
                invalidDirections |= Directions.Up;
                invalidDirections |= Directions.Down;
            }
            if ((currentSide & Sides.Bottom) > 0)
            {
                invalidDirections |= Directions.Up;
                invalidDirections |= Directions.Down;
            }
            if ((currentSide & Sides.Front) > 0)
            {
                invalidDirections |= Directions.Back;
                invalidDirections |= Directions.Forward;
            }
            if ((currentSide & Sides.Rear) > 0)
            {
                invalidDirections |= Directions.Back;
                invalidDirections |= Directions.Forward;
            }

            while (!foundValidDirection)
            {
                intendedLocation = currentLocation;

                if (invalidDirections >= Directions.NUM_VALUES - 1)
                {
                    throw new InvalidSolveException();
                }

                var directions = invalidDirections;
                lastDirection = possibleDirections.Where(d => (directions & d) == 0).SelectRandom();
                if (lastDirection == Directions.None)
                    throw new InvalidSolveException();

                switch (lastDirection)
                {
                    case Directions.Forward:
                        intendedLocation.Z += 1;
                        if (intendedLocation.Z == room.Depth)
                        {
                            invalidDirections |= lastDirection;
                        }
                        else
                        {
                            if (room.roomBuffer[intendedLocation.X, intendedLocation.Y, intendedLocation.Z] !=
                                Sides.None)
                            {
                                invalidDirections |= lastDirection;
                            }
                            else
                            {
                                foundValidDirection = true;
                            }
                        }
                        break;

                    case Directions.Back:
                        intendedLocation.Z -= 1;
                        if (intendedLocation.Z < 0)
                        {
                            invalidDirections |= lastDirection;
                        }
                        else
                        {
                            if (room.roomBuffer[intendedLocation.X, intendedLocation.Y, intendedLocation.Z] !=
                                Sides.None)
                            {
                                invalidDirections |= lastDirection;
                            }
                            else
                            {
                                foundValidDirection = true;
                            }
                        }
                        break;

                    case Directions.Up:
                        intendedLocation.Y += 1;
                        if (intendedLocation.Y == room.Height)
                        {
                            invalidDirections |= lastDirection;
                        }
                        else
                        {
                            if (room.roomBuffer[intendedLocation.X, intendedLocation.Y, intendedLocation.Z] !=
                                Sides.None)
                            {
                                invalidDirections |= lastDirection;
                            }
                            else
                            {
                                foundValidDirection = true;
                            }
                        }
                        break;

                    case Directions.Down:
                        intendedLocation.Y -= 1;
                        if (intendedLocation.Y < 0)
                        {
                            invalidDirections |= lastDirection;
                        }
                        else
                        {
                            if (room.roomBuffer[intendedLocation.X, intendedLocation.Y, intendedLocation.Z] !=
                                Sides.None)
                            {
                                invalidDirections |= lastDirection;
                            }
                            else
                            {
                                foundValidDirection = true;
                            }
                        }
                        break;

                    case Directions.Left:
                        intendedLocation.X -= 1;
                        if (intendedLocation.X < 0)
                        {
                            invalidDirections |= lastDirection;
                        }
                        else
                        {
                            if (room.roomBuffer[intendedLocation.X, intendedLocation.Y, intendedLocation.Z] !=
                                Sides.None)
                            {
                                invalidDirections |= lastDirection;
                            }
                            else
                            {
                                foundValidDirection = true;
                            }
                        }
                        break;

                    case Directions.Right:
                        intendedLocation.X += 1;
                        if (intendedLocation.X == room.Width)
                        {
                            invalidDirections |= lastDirection;
                        }
                        else
                        {
                            if (room.roomBuffer[intendedLocation.X, intendedLocation.Y, intendedLocation.Z] !=
                                Sides.None)
                            {
                                invalidDirections |= lastDirection;
                            }
                            else
                            {
                                foundValidDirection = true;
                            }
                        }
                        break;
                }
            }

            return intendedLocation;
        }
Ejemplo n.º 3
0
 private void DefineEntryPoint(int width, int height)
 {
     var entryLateralPoint = Random.Range(0, width);
     var entryVerticalPoint = Random.Range(0, height);
     entryPoint = new Int3(entryLateralPoint, entryVerticalPoint, 0);
 }