Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[example
- local computer = require('computer')
- local robot = require('robot')
- local farmer = require('farmer')
- local f_type = 'cactus'
- local timer = 900
- local function check_energy()
- energy = computer.energy()/computer.maxEnergy()
- if energy <= 0.1 then
- print('Уровень энергии менее 10%')
- os.sleep(timer)
- end
- end
- local function dig(var)
- if var == 'cactus' then
- if robot.detectDown() then
- robot.swingDown()os.sleep(0.4)
- robot.down() os.sleep(0.4)
- robot.swingDown() os.sleep(0.4)
- robot.up()
- end
- elseif var == 'crops' then
- robot.useDown()
- elseif var == 'tree' then
- local srd = 1
- while robot.detectUp() do
- robot.swingUp()
- robot.up()
- robot.swing()
- srd = srd + 1
- end
- for d = 1, srd do
- robot.down()
- end
- end
- end
- while true do
- check_energy()
- dig(f_type)
- farmer.killer()
- robot.forward()
- if robot.detect() then
- dig(f_type)
- farmer.killer()
- if farmer.turn('left') then
- if farmer.findChest() then
- for i = 2, robot.inventorySize() do
- robot.select(i)
- robot.drop()
- end
- robot.turnAround()
- robot.select(1)
- os.sleep(timer)
- end
- end
- end
- end
- ]]
- local farmer = {}
- local robot = require 'robot'
- local side, tmps = 'left', 'left'
- local chest = 1
- function farmer.killer() -- убийца сущностей
- if robot.detect() then -- что-то нашел
- local _, t = robot.detect()
- if t == 'entity' then -- если перед роботом сущность
- while robot.detect() do
- robot.swing()
- end
- elseif t == 'replaceable' or t == 'passable' then
- robot.swing()
- end
- end
- end
- function farmer.findItem(name, damage) -- поиск предмета в контейнере перед носом
- robot.select(1)
- for sl = 1, component.inventory_controller.getInventorySize(3) do
- if component.inventory_controller.getStackInSlot(3, sl) == nil then
- else
- item = component.inventory_controller.getStackInSlot(3, sl)
- if item.name == name and item.damage == damage then
- component.inventory_controller.suckFromSlot(3, sl)
- end
- end
- end
- end
- function farmer.findChest()--поиск сундука
- side = tmps
- while true do
- if robot.detect() then --что-то есть!
- farmer.killer()
- robot.select(chest) --выбираем сундук
- for a=1, 3 do
- if robot.compare() then --это сундук?
- return true -- да
- else
- robot.turnRight() --нет, поворачиваем направо
- end
- end
- else --ничего нет
- robot.forward() --едем дальше
- end
- end
- end
- function farmer.turn() --поворотник
- if side == 'left' then --если раньше поворачивал направо - стоит left
- robot.turnLeft() --поворачиваем
- if robot.detect() then --проверяем, есть ли блок
- return true --если есть, то сообщаем про угол
- else --иначе
- robot.forward() --делаем шаг
- robot.turnLeft() --и разворачиваем на новую линию
- end
- side = 'right'
- -----------------------------------------------------------
- elseif side == 'right' then
- robot.turnRight()
- if robot.detect() then
- return true
- else
- robot.forward()
- robot.turnRight()
- end
- side = 'left'
- end
- end
- return farmer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement