Exemplo n.º 1
0
        static TestUtil()
        {
            // Using a "square" pattern for now to simplify initial test code
            SquareTestCave.AddRoom(0, new int[] { -1, 1, -1, -1 });
            SquareTestCave.AddRoom(1, new int[] { 2, 3, -1, 0 });
            SquareTestCave.AddRoom(2, new int[] { -1, -1, 1, -1 });
            SquareTestCave.AddRoom(3, new int[] { -1, 4, -1, 1 });
            SquareTestCave.AddRoom(4, new int[] { -1, 5, -1, 3 });
            SquareTestCave.AddRoom(5, new int[] { -1, -1, -1, 4 });

            HexTestCave.AddRoom(0, new int[] { -1, 1, -1, -1, -1, -1 });
            HexTestCave.AddRoom(1, new int[] { -1, 2, -1, -1, 0, -1 });
            HexTestCave.AddRoom(2, new int[] { -1, -1, 3, -1, 1, -1 });
            HexTestCave.AddRoom(3, new int[] { -1, -1, -1, -1, -1, 2 });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a cave without hazards and gold.
        /// </summary>
        /// <param name="roomsToCreate"></param>
        /// <param name="maxConnectionsToCreatePerRoom"></param>
        /// <returns></returns>
        private static Cave CreateRooms(int roomsToCreate, int maxConnectionsToCreatePerRoom)
        {
            Cave NewCave         = new Cave();
            int  numRoomsCreated = 0;
            int  id       = 0;
            int  lastRoom = 0;

            // Must be at least maxRoomCount * 2. 3 to be safe
            int size = roomsToCreate * 3;

            // Holds all the rooms that exist, -1 if no room
            int[,] rooms = new int[size, size];
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    rooms[i, j] = -1;
                }
            }

            //Start in the middle
            Point lastPos = new Point(size / 2, size / 2);

            NewCave.AddRoom(id, Enumerable.Repeat(-1, 6).ToArray());
            rooms[lastPos.X, lastPos.Y] = id;
            id++;
            // Iterate while we still need to make more rooms
            while (true)
            {
                int numToCreate = Rand.Next(maxConnectionsToCreatePerRoom);
                for (int j = 0; j < numToCreate; j++)
                {
                    int direction        = Rand.Next(6);
                    int inverseDirection = GetInverseDirection(direction);

                    Point newPoint = GetDifference(lastPos.X, lastPos.Y, direction);

                    if (rooms[newPoint.X, newPoint.Y] >= 0)
                    {
                        // If we can add more connections
                        if (NewCave[lastRoom].AdjacentRooms.Count(i => i != -1) < maxConnectionsToCreatePerRoom &&
                            NewCave[rooms[newPoint.X, newPoint.Y]].AdjacentRooms.Count(i => i != -1) < maxConnectionsToCreatePerRoom)
                        {
                            //We ran into an old room. Create a connection between us and it.
                            NewCave[lastRoom].AdjacentRooms[direction] = rooms[newPoint.X, newPoint.Y];
                            NewCave[rooms[newPoint.X, newPoint.Y]].AdjacentRooms[GetInverseDirection(direction)] = lastRoom;
                        }

                        if (j == numToCreate - 1)
                        {
                            //Set last room to current room
                            lastRoom = rooms[newPoint.X, newPoint.Y];
                            lastPos  = newPoint;
                        }
                    }
                    else
                    {
                        //Set connections
                        int[] connections = Enumerable.Repeat(-1, 6).ToArray();
                        connections[inverseDirection] = lastRoom;
                        NewCave[lastRoom].AdjacentRooms[direction] = id;

                        //Add room
                        NewCave.AddRoom(id, connections);
                        numRoomsCreated++;
                        // If we've created enough rooms
                        if (numRoomsCreated >= roomsToCreate)
                        {
                            // Create side connections and we're done
                            // Algorithm is giving weird results right now
                            // AddSideConnections(NewCave, maxConnectionsToCreatePerRoom);
                            return(NewCave);
                        }

                        rooms[newPoint.X, newPoint.Y] = id;
                        if (j == numToCreate - 1)
                        {
                            //Set last room to current room
                            lastRoom = id;
                            lastPos  = newPoint;
                        }

                        id++;
                    }
                }
            }
        }