Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local r = require('robot')
- local comp = require('computer')
- local term = require('term')
- local inv = require('component').inventory_controller
- local tractorSuck = require('component').tractor_beam.suck
- local rast_x = 1
- local rast_z = 1
- local num_of_lines_x = 2
- local num_of_blocks_z = 4
- local sleep = 480
- --replace_instrument = false
- local num_all = num_of_lines_x * num_of_blocks_z - 1
- local numRub
- sleep = sleep / 10
- -- =============================== Н А В И Г А Ц И Я ============================== --
- local x, z, b, dx ,dz = 0, 0, 0, 0, 1
- local function forward(num)
- if not num then num = 1 end
- for i = 1, num do
- while not r.forward() do r.swing() end
- end
- x = x + dx * num
- z = z + dz * num
- end
- local function turnLeft()
- b = (b + 3)%4
- r.turnLeft()
- if b == 0 then dz = 1; dx = 0
- elseif b == 1 then dx = 1; dz = 0
- elseif b == 2 then dz = -1; dx = 0
- else dx = -1; dz = 0 end
- end
- local function turnRight()
- b = (b + 1)%4
- r.turnRight()
- if b == 0 then dz = 1; dx = 0
- elseif b == 1 then dx = 1; dz = 0
- elseif b == 2 then dz = -1; dx = 0
- else dx = -1; dz = 0 end
- end
- local function turnAround()
- b = (b + 2)%4
- r.turnAround()
- dx = dx * (-1)
- dz = dz * (-1)
- end
- local function turn(side)
- if side == 0 and b == 3 then
- r.turnRight()
- b = side
- elseif side == 3 and b == 0 then
- r.turnLeft()
- b = side
- else
- while (b < side) do
- r.turnRight()
- b = b + 1
- end
- while (b > side) do
- r.turnLeft()
- b = b - 1
- end
- end
- if b == 0 then dz = 1; dx = 0
- elseif b == 1 then dx = 1; dz = 0
- elseif b == 2 then dz = -1; dx = 0
- else dx = -1; dz = 0 end
- end
- -- ============================== Ф У Н К Ц И И ============================= --
- -- Замена инструмента
- --[==[
- function zamena()
- slotInstr = 2
- while (inv.getStackInInternalSlot(slotInstr) ~= nil) and (slotInstr < allSlots) do
- slotInstr = slotInstr + 1
- end
- r.select(slotInstr)
- inv.equip()
- turnAround()
- r.drop()
- while inv.getStackInSlot(3,1).charge < inv.getStackInSlot(3,1).maxCharge do
- os.sleep(0.2)
- end
- r.suck()
- inv.equip()
- turnAround()
- slotInstr = nil
- end
- --]==]
- -- Возврат домой
- local function home(noreturn)
- local x1 = x
- local z1 = z
- local b1 = b
- local item
- local resources = {}
- resources['minecraft:log'] = true
- resources['minecraft:coal'] = true
- if x1 > 0 then
- turn(3)
- forward(x1)
- end
- turn(2)
- forward(z1)
- turnLeft()
- r.select(16)
- r.suckDown()
- for slot = 1 , 15 do
- item = inv.getStackInInternalSlot(slot)
- if item then
- if resources[item.name] then
- r.select(slot)
- while r.count(slot) > 0 do
- if not r.drop() then
- os.sleep(4)
- term.clear()
- print('освободите инвентарь для остальных вещей')
- end
- end
- elseif item.name == "minecraft:sapling" then
- r.select(slot)
- r.dropDown()
- end
- end
- end
- turnAround()
- for slot = 1 , 15 do
- if r.count(slot) > 0 then
- r.select(slot)
- while r.count(slot) > 0 do
- if not r.drop() then
- os.sleep(4)
- term.clear()
- print('освободите инвентарь для остальных вещей')
- end
- end
- end
- end
- turnRight()
- --[[
- --Починка или подзарядка инструмента
- if r.durability() < 0.3 then
- if not replace_instrument then
- while r.durability() < 0.3 do
- os.sleep(8)
- end
- else
- zamena()
- end
- end
- --]]
- -- зарядка
- while comp.energy() < 100000 do
- os.sleep(8)
- end
- -- возвращаемся если нет флага "в один конец"
- if not noreturn then
- forward(z1)
- if x1 > 0 then
- turn(1)
- forward(x1)
- end
- turn(b1)
- end
- end
- --Рубка дерева
- local function axe()
- r.select(16)
- if not r.compare(true) then
- r.swing()
- r.place()
- return 1
- end
- return 0
- end
- -- ======================== Г Л А В Н Ы Й Ц И К Л ======================== --
- -- робот движется по полю по кругу, и рубит выросшие деревья
- while true do
- -- робот едет на стартовую позицию (левый передний угол поля)
- forward(4)
- -- проход по ферме
- for i = 1, num_all + 1 do
- if i%4 == 0 and (r.count(15) > 32 or r.count(16) < 8 or comp.energy() < 50000 or r.durability() < 0.3) then
- home(false)
- end
- forward()
- -- проверка дерева слева и справа
- turnLeft()
- numRub = axe()
- turnAround()
- numRub = numRub + axe()
- turnLeft()
- if numRub > 0.5 then
- os.sleep(0.5)
- while tractorSuck() do end
- end
- if i%num_of_blocks_z ~= 0 or i >= num_all then
- forward(rast_z)
- else
- forward()
- if z ~= 4 then
- turnRight()
- forward(rast_x + 3)
- turnRight()
- else
- turnLeft()
- forward(rast_x + 3)
- turnLeft()
- end
- end
- --
- end
- -- робот возвращается домой
- -- (флаг true означает - в один конец)
- home(true)
- -- робот спит и видит сны =)
- term.clear()
- print('Progress of sleeping')
- for i=1,10 do
- os.sleep(sleep)
- print(i*10,' %')
- end
- term.clear()
- print('moving')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement