Advertisement
serafim7

lazer quarry [OpenComputers]

Nov 21st, 2018
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.64 KB | None | 0 0
  1. --[[ opencomputers лазерный карьер by serafim
  2.      pastebin.com/WGYGG261  update 09.06.2019
  3.  
  4. робот в качестве инструмента использует лазер из мода Industrial Craft
  5. из плюсов большая скорость работы, низкий расход энергии,
  6. за один подход проходит 30 слоёв породы.
  7. из минусов лазер плохо работает в жидкостях(вода,лава), в итоге
  8. робот может пропускать столбы пород, или застрять под лавой.
  9.  
  10. требования:
  11. корпус третьего уровня, притягивающий луч, инвентарь(можно три),
  12. контроллер инвентаря, генератор, улучшение парение,
  13. чанк лоадер если нужно.
  14.  
  15. пример сборки:
  16. https://i.imgur.com/JAm0IS0.png
  17.  
  18. номера слотов:
  19. 1: лазер из мода Industrial Craft
  20. 2: уголь
  21. 3: булыжник
  22.  
  23. использование:
  24. на старте, сзади робота поставить сундук.
  25. положить в инвентарь роботу:
  26. запасной заряженный лазер, уголь, булыжник.
  27. дать роботу ЛАЗЕР В РЕЖИМЕ ДАЛЬНЕГО ДЕЙСТВИЯ !
  28.  
  29. пример запуска: (по умолчанию один чанк)
  30. quarry 16 16 30  или  quarry
  31.  
  32. получить названия мусорных блоков  pastebin.com/q4g9JDeW
  33. --]]
  34.  
  35. local a = {...}
  36. local x = tonumber(a[1]) or 16 --ширина
  37. local y = tonumber(a[2]) or 16 --длина
  38. local z = tonumber(a[3]) or 30 --глубина одного слоя
  39.  
  40. local unloaded,countsort,PosZ,fuelinv,fuelchest,ridht,stepdown = 0,0,0,true,true,true,false
  41. local xPos,depth,zPos,xDir,zDir = 0,0,0,0,1
  42. local lazer,coal,cobble = 1,2,3
  43.  
  44. --список мусорных блоков
  45. local scrap = {
  46.   "minecraft:cobblestone",
  47.   "minecraft:dirt",
  48.   "minecraft:gravel",
  49.   "minecraft:flint",
  50.   "minecraft:sand",
  51.   "minecraft:sandstone",
  52.   "chisel:andesite",
  53.   "chisel:diorite",
  54.   "chisel:granite",
  55.   "chisel:limestone",
  56.   "minecraft:wheat_seeds",
  57. }
  58.  
  59. local computer = require("computer")
  60. local com = require("component")
  61.  
  62. if not com.isAvailable("robot") then
  63.   print("только роботы могут использовать эту программу")
  64.   os.exit()
  65. end
  66. local r = require("robot")
  67. local invsize = r.inventorySize()
  68.  
  69. if not com.isAvailable("inventory_controller") then
  70.   print("для работы нужен контроллер инвентаря")
  71.   os.exit()
  72. end
  73. local i_c = com.inventory_controller
  74.  
  75. if not com.isAvailable("generator") then
  76.   print("для работы нужен генератор")
  77.   os.exit()
  78. end
  79. local gen = com.generator
  80.  
  81. if not com.isAvailable("tractor_beam") then
  82.   print("для работы нужен притягивающий луч")
  83.   os.exit()
  84. end
  85. local tb = com.tractor_beam
  86.  
  87. if com.isAvailable("chunkloader") then
  88.   cloader = com.chunkloader
  89.   cloader.setActive(true)
  90.   print("чанк лоадер активен")
  91. end
  92.  
  93. --поиск предмета в инвентаре робота
  94. local function slotitem(slot, name)
  95.   local item = i_c.getStackInInternalSlot(slot)
  96.   if item and item.name == name then
  97.     return true
  98.   else
  99.     for i = 1, invsize do
  100.       local item = i_c.getStackInInternalSlot(i)
  101.       if item and item.name == name then
  102.         r.select(i)
  103.         r.transferTo(slot)
  104.         return true
  105.       end
  106.     end
  107.     print(name.."\n".."в инвентаре не найден :(")
  108.     return false
  109.   end
  110. end
  111.  
  112. --ставим под робота булыжник
  113. local function placecobble()
  114.   if not r.detectDown() then
  115.     if r.count(cobble) > 16 then
  116.       r.select(cobble)
  117.       r.placeDown()
  118.     else
  119.       slotitem(cobble, "minecraft:cobblestone")
  120.       r.select(cobble)
  121.       r.placeDown()
  122.     end
  123.   end
  124. end
  125.  
  126. --сортируем лут
  127. local function sortlut(droplut)
  128.   print("сортирую лут...")
  129.   --складываем лут в сундук
  130.   local unload = function()
  131.     local inv = i_c.getInventorySize(3)
  132.     if inv then
  133.       for i = 4,invsize do
  134.         if r.count(i) > 0 then
  135.           r.select(i)
  136.           unloaded = unloaded + r.count(i)
  137.           if not r.drop() then
  138.             returnHome("в сундуке нет места :(" , false)
  139.           end
  140.         end
  141.       end
  142.     else
  143.       returnHome("нет сундука :(" , false)
  144.     end
  145.     print("всего добыто руды: "..unloaded)
  146.     fuelchest = true
  147.     r.select(cobble)
  148.   end
  149.   --выкидываем мусор
  150.   local isscrap = function()
  151.     r.useDown()
  152.     for i = 4, invsize do      
  153.       local slot = i_c.getStackInInternalSlot(i)
  154.       if slot ~= nil then
  155.         for k, name in pairs(scrap) do
  156.           if slot.name == name then
  157.             r.select(i)
  158.             r.dropDown()
  159.           end
  160.         end
  161.       end
  162.     end
  163.     r.select(cobble)
  164.   end
  165.   --компонуем лут
  166.   local compact = function()
  167.     for i = 3, invsize do
  168.       if r.count(i) == 0 then
  169.         for j = invsize, 1, -1 do
  170.           if r.count(j) > 0 then
  171.             if j < i then
  172.               break
  173.             end
  174.             r.select(j)
  175.             r.transferTo(i)
  176.             break
  177.           end
  178.         end
  179.       end
  180.     end
  181.     r.select(cobble)
  182.   end
  183.   --возврат к сундуку и обратно
  184.   local gotochest = function()
  185.     xP,yP,zP,xD,zD = xPos,depth,zPos,xDir,zDir
  186.     print("возвращаюсь к сундуку сложить лут")
  187.     goTo( 0,0,0,0,-1 )
  188.     isscrap()
  189.     unload()
  190.     countsort = 0
  191.     goTo( xP,yP,zP,xD,zD )
  192.   end
  193.   --проверить есть ли запас угля
  194.   if r.count(coal) < 32 then
  195.     slotitem(coal, "minecraft:coal")
  196.   end
  197.   --проверить достигнут ли низ
  198.   if stepdown then
  199.     gotochest()
  200.     return
  201.   else
  202.     countsort = countsort + 1
  203.     if countsort == 1 then
  204.       PosZ = zPos
  205.     elseif countsort == 2 and zPos == PosZ then
  206.       local countcobbl = 0
  207.       for i = 4, invsize do
  208.         local item = i_c.getStackInInternalSlot(i)
  209.         if item and item.name == "minecraft:cobblestone" then
  210.           countcobbl = countcobbl + item.size
  211.         end
  212.         if countcobbl >= 128 then
  213.           isscrap()
  214.           print("ожидаю очистку мира от мусора...")
  215.           os.sleep(360)
  216.           if tb.suck() then
  217.             print("6 минут мало, ожидаю еще 6 минут")
  218.             os.sleep(360)
  219.           end
  220.           if tb.suck() then
  221.             returnHome("устал ждать :(" , true)
  222.           end
  223.           print("низ достигнут !")
  224.           stepdown = true
  225.           countsort = 0
  226.           return
  227.         end
  228.       end
  229.       countsort = 0
  230.     else
  231.       countsort = 0
  232.     end
  233.   end
  234.   --сортировка
  235.   isscrap()
  236.   if droplut then
  237.     unload()
  238.     return
  239.   end
  240.   compact()
  241.   if r.count(invsize - 4) > 0 then
  242.     gotochest()
  243.   end
  244. end
  245.  
  246. --стоим и заряжаемся, если генератор не справляется
  247. local function checkenergy()
  248.   if computer.energy() < 5000 then
  249.     if gen.count() >= 10 then
  250.       print("мало энергии, заряжаюсь до 5000")
  251.       while true do
  252.         os.sleep(30)
  253.         if computer.energy() >= 5000 then
  254.           print("зарядился :)")
  255.           break
  256.         end
  257.         if gen.count() == 0 then
  258.           returnHome("закончилось топливо :(" , true)
  259.         end
  260.       end
  261.     else
  262.       returnHome("закончилось топливо :(" , true)
  263.     end
  264.   end
  265. end
  266.  
  267. --возвращаемся к сундуку за углем
  268. local function fuelinchest()
  269.   if fuelchest then
  270.     xP,yP,zP,xD,zD = xPos,depth,zPos,xDir,zDir
  271.     print("возвращаюсь к сундуку за углем")
  272.     goTo( 0,0,0,0,-1 )
  273.     local inv = i_c.getInventorySize(3)
  274.     if inv then
  275.       for slot = 1, inv do
  276.         local item = i_c.getStackInSlot(3, slot)
  277.         if item and item.name == "minecraft:coal" then
  278.           r.select(coal)
  279.           if r.count(coal) > 0 then
  280.             r.dropDown()
  281.           end
  282.           i_c.suckFromSlot(3, slot)
  283.           gen.insert()
  284.           r.select(cobble)
  285.           print("заправился, топлива в генераторе = "..gen.count())
  286.           if r.count(4) > 0 then
  287.             sortlut(true)
  288.           end
  289.           goTo( xP,yP,zP,xD,zD )
  290.           return true
  291.         end
  292.       end
  293.       fuelchest = false
  294.       print("нет топлива в сундуке :(")
  295.       goTo( xP,yP,zP,xD,zD )
  296.     else
  297.       returnHome("нет сундука !" , false)
  298.     end
  299.   end
  300. end
  301.  
  302. --дозаправка углём из инвентаря
  303. local function checkfuel()
  304.   if fuelinv and gen.count() <= 10 then
  305.     local gencount = gen.count()
  306.     for i = 1,invsize do
  307.       local item = i_c.getStackInInternalSlot(i)
  308.       if item and item.name == "minecraft:coal" then
  309.         r.select(i)
  310.         r.transferTo(coal)
  311.       end
  312.     end
  313.     r.select(coal)
  314.     gen.insert()
  315.     if gen.count() > gencount then
  316.       print("заправился, топлива в генераторе = "..gen.count())
  317.     else
  318.       print("нет топлива в инвентаре робота !!!")
  319.       fuelinv = false
  320.       fuelinchest()
  321.     end
  322.     r.select(cobble)
  323.   end
  324. end
  325.  
  326. --ищем лазер в сундуке
  327. local function findlazerinchest()
  328.   xP,yP,zP,xD,zD = xPos,depth,zPos,xDir,zDir
  329.   print("возвращаюсь к сундуку за лазером")
  330.   goTo( 0,0,0,0,-1 )
  331.   local inv = i_c.getInventorySize(3)
  332.   if inv then
  333.     for slot = 1, inv do
  334.       local item = i_c.getStackInSlot(3, slot)
  335.       if item and item.name == "IC2:itemToolMiningLaser" then
  336.         print("лазер в слоте: "..slot.." заряд: "..item.charge)
  337.         if item.charge > 10000 then
  338.           r.select(lazer)
  339.           r.drop()
  340.           i_c.suckFromSlot(3, slot)
  341.           i_c.equip()
  342.           r.drop()
  343.           r.select(cobble)
  344.           print("взял лазер из сундука :)")
  345.           if r.count(4) > 0 then
  346.             sortlut(true)
  347.           end
  348.           goTo( xP,yP,zP,xD,zD )
  349.           return true
  350.         end
  351.       end
  352.     end
  353.     returnHome("нет заряженного лазера в сундуке :(" , false)
  354.   else
  355.     returnHome("нет сундука !" , false)
  356.   end
  357. end
  358.  
  359. --ищем лазер в инвентаре
  360. local function lazerequip()
  361.   if r.durability() == nil then
  362.     if slotitem(lazer, "IC2:itemToolMiningLaser") then
  363.       r.select(lazer)
  364.       i_c.equip()
  365.       r.select(cobble)
  366.       print("взял запасной лазер :)")
  367.     else
  368.       returnHome("положите в инвентарь лазер !!!" , true)
  369.     end
  370.   elseif r.durability() < 0.02 then
  371.     if slotitem(lazer, "IC2:itemToolMiningLaser") then
  372.       local device = i_c.getStackInInternalSlot(lazer)
  373.       if device.charge > 10000 then
  374.         r.select(lazer)
  375.         i_c.equip()
  376.         r.select(cobble)
  377.       else
  378.         findlazerinchest()
  379.       end
  380.     else
  381.       findlazerinchest()
  382.     end
  383.   end
  384. end
  385.  
  386. --идём по Х в 0 координат
  387. local function x_forward()
  388.   for xx = 1,x-1 do
  389.     while true do
  390.       if r.forward() then
  391.         xPos = xPos - 1
  392.         break
  393.       else
  394.         r.use()
  395.       end
  396.     end
  397.   end
  398. end
  399.  
  400. --идём вперед
  401. local function go_forward()
  402.   local tru = 0
  403.   while true do
  404.     if r.forward() then
  405.       xPos = xPos + xDir
  406.       zPos = zPos + zDir
  407.       break
  408.     else
  409.       r.use()
  410.       tru = tru + 1
  411.       if tru == 3 then
  412.         lazerequip()
  413.       end
  414.       if tru >= 10 then
  415.         tru = 0
  416.         returnHome("непреодолимое препятствие :(" , true)
  417.       end
  418.     end
  419.   end
  420. end
  421.  
  422. --спускаемся отлавливая бедрок
  423. local function go_down()
  424.   for zz = 1,z do
  425.     local tru = 0
  426.     while true do
  427.       if r.down() then
  428.         depth = depth + 1
  429.         break
  430.       else
  431.         r.useDown()
  432.         tru = tru + 1
  433.         if tru >= 5 then
  434.           stepdown = true
  435.           print("низ достигнут !")
  436.           lazerequip()
  437.           r.useUp()
  438.           for i = 1,2 do
  439.             if r.up() then
  440.               depth = depth - 1
  441.             end
  442.           end
  443.           xP,yP,zP,xD,zD = xPos,depth,zPos,xDir,zDir
  444.           while true do
  445.             r.use()
  446.             if not r.detect() then
  447.               return
  448.             else
  449.               if r.up() then
  450.                 depth = depth - 1
  451.               end
  452.               tru = tru + 1
  453.               if tru >= 10 then
  454.                 tru = 0
  455.                 returnHome("непреодолимое препятствие :(" , false)
  456.                 goTo( xP,yP,zP,xD,zD )
  457.                 return
  458.               end
  459.             end
  460.           end
  461.         end
  462.       end
  463.     end
  464.   end
  465. end
  466.  
  467. --поворот в лево
  468. local function turnLeft()
  469.   r.turnLeft()
  470.   xDir, zDir = -zDir, xDir
  471. end
  472.  
  473. --поворот в право
  474. local function turnRight()
  475.   r.turnRight()
  476.   xDir, zDir = zDir, -xDir
  477. end
  478.  
  479. --навигация к сундуку
  480. function goTo( xP, yP, zP, xD, zD )
  481.   while depth > yP do
  482.     if r.up() then
  483.       depth = depth - 1
  484.     else
  485.       r.useUp()
  486.       r.swingUp()
  487.     end
  488.   end
  489.   if xPos > xP then
  490.     while xDir ~= -1 do
  491.       turnLeft()
  492.     end
  493.     while xPos > xP do
  494.       if r.forward() then
  495.         xPos = xPos - 1
  496.       else
  497.         r.use()
  498.         r.swing()
  499.       end
  500.    end
  501.   elseif xPos < xP then
  502.     while xDir ~= 1 do
  503.       turnLeft()
  504.     end
  505.    while xPos < xP do
  506.       if r.forward() then
  507.         xPos = xPos + 1
  508.       else
  509.         r.use()
  510.         r.swing()
  511.       end
  512.     end
  513.   end
  514.   if zPos > zP then
  515.     while zDir ~= -1 do
  516.       turnLeft()
  517.     end
  518.     while zPos > zP do
  519.       if r.forward() then
  520.         zPos = zPos - 1
  521.       else
  522.         r.use()
  523.         r.swing()
  524.       end
  525.     end
  526.   elseif zPos < zP then
  527.     while zDir ~= 1 do
  528.       turnLeft()
  529.     end
  530.     while zPos < zP do
  531.       if r.forward() then
  532.         zPos = zPos + 1
  533.       else
  534.         r.use()
  535.         r.swing()
  536.       end
  537.     end
  538.   end
  539.   while depth < yP do
  540.     if r.down() then
  541.       depth = depth + 1
  542.     else
  543.       r.useDown()
  544.       r.swingDown()
  545.     end
  546.   end
  547.   while zDir ~= zD or xDir ~= xD do
  548.     turnLeft()
  549.   end
  550. end
  551.  
  552. --возврат к старту
  553. function returnHome(message, savepoint)
  554.   if savepoint then
  555.     xP,yP,zP,xD,zD = xPos,depth,zPos,xDir,zDir
  556.   end
  557.   goTo( 0,0,0,0,-1 )
  558.   sortlut(true)
  559.   goTo( 0,0,0,0,1 )
  560.   print("\n".."!!! стоп !!!".."\n"..message.."\n")
  561.   r.setLightColor(0xFF0000)
  562.   computer.beep(2000, 3)
  563.   io.write("продолжить копать ? (y/n) >")
  564.   oq = io.read()
  565.   if oq == "y" or oq == "Y" then
  566.     r.setLightColor(0xFFFFFF)
  567.     print("продолжаю копать...")
  568.     goTo( xP,yP,zP,xD,zD )
  569.   else
  570.     print("программа завершена")
  571.     os.exit()
  572.   end
  573. end
  574.  
  575. --копаем
  576. local function start()
  577.   r.setLightColor(0xFFFFFF)
  578.   print("размер карьера:  X "..x.."  Y "..y.."  Z "..z)
  579.   while true do
  580.     lazerequip()
  581.     checkfuel()
  582.     checkenergy()
  583.     print("опускаюсь на "..z.." блоков...")
  584.     r.select(cobble)
  585.     go_down()
  586.     print("копаю...")
  587.     for xx = 1,x do
  588.       for yy = 1,y-1 do
  589.         go_forward()
  590.         placecobble()
  591.         r.useUp()
  592.         lazerequip()
  593.         checkfuel()
  594.         checkenergy()
  595.         while tb.suck() do
  596.           if r.count(invsize) > 0 then
  597.             sortlut()
  598.             placecobble()
  599.           end
  600.         end
  601.       end
  602.       if xx ~= x then
  603.         if ridht then
  604.           turnRight()
  605.           go_forward()
  606.           turnRight()
  607.           ridht = false
  608.         else
  609.           turnLeft()
  610.           go_forward()
  611.           turnLeft()
  612.           ridht = true
  613.         end
  614.         placecobble()
  615.         r.useUp()
  616.         fuelinv = true
  617.       end
  618.     end
  619.     if ridht then
  620.       turnLeft()
  621.       x_forward()
  622.       turnLeft()
  623.       ridht = false
  624.     else
  625.       turnRight()
  626.       x_forward()
  627.       turnRight()
  628.       ridht = true
  629.     end
  630.     if stepdown then
  631.       goTo( 0,0,0,0,-1 )
  632.       sortlut(true)
  633.       r.turnAround()
  634.       print("\n".."завершил копать :)")
  635.       r.setLightColor(0xFF0000)
  636.       computer.beep(2000, 3)
  637.       os.exit()
  638.     end
  639.   end
  640. end
  641.  
  642. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement