WolpertingerLP

Opencomputers_Robot_StringFarm2

May 1st, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.73 KB | None | 0 0
  1. local robot = require("robot")
  2. local os = require("os")
  3. local component = require("component")
  4.  
  5. local waitTime = 100
  6.  
  7. local maintainSlot = robot.inventorySize()
  8. local nextToolSlot = maintainSlot - 1
  9. local compareSlot = nextToolSlot - 1
  10. local buttonSlot = compareSlot - 1
  11.  
  12. local currentDirection = nil
  13. local searchForButtonDirectionForth = "left"
  14. local searchForButtonDirectionBack = "right"
  15.  
  16. local searchForLeavesDirectionForth = "left"
  17. local searchForLeavesDirectionBack = "right"
  18.  
  19. function updateDirection()
  20.     if currentDirection == nil then
  21.         currentDirection = "forth"
  22.     elseif currentDirection == "forth" then
  23.         currentDirection = "back"
  24.     elseif currentDirection == "back" then
  25.         currentDirection = "forth"
  26.     end
  27. end
  28.  
  29. function checkDirection()
  30.     updateDirection()
  31.    
  32.     turns = 0
  33.     turnDirection = nil
  34.    
  35.     if currentDirection == "forth" then
  36.         turnDirection = searchForLeavesDirectionForth
  37.     elseif currentDirection == "back" then
  38.         turnDirection = searchForLeavesDirectionBack
  39.     end
  40.    
  41.     robot.select(compareSlot)
  42.     for x = 0, 4 do
  43.         if robot.compare() then
  44.             if turns < 3 then
  45.                 if currentDirection == "forth" then
  46.                     if turnDirection == "left" then
  47.                         searchForLeavesDirectionForth = "left"
  48.                     elseif turnDirection == "right" then
  49.                         searchForLeavesDirectionForth = "right"
  50.                     end
  51.                 elseif currentDirection == "back" then
  52.                     if turnDirection == "left" then
  53.                         searchForLeavesDirectionBack = "left"
  54.                     elseif turnDirection == "right" then
  55.                         searchForLeavesDirectionBack = "right"
  56.                     end
  57.                 end
  58.             else
  59.                 if currentDirection == "forth" then
  60.                     if turnDirection == "left" then
  61.                         searchForLeavesDirectionForth = "right"
  62.                         print("Changing search for leaves direction to right")
  63.                     elseif turnDirection == "right" then
  64.                         searchForLeavesDirectionForth = "left"
  65.                         print("Changing search for leaves direction to left")
  66.                     end
  67.                 elseif currentDirection == "back" then
  68.                     if turnDirection == "left" then
  69.                         searchForLeavesDirectionBack = "right"
  70.                         print("Changing search for leaves direction to right")
  71.                     elseif turnDirection == "right" then
  72.                         searchForLeavesDirectionBack = "left"
  73.                         print("Changing search for leaves direction to left")
  74.                     end
  75.                 end
  76.             end
  77.             robot.select(1)
  78.             return
  79.         else
  80.             turns = turns + 1
  81.             if turnDirection == "left" then
  82.                 robot.turnLeft()
  83.             elseif turnDirection == "right" then
  84.                 robot.turnRight()
  85.             end
  86.         end    
  87.     end
  88.     print("Could not determine correct direction :(")
  89. end
  90.  
  91. function pressButton()
  92.     robot.select(buttonSlot)
  93.     turns = 0
  94.     turnDirection = nil
  95.    
  96.     if currentDirection == "forth" then
  97.         turnDirection = searchForButtonDirectionForth
  98.     elseif currentDirection == "back" then
  99.         turnDirection = searchForButtonDirectionBack
  100.     end
  101.    
  102.     for x = 0, 4 do
  103.         if robot.compare() then
  104.             if turns < 3 then
  105.                 if currentDirection == "forth" then
  106.                     if turnDirection == "left" then
  107.                         searchForButtonDirectionForth = "left"
  108.                     elseif turnDirection == "right" then
  109.                         searchForButtonDirectionForth = "right"
  110.                     end
  111.                 elseif currentDirection == "back" then
  112.                     if turnDirection == "left" then
  113.                         searchForButtonDirectionBack = "left"
  114.                     elseif turnDirection == "right" then
  115.                         searchForButtonDirectionBack = "right"
  116.                     end
  117.                 end
  118.             else
  119.                 if currentDirection == "forth" then
  120.                     if turnDirection == "left" then
  121.                         searchForButtonDirectionForth = "right"
  122.                         print("Changing search for button direction to right")
  123.                     elseif turnDirection == "right" then
  124.                         searchForButtonDirectionForth = "left"
  125.                         print("Changing search for button direction to left")
  126.                     end
  127.                 elseif currentDirection == "back" then
  128.                     if turnDirection == "left" then
  129.                         searchForButtonDirectionBack = "right"
  130.                         print("Changing search for button direction to right")
  131.                     elseif turnDirection == "right" then
  132.                         searchForButtonDirectionBack = "left"
  133.                         print("Changing search for button direction to left")
  134.                     end
  135.                 end
  136.             end
  137.             robot.use()
  138.             break
  139.         else
  140.             turns = turns + 1
  141.             if turnDirection == "left" then
  142.                 robot.turnLeft()
  143.             elseif turnDirection == "right" then
  144.                 robot.turnRight()
  145.             end
  146.         end
  147.     end
  148. end
  149.  
  150. function isNextToolAlreadyInInventory()
  151.     toolItem = component.inventory_controller.getStackInInternalSlot(nextToolSlot)
  152.     if toolItem == nil then return false end
  153.     if toolItem.label == "Shears" then
  154.         return true
  155.     end
  156.     return false
  157. end
  158.  
  159. function getToolItem()
  160.     robot.select(maintainSlot)
  161.     component.inventory_controller.equip()
  162.     toolItem = component.inventory_controller.getStackInInternalSlot(maintainSlot)
  163.     component.inventory_controller.equip()
  164.     robot.select(1)
  165.     return toolItem
  166. end
  167.  
  168. function checkTool()
  169.     if isNextToolAlreadyInInventory() == false then
  170.         robot.select(nextToolSlot)
  171.         if robot.suckDown(1) then
  172.             print("Equipping next tool from external storage")
  173.         end
  174.     end
  175.     robot.select(maintainSlot)
  176.     toolItem = getToolItem()
  177.     if toolItem == nil then
  178.         robot.select(nextToolSlot)
  179.         if component.inventory_controller.equip() then
  180.             print("New tool equipped in tool slot")
  181.         else
  182.             print("I´m not happy with my job. Without tools i will quit... for now")
  183.         end
  184.     else
  185.         print(string.format("Current tool damage: %s of %s",toolItem.damage, toolItem.maxDamage))
  186.     end
  187. end
  188.  
  189. function dropAll()
  190.     for x = 1, buttonSlot-1 do
  191.         robot.select(x)
  192.         robot.drop()
  193.     end
  194.     robot.select(1)
  195. end
  196.  
  197. function rest()
  198.     step = waitTime / 10
  199.     print(string.format("\nSleeping for %s seconds...",waitTime))
  200.     for x = 0,9 do
  201.         os.sleep(step)
  202.         print(string.format("Still sleeping for about %s seconds",waitTime - (x+1)*step))
  203.     end
  204.     print("\nGood Morning little SchoolGirl")
  205.     print("New day new chance\n")
  206. end
  207.  
  208. function checkInventory()
  209.     checkupOK = true
  210.     robot.select(maintainSlot)
  211.     toolItem = getToolItem()
  212.     if toolItem == nil then
  213.         print(string.format("Need shears in slot #%s to start my working day",maintainSlot))
  214.         checkupOK = false
  215.     else
  216.         if toolItem.label == "Shears" then
  217.             if(toolItem.damage == 0) then
  218.                 print("Perfect, I found brand new shears, good boy")
  219.             else
  220.                 print("Not perfect but the shears\nwill do the job for a while")
  221.             end
  222.         end
  223.     end
  224.    
  225.     robot.select(compareSlot)
  226.     toolItem = getToolItem()
  227.     if toolItem == nil then
  228.         print(string.format("Need one infested leave in slot #%s\nto start my working day",compareSlot))
  229.         checkupOK = false
  230.     else
  231.         if toolItem.label == "Infested Leaves" then
  232.             print("Perfect, now I know what I have to look about, thanks")
  233.         else
  234.             print(string.format("Need one infested leave in slot #%s\nto start my working day",compareSlot))
  235.             checkupOK = false
  236.         end
  237.     end
  238.  
  239.     robot.select(buttonSlot)
  240.     toolItem = getToolItem()
  241.     if toolItem == nil then
  242.         print(string.format("Need one button in slot #%s\nto summon new leaves",buttonSlot))
  243.         checkupOK = false
  244.     else
  245.         if toolItem.label == "Button" then
  246.             print("Perfect, now I know what I have to press to get this working")
  247.         else
  248.             print(string.format("Need one button in slot #%s\nto summon new leaves",buttonSlot))
  249.             checkupOK = false
  250.         end
  251.     end
  252.     return checkupOK
  253. end
  254.  
  255. function doWork()
  256.     haveWork = true
  257.     while haveWork do
  258.         checkTool()
  259.         robot.select(compareSlot)
  260.         somethingToDo = robot.compare()
  261.         robot.select(1)
  262.         if somethingToDo then
  263.             robot.swing()
  264.             robot.forward()
  265.         else
  266.             robot.forward()
  267.             haveWork = false
  268.         end
  269.     end
  270. end
  271.  
  272. function startUp()
  273.     print("Starting string farm program ...\n")
  274.     os.sleep(1)
  275.     print("Checking inventory ...")
  276.     --if checkInventory() then
  277.         print("Warmup finished/n")
  278.         while true do
  279.             checkDirection()
  280.             doWork()
  281.             dropAll()
  282.             pressButton()
  283.             rest()
  284.         end
  285.     --else
  286.     --  print("Sorry, but I can´t work without a minimum of support")
  287.     --end
  288. end
  289.  
  290. startUp()
Advertisement
Add Comment
Please, Sign In to add comment