Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local APIS={}
- local inv_size = 16
- local inv_side = require("sides").left
- local charge_side = require("sides").right
- local battery_side = require("sides").bottom
- local scrap = {
- "minecraft:stone",
- "minecraft:cobblestone",
- "minecraft:dirt",
- "minecraft:gravel",
- "minecraft:sand",
- "minecraft:grass"}
- local lc = {x=0, y=0, z=0, dr=3}
- function APIS.charge(charge_side)
- comp = require("computer")
- while comp.energy() < (comp.maxEnergy()/2) do
- --local rs = require("component").redstone
- --rs.setOutput(charge_side, 15)
- require("term").clear()
- APIS.use(charge_side)
- print("Зарядка...")
- os.sleep(60)
- --rs.setOutput(charge_side, 0)
- APIS.use(charge_side)
- end
- print("Батарея заряжена")
- return true
- end
- function APIS.charge_tool(s)
- local inv = require("component").inventory_controller
- local r = require("robot")
- if r.durability() < 0.2 then
- r.select(1)
- inv.equip()
- if s == 1 then
- r.dropUp()
- os.sleep(15)
- r.suckUp()
- elseif s == 2 then
- r.turnAround()
- r.drop()
- os.sleep(15)
- r.suck()
- r.turnAround()
- elseif s == 3 then
- r.drop()
- os.sleep(15)
- r.suck()
- elseif s == 4 then
- r.turnRight()
- r.drop()
- os.sleep(15)
- r.suck()
- r.turnLeft()
- elseif s==5 then
- r.turnLeft()
- r.drop()
- os.sleep(15)
- r.suck()
- r.turnRight()
- else
- r.dropDown()
- os.sleep(15)
- r.suckDown()
- end
- end
- end
- function APIS.use(s)
- local r = require("robot")
- if s == 1 then r.useUp()
- elseif s == 2 then
- r.turnAround()
- r.use()
- r.turnAround()
- elseif s == 3 then r.use()
- elseif s == 4 then
- r.turnRight()
- r.use()
- r.turnLeft()
- elseif s==5 then
- r.turnLeft()
- r.use()
- r.turnRight()
- else
- r.useDown()
- end
- end
- function APIS.drop()
- local inv = require("component").inventory_controller
- local r= require("robot")
- if r.detect() then
- if inv.getInventorySize(3) ~= nil then
- for i=1,inv_size do
- r.select(i)
- r.drop()
- end
- else
- print("Блок не является сундуком.")
- end
- else
- print("Нет сундука.")
- end
- r.select(1)
- end
- function APIS.printState()
- print("Статус:")
- print(lc.x, lc.y, lc.z, lc.dr)
- end
- function APIS.harvest(count)
- local r=require("robot")
- for i=1,count do
- r.useDown()
- end
- end
- function APIS.dig(arg)
- r = require("robot")
- event = require("event")
- if arg == 3 then
- r.swing()
- elseif arg == 1 then
- while r.detectUp() do
- r.swingUp()
- event.pull(0.6)
- end
- elseif arg == 0 then
- r.swingDown()
- end
- end
- function APIS.ore_analyze(arg)
- if arg ~= nil then
- if require("component").isAvailable("geolyzer") then
- local lyz = require("component").geolyzer
- for i, name in pairs(scrap) do
- if lyz.analyze(arg).name == name then
- --print("Блок - мусор.")
- return false
- end
- end
- --print("Блок - Руда.")
- return true
- else
- --print("Геолайзер не обнаружен в системе.")
- return false
- end
- end
- end
- function APIS.dig_ore()
- local r=require("robot")
- if r.detectUp() then
- if APIS.ore_analyze(1) then
- APIS.dig(1)
- end
- end
- if r.detectDown() then
- if APIS.ore_analyze(0) then
- APIS.dig(0)
- end
- end
- end
- function APIS.turnLeft()
- local r = require("robot")
- r.turnLeft()
- if lc.dr == 3 then
- lc.dr = 5
- elseif lc.dr == 4 then
- lc.dr = 3
- elseif lc.dr == 2 then
- lc.dr = 4
- elseif lc.dr == 5 then
- lc.dr = 2
- end
- end
- function APIS.rot(side)
- if (side ~= 1) and (side ~= 0) then
- local sides = require("sides")
- local r = require("robot")
- while lc.dr ~= side do
- APIS.turnLeft()
- end
- end
- APIS.printState()
- end
- function APIS.mUp()
- local r = require("robot")
- local try = 1
- while not r.up() == true do
- r.swingUp()
- try = try + 1
- if try >= 10 then
- APIS.back_to_the_future()
- APIS.printState()
- return false
- end
- end
- if try < 10 then
- lc.y = lc.y + 1
- APIS.printState()
- return true
- end
- end
- function APIS.mDown()
- local r = require("robot")
- local event = require("event")
- local try = 1
- while not r.down() == true do
- r.swingDown()
- try = try + 1
- if try >= 10 then
- APIS.printState()
- APIS.back_to_the_future()
- return false
- end
- end
- if try < 10 then
- lc.y = lc.y - 1
- return true
- end
- end
- function APIS.mForw(action,arg)
- local r = require("robot")
- local event = require("event")
- local try = 1
- local sides = require("sides")
- if action ~= nil then
- action(arg)
- end
- while not r.forward() do
- r.swing()
- try = try + 1
- if try >= 10 then
- APIS.back_to_the_future()
- return false
- end
- end
- if try < 10 then
- if lc.dr==2 then
- lc.x = lc.x - 1
- elseif lc.dr==3 then
- lc.x = lc.x + 1
- elseif lc.dr==4 then
- lc.z = lc.z + 1
- elseif lc.dr==5 then
- lc.z = lc.z - 1
- end
- APIS.printState()
- return true
- end
- end
- function APIS.mTo(x, y, z, action, arg)
- local r = require("robot")
- local sides = require("sides")
- if lc.x > x then
- APIS.rot(sides.back)
- while lc.x > x do
- APIS.mForw(action,arg)
- end
- end
- if lc.x < x then
- APIS.rot(sides.forward)
- while lc.x < x do
- APIS.mForw(action,arg)
- end
- end
- if lc.z > z then
- APIS.rot(sides.left)
- while lc.z > z do
- APIS.mForw(action,arg)
- end
- end
- if lc.z < z then
- APIS.rot(sides.right)
- while lc.z < z do
- APIS.mForw(action,arg)
- end
- end
- while lc.y > y do
- APIS.mDown()
- --print("едем вниз")
- end
- while lc.y < y do
- APIS.mUp()
- --print("едем вверх")
- end
- end
- function APIS.plane(x, y, z, action, arg, forward)
- local dir = "forward"
- if forward then
- print("Плоскость вперед")
- for j=0,z do
- if dir == "forward" then
- print("Движение вперед")
- for i=0,x do
- print("шаг вперед")
- APIS.mTo(i,y,j,action,arg)
- end
- APIS.check_state()
- dir = "back"
- print("Поворот назад")
- else
- print("Движение назад")
- for i=x,0,-1 do
- print("шаг назад")
- APIS.mTo(i,y,j,action,arg)
- end
- APIS.check_state()
- dir = "forward"
- print("Поворот вперед")
- end
- end
- else
- print("Плоскость обратная")
- for j=z,0,-1 do
- if dir == "forward" then
- for i=x,0,-1 do
- APIS.mTo(i,y,j,action,arg)
- end
- APIS.check_state()
- dir = "back"
- else
- for i=x,0,-1 do
- APIS.mTo(i,y,j,action,arg)
- end
- APIS.check_state()
- dir = "forward"
- end
- end
- end
- end
- function APIS.home(action,arg)
- APIS.mTo(0,0,0, action,arg)
- APIS.rot(2)
- APIS.drop()
- APIS.rot(3)
- end
- function APIS.back_to_the_future()
- print("Возврат робота.")
- APIS.mTo(lc.x, 0, lc.z)
- APIS.home()
- print("Робот на исходной. Завершение программы.")
- os.exit()
- end
- function APIS.check_inv()
- local r = require("robot")
- local i = 16
- local count = 16
- while pcall(r, i) do
- count = i
- i=i+1
- end
- return count
- end
- function APIS.check_state()
- local r = require("robot")
- local inv = require("component").inventory_controller
- local comp = require("computer")
- local need_fuel = false
- if comp.energy() < (comp.maxEnergy()/4) then
- need_fuel = true
- end
- if (r.count(inv_size-2) > 0) or need_fuel then
- for i=1, inv_size do
- local slot = inv.getStackInInternalSlot(i)
- for j, name in pairs(scrap) do
- if slot.name == name then
- r.select(i)
- r.drop()
- --print("Блок - мусор.")
- elseif need_fuel then
- if slot.name == "minecraft:coal" then
- local gen = require("component").generator
- if gen.count() ~= 0 then
- r.select(i)
- gen.insert()
- end
- end
- end
- end
- end
- r.select(1)
- return true
- else
- return false
- end
- end
- function APIS.quarry(x, z)
- inv_size = APIS.check_inv()
- for i=0, -256, -3 do
- if i%2 == 0 then
- APIS.plane((x - 1),i,(z - 1), APIS.dig_ore, true)
- else
- APIS.plane((x - 1),i,(z - 1), APIS.dig_ore, false)
- end
- end
- APIS.home()
- end
- function APIS.field(x, y, z, time)
- while true do
- APIS.charge(require("sides").top)
- APIS.plane(x-1, y, z-1, harvest, 2)
- APIS.use(require("sides").down)
- APIS.use(require("sides").down)
- APIS.home()
- print("Ожидание созревания...", time, "минут.")
- os.sleep(60*time)
- end
- end
- function APIS.chop_single()
- local inv = require("component").inventory_controller
- local beam = require("component").tractor_beam
- local r = require("robot")
- local function start()
- APIS.rot(inv_side)
- APIS.drop()
- inv.suckFromSlot(3, APIS.inv_scaner("minecraft:sapling"), 1)
- APIS.rot(3)
- r.place()
- end
- start()
- while true do
- require("term").clear()
- print("Лесоруб, одиночное дерево. Ожидание роста саженца.")
- local _, wood = r.detect()
- if wood == "solid" then
- print("Рубка дерева.")
- r.swing()
- os.sleep(5)
- for i=1, 8 do
- beam.suck()
- end
- start()
- require("term").clear()
- APIS.charge(charge_side)
- APIS.charge_tool(battery_side)
- end
- os.sleep(10)
- end
- end
- function APIS.inv_scaner(filter)
- local inv = require("component").inventory_controller
- ins = inv.getInventorySize(3)
- if ins ~= nil then
- for i=1, ins do
- if inv.getStackInSlot(3, i).name == filter then
- print(filter.."найден!")
- return i
- end
- end
- print("Найти "..filter.."не удалось.")
- return 0
- end
- end
- return APIS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement