Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Generates a random map based on Depth-First Search,
- Displays on ComputerCraft advanced computer
- ]]--
- args = {...}
- width = 10
- height = 9
- for i,v in ipairs(args) do
- print(v)
- if tonumber(v) ~= nil then
- if i == 1 then width = math.abs(tonumber(v))
- elseif i == 2 then height = math.abs(tonumber(v))
- end
- end
- end
- cellvisit = {}
- for i = 1, width do
- cellvisit[i] = {}
- for j=1, height do
- cellvisit[i][j] = 0
- end
- end
- cellwall = {}
- for i=1, width do
- cellwall[i] = {}
- for j=1, height*2-1 do
- cellwall[i][j] = 0
- end
- end
- cCell = {}
- cCell.x = 1
- cCell.y = 1
- walls = {}
- wallsaround = 0
- local function delay()
- os.sleep(0.002)
- end
- local function setWalls()
- walls = 0
- wallsaround = 0
- if cCell.y == 1 then --up
- walls = walls + 1
- wallsaround = wallsaround+1
- elseif cellvisit[cCell.x][cCell.y-1] == 1 then
- walls = walls + 1
- wallsaround = wallsaround+1
- end
- if cCell.x == width then --right
- walls = walls + 2
- wallsaround = wallsaround+1
- elseif cellvisit[cCell.x+1][cCell.y] == 1 then
- walls = walls + 2
- wallsaround = wallsaround+1
- end
- if cCell.y == height then --down
- walls = walls + 4
- wallsaround = wallsaround+1
- elseif cellvisit[cCell.x][cCell.y+1] == 1 then
- walls = walls + 4
- wallsaround = wallsaround+1
- end
- if cCell.x == 1 then --left
- walls = walls + 8
- wallsaround = wallsaround+1
- elseif cellvisit[cCell.x-1][cCell.y] == 1 then
- walls = walls + 8
- wallsaround = wallsaround+1
- end
- end
- local function render()
- term.setBackgroundColor(colors.black)
- term.clear()
- for x=1, width do
- for y=1, height do
- term.setCursorPos(x*2-1, y*2-1)
- if cellvisit[x][y] == 1 then term.setBackgroundColor(colors.white)
- else term.setBackgroundColor(colors.white) end
- term.write(" ")
- end
- end
- for x=1, width do
- for y=1, height*2 do
- term.setCursorPos(x*2+(y%2)-1, y)
- if cellwall[x][y] == 1 then
- term.setBackgroundColor(colors.white)
- else
- term.setBackgroundColor(colors.black)
- end
- term.write(" ")
- end
- end
- term.setBackgroundColor(colors.orange)
- term.setCursorPos(cCell.x*2-1, cCell.y*2-1)
- term.write(" ")
- delay()
- end
- path = {}--path rotation direction, 1 = up, 2 = right, 3 = down, 4 = left
- pathdist = 0
- exitting = false
- local function backtrack()
- if pathdist > 1 then
- if path[pathdist] == 1 then cCell.y = cCell.y + 1 end
- if path[pathdist] == 2 then cCell.x = cCell.x - 1 end
- if path[pathdist] == 3 then cCell.y = cCell.y - 1 end
- if path[pathdist] == 4 then cCell.x = cCell.x + 1 end
- end
- pathdist = pathdist - 1
- end
- local function foretrack(rotation)
- pathdist = pathdist + 1
- path[pathdist] = rotation
- end
- tempcell = {}
- setWalls()
- repeat
- setWalls()
- direction = math.random(4)
- notwall = bit.band(bit.brshift(walls, direction-1), 1)
- wallschecked = 0
- --print(wallsaround.." "..wallschecked)
- cellvisit[cCell.x][cCell.y] = 1
- if wallsaround >= 4 then
- backtrack()
- else
- checkwalls = {}
- index = 0
- for i=1, 4 do
- if (bit.brshift(walls, i-1))%2 == 0 then
- index = index + 1
- checkwalls[index] = i
- end
- end
- for i=1, 4-wallsaround do
- if index <= 0 then
- print("something went wrong with indexing")
- exitting = true
- break
- end
- direction = checkwalls[math.random(index)]
- notwall = bit.band(bit.brshift(walls, direction-1), 1)
- walloffset = {}
- if notwall == 0 then
- tempcell.x = cCell.x
- tempcell.y = cCell.y
- walloffset.x = 0 walloffset.y = 0
- if direction == 1 then tempcell.y = tempcell.y - 1 walloffset.y = 1 end --up
- if direction == 2 then tempcell.x = tempcell.x + 1 walloffset.x = 0 end --right
- if direction == 3 then tempcell.y = tempcell.y + 1 walloffset.y = 0 end --down
- if direction == 4 then tempcell.x = tempcell.x - 1 walloffset.x = 1 end --left
- --print("cell movement: "..tempcell.x.." "..tempcell.y.." "..direction.." "..walls)
- if cellvisit[tempcell.x][tempcell.y] == 0 then
- if direction == 2 or direction == 4 then
- cellwall[cCell.x-walloffset.x][cCell.y*2-1] = 1
- else
- cellwall[cCell.x][(cCell.y-walloffset.y)*2] = 1
- end
- cCell.x = tempcell.x
- cCell.y = tempcell.y
- cellvisit[cCell.x][cCell.y] = 1
- foretrack(direction)
- break
- end
- end
- wallschecked = wallschecked + bit.blshift(1, direction-1)
- if wallschecked >= 15 then
- break
- end
- delay()
- end
- end
- render()
- delay()
- until (pathdist == 0 and walls == 15 or exitting)
- cCell.x = 1
- cCell.y = 1
- render()
- term.setCursorPos(1, 19)
- io.write("Maze complete!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement