Procedural Dungeon Creation Room Overlap

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

Procedural Dungeon Creation Room Overlap

Post 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
pikuchan
Prole
Posts: 16
Joined: Wed Jan 09, 2013 1:01 am

Re: Procedural Dungeon Creation Room Overlap

Post 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
User avatar
Nuthen224
Citizen
Posts: 50
Joined: Sun Jul 28, 2013 9:40 pm

Re: Procedural Dungeon Creation Room Overlap

Post by Nuthen224 »

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

Post by pikuchan »

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

Post 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 :)
Post Reply

Who is online

Users browsing this forum: No registered users and 58 guests