Log In  


hey so i have started making a rogue like and i started with dungeon generation, i am doing this by using Binary Space Partitioning(BSP) but when i go to process my binary tree it only takes the left route and not the right! it appears that the right route is always nil and i cant figure out why and i hope you guys can figure it out.
All help is appreciated!

-- d&!d
-- by anton wolfarth

state = 1

player = {}
-- player.[up,down,left,right] = {still, anim1, anim2}
player.down = {1,2,3}
player.up = {8,9,10}
player.left = {4,5,4}
player.right = {6,7,6}

rooms = {}

err = ""

gold = 0

function aabb(x1,y1,w1,x2,y2,w2)
	return
end

function _init()

end

function _update()
	if(state == 1)then
		generatemap()
		state = 2
	end
	if(state == 2)then

	end
end

function drawrooms(room)
	print(room.x .. ", " .. room.y .. ". " .. room.width .. ", " ..  room.height)

	--rect(room.x, room.y, room.x+room.width, room.y, room.y+room.height, 2)
end

function _draw()
	cls()
	foreach(rooms, drawrooms)
	print(err)
	print(#rooms)
end
-->8
-- map generation
function generatemap()
	-- parent, left, right, depth, x,y,w,h
	tree = {parent = nil,
			left = nil,
			right = nil,
			depth = 0,
			x = 0,
			y = 0,
			width = 256,
			height = 256
	}
	makechildren(5, tree)

	makerooms(tree)
end

function makechildren(maxdepth,parent)
		xy = flr(rnd(2))
		box1 = {} 
		box2 = {}
		if(xy == 0) then
			-- split by x
			box1.width = flr(rnd(parent.width / 3)+parent.width / 3)
			box2.width = parent.width - box1.width
			box1.height = parent.height
			box2.height = parent.height
			box1.x = parent.x
			box2.x = parent.x + box1.width + 1
			box1.y = parent.y
			box2.y = parent.y

		else
			-- split by y
			box1.height = flr(rnd(parent.height/3)+parent.height/3)
			box2.height = parent.height - box1.height
			box1.width = parent.width
			box2.width = parent.width
			box1.y = parent.y
			box2.y = parent.y + box1.height + 1
			box1.x = parent.x
			box2.x = parent.x
		end

		box1.depth = parent.depth + 1
		box2.depth = parent.depth + 1

		parent.left = box1
		parent.right = box2
		box1.parent = parent
		box2.parent = parent		

		if(
				box1.depth < maxdepth and
				box1.width > 5 and
				box1.height > 5
		)then
			makechildren(maxdepth, box1)
		else
			parent.left = nil	
		end

		if(
				box2.depth < maxdepth and
				box2.width > 5 and
				box2.height > 5
		)then
			makechildren(maxdepth, box2)
		else
			parent.right = nil	
		end
end

function makerooms(tree)
	err =  err .. tree.depth .. ", "
 if(tree.left != nil)then
 	makerooms(tree.left)
 	err = err .. "l"
 end

 if(tree.right != nil)then
 	makerooms(tree.right)
 	err = err .. "r"
 end

 if(tree.left == nil and tree.right == nil)then
 	generateroom(tree)
 end
end

function generateroom(leaf)
	add(rooms, {
			x = leaf.x, 
			y = leaf.y,
			width = leaf.width,
			height = leaf.height			
	})
end

once again thanks for any help in advance!



Recursive functions and global variables are a bad mix. Use LOCAL to declare every variable you want local scope. For instance: local box1={}


I will give that a go and report back if it gets solved, tbh im not familiar with lua or this platform, didnt even realise that you had to define the scope locally!

update: seems to be working well! thanks! should probably read up more on how scope works in pico-8!



[Please log in to post a comment]