nSun

turtle_pos

Mar 26th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. args = {...}
  2.  
  3. -- Check arguments
  4. if #args ~= 4  then
  5.   print("Usage: landmark <x> <y> <z> <facing N/E/S/W>")
  6.   return
  7. end
  8.  
  9. local validInputs = true
  10. x, y, z = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
  11. facing = string.lower( args[4] )
  12.  
  13. if x == nil or y == nil or z == nil then
  14.     print("Invalid Pos <x> <y> <z>  (number)")
  15.     validInputs = false
  16. end
  17.  
  18. if facing ~= "n" and facing ~= "e" and facing ~= "s" and facing ~= "w" then
  19.     print("Invalid facing <N,E,S,W> (North, East, South, West)")
  20.     validInputs = false
  21. end
  22.  
  23. -- Bad inputs
  24. if not validInputs then
  25.     return
  26. end
  27.  
  28. function getDir()
  29.     if facing == "n" then return "North"
  30.     elseif facing == "e" then return "East"
  31.     elseif facing == "s" then return "South"
  32.     elseif facing == "w" then return "West"
  33.     else return "..."
  34.     end
  35. end
  36.  
  37. -- Movement
  38. -- direction = [L,R]
  39. function turn(direction)
  40.     direction = string.upper( direction )
  41.     if direction == "L" then
  42.         if turtle.turnLeft() then
  43.             if facing == "n" then facing  = "w"
  44.             elseif facing == "e" then facing  = "n"
  45.             elseif facing == "s" then facing = "e"
  46.             elseif facing == "w" then facing = "s"
  47.             end
  48.         end
  49.     elseif direction == "R" then
  50.         if turtle.turnRight() then
  51.             if facing == "n" then facing = "e"
  52.             elseif facing == "e" then facing = "s"
  53.             elseif facing == "s" then facing = "w"
  54.             elseif facing == "w" then facing = "n"
  55.             end
  56.         end
  57.     end
  58. end
  59. -- axis = [F,B,U,D]
  60. function move(axis)
  61.     axis = string.upper( axis )
  62.     if axis ~= "F" and axis ~= "B" and axis ~= "U" and axis ~= "D" then
  63.         print("pos.move( "..axis.." ) invalid argument")
  64.         return
  65.     end
  66.     if axis == "U" and turtle.up() then
  67.         y = y + 1
  68.     elseif axis == "D" and turtle.down() then
  69.         y = y - 1
  70.     elseif axis == "F" and turtle.forward() then
  71.         if facing == "n" then z = z - 1
  72.         elseif facing == "e" then x = x + 1
  73.         elseif facing == "s" then z = z + 1
  74.         elseif facing == "w" then x = x - 1
  75.         end
  76.     elseif axis == "B" and turtle.back() then
  77.         if facing == "n" then z = z + 1
  78.         elseif facing == "e" then x = x - 1
  79.         elseif facing == "s" then z = z - 1
  80.         elseif facing == "w" then x = x + 1
  81.         end
  82.     else
  83.         -- La turtle n'a pas pu effectuer le mouvement
  84.         return false
  85.     end
  86.     return true
  87. end
  88.  
  89. --GOTO
  90. function gotoabs(_x,_y,_z)
  91.     print("wait 10s")
  92.     sleep(10)
  93.     -- facing X
  94.     if _x < x and facing ~= "w" then
  95.         if facing == "n" then turn("L")
  96.         elseif facing == "s" then turn("R")
  97.         elseif facing == "e" then
  98.             turn("R")
  99.             turn("R")
  100.         end
  101.         print("now facing W ("..getDir()..")")
  102.     elseif _x > x and facing ~= "e" then
  103.         if facing == "n" then turn("R")
  104.         elseif facing == "s" then turn("L")
  105.         elseif facing == "w" then
  106.             turn("R")
  107.             turn("R")
  108.         end
  109.         print("now facing E ("..getDir()..")")
  110.     end
  111.     -- Move X
  112.     print("Moving X")
  113.     while _x ~= x do
  114.         if not move("F") then
  115.             fail = 0
  116.             turtle.attack()
  117.             while turtle.detect() do
  118.                 turtle.dig()
  119.                 fail  = fail + 1
  120.                 if fail > 16 then break end
  121.                 sleep(0.3)
  122.             end
  123.             if not turtle.forward() then break end
  124.         end
  125.         sleep(0.1)
  126.     end
  127.    
  128.     -- Move Y
  129.     print("Moving Y")
  130.     if _y < y then
  131.         while _y ~= y do
  132.             if not move("D") then
  133.                 fail = 0
  134.                 turtle.attackDown()
  135.                 while turtle.detectDown() do
  136.                     turtle.digDown()
  137.                     fail  = fail + 1
  138.                     if fail > 16 then break end
  139.                     sleep(0.3)
  140.                 end
  141.                 if not turtle.Down() then break end
  142.             end
  143.             sleep(0.1)
  144.         end
  145.     elseif _y > y then
  146.         while _y ~= y do
  147.             if not move("U") then
  148.                 fail = 0
  149.                 turtle.attackUp()
  150.                 while turtle.detectUp() do
  151.                     turtle.digUp()
  152.                     fail  = fail + 1
  153.                     if fail > 16 then break end
  154.                     sleep(0.3)
  155.                 end
  156.                 if not turtle.Up() then break end
  157.             end
  158.             sleep(0.1)
  159.         end
  160.     end
  161.  
  162.     -- facing Z
  163.     if _z < z and facing ~= "n" then
  164.         if facing == "w" then turn("R")
  165.         elseif facing == "e" then turn("L")
  166.         end
  167.         print("now facing "..getDir() )
  168.     end
  169.     -- Move Z
  170.     print("Moving Z")
  171.     while _z ~= z do
  172.         if not move("F") then
  173.             fail = 0
  174.             turtle.attack()
  175.             while turtle.detect() do
  176.                 turtle.dig()
  177.                 fail  = fail + 1
  178.                 if fail > 16 then break end
  179.                 sleep(0.3)
  180.             end
  181.             if not turtle.forward() then break end
  182.         end
  183.         sleep(0.1)
  184.     end
  185.     print("Stop")
  186. end
  187.  
  188.  
  189. -- Menu
  190. -- help
  191. -- clear
  192. -- get pos
  193. -- goto absolute
  194. -- goto relative
  195. -- exit
  196. action = nil
  197. local function menu()
  198.     print("--------------------")
  199.     print("Wait instructions")
  200.     action = string.lower( read() )
  201.         print("--------------------")
  202.     if action == "exit" then
  203.         print("Goodbye")
  204.         return false
  205.     elseif action == "clear" then
  206.         shell.run('clear')
  207.     elseif action == "getpos" then
  208.         print("x: "..x)
  209.         print("y: "..y)
  210.         print("z: "..z)
  211.         print("facing: "..getDir())
  212.     elseif action == "gotoabs" then
  213.         toX, toY, toZ = x, y, z
  214.         print("X ? ("..x..")")
  215.         toX = tonumber( read() )
  216.         print("Y ? ("..y..")")
  217.         toY = tonumber( read() )
  218.         print("Z ? ("..z..")")
  219.         toZ = tonumber( read() )
  220.         print("Goto Abs("..toX..", "..toY..", "..toZ..")")
  221.         gotoabs(toX, toY, toZ)
  222.     else
  223.         if action ~= "help" then print("Unknow: "..action) end
  224.         print("-MENU---------------")
  225.         print("- help")
  226.         print("- clear")
  227.         print("- getPos")
  228.         print("- gotoAbs")
  229.         print("- gotoRel")
  230.         print("- exit")
  231.     end
  232.     return true
  233. end
  234.  
  235. while true do
  236.     if not menu() then break end
  237. end
  238. return
Advertisement
Add Comment
Please, Sign In to add comment