Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- args = {...}
- -- Check arguments
- if #args ~= 4 then
- print("Usage: landmark <x> <y> <z> <facing N/E/S/W>")
- return
- end
- local validInputs = true
- x, y, z = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
- facing = string.lower( args[4] )
- if x == nil or y == nil or z == nil then
- print("Invalid Pos <x> <y> <z> (number)")
- validInputs = false
- end
- if facing ~= "n" and facing ~= "e" and facing ~= "s" and facing ~= "w" then
- print("Invalid facing <N,E,S,W> (North, East, South, West)")
- validInputs = false
- end
- -- Bad inputs
- if not validInputs then
- return
- end
- function getDir()
- if facing == "n" then return "North"
- elseif facing == "e" then return "East"
- elseif facing == "s" then return "South"
- elseif facing == "w" then return "West"
- else return "..."
- end
- end
- -- Movement
- -- direction = [L,R]
- function turn(direction)
- direction = string.upper( direction )
- if direction == "L" then
- if turtle.turnLeft() then
- if facing == "n" then facing = "w"
- elseif facing == "e" then facing = "n"
- elseif facing == "s" then facing = "e"
- elseif facing == "w" then facing = "s"
- end
- end
- elseif direction == "R" then
- if turtle.turnRight() then
- if facing == "n" then facing = "e"
- elseif facing == "e" then facing = "s"
- elseif facing == "s" then facing = "w"
- elseif facing == "w" then facing = "n"
- end
- end
- end
- end
- -- axis = [F,B,U,D]
- function move(axis)
- axis = string.upper( axis )
- if axis ~= "F" and axis ~= "B" and axis ~= "U" and axis ~= "D" then
- print("pos.move( "..axis.." ) invalid argument")
- return
- end
- if axis == "U" and turtle.up() then
- y = y + 1
- elseif axis == "D" and turtle.down() then
- y = y - 1
- elseif axis == "F" and turtle.forward() then
- if facing == "n" then z = z - 1
- elseif facing == "e" then x = x + 1
- elseif facing == "s" then z = z + 1
- elseif facing == "w" then x = x - 1
- end
- elseif axis == "B" and turtle.back() then
- if facing == "n" then z = z + 1
- elseif facing == "e" then x = x - 1
- elseif facing == "s" then z = z - 1
- elseif facing == "w" then x = x + 1
- end
- else
- -- La turtle n'a pas pu effectuer le mouvement
- return false
- end
- return true
- end
- --GOTO
- function gotoabs(_x,_y,_z)
- print("wait 10s")
- sleep(10)
- -- facing X
- if _x < x and facing ~= "w" then
- if facing == "n" then turn("L")
- elseif facing == "s" then turn("R")
- elseif facing == "e" then
- turn("R")
- turn("R")
- end
- print("now facing W ("..getDir()..")")
- elseif _x > x and facing ~= "e" then
- if facing == "n" then turn("R")
- elseif facing == "s" then turn("L")
- elseif facing == "w" then
- turn("R")
- turn("R")
- end
- print("now facing E ("..getDir()..")")
- end
- -- Move X
- print("Moving X")
- while _x ~= x do
- if not move("F") then
- fail = 0
- turtle.attack()
- while turtle.detect() do
- turtle.dig()
- fail = fail + 1
- if fail > 16 then break end
- sleep(0.3)
- end
- if not turtle.forward() then break end
- end
- sleep(0.1)
- end
- -- Move Y
- print("Moving Y")
- if _y < y then
- while _y ~= y do
- if not move("D") then
- fail = 0
- turtle.attackDown()
- while turtle.detectDown() do
- turtle.digDown()
- fail = fail + 1
- if fail > 16 then break end
- sleep(0.3)
- end
- if not turtle.Down() then break end
- end
- sleep(0.1)
- end
- elseif _y > y then
- while _y ~= y do
- if not move("U") then
- fail = 0
- turtle.attackUp()
- while turtle.detectUp() do
- turtle.digUp()
- fail = fail + 1
- if fail > 16 then break end
- sleep(0.3)
- end
- if not turtle.Up() then break end
- end
- sleep(0.1)
- end
- end
- -- facing Z
- if _z < z and facing ~= "n" then
- if facing == "w" then turn("R")
- elseif facing == "e" then turn("L")
- end
- print("now facing "..getDir() )
- end
- -- Move Z
- print("Moving Z")
- while _z ~= z do
- if not move("F") then
- fail = 0
- turtle.attack()
- while turtle.detect() do
- turtle.dig()
- fail = fail + 1
- if fail > 16 then break end
- sleep(0.3)
- end
- if not turtle.forward() then break end
- end
- sleep(0.1)
- end
- print("Stop")
- end
- -- Menu
- -- help
- -- clear
- -- get pos
- -- goto absolute
- -- goto relative
- -- exit
- action = nil
- local function menu()
- print("--------------------")
- print("Wait instructions")
- action = string.lower( read() )
- print("--------------------")
- if action == "exit" then
- print("Goodbye")
- return false
- elseif action == "clear" then
- shell.run('clear')
- elseif action == "getpos" then
- print("x: "..x)
- print("y: "..y)
- print("z: "..z)
- print("facing: "..getDir())
- elseif action == "gotoabs" then
- toX, toY, toZ = x, y, z
- print("X ? ("..x..")")
- toX = tonumber( read() )
- print("Y ? ("..y..")")
- toY = tonumber( read() )
- print("Z ? ("..z..")")
- toZ = tonumber( read() )
- print("Goto Abs("..toX..", "..toY..", "..toZ..")")
- gotoabs(toX, toY, toZ)
- else
- if action ~= "help" then print("Unknow: "..action) end
- print("-MENU---------------")
- print("- help")
- print("- clear")
- print("- getPos")
- print("- gotoAbs")
- print("- gotoRel")
- print("- exit")
- end
- return true
- end
- while true do
- if not menu() then break end
- end
- return
Advertisement
Add Comment
Please, Sign In to add comment