Hikooshi

rFarmer

Jan 17th, 2021 (edited)
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.02 KB | None | 0 0
  1. local component = require("component")
  2. local robot = require("robot")
  3. local invCon
  4. local redstone
  5. local modem
  6. local checkComp = true
  7. if not component.isAvailable("inventory_controller") then
  8. print("Не установлен компонент 'Контролер инвентаря'")
  9. checkComp = false
  10. else
  11. invCon = component.inventory_controller
  12. end
  13. if not component.isAvailable("redstone") then
  14. print("Не установлена редстоун-карта")
  15. checkComp = false
  16. else
  17. redstone = component.redstone
  18. end
  19. if robot.select(1) == false then
  20. print("Необходимо установить улучшение 'Инвентарь'")
  21. checkComp = false
  22. end
  23. --if not component.isAvailable("experience") then
  24. --print("Не установлен компонент 'Опыт'")
  25. --checkComp = false
  26. --end
  27. if not checkComp then os.exit() end
  28.   if component.isAvailable("modem") then modem = component.modem end
  29. local sides = require("sides")
  30. local event = require("event")
  31. local computer = require("computer")
  32. local serialization = require("serialization")
  33.  
  34. local wheatSide = 0
  35. local dropSide = 0
  36. local farmCount = 14 --Количество коров
  37. local feed = true --Кормить или не кормить телят
  38. local rInv = robot.inventorySize()
  39. local time = os.time()
  40. local waitTime = 10 --Ожидание до первого фарма, секунд
  41. local osWaitTime = 320 --Ожидание последующих циклов фарма, секунд
  42. local port = 384 --Порт, для передачи данных
  43. local recievingAddress = ""
  44. local feedTime = 32 --Количество пшеницы на одного теленка
  45. local redSide = "right" --Сторона робота для контроля сигнала редстоуна
  46.  
  47. local file = io.open("/home/connectionAddress", "r")
  48. if file then
  49. recievingAddress = file:read()
  50. file:close()
  51. end
  52.  
  53. if modem then modem.open(port) end
  54.  
  55. function init()
  56.  
  57. for i = 1, rInv do
  58. robot.select(i)
  59.   if robot.count() > 0 then
  60.   invCon.equip()
  61. --  robot.select(1)
  62.   break
  63.   end
  64. end
  65. robot.select(1)
  66. for i = 1, 4 do
  67. local slotsCount = 0
  68. slotsCount = invCon.getInventorySize(3)
  69.   if slotsCount ~= nil then
  70.   local p = {}
  71.   local e = 0
  72.     for l = 1, slotsCount do
  73.     p = invCon.getStackInSlot(3, l)
  74.       if p ~= nil then
  75.         if p.name == "minecraft:wheat" then
  76.         wheatSide = i
  77.         break
  78.         else
  79.         e = e + 1
  80.         end
  81.       else
  82.       e = e+1
  83.       end
  84.     end
  85.         if e == slotsCount then dropSide = i end
  86.   end
  87. robot.turnRight()
  88. end
  89. --print(wheatSide, dropSide)
  90.  
  91. end
  92.  
  93. function cFarm()
  94.  
  95. if modem ~= nil and recievingAddress ~= "" then
  96. modem.send(recievingAddress, port, "Работаю")
  97. end
  98. local pCount = farmCount
  99. for i = 1, wheatSide-1 do
  100. robot.turnRight()
  101. end
  102. --for i = 1, invCon.getInventorySize(3) do
  103. --pCount = pCount - invCon.suckFromSlot(3, i, pCount)
  104. --  if pCount == 0 then break end
  105. --end
  106. while pCount > 0 do
  107. pCount = pCount - robot.suck(pCount)
  108. end
  109.         if feed then
  110.         local feedCount = feedTime * farmCount / 2
  111.           while feedCount > 0 do
  112.           feedCount = feedCount - robot.suck(feedCount)
  113.           end
  114.         end
  115. for i = 1, wheatSide-1 do
  116. robot.turnLeft()
  117. end
  118. invCon.equip()
  119. robot.forward()
  120. for i = 1, farmCount do
  121. robot.forward()
  122. robot.useDown()
  123. robot.forward()
  124. end
  125. robot.turnAround()
  126. os.sleep(8)
  127. --  for i = 1, robot.inventorySize() do
  128. --    if invCon.getStackInInternalSlot(i).name:match("sword") then
  129. --    robot.select(i)
  130. --    invCon.equip()
  131. --    break
  132. --    end
  133. --  end
  134. robot.select(1)
  135. invCon.equip()
  136. if feed then
  137. local trfSlot = 1
  138.   for i = 1, rInv do
  139.   robot.select(i)
  140.     if robot.count() > 0 then break end
  141.   end
  142.       if robot.count() % 32 ~= 0 then
  143.         trfSlot = robot.select()
  144.           for i = trfSlot+1, rInv do
  145.           robot.compareTo(i)
  146.             if robot.count(i) < 64 and robot.count(i) % 32 ~= 0 then
  147.             robot.transferTo(i)
  148.             break
  149.             end
  150.           end
  151.       end
  152. end
  153. for i = 1, farmCount do
  154. robot.forward()
  155. local cSwing = false
  156. local cFeed = false
  157.   if redstone.getInput(sides[redSide]) > 0 then
  158.   cSwing = true
  159.     if feed then cFeed = true end
  160.   end
  161.     while cSwing do
  162.     cSwing = robot.swingDown()
  163.     os.sleep(0.2)
  164.     end
  165.       if cFeed then
  166.       robot.down()
  167.       local slotContent
  168.         for i = 1, rInv do
  169.         slotContent = invCon.getStackInInternalSlot(i)
  170.           if slotContent and slotContent.name:match("wheat") then
  171.           robot.select(i)
  172.           invCon.equip()
  173.           break
  174.           end
  175.         end
  176.             local fOp = feedTime
  177.             while fOp > 0 do
  178.               if robot.useDown() then
  179.               fOp = fOp - 1
  180.               end
  181.             end
  182.             invCon.equip()
  183.             robot.up()
  184.             cFeed = false
  185.       end
  186. robot.forward()
  187. end
  188. robot.forward()
  189. robot.turnAround()
  190. for i = 1, dropSide-1 do
  191. robot.turnRight()
  192. end
  193. local totalResourses = {label = "count"}
  194. for i = 1, rInv do
  195. robot.select(i)
  196. local resourse = ""
  197. local result = true
  198. resourse = invCon.getStackInInternalSlot(i)
  199.   if resourse then
  200.     for k,v in pairs(totalResourses) do
  201.       if k == nil or resourse.label ~= k then
  202.       result = false
  203.       else
  204.       result = true
  205.       break
  206.       end
  207.     end
  208.         if not result then
  209.         totalResourses[resourse.label] = resourse.size
  210.         else
  211.         totalResourses[resourse.label] = totalResourses[resourse.label] + resourse.size
  212.         end
  213.   end
  214. robot.drop()
  215. end
  216. robot.select(1)
  217. for i = 1, dropSide-1 do
  218. robot.turnLeft()
  219. end
  220. totalResourses.label = nil
  221. --for k,v in pairs(totalResourses) do print(k,v) end
  222. waitTime = osWaitTime
  223. time = os.time()
  224. if modem ~= nil and recievingAddress ~= "" then
  225. modem.send(recievingAddress, port, "Ожидаю", serialization.serialize(totalResourses))
  226. end
  227.  
  228. end
  229.  
  230. function getTableLength(tbl)
  231.  
  232. local length = 0
  233. for k,v in pairs(tbl) do
  234.   if v then length = length + 1 end
  235. end
  236. return length
  237.  
  238. end
  239.  
  240. init()
  241. while true do
  242. local event,robotModemAddress,modemAddress,_,_,message,message1,message2 = event.pull(10, "modem_message")
  243. local newTime = os.time()
  244.   if event then
  245.   local result = ""
  246.     if message == "connect".." "..robotModemAddress:sub(1, 3) then
  247.       if recievingAddress ~= modemAddress then
  248.       local file = io.open("/home/connectionAddress", "w")
  249.       file:write(modemAddress)
  250.       recievingAddress = modemAddress
  251.       file:close()
  252.       result = "Подключение успешно"
  253.       else
  254.       result = "Робот уже был подключен"
  255.       end
  256.     elseif message == "cng"  and modemAddress == recievingAddress then
  257.       if message1 == "feed" then
  258.         if message2 == "true" then
  259.         feed = true
  260.         result = "Параметр feed изменен на true"
  261.         elseif message2 == "false" then
  262.         feed = false
  263.         result = "Параметр feed изменен на false"
  264.         else
  265.         result = "Параметр задан не верно. Введите true или false"
  266.         end
  267.       elseif message1 == "time" then
  268.         local newOsWaitTime = tonumber(message2)
  269.           if newOsWaitTime then
  270.           osWaitTime = newOsWaitTime
  271.           result = "Время ожидания изменено на "..message2.." секунд"
  272.           else
  273.           result = "Не правильно указано время"
  274.           end
  275.       elseif message1 == "fdtm" then
  276.         local newFeedTime = tonumber(message2)
  277.           if newFeedTime then
  278.           feedTime = newFeedTime
  279.           result = "Количество пшеницы для кормежки изменено на "..message2.." штук"
  280.           else
  281.           result = "Неверно задано количество пшеницы"
  282.           end
  283.       elseif message1 == "fcnt" then
  284.         local newFarmCount = tonumber(message2)
  285.           if newFarmCount then
  286.           farmCount = newFarmCount
  287.           result = "Количество особей изменено на " .. message2
  288.           else
  289.           result = "Неверно задано количество особей"
  290.           end
  291.       end
  292.     elseif message == "req" and modemAddress == recievingAddress then
  293.       if message1 == "nrg" then
  294.       result = tostring(math.modf(computer.energy())).."/"..tostring(computer.maxEnergy())
  295.       elseif message1 == "dura" then
  296.         local durability = robot.durability() * 100
  297.           if not durability then
  298.           result = "Инструмент сломан"
  299.           else
  300.           result = "Осталось "..math.modf(durability).."% прочности инструмента"
  301.           end
  302.       elseif message1 == "wht" then
  303.         for i = 1, wheatSide-1 do
  304.         robot.turnRight()
  305.         end
  306.         local wheatCount = 0
  307.           for i = 1, invCon.getInventorySize(3) do
  308.             local slotData = invCon.getStackInSlot(3, i)
  309.               if slotData and slotData.name:match("wheat") then
  310.               wheatCount = wheatCount + slotData.size
  311.               end
  312.           end
  313.                 for i = 1, wheatSide-1 do
  314.                 robot.turnLeft()
  315.                 end
  316.         result = "Осталось "..tostring(wheatCount).." пшеницы"
  317.       elseif message1 == "slot" then
  318.         for i = 1, dropSide-1 do
  319.         robot.turnRight()
  320.         end
  321.           if message2 == "all" then
  322.           local slts = {}
  323.             for i = 1, invCon.getInventorySize(3) do
  324.             local slotData = invCon.getStackInSlot(3, i)
  325.               if slotData then
  326.                 if slts[slotData.label] then
  327.                 slts[slotData.label] = slts[slotData.label] + slotData.size
  328.                 else
  329.                 slts[slotData.label] = slotData.size
  330.                 end
  331.               end
  332.             end
  333.                 if getTableLength(slts) > 0 then
  334.                 result = "В слотах находятся: "
  335.                 else
  336.                 result = "Слоты пусты"
  337.                 end
  338.               for name, count in pairs(slts) do
  339.               result = result .. name .. " " .. tostring(count) .. " штук "
  340.               end
  341.           else
  342.             local slt = tonumber(message2)
  343.             if slt and slt <= invCon.getInventorySize(3) then
  344.             local slotData = invCon.getStackInSlot(3, slt)
  345.               if slotData then
  346.               result = "В слоте "..message2.." "..slotData.label.." в количестве "..slotData.size.." штук"
  347.               else
  348.               result = "Слот пуст"
  349.               end
  350.             else
  351.               if not slt then
  352.               result = "Номер слота задан неверно"
  353.               else
  354.               result = "Значение превышает размер инвентаря"
  355.               end
  356.             end
  357.           end
  358.                 for i = 1, dropSide-1 do
  359.                 robot.turnLeft()
  360.                 end
  361.       elseif message1 == "iSlot" then
  362.         if message2 == "all" then
  363.         local iSlts = {}
  364.           for i = 1, rInv do
  365.           local slotData = invCon.getStackInInternalSlot(i)
  366.             if slotData then
  367.               if iSlts[slotData.label] then
  368.               iSlts[slotData.label] = iSlts[slotData.label] + slotData.size
  369.               else
  370.               iSlts[slotData.label] = slotData.size
  371.               end
  372.             end
  373.           end
  374.                 if getTableLength(iSlts) > 0 then
  375.                 result = "Во внутренних слотах находятся: "
  376.                 else
  377.                 result = "Внутренние слоты пусты"
  378.                 end
  379.             for name, count in pairs(iSlts) do
  380.             result = result .. name .. " в количестве " .. tostring(count) .. " штук "
  381.             end
  382.         else
  383.           local iSlt = tonumber(message2)
  384.           if iSlt and iSlt <= rInv then
  385.           local slotData = invCon.getStackInInternalSlot(iSlt)
  386.             if slotData then
  387.             result = "Во внутреннем слоте " .. message2 .. " " .. slotData.label.. " в количестве " .. tostring(slotData.size) .. " штук"
  388.             else
  389.             result = "Слот пуст"
  390.             end
  391.           else
  392.             if not iSlt then
  393.             result = "Неверно задан номер слота"
  394.             else
  395.             result = "Значение превышает размер инвентаря робота"
  396.             end
  397.           end
  398.         end
  399.       elseif message1 == "time" then
  400.       result = tostring(waitTime)
  401.       elseif message1 == "fdtm" then
  402.       result = tostring(feedTime)
  403.       elseif message1 == "fcnt" then
  404.       result = tostring(farmCount)
  405.       elseif message1 == "feed" then
  406.       result = feed and "true" or "false"
  407.       else
  408.       result = "Неверный запрос"
  409.       end
  410.     end
  411.   modem.send(recievingAddress, port, result)
  412.   end
  413.     if newTime - time > waitTime * 100 then
  414.     cFarm()
  415.     end
  416. end
Add Comment
Please, Sign In to add comment