Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local robot = require("robot")
- local component = require("component")
- local sides = require("sides")
- local computer = require("computer")
- local inventory = component.inventory_controller
- local items = {
- AXE_NAME = "EMT:ChainsawStream",
- HOE_NAME = "EMT:ElectricHoeGrowth",
- SAPLING_NAME = "sapling"
- }
- local patternErorrs = {
- AXE_NAME = "Электропила потока не найдена!",
- HOE_NAME = "Электромотыга роста не найдена!",
- SAPLING_NAME = "Саженец дерева не найден!"
- }
- function robotError(msg)
- print("Ошибка: ", msg)
- computer.beep(1000,0.3)
- computer.beep(1000,0.3)
- computer.beep(1000,0.3)
- os.exit()
- end
- function equipItem(nameItem)
- if nameItem ~= nil then
- for i = 1, robot.inventorySize() do
- local item = inventory.getStackInInternalSlot(i)
- if item and item.name and item.name == nameItem then
- robot.select(i)
- inventory.equip()
- end
- end
- else
- robot.select(1)
- end
- end
- function chargeItems()
- for i = 1, robot.inventorySize() do
- local item = inventory.getStackInInternalSlot(i)
- if item then
- if item.charge and item.charge <= 10000 then
- robot.turnAround()
- robot.select(i)
- if(inventory.dropIntoSlot(sides.front, 1)) then
- print("charging " .. item.name .. " started")
- os.sleep(5)
- if(inventory.suckFromSlot(sides.front, 1)) then
- print("charging " .. item.name .. " finished")
- robot.turnAround()
- end
- end
- end
- end
- end
- end
- function safeMovement(func)
- local result, reason = func()
- while not result do
- print(result, result)
- result, reason = func()
- end
- end
- function find(itemName)
- local state = false
- for i = 1, robot.inventorySize() do
- local item = inventory.getStackInInternalSlot(i)
- if item and item.name and item.name:find(itemName) then
- state = item.name
- end
- end
- return state
- end
- function findNeedItems()
- for name, value in pairs(items) do
- local candidate = find(value)
- if candidate then
- if value == "sapling" then
- items["SAPLING_NAME"] = candidate
- end
- else
- robotError(patternErorrs[name])
- end
- end
- end
- function plantSapling()
- equipItem(items["SAPLING_NAME"])
- robot.swing(sides.front)
- safeMovement(robot.up)
- safeMovement(robot.forward)
- safeMovement(robot.forward)
- robot.useDown()
- print("plant sapling")
- safeMovement(robot.back)
- safeMovement(robot.down)
- growSapling()
- end
- function growSapling()
- equipItem(items["HOE_NAME"])
- print("grow sapling")
- local state = false
- while state == false do
- local bool, block = robot.detect()
- if bool and block == "solid" then
- state = true
- end
- robot.use(sides.front)
- end
- os.sleep(0.2)
- chopSapling()
- end
- function chopSapling()
- equipItem(items["AXE_NAME"])
- print("chop tree")
- local state = false
- while state == false do
- if robot.detect() == false then
- state = true
- end
- robot.swing(sides.front)
- end
- os.sleep(0.2)
- safeMovement(robot.back)
- suckItems()
- end
- function suckItems()
- os.sleep(1)
- print("pick up items")
- for i = 1, 10 do
- robot.suck()
- robot.suckUp()
- robot.suckDown()
- end
- dropItems()
- end
- function dropItems()
- os.sleep(1)
- local stateSapling = false
- robot.turnLeft()
- print("drop items")
- local rSize = robot.inventorySize()
- for i = 1, rSize do
- local item = inventory.getStackInInternalSlot(i)
- if item then
- if item.name and item.name == items["AXE_NAME"] then
- goto continue
- elseif item.name and item.name == items["HOE_NAME"] then
- goto continue
- elseif item.label and item.name == items["SAPLING_NAME"] and stateSapling == false then
- stateSapling = true
- goto continue
- else
- local index = i + 1 and i + 1 or i
- for j = i, index do
- robot.select(j)
- inventory.dropIntoSlot(3, j)
- os.sleep(0.3)
- end
- end
- end
- :: continue ::
- end
- robot.turnRight()
- end
- function start()
- findNeedItems()
- while true do
- chargeItems()
- plantSapling()
- end
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement