Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function generateGridLayout:generateRooms()
- -- Define which height levels will have vertical connections
- -- These are predetermined with some randomization for variety
- local heightOfHeightLines = {3, 4, 6, 7, 10, 12, 13, 16, 18}
- -- Randomly add optional height lines for variation
- if math.random(1, 3) == 3 then
- table.insert(heightOfHeightLines, 8)
- end
- if not table.find(heightOfHeightLines, 8) then
- table.insert(heightOfHeightLines, 9)
- end
- if math.random(1, 3) == 3 then
- table.insert(heightOfHeightLines, 11)
- end
- if math.random(1, 3) == 3 then
- table.insert(heightOfHeightLines, 14)
- end
- if not table.find(heightOfHeightLines, 14) then
- table.insert(heightOfHeightLines, 15)
- end
- if math.random(1, 5) == 5 then
- table.insert(heightOfHeightLines, 17)
- end
- print("Current seed:", randomSeed)
- assert(self.StartingRoom, "No starting room assigned")
- -- Block diagonals around starting room and create first connection
- self:_blockGridsInDiagonalOrder(self.StartingRoom.HeightIndex, self.StartingRoom.WideIndex)
- self:_blockGrid(1, 7) -- Blocks grid and Height 1 and width 7, this is blocked due to a spawnroom being located near that area, preventing room generation spawning room inside a spawn room.
- self.Grids[simplifyName(self.StartingRoom.HeightIndex - 1, self.StartingRoom.WideIndex)]:markAsRoom()
- local lastWideIndexOfHeightLine = self.StartingRoom.WideIndex
- -- Generate rooms for each height level
- for currentHeight = 2, GRIDS_HEIGHT * ZONES do
- if table.find(heightOfHeightLines, currentHeight) then
- -- This is a height line - create vertical connections
- local chances = heightSettings[currentHeight]
- -- Use alternate settings if the previous level wasn't a height line.
- -- As observed earlier, some height levels are luck-based, and during testing,
- -- it was found that room generation can break when attempting to connect all previous lines.
- -- For example, there is a chance that Height 17 could be either a horizontal or a vertical line.
- -- If it's a horizontal one, then Height 18 has a rule set to establish a connection between all previous lines.
- -- This check ensures that if such a case occurs, it uses an alternate setting that creates two vertical lines
- -- instead of trying to connect all previous ones. Since if it connected all, it would double rooms, and this is not an intended behavior
- if not table.find(heightOfHeightLines, currentHeight - 1) and heightSettings[tostring(currentHeight) .. "-1"] then
- chances = heightSettings[tostring(currentHeight) .. "-1"]
- end
- local connections
- if chances then
- connections = rollChance(chances)
- end
- local randomWide = self:_generateHeightLine(currentHeight, connections)
- lastWideIndexOfHeightLine = randomWide
- else
- -- This is a regular level - create horizontal lines in both directions
- local previousDirectionRoomsUsed = nil
- for index = 1, 2 do
- local direction = index == 1 and 1 or -1 -- 1 = right, -1 = left
- local chances = lineSettings[currentHeight]
- -- Dynamically adjust chances based on previous direction's room count
- -- This creates more balanced layouts
- if previousDirectionRoomsUsed and previousDirectionRoomsUsed <= 4 then
- -- Previous direction was short, make this direction longer
- chances[3].reward = math.random(5, 8)
- chances[4] = nil
- chances[5] = nil
- elseif previousDirectionRoomsUsed and previousDirectionRoomsUsed >= 8 then
- -- Previous direction was long, make this direction shorter
- chances[1].chance = 65
- chances[2].chance = 55
- chances[3].chance = 35
- chances[4].chance = 80
- chances[5] = nil
- end
- local rooms = rollChance(chances)
- self.Grids[simplifyName(currentHeight, lastWideIndexOfHeightLine)]:markAsRoom()
- self:_generateLine(currentHeight, lastWideIndexOfHeightLine, direction, rooms)
- previousDirectionRoomsUsed = rooms
- end
- end
- end
- -- This selects how many additional rooms will spawn at the first lane
- -- For more variety, since without it the first lane of halls would just be a long line occasionally stretching upward.
- local roomChances = {
- {chance = 80, reward = 2},
- {chance = 35, reward = 1},
- {chance = 10, reward = 3},
- {chance = 5, reward = 0},
- }
- for index = 1, rollChance(roomChances) do
- local foundUsableRoom = false
- while not foundUsableRoom do
- local secondRowConnections = gridsInUse["2"]
- local randomSecondRowRoom = secondRowConnections[math.random(1, #secondRowConnections)]
- local roomBelow = self.Grids[simplifyName(randomSecondRowRoom.HeightIndex-1, randomSecondRowRoom.WideIndex)]
- if roomBelow.GridIsInUse or roomBelow.isDisabled then
- continue
- end
- self:_blockGridsInVerticalHorizontalOrder(roomBelow.HeightIndex, roomBelow.WideIndex)
- roomBelow:markAsRoom()
- foundUsableRoom = true
- end
- end
- -- Calculate connections between all adjacent grids
- self:_countAdjutantGrids()
- -- Room placement phase - match 3D room models to grid cells
- self:_placeRooms()
- -- Palces down doors in the room connections
- for i, door: Model in doorsInGrids do
- local zone = door:GetAttribute("Zone")
- local doorPresets = game.ServerStorage.Rooms:FindFirstChild("Zone"..tostring(zone)):FindFirstChild("DoorsPresets")
- local foundDoor = nil
- for j, doorPreset in doorPresets:GetChildren() do
- if string.find(doorPreset.Name, "Push") then
- foundDoor = doorPreset
- break
- end
- end
- local cloned = foundDoor:Clone()
- cloned:PivotTo(door.CFrame)
- door:Destroy()
- cloned.Parent = workspace
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment