Advertisement
FERROUSSAGE

farm tree robot

Aug 23rd, 2021 (edited)
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.85 KB | None | 0 0
  1. local robot = require("robot")
  2. local component = require("component")
  3. local sides = require("sides")
  4. local computer = require("computer")
  5. local inventory = component.inventory_controller
  6.  
  7. local items = {
  8.     AXE_NAME = "EMT:ChainsawStream",
  9.     HOE_NAME = "EMT:ElectricHoeGrowth",
  10.     SAPLING_NAME = "sapling"
  11. }
  12.  
  13. local patternErorrs = {
  14.     AXE_NAME = "Электропила потока не найдена!",
  15.     HOE_NAME = "Электромотыга роста не найдена!",
  16.     SAPLING_NAME = "Саженец дерева не найден!"
  17. }
  18.  
  19. function robotError(msg)
  20.     print("Ошибка: ", msg)
  21.     computer.beep(1000,0.3)
  22.     computer.beep(1000,0.3)
  23.     computer.beep(1000,0.3)
  24.     os.exit()
  25. end
  26.  
  27. function equipItem(nameItem)
  28.     if nameItem ~= nil then
  29.         for i = 1, robot.inventorySize() do
  30.             local item = inventory.getStackInInternalSlot(i)
  31.             if item and item.name and item.name == nameItem then
  32.                 robot.select(i)
  33.                 inventory.equip()
  34.             end
  35.         end
  36.     else
  37.         robot.select(1)
  38.     end
  39. end
  40.  
  41. function chargeItems()
  42.     for i = 1, robot.inventorySize() do
  43.         local item = inventory.getStackInInternalSlot(i)
  44.         if item then
  45.             if item.charge and item.charge <= 10000 then
  46.                 robot.turnAround()
  47.                 robot.select(i)
  48.  
  49.                 if(inventory.dropIntoSlot(sides.front, 1)) then
  50.                     print("charging " .. item.name .. " started")
  51.                     os.sleep(5)
  52.                     if(inventory.suckFromSlot(sides.front, 1)) then
  53.                         print("charging " .. item.name .. " finished")
  54.                         robot.turnAround()
  55.                     end
  56.                 end
  57.             end
  58.         end
  59.     end
  60. end
  61.  
  62. function safeMovement(func)
  63.     local result, reason = func()
  64.     while not result do
  65.         print(result, result)
  66.         result, reason = func()
  67.     end
  68. end
  69.  
  70. function find(itemName)
  71.     local state = false
  72.     for i = 1, robot.inventorySize() do
  73.         local item = inventory.getStackInInternalSlot(i)
  74.         if item and item.name and item.name:find(itemName) then
  75.             state = item.name
  76.         end
  77.     end
  78.     return state
  79. end
  80.  
  81. function findNeedItems()
  82.     for name, value in pairs(items) do
  83.         local candidate = find(value)
  84.         if candidate then
  85.             if value == "sapling" then
  86.                 items["SAPLING_NAME"] = candidate
  87.             end
  88.         else
  89.             robotError(patternErorrs[name])
  90.         end
  91.     end
  92. end
  93.  
  94.  
  95. function plantSapling()
  96.     equipItem(items["SAPLING_NAME"])
  97.  
  98.     robot.swing(sides.front)
  99.  
  100.     safeMovement(robot.up)
  101.     safeMovement(robot.forward)
  102.     safeMovement(robot.forward)
  103.     robot.useDown()
  104.     print("plant sapling")
  105.     safeMovement(robot.back)
  106.     safeMovement(robot.down)
  107.  
  108.     growSapling()
  109. end
  110.  
  111. function growSapling()
  112.     equipItem(items["HOE_NAME"])
  113.     print("grow sapling")
  114.  
  115.     local state = false
  116.  
  117.     while state == false do
  118.         local bool, block = robot.detect()
  119.         if bool and block == "solid" then
  120.             state = true
  121.         end
  122.         robot.use(sides.front)
  123.     end
  124.     os.sleep(0.2)
  125.  
  126.     chopSapling()
  127. end
  128.  
  129. function chopSapling()
  130.     equipItem(items["AXE_NAME"])
  131.     print("chop tree")
  132.  
  133.     local state = false
  134.  
  135.     while state == false do
  136.         if robot.detect() == false then
  137.             state = true
  138.         end
  139.         robot.swing(sides.front)
  140.     end
  141.     os.sleep(0.2)
  142.     safeMovement(robot.back)
  143.     suckItems()
  144. end
  145.  
  146. function suckItems()
  147.     os.sleep(1)
  148.     print("pick up items")
  149.     for i = 1, 10 do
  150.         robot.suck()
  151.         robot.suckUp()
  152.         robot.suckDown()
  153.     end
  154.  
  155.     dropItems()
  156. end
  157.  
  158. function dropItems()
  159.     os.sleep(1)
  160.  
  161.     local stateSapling = false
  162.  
  163.     robot.turnLeft()
  164.     print("drop items")
  165.  
  166.     local rSize = robot.inventorySize()
  167.     for i = 1, rSize do
  168.         local item = inventory.getStackInInternalSlot(i)
  169.         if item then
  170.             if item.name and item.name == items["AXE_NAME"] then
  171.                 goto continue
  172.             elseif item.name and item.name == items["HOE_NAME"] then
  173.                 goto continue
  174.             elseif item.label and item.name == items["SAPLING_NAME"] and stateSapling == false then
  175.                 stateSapling = true
  176.                 goto continue
  177.             else
  178.                 local index = i + 1 and i + 1 or i
  179.                 for j = i, index  do
  180.                     robot.select(j)
  181.                     inventory.dropIntoSlot(3, j)
  182.                     os.sleep(0.3)
  183.                 end
  184.             end
  185.         end
  186.  
  187.         :: continue ::
  188.     end
  189.     robot.turnRight()
  190. end
  191.  
  192. function start()
  193.     findNeedItems()
  194.     while true do
  195.         chargeItems()
  196.         plantSapling()
  197.     end
  198. end
  199.  
  200. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement