pigiloop

Opencomputers_Robot_StringFarm

Feb 2nd, 2019
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Minecraft Open Computers String Farm Robot
  2. -- Code by WolpertingerLP.de
  3. -- Have fun :)
  4.  
  5. local robot = require("robot")
  6. local os = require("os")
  7. local component = require("component")
  8.  
  9. local waitTime = 100
  10.  
  11. local maintainSlot = robot.inventorySize()
  12. local nextToolSlot = maintainSlot - 1
  13. local compareSlot = nextToolSlot - 1
  14. local buttonSlot = compareSlot - 1
  15.  
  16. local currentDirection = nil
  17. local searchForButtonDirectionForth = "left"
  18. local searchForButtonDirectionBack = "right"
  19.  
  20. local searchForLeavesDirectionForth = "left"
  21. local searchForLeavesDirectionBack = "right"
  22.  
  23. function updateDirection()
  24.     if currentDirection == nil then
  25.         currentDirection = "forth"
  26.     elseif currentDirection == "forth" then
  27.         currentDirection = "back"
  28.     elseif currentDirection == "back" then
  29.         currentDirection = "forth"
  30.     end
  31. end
  32.  
  33. function checkDirection()
  34.     updateDirection()
  35.    
  36.     turns = 0
  37.     turnDirection = nil
  38.    
  39.     if currentDirection == "forth" then
  40.         turnDirection = searchForLeavesDirectionForth
  41.     elseif currentDirection == "back" then
  42.         turnDirection = searchForLeavesDirectionBack
  43.     end
  44.    
  45.     robot.select(compareSlot)
  46.     for x = 0, 4 do
  47.         if robot.compare() then
  48.             if turns < 3 then
  49.                 if currentDirection == "forth" then
  50.                     if turnDirection == "left" then
  51.                         searchForLeavesDirectionForth = "left"
  52.                     elseif turnDirection == "right" then
  53.                         searchForLeavesDirectionForth = "right"
  54.                     end
  55.                 elseif currentDirection == "back" then
  56.                     if turnDirection == "left" then
  57.                         searchForLeavesDirectionBack = "left"
  58.                     elseif turnDirection == "right" then
  59.                         searchForLeavesDirectionBack = "right"
  60.                     end
  61.                 end
  62.             else
  63.                 if currentDirection == "forth" then
  64.                     if turnDirection == "left" then
  65.                         searchForLeavesDirectionForth = "right"
  66.                         print("Changing search for leaves direction to right")
  67.                     elseif turnDirection == "right" then
  68.                         searchForLeavesDirectionForth = "left"
  69.                         print("Changing search for leaves direction to left")
  70.                     end
  71.                 elseif currentDirection == "back" then
  72.                     if turnDirection == "left" then
  73.                         searchForLeavesDirectionBack = "right"
  74.                         print("Changing search for leaves direction to right")
  75.                     elseif turnDirection == "right" then
  76.                         searchForLeavesDirectionBack = "left"
  77.                         print("Changing search for leaves direction to left")
  78.                     end
  79.                 end
  80.             end
  81.             robot.select(1)
  82.             return
  83.         else
  84.             turns = turns + 1
  85.             if turnDirection == "left" then
  86.                 robot.turnLeft()
  87.             elseif turnDirection == "right" then
  88.                 robot.turnRight()
  89.             end
  90.         end    
  91.     end
  92.     print("Could not determine correct direction :(")
  93. end
  94.  
  95. function pressButton()
  96.     robot.select(buttonSlot)
  97.     turns = 0
  98.     turnDirection = nil
  99.    
  100.     if currentDirection == "forth" then
  101.         turnDirection = searchForButtonDirectionForth
  102.     elseif currentDirection == "back" then
  103.         turnDirection = searchForButtonDirectionBack
  104.     end
  105.    
  106.     for x = 0, 4 do
  107.         if robot.compare() then
  108.             if turns < 3 then
  109.                 if currentDirection == "forth" then
  110.                     if turnDirection == "left" then
  111.                         searchForButtonDirectionForth = "left"
  112.                     elseif turnDirection == "right" then
  113.                         searchForButtonDirectionForth = "right"
  114.                     end
  115.                 elseif currentDirection == "back" then
  116.                     if turnDirection == "left" then
  117.                         searchForButtonDirectionBack = "left"
  118.                     elseif turnDirection == "right" then
  119.                         searchForButtonDirectionBack = "right"
  120.                     end
  121.                 end
  122.             else
  123.                 if currentDirection == "forth" then
  124.                     if turnDirection == "left" then
  125.                         searchForButtonDirectionForth = "right"
  126.                         print("Changing search for button direction to right")
  127.                     elseif turnDirection == "right" then
  128.                         searchForButtonDirectionForth = "left"
  129.                         print("Changing search for button direction to left")
  130.                     end
  131.                 elseif currentDirection == "back" then
  132.                     if turnDirection == "left" then
  133.                         searchForButtonDirectionBack = "right"
  134.                         print("Changing search for button direction to right")
  135.                     elseif turnDirection == "right" then
  136.                         searchForButtonDirectionBack = "left"
  137.                         print("Changing search for button direction to left")
  138.                     end
  139.                 end
  140.             end
  141.             robot.use()
  142.             break
  143.         else
  144.             turns = turns + 1
  145.             if turnDirection == "left" then
  146.                 robot.turnLeft()
  147.             elseif turnDirection == "right" then
  148.                 robot.turnRight()
  149.             end
  150.         end
  151.     end
  152. end
  153.  
  154. function isNextToolAlreadyInInventory()
  155.     toolItem = component.inventory_controller.getStackInInternalSlot(nextToolSlot)
  156.     if toolItem == nil then return false end
  157.     if toolItem.label == "Shears" then
  158.         return true
  159.     end
  160.     return false
  161. end
  162.  
  163. function getToolItem()
  164.     robot.select(maintainSlot)
  165.     component.inventory_controller.equip()
  166.     toolItem = component.inventory_controller.getStackInInternalSlot(maintainSlot)
  167.     component.inventory_controller.equip()
  168.     robot.select(1)
  169.     return toolItem
  170. end
  171.  
  172. function checkTool()
  173.     if isNextToolAlreadyInInventory() == false then
  174.         robot.select(nextToolSlot)
  175.         if robot.suckDown(1) then
  176.             print("Equipping next tool from external storage")
  177.         end
  178.     end
  179.     robot.select(maintainSlot)
  180.     toolItem = getToolItem()
  181.     if toolItem == nil then
  182.         robot.select(nextToolSlot)
  183.         if component.inventory_controller.equip() then
  184.             print("New tool equipped in tool slot")
  185.         else
  186.             print("I´m not happy with my job. Without tools i will quit... for now")
  187.         end
  188.     else
  189.         print(string.format("Current tool damage: %s of %s",toolItem.damage, toolItem.maxDamage))
  190.     end
  191. end
  192.  
  193. function dropAll()
  194.     for x = 1, buttonSlot-1 do
  195.         robot.select(x)
  196.         robot.drop()
  197.     end
  198.     robot.select(1)
  199. end
  200.  
  201. function rest()
  202.     step = waitTime / 10
  203.     print(string.format("\nSleeping for %s seconds...",waitTime))
  204.     for x = 0,9 do
  205.         os.sleep(step)
  206.         print(string.format("Still sleeping for about %s seconds",waitTime - (x+1)*step))
  207.     end
  208.     print("\nGood Morning little SchoolGirl")
  209.     print("A new day another chance\n")
  210. end
  211.  
  212. function doWork()
  213.     haveWork = true
  214.     while haveWork do
  215.         checkTool()
  216.         robot.select(compareSlot)
  217.         somethingToDo = robot.compare()
  218.         robot.select(1)
  219.         if somethingToDo then
  220.             robot.swing()
  221.             robot.forward()
  222.         else
  223.             robot.forward()
  224.             haveWork = false
  225.         end
  226.     end
  227. end
  228.  
  229. function startUp()
  230.     print("Starting string farm program ...\n")
  231.     os.sleep(1)
  232.     print("Checking inventory ...")
  233.     print("Warmup finished/n")
  234.     while true do
  235.         checkDirection()
  236.         doWork()
  237.         dropAll()
  238.         pressButton()
  239.         rest()
  240.     end
  241. end
  242.  
  243. startUp()
Advertisement
Add Comment
Please, Sign In to add comment