pluggeman

robot

May 28th, 2023
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.32 KB | None | 0 0
  1. local x = 0
  2. local y = 0
  3. local z = 0
  4. local direction = 0
  5.  
  6. DIRECTION_SOUTH = 0
  7. DIRECTION_WEST = 1
  8. DIRECTION_NORTH = 2
  9. DIRECTION_EAST = 3
  10.  
  11. local CONFIG_PATH = "robot_config"
  12.  
  13. function load()
  14.     if fs.exists(CONFIG_PATH) then
  15.         handle = fs.open(CONFIG_PATH, 'r')
  16.         x = tonumber(handle.readLine())
  17.         y = tonumber(handle.readLine())
  18.         z = tonumber(handle.readLine())
  19.         direction = tonumber(handle.readLine())
  20.         handle.close()
  21.     else
  22.         initGPS()
  23.     end
  24. end
  25.  
  26. function save()
  27.     handle = fs.open(CONFIG_PATH, 'w')
  28.     handle.writeLine(x)
  29.     handle.writeLine(y)
  30.     handle.writeLine(z)
  31.     handle.writeLine(direction)
  32.     handle.close()
  33. end
  34.  
  35. function log()
  36.     print("x: " .. x)
  37.     print("y: " .. y)
  38.     print("z: " .. z)
  39.     print("dir: " .. direction)
  40. end
  41.  
  42. function initGPS()
  43.     term.write("x-pos: ")
  44.     x = tonumber(read())
  45.     term.write("y-pos: ")
  46.     y = tonumber(read())
  47.     term.write("z-pos: ")
  48.     z = tonumber(read())
  49.     term.write("direction (0-3)")
  50.     direction = tonumber(read())
  51.     save()
  52. end
  53.  
  54. function getX()
  55.     return x
  56. end
  57. function getY()
  58.     return y
  59. end
  60. function getZ()
  61.     return z
  62. end
  63. function getDirection()
  64.     return direction
  65. end
  66.  
  67. function moveX(count, cb)
  68.     if count > 0 then
  69.         turnTo(3)
  70.     elseif count < 0 then
  71.         turnTo(1)
  72.     end
  73.     forward(math.abs(count), cb)
  74. end
  75.  
  76. function moveToX(c, cb)
  77.     moveX(c - x, cb)
  78. end
  79.  
  80. function moveY(count, cb)
  81.     if count > 0 then
  82.         up(count, cb)
  83.     elseif count < 0 then
  84.         down(math.abs(count), cb)
  85.     end
  86. end
  87.  
  88. function moveToY(c, cb)
  89.     moveY(c - y, cb)
  90. end
  91.  
  92. function moveZ(count, cb)
  93.     if count > 0 then
  94.         turnTo(0)
  95.     elseif count < 0 then
  96.         turnTo(2)
  97.     end
  98.     forward(math.abs(count), cb)
  99. end
  100.  
  101. function moveToZ(c, cb)
  102.     moveZ(c - z, cb)
  103. end
  104.  
  105. function move(cx, cy, cz, cb)
  106.     moveX(cx, cb)
  107.     moveY(cy, cb)
  108.     moveZ(cz, cb)
  109. end
  110.  
  111. function moveTo(cx, cy, cz, cb)
  112.     moveToX(cx, cb)
  113.     moveToY(cy, cb)
  114.     moveToZ(cz, cb)
  115. end
  116.  
  117. function forward(count, cb)
  118.     count = count or 1
  119.     for i = 1, count do
  120.         while not turtle.forward() do
  121.             turtle.dig()
  122.         end
  123.         if direction == DIRECTION_SOUTH then
  124.             z = z + 1
  125.         elseif direction == DIRECTION_NORTH then
  126.             z = z - 1
  127.         elseif direction == DIRECTION_WEST then
  128.             x = x - 1
  129.         elseif direction == DIRECTION_EAST then
  130.             x = x + 1
  131.         end
  132.         if cb then cb() end
  133.     end
  134. end
  135.  
  136. function back(count, cb)
  137.     count = count or 1
  138.     for i = 1, count do
  139.         if turtle.back() then
  140.             if direction == DIRECTION_SOUTH then
  141.                 z = z + 1
  142.             elseif direction == DIRECTION_NORTH then
  143.                 z = z - 1
  144.             elseif direction == DIRECTION_WEST then
  145.                 x = x - 1
  146.             elseif direction == DIRECTION_EAST then
  147.                 x = x + 1
  148.             end
  149.         end
  150.         if cb then cb() end
  151.     end
  152. end
  153.  
  154. function down(count, cb)
  155.     count = count or 1
  156.     for i = 1, count do
  157.         while not turtle.down() do
  158.             turtle.digDown()
  159.         end
  160.         y = y - 1
  161.         if cb then cb() end
  162.     end
  163. end
  164.  
  165. function up(count, cb)
  166.     count = count or 1
  167.     for i = 1, count do
  168.         while not turtle.up() do
  169.             turtle.digUp()
  170.         end
  171.         y = y + 1
  172.         if cb then cb() end
  173.     end
  174. end
  175.  
  176. function turnleft(count, cb)
  177.     count = count or 1
  178.     for i = 1, count do
  179.         if turtle.turnLeft() then
  180.             if direction <= 0 then
  181.                 direction = 3
  182.             else
  183.                 direction = direction - 1
  184.             end
  185.             if cb then cb() end
  186.         end
  187.     end
  188. end
  189.  
  190. function turnright(count, cb)
  191.     count = count or 1
  192.     for i = 1, count do
  193.         if turtle.turnRight() then
  194.             if direction >= 3 then
  195.                 direction = 0
  196.             else
  197.                 direction = direction + 1
  198.             end
  199.             if cb then cb() end
  200.         end
  201.     end
  202. end
  203.  
  204. function turnTo(dir, cb)
  205.     count = direction - dir
  206.     dif = math.abs(count)
  207.     if dif > 2 then
  208.         count = -(count / dif)
  209.     end
  210.     if count > 0 then
  211.         turnleft(count)
  212.     elseif count < 0 then
  213.         turnright(-count)
  214.     end
  215.     if cb then cb() end
  216. end
  217.  
  218. function dropAmount(slot, amount)
  219.     if amount == nil then
  220.         return turtle.getItemCount(slot)
  221.     elseif amount < 0 then
  222.         return turtle.getItemCount(slot) + amount
  223.     else return amount
  224.     end
  225. end
  226.  
  227. function drop(slot, amount)
  228.     amount = dropAmount(slot, amount)
  229.     turtle.select(slot)
  230.     turtle.drop(amount)
  231. end
  232.  
  233. function dropUp(slot, amount)
  234.     amount = dropAmount(slot, amount)
  235.     turtle.select(slot)
  236.     turtle.dropUp(amount)
  237. end
  238.  
  239. function dropDown(slot, amount)
  240.     amount = dropAmount(slot, amount)
  241.     turtle.select(slot)
  242.     turtle.dropDown(amount)
  243. end
  244.  
  245. function compareItems(s1, s2)
  246.     turtle.select(s1)
  247.     return turtle.compareTo(s2)
  248. end
  249.  
  250. function compareFrontTo(slot)
  251.     turtle.select(slot)
  252.     return turtle.compare()
  253. end
  254.  
  255. function compareDownTo(slot)
  256.     turtle.select(slot)
  257.     return turtle.compareDown()
  258. end
  259.  
  260. function compareUpTo(slot)
  261.     turtle.select(slot)
  262.     return turtle.compareUp()
  263. end
Advertisement
Add Comment
Please, Sign In to add comment