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 com = require('component')
- local geolyzer = com.geolyzer
- local inv = com.inventory_controller
- local gen = com.generator
- local modul = math.abs
- local x2=1
- local y2=1
- local z2=1
- local allSlots=r.inventorySize()
- --==============СПИСОК СНОСЯЩИХ УЛЬИ ИНСТРУМЕНТОВ=============--
- local uleyInstr={}
- uleyInstr['ExtraBees:hive'] = true
- uleyInstr['IC2:itemToolMiningLaser'] = true
- --========================СПИСОК МУСОРА=======================--
- local musor={}
- musor['minecraft:stone'] = true
- musor['minecraft:cobblestone'] = true
- musor['minecraft:dirt'] = true
- musor['minecraft:gravel'] = true
- musor['minecraft:sandstone'] = true
- musor['minecraft:stonebrick'] = true
- musor['minecraft:flint'] = true
- --========================СПИСОК БУРОВ========================--
- local instr = {}
- instr['IC2:ItemToolDrill'] = true
- instr['IC2:itemToolDDrill'] = true
- instr['IC2:itemToolIridiumDrill'] = true
- instr['GraviSuiteAdvDDrill'] = true
- --======================ЗАМЕНА ИНСТРУМЕНТА====================--
- function zamena()
- local slotInstr = 1
- while (inv.getStackInInternalSlot(slotInstr) ~= nil) and (slotInstr < allSlots) do
- slotInstr = slotInstr + 1
- end
- r.select(slotInstr)
- inv.equip()
- if r.count(slotInstr) == 0 then
- turnLeft()
- r.suck()
- inv.equip()
- turnRight()
- elseif instr[inv.getStackInInternalSlot(slotInstr).name] == nil then
- turnAround()
- while not r.drop() do
- os.sleep(0.2)
- end
- turnRight()
- r.suck()
- inv.equip()
- turnRight()
- elseif instr[inv.getStackInInternalSlot(slotInstr).name] ~= nil then
- 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()
- end
- slotInstr = nil
- end
- --======================ПОИСК ПО ИНВЕНТАРЮ====================--
- function poisk(item)
- for slot=1,allSlots do
- if inv.getStackInInternalSlot(slot) ~= nil then
- if inv.getStackInInternalSlot(slot).name == item then
- r.select(slot)
- return true
- end
- end
- end
- return false
- end
- --===========================СНОС УЛЬЯ========================--
- function snos(storona)
- if geolyzer.analyze(storona).name == "ExtraBees:hive" then
- if poisk('Forestry:scoop') then
- inv.equip()
- if storona == 1 then
- r.swingUp()
- elseif storona == 2 then
- r.swingDown()
- elseif storona == 3 then
- r.swing()
- end
- inv.equip()
- return true
- else
- poisk('IC2:itemToolMiningLaser')
- inv.equip()
- if storona == 1 then
- r.useUp()
- os.sleep(0.5)
- r.suckUp()
- elseif storona == 2 then
- r.useDown()
- os.sleep(0.5)
- r.suckDown()
- elseif storona == 3 then
- r.use()
- os.sleep(0.5)
- r.suck()
- end
- inv.equip()
- return true
- end
- end
- return false
- end
- --===================== Н А В И Г А Ц И Я ====================--
- loc = {x=0, y=0, z=0, b=0}
- function forward()
- if r.detect() then
- r.swing()
- popitka = 0
- while not r.forward() do
- r.swing()
- popitka = popitka +1
- if popitka > 20 then
- snos(3)
- end
- end
- else
- r.forward()
- end
- if loc.b == 0 then loc.y = loc.y+1
- elseif loc.b == 1 then loc.x = loc.x+1
- elseif loc.b == 2 then loc.y = loc.y-1
- else loc.x = loc.x-1 end
- return true
- end
- function back()
- if not r.back() then
- r.turnAround()
- while not r.forward() do
- r.swing()
- end
- r.turnAround()
- end
- if loc.b == 0 then loc.y = loc.y-1
- elseif loc.b == 1 then loc.x = loc.x-1
- elseif loc.b == 2 then loc.y = loc.y+1
- else loc.x = loc.x+1 end
- return true
- end
- function up()
- if r.detectUp() then
- r.swingUp()
- popitka = 0
- while not r.up() do
- r.swingUp()
- popitka = popitka +1
- if popitka > 20 then
- snos(1)
- end
- end
- else
- r.up()
- end
- loc.z = loc.z+1
- return true
- end
- function down()
- if r.detectDown() then
- r.swingDown()
- popitka = 0
- while not r.down() do
- r.swingDown()
- popitka = popitka +1
- if popitka > 20 then
- snos(1)
- end
- end
- else
- r.down()
- end
- loc.z = loc.z-1
- return true
- end
- function turnRight()
- loc.b = (loc.b+3)%4
- r.turnRight()
- end
- function turnAround()
- loc.b = (loc.b+2)%4
- r.turnAround()
- end
- function turnLeft()
- loc.b = (loc.b+1)%4
- r.turnLeft()
- end
- --=========================ПОВЕРНУТЬ В НУЖНУЮ СТОРОНУ======================--
- function povernut(tuda)
- if tuda == 0 and loc.b == 3 then
- turnLeft()
- elseif tuda == 3 and loc.b == 0 then
- turnRight()
- end
- while (loc.b < tuda) do
- turnLeft()
- end
- while loc.b > tuda do
- turnRight()
- end
- end
- --=========================ПОХОД В НУЖНУЮ ТОЧКУ============================--
- function goTo(x, y, z)
- while z>loc.z do
- up()
- end
- while z < loc.z do
- down()
- end
- if x > loc.x then
- povernut(1)
- elseif x < loc.x then
- povernut(3)
- end
- while modul(x-loc.x)>0 do
- forward()
- end
- if y > loc.y then
- povernut(0)
- elseif y < loc.y then
- povernut(2)
- end
- while modul(y-loc.y)>0 do
- forward()
- end
- end
- --============================ К О П К А Ж И Л Ы =========================--
- function check(b, x, y, z)
- if b[x] == nil then return false end
- if b[x][y] == nil then return false end
- if b[x][y][z] == nil then return false end
- return true
- end
- function zila()
- --запись предыдущей позиции
- c[u]={x1=loc.x,y1=loc.y,z1=loc.z,b1=loc.b}
- --проверка на соседство и добыча блоков
- if check(b, loc.x, loc.y, loc.z+1) then
- r.swingUp()
- up()
- a.x[b[loc.x][loc.y][loc.z]] = l
- a.y[b[loc.x][loc.y][loc.z]] = l
- a.z[b[loc.x][loc.y][loc.z]] = l
- b[loc.x][loc.y][loc.z] = nil
- u=u+1
- zila()
- elseif check(b, loc.x, loc.y+1, loc.z) then
- povernut(0)
- u=u+1
- r.swing()
- forward()
- a.x[b[loc.x][loc.y][loc.z]] = l
- a.y[b[loc.x][loc.y][loc.z]] = l
- a.z[b[loc.x][loc.y][loc.z]] = l
- b[loc.x][loc.y][loc.z] = nil
- zila()
- elseif check(b, loc.x, loc.y-1, loc.z) then
- povernut(2)
- u=u+1
- r.swing()
- forward()
- a.x[b[loc.x][loc.y][loc.z]] = l
- a.y[b[loc.x][loc.y][loc.z]] = l
- a.z[b[loc.x][loc.y][loc.z]] = l
- b[loc.x][loc.y][loc.z] = nil
- zila()
- elseif check(b, loc.x+1, loc.y, loc.z) then
- povernut(1)
- u=u+1
- r.swing()
- forward()
- a.x[b[loc.x][loc.y][loc.z]] = l
- a.y[b[loc.x][loc.y][loc.z]] = l
- a.z[b[loc.x][loc.y][loc.z]] = l
- b[loc.x][loc.y][loc.z] = nil
- zila()
- elseif check(b, loc.x-1, loc.y, loc.z) then
- povernut(3)
- u=u+1
- r.swing()
- forward()
- a.x[b[loc.x][loc.y][loc.z]] = l
- a.y[b[loc.x][loc.y][loc.z]] = l
- a.z[b[loc.x][loc.y][loc.z]] = l
- b[loc.x][loc.y][loc.z] = nil
- zila()
- elseif check(b, loc.x, loc.y, loc.z-1) then
- u=u+1
- r.swingDown()
- down()
- a.x[b[loc.x][loc.y][loc.z]] = l
- a.y[b[loc.x][loc.y][loc.z]] = l
- a.z[b[loc.x][loc.y][loc.z]] = l
- b[loc.x][loc.y][loc.z] = nil
- zila()
- else
- --возврат на предыдущую точку если нету блоков по соседству
- u=u-1
- if u>0 then
- if loc.z==c[u].z1 then
- back()
- else
- while c[u].z1>loc.z do
- up()
- end
- while c[u].z1<loc.z do
- down()
- end
- end
- povernut(c[u].b1)
- zila()
- else
- return
- end
- end
- end
- --=========================ПОИСК БЛИЖАЙШЕГО БЛОКА РУДЫ=====================--
- function sravn()
- if n > 0 then
- local num = 2
- local q = modul(a.x[1]-loc.x)+modul(a.y[1]-loc.y) + modul(a.z[1]-loc.z)
- if q > 0 then
- e = {x = a.x[1],y = a.y[1],z = a.z[1]}
- else
- a.x[b[loc.x][loc.y][loc.z]] = l
- a.y[b[loc.x][loc.y][loc.z]] = l
- a.z[b[loc.x][loc.y][loc.z]] = l
- e = {x = a.x[2],y = a.y[2],z = a.z[2]}
- num = 3
- end
- for i = num, n do
- k = modul(a.x[i]-loc.x)+modul(a.y[i]-loc.y) +modul(a.z[i]-loc.z)
- if (q > k) and (k > 0) then
- q = k
- e = {x = a.x[i],y = a.y[i],z = a.z[i]}
- elseif k==0 then
- a.x[b[loc.x][loc.y][loc.z]] = l
- a.y[b[loc.x][loc.y][loc.z]] = l
- a.z[b[loc.x][loc.y][loc.z]] = l
- end
- end
- --если нету полезных блоков
- else
- e={x = l, y = l,z = l}
- end
- end
- --=======================ВЫБРАСЫВАНИЕ ОТХОДОВ==========================--
- function vibros()
- local kolMus = 0
- turnAround()
- for MusorSlot=1,allSlots do
- if inv.getStackInInternalSlot(MusorSlot) ~= nil then
- if musor[inv.getStackInInternalSlot(MusorSlot).name] then
- r.select(MusorSlot)
- r.drop()
- kolMus = kolMus + 1
- end
- end
- end
- r.select(1)
- turnAround()
- return kolMus
- end
- --=========================КОНТРОЛЬ ЭНЕРГИИ============================--
- function fuel()
- if (comp.maxEnergy() - comp.energy()) > 500000 then
- if gen.count() == 0 then
- for i=1, allSlots do
- r.select(i)
- if gen.insert() then break end
- end
- end
- end
- end
- --=========================ВОЗВРАТ ДОМОЙ===============================--
- function dom(noreturn)
- local x = loc.x
- local y = loc.x
- local z = loc.x
- goTo(0, 0, 0)
- --выброс мусора
- vibros()
- --сброс полезностей
- r.select(1)
- for sl=1,allSlots do
- if inv.getStackInInternalSlot(sl) ~= nil then
- if uleyInstr[inv.getStackInInternalSlot(sl).name] ~= true then
- r.select(sl)
- while r.drop() == false do
- os.sleep(0.2)
- end
- end
- end
- end
- r.select(1)
- turnAround()
- if r.durability() < 0.3 then
- zamena()
- end
- while comp.energy()<comp.maxEnergy() * 0.7 do
- os.sleep(10)
- end
- --возврат на предыдущую точку,если нужно
- if not noreturn then
- sravn()
- while loc.y < e.y do
- forward()
- end
- povernut(1)
- while loc.x > e.x do
- forward()
- end
- while loc.z > e.z do
- down()
- end
- while loc.z < e.z do
- up()
- end
- end
- end
- --==============================УСЛОВИЯ ДЛЯ ВОЗВРАТА ДОМОЙ==============================--
- function usl()
- if comp.energy() < comp.maxEnergy() * 0.3 then
- dom()
- elseif r.count(allSlots-1)>0 then
- if vibros() < 2 then
- dom()
- end
- elseif r.durability() < 0.3 then
- dom()
- end
- end
- --===============================ГЛАВНЫЙ ЦИКЛ==========================================--
- l=1000
- -- топорный алгоритм вычисления стороны света(зато без gps)
- forward()
- local s={}
- n=0
- s[1]=geolyzer.scan(0,2)[33]
- s[2]=geolyzer.scan(0,-2)[33]
- s[3]=geolyzer.scan(2,0)[33]
- s[4]=geolyzer.scan(-2,0)[33]
- for r=1,4 do
- if s[r]>2 then
- n=n+1
- end
- end
- if n>1 then
- turnLeft()
- for r1=1,3 do
- forward()
- a,b = r.detect()
- if b == 'solid' then
- r.swing()
- end
- back()
- turnRight()
- end
- turnAround()
- s[1]=geolyzer.scan(0,2)[33]
- s[2]=geolyzer.scan(0,-2)[33]
- s[3]=geolyzer.scan(2,0)[33]
- s[4]=geolyzer.scan(-2,0)[33]
- a=nil
- b=nil
- n=nil
- r1=nil
- end
- if s[1]>2 then
- napr=1
- elseif s[2]>2 then
- napr=3
- elseif s[3]>2 then
- napr=4
- else
- napr=2
- end
- s=nil
- for qz=1,z2 do
- for qx=1,x2 do
- for qy=1,y2 do
- -- переезд на точку для скана
- while loc.z<20*(qz-1) do
- up()
- end
- while loc.y<10+19*(qy-1) do
- forward()
- end
- turnRight()
- while math.abs(loc.x)<9+19*(qx-1) do
- forward()
- end
- turnLeft()
- --таблицы для координат полезных блоков
- a={ x={}, y={} ,z={} }
- b={}
- n=0
- --уровень бедрока если найдёт
- z2=19
- --сканирование
- for x1=-9,9 do
- b[x1+loc.x] = {}
- for y1=-9,9 do
- b[x1+loc.x][y1+loc.y] = {}
- sk=geolyzer.scan(x1,y1)
- for z1=21,45 do
- if sk[z1]>2 and sk[z1]<40 then
- n=n+1
- if napr == 3 then
- a.x[n] = x1+loc.x
- a.y[n] = y1+loc.y
- elseif napr == 1 then
- a.x[n] = -x1+loc.x
- a.y[n] = -y1+loc.y
- elseif napr == 2 then
- a.x[n] = -y1+loc.x
- a.y[n] = x1+loc.y
- else
- a.x[n] = y1+loc.x
- a.y[n] = -x1+loc.y
- end
- a.z[n] = z1+loc.z-33
- end
- -- поиск бедрока и запись его высоты
- if sk[z1]<0 and z2<z1 and z1<33 then
- z2=z1
- end
- end
- end
- end
- if n>0 then
- for r1=1,n do
- b[a.x[r1]][a.y[r1]][a.z[r1]] = r1
- end
- end
- r1=nil
- z2=z2-33
- --отсев блоков если они ниже бедрока
- for i=1,n do
- if a.z[i]<=z2+loc.z then
- a.x[i] = l
- a.y[i] = l
- a.z[i] = l
- end
- end
- --начало копки
- sravn()
- while e.z < 257 do
- --подход к ближайшему блоку
- if loc.x < e.x then
- goTo(e.x - 1, e.y, e.z)
- elseif loc.x > e.x then
- goTo(e.x + 1, e.y, e.z)
- else
- goTo(e.x, e.y, e.z)
- end
- --проверка условий
- usl()
- --объявление таблицы для функции zila
- u=1
- c={}
- zila()
- while loc.z<=z2 do
- r.swingUp()
- up()
- end
- sravn()
- end
- --удаление таблиц
- a=nil
- b=nil
- --возврат в один конец
- dom(true)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement