Ejemplo n.º 1
0
        private static bool buildOnePath(SimplifiedLayoutDataPacket simpleDataPacket, byte lengthOfPath, bool isKeyPath, System.Random random_thread)
        {
            byte[] roomCoords = new byte[2]; // X, Y
            byte[] roomInfo   = new byte[2]; // direction, clearance

            // First, find a good spotm and gather information:
            do
            {
                roomCoords = simpleDataPacket.getRandomCoordInBoundingBox(random_thread);
                roomInfo   = gatherInfoAboutRandomAdjacentPlacedRoom(simpleDataPacket.getSimpleArray_building(), roomCoords, random_thread);
            } while (roomInfo[0] == Constants.doorID_null);

            // Reset the path array, update it's position relative to the builderArray, and keep track of the first room:
            SimpleRoom_Building firstRoomInPath = simpleDataPacket.resetArray_Path(roomCoords[0], roomCoords[1]);

            // define the first room, which will always have a door on a keyPath:
            if (isKeyPath)
            {
                roomInfo[1] = simpleDataPacket.getCurrentClearanceLevel();
                if (lengthOfPath > 1)
                {
                    firstRoomInPath.setInformation(roomInfo[1], roomInfo[0], roomInfo[1], 0);
                }
                else
                {
                    firstRoomInPath.setInformation(roomInfo[1], roomInfo[0], roomInfo[1], (byte)(roomInfo[1] + 1));
                }
            }
            else
            {
                byte doorClearance = simpleDataPacket.getRandomClearanceLevel(random_thread);
                roomInfo[1] = (byte)Mathf.Max(doorClearance, roomInfo[1]);
                firstRoomInPath.setInformation(roomInfo[1], roomInfo[0], doorClearance, 0);
            }

            // now, attempt to place the next room. If any room fails, this will return false. If they all succeed, this will return true:
            if (addNextRoomToPath(simpleDataPacket, roomCoords, roomInfo, (byte)(lengthOfPath - 1), isKeyPath, random_thread))
            {
                simpleDataPacket.joinPathArrayElementsIntoBuildingArray();
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        private static bool addNextRoomToPath(SimplifiedLayoutDataPacket simpleDataPacket, byte[] previousRoomCoords, byte[] previousRoomInfo, byte moreRoomsToPlace, bool isKeyPath, System.Random random_thread)
        {
            // If there are no more rooms to place, send 'true' all the way up:
            if (moreRoomsToPlace == 0)
            {
                return(true);
            }

            // I need to set the previous space to 'tried' right away:
            simpleDataPacket.getPathRoom_usingBuildingCoords(previousRoomCoords[0], previousRoomCoords[1]).setHasntBeenTried(false);

            // We need some info to be defined upfront. The last room of a keyPath needs to have a key:
            byte keyToPlace = 0;

            if (moreRoomsToPlace == 1 && isKeyPath)
            {
                keyToPlace = (byte)(simpleDataPacket.getCurrentClearanceLevel() + 1);
            }

            // We assume the first room has been done already, so any later room could get a random door:
            byte doorToPlace = simpleDataPacket.getRandomClearanceLevel(random_thread);

            // The clearance might update based on the door placed:
            byte[] nextRoomInfo = new byte[2];
            nextRoomInfo[1] = (byte)Mathf.Max(doorToPlace, previousRoomInfo[1]);

            byte[] nextRoomCoords = new byte[2];


            // Now, check each of the surrounding rooms, starting with a random one:
            // byte randomDirection = (byte)Random.Range(0, 4);
            byte randomDirection = (byte)random_thread.Next(0, 4);

            for (int index = 0; index < 4; index++)
            {
                switch (randomDirection)
                {
                case Constants.doorID_left:
                    nextRoomCoords[0] = (byte)(previousRoomCoords[0] - 1);
                    nextRoomCoords[1] = previousRoomCoords[1];
                    nextRoomInfo[0]   = Constants.doorID_right;
                    break;

                case Constants.doorID_right:
                    nextRoomCoords[0] = (byte)(previousRoomCoords[0] + 1);
                    nextRoomCoords[1] = previousRoomCoords[1];
                    nextRoomInfo[0]   = Constants.doorID_left;
                    break;

                case Constants.doorID_up:
                    nextRoomCoords[0] = previousRoomCoords[0];
                    nextRoomCoords[1] = (byte)(previousRoomCoords[1] - 1);
                    nextRoomInfo[0]   = Constants.doorID_down;
                    break;

                default: //case Constants.doorID_down:
                    nextRoomCoords[0] = previousRoomCoords[0];
                    nextRoomCoords[1] = (byte)(previousRoomCoords[1] + 1);
                    nextRoomInfo[0]   = Constants.doorID_up;
                    break;
                }



                if (simpleDataPacket.checkIfCoordIsAvailable(nextRoomCoords[0], nextRoomCoords[1]))
                {
                    if (addNextRoomToPath(simpleDataPacket, nextRoomCoords, nextRoomInfo, (byte)(moreRoomsToPlace - 1), isKeyPath, random_thread))
                    {
                        simpleDataPacket.getPathRoom_usingBuildingCoords(nextRoomCoords[0], nextRoomCoords[1]).setInformation(nextRoomInfo[1], nextRoomInfo[0], doorToPlace, keyToPlace);
                        return(true);
                    }
                }

                randomDirection = (byte)((randomDirection + 1) % 4);
            }

            // If we get here, then none of the adjacent spaces had room. We return false, so the room above can try another door:
            return(false);
        }