Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Hill Eater
- -- Version 1.1
- -- By JPiolho
- -- Feel free to copy and edit as long you credit the original author
- -- More Info @ http://bit.ly/zoxn6V
- local curX = 0
- local curY = 0
- local curZ = 0
- local currentFacing = 1
- local visited = {}
- local stack = {}
- local function AddVisited(x,y,z)
- table.insert(visited,{x,y,z});
- end
- local function AddStack(x,y,z)
- table.insert(stack,{x,y,z})
- end
- local function PopStack()
- local val = stack[#stack]
- table.remove(stack,#stack);
- return val[1],val[2],val[3]
- end
- local function IsVisited(x,y,z)
- if z < 0 then return true end
- local v
- for _,v in pairs(visited) do
- if v[1] == x and v[2] == y and v[3] == z then
- return true
- end
- end
- return false
- end
- local function FaceDirection(dir)
- local i
- if dir == 1 and currentFacing == 4 then
- turtle.turnRight()
- currentFacing = 1
- end
- if dir == 4 and currentFacing == 1 then
- turtle.turnLeft()
- currentFacing = 4
- end
- if dir > currentFacing then
- while currentFacing < dir do
- turtle.turnRight()
- currentFacing = currentFacing + 1
- end
- end
- if dir < currentFacing then
- while currentFacing > dir do
- turtle.turnLeft()
- currentFacing = currentFacing - 1
- end
- end
- end
- local hasMoved = false
- local function MoveToCell(x,y,z,mustdig)
- hasMoved = false
- if curX == x and curY == y and curZ == z then
- hasMoved = true
- return
- end
- if x > curX then
- FaceDirection(2)
- elseif x < curX then
- FaceDirection(4)
- elseif y > curY then
- FaceDirection(1)
- elseif y < curY then
- FaceDirection(3)
- end
- if z < curZ then
- if turtle.digDown() or mustdig == false then
- while turtle.down() == false do
- sleep(0.5)
- turtle.digDown()
- end
- else
- if IsVisited(x,y,z) == false then AddVisited(x,y,z) end
- return
- end
- elseif z > curZ then
- if turtle.digUp() or mustdig == false then
- while turtle.up() == false do
- sleep(0.5)
- turtle.digUp()
- end
- else
- if IsVisited(x,y,z) == false then AddVisited(x,y,z) end
- return
- end
- else
- if turtle.dig() or mustdig == false then
- while turtle.forward() == false do
- sleep(0.5)
- turtle.dig()
- end
- else
- if IsVisited(x,y,z) == false then AddVisited(x,y,z) end
- return
- end
- end
- curX = x
- curY = y
- curZ = z
- hasMoved = true
- end
- math.randomseed(os.time())
- AddVisited(curX,curY,curZ)
- AddStack(curX,curY,curZ)
- repeat
- local valids = {}
- if curZ > 0 then
- if IsVisited(curX,curY,curZ-1) == false then
- if turtle.detectDown() then table.insert(valids,{curX,curY,curZ-1}) else AddVisited(curX,curY,curZ-1) end
- end
- end
- if IsVisited(curX,curY,curZ+1) == false then
- if turtle.detectUp() then table.insert(valids,{curX,curY,curZ+1}) else AddVisited(curX,curY,curZ+1) end
- end
- if IsVisited(curX,curY+1,curZ) == false then
- FaceDirection(1)
- if turtle.detect() then table.insert(valids,{curX,curY+1,curZ}) else AddVisited(curX,curY+1,curZ) end
- end
- if IsVisited(curX+1,curY,curZ) == false then
- FaceDirection(2)
- if turtle.detect() then table.insert(valids,{curX+1,curY,curZ}) else AddVisited(curX+1,curY,curZ) end
- end
- if IsVisited(curX,curY-1,curZ) == false then
- FaceDirection(3)
- if turtle.detect() then table.insert(valids,{curX,curY-1,curZ}) else AddVisited(curX,curY-1,curZ) end
- end
- if IsVisited(curX-1,curY,curZ) == false then
- FaceDirection(4)
- if turtle.detect() then table.insert(valids,{curX-1,curY,curZ}) else AddVisited(curX-1,curY,curZ) end
- end
- if #valids > 0 then
- AddStack(curX,curY,curZ)
- local targetX
- local targetY
- local targetZ
- local i = math.random(1,#valids)
- targetX = valids[i][1]
- targetY = valids[i][2]
- targetZ = valids[i][3]
- AddVisited(targetX,targetY,targetZ)
- MoveToCell(targetX,targetY,targetZ,true)
- if hasMoved then
- end
- else
- local targetX, targetY,targetZ
- targetX, targetY,targetZ = PopStack()
- repeat
- MoveToCell(targetX,targetY,targetZ,false)
- until hasMoved == true
- end
- until #stack == 0
Advertisement
Add Comment
Please, Sign In to add comment