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 sqrt = math.abs
- local rast_x, rast_z, num_of_lines_x, num_of_blocks_z,
- rast_x = 1
- rast_z = 1
- num_of_lines_x = 4
- num_of_blocks_z = 10
- replace_instrument = false
- local num_all = num_of_lines_x * num_of_blocks_z
- db = 0
- -- =============================== Н А В И Г А Ц И Я ============================== --
- x = 0; z = 0; y = 0; b = 0; dx = 0 ; dz = 1;
- function forward()
- while not r.forward() do r.swing() end
- x = x + dx
- z = z + dz
- end
- function back()
- if not r.back() then
- r.turnAround()
- while r.detect() or not r.forward() do r.swing() end
- r.turnAround()
- end
- x = x - dx
- z = z - dz
- end
- function up()
- while r.detectUp() or not r.up() do r.swingUp() end
- y = y + 1
- end
- function down()
- while r.detectDown() or not r.down() do r.swingDown() end
- y = y - 1
- end
- 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
- 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
- function turnAround()
- b = (b + 2)%4
- r.turnAround()
- dx = dx * (-1)
- dz = dz * (-1)
- end
- 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 goTo(x1, z1, y1, b1, hod)
- local a1, a2, b2, b3
- if x < x1 then
- a1 = 1
- elseif x > x1 then
- a1 = 3
- end
- if z < z1 then
- a2 = 0
- elseif z > z1 then
- a2 = 2
- end
- if hod then
- b2 = sqrt(z1 - z); b3 = sqrt(x1 - x)
- else
- a1, a2 = a2, a1
- b2 = sqrt(x1 - x); b3 = sqrt(z1 - z)
- end
- while y1 < y do
- while r.detectDown() or not r.down() do r.swingDown() end
- y = y - 1
- end
- while y1 > y do
- while r.detectUp() or not r.up() do r.swingUp() end
- y = y + 1
- end
- if a2 then
- turn(a2)
- end
- for i = 1, b2 do
- while r.detect() or not r.forward() do r.swing() end
- x = x + dx
- z = z + dz
- end
- if a1 then
- turn(a1)
- end
- for i = 1, b3 do
- while r.detect() or not r.forward() do r.swing() end
- x = x + dx
- z = z + dz
- end
- turn(b1)
- end
- -- ============================== Ф У Н К Ц И И ============================= --
- -- Таблица ресурсов
- local resources = {}
- resources['minecraft:log'] = true
- resources['minecraft:coal'] = true
- -- Замена инструмента
- --[==[
- function zamena()
- slotInstr = 2
- while (inv.getStackInInternalSlot(slotInstr) ~= nil) and (slotInstr < allSlots) do
- slotInstr = slotInstr + 1
- end
- r.select(slotInstr)
- inv.equip()
- turnLeft()
- r.drop()
- while inv.getStackInSlot(3,1).charge < inv.getStackInSlot(3,1).maxCharge do
- os.sleep(0.2)
- end
- r.suck()
- inv.equip()
- turnRight()
- slotInstr = nil
- end
- --]==]
- -- Возврат домой
- function dom(noreturn)
- local b1 = b
- local x1 = x
- local z1 = z
- local item
- local resources = {}
- resources['minecraft:log'] = true
- resources['minecraft:coal'] = true
- goTo(0, 0, 0, 2,false)
- turnLeft()
- 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
- r.select(16)
- r.suckDown(64-r.count(16))
- 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.5 do
- os.sleep(8)
- end
- else
- zamena()
- end
- end
- -- зарядка
- while comp.energy() < 25000 do
- os.sleep(8)
- end
- -- возвращаемся если нет флага "в один конец"
- if not noreturn then
- goTo(x1, z1, 0, b1, true)
- end
- end
- -- Проверка условий, на возврат домой
- function usl()
- if r.count(15) > 32 then
- dom(false)
- elseif r.count(16) < 8 then
- dom(false)
- elseif comp.energy() < 11000 then
- dom(false)
- elseif r.durability() < 0.3 then
- dom(false)
- end
- end
- --Рубка дерева
- function axe()
- r.select(16)
- if not r.compare(true) then
- r.swing()
- r.place()
- end
- end
- -- ======================== Г Л А В Н Ы Й Ц И К Л ======================== --
- -- робот движется по полю по кругу,
- -- и рубит выросшие деревья
- while true do
- num_of_turning = 1
- db = 0
- -- робот едет на стартовую позицию
- -- (левый передний угол поля)
- for i = 1, 4 do
- forward()
- end
- -- проход по ферме
- for i=1, num_all do
- -- проверка дерева слева и справа
- usl()
- forward()
- turnLeft()
- axe()
- turnAround()
- axe()
- turnLeft()
- os.sleep(1)
- while tracktorSuck() do end -- tractorSuck
- if rast_z == 1 or i%num_of_blocks_z == 0 then
- forward()
- else
- for zz = 1, rast_z do
- forward()
- end
- end
- if i%num_of_blocks_z == 0 and i<num_all then
- turn(1)
- for j=1, 3 + rast_x do forward() end
- db = (db + 2)%4
- turn(db)
- num_of_turning = num_of_turning + 1
- end
- end
- -- робот возвращается домой
- -- (флаг true означает - в один конец)
- dom(true)
- -- робот спит и видит сны =)
- term.clear()
- print('Progress of sleeping')
- for i=1,10 do
- os.sleep(72)
- print(i*10,' %')
- end
- term.clear()
- print('moving')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement