PaffcioStudio

Turtle - Goto

Feb 2nd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. rednet.open("right")
  2. local tArgs = { ... }
  3. local gox = tonumber(tArgs[1])
  4. local goy = tonumber(tArgs[2])
  5. local goz = tonumber(tArgs[3])
  6. if #tArgs ~= 3 then
  7. print("Uzycie: goto <x> <y> <z>")
  8. end
  9. local digBlocks = false
  10. local goneUp = 0
  11. local dir = 0
  12. function forward()
  13. while not turtle.forward() do
  14.   if turtle.getFuelLevel() == 0 then
  15.    turtle.select(1)
  16.    turtle.refuel(1)
  17.   end
  18.   if digBlocks then
  19.    turtle.dig()
  20.   else
  21.    turtle.up()
  22.    goneUp = goneUp + 1
  23.   end
  24. end
  25. while goneUp > 0 and not turtle.detectDown() do
  26.   turtle.down()
  27.   goneUp = goneUp - 1
  28. end
  29. end
  30. function up()
  31. while not turtle.up() do
  32.   if turtle.getFuelLevel() == 0 then
  33.    turtle.select(1)
  34.    turtle.refuel(1)
  35.   end
  36.   if digBlocks then
  37.    turtle.digUp()
  38.   end
  39. end
  40. end
  41. function down()
  42. while not turtle.down() do
  43.   if turtle.getFuelLevel() == 0 then
  44.    turtle.select(1)
  45.    turtle.refuel(1)
  46.   end
  47.   if digBlocks then
  48.    turtle.digDown()
  49.   end
  50. end
  51. end
  52. function getPos()
  53. cx, cy, cz = gps.locate(10)
  54. end
  55. function getDir()
  56. getPos()
  57. ox, oy, oz = cx, cy, cz
  58. forward()
  59. getPos()
  60. if oz > cz then dir = 0
  61. elseif oz < cz then dir = 2
  62. elseif ox < cx then dir = 1
  63. elseif ox > cx then dir = 3 end
  64. turtle.back()
  65. getPos()
  66. end
  67. function turn(d)
  68. getDir()
  69. while dir ~= d do
  70.   turtle.turnRight()
  71.   dir = dir + 1
  72.   if dir == 4 then dir = 0 end
  73. end
  74. end
  75. function moveX()
  76. getPos()
  77. if gox > cx then
  78.   turn(1)
  79.   for x = 1, gox - cx do
  80.    forward()
  81.    cx = cx + 1
  82.   end
  83. elseif gox < cx then
  84.   turn(3)
  85.   for x = 1, cx - gox do
  86.    forward()
  87.    cx = cx - 1
  88.   end
  89. end
  90. end
  91. function moveZ()
  92. getPos()
  93. if goz > cz then
  94.   turn(2)
  95.   for z = 1, goz - cz do
  96.    forward()
  97.    cz = cz + 1
  98.   end
  99. elseif goz < cz then
  100.   turn(0)
  101.   for z = 1, cz - goz do
  102.    forward()
  103.    cz = cz - 1
  104.   end
  105. end
  106. end
  107. function moveY()
  108. getPos()
  109. if goy > cy then
  110.   for z = 1, goy - cy do
  111.    up()
  112.    cy = cy + 1
  113.   end
  114. elseif goy < cy then
  115.   for z = 1, cy - goy do
  116.    down()
  117.    cy = cy - 1
  118.   end
  119. end
  120. end
  121. getPos()
  122. if goy > cy then
  123. moveY()
  124. moveX()
  125. moveZ()
  126. else
  127. moveX()
  128. moveZ()
  129. moveY()
  130. end
  131. rednet.close()
Add Comment
Please, Sign In to add comment