Advertisement
ivan52

scan

Jun 25th, 2015
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.57 KB | None | 0 0
  1. local r = require('robot')
  2. local comp = require('computer')
  3. local term = require('term')
  4. local com = require('component')
  5. local geolyzer = com.geolyzer
  6. local inv = com.inventory_controller
  7. local gen = com.generator
  8. --количество кластеров
  9. x2=1
  10. y2=1
  11. z2=1
  12. --радиус кластера
  13. xk = 32
  14. yk = 32
  15. allSlots=r.inventorySize()
  16. --==============СПИСОК СНОСЯЩИХ УЛЬИ ИНСТРУМЕНТОВ=============--
  17. uleyInstr={}
  18. uleyInstr['ExtraBees:hive'] = true
  19. uleyInstr['IC2:itemToolMiningLaser'] = true
  20. --========================СПИСОК МУСОРА=======================--
  21. musor={}
  22. musor['minecraft:stone'] = true
  23. musor['minecraft:cobblestone'] = true
  24. musor['minecraft:dirt'] = true
  25. musor['minecraft:gravel'] = true
  26. musor['minecraft:sandstone'] = true
  27. musor['minecraft:stonebrick'] = true
  28. musor['minecraft:flint'] = true
  29. --========================СПИСОК БУРОВ========================--
  30. instr = {}
  31. instr['IC2:ItemToolDrill'] = true
  32. instr['IC2:itemToolDDrill'] = true
  33. instr['IC2:itemToolIridiumDrill'] = true
  34. instr['GraviSuiteAdvDDrill'] = true
  35. --======================ЗАМЕНА ИНСТРУМЕНТА====================--
  36. function zamena()
  37.   slotInstr = 1
  38.   while (inv.getStackInInternalSlot(slotInstr) ~= nil) and (slotInstr < allSlots) do
  39.     slotInstr = slotInstr + 1
  40.   end
  41.   r.select(slotInstr)
  42.   inv.equip()
  43.   if r.count(slotInstr) == 0 then
  44.     turnLeft()
  45.     r.suck()
  46.     inv.equip()
  47.     turnRight()
  48.   elseif instr[inv.getStackInInternalSlot(slotInstr).name] == nil then
  49.     turnAround()
  50.     while not r.drop() do
  51.       os.sleep(0.2)
  52.     end
  53.     turnRight()
  54.     r.suck()
  55.     inv.equip()
  56.     turnRight()
  57.   elseif instr[inv.getStackInInternalSlot(slotInstr).name] ~= nil then
  58.     turnLeft()
  59.     r.drop()
  60.     while inv.getStackInSlot(3,1).charge < inv.getStackInSlot(3,1).maxCharge do
  61.       os.sleep(0.2)
  62.     end
  63.     r.suck()
  64.     inv.equip()
  65.     turnRight()
  66.   end
  67.   slotInstr = nil
  68. end
  69. --======================ПОИСК ПО ИНВЕНТАРЮ====================--
  70. function poisk(item)
  71.   for slot=1,allSlots do
  72.     if inv.getStackInInternalSlot(slot) ~= nil then
  73.       if inv.getStackInInternalSlot(slot).name == item then
  74.         r.select(slot)
  75.         return true
  76.       end
  77.     end
  78.   end
  79.   return false
  80. end
  81. --===========================СНОС УЛЬЯ========================--
  82. function snos(storona)
  83.   if geolyzer.analyze(storona).name == "ExtraBees:hive"  then
  84.     if poisk('Forestry:scoop') then
  85.       inv.equip()
  86.       if storona == 1 then
  87.         r.swingUp()
  88.       elseif storona == 2 then
  89.         r.swingDown()
  90.       elseif storona == 3 then
  91.         r.swing()
  92.       end
  93.       inv.equip()
  94.       return true      
  95.     else
  96.       poisk('IC2:itemToolMiningLaser')
  97.       inv.equip()
  98.       if storona == 1 then
  99.          r.useUp()
  100.          os.sleep(0.5)
  101.          r.suckUp()
  102.       elseif storona == 2 then
  103.         r.useDown()
  104.         os.sleep(0.5)
  105.         r.suckDown()
  106.       elseif storona == 3 then
  107.         r.use()
  108.         os.sleep(0.5)
  109.         r.suck()
  110.       end
  111.       inv.equip()
  112.       return true    
  113.     end
  114.   end
  115.   return false
  116. end
  117. --===================== Н А В И Г А Ц И Я ====================--
  118. loc = {x=0, y=0, z=0, b=0}
  119. function forward()
  120.     attempt = 0
  121.     while r.detect() do
  122.       r.swing()
  123.       attempt = attempt + 1
  124.       if attempt > 20 then
  125.         snos(3)
  126.         attempt = 0
  127.       end
  128.     end
  129.     while not r.forward() do r.swing() end
  130.     if loc.b == 0 then loc.y = loc.y+1
  131.     elseif loc.b == 1 then loc.x = loc.x+1
  132.     elseif loc.b == 2 then loc.y = loc.y-1
  133.     else loc.x = loc.x-1 end
  134.     return true
  135. end
  136. function back()
  137.    if not r.back() then
  138.      r.turnAround()
  139.      while not r.forward() do
  140.        r.swing()
  141.      end
  142.      r.turnAround()
  143.     end
  144.     if loc.b == 0 then loc.y = loc.y-1
  145.     elseif loc.b == 1 then loc.x = loc.x-1
  146.     elseif loc.b == 2 then loc.y = loc.y+1
  147.     else loc.x = loc.x+1 end
  148.     return true
  149. end
  150. function up()
  151.   attempt = 0
  152.   while r.detectUp() do
  153.     r.swingUp()
  154.     attempt = attempt +1
  155.     if attempt > 20 then
  156.       snos(1)
  157.       attempt = 0
  158.     end
  159.   end  
  160.   while not r.up() do r.swingUp() end
  161.   loc.z = loc.z+1
  162.   return true
  163. end
  164. function down()
  165.   attempt = 0
  166.   while r.detectDown() do
  167.     r.swingDown()
  168.     attempt = attempt + 1
  169.     if attempt > 20 then
  170.       snos(1)
  171.       attempt = 0
  172.     end  
  173.   end
  174.   while not r.down() do r.swingDown() end
  175.   loc.z = loc.z-1
  176.   return true
  177. end
  178. function turnRight()
  179.   loc.b = (loc.b+3)%4
  180.   r.turnRight()
  181. end
  182. function turnAround()
  183.   loc.b = (loc.b+2)%4
  184.   r.turnAround()
  185. end
  186. function turnLeft()
  187.   loc.b = (loc.b+1)%4
  188.   r.turnLeft()
  189. end
  190. --=========================ПОВЕРНУТЬ В НУЖНУЮ СТОРОНУ======================--
  191. function povernut(tuda)
  192.   if tuda == 0 and loc.b == 3 then
  193.     turnLeft()
  194.   elseif tuda == 3 and loc.b == 0 then
  195.     turnRight()
  196.   end
  197.   while (loc.b < tuda) do
  198.     turnLeft()
  199.   end
  200.   while loc.b > tuda do
  201.     turnRight()
  202.   end
  203. end
  204. --============================К О П К А    Ж И Л Ы=========================--
  205. function check(b, x, y, z)
  206.   if b[x] == nil then return false end
  207.   if b[x][y] == nil then return false end
  208.   if b[x][y][z] == nil then
  209.     return false
  210.   else
  211.     return true
  212.   end
  213.   return false
  214. end
  215. function zila()
  216. --запись предыдущей позиции
  217. c[u]={x1=loc.x,y1=loc.y,z1=loc.z,b1=loc.b}
  218. --проверка на соседство и добыча блоков
  219.   if check(b, loc.x, loc.y, loc.z+1) then
  220.     r.swingUp()
  221.     up()
  222.     os.sleep(0.2)
  223.     a[  b[loc.x][loc.y][loc.z] ] = {x=l,y=l,z=l}
  224.     b[loc.x][loc.y][loc.z] = nil
  225.     u=u+1
  226.     zila()
  227.   elseif check(b, loc.x, loc.y+1, loc.z) then
  228.     povernut(0)
  229.     u=u+1
  230.     r.swing()
  231.     forward()
  232.     os.sleep(0.2)
  233.     a[ b[loc.x][loc.y][loc.z] ] = {x=l,y=l,z=l}
  234.     b[loc.x][loc.y][loc.z] = nil
  235.     zila()
  236.   elseif check(b, loc.x, loc.y-1, loc.z) then
  237.     povernut(2)
  238.     u=u+1
  239.     r.swing()
  240.     forward()
  241.     os.sleep(0.2)
  242.     a[ b[loc.x][loc.y][loc.z] ] = {x=l,y=l,z=l}
  243.     b[loc.x][loc.y][loc.z] = nil
  244.     zila()
  245.   elseif check(b, loc.x+1, loc.y, loc.z) then
  246.     povernut(1)
  247.     u=u+1
  248.     r.swing()
  249.     forward()
  250.     os.sleep(0.2)
  251.     a[ b[loc.x][loc.y][loc.z] ] = {x=l,y=l,z=l}
  252.     b[loc.x][loc.y][loc.z] = nil
  253.     zila()
  254.   elseif check(b, loc.x-1, loc.y, loc.z) then
  255.     povernut(3)
  256.     u=u+1
  257.     r.swing()
  258.     forward()
  259.     os.sleep(0.2)
  260.     a[b[loc.x][loc.y][loc.z] ] = {x=l,y=l,z=l}
  261.     b[loc.x][loc.y][loc.z] = nil
  262.     zila()
  263.   elseif check(b, loc.x, loc.y, loc.z-1) then
  264.     u=u+1
  265.     r.swingDown()
  266.     down()
  267.     os.sleep(0.2)
  268.     a[ b[loc.x][loc.y][loc.z] ] = {x=l,y=l,z=l}
  269.     b[loc.x][loc.y][loc.z] = nil
  270.     zila()
  271.   else
  272.  --возврат на предыдущую точку если нету блоков по соседству
  273.     u=u-1
  274.     os.sleep(0.2)
  275.     if u>0 then
  276.       if loc.z==c[u].z1 then
  277.          back()
  278.       else
  279.         while c[u].z1>loc.z do
  280.           up()
  281.         end
  282.         while c[u].z1<loc.z do
  283.           down()
  284.         end
  285.       end
  286.       povernut(c[u].b1)
  287.       zila()
  288.     else
  289.       return
  290.     end
  291.   end
  292. end
  293. --=========================ПОИСК БЛИЖАЙШЕГО БЛОКА РУДЫ=====================--
  294. function sravn()
  295.  if n>0 then
  296.   e={a[1].x,a[1].y,a[1].z}
  297.   q=math.abs(a[1].x-loc.x)+math.abs(a[1].y-loc.y) +math.abs(a[1].z-loc.z)
  298.   for i=2,n do
  299.     if q>math.abs(a[i].x-loc.x)+math.abs(a[i].y-loc.y) +math.abs(a[i].z-loc.z) and q~=0 then
  300.        q=math.abs(a[i].x-loc.x)+math.abs(a[i].y-loc.y) +math.abs(a[i].z-loc.z)
  301.        e={a[i].x,a[i].y,a[i].z}
  302.     end
  303.   end
  304.   if q==0 then
  305.     a[b[loc.x][loc.y][loc.z]]={x=l,y=l,z=l}
  306.     sravn()
  307.   end
  308.   else
  309. --если нету полезных блоков
  310.      e={l,l,l}
  311.   end
  312. end
  313. --=======================ВЫБРАСЫВАНИЕ ОТХОДОВ==========================--
  314. function vibros()
  315.   kolMus = 0
  316.   turnAround()
  317.   for MusorSlot=1,allSlots do
  318.    if (inv.getStackInInternalSlot(MusorSlot) ~= nil) then
  319.      if musor[inv.getStackInInternalSlot(MusorSlot).name] then
  320.          r.select(MusorSlot)
  321.          r.drop()
  322.          kolMus = kolMus + 1
  323.       end
  324.     end
  325.   end
  326.   r.select(1)
  327.   turnAround()
  328. end
  329. --Уплотнение Ресурсов в Инвентаре
  330. function uplotnenie(i2)
  331.     for i1 = i2, 1 , -1  do
  332.         while(r.count(i1) == 0 and i1 > 0) do i1 = i1 - 1 end
  333.         if (i1 > 0) then
  334.             r.select(i1)
  335.             local slot = 1
  336.             while (r.transferTo(slot) == false and slot < i1) do slot = slot + 1 end
  337.             i1 = i1 - 1
  338.         end
  339.     end
  340. end
  341. --=========================КОНТРОЛЬ ЭНЕРГИИ============================--
  342. function fuel()
  343.   if (comp.maxEnergy() - comp.energy()) > 500000 then
  344.     if gen.count() == 0 then
  345.       for i=1, allSlots do
  346.         r.select(i)
  347.         if gen.insert() then break end
  348.       end
  349.     end
  350.   end
  351. end
  352. --=========================ВОЗВРАТ ДОМОЙ===============================--
  353. function dom()
  354. local x=loc.x
  355. local y=loc.x
  356. local z=loc.x
  357.   while loc.z<0 do
  358.     r.swingUp()
  359.     up()
  360.   end
  361.   while loc.z>0 do
  362.     r.swingDown()
  363.     down()
  364.   end
  365.     povernut(1)
  366.     while loc.x<0 do
  367.       r.swing()
  368.       forward()
  369.     end
  370.     povernut(2)
  371.     while loc.y>0 do
  372.       r.swing()
  373.       forward()
  374.     end
  375. --выброс мусора
  376.     vibros()  
  377. --сброс полезностей
  378.     r.select(1)
  379.     for sl=1,allSlots do
  380. if  inv.getStackInInternalSlot(sl) ~= nil then
  381. if uleyInstr[inv.getStackInInternalSlot(sl).name] ~= true then
  382.   r.select(sl)
  383.   while r.drop() == false do
  384.     os.sleep(0.2)
  385.   end
  386. end
  387. end
  388.     end
  389.     r.select(1)
  390.     turnAround()
  391.     if r.durability() < 0.3 then
  392.       zamena()
  393.     end
  394.     while comp.energy()<comp.maxEnergy() * 0.7 do
  395.       os.sleep(10)
  396.     end
  397. --возврат на предыдущую точку,если нужно
  398.   if not noreturn then
  399.     while loc.y<y do
  400.       forward()
  401.     povernut(1)
  402.     while loc.x>x do
  403.       forward()
  404.     end
  405.     while loc.z>z do
  406.       down()
  407.     end
  408.     while loc.z<z do
  409.       up()
  410.     end
  411.   end
  412. end
  413. end
  414. --==============================УСЛОВИЯ ДЛЯ ВОЗВРАТА ДОМОЙ==============================--
  415. function usl()
  416.   if comp.energy() < comp.maxEnergy() * 0.3 then
  417.    dom()
  418.   end
  419.   if r.count(allSlots-1)>0 then
  420.     vibros()
  421.     uplotnenie(allSlots)
  422.      if kolMus < 2 then
  423.        dom()
  424.      end
  425.    end
  426.   if r.durability() < 0.3 then
  427.     dom()
  428.   end
  429. end
  430. --===============================ГЛАВНЫЙ ЦИКЛ==========================================--
  431. l=1000
  432. -- топорный алгоритм вычисления стороны света(зато без gps)
  433. forward()
  434. s={}
  435. n=0
  436. s[1]=geolyzer.scan(0,2)[33]
  437. s[2]=geolyzer.scan(0,-2)[33]
  438. s[3]=geolyzer.scan(2,0)[33]
  439. s[4]=geolyzer.scan(-2,0)[33]
  440. for r=1,4 do
  441.   if s[r]>2 then
  442.     n=n+1
  443.   end
  444. end
  445. if n>1 then
  446.   turnLeft()
  447.   for r1=1,3 do
  448.     forward()
  449.     a,b = r.detect()
  450.     if b == 'solid' then
  451.       r.swing()
  452.     end
  453.     back()
  454.     turnRight()
  455.   end
  456.   turnAround()
  457.   s[1]=geolyzer.scan(0,2)[33]
  458.   s[2]=geolyzer.scan(0,-2)[33]
  459.   s[3]=geolyzer.scan(2,0)[33]
  460.   s[4]=geolyzer.scan(-2,0)[33]
  461.   a=nil
  462.   b=nil
  463.   n=nil
  464.   r1=nil
  465. end
  466. if s[1]>2 then
  467.   napr=1
  468. elseif s[2]>2 then
  469.   napr=3
  470. elseif s[3]>2 then
  471.   napr=4
  472. else
  473.   napr=2
  474. end
  475. s=nil
  476. for qz=1,z2 do
  477. for qx=1,x2 do
  478. for qy=1,y2 do
  479. -- переезд на точку для скана
  480. while loc.z<20*(qz-1) do
  481.   r.swingUp()
  482.   up()
  483. end
  484. while loc.y<yk+1+(yk*2+1)*(qy-1) do
  485.   r.swing()
  486.   forward()
  487. end
  488. turnRight()
  489. while math.abs(loc.x)<xk+(xk*2+1)*(qx-1) do
  490.   r.swing()
  491.   forward()
  492. end
  493.   turnLeft()
  494. --таблицы для координат полезных блоков
  495. a={}
  496. b={}
  497. n=0
  498. --уровень бедрока если найдёт
  499. z2=19
  500. --сканирование
  501. for x1 = -1 * xk, xk do
  502.   b[x1+loc.x] = {}
  503.   for y1 = -1*yk, yk do
  504.     b[x1+loc.x][y1+loc.y] = {}
  505.     sk=geolyzer.scan(x1,y1)
  506.     for z1=21,45 do
  507.       if sk[z1]>2 and sk[z1]<40 then
  508.         n=n+1
  509.         if napr == 3 then
  510.           a[n] = {x=x1+loc.x,y=y1+loc.y,z=z1+loc.z-33}
  511.         elseif napr  ==  1 then
  512.           a[n] = {x=-x1+loc.x,y=-y1+loc.y,z=z1+loc.z-33}
  513.         elseif napr == 2 then
  514.           a[n] = {x=-y1+loc.x,y=x1+loc.y,z=z1+loc.z-33}
  515.         else
  516.           a[n] = {x=y1+loc.x,y=-x1+loc.y,z=z1+loc.z-33}  
  517.         end
  518.       end
  519.       -- поиск бедрока и запись его высоты
  520.       if sk[z1]<0 and z2<z1 and z1<33 then
  521.         z2=z1
  522.       end
  523.     end
  524.   end
  525. end
  526. if n>0 then
  527.   for r1=1,n do
  528.     b[a[r1].x][a[r1].y][a[r1].z] = r1
  529.   end
  530. end
  531. r1=nil
  532. z2=z2-33
  533. --отсев блоков если они ниже бедрока
  534. for i=1,n do
  535.  if a[i].z<=z2+loc.z then
  536.    a[i]={x=l,y=l,z=l}
  537.  end
  538. end
  539. sravn()
  540. --подход к ближайшему блоку
  541. while e[3] < 257 do
  542.   if e[2]>loc.y then
  543.     povernut(0)
  544.   else
  545.     povernut(2)
  546.   end
  547.   for i=1,math.abs(e[2]-loc.y) do
  548.     r.swing()
  549.     forward()
  550.     fuel()
  551.   end
  552.   if e[1]>loc.x then
  553.     povernut(1)
  554.   else
  555.     povernut(3)
  556.   end
  557.   for i=1,math.abs(e[1]-loc.x)-1 do
  558.     r.swing()
  559.     fuel()
  560.     forward()
  561.   end
  562.   while e[3]>loc.z do
  563.     r.swingUp()
  564.     up()
  565.     fuel()
  566.   end
  567.   while e[3]<loc.z do
  568.     r.swingDown()
  569.     down()
  570.     fuel()
  571.   end
  572. --проверка условий
  573.   usl()
  574. --объявление таблицы для функции zila
  575.   u=1
  576.   c={}
  577.   zila()
  578.   while loc.z<=z2 do
  579.     r.swingUp()
  580.     up()
  581.   end
  582.   sravn()
  583. end
  584. --удаление таблиц
  585. a=nil
  586. b=nil
  587. --возврат в один конец
  588. dom(true)
  589. end
  590. end
  591. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement