Guest User

Untitled

a guest
Nov 23rd, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.55 KB | Gaming | 0 0
  1. function generateGridLayout:generateRooms()
  2.     -- Define which height levels will have vertical connections
  3.     -- These are predetermined with some randomization for variety
  4.     local heightOfHeightLines = {3, 4, 6, 7, 10, 12, 13, 16, 18}
  5.  
  6.     -- Randomly add optional height lines for variation
  7.     if math.random(1, 3) == 3 then
  8.         table.insert(heightOfHeightLines, 8)
  9.     end
  10.     if not table.find(heightOfHeightLines, 8) then
  11.         table.insert(heightOfHeightLines, 9)
  12.     end
  13.     if math.random(1, 3) == 3 then
  14.         table.insert(heightOfHeightLines, 11)
  15.     end
  16.     if math.random(1, 3) == 3 then
  17.         table.insert(heightOfHeightLines, 14)
  18.     end
  19.     if not table.find(heightOfHeightLines, 14) then
  20.         table.insert(heightOfHeightLines, 15)
  21.     end
  22.     if math.random(1, 5) == 5 then
  23.         table.insert(heightOfHeightLines, 17)
  24.     end
  25.  
  26.     print("Current seed:", randomSeed)
  27.     assert(self.StartingRoom, "No starting room assigned")
  28.  
  29.     -- Block diagonals around starting room and create first connection
  30.     self:_blockGridsInDiagonalOrder(self.StartingRoom.HeightIndex, self.StartingRoom.WideIndex)
  31.     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.
  32.     self.Grids[simplifyName(self.StartingRoom.HeightIndex - 1, self.StartingRoom.WideIndex)]:markAsRoom()
  33.  
  34.     local lastWideIndexOfHeightLine = self.StartingRoom.WideIndex
  35.  
  36.     -- Generate rooms for each height level
  37.     for currentHeight = 2, GRIDS_HEIGHT * ZONES do
  38.         if table.find(heightOfHeightLines, currentHeight) then
  39.             -- This is a height line - create vertical connections
  40.             local chances = heightSettings[currentHeight]
  41.  
  42.             -- Use alternate settings if the previous level wasn't a height line.
  43.             -- As observed earlier, some height levels are luck-based, and during testing,
  44.             -- it was found that room generation can break when attempting to connect all previous lines.
  45.             -- For example, there is a chance that Height 17 could be either a horizontal or a vertical line.
  46.             -- If it's a horizontal one, then Height 18 has a rule set to establish a connection between all previous lines.
  47.             -- This check ensures that if such a case occurs, it uses an alternate setting that creates two vertical lines
  48.             -- instead of trying to connect all previous ones. Since if it connected all, it would double rooms, and this is not an intended behavior
  49.             if not table.find(heightOfHeightLines, currentHeight - 1) and heightSettings[tostring(currentHeight) .. "-1"] then
  50.                 chances = heightSettings[tostring(currentHeight) .. "-1"]
  51.             end
  52.  
  53.             local connections
  54.             if chances then
  55.                 connections = rollChance(chances)
  56.             end
  57.  
  58.             local randomWide = self:_generateHeightLine(currentHeight, connections)
  59.             lastWideIndexOfHeightLine = randomWide
  60.         else
  61.             -- This is a regular level - create horizontal lines in both directions
  62.             local previousDirectionRoomsUsed = nil
  63.             for index = 1, 2 do
  64.                 local direction = index == 1 and 1 or -1  -- 1 = right, -1 = left
  65.  
  66.                 local chances = lineSettings[currentHeight]
  67.  
  68.                 -- Dynamically adjust chances based on previous direction's room count
  69.                 -- This creates more balanced layouts
  70.                 if previousDirectionRoomsUsed and previousDirectionRoomsUsed <= 4 then
  71.                     -- Previous direction was short, make this direction longer
  72.                     chances[3].reward = math.random(5, 8)
  73.                     chances[4] = nil
  74.                     chances[5] = nil
  75.                 elseif previousDirectionRoomsUsed and previousDirectionRoomsUsed >= 8 then
  76.                     -- Previous direction was long, make this direction shorter
  77.                     chances[1].chance = 65
  78.                     chances[2].chance = 55
  79.                     chances[3].chance = 35
  80.                     chances[4].chance = 80
  81.                     chances[5] = nil
  82.                 end
  83.  
  84.                 local rooms = rollChance(chances)
  85.                 self.Grids[simplifyName(currentHeight, lastWideIndexOfHeightLine)]:markAsRoom()
  86.                 self:_generateLine(currentHeight, lastWideIndexOfHeightLine, direction, rooms)
  87.                 previousDirectionRoomsUsed = rooms
  88.             end
  89.         end
  90.     end
  91.    
  92.     -- This selects how many additional rooms will spawn at the first lane
  93.     -- For more variety, since without it the first lane of halls would just be a long line occasionally stretching upward.
  94.     local roomChances = {
  95.         {chance = 80, reward = 2},
  96.         {chance = 35, reward = 1},
  97.         {chance = 10, reward = 3},
  98.         {chance = 5, reward = 0},
  99.     }
  100.  
  101.     for index = 1, rollChance(roomChances) do
  102.         local foundUsableRoom = false
  103.         while not foundUsableRoom do
  104.             local secondRowConnections = gridsInUse["2"]
  105.             local randomSecondRowRoom = secondRowConnections[math.random(1, #secondRowConnections)]
  106.             local roomBelow = self.Grids[simplifyName(randomSecondRowRoom.HeightIndex-1, randomSecondRowRoom.WideIndex)]
  107.             if roomBelow.GridIsInUse or roomBelow.isDisabled then
  108.                 continue
  109.             end
  110.             self:_blockGridsInVerticalHorizontalOrder(roomBelow.HeightIndex, roomBelow.WideIndex)
  111.             roomBelow:markAsRoom()
  112.             foundUsableRoom = true
  113.         end
  114.     end
  115.  
  116.     -- Calculate connections between all adjacent grids
  117.     self:_countAdjutantGrids()
  118.  
  119.     -- Room placement phase - match 3D room models to grid cells
  120.     self:_placeRooms()
  121.    
  122.     -- Palces down doors in the room connections
  123.     for i, door: Model in doorsInGrids do
  124.         local zone = door:GetAttribute("Zone")
  125.         local doorPresets = game.ServerStorage.Rooms:FindFirstChild("Zone"..tostring(zone)):FindFirstChild("DoorsPresets")
  126.         local foundDoor = nil
  127.         for j, doorPreset in doorPresets:GetChildren() do
  128.             if string.find(doorPreset.Name, "Push") then
  129.                 foundDoor = doorPreset
  130.                 break
  131.             end
  132.         end
  133.  
  134.         local cloned = foundDoor:Clone()
  135.         cloned:PivotTo(door.CFrame)
  136.         door:Destroy()
  137.         cloned.Parent = workspace
  138.     end
  139. end
Advertisement
Add Comment
Please, Sign In to add comment