Advertisement
tahg

utils

May 15th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. local timeout = 10
  2. function forward()
  3. local t = 0
  4. while not turtle.forward() do
  5. turtle.dig()
  6. turtle.attack()
  7. t = t + 1
  8. if t == timeout then return false end
  9. end
  10. return true
  11. end
  12.  
  13. local function up()
  14. local t = 0
  15. while not turtle.up() do
  16. turtle.digUp()
  17. turtle.attackUp()
  18. t = t + 1
  19. if t == timeout then return false end
  20. end
  21. return true
  22. end
  23.  
  24. local function down()
  25. local t = 0
  26. while not turtle.down() do
  27. turtle.digDown()
  28. turtle.attackDown()
  29. t = t + 1
  30. if t == timeout then return false end
  31. end
  32. return true
  33. end
  34.  
  35. local function timesX(func, n)
  36. if func ~= nil then
  37. for i = 1, n do
  38. if not func() then return false end
  39. end
  40. end
  41. return true
  42. end
  43.  
  44.  
  45. local function shouldMove(x, z, dx, dz)
  46. return (x * dx > 0) or (z * dz > 0)
  47. end
  48.  
  49. function goto(x, y, z)
  50. while turtle.y > y do if not down() then return false end end
  51. while turtle.y < y do if not up() then return false end end
  52. local xx = x - turtle.x
  53. local zz = z - turtle.z
  54. local dx, dz = turtle.dir()
  55. local f = shouldMove(xx, zz, dx, dz)
  56. local b = shouldMove(xx, zz, -dx, -dz)
  57. local l = shouldMove(xx, zz, dz, -dx)
  58. local r = shouldMove(xx, zz, -dz, dx)
  59. if f and not timesX(forward, xx * dx + zz * dz) then return false end
  60. if l then turtle.turnLeft() if not timesX(forward, xx * dz - zz * dx) then return false end end
  61. if r then turtle.turnRight() if not timesX(forward, zz * dx - xx * dz) then return false end end
  62. if b then
  63. if l then turtle.turnLeft()
  64. else
  65. if not r then turtle.turnRight() end
  66. turtle.turnRight()
  67. end
  68. if not timesX(forward, xx * -dx + zz * -dz) then return false end
  69. end
  70. return x == turtle.x and y == turtle.y and z == turtle.z
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement