Page 1 of 1

Procedural Dungeon Creation Room Overlap

Posted: Mon Feb 29, 2016 1:59 am
by pikuchan
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
    local badRoom = true

    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)

    until badRoom == false

    --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)
  local badPlacement = false

  --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

Re: Procedural Dungeon Creation Room Overlap

Posted: Mon Feb 29, 2016 2:12 am
by pikuchan
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

Re: Procedural Dungeon Creation Room Overlap

Posted: Mon Feb 29, 2016 2:15 am
by Nuthen224
Just as I was about to respond! :)

Re: Procedural Dungeon Creation Room Overlap

Posted: Mon Feb 29, 2016 3:15 am
by pikuchan
The sad thing is the amount of time that I stared at that before I noticed the problem.

Re: Procedural Dungeon Creation Room Overlap

Posted: Mon Feb 29, 2016 3:41 pm
by Zarty55
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 :)