Advertisement
serafim7

quarry карьер Без замены инструмента + пчёлы из Extra Bees [OpenComputers]

Feb 7th, 2021 (edited)
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.99 KB | None | 0 0
  1. --[[ opencomputers карьер by serafim
  2.      pastebin.com/jgdzfdZe   update 13.06.21
  3.  
  4. Карьер без замены инструмента + пчёлы из Extra Bees !!!
  5.  
  6. Копает змейкой квадрат проходя три слоя блоков за один подход,
  7. пока не выкопает весь объём до бэдрока или не упрётся в
  8. не разрушаемый блок, при этом вернётся к старту и сообщит о проблеме,
  9. также предложит продолжить копать с последней позиции.
  10. Умеет ломать сачком подземные улики.
  11.  
  12. требования:
  13. инвентарь, контроллер инвентаря, генератор, улучшение парение.
  14.  
  15. пример сборки:
  16. https://i.imgur.com/fR7F4fA.png
  17.  
  18. использование:
  19. на старте, сзади робота поставить сундук, положить в сундук
  20. уголь больше 20 штук, сачок из мода forestry.
  21. Робот может работать без сундука и всего прочего, нужна только кирка.
  22.  
  23. пример запуска: (по умолчанию один чанк)
  24. quarry 16 16 2  или  quarry
  25. -- 2 опустится в низ на 2 блока от старта.
  26.  
  27. получить список предметов:
  28. https://pastebin.com/au9etcfF
  29. ]]--
  30.  
  31. local a = {...}
  32. local width = tonumber(a[1]) or 16 --ширина
  33. local length = tonumber(a[2]) or 16 --длина
  34. local down = tonumber(a[3]) or 2 --опустится в низ
  35.  
  36. --список мусора
  37. local scrap = {
  38.   "cobblestone", --булыжник
  39.   "stone",       --камень
  40.   "dirt",        --земля
  41.   "gravel",      --гравий
  42.   "flint",       --кремень
  43.   "sand",        --песок
  44.   "sandstone"    --песчаник
  45. }
  46.  
  47. local event = require("event")
  48. local com = require('component')
  49. local computer = require("computer")
  50. local cheststart,fuelchest,fuelinv,refuel = true,true,true,true
  51. local depth,xPos,zPos,xDir,zDir = 0,0,0,0,1
  52. local ridht,bedrock,unloaded = true,false,0
  53. local timestart = computer.uptime()
  54. local x,y,z,xd,zd
  55.  
  56. if not com.isAvailable("robot") then
  57.   print("только роботы могут использовать эту программу")
  58.   os.exit()
  59. end
  60. local r = require("robot")
  61.  
  62. local invsize = r.inventorySize()
  63. local coal = invsize
  64. local tools = invsize-1
  65. local scoop = invsize-2
  66.  
  67. if not com.isAvailable("inventory_controller") then
  68.   print("нет контроллера инвентаря")
  69.   os.exit()
  70. end
  71. local i_c = com.inventory_controller
  72.  
  73. if not com.isAvailable("generator") then
  74.   print("нет генератора")
  75.   os.exit()
  76. end
  77. local gen = com.generator
  78.  
  79. --статус
  80. local function status(message)
  81.   print("[ "..os.date("%H:%M:%S",computer.uptime()-timestart).." ] "..message)
  82. end
  83.  
  84. --чанк лоадер
  85. local function chunkload(state)
  86.   if com.isAvailable("chunkloader") then
  87.     if state then
  88.       com.chunkloader.setActive(true)
  89.       print("чанк лоадер активен")
  90.     else
  91.       com.chunkloader.setActive(false)
  92.       print("чанк лоадер деактивирован".."\n")
  93.     end
  94.   end
  95. end
  96.  
  97. --поиск предмета в инвентаре робота
  98. local function slotitem(slot, list)
  99.   local item = i_c.getStackInInternalSlot(slot)
  100.   if item then
  101.     for j, name in pairs(list) do
  102.       if string.find(item.name,name) then
  103.         if item.charge == nil or item.charge/item.maxCharge > toolstrength then
  104.           return true
  105.         end
  106.       end
  107.     end
  108.   end
  109.   for i = 1,invsize do
  110.     if r.count(i) > 0 then
  111.       for j, name in pairs(list) do
  112.         local item = i_c.getStackInInternalSlot(i)
  113.         if item and string.find(item.name,name) then
  114.           r.select(i)
  115.           r.transferTo(slot)
  116.           return true
  117.         end
  118.       end
  119.     end
  120.   end
  121.   return false
  122. end
  123.  
  124. --взять из сундука расходники
  125. local function servise(slotinv, list)
  126.   local inv = i_c.getInventorySize(3)
  127.   if inv then
  128.     if slotitem(slotinv, list) then
  129.       return true
  130.     end
  131.     r.select(slotinv)
  132.     if r.count(slotinv) > 0 and not r.drop() then
  133.       returnHome("в сундуке нет места")
  134.       return false
  135.     end
  136.     for slot = 1,inv do
  137.       for j, name in pairs(list) do
  138.         local item = i_c.getStackInSlot(3,slot)
  139.         if item and string.find(item.name,name) then
  140.           i_c.suckFromSlot(3, slot)
  141.           return true
  142.         end
  143.       end
  144.     end
  145.     return false
  146.   else
  147.     returnHome("нет сундука")
  148.   end
  149. end
  150.  
  151. --заряжаемся от зарядчика
  152. local function charging()
  153.   local chardg = true
  154.   while true do
  155.     local e1 = computer.energy()
  156.     os.sleep(2)
  157.     local e2 = computer.energy()  
  158.     if e2 > e1 + 500 then
  159.       if chardg then
  160.         chardg = false
  161.         status("заряжаюсь...")
  162.       end
  163.       os.sleep(2)
  164.     else
  165.       break
  166.     end
  167.   end
  168. end
  169.  
  170. --сортируем лут
  171. local function sortlut(droplut)
  172.   status("сортирую лут...")
  173.   r.swingDown()
  174.   for i = 1,invsize do      
  175.     if r.count(i) > 0 then
  176.       local item = i_c.getStackInInternalSlot(i)
  177.       if string.find(item.name,"coal") then
  178.         r.select(i)
  179.         r.transferTo(coal)
  180.       else
  181.         for n = 1,#scrap do
  182.           if scrap[n] == item.name:gsub('%g+:','') then
  183.             r.select(i)
  184.             r.dropDown()
  185.             break
  186.           end
  187.         end
  188.       end
  189.     end
  190.   end
  191.   for i = 1,invsize-3 do
  192.     if r.count(i) == 0 then
  193.       for j = invsize-3,1,-1 do
  194.         if r.count(j) > 0 then
  195.           if j < i then
  196.             break
  197.           end
  198.           r.select(j)
  199.           r.transferTo(i)
  200.           break
  201.         end
  202.       end
  203.     end
  204.   end
  205.   local unload = function()
  206.     local inv = i_c.getInventorySize(3)
  207.     if inv then
  208.       cheststart = true
  209.       for i = 1,invsize-3 do
  210.         if r.count(i) > 0 then
  211.           r.select(i)
  212.           unloaded = unloaded + r.count(i)
  213.           if not r.drop() then
  214.             returnHome("в сундуке нет места")
  215.             return
  216.           end
  217.         end
  218.       end
  219.       servise(scoop, {"scoop"})
  220.       r.select(coal)
  221.       gen.insert()
  222.       servise(coal, {"coal"})
  223.       charging()
  224.     elseif not cheststart then
  225.       returnHome("инвентарь заполнен")
  226.     else
  227.       returnHome("нет сундука")
  228.     end
  229.     fuelchest = true
  230.     status("всего добыто руды: "..unloaded)
  231.   end
  232.   if r.count(invsize-6) > 0 then
  233.     status("возвращаюсь к сундуку сложить лут")
  234.     x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  235.     goTo( 0,0,0,0,-1 )
  236.     unload()
  237.     goTo( x,y,z,xd,zd )
  238.   end
  239.   if droplut and cheststart then
  240.     unload()
  241.   end
  242.   r.select(1)
  243. end
  244.  
  245. --стоп
  246. local function stop(message)
  247.   print("\n".."!!! стоп !!!".."\n"..message.."\n")
  248.   r.setLightColor(0xFF0000)
  249.   r.turnAround()
  250.   chunkload(false)
  251.   computer.beep(1000, 1)
  252.   status("программа завершена")
  253.   os.exit()
  254. end
  255.  
  256. --возврат к старту
  257. function returnHome(message, save)
  258.   r.setLightColor(0xFF0000)
  259.   if save then
  260.     x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  261.   end
  262.   if y ~= nil then
  263.     print("последняя ширина "..width..", длина "..length..", глубина "..y)
  264.   end
  265.   goTo( 0,0,0,0,-1 )
  266.   if save and cheststart and r.count(1) > 0 then
  267.     sortlut(true)
  268.   end
  269.   goTo( 0,0,0,0,1 )
  270.   status("!!! стоп !!!".."\n"..message.."\n")
  271.   chunkload(false)
  272.   print("продолжить копать ? [Y/n]")
  273.   while true do
  274.     r.setLightColor(0xFFFFFF)
  275.     computer.beep(1000, 1)
  276.     r.setLightColor(0xFF0000)
  277.     local e = ({event.pull(10,"key_down")})[4]
  278.     if e == 49 then
  279.       computer.beep(500, 0.1)
  280.       print("программа завершена")
  281.       os.exit()
  282.     elseif e == 21 or e == 28 then
  283.       computer.beep(500, 0.1)
  284.       r.setLightColor(0xFFFFFF)
  285.       print("продолжаю копать...")
  286.       chunkload(true)
  287.       goTo( x,y,z,xd,zd )
  288.       break
  289.     end
  290.   end
  291. end
  292.  
  293. --дозаправка углём
  294. local function checkfuel()
  295.   if computer.energy() <= 6000 then
  296.     refuel = true
  297.   end
  298.   if gen.count() <= 10 and refuel then
  299.     for i = 1,invsize do
  300.       local item = i_c.getStackInInternalSlot(i)
  301.       if item and string.find(item.name,"coal") then
  302.         r.select(i)
  303.         r.transferTo(coal)
  304.       end
  305.     end
  306.     r.select(coal)
  307.     if gen.insert() then
  308.       fuelinv = true
  309.       status("заправился, угля в генераторе = "..gen.count())
  310.     elseif fuelchest and cheststart then
  311.       x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  312.       status("возвращаюсь к сундуку за углем")
  313.       goTo( 0,0,0,0,-1 )
  314.       if servise(coal, {"coal"}) then
  315.         r.select(coal)
  316.         gen.insert()
  317.         status("заправился, угля в генераторе = "..gen.count())
  318.       else
  319.         fuelchest = false
  320.         status("в сундуке нет угля")
  321.       end
  322.       charging()
  323.       goTo( x,y,z,xd,zd )
  324.     else
  325.       refuel = false
  326.       if fuelinv then
  327.         fuelinv = false
  328.         status("нет угля в инвентаре робота !!!")
  329.       end
  330.     end
  331.     r.select(1)
  332.   end
  333.   if computer.energy() <= 5000 then
  334.     x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  335.     status("энергии < 5 000 возвращаюсь")
  336.     goTo( 0,0,0,0,1 )
  337.     charging()
  338.     if computer.energy() >= 10000 then
  339.       status("зарядился :)")
  340.       goTo( x,y,z,xd,zd )
  341.       return
  342.     elseif gen.count() >= 10 then
  343.       status("мало энергии, заряжаюсь до 10 000")
  344.       while true do
  345.         os.sleep(10)
  346.         if computer.energy() >= 10000 then
  347.           status("зарядился :)")
  348.           goTo( x,y,z,xd,zd )
  349.           break
  350.         elseif gen.count() == 0 then
  351.           returnHome("закончился уголь")
  352.           break
  353.         end
  354.       end
  355.     else
  356.       returnHome("закончился уголь")
  357.     end
  358.   end
  359. end
  360.  
  361. --движение в сторону side
  362. local function robotMove(side)
  363.   while true do
  364.     local ok,reason = com.robot.move(side)
  365.     if ok then
  366.       return true
  367.     elseif reason and reason == "already moving" then
  368.       os.sleep(0.1)
  369.     else
  370.       return false,reason
  371.     end
  372.   end
  373. end
  374.  
  375. --идём по Х в 0 координат
  376. local function x_forward()
  377.   for xx = 1,width-1 do
  378.     while true do
  379.       if robotMove(3) then
  380.         xPos = xPos - 1
  381.         break
  382.       else
  383.         r.swing()
  384.       end
  385.     end
  386.   end
  387. end
  388.  
  389. --движемся в верх
  390. local function tryUp()
  391.   local tru = 0
  392.   while true do
  393.     if robotMove(1) then
  394.       depth = depth - 1
  395.       break
  396.     else
  397.       r.swingUp()
  398.     end
  399.     tru = tru + 1
  400.     if tru >= 7 then
  401.       tru = 0
  402.       returnHome("не могу подняться",true)
  403.     end
  404.   end
  405. end
  406.  
  407. --обнаружение бедрока
  408. local function checkbedrock()
  409.   for i = 1,3 do
  410.     if r.detectDown() and not r.swingDown() then
  411.       if bedrock then
  412.         break
  413.       elseif i == 3 then
  414.         status("низ достигнут !")
  415.         bedrock = true
  416.         if y ~= nil then
  417.           status("глубина "..y)
  418.         end
  419.       end
  420.     else
  421.       break
  422.     end
  423.   end
  424. end
  425.  
  426. --сачком добываем улик
  427. local function hive(side)
  428.   if slotitem(scoop, {"scoop"}) then
  429.     r.select(scoop)
  430.     i_c.equip()
  431.     r.select(1)
  432.     if side == 3 then
  433.       r.swing()
  434.     elseif side == 1 then
  435.       r.swingUp()
  436.     elseif side == 0 then
  437.       checkbedrock()
  438.     end
  439.     r.select(scoop)
  440.     i_c.equip()
  441.     r.select(1)
  442.   elseif cheststart then
  443.     x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  444.     status("возвращаюсь к сундуку за сачком")
  445.     goTo( 0,0,0,0,-1 )
  446.     if servise(scoop, {"scoop"}) then
  447.       status("взял сачок из сундука")
  448.     else
  449.       returnHome("в сундуке нет сачка")
  450.       return
  451.     end
  452.     goTo( x,y,z,xd,zd )
  453.   else
  454.     returnHome("в инвентаре нет сачка ",true)
  455.   end
  456. end
  457.  
  458. --копаем и движемся вперед
  459. local function digForward()
  460.   if r.detectUp() and not r.swingUp() then
  461.     hive(1)
  462.   end
  463.   if r.detect() and not r.swing() then
  464.     hive(3)
  465.   end
  466.   if r.detectDown() and not r.swingDown() then
  467.     if not bedrock then
  468.       hive(0)
  469.     end
  470.   end
  471.   if r.count(invsize-3) > 0 then
  472.     sortlut()
  473.   end
  474.   local tru,reason = 0
  475.   while true do
  476.     if r.detect() then
  477.       _,reason = r.swing()
  478.     else
  479.       ok,reason = robotMove(3)
  480.       if ok then
  481.         xPos = xPos + xDir
  482.         zPos = zPos + zDir
  483.         break
  484.       end
  485.     end
  486.     tru = tru + 1
  487.     if tru == 4 or tru == 5 then
  488.       if bedrock then
  489.         tryUp()
  490.       end
  491.     elseif tru >= 20 then
  492.       if reason then
  493.         print("причина возврата: "..reason)
  494.       end
  495.       tru = 0
  496.       returnHome("непреодолимое препятствие",true)
  497.     end
  498.   end
  499. end
  500.  
  501. --спускаемся в низ
  502. local function go_down()
  503.   for zz = 1,down do
  504.     while true do
  505.       if r.detectDown() and not r.swingDown() then
  506.         hive(0)
  507.       end
  508.       if robotMove(0) then
  509.         depth = depth + 1
  510.         break
  511.       elseif bedrock then
  512.         while true do
  513.           r.swing()
  514.           if r.detect() then
  515.             tryUp()
  516.           else
  517.             return
  518.           end
  519.         end
  520.       end
  521.     end
  522.   end
  523. end
  524.  
  525. --поворот в лево
  526. local function turnLeft()
  527.   while not r.turnLeft() do
  528.     os.sleep(0.1)
  529.   end
  530.   xDir, zDir = -zDir, xDir
  531. end
  532.  
  533. --поворот в право
  534. local function turnRight()
  535.   while not r.turnRight() do
  536.     os.sleep(0.1)
  537.   end
  538.   xDir, zDir = zDir, -xDir
  539. end
  540.  
  541. --навигация к сундуку
  542. function goTo( x,y,z,xd,zd )
  543.   local Xposition = function()
  544.     if xPos > x then
  545.       while xDir ~= -1 do
  546.         turnLeft()
  547.       end
  548.       while xPos > x do
  549.         if robotMove(3) then
  550.           xPos = xPos - 1
  551.         else
  552.           r.swing()
  553.         end
  554.       end
  555.     elseif xPos < x then
  556.       while xDir ~= 1 do
  557.         turnRight()
  558.       end
  559.       while xPos < x do
  560.         if robotMove(3) then
  561.           xPos = xPos + 1
  562.         else
  563.           r.swing()
  564.         end
  565.       end
  566.     end
  567.   end
  568.   local Zposition = function()
  569.     if zPos > z then
  570.       while zDir ~= -1 do
  571.         turnLeft()
  572.       end
  573.       while zPos > z do
  574.         if robotMove(3) then
  575.           zPos = zPos - 1
  576.         else
  577.           r.swing()
  578.         end
  579.       end
  580.     elseif zPos < z then
  581.       while zDir ~= 1 do
  582.         turnRight()
  583.       end
  584.       while zPos < z do
  585.         if robotMove(3) then
  586.           zPos = zPos + 1
  587.         else
  588.           r.swing()
  589.         end
  590.       end
  591.     end
  592.   end
  593.   r.select(1)
  594.   while depth < y do
  595.     while xDir ~= 0 or zDir ~= 1 do
  596.       turnRight()
  597.     end
  598.     if robotMove(0) then
  599.       depth = depth + 1
  600.     else
  601.       r.swingDown()
  602.     end
  603.   end
  604.   if y > 0 then
  605.     Zposition()
  606.     Xposition()
  607.   else
  608.     Xposition()
  609.     Zposition()
  610.   end
  611.   while depth > y do
  612.     if robotMove(1) then
  613.       depth = depth - 1
  614.     else
  615.       r.swingUp()
  616.     end
  617.   end
  618.   while xDir ~= xd or zDir ~= zd do
  619.     turnRight()
  620.   end
  621. end
  622.  
  623. --копаем
  624. local function dig()
  625.   os.execute("cls")
  626.   print("Без замены инструмента + пчёлы из Extra Bees".."\n")
  627.   print("размер карьера:  ширина "..width.."  длина "..length)
  628.   print("угля в генераторе = "..gen.count())
  629.   print("энергии = "..math.floor(100*computer.energy()/computer.maxEnergy()).." %")
  630.   print("подготовка...")
  631.   goTo( 0,0,0,0,-1 )
  632.   for slot = 1,invsize do
  633.     if r.count(slot) == 0 then
  634.       r.select(slot)
  635.       i_c.equip()
  636.       break
  637.     end
  638.     if slot == invsize then
  639.       stop("в инвентаре робота нет пустых слотов")
  640.     end
  641.   end
  642.   local inv = i_c.getInventorySize(3)
  643.   if inv then
  644.     cheststart = true
  645.     print("доступен сундук")
  646.     if servise(coal, {"coal"}) then
  647.       r.select(coal)
  648.       if gen.insert() then
  649.         print("заправился, угля в генераторе = "..gen.count())
  650.         servise(coal, {"coal"})
  651.       end
  652.     else
  653.       fuelchest = false
  654.     end
  655.     if servise(tools, {"ickaxe"}) then
  656.       r.select(tools)
  657.       i_c.equip()
  658.     else
  659.       stop("нет инструмента")
  660.     end
  661.     servise(scoop, {"scoop"})
  662.   else
  663.     cheststart = false
  664.     print("сундук сзади робота не найден")
  665.     if slotitem(tools, {"ickaxe"}) then
  666.       r.select(tools)
  667.       i_c.equip()
  668.     else
  669.       stop("нет инструмента")
  670.     end
  671.     checkfuel()
  672.     slotitem(scoop, {"scoop"})
  673.   end
  674.   goTo( 0,0,0,0,1 )
  675.   charging()
  676.   r.setLightColor(0xFFFFFF)
  677.   chunkload(true)
  678.   print("опускаюсь на "..down.." блока(ов)")
  679.   for height = 1,256 do
  680.     refuel = true
  681.     checkfuel()
  682.     go_down()
  683.     down = 3
  684.     for xx = 1,width do
  685.       for yy = 1,length-1 do
  686.         digForward()
  687.       end
  688.       checkfuel()
  689.       if xx ~= width then
  690.         if ridht then
  691.           turnRight()
  692.           digForward()
  693.           turnRight()
  694.           ridht = false
  695.         else
  696.           turnLeft()
  697.           digForward()
  698.           turnLeft()
  699.           ridht = true
  700.         end
  701.       end
  702.     end
  703.     r.swingUp()
  704.     r.swingDown()
  705.     if ridht then
  706.       turnLeft()
  707.       x_forward()
  708.       turnLeft()
  709.       ridht = false
  710.     else
  711.       turnRight()
  712.       x_forward()
  713.       turnRight()
  714.       ridht = true
  715.     end
  716.     if bedrock then
  717.       goTo( 0,0,0,0,-1 )
  718.       sortlut(true)
  719.       stop("завершил копать :)")
  720.     end
  721.   end
  722. end
  723.  
  724. dig()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement