Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local c = require("component")
- local fs = require("filesystem")
- local term = require('term')
- local r = require("robot")
- local srl = require("serialization")
- local ic = c.inventory_controller
- local red = c.redstone
- local craftItem = {}
- local function rob(np,n,rz) --движение робота (направление, повтор, не разрушать) (0;вниз 1;вверх 2:назад 3:вперед 4:налево 5:направо 6:кругом)
- local err = 0
- local errStop = 10
- if not n then
- n = 1
- end
- for i=1,n do
- if np == 1 then --вверх
- while not r.up() do
- if not rz then
- r.swingUp()
- else
- os.sleep(0)
- if err > errStop then
- break
- else
- err = err + 1
- end
- end
- end
- elseif np == 0 then --вниз
- while not r.down() do
- if not rz then
- r.swingDown()
- else
- os.sleep(0)
- if err > errStop then
- break
- else
- err = err + 1
- end
- end
- end
- elseif np == 3 then --вперед
- while not r.forward() do
- if not rz then
- r.swing()
- else
- os.sleep(0)
- if err > errStop then
- break
- else
- err = err + 1
- end
- end
- end
- elseif np == 4 then --налево
- r.turnLeft()
- elseif np == 5 then --направо
- r.turnRight()
- elseif np == 6 then --кругом
- r.turnAround()
- elseif np == 2 then --назад
- while not r.back() do
- if not rz then
- rob(6)
- rob(3)
- rob(6)
- else
- os.sleep(0)
- if err > errStop then
- break
- else
- err = err + 1
- end
- end
- end
- end
- end
- if err > errStop then
- return false
- else
- return true
- end
- end
- local function build()
- rob(3,3)
- r.select(1)
- for z = 1, 3 do
- for y = 1, 3 do
- for x = 1, 3 do
- if z == 2 and y == 2 and x == 2 then
- r.select(2)
- r.placeDown(0)
- r.select(1)
- else
- r.placeDown(0)
- end
- if x ~= 3 then
- rob(3,1)
- end
- end
- if math.fmod(y, 2) == 1 then
- if y ~= 3 then
- rob(4)
- rob(3,1)
- rob(4)
- end
- else
- rob(5)
- rob(3,1)
- rob(5)
- end
- end
- rob(5)
- rob(3,2)
- rob(5)
- rob(3,2)
- rob(6)
- rob(1)
- end
- rob(3)
- rob(4)
- rob(3)
- r.select(3)
- r.dropDown(1)
- end
- local function seachLabel(label, size) --поиск в сундуке
- for i=1, ic.getInventorySize(1) do
- vr = ic.getStackInSlot(1, i)
- if vr and vr.label == label and vr.size >= size then
- return i
- end
- end
- return false
- end
- local function loadCraft()
- print('Загрузка базы крафта')
- local f = io.open("/usr/bin/craft.tbl", "r")
- if f then
- craftItem = srl.unserialize(f:read("*a"))
- f:close()
- print('Успешно!')
- else
- print('Ошибка!')
- print('Необходимо закрепить ресурсы крафта')
- for i = 1, 2 do
- print('Разложите ресурсы для #'..i..' крафта')
- print('Пожалуйста положите: \n 1 слот образец стенки\n 2 слот центральный блок \n 3 слот предмет активации \nПо окончании нажмите enter')
- io.read()
- craftItem[i] = {ic.getStackInInternalSlot(1).label, ic.getStackInInternalSlot(2).label, ic.getStackInInternalSlot(3).label}
- print('Крафт #'..i..' записан')
- end
- if not fs.exists('/usr/bin/') then
- fs.makeDirectory('/usr/bin/')
- end
- f = io.open("/usr/bin/craft.tbl", "w")
- f:write(srl.serialize(craftItem))
- f:close()
- end
- end
- local function seachItem()
- local tmp = {}
- local size = 1
- for i = 1, 2 do
- for j = 1, 3 do
- if j == 1 then
- size = 26
- else
- size = 1
- end
- print('Поиск '..craftItem[i][j])
- if seachLabel(craftItem[i][j], size) then
- tmp[j] = true
- else
- tmp[j] = false
- end
- end
- if tmp[1] and tmp[2] and tmp[3] then
- return i
- end
- end
- return false
- end
- local function export()
- for i = 1, r.inventorySize() do
- if r.count(i) > 0 then
- r.select(i)
- r.dropUp(64)
- end
- end
- r.select(1)
- end
- local function importItem(craft)
- local tmp, size = 0, 0
- for i = 1, 3 do
- r.select(i)
- if i == 1 then
- size = 26
- else
- size = 1
- end
- tmp = seachLabel(craftItem[craft][i], size)
- if tmp then
- if ic.suckFromSlot(1, tmp, size) ~= size then
- print('Ошибка, не смог собрать ресурсы')
- export()
- return false
- end
- else
- print('Ошибка, нехватает ресурсов')
- export()
- return false
- end
- end
- return true
- end
- local function goHome()
- rob(4)
- rob(3,3)
- rob(4)
- rob(3,1)
- rob(0,3)
- rob(5)
- rob(3,1)
- rob(6)
- end
- local function sorting()
- print('Выполняется объединение стаков')
- for i = 1, ic.getInventorySize(1) do
- r.suckUp(64)
- end
- export()
- end
- loadCraft()
- sorting()
- term.clear()
- while true do
- if red.getInput(4) > 0 then
- print('Поиск ресурсов для крафта')
- tmp = seachItem()
- if tmp then
- print('Ресурсы для крафта #'..tmp..' найдены')
- if importItem(tmp) then
- print('Выполняется строительство крафта')
- build()
- export()
- print('Возврат на базу')
- goHome()
- sorting()
- print('Ожидаю завершения крафта')
- os.sleep(20)
- else
- term.clear()
- print('Ошибка при получении ресурсов!')
- os.sleep(10)
- end
- else
- sorting()
- term.clear()
- print('Ресурсы не найдены!')
- os.sleep(10)
- end
- else
- term.clear()
- print('Ожидаю сигнала редстоуна')
- os.sleep(10)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement