Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with a procedurally generated dungeon with preset room types?

Asked by 3 years ago
Edited 3 years ago

As the title says, for the past couple days I've been working on making a procedural generation with preset room types. I'm not an advanced scripter, and I'm not advanced in lua either, but I know my way around the language. I've gotten some code done, but at the moment all it does is pick a random room out of 3 rooms and place it in a doorway of main central room. I'll post my code, but be warned it's probably the worst mess you'll ever see. This is inside a module script:

local room1 = game.ReplicatedStorage.Room1
local room2 = game.ReplicatedStorage.Room2
local deadend = game.ReplicatedStorage.DeadEnd

local BackroomsGeneration = {
}

BackroomsGeneration.LoadChunksRoom1Main = function()
    local randomness = math.random(1,3)
    local room1 = game.ReplicatedStorage.Room1:Clone()
    local room2 = game.ReplicatedStorage.Room2:Clone()
    local deadend = game.ReplicatedStorage.DeadEnd:Clone()
if randomness == 1 then
    room1.Parent = workspace
    room1.PrimaryPart.CFrame = game.Workspace.Room1.PrimaryPart.CFrame
        room1.PrimaryPart.CFrame = game.Workspace.Room1.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(180), 0);
elseif randomness == 2 then
    room2.Parent = workspace
        room2.PrimaryPart.CFrame = game.Workspace.Room1.PrimaryPart.CFrame
elseif randomness == 3 then
    deadend.Parent = workspace
        deadend.CFrame = game.Workspace.Room1.PrimaryPart.CFrame
        deadend.CFrame = game.Workspace.Room1.PrimaryPart.CFrame * CFrame.Angles(math.rad(90), math.rad(0), math.rad(0));
    end
end

BackroomsGeneration.LoadChunksRoom1D1 = function()
    local randomness = math.random(1,3)
    local room1 = game.ReplicatedStorage.Room1:Clone()
    local room2 = game.ReplicatedStorage.Room2:Clone()
    local deadend = game.ReplicatedStorage.DeadEnd:Clone()
    print(randomness)
    if randomness == 1 then
        room1.Parent = workspace
        room1.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr1.CFrame
        room1.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr1.CFrame * CFrame.Angles(0, math.rad(0), 0);
    elseif randomness == 2 then
        room2.Parent = workspace
        room2.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr1.CFrame
        room2.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr1.CFrame * CFrame.Angles(0, math.rad(180), 0);
    elseif randomness == 3 then
        deadend.Parent = workspace
        deadend.CFrame = game.Workspace.Room1.DoorSpawnr1.CFrame
        deadend.CFrame = game.Workspace.Room1.DoorSpawnr1.CFrame * CFrame.Angles(math.rad(90), math.rad(0), math.rad(0));
    end
end

BackroomsGeneration.LoadChunksRoom1D2 = function()
    local randomness = math.random(1,3)
    local room1 = game.ReplicatedStorage.Room1:Clone()
    local room2 = game.ReplicatedStorage.Room2:Clone()
    local deadend = game.ReplicatedStorage.DeadEnd:Clone()
    print(randomness)
    if randomness == 1 then
room1.Parent = workspace
room1.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr2.CFrame
        room1.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr2.CFrame * CFrame.Angles(0, math.rad(0), 0);
elseif randomness == 2 then
    room2.Parent = workspace
        room2.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr2.CFrame
        room2.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr2.CFrame * CFrame.Angles(0, math.rad(180), 0);
elseif randomness == 3 then
    deadend.Parent = workspace
        deadend.CFrame = game.Workspace.Room1.DoorSpawnr2.CFrame
        deadend.CFrame = game.Workspace.Room1.DoorSpawnr2.CFrame * CFrame.Angles(math.rad(90), math.rad(0), math.rad(0));
end
end

BackroomsGeneration.LoadChunksRoom1D3 = function()
    local randomness = math.random(1,3)
    local room1 = game.ReplicatedStorage.Room1:Clone()
    local room2 = game.ReplicatedStorage.Room2:Clone()
    local deadend = game.ReplicatedStorage.DeadEnd:Clone()
    print(randomness)
    if randomness == 1 then
        room1.Parent = workspace
        room1.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr3.CFrame
        room1.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr3.CFrame * CFrame.Angles(0, math.rad(180), 0);
    elseif randomness == 2 then
        room2.Parent = workspace
        room2.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr3.CFrame
        room1.PrimaryPart.CFrame = game.Workspace.Room1.DoorSpawnr3.CFrame * CFrame.Angles(0, math.rad(0), 0);
    elseif randomness == 3 then
        deadend.Parent = workspace
        deadend.CFrame = game.Workspace.Room1.DoorSpawnr3.CFrame
        deadend.CFrame = game.Workspace.Room1.DoorSpawnr3.CFrame * CFrame.Angles(math.rad(90), math.rad(0), math.rad(0));
    end
end


        return BackroomsGeneration

If you don't care to read that mess, all it does is pick random rooms to put in each doorway on each side of the central room. The next script just runs the functions from the module script.

local Generator = require(game.ServerScriptService.BackroomsGen)
debounce = true


if debounce == true then
    Generator.NorthAttatchGen()
    Generator.EastAttatchGen()
    Generator.SouthAttatchGen()
    Generator.WestAttatchGen()
end

This is all I have at the moment. Could anyone point me in the right direction for how to make this an actual generator that will place new rooms and build off of them? Such as, when a new room gets added to a doorway, it places new rooms in front of the new doorways that are generated? Thanks. If anyone is interested in helping and needs more info, feel free to ask.

Answer this question