Advertisement
meigrafd

OpenComputers - GeoMine

Jun 16th, 2017
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.19 KB | None | 0 0
  1. --[[
  2.  
  3. https://github.com/OpenPrograms/OpenPrograms.ru/tree/master/geomine
  4.  
  5. --]]
  6.  
  7. local node, h_min, h_max = 9, 1.7, 51
  8. local computer, component = require('computer'), require('component')
  9. local IC, g, R = component.inventory_controller, component.geolyzer, component.robot
  10. local x, y, z, d, a_dr, x_dr, z_dr, x1, z1 = 0, 0, 0, nil, 1, 0, 0, 0, 0
  11. local tWorld = {x = {}, y = {}, z = {}}
  12. local GS, IS, tg, p, height, tTest, bedrock, x_f, y_f, z_f, gen, xS, yS, zS, D0, D1, ind, sb, cb, Hm = IC.getStackInInternalSlot, R.inventorySize, 0, 1, 64
  13. local tWaste = {
  14.   'cobblestone',
  15.   'sandstone',
  16.   'stone',
  17.   'dirt',
  18.   'grass',
  19.   'gravel',
  20.   'sand',
  21.   'end_stone',
  22.   'hardened_clay',
  23.   'mossy_cobblestone',
  24.   'planks',
  25.   'fence',
  26.   'torch',
  27.   'nether_brick',
  28.   'nether_brick_fence',
  29.   'nether_brick_stairs',
  30.   'netherrack',
  31.   'soul_sand'
  32. }
  33.  
  34. local function report(msg)
  35.   print(msg)
  36.   if component.isAvailable('tunnel') then
  37.     component.tunnel.send(msg)
  38.   end
  39. end
  40.  
  41. local function compass()
  42.   local tCmps = {{-1, 0}, {0, -1}, {1, 0}, [0] = {0, 1}}
  43.   while not d do
  44.     for c = 0, 3 do
  45.       R.swing(3)
  46.       if g.scan(tCmps[c][1], tCmps[c][2], 0, 1, 1, 1)[1] == 0 and R.place(3) then
  47.         if g.scan(tCmps[c][1], tCmps[c][2], 0, 1, 1, 1)[1] > 0 then
  48.           d = c
  49.           return
  50.         end
  51.       end
  52.     end
  53.     R.turn(true)
  54.   end
  55. end
  56.  
  57. local function delta(xD, yD, zD)
  58.   xS, yS, zS, D0, D1, ind = 0, 0, 0, math.huge, math.huge, 0
  59.   for bl = 1, #tWorld.x do
  60.     xS, yS, zS = tWorld.x[bl], tWorld.y[bl], tWorld.z[bl]
  61.     if xS < xD then xS = xD - xS else xS = xS - xD end
  62.     if yS < yD then yS = yD - yS else yS = yS - yD end
  63.     if zS < zD then zS = zD - zS else zS = zS - zD end
  64.     D0 = xS + yS + zS
  65.     if D0 < D1 then
  66.       D1 = D0
  67.       ind = bl
  68.     end
  69.   end
  70.   return ind
  71. end
  72.  
  73. local tMove = {
  74.   function() x, x1 = x - 1, x1 - 1 end,
  75.   function() z, z1 = z - 1, z1 - 1 end,
  76.   function() x, x1 = x + 1, x1 + 1 end,
  77.   [0] = function() z, z1 = z + 1, z1 + 1 end
  78. }
  79.  
  80. local function move(side)
  81.   R.swing(0)
  82.   sb, cb = R.swing(side)
  83.   if not sb and cb == 'block' then
  84.     tWorld.x, tWorld.y, tWorld.z = {}, {}, {}
  85.     move(1)
  86.     report('ERROR: PZ!')
  87.     Hm()
  88.   else
  89.     while R.swing(side) do
  90.     end
  91.   end
  92.   if R.move(side) then
  93.     if side == 0 then
  94.       y = y - 1
  95.     elseif side == 1 then
  96.       y = y + 1
  97.     elseif side == 3 then
  98.       tMove[d]()
  99.     end
  100.   end
  101.   if #tWorld.z ~= 0 then
  102.     for m = 1, #tWorld.z do
  103.       if x == tWorld.x[m] and y == tWorld.y[m] and z == tWorld.z[m] then
  104.         table.remove(tWorld.x, m)
  105.         table.remove(tWorld.y, m)
  106.         table.remove(tWorld.z, m)
  107.         break
  108.       end
  109.     end
  110.   end
  111. end
  112.  
  113. local function turn(cc)
  114.   if not cc then
  115.     cc = false
  116.   end
  117.   if R.turn(cc) then
  118.     if cc then
  119.       d = (d + 1) % 4
  120.     else
  121.       d = (d - 1) % 4
  122.     end
  123.   end
  124. end
  125.  
  126. local function spiral(node_t)
  127.   a_dr, x_dr, z_dr = 1, 0, 0
  128.   while true do
  129.     for i = 1, a_dr do
  130.       if a_dr % 2 == 0 then
  131.         x_dr = x_dr + 1
  132.       else
  133.         x_dr = x_dr - 1
  134.       end
  135.       node_t = node_t - 1
  136.       if node_t == 0 then
  137.         return
  138.       end
  139.     end
  140.     for i = 1, a_dr do
  141.       if a_dr % 2 == 0 then
  142.         z_dr = z_dr + 1
  143.       else
  144.         z_dr = z_dr - 1
  145.       end
  146.       node_t = node_t - 1
  147.       if node_t == 0 then
  148.         return
  149.       end
  150.     end
  151.     a_dr = a_dr + 1
  152.   end
  153. end
  154.  
  155. local function sturn(dT)
  156.   while d ~= dT do
  157.     turn((dT - d) % 4 == 1)
  158.   end
  159. end
  160.  
  161. local function gotot(xt, yt, zt)
  162.   while y ~= yt do
  163.     if y < yt then
  164.       move(1)
  165.     elseif y > yt then
  166.       move(0)
  167.     end
  168.   end
  169.   if x < xt and d ~= 3 then
  170.     sturn(3)
  171.   elseif x > xt and d ~= 1 then
  172.     sturn(1)
  173.   end
  174.   while x ~= xt do
  175.     move(3)
  176.   end
  177.   if z < zt and d ~= 0 then
  178.     sturn(0)
  179.   elseif z > zt and d ~= 2 then
  180.     sturn(2)
  181.   end
  182.   while z ~= zt do
  183.     move(3)
  184.   end
  185. end
  186.  
  187. local function scan(sy)
  188.   tTest = g.scan(-x1, -z1, sy, 8, 8, 1)
  189.   p = 1
  190.   for sz = -z1, 7-z1 do
  191.     for sx = -x1, 7-x1 do
  192.       if tTest[p] >= h_min and tTest[p] <= h_max then
  193.         if sy == 0 and sz == z1 and sx == x1 then
  194.         else
  195.           table.insert(tWorld.x, x+sx)
  196.           table.insert(tWorld.y, y+sy)
  197.           table.insert(tWorld.z, z+sz)
  198.         end
  199.       elseif tTest[p] < -0.3 then
  200.         tWorld.x, tWorld.y, tWorld.z = {}, {}, {}
  201.         bedrock = y
  202.         return false
  203.       end
  204.       p = p + 1
  205.     end
  206.   end
  207. end
  208.  
  209. local function fullness()
  210.   local item
  211.   for slot = 1, IS() do
  212.     if R.count(slot) > 0 then
  213.       if not item then
  214.         item = GS(slot).size
  215.       else
  216.         item = item + GS(slot).size
  217.       end
  218.     end
  219.   end
  220.   if item then
  221.     return item/(IS()*64)
  222.   else
  223.     return 0
  224.   end
  225. end
  226.  
  227. local function packer()
  228.   if component.isAvailable('crafting') then
  229.     local tCrafting, tBlocks = {1, 2, 3, 5, 6, 7, 9, 10, 11}, {'redstone', 'coal', 'dye', 'diamond', 'emerald'}
  230.     local function clear_table()
  231.       for slot = 1, 9 do
  232.         if R.count(tCrafting[slot]) > 0 then
  233.           R.select(tCrafting[slot])
  234.           for slot1 = 4, IS()-1 do
  235.             if slot1 == 4 or slot1 == 8 or slot1 > 11 then
  236.               R.transferTo(slot1, 64)
  237.             end
  238.           end
  239.         end
  240.       end
  241.     end
  242.     for slot = IS(), 1, -1 do
  243.       for slot1 = 1, slot-1 do
  244.         if R.count(slot) > 0 then
  245.           item = GS(slot)
  246.           item1 = GS(slot1)
  247.           if not item1 or item.name == item1.name and item.maxSize-item.size ~= 0 then
  248.             R.select(slot)
  249.             R.transferTo(slot1, 64)
  250.           end
  251.         end
  252.       end
  253.     end
  254.     for i = 1, #tBlocks do
  255.       clear_table()
  256.       for slot = 4, IS() do
  257.         if slot == 4 or slot == 8 or slot > 11 then
  258.           if R.count(slot) >= 9 then
  259.             if GS(slot).name == 'minecraft:'..tBlocks[i] then
  260.               R.select(slot)
  261.               while R.count() > 0 do
  262.                 for slot1 = 1, 9 do
  263.                   R.transferTo(tCrafting[slot1], 1)
  264.                 end
  265.               end
  266.             end
  267.           end
  268.         end
  269.       end
  270.       component.crafting.craft(64)
  271.     end
  272.   end
  273. end
  274.  
  275. local function dropping(cont)
  276.   local function isWaste(n)
  277.     for w = 1, #tWaste do
  278.       if n == 'minecraft:'..tWaste[w] then
  279.         return true
  280.       end
  281.     end
  282.   end
  283.   local function drop()
  284.     for slot = 1, IS() do
  285.       if R.count(slot) > 0 then
  286.         R.select(slot)
  287.         if isWaste(GS(slot).name) then
  288.           R.drop(0)
  289.         else
  290.           if cont then
  291.             if not R.drop(3) then
  292.               report('ERROR: SPACE?')
  293.               while not R.drop(3) do
  294.                 os.sleep(10)
  295.               end
  296.             end
  297.           end
  298.         end
  299.       end
  300.     end
  301.   end
  302.   local s_cont
  303.   if cont then
  304.     for side = 0, 3 do
  305.       if IC.getInventorySize(3) and IC.getInventorySize(3) > 1 then
  306.         s_cont = true
  307.         drop()
  308.         break
  309.       end
  310.       turn()
  311.     end
  312.     if not s_cont then
  313.       report('ERROR: CHEST?!')
  314.       os.sleep(30)
  315.       dropping(true)
  316.     end
  317.   else
  318.     drop()
  319.   end
  320. end
  321.  
  322. Hm = function()
  323.   gotot(0, -1, 0)
  324.   move(1)
  325.   packer()
  326.   dropping(true)
  327.   local status = 0
  328.   for side = 0, 3 do
  329.     if IC.getInventorySize(3) and IC.getInventorySize(3) == 1 then
  330.       while status == 0 do
  331.         if R.durability() ~= 1 then
  332.           IC.equip()
  333.           R.drop(3)
  334.           os.sleep(30)
  335.           R.suck(3)
  336.           IC.equip()
  337.         else
  338.           status = 1
  339.         end
  340.       end
  341.       break
  342.     end
  343.     turn()
  344.   end
  345. end
  346.  
  347. local function recovery()
  348.   x_f, y_f, z_f = x, y, z
  349.   Hm()
  350.   move(0)
  351.   gotot(x_f, y_f, z_f)
  352. end
  353.  
  354. local function CL(set)
  355.   if component.isAvailable('chunkloader') then
  356.     component.chunkloader.setActive(set)
  357.   end
  358. end
  359.  
  360. local function state()
  361.   if fullness() > 0.95 then
  362.     dropping()
  363.     packer()
  364.     if fullness() > 0.95 then
  365.       recovery()
  366.     end
  367.   end
  368.   if R.durability() < 0.1 then
  369.     recovery()
  370.   end
  371.   if computer.energy()/computer.maxEnergy() < 0.2 then
  372.     if component.isAvailable('generator') then
  373.       for slot = 1, IS() do
  374.         if component.generator.insert(64) then
  375.           gen = true
  376.           os.sleep(30)
  377.           break
  378.         end
  379.       end
  380.       if gen then
  381.         gen = nil
  382.       else
  383.         recovery()
  384.       end
  385.     else
  386.       recovery()
  387.     end
  388.   end
  389. end
  390.  
  391. local tArgs = {...}
  392. if tArgs[1] then
  393.   node = tonumber(tArgs[1])
  394. end
  395. if tArgs[2] then
  396.   height = tonumber(tArgs[2])
  397. end
  398.  
  399. CL(true)
  400. local test_time = computer.uptime()
  401. move(0)
  402. compass()
  403. for n = 1, node do
  404.   while not bedrock do
  405.     scan(-1)
  406.     if #tWorld.x ~= 0 then
  407.       while #tWorld.x ~= 0 do
  408.         tg = delta(x, y, z)
  409.         gotot(tWorld.x[tg], tWorld.y[tg], tWorld.z[tg])
  410.       end
  411.     else
  412.       if not bedrock then
  413.         move(0)
  414.       end
  415.     end
  416.     state()
  417.     if y == height then
  418.       bedrock = y
  419.     end
  420.   end
  421.   state()
  422.   if n ~= node then
  423.     spiral(n)
  424.     gotot(x_dr*8, math.abs(bedrock)+y-1, z_dr*8)
  425.     x1, z1 = 0, 0
  426.     bedrock = nil
  427.   end
  428. end
  429. Hm()
  430. CL(false)
  431. local min, sec = math.modf((computer.uptime()-test_time)/60)
  432. report('Done: '.. min ..' min. '.. math.ceil(sec*60) ..' sec.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement