## Procedural Dungeon Creation Room Overlap

General discussion about LÖVE, Lua, game development, puns, and unicorns.
pikuchan
Prole
Posts: 16
Joined: Wed Jan 09, 2013 1:01 am

### Procedural Dungeon Creation Room Overlap

Thanks in advance. For the life of me, I cannot figure out why my code is not working. I am trying to have a dungeon with randomly created rooms that never overlap. Attached is the relevant code. Any help would be appreciated.

Code: Select all

function createDungeon()
--start generating rooms
local roomWidth = 1
local roomHeight = 1
local roomX = 1
local roomY = 1

for roomCount=1, roomsToCreate do

repeat
--pick some random x,y coordinates on the map and also randomly determine the size of our room
--rooms of the same size are boring
roomWidth = love.math.random(minRoomWidth, maxRoomWidth)
roomHeight = love.math.random(minRoomHeight, maxRoomHeight)
roomX = love.math.random(1, dungeonWidth -  roomWidth)
roomY = love.math.random(1, dungeonHeight - roomHeight)

--badRoom = CheckRoomPlacement(roomX, roomY, roomWidth, roomHeight)
--love.window.showMessageBox("Overlap check complete", tostring(doRoomsOverlap), "error")
badRoom = CheckRoomPlacement(roomX, roomY, roomWidth, roomHeight)

--now let's set that position in our table to be a 1 (tile)
dungeonRooms[roomCount] = {roomX, roomY, roomWidth, roomHeight}

for xPos=roomX, roomX + roomWidth do
for yPos=roomY, roomY + roomHeight do
dungeon[yPos][xPos] = 1
end -- end for
end -- end for

end -- end for loop
end

Code: Select all

function CheckRoomPlacement(x, y, width, height)

--if we try to place a 1 where there is already a 1, then we are overlapping our rooms
for xPos=x, x + width do
for yPos=y, y + height do
if dungeon[y][x] == 1 then
love.window.showMessageBox("True", "Bad room placement found", "error")
return true
end
end
end

return false
end

pikuchan
Prole
Posts: 16
Joined: Wed Jan 09, 2013 1:01 am

### Re: Procedural Dungeon Creation Room Overlap

Ugh....finally figured it out

Code: Select all

for xPos=x, x + width do
for yPos=y, y + height do
if dungeon[yPos][xPos] == 1 then

Nuthen224
Citizen
Posts: 50
Joined: Sun Jul 28, 2013 9:40 pm

### Re: Procedural Dungeon Creation Room Overlap

Just as I was about to respond!

pikuchan
Prole
Posts: 16
Joined: Wed Jan 09, 2013 1:01 am

### Re: Procedural Dungeon Creation Room Overlap

The sad thing is the amount of time that I stared at that before I noticed the problem.

Zarty55
Citizen
Posts: 79
Joined: Thu Jul 25, 2013 2:36 am

### Re: Procedural Dungeon Creation Room Overlap

pikuchan wrote:The sad thing is the amount of time that I stared at that before I noticed the problem.
Try using the rubber duck debugging method

### Who is online

Users browsing this forum: No registered users and 16 guests