Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local robot = require("robot")
- local invCon
- local redstone
- local modem
- local checkComp = true
- if not component.isAvailable("inventory_controller") then
- print("Не установлен компонент 'Контролер инвентаря'")
- checkComp = false
- else
- invCon = component.inventory_controller
- end
- if not component.isAvailable("redstone") then
- print("Не установлена редстоун-карта")
- checkComp = false
- else
- redstone = component.redstone
- end
- if robot.select(1) == false then
- print("Необходимо установить улучшение 'Инвентарь'")
- checkComp = false
- end
- --if not component.isAvailable("experience") then
- --print("Не установлен компонент 'Опыт'")
- --checkComp = false
- --end
- if not checkComp then os.exit() end
- if component.isAvailable("modem") then modem = component.modem end
- local sides = require("sides")
- local event = require("event")
- local computer = require("computer")
- local serialization = require("serialization")
- local wheatSide = 0
- local dropSide = 0
- local farmCount = 14 --Количество коров
- local feed = true --Кормить или не кормить телят
- local rInv = robot.inventorySize()
- local time = os.time()
- local waitTime = 10 --Ожидание до первого фарма, секунд
- local osWaitTime = 320 --Ожидание последующих циклов фарма, секунд
- local port = 384 --Порт, для передачи данных
- local recievingAddress = ""
- local feedTime = 32 --Количество пшеницы на одного теленка
- local redSide = "right" --Сторона робота для контроля сигнала редстоуна
- local file = io.open("/home/connectionAddress", "r")
- if file then
- recievingAddress = file:read()
- file:close()
- end
- if modem then modem.open(port) end
- function init()
- for i = 1, rInv do
- robot.select(i)
- if robot.count() > 0 then
- invCon.equip()
- -- robot.select(1)
- break
- end
- end
- robot.select(1)
- for i = 1, 4 do
- local slotsCount = 0
- slotsCount = invCon.getInventorySize(3)
- if slotsCount ~= nil then
- local p = {}
- local e = 0
- for l = 1, slotsCount do
- p = invCon.getStackInSlot(3, l)
- if p ~= nil then
- if p.name == "minecraft:wheat" then
- wheatSide = i
- break
- else
- e = e + 1
- end
- else
- e = e+1
- end
- end
- if e == slotsCount then dropSide = i end
- end
- robot.turnRight()
- end
- --print(wheatSide, dropSide)
- end
- function cFarm()
- if modem ~= nil and recievingAddress ~= "" then
- modem.send(recievingAddress, port, "Работаю")
- end
- local pCount = farmCount
- for i = 1, wheatSide-1 do
- robot.turnRight()
- end
- --for i = 1, invCon.getInventorySize(3) do
- --pCount = pCount - invCon.suckFromSlot(3, i, pCount)
- -- if pCount == 0 then break end
- --end
- while pCount > 0 do
- pCount = pCount - robot.suck(pCount)
- end
- if feed then
- local feedCount = feedTime * farmCount / 2
- while feedCount > 0 do
- feedCount = feedCount - robot.suck(feedCount)
- end
- end
- for i = 1, wheatSide-1 do
- robot.turnLeft()
- end
- invCon.equip()
- robot.forward()
- for i = 1, farmCount do
- robot.forward()
- robot.useDown()
- robot.forward()
- end
- robot.turnAround()
- os.sleep(8)
- -- for i = 1, robot.inventorySize() do
- -- if invCon.getStackInInternalSlot(i).name:match("sword") then
- -- robot.select(i)
- -- invCon.equip()
- -- break
- -- end
- -- end
- robot.select(1)
- invCon.equip()
- if feed then
- local trfSlot = 1
- for i = 1, rInv do
- robot.select(i)
- if robot.count() > 0 then break end
- end
- if robot.count() % 32 ~= 0 then
- trfSlot = robot.select()
- for i = trfSlot+1, rInv do
- robot.compareTo(i)
- if robot.count(i) < 64 and robot.count(i) % 32 ~= 0 then
- robot.transferTo(i)
- break
- end
- end
- end
- end
- for i = 1, farmCount do
- robot.forward()
- local cSwing = false
- local cFeed = false
- if redstone.getInput(sides[redSide]) > 0 then
- cSwing = true
- if feed then cFeed = true end
- end
- while cSwing do
- cSwing = robot.swingDown()
- os.sleep(0.2)
- end
- if cFeed then
- robot.down()
- local slotContent
- for i = 1, rInv do
- slotContent = invCon.getStackInInternalSlot(i)
- if slotContent and slotContent.name:match("wheat") then
- robot.select(i)
- invCon.equip()
- break
- end
- end
- local fOp = feedTime
- while fOp > 0 do
- if robot.useDown() then
- fOp = fOp - 1
- end
- end
- invCon.equip()
- robot.up()
- cFeed = false
- end
- robot.forward()
- end
- robot.forward()
- robot.turnAround()
- for i = 1, dropSide-1 do
- robot.turnRight()
- end
- local totalResourses = {label = "count"}
- for i = 1, rInv do
- robot.select(i)
- local resourse = ""
- local result = true
- resourse = invCon.getStackInInternalSlot(i)
- if resourse then
- for k,v in pairs(totalResourses) do
- if k == nil or resourse.label ~= k then
- result = false
- else
- result = true
- break
- end
- end
- if not result then
- totalResourses[resourse.label] = resourse.size
- else
- totalResourses[resourse.label] = totalResourses[resourse.label] + resourse.size
- end
- end
- robot.drop()
- end
- robot.select(1)
- for i = 1, dropSide-1 do
- robot.turnLeft()
- end
- totalResourses.label = nil
- --for k,v in pairs(totalResourses) do print(k,v) end
- waitTime = osWaitTime
- time = os.time()
- if modem ~= nil and recievingAddress ~= "" then
- modem.send(recievingAddress, port, "Ожидаю", serialization.serialize(totalResourses))
- end
- end
- function getTableLength(tbl)
- local length = 0
- for k,v in pairs(tbl) do
- if v then length = length + 1 end
- end
- return length
- end
- init()
- while true do
- local event,robotModemAddress,modemAddress,_,_,message,message1,message2 = event.pull(10, "modem_message")
- local newTime = os.time()
- if event then
- local result = ""
- if message == "connect".." "..robotModemAddress:sub(1, 3) then
- if recievingAddress ~= modemAddress then
- local file = io.open("/home/connectionAddress", "w")
- file:write(modemAddress)
- recievingAddress = modemAddress
- file:close()
- result = "Подключение успешно"
- else
- result = "Робот уже был подключен"
- end
- elseif message == "cng" and modemAddress == recievingAddress then
- if message1 == "feed" then
- if message2 == "true" then
- feed = true
- result = "Параметр feed изменен на true"
- elseif message2 == "false" then
- feed = false
- result = "Параметр feed изменен на false"
- else
- result = "Параметр задан не верно. Введите true или false"
- end
- elseif message1 == "time" then
- local newOsWaitTime = tonumber(message2)
- if newOsWaitTime then
- osWaitTime = newOsWaitTime
- result = "Время ожидания изменено на "..message2.." секунд"
- else
- result = "Не правильно указано время"
- end
- elseif message1 == "fdtm" then
- local newFeedTime = tonumber(message2)
- if newFeedTime then
- feedTime = newFeedTime
- result = "Количество пшеницы для кормежки изменено на "..message2.." штук"
- else
- result = "Неверно задано количество пшеницы"
- end
- elseif message1 == "fcnt" then
- local newFarmCount = tonumber(message2)
- if newFarmCount then
- farmCount = newFarmCount
- result = "Количество особей изменено на " .. message2
- else
- result = "Неверно задано количество особей"
- end
- end
- elseif message == "req" and modemAddress == recievingAddress then
- if message1 == "nrg" then
- result = tostring(math.modf(computer.energy())).."/"..tostring(computer.maxEnergy())
- elseif message1 == "dura" then
- local durability = robot.durability() * 100
- if not durability then
- result = "Инструмент сломан"
- else
- result = "Осталось "..math.modf(durability).."% прочности инструмента"
- end
- elseif message1 == "wht" then
- for i = 1, wheatSide-1 do
- robot.turnRight()
- end
- local wheatCount = 0
- for i = 1, invCon.getInventorySize(3) do
- local slotData = invCon.getStackInSlot(3, i)
- if slotData and slotData.name:match("wheat") then
- wheatCount = wheatCount + slotData.size
- end
- end
- for i = 1, wheatSide-1 do
- robot.turnLeft()
- end
- result = "Осталось "..tostring(wheatCount).." пшеницы"
- elseif message1 == "slot" then
- for i = 1, dropSide-1 do
- robot.turnRight()
- end
- if message2 == "all" then
- local slts = {}
- for i = 1, invCon.getInventorySize(3) do
- local slotData = invCon.getStackInSlot(3, i)
- if slotData then
- if slts[slotData.label] then
- slts[slotData.label] = slts[slotData.label] + slotData.size
- else
- slts[slotData.label] = slotData.size
- end
- end
- end
- if getTableLength(slts) > 0 then
- result = "В слотах находятся: "
- else
- result = "Слоты пусты"
- end
- for name, count in pairs(slts) do
- result = result .. name .. " " .. tostring(count) .. " штук "
- end
- else
- local slt = tonumber(message2)
- if slt and slt <= invCon.getInventorySize(3) then
- local slotData = invCon.getStackInSlot(3, slt)
- if slotData then
- result = "В слоте "..message2.." "..slotData.label.." в количестве "..slotData.size.." штук"
- else
- result = "Слот пуст"
- end
- else
- if not slt then
- result = "Номер слота задан неверно"
- else
- result = "Значение превышает размер инвентаря"
- end
- end
- end
- for i = 1, dropSide-1 do
- robot.turnLeft()
- end
- elseif message1 == "iSlot" then
- if message2 == "all" then
- local iSlts = {}
- for i = 1, rInv do
- local slotData = invCon.getStackInInternalSlot(i)
- if slotData then
- if iSlts[slotData.label] then
- iSlts[slotData.label] = iSlts[slotData.label] + slotData.size
- else
- iSlts[slotData.label] = slotData.size
- end
- end
- end
- if getTableLength(iSlts) > 0 then
- result = "Во внутренних слотах находятся: "
- else
- result = "Внутренние слоты пусты"
- end
- for name, count in pairs(iSlts) do
- result = result .. name .. " в количестве " .. tostring(count) .. " штук "
- end
- else
- local iSlt = tonumber(message2)
- if iSlt and iSlt <= rInv then
- local slotData = invCon.getStackInInternalSlot(iSlt)
- if slotData then
- result = "Во внутреннем слоте " .. message2 .. " " .. slotData.label.. " в количестве " .. tostring(slotData.size) .. " штук"
- else
- result = "Слот пуст"
- end
- else
- if not iSlt then
- result = "Неверно задан номер слота"
- else
- result = "Значение превышает размер инвентаря робота"
- end
- end
- end
- elseif message1 == "time" then
- result = tostring(waitTime)
- elseif message1 == "fdtm" then
- result = tostring(feedTime)
- elseif message1 == "fcnt" then
- result = tostring(farmCount)
- elseif message1 == "feed" then
- result = feed and "true" or "false"
- else
- result = "Неверный запрос"
- end
- end
- modem.send(recievingAddress, port, result)
- end
- if newTime - time > waitTime * 100 then
- cFarm()
- end
- end
Add Comment
Please, Sign In to add comment