Advertisement
Lupino

dig.lua

Jul 18th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. local robot = require("robot")
  2. local component = require("component")
  3.  
  4. local currentSlot = 1
  5. local maxSlot = robot.inventorySize()
  6.  
  7. local enableIC = component.isAvailable("inventory_controller")
  8.  
  9. local itemName = 'ignore'
  10.  
  11. function getItemName(slot)
  12.     if not enableIC then
  13.         return ''
  14.     end
  15.  
  16.     local item = component.inventory_controller.getStackInInternalSlot(slot)
  17.     if item then
  18.         return item.name
  19.     else
  20.         return ''
  21.     end
  22. end
  23.  
  24. function up()
  25.     local can, type = robot.detectUp()
  26.     if can then
  27.         robot.swingUp()
  28.         up()
  29.     else
  30.         robot.up()
  31.     end
  32. end
  33.  
  34. function down()
  35.     local can, type = robot.detectDown()
  36.     if can then
  37.         robot.swingDown()
  38.         down()
  39.     else
  40.         robot.down()
  41.     end
  42. end
  43.  
  44. function forward()
  45.     local can, type = robot.detect()
  46.     if can then
  47.         robot.swing()
  48.         forward()
  49.     else
  50.         robot.forward()
  51.     end
  52. end
  53.  
  54. function findItem()
  55.     for s = 1, maxSlot, 1 do
  56.         if getItemName(s) ~= '' then
  57.             return s
  58.         end
  59.     end
  60.     return 0
  61. end
  62.  
  63. function checkSlot()
  64.     local newItemName = getItemName(currentSlot)
  65.     if newItemName ~= '' then
  66.         robot.select(currentSlot)
  67.         return true
  68.     else
  69.         currentSlot = findItem()
  70.         if currentSlot ~= 0 then
  71.             robot.select(currentSlot)
  72.             return true
  73.         end
  74.         return false
  75.     end
  76. end
  77.  
  78. function placeDown()
  79.     local can, type = robot.detectDown()
  80.     if can then
  81.         robot.swingDown()
  82.         placeDown()
  83.     else
  84.         if itemName ~= '' then
  85.             if checkSlot() then
  86.                 robot.placeDown()
  87.             end
  88.         end
  89.     end
  90. end
  91.  
  92. function placeUp()
  93.     local can, type = robot.detectUp()
  94.     if can then
  95.         robot.swingUp()
  96.         placeUp()
  97.     else
  98.         if itemName ~= '' then
  99.             if checkSlot() then
  100.                 robot.placeUp()
  101.             end
  102.         end
  103.     end
  104. end
  105.  
  106. function place()
  107.     local can, type = robot.detect()
  108.     if can then
  109.         robot.swing()
  110.         place()
  111.     else
  112.         if itemName ~= '' then
  113.             if checkSlot() then
  114.                 robot.place()
  115.             end
  116.         end
  117.     end
  118. end
  119.  
  120. function dig()
  121.     forward()
  122.  
  123.     placeDown()
  124.  
  125.     robot.turnLeft()
  126.  
  127.     place()
  128.     up()
  129.  
  130.     place()
  131.     up()
  132.  
  133.     place()
  134.     up()
  135.  
  136.     place()
  137.  
  138.     placeUp()
  139.  
  140.     robot.turnLeft()
  141.     robot.turnLeft()
  142.  
  143.     place()
  144.     down()
  145.  
  146.     place()
  147.     down()
  148.  
  149.     place()
  150.     down()
  151.  
  152.     place()
  153.  
  154.     robot.turnLeft()
  155. end
  156.  
  157. function main()
  158.     while true do
  159.         dig()
  160.     end
  161. end
  162.  
  163. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement