Advertisement
artem211

OC_Robot_API

Jul 2nd, 2015
2,607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 39.87 KB | None | 0 0
  1.  
  2.  
  3. local APIS={}
  4. local inv_size = 16
  5. local inv_side = require("sides").back
  6. local charge_side = require("sides").left
  7. local battery_side = require("sides").bottom
  8. local temp_state = {x=0, y=0, z=0, dr=3}
  9. local lc = {x=0, y=0, z=0, dr=3, xMax = 0, zMax = 0}
  10. local way = 0
  11. local ore_count = 0
  12. local directives = {
  13.     pause = false,
  14.     home = false,
  15.     report = false,
  16.     move = false,
  17.     read_rep = false
  18. }
  19. local scrap = {
  20.     "minecraft:stone",
  21.     "minecraft:cobblestone",
  22.     "minecraft:netherrack",
  23.     "minecraft:dirt",
  24.     "minecraft:gravel",
  25.     "minecraft:sand",
  26.     "minecraft:grass",
  27.     "minecraft:sandstone",
  28.     "minecraft:mossy_cobblestone",
  29.     "minecraft:stonebrick",
  30.     "minecraft:brown_mushroom",
  31.     "minecraft:red_mushroom"
  32.     }
  33. local fuel_list = {
  34.   "minecraft:fence",
  35.   "minecraft:planks",
  36.   "minecraft:log",
  37.   "minecraft:coal_block",
  38.   "minecraft:coal"
  39. }    
  40. local mining_tools_list = {
  41.   "minecraft:iron_pickaxe",
  42.   "minecraft:golden_pickaxe",
  43.   "appliedenergistics2:item.ToolCertusQuartzPickaxe",
  44.   "appliedenergistics2:item.ToolNetherQuartzPickaxe",
  45.   "IC2:itemToolBronzePickaxe",
  46.   "Forestry:bronzePickaxe",
  47.   "minecraft:diamond_pickaxe",
  48.   "IC2:itemToolDrill",
  49.   "IC2:itemToolDDrill",
  50.   "IC2:itemToolIridiumDrill",
  51.   "GraviSuite:advDDrill"
  52. }
  53.  
  54.  
  55. function APIS.duster(tmr)
  56.  local inv = require("component").inventory_controller
  57.  local r = require("robot")
  58.  while true do
  59.   for i=1,7 do
  60.    local temp = inv.getStackInSlot(3,i)
  61.    if temp then
  62.     if  temp.name == "IC2:itemDustSmall" and math.floor(temp.size/9) > 0 then
  63.      for j=1,3 do
  64.       r.select(j)
  65.       inv.suckFromSlot(3,i,math.floor(temp.size/9))
  66.      end
  67.      for j=5,7 do
  68.       r.select(j)
  69.       inv.suckFromSlot(3,i,math.floor(temp.size/9))
  70.      end
  71.      for j=9,11 do
  72.       r.select(j)
  73.       inv.suckFromSlot(3,i,math.floor(temp.size/9))
  74.      end
  75.      r.select(4)
  76.      require("component").crafting.craft(math.floor(temp.size/9))
  77.      r.drop()
  78.     end
  79.    end
  80.   end
  81.   os.sleep(tmr)
  82.  end
  83. end
  84. function APIS.charge(charge_side)
  85. --функция зарядки робота от чарджера, т.к. в этом билде ОС он категорически не принимает
  86. --ред сигнал робота, временно переориентировал на рычаг.
  87. comp = require("computer")
  88. while comp.energy() < (comp.maxEnergy()/2) do
  89. --local rs = require("component").redstone
  90. --rs.setOutput(charge_side, 15)
  91. require("term").clear()
  92. APIS.use(charge_side)
  93. print("Зарядка...")
  94. os.sleep(60)
  95. --rs.setOutput(charge_side, 0)
  96. APIS.use(charge_side)
  97. end
  98. print("Батарея заряжена")
  99. return true
  100. end
  101. function APIS.charge_tool(chargerSide, slot)
  102. --функция проверки заряда бура в руках робота, передача его на зарядку в батбокс/чарджер
  103.     local inv = require("component").inventory_controller
  104.     local r = require("robot")    
  105.     local side = 3
  106.     local tool = nil
  107.     if chargerSide == 1 then
  108.         side=1
  109.     elseif chargerSide == 0 then
  110.         side=0
  111.     end
  112.     if r.durability() == nil then
  113.         return false
  114.     end
  115.     if r.durability() < 0.3 then
  116.         r.select(slot)
  117.         inv.equip()
  118.         tool = inv.getStackInInternalSlot(slot)
  119.         inv.equip()
  120.         if not(lc.x == 0 and lc.y == 0 and lc.z == 0) then
  121.             return true
  122.         end
  123.     else
  124.         return false
  125.     end
  126.  
  127.     local function isElectric(device)
  128.         if device.maxCharge ~= nil then
  129.             return true
  130.         else
  131.             return false
  132.         end
  133.     end
  134.     local function find_new_tool()
  135.         APIS.rot(inv_side)
  136.         local temp = APIS.inv_scaner(mining_tools_list, false, start_slot)
  137.         print("Поиск замены инструменту в сундуке.")
  138.         while temp ~= 0 do
  139.             local temp_device = inv.getStackInSlot(3, temp)
  140.             if isElectric(temp_device) then
  141.                 if temp_device.charge/temp_device.maxCharge > 0.6 then
  142.                     break
  143.                 else
  144.                     temp = APIS.inv_scaner(mining_tools_list, false, temp+1)
  145.                 end
  146.             else
  147.                 if temp_device.damage/temp_device.maxDamage < 0.4 then
  148.                     break
  149.                 else
  150.                     temp = APIS.inv_scaner(mining_tools_list, false, temp+1)
  151.                 end
  152.             end
  153.         end
  154.         return temp
  155.     end
  156.     local function service(device)
  157.         if isElectric(device) then
  158.             APIS.rot(chargerSide)  
  159.             if not inv.getInventorySize(3) ~= nil then
  160.                 print("Зарядник не найден. Установите зарядник.")
  161.                 while not inv.getInventorySize(3) ~= nil do
  162.                     os.sleep(5)
  163.                 end
  164.             end
  165.             r.select(slot)
  166.             inv.equip()
  167.             inv.dropIntoSlot(3,1)
  168.             print("Зарядка инструмента.")
  169.             while inv.getStackInSlot(3,1).charge < device.maxCharge do
  170.                 os.sleep(10)
  171.             end
  172.             inv.suckFromSlot(3,1)
  173.             inv.equip()            
  174.         else
  175.             print("Поиск инструмента в сундуке.")
  176.             APIS.rot(inv_side)
  177.             while true do
  178.                 local temp = find_new_tool()
  179.                 if temp ~= 0 then
  180.                     r.select(slot)
  181.                     inv.equip()
  182.                     if not r.drop() then
  183.                         print("Нет места в сундуке. Освободите место.")
  184.                         while not r.drop() do
  185.                             os.sleep(10)
  186.                         end
  187.                     end
  188.                     inv.suckFromSlot(3, temp)
  189.                     inv.equip()
  190.                     r.select(1)
  191.                     break
  192.                 end
  193.             end
  194.         end
  195.     end
  196.  
  197.  
  198.     if lc.x == 0 and lc.y == 0 and lc.z == 0 then
  199.         print("Сервис инструмента.")
  200.         service(tool)
  201.     else
  202.         return false
  203.     end  
  204. end
  205. function APIS.use(s)
  206. --функция юза в заданную сторону.
  207. local r = require("robot")
  208. if s == 1 then r.useUp()
  209. elseif s == 2 then
  210. r.turnAround()
  211. r.use()
  212. r.turnAround()
  213. elseif s == 3 then r.use()
  214. elseif s == 4 then
  215. r.turnRight()
  216. r.use()
  217. r.turnLeft()
  218. elseif s==5 then
  219. r.turnLeft()
  220. r.use()
  221. r.turnRight()
  222. else
  223. r.useDown()
  224. end
  225. end
  226. function APIS.drop()
  227. --функция дропа всего инвентаря в сундук, если таковой стоит перед носом
  228.  local inv = require("component").inventory_controller
  229.  local r= require("robot")
  230.  while true do
  231.   if r.detect() then
  232.    if inv.getInventorySize(3) ~= nil then
  233.     for i=1,inv_size-1 do
  234.      if inv.getStackInInternalSlot(i) ~= nil then
  235.       r.select(i)
  236.       if not r.drop() then
  237. --       print("Сундук переполнен. Освободите место под складирование.")
  238.        while not r.drop() do
  239.         os.sleep(5)
  240.        end
  241.       end
  242.      end
  243.     end
  244.     break
  245.    else
  246.     print("Блок не является сундуком.")
  247.     os.sleep(5)
  248.    end
  249.   else
  250.    print("Установите сундук!")
  251.    os.sleep(5)
  252.   end
  253.  end
  254. end
  255. function APIS.printState()
  256. --используется для отладки движений робота
  257. print("Статус:")
  258. print(lc.x, lc.y, lc.z, lc.dr)
  259. print(temp_state.x, temp_state.y, temp_state.z, temp_state.dr)
  260. end
  261. function APIS.harvest(count)
  262. --функция для фермера на IC кропсах, аргумент - сколько раз собирать с 1 места
  263.  local r=require("robot")
  264.  for i=1,count do
  265.   r.useDown()
  266.  end
  267. end
  268. function APIS.dig(arg)
  269. --функция целевого копания по стороне-аргументу, скорее всего потру ее, при оптимизации
  270.  r = require("robot")
  271.  event = require("event")
  272.  if arg == 3 then
  273.   r.swing()
  274.  elseif arg == 1 then
  275.   while r.detectUp() do
  276.    r.swingUp()
  277.   end
  278.  elseif arg == 0 then
  279.   r.swingDown()
  280.  end
  281. end
  282. function APIS.isScrap(name)
  283.  --проверка является ли итем по имени мусором.
  284.  for i, nm in pairs(scrap) do
  285.   if name == nm then  
  286.    return true
  287.   end  
  288.  end
  289.  return false
  290. end
  291. function APIS.ore_analyze(arg)
  292.  --метод проверки, является ли блок "немусором"
  293.  if arg ~= nil then
  294.   if require("component").isAvailable("geolyzer") then
  295.    local lyz = require("component").geolyzer
  296.    if APIS.isScrap(lyz.analyze(arg).name) then
  297.     return false
  298.    else
  299.     return true
  300.    end
  301.   else
  302.    print("Геолайзер не обнаружен в системе.")
  303.    return false
  304.   end
  305.  else
  306.   print("Не указан аргумент(сторона проверки).")
  307.  end
  308. end
  309. function APIS.dig_ore()
  310. --метод для копания только руды, в слоеном карьере.
  311.  local r=require("robot")
  312.  if r.detectUp() then
  313.   if APIS.ore_analyze(1) then
  314.    APIS.dig(1)
  315.   end
  316.  end
  317.  if r.detectDown() then
  318.   if APIS.ore_analyze(0) then
  319.    APIS.dig(0)
  320.   end
  321.  end
  322. end
  323. function APIS.turnLeft()
  324. local r = require("robot")
  325. r.turnLeft()
  326. if lc.dr == 3 then
  327. lc.dr = 5
  328. elseif lc.dr == 4 then
  329. lc.dr = 3
  330. elseif lc.dr == 2 then
  331. lc.dr = 4
  332. elseif lc.dr == 5 then
  333. lc.dr = 2
  334. end
  335. end
  336. function APIS.turnRight()
  337. local r = require("robot")
  338. r.turnRight()
  339. if lc.dr == 3 then
  340. lc.dr = 4
  341. elseif lc.dr == 4 then
  342. lc.dr = 2
  343. elseif lc.dr == 2 then
  344. lc.dr = 5
  345. elseif lc.dr == 5 then
  346. lc.dr = 3
  347. end
  348. end
  349. function APIS.turnAround()
  350.  local r = require("robot")
  351.     r.turnAround()
  352.     if lc.dr == 3 then
  353.         lc.dr = 2
  354.     elseif lc.dr == 4 then
  355.         lc.dr = 5
  356.     elseif lc.dr == 2 then
  357.         lc.dr = 3
  358.     elseif lc.dr == 5 then
  359.         lc.dr = 4
  360.     end
  361. end
  362. function APIS.rot(side)
  363.  if (side ~= 1) and (side ~= 0) and lc.dr-side ~=0 then
  364.   local sides = require("sides")
  365.   local r = require("robot")
  366.   if lc.dr == 3 then
  367.     if side == 4 then APIS.turnRight()
  368.     elseif side == 2 then APIS.turnAround()
  369.     elseif side == 5 then APIS.turnLeft()
  370.     end
  371.   elseif lc.dr == 4 then
  372.     if side == 2 then APIS.turnRight()
  373.     elseif side == 5 then APIS.turnAround()
  374.     elseif side == 3 then APIS.turnLeft()
  375.     end
  376.   elseif lc.dr == 2 then
  377.     if side == 5 then APIS.turnRight()
  378.     elseif side == 3 then APIS.turnAround()
  379.     elseif side == 4 then APIS.turnLeft()
  380.     end
  381.   else--5
  382.     if side == 3 then APIS.turnRight()
  383.     elseif side == 4 then APIS.turnAround()
  384.     elseif side == 2 then APIS.turnLeft()
  385.     end
  386.   end
  387.  end
  388.  --APIS.printState()
  389. end
  390. function APIS.mUp()
  391. --функция движения вверх, сквозь породу.
  392.  local r = require("robot")
  393.  local try = 1
  394.  repeat
  395.   r.swingUp()
  396.   try = try + 1
  397.   if try >= 15 then
  398.    print("Препятствие у точки: x="..lc.x.." z="..lc.z.." y="..lc.y.." Направление Dr=вверх"..lc.dr)
  399.    local _, det = require("robot").detectUp()
  400.    print(det.." сверху.")
  401.    print(require("component").geolyzer.analyze(1).name)
  402.    APIS.mTo(lc.x+2,lc.y,lc.z)  
  403.    APIS.back_to_the_future()  
  404.    --APIS.printState()
  405.    return false
  406.   elseif try > 12 then
  407.    if require("component").geolyzer.analyze(1).name == "ExtraBees:hive" then
  408.     local inv = require("component").inventory_controller
  409.     if pcall(r.select,APIS.inv_scaner("Forestry:scoop", true)) then
  410.      inv.equip()
  411.      r.swingUp()
  412.      inv.equip()
  413.      r.select(1)
  414.     end
  415.    end
  416.   end
  417.  until not r.detectUp()
  418.  while try < 15 and not r.up() do
  419.   r.swingUp()
  420.   try = try + 1
  421.  end
  422.  if try < 15 then
  423.   lc.y = lc.y + 1
  424.   way = way + 1
  425. --APIS.printState()
  426.   return true
  427.  end
  428. end
  429. function APIS.mDown(action, arg)
  430. --функция движения вниз, сквозь породу.
  431.  local r = require("robot")
  432.  local event = require("event")
  433.  local try = 1
  434.  if action ~= nil then
  435.   action(arg)
  436.  end
  437.  repeat
  438.   r.swingDown()
  439.   try = try + 1
  440.   if try >= 15 then
  441.    --APIS.printState()
  442.    print("Препятствие у точки: x="..lc.x.." z="..lc.z.." y="..lc.y.." Направление Dr= вниз"..lc.dr)
  443.    local _, det = require("robot").detectDown()
  444.    print(det.." снизу.")
  445.    print(require("component").geolyzer.analyze(0).name)
  446.    APIS.back_to_the_future()
  447.    return false
  448.   elseif try > 12 then
  449.    if require("component").geolyzer.analyze(0).name == "ExtraBees:hive" then
  450.     local inv = require("component").inventory_controller
  451.     if pcall(r.select,APIS.inv_scaner("Forestry:scoop", true)) then
  452.      inv.equip()
  453.      r.swingDown()
  454.      inv.equip()
  455.      r.select(1)
  456.     end
  457.    end
  458.   end
  459.  until not r.detectDown()
  460.  while try < 15 and not r.down() do
  461.   r.swingDown()
  462.   try = try + 1
  463.  end
  464.  
  465.  if try < 15 then
  466.   lc.y = lc.y - 1
  467.   way = way + 1
  468.   return true
  469.  end
  470. end
  471. function APIS.mForw(action,arg)
  472. --шаг вперед с действием или безБ сквозь породу, координатные отметки
  473.  local r = require("robot")
  474.  local event = require("event")
  475.  local try = 1
  476.  local sides = require("sides")
  477.  if action ~= nil then
  478.   action(arg)
  479.  end
  480.  repeat
  481.   r.swing()
  482.   try = try + 1
  483.   if try >= 15 then
  484.    print("Препятствие у точки: x="..lc.x.." z="..lc.z.." y="..lc.y.." Направление Dr="..lc.dr)
  485.    local _, det = require("robot").detect()
  486.    print(det.." спереди.")
  487.    print(require("component").geolyzer.analyze(3).name)
  488.    APIS.back_to_the_future()
  489.    return false
  490.   elseif try > 12 then
  491.    if require("component").geolyzer.analyze(3).name == "ExtraBees:hive" then
  492.     local inv = require("component").inventory_controller
  493.     if pcall(r.select,APIS.inv_scaner("Forestry:scoop", true)) then
  494.      inv.equip()
  495.      r.swing()
  496.      inv.equip()
  497.      r.select(1)
  498.     end
  499.    end
  500.   end
  501.  until not r.detect()
  502.  while try < 15 and not r.forward() do
  503.   r.swing()
  504.   try = try + 1
  505.  end
  506.  if try < 15 then
  507.   way = way + 1
  508.  if lc.dr==2 then
  509.   lc.x = lc.x - 1
  510.  elseif lc.dr==3 then
  511.   lc.x = lc.x + 1
  512.  elseif lc.dr==4 then
  513.   lc.z = lc.z + 1
  514.  elseif lc.dr==5 then
  515.   lc.z = lc.z - 1
  516.  end  
  517.  --APIS.printState()
  518.  return true
  519.  end
  520. end
  521. function APIS.mTo(x, y, z, action, arg)
  522. --движение в заданную точку с заданым действием и аргументом действия. Координатное.
  523.     if directives.pause then
  524.         APIS.execDirective()
  525.     end
  526. local r = require("robot")
  527. local sides = require("sides")
  528.  if lc.x > x then
  529.   APIS.rot(sides.back)
  530.   while lc.x > x do
  531.    APIS.mForw(action,arg)
  532.   end
  533.  end
  534.  if lc.x < x then
  535.   APIS.rot(sides.forward)
  536.   while lc.x < x do
  537.    APIS.mForw(action,arg)
  538.   end
  539.  end
  540.  if lc.z > z then
  541.   APIS.rot(sides.left)
  542.   while lc.z > z do
  543.    APIS.mForw(action,arg)
  544.   end  
  545.  end
  546.  if lc.z < z then
  547.   APIS.rot(sides.right)
  548.   while lc.z < z do
  549.    APIS.mForw(action,arg)
  550.   end
  551.  end
  552.  while lc.y > y do
  553.   APIS.mDown(action,arg)
  554.   --print("едем вниз")
  555.  end
  556.  while lc.y < y do
  557.   APIS.mUp(action,arg)
  558.   --print("едем вверх")
  559.  end
  560. end
  561. function APIS.plane(x, y, z, action, arg, forward)
  562. --проходка горизонтальной плоскости, координатная. Слоеный карьер, фермер.
  563.  local dir = "forward"  
  564.  if forward then
  565.   for j=0,z do
  566.    if dir == "forward" then
  567.     for i=0,x do
  568.      APIS.mTo(i,y,j,action,arg)
  569.     end
  570.     APIS.check_state()
  571.     dir = "back"
  572.    else
  573.     for i=x,0,-1 do
  574.      APIS.mTo(i,y,j,action,arg)
  575.     end
  576.     APIS.check_state()
  577.     dir = "forward"
  578.    end
  579.   end
  580.  else
  581.  if z%2==0 then dir = "back" end
  582.   for j=z,0,-1 do
  583.    if dir == "forward" then
  584.     for i=0,x do
  585.      APIS.mTo(i,y,j,action,arg)
  586.     end
  587.     APIS.check_state()
  588.     dir = "back"
  589.    else
  590.     for i=x,0,-1 do
  591.      APIS.mTo(i,y,j,action,arg)
  592.     end
  593.     APIS.check_state()
  594.     dir = "forward"
  595.    end
  596.   end
  597.  end
  598. end
  599. function APIS.home(action,arg)
  600.  --функция возврата к сундуку для очистки инвентаря.
  601.  
  602.  temp_state.x = lc.x
  603.  temp_state.y = lc.y
  604.  temp_state.z = lc.z
  605.  temp_state.dr = lc.dr
  606.  APIS.mTo(0,0,0, action,arg)
  607.  APIS.rot(2)
  608.  APIS.drop_scrap()
  609.  APIS.drop()
  610.  --APIS.rot(3)
  611. end
  612. function APIS.drop_scrap()
  613.  local r = require("robot")
  614.  local inv = require("component").inventory_controller
  615.   for i=1, inv_size do  
  616.    local slot = inv.getStackInInternalSlot(i)
  617.    if slot ~= nil then  
  618.     if APIS.isScrap(slot.name) then
  619.      r.select(i)
  620.      r.dropDown()
  621.     end
  622.    end
  623.   end
  624.   return APIS.inv_sorting()  
  625. end
  626. function APIS.back_to_mine(action,arg)
  627.  --возврат на точку останова(после очистки инвентаря) для возобновления работы.
  628.     APIS.mTo(0,temp_state.y,0, action,arg)
  629.     APIS.mTo(temp_state.x,temp_state.y,temp_state.z, action,arg)
  630.     APIS.rot(temp_state.dr)
  631.     temp_state.x = 0
  632.     temp_state.y = 0
  633.     temp_state.z = 0
  634.     temp_state.dr = 3
  635. end
  636. function APIS.back_to_the_future()
  637.  --Завершение программы ввиду непреодолимого препятствия.
  638.  print("Ошибка/препятствие. Возврат робота.")
  639.  APIS.mTo(lc.x, 0, lc.z)
  640.  APIS.home()
  641.  APIS.rot(3)
  642.  print("Робот на исходной. Завершение программы.")
  643.  os.exit()
  644. end
  645. function APIS.check_inv()
  646. --вычисление величины собственного инвентаря
  647.  local r = require("robot")
  648.  local i = 16
  649.  local count = 16
  650.  while pcall(r.count, i) do
  651.   count = i
  652.   i=i+1
  653.  end
  654.  return count
  655. end
  656. function APIS.inv_sorting()
  657.  --Функция сортировки инвентаря(перекладывание содержимого без пустых ячеек)
  658.  --возвращает процентное заполнение инвентаря в целом
  659.  local inv = require("component").inventory_controller
  660.  local r = require("robot")
  661.  local items_stored = 0
  662.  inv_size = APIS.check_inv()
  663.  --цикл от начала до конца ищет пустые ячейки, когда находит - второй цикл от конца до
  664.  --начала ищет не пустые и перемещает на место пустых(упорядочивает)
  665.  --когда цикл начальный и конечный пересекается - перестает запускаться цикл с конца
  666.  --чтоб не было бесконечного перемешивания. После подсчет % занятого пространства и возврат значений.
  667.  for i=1, inv_size-1 do
  668.   if r.count(i) == 0 then
  669.    for j=inv_size-1, 1, -1 do
  670.     if r.count(j) > 0 then
  671.      if j<i then
  672.      break
  673.      end
  674.      r.select(j)
  675.      r.transferTo(i)
  676.      break
  677.     end
  678.    end
  679.   end
  680.  end
  681.  for i=1,inv_size do
  682.   if r.count(i) > 0 then
  683.    items_stored = items_stored + 1
  684.   end
  685.  end
  686.  --print(items_stored)
  687.  r.select(1)
  688.  return items_stored/inv_size
  689. end
  690. function APIS.check_state()
  691.  --функция проверки состояния инвентаря и заряда батареи робота
  692.  --выбрасывание мусора из карманов и пожирание угля, если требуется
  693.  local r = require("robot")
  694.  local inv = require("component").inventory_controller
  695.  local comp = require("computer")
  696.  local c = require("component")
  697.  local need_fuel = comp.energy() < (comp.maxEnergy()*0.90)
  698.  
  699.  local function inventory()
  700.   local need_to_home = false
  701.   if (r.count(inv_size-2) > 0) then  
  702.     if APIS.drop_scrap() > 0.9 then
  703.      need_to_home = true
  704.     else
  705.      need_to_home = false
  706.     end
  707.   end
  708.   return need_to_home
  709.  end
  710.  local function fuel(internal)
  711.   local need_to_home = false
  712.   if need_fuel and c.isAvailable("generator") and c.generator.count() == 0 then
  713.     local slt = APIS.inv_scaner(fuel_list, internal)
  714.     if slt ~= 0 and not internal then
  715.      r.select(inv_size-2)
  716.      inv.suckFromSlot(3, slt)
  717.      c.generator.insert()
  718.      r.select(1)
  719.      need_to_home = false
  720.     else
  721.      slt = APIS.inv_scaner(fuel_list, internal)
  722.      if slt ~= 0 and internal then  
  723.       r.select(slt)
  724.       c.generator.insert()
  725.       r.select(1)
  726.       need_to_home = false
  727.      else
  728.       need_to_home = true
  729.      end
  730.     end
  731.   elseif not c.isAvailable("generator") and need_fuel then
  732.    need_to_home = true
  733.   end
  734.   return need_to_home
  735.  end
  736.  
  737.     if  inventory() or fuel(true) or APIS.charge_tool(charge_side, inv_size-2) then
  738.         APIS.home()
  739.         os.sleep(15)
  740.         APIS.charge_tool(charge_side, inv_size-2)  
  741.         fuel(false)
  742.         APIS.back_to_mine()
  743.     end    
  744. end
  745. function APIS.quarry(x, z)
  746. --слоистый геокарьер, копает слои 1 через 2, проверяет все блоки, берет только руду.
  747.  inv_size = APIS.check_inv()
  748.  for i=0, -256, -3 do  
  749.   if i%2 == 0 then  
  750.    APIS.plane((x - 1),i,(z - 1), APIS.dig_ore, 3, true)  
  751.   else  
  752.    APIS.plane((x - 1),i,(z - 1), APIS.dig_ore, 3, false)  
  753.   end  
  754.  end
  755.  APIS.home()
  756. end
  757. function APIS.field(x, z, time)
  758. --фермер для кропсов, длинна, ширина, таймер повторения в минутах(время созревания)
  759. --нуждается в модернизации, автоматизированной настройке таймера повторений.
  760.     if time == nil then time = 5 end
  761.  while true do
  762.   APIS.plane(x-1, 0, z-1, APIS.harvest, 2, true)
  763.   APIS.use(require("sides").down)
  764.   APIS.use(require("sides").down)
  765.   APIS.home()
  766.   APIS.rot(3)
  767.   require("term").clear()
  768.   print("Ожидание созревания...", time, "минут.")
  769.   os.sleep(60*time)
  770.  end
  771. end
  772. function APIS.chop_single()
  773. --сырая автолесорубка для 1 дерева, пассивная, магнит апгрейд необходим. Рабочая.
  774.  local inv = require("component").inventory_controller
  775.  local beam = require("component").tractor_beam
  776.  local r = require("robot")    
  777.  local function start()
  778.   APIS.rot(inv_side)
  779.   APIS.drop()
  780.   inv.suckFromSlot(3, APIS.inv_scaner("minecraft:sapling"), 1)        
  781.   APIS.rot(3)
  782.   r.place()
  783.  end
  784.  start()
  785.  while true do
  786.   require("term").clear()        
  787.   print("Лесоруб, одиночное дерево. Ожидание роста саженца.")
  788.   local _, wood = r.detect()
  789.   if wood == "solid" then
  790.    print("Рубка дерева.")            
  791.    r.swing()
  792.    os.sleep(5)
  793.    for i=1, 8 do
  794.     beam.suck()
  795.    end
  796.    start()        
  797.    require("term").clear()
  798.    APIS.charge(charge_side)
  799.    APIS.charge_tool(battery_side)
  800.   end
  801.   os.sleep(10)
  802.  end
  803. end
  804. function APIS.inv_scaner(filter, internal, start_slot)
  805. --автопоисковик заданного итема в своем инвентаре по системному имени.
  806. --возвращает номер ячейки итема, первого найденного от начала ивентаря.
  807.     local inv = require("component").inventory_controller
  808.     ins = inv.getInventorySize(3)
  809.     if start_slot == nil then
  810.         start_slot = 1
  811.     end
  812.     if filter == "empty" then
  813.         if internal then
  814.             for i=start_slot, inv_size do
  815.                 if inv.getStackInInternalSlot(i) == nil then
  816.                     return i
  817.                 end
  818.             end
  819.         else
  820.             for i=start_slot, inv.getInventorySize(3) do
  821.                 if inv.getStackInSlot(3, i) == nil then
  822.                     return i
  823.                 end
  824.             end
  825.         end
  826.     end  
  827.     if internal then
  828.         for i=start_slot, inv_size do
  829.             if inv.getStackInInternalSlot(i) ~= nil then
  830.                 if pcall(pairs, filter) then
  831.                     for j, name in pairs(filter) do
  832.                         if inv.getStackInInternalSlot(i).name == name then
  833.                             --print(filter.." найден!")
  834.                             return i
  835.                         end
  836.                     end
  837.                 else
  838.                     if inv.getStackInInternalSlot(i).name == filter then
  839.                         --print(filter.." найден!")
  840.                         return i
  841.                     end
  842.                 end
  843.             end
  844.         end
  845.         --print("Найти "..filter.." не удалось.")
  846.         return 0  
  847.     else
  848.         if ins ~= nil  then
  849.             for i=start_slot, ins do
  850.                 if inv.getStackInSlot(3, i) ~= nil then
  851.                     if pcall(pairs, filter) then
  852.                         for j, name in pairs(filter) do
  853.                             if inv.getStackInSlot(3, i).name == name then
  854.                                 --print(filter.." найден!")
  855.                                 return i
  856.                             end
  857.                         end
  858.                     else
  859.                         if inv.getStackInSlot(3, i).name == filter then
  860.                             --print(filter.." найден!")
  861.                             return i
  862.                         end
  863.                     end
  864.                 end
  865.             end
  866.             --print("Найти "..filter.." не удалось.")
  867.             return 0
  868.         else
  869.             return 0
  870.         end
  871.     end
  872. end
  873. function APIS.distance(blockA, blockB)
  874.  local dist = math.sqrt(math.pow(blockA.x - blockB.x,2) +
  875.    math.pow(blockA.z - blockB.z,2) + math.pow(blockA.y - blockB.y,2))
  876.  return dist
  877. end
  878. function APIS.closest_point(point, points)
  879.  local cl_num = 1
  880.  local length = APIS.distance(point, points[1])
  881.  for i=1, #points do
  882.   local l = APIS.distance(point, points[i])
  883.   if l < length then
  884.    cl_num = i
  885.    length = l
  886.   end
  887.  end
  888.  return cl_num
  889. end
  890. function APIS.waypoints(ores_table, last)
  891.  local way_table = {}
  892.  local count = #ores_table
  893.  table.insert(way_table, {x=lc.x, z=lc.z, y=lc.y})
  894.  while count ~= #way_table - 1 do
  895.   local j = APIS.closest_point(way_table[#way_table], ores_table)
  896.   table.insert(way_table, ores_table[j])
  897.   table.remove(ores_table, j)
  898.  end
  899.  return way_table, last
  900. end
  901. function APIS.scanVolume(xn,zn, bedrock, side, hight_border)
  902.  --сканирование карьерного "этажа" заданного радиуса -10 блоков вниз+сканер+10 блоков вверх
  903.  local geo = require("component").geolyzer
  904.  local ores_table = {}
  905.  local last = false
  906.  local x_limit = 0
  907.  local z_limit = 0
  908.  local x_increment = 1
  909.  local z_increment = 1
  910.  
  911.  if side == "north" or side == "север" then
  912.   x_limit = zn
  913.   x_increment = 1
  914.   z_limit = -xn
  915.   z_increment = -1
  916.  -- print("Выбранное направление: Север. Старт сканирования.")
  917.  elseif side == "west" or side == "запад" then
  918.   x_limit = -xn
  919.   x_increment = -1
  920.   z_limit = -zn
  921.   z_increment = -1
  922.  -- print("Выбранное направление: Запад. Старт сканирования.")
  923.  elseif side == "south" or side == "юг" then
  924.   x_limit = -zn
  925.   x_increment = -1
  926.   z_limit = xn
  927.   z_increment = 1
  928.  -- print("Выбранное направление: Юг. Старт сканирования.")
  929.  elseif side == "east" or side == "восток" or side == nil then
  930.   x_limit = xn
  931.   x_increment = 1
  932.   z_limit = zn
  933.   z_increment = 1
  934.  -- print("Выбранное направление: Запад. сканирования.")
  935.  end
  936.  
  937.  if xn == 0 and zn == 0 then
  938.     --print("Сканирование границ бедрока")
  939.   local tempr=geo.scan(0,0)
  940.   for i=10, 32 do
  941.    if tempr[i] < -0.3 then
  942.     --print("Бедрок обнаружен на уровне "..i-33+lc.y)
  943.     return i-33+lc.y, true
  944.    end
  945.   end
  946.   return 0, false
  947.  end
  948.  --print("Сканирование заданного объема, может занять несколько минут...")
  949.  for xt=0,x_limit,x_increment do
  950.   for zt=0,z_limit,z_increment do
  951.    local scan = geo.scan(xt,zt,true)
  952.    for yt=hight_border+33, 33+math.abs(hight_border) do
  953.     if scan[yt] > 2.05 and scan[yt] < 40 and ((yt-33)+lc.y) > bedrock then
  954.       if side == "north" or side == "север" then
  955.        table.insert(ores_table, {x=math.abs(zt)+lc.x, z=math.abs(xt)+lc.z, y=(yt-33)+lc.y})
  956.       elseif side == "west" or side == "запад" then
  957.        table.insert(ores_table, {x=math.abs(xt)+lc.x, z=math.abs(zt)+lc.z, y=(yt-33)+lc.y})
  958.       elseif side == "south" or side == "юг" then
  959.        table.insert(ores_table, {x=math.abs(zt)+lc.x, z=math.abs(xt)+lc.z, y=(yt-33)+lc.y})
  960.       elseif side == "east" or side == "восток" or side == nil then
  961.        table.insert(ores_table, {x=math.abs(xt)+lc.x, z=math.abs(zt)+lc.z, y=(yt-33)+lc.y})
  962.       end    
  963.     end
  964.    end
  965.   end
  966.  end
  967.  return ores_table
  968. end
  969. function APIS.whatsSide()
  970.  local geo = require("component").geolyzer
  971.  local r= require("robot")
  972.  local function isBlock(dens)
  973.   if dens ~= nil and dens ~= 0 then
  974.    return 1
  975.   elseif dens == 0 then
  976.    return 0
  977.   end
  978.  end
  979.  local function check(fig, front)
  980.   local figure1 = {
  981.    east = isBlock(geo.scan(1,0)[33]),
  982.    south = isBlock(geo.scan(0,1)[33]),
  983.    west = isBlock(geo.scan(-1,0)[33]),  
  984.    north = isBlock(geo.scan(0,-1)[33])
  985.    }  
  986.   if front then
  987.    if fig.east > figure1.east  then
  988.     return "east"
  989.    elseif fig.south > figure1.south then
  990.     return "south"
  991.    elseif fig.west > figure1.west then
  992.     return "west"
  993.    elseif fig.north > figure1.north then
  994.     return "north"
  995.    end
  996.   elseif not front then
  997.    if fig.east < figure1.east  then
  998.     return "east"
  999.    elseif fig.south < figure1.south then
  1000.     return "south"
  1001.    elseif fig.west < figure1.west then
  1002.     return "west"
  1003.    elseif fig.north < figure1.north then
  1004.     return "north"
  1005.    end  
  1006.   end
  1007.  end
  1008.  local figure = {
  1009.    east = isBlock(geo.scan(1,0)[33]),
  1010.    south = isBlock(geo.scan(0,1)[33]),
  1011.    west = isBlock(geo.scan(-1,0)[33]),  
  1012.    north = isBlock(geo.scan(0,-1)[33])
  1013.    }
  1014.  APIS.rot(3)
  1015.  while true do
  1016.   if r.detect() then
  1017.    r.swing()
  1018.    local direction = check(figure, true)
  1019.    r.place()
  1020.    return direction
  1021.   elseif r.detectDown() then
  1022.    r.swingDown()
  1023.    r.place()
  1024.    local direction = check(figure, false)
  1025.    r.swing()
  1026.    r.placeDown()
  1027.    return direction
  1028.   end
  1029.   print("Для ориентирования в пространстве недостаточно данных.")
  1030.   print("Пожалуйста установите любой блок перед или под роботом и повторите попытку")
  1031.   os.exit()
  1032.  end
  1033. end
  1034. function APIS.findoutBedrockLevel()
  1035.  local bedrock = -1
  1036.  local start_level = lc.y
  1037.  for i = lc.y, -256, -10 do
  1038.   local bed, catch = APIS.scanVolume(0,0)
  1039.   --print("Сканирование бедрока")
  1040.   if not catch then
  1041.    APIS.mTo(lc.x, i, lc.z)
  1042.   elseif bed < lc.y - 5 then
  1043.    --print("Найден бедрок")
  1044.    APIS.mTo(lc.x, i-4, lc.z)
  1045.    bedrock = APIS.scanVolume(0,0)
  1046.    break
  1047.   end
  1048.  end
  1049.  APIS.mTo(lc.x, start_level, lc.z)
  1050.  return bedrock
  1051. end
  1052. function APIS.clusterDigger(start_point, x,z, bedrock, side)
  1053.     --print("Кластер диггер")
  1054.     if side == nil then
  1055.         --print("В функцию кластер диггер сайд не поступила")
  1056.         side = APIS.whatsSide()
  1057.         --print("Направление: "..side)
  1058.     end
  1059.     if start_point == nil then
  1060.         start_point = lc
  1061.     end
  1062.     inv_size = APIS.check_inv()  
  1063.     APIS.mTo(start_point.x, start_point.y, start_point.z)
  1064.     for Y=lc.y, bedrock+6, -1 do
  1065.         if Y%21 == 0 or Y == bedrock + 9 then
  1066.             APIS.mTo(start_point.x, Y, start_point.z)
  1067.             APIS.rot(3)
  1068.             arr=APIS.waypoints(APIS.scanVolume(x-1,z-1, bedrock+4, side, -11))
  1069.             ore_count = ore_count + (#arr-1)
  1070.             for i=1, #arr do
  1071.                 APIS.mTo(arr[i].x, arr[i].y, arr[i].z)
  1072.                 if i%10 == 0 then
  1073.                     APIS.check_state()
  1074.                 end
  1075.                 if way%30 == 0 then
  1076.                     APIS.check_state()
  1077.                 end
  1078.             end
  1079.         end
  1080.     end
  1081. end
  1082. function APIS.geoMiner(x, z, bedrock, side, x_lim, z_lim)
  1083.  local start_point = {x=0, y=0, z=0}
  1084.  local x_limit = x_lim
  1085.  local z_limit = z_lim
  1086.  if x_lim == nil then
  1087.   x_limit = 32
  1088.  end
  1089.  if z_lim == nil then
  1090.   z_limit = 32
  1091.  end
  1092.  if side == nil then
  1093.   --print("В функцию геомайнер сайд не поступила.")
  1094.   side = APIS.whatsSide()
  1095.  end
  1096.  
  1097.  local function z_glide()
  1098.  
  1099.   if z/z_limit < 1 then
  1100.    APIS.clusterDigger(start_point, x_limit, z, bedrock, side)
  1101.   else
  1102.    for j=1, math.floor(z/z_limit) do
  1103.     --print("Карьер заданных лимитов по Z")
  1104.     APIS.clusterDigger(start_point, x_limit, z_limit, bedrock, side)
  1105.     start_point.z = j*z_limit+1
  1106.    end
  1107.    if z%z_limit > 0 then
  1108.     --print("Карьер остатков по Z")
  1109.     APIS.clusterDigger(start_point, x_limit, z%z_limit, bedrock, side)
  1110.    end
  1111.    start_point.z = 0
  1112.   end
  1113.  end
  1114.  
  1115.  if x/x_limit < 1 then
  1116.   local temp = x_limit
  1117.   x_limit = x
  1118.   z_glide()
  1119.   x_limit = temp
  1120.  else
  1121.   for i=1, math.floor(x/x_limit) do
  1122.    --print("Карьер заданных лимитов по X")
  1123.    z_glide()
  1124.    start_point.x = i*x_limit+1
  1125.   end
  1126.   if x%x_limit > 0 then
  1127.    x_limit = x%x_limit
  1128.    --print("Карьер остатков по x")
  1129.    z_glide()
  1130.   end
  1131.  end
  1132.  APIS.home()
  1133.  APIS.rot(3)
  1134.  print("Итого руды добыто: "..ore_count)
  1135.  print("Всего блоков пройдено: "..way)
  1136.  ore_count = 0
  1137. end
  1138. function APIS.mine(x,z, bedrock, side,x_lim, z_lim)
  1139.     APIS.check_components()
  1140.     lc.xMax = x
  1141.     lc.zMax = z
  1142.     way = 0
  1143.     local term = require("term")
  1144.     local r = require("robot")
  1145.     local comp = require("component")
  1146.     term.clear()
  1147.     print("Старт карьера: "..x.."x"..z.." блоков.")
  1148.     APIS.check_state()
  1149.     local side = APIS.whatsSide()
  1150.  
  1151.     if bedrock == nil then
  1152.         print("Проверка уровня бедрока. Вертикальная шахта до дна и обратно.")
  1153.         bedrock = APIS.findoutBedrockLevel()
  1154.     end
  1155.     print("Сканирование заданного объема, может занять несколько минут...")
  1156.     if not pcall(APIS.geoMiner, x, z, bedrock, side, x_lim, z_lim) then
  1157.         pcall(APIS.back_to_the_future)
  1158.     end
  1159. end
  1160. function APIS.check_components()
  1161.     local c = require("component")
  1162.     local r = require("robot")
  1163.     inv_size = APIS.check_inv()
  1164.     if c.isAvailable("chunkloader") then
  1165.         c.chunkloader.setActive(true)
  1166.         print("\t Чанклоадер....доступен.")
  1167.     else
  1168.         print("\t Чанклоадер не обнаружен, возможны проблемы и ошибки. Принудительная остановка программы.")
  1169. --        os.exit()
  1170.     end
  1171.     if c.isAvailable("inventory_controller") then
  1172.         print("\t Контроллер инвентаря....доступен.")
  1173.     else
  1174.         print("\t Контроллер инвентаря не обнаружен, возможны проблемы и ошибки. Принудительная остановка программы.")
  1175.         os.exit()
  1176.     end
  1177.     if c.isAvailable("generator") then
  1178.         print("\t Генератор....доступен.")
  1179.     else
  1180.         print("\t Генератор не обнаружен, возможны проблемы и ошибки. Принудительная остановка программы.")
  1181.         os.exit()
  1182.     end
  1183.     if c.isAvailable("geolyzer") then
  1184.         print("\t Геосканер....доступен.")
  1185.     else
  1186.         print("\t Геосканер не обнаружен, возможны проблемы и ошибки. Принудительная остановка программы.")
  1187.         os.exit()
  1188.     end
  1189. --    if pcall(r.select,APIS.inv_scaner("Forestry:scoop", true)) then
  1190. --        r.transferTo(inv_size)
  1191. --        r.select(1)
  1192. --        print("\t Сачек....доступен.")
  1193. --    else
  1194. --        print("\t Сачек не обнаружен, возможны проблемы и ошибки. Принудительная остановка программы.")
  1195. --        os.exit()
  1196. --    end
  1197.     if r.durability() ~= nil then
  1198.         print("\t Инструмент....доступен.")
  1199.     else
  1200.         print("\t Инструмент не обнаружен, возможны проблемы и ошибки. Принудительная остановка программы.")
  1201.         os.exit()
  1202.     end
  1203.     print("\n Все компоненты в наличии.\n Программа может быть запущена.")
  1204. end
  1205. function APIS.ore_map(lc, ore_array, hight_border)
  1206.  --отрисовка карты руд в отсканированном роботом объеме
  1207.  local hol = require("component").hologram
  1208.  hol.clear()
  1209.  hol.setTranslation(0,0.3,0)
  1210.  hol.setScale(1.2)
  1211.  hol.set(lc.x+8, lc.y+math.abs(hight_border)*2+1, lc.z+8, 03)
  1212.  for i=1, #ore_array do  
  1213.   hol.set(ore_array[i].x+8, ore_array[i].y+21, ore_array[i].z+8, 01)
  1214.  end  
  1215.  require("term").clear()
  1216.  print("Руды найдено: "..#ore_array)
  1217. end
  1218. function APIS.getTunnelMessage(label, internal_adress, sender_adress, port, distance, text, power)
  1219.     local event = require("event")
  1220.     local ser = require("serialization")
  1221.     local cmpn = require("component")
  1222.     local command = ""
  1223.     local coords = {}
  1224.     event.listen("modem_message", APIS.getTunnelMessage)
  1225.     if cmpn.isAvailable("robot") then    
  1226.         if text ~= nil then
  1227.             event.ignore("modem_message", APIS.getTunnelMessage)
  1228.             command = string.sub(text, 1,4)
  1229.             if #text > 4 then
  1230.                 text = string.sub(text, 5, #text)
  1231.                 text = ser.unserialize(text)
  1232.             end
  1233.             APIS.exec(command)
  1234.             event.listen("modem_message", APIS.getTunnelMessage)
  1235.         end
  1236.     elseif false then
  1237.        
  1238.     end
  1239. end
  1240. function APIS.pushCommand(command)
  1241.     local tunnel = require("component").tunnel
  1242.     tunnel.send(tostring(command))
  1243. end
  1244. function APIS.pushReport()
  1245.     local ser = require("serialization")
  1246.     local message = "answ"
  1247.     message = message..ser.serialize({
  1248.         energy = require("computer").energy(),
  1249.         tool = require("robot").durability(),
  1250.         way=way,
  1251.         ore_count=ore_count,
  1252.         lc = lc
  1253.     })
  1254.     --require("component").tunnel.send(message)
  1255.     return message
  1256. end
  1257. function APIS.exec(command)
  1258.     if command ~= nil then
  1259.         if command == "back" or command == "stop" or "home" then
  1260.             directives.pause = true
  1261.             directives.home = true
  1262.         elseif command == "repo" or command == "resp" then
  1263.             directives.pause = true
  1264.             directives.report = true
  1265.         elseif command == "move" then
  1266.             directives.pause = true
  1267.             directives.move = true
  1268.         elseif command =="answ" then
  1269.             directives.pause = true
  1270.             directives.read_rep = true
  1271.         end
  1272.     end
  1273. end
  1274. function APIS.execDirective()
  1275.     if directives.home then
  1276.         directives.pause = false
  1277.         directives.home = false
  1278.         APIS.home()
  1279.         APIS.delay("comm")
  1280.     elseif directives.report then
  1281.         directives.pause = false
  1282.         directives.report = false
  1283.         APIS.pushReport()
  1284.     elseif directives.move then
  1285.         directives.pause = false
  1286.         directives.move = false
  1287.         APIS.mTo(temp_state.x, temp_state.y, temp_state.z)
  1288.         temp_state = {x=0, y=0, z=0, dr=3}        
  1289.     end
  1290. end
  1291. function APIS.delay(ask)
  1292.     if ask == "comm" then
  1293.         print("Ожидание команды: ")
  1294.         pcall(APIS.exec(tostring(io.read())))
  1295.         return
  1296.     elseif ask == "stop" or ask == "exit" or ask == "break" then
  1297.         os.exit()
  1298.     end    
  1299. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement