Last active
June 11, 2017 20:15
-
-
Save Level0gamedev/a38228eac128eb0822081e82c0bfc733 to your computer and use it in GitHub Desktop.
Recursive Division Dungeon Generator (pico-8 code)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dungeon = {} --array for dungeon tiles | |
dungeon.w = 48 | |
dungeon.h = 32 | |
rooms= {} | |
wall_h = 1 | |
grid_w = nil | |
grid_h = nil | |
room_size = 4 | |
--pico8 testing stuff only | |
--pointer | |
pointerx=32 | |
pointery=32 | |
------------------------------ | |
--mapgen starts here | |
------------------------------ | |
--make blank map | |
for i = 1,dungeon.w+2 do | |
dungeon[i] = {} | |
for j = 1,dungeon.h+2 do | |
if i==1 or i==dungeon.w or j==1 or j==dungeon.h then | |
dungeon[i][j]=2 | |
else | |
dungeon[i][j]=1 | |
end | |
end | |
end | |
function make_rooms() | |
local r = {x=2,y=2,w=dungeon.w,h=dungeon.h} | |
add (rooms,r) | |
local ii = 1 | |
while ii>= 1 do | |
local wall_orientation = 2 | |
--check room dimmension and change wall orientation accordingly | |
if rooms[ii].w < rooms[ii].h then | |
if rooms[ii].h >= 2*room_size+2 then | |
wall_orientation = 0 | |
end | |
elseif rooms[ii].w > rooms[ii].h then | |
if rooms[ii].w >= 2*room_size+2 then | |
wall_orientation = 1 | |
end | |
else | |
if rooms[ii].w >= 2*room_size+2 then | |
wall_orientation = randint(0,2) | |
end | |
end | |
if wall_orientation!=2 then | |
if wall_orientation == 1 then | |
local wx =randint(1+room_size, rooms[ii].w-room_size ) | |
if dungeon[rooms[ii].x+wx-1][rooms[ii].y-1] ==2 | |
or dungeon[rooms[ii].x+wx-1][rooms[ii].y+rooms[ii].h] ==2 then | |
if randint(1,3)==1 then | |
wx+=1 | |
else | |
wx-=1 | |
end | |
end --end dungeon room check? | |
for yy=2,rooms[ii].h-1 do | |
dungeon[rooms[ii].x+wx-1][rooms[ii].y+yy]=2 | |
helper=yy | |
end | |
dungeon[rooms[ii].x+wx-1][rooms[ii].y]=34 --door? | |
rooms[ii+1]={ | |
x=rooms[ii].x+wx, | |
y=rooms[ii].y, | |
w=rooms[ii].w-wx, | |
h=rooms[ii].h | |
} | |
rooms[ii]={ | |
x=rooms[ii].x, | |
y=rooms[ii].y, | |
w=wx-1, | |
h=rooms[ii].h | |
} | |
else --wall==1 | |
wy = randint(1+room_size,rooms[ii].h-room_size) | |
if dungeon[rooms[ii].x-1][rooms[ii].y-1] ==18 | |
or dungeon[rooms[ii].x+rooms[ii].w][rooms[ii].y+wy-1] == 18 then | |
if randint(1,3)==1 then | |
wy+=1 | |
else | |
wy-=1 | |
end | |
end | |
for xx = 2,rooms[ii].w-1 do | |
dungeon[rooms[ii].x+xx][rooms[ii].y+wy-1] = 2 | |
helper=xx | |
end | |
dungeon[ rooms[ii].x] [rooms[ii].y+wy-1] =18 | |
rooms[ii+1]={ | |
x=rooms[ii].x, | |
y=rooms[ii].y+wy, | |
w=rooms[ii].w, | |
h=rooms[ii].h-wy | |
} | |
rooms[ii]={ | |
x=rooms[ii].x, | |
y=rooms[ii].y, | |
w=rooms[ii].w, | |
h=wy-1 | |
} | |
end --wall==1 | |
ii+=1 | |
else | |
ii-=1 | |
end --wall==!2 | |
end --of while | |
end -- of make rooms | |
-------------map gen ends here | |
function _update() | |
if btnp(0) then pointerx-=8 end | |
if btnp(1) then pointerx+=8 end | |
if btnp(2) then pointery-=8 end | |
if btnp(3) then pointery+=8 end | |
if btnp(4) then make_rooms() end | |
end | |
--draw the map from dungeon table | |
function _draw() | |
cls() | |
for i = 1,dungeon.w do | |
for j = 1,dungeon.h do | |
local _x = i*8 -8 | |
local _y = j*8 -8 | |
spr(dungeon[i][j],_x,_y) | |
end | |
end | |
camera(pointerx-64,pointery-64) | |
--draw pointer | |
spr(63,pointerx,pointery) | |
print(#rooms,8,8,8) | |
end | |
function randint(minn, maxn) -- helper function for random integer | |
local range=(maxn+1)-minn | |
return flr(rnd(range)+minn) | |
end | |
make_rooms() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment