Advertisement
ivan52

TestScan

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