Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- COMPUTERCRAFT 3D MAZE SOLVER
- BY 0099
- FOR KONLAB'S COMPETITION
- ]]
- -- directions:
- -- 1. north
- -- 2. east
- -- 3. south
- -- 4. west
- -- 5. up
- -- 6. down
- local opp = {
- [1] = 3,
- [2] = 4,
- [3] = 1,
- [4] = 2,
- [5] = 6,
- [6] = 5,
- }
- local right = {
- [1] = 2,
- [2] = 3,
- [3] = 4,
- [4] = 1,
- }
- local left = {
- [1] = 4,
- [2] = 1,
- [3] = 2,
- [4] = 3,
- }
- function seqGen()
- local f
- f = function(n, l)
- for i=1,n,1 do
- if l == 1 then
- coroutine.yield(tostring(i))
- else
- local c = coroutine.create(f)
- while true do
- local status, a = coroutine.resume(c, n, l-1)
- if status == false then
- error("Coroutine error (1) : "..a)
- end
- if a == "end" then
- break
- end
- coroutine.yield(tostring(i)..","..a)
- end
- end
- end
- return "end"
- end
- local f2 = function()
- for i=1,1000000,1 do
- local c = coroutine.create(f)
- while true do
- local status, a = coroutine.resume(c, i, i)
- if status == false then
- error("Coroutine error (2) : "..a)
- end
- if a == "end" then
- break
- end
- coroutine.yield(a)
- end
- end
- return "end"
- end
- local c = coroutine.create(f2)
- local f3 = function()
- while true do
- local status, a = coroutine.resume(c)
- if status == false then
- error("Coroutine error (3) : "..a)
- end
- if a == "end" then
- error("This maze is impossible. I give up.")
- end
- local t = loadstring("return {"..a.."}")()
- for i=1,#t,1 do
- coroutine.yield(t[i])
- end
- end
- end
- local c2 = coroutine.create(f3)
- local f4 = function()
- local status, a = coroutine.resume(c2)
- if status == false then
- error("Coroutine error (4) : "..a)
- end
- return a
- end
- return f4
- end
- function scan()
- local dirs = {}
- local dirs2 = {}
- local n = 0
- if map[gx] and map[gx][gy] and map[gx][gy][gz] then
- return unpack(map[gx][gy][gz])
- end
- for i=1,4,1 do
- if turtle.detect() == false then
- dirs[dir] = true
- table.insert(dirs2, dir)
- n = n + 1
- end
- turtle.turnRight()
- dir = right[dir]
- end
- if turtle.detectUp() == false then
- dirs[5] = true
- table.insert(dirs2, 5)
- n = n+1
- end
- if turtle.detectDown() == false then
- dirs[6] = true
- table.insert(dirs2, 6)
- n = n+1
- end
- if not map[gx] then
- map[gx] = {}
- end
- if not map[gx][gy] then
- map[gx][gy] = {}
- end
- map[gx][gy][gz] = {dirs, dirs2, n}
- return dirs, dirs2, n
- end
- function goTo(d)
- print("Going to "..tostring(d))
- if d == 5 then
- turtle.up()
- gy = gy+1
- elseif d == 6 then
- turtle.down()
- gy = gy-1
- else
- if d == right[dir] then
- turtle.turnRight()
- elseif d == left[dir] then
- turtle.turnLeft()
- elseif d == opp[dir] then
- turtle.turnRight()
- turtle.turnRight()
- end
- turtle.forward()
- dir = d
- if dir == 1 then
- gz = gz-1
- elseif dir == 2 then
- gx = gx+1
- elseif dir == 3 then
- gz = gz+1
- elseif dir == 4 then
- gx = gx-1
- end
- end
- cameFrom = opp[d]
- end
- function main()
- local gen = seqGen()
- dir = 1
- gx, gy, gz = 0, 0, 0
- map = {}
- while true do
- print(string.format("x=%d, y=%d, z=%d", gx, gy, gz))
- if turtle.getFuelLevel() == 0 then
- turtle.refuel()
- end
- local dirs, dirs2, n = scan()
- print(tostring(n).." directions detected")
- print(unpack(dirs2))
- if n == 1 then
- for i in pairs(dirs) do
- goTo(i)
- end
- elseif n == 2 then
- for i in pairs(dirs) do
- if i ~= cameFrom then
- goTo(i)
- break
- end
- end
- else
- local d = gen()
- print("Sequence: "..tostring(d))
- local j
- for i, d2 in pairs(dirs2) do
- if d2 == cameFrom or cameFrom == nil then
- j = i
- break
- end
- end
- print("Old dir: "..tostring(dirs2[j]))
- j = (j-1+d)%n+1
- print("New dir: "..tostring(dirs2[j]))
- goTo(dirs2[j])
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment