Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Creates a square maze with specified side length-
- args = {...}
- if #args < 1 then
- print("Usage: maze <size>")
- return
- end
- print("Place torches in slot 1 and fuel in slot 16, then press enter.")
- read()
- turtle.select(16)
- size = tonumber(args[1])
- maze = {}
- for x = 1,size do
- maze[x] = {}
- for y = 1,size do
- maze[x][y] = false
- end
- end
- mx = 1 --X-coordinate
- my = 1 --Y-coordinate
- md = 1 --Direction
- maze[1][1] = true
- -------------------------
- -------------------------
- function isOpen(d)
- x = mx + dx(d)
- y = my + dy(d)
- if x < 1 or y < 1 or x > size or y > size then
- return false
- end
- m = maze[x][y]
- return not m
- end
- -------------------------
- function dx(d)
- if d == 2 then
- return 1
- elseif d == 4 then
- return -1
- end
- return 0
- end
- -------------------------
- function dy(d)
- if d == 1 then
- return 1
- elseif d == 3 then
- return -1
- end
- return 0
- end
- -------------------------
- function face(d)
- if d > 4 then d = d - 4 end
- if math.abs(md - d) == 1 then
- if md > d then
- turnLeft()
- else
- turnRight()
- end
- elseif math.abs(md - d) == 2 then
- turnRight()
- turnRight()
- elseif math.abs(md - d) == 3 then
- if md > d then
- turnRight()
- else
- turnLeft()
- end
- end
- md = d
- end
- -------------------------
- function turnLeft()
- md = md - 1
- if md == 0 then md = 4 end
- turtle.turnLeft()
- end
- function turnRight()
- md = md + 1
- if md == 5 then md = 1 end
- turtle.turnRight()
- end
- -------------------------
- torches = 0
- function carve(t)
- for i = 1,2 do
- if turtle.getFuelLevel() == 0 then
- if not turtle.refuel(1) then
- shell.run("reboot")
- end
- end
- while not turtle.forward() do
- turtle.dig()
- end
- if t then turtle.digDown() end
- end
- mx = mx + dx(md)
- my = my + dy(md)
- maze[mx][my] = true
- if t then
- torches = torches + 1
- if torches == 5 then
- turtle.select(1)
- torches = 0
- turtle.placeDown()
- turtle.select(16)
- end
- end
- end
- -------------------------
- --Fisher-Yates shuffle---
- function shuffle(t)
- local n = #t
- while n >= 2 do
- local k = math.random(n)
- t[n], t[k] = t[k], t[n]
- n = n - 1
- end
- return t
- end
- -------------------------
- function main()
- local last = 0
- local dirs = {1,2,3,4}
- dirs = shuffle(dirs)
- last = md
- for k,v in ipairs(dirs) do
- if isOpen(v) then
- face(v)
- carve(true)
- main()
- end
- end
- face(last + 2)
- carve(false)
- end
- -------------------------
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement