Advertisement
rerere284

geomine.lua edit

Feb 18th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.96 KB | None | 0 0
  1. local node, h_min, h_max = 9, 1.7, 51
  2. local computer, component = require('computer'), require('component')
  3. local IC, g, R = component.inventory_controller, component.geolyzer, component.robot
  4. local x, y, z, d, a_dr, x_dr, z_dr, x1, z1 = 0, 0, 0, nil, 1, 0, 0, 0, 0
  5. local tWorld = {x = {}, y = {}, z = {}}
  6. local GS, IS, tg, p, height, tTest, bedrock, x_f, y_f, z_f, gen, xS, yS, zS, D0, D1, ind, sb, cb, Hm, energyLevel, timer, notBroken = IC.getStackInInternalSlot, R.inventorySize, 0, 1, 64
  7. local tWaste = {
  8.   'cobblestone',
  9.   'sandstone',
  10.   'stone',
  11.   'dirt',
  12.   'grass',
  13.   'gravel',
  14.   'flint',
  15.   'sand',
  16.   'end_stone',
  17.   'hardened_clay',
  18.   'planks',
  19.   'fence',
  20.   'torch',
  21.   'nether_brick',
  22.   'nether_brick_fence',
  23.   'nether_brick_stairs',
  24.   'netherrack',
  25.   'soul_sand',
  26.   'basalt'
  27. }
  28. local compressible = {
  29.   'redstone',
  30.   'coal',
  31.   'dye', --lapis
  32.   'diamond',
  33.   'emerald'
  34. }
  35.  
  36. local function report(msg)
  37.   print(msg)
  38.   if component.isAvailable('tunnel') then
  39.     component.tunnel.send(msg)
  40.   end
  41. end
  42.  
  43. local function compass()
  44.   local tCmps = {{-1, 0}, {0, -1}, {1, 0}, [0] = {0, 1}}
  45.   while not d do
  46.     for c = 0, 3 do
  47.       R.swing(3)
  48.       if g.scan(tCmps[c][1], tCmps[c][2], 0, 1, 1, 1)[1] == 0 and R.place(3) then
  49.         if g.scan(tCmps[c][1], tCmps[c][2], 0, 1, 1, 1)[1] > 0 then
  50.           d = c
  51.           return
  52.         end
  53.       end
  54.     end
  55.     R.turn(true)
  56.   end
  57. end
  58.  
  59. local function delta(xD, yD, zD)
  60.   xS, yS, zS, D0, D1, ind = 0, 0, 0, math.huge, math.huge, 0
  61.   for bl = 1, #tWorld.x do
  62.     xS, yS, zS = tWorld.x[bl], tWorld.y[bl], tWorld.z[bl]
  63.     if xS < xD then xS = xD - xS else xS = xS - xD end
  64.     if yS < yD then yS = yD - yS else yS = yS - yD end
  65.     if zS < zD then zS = zD - zS else zS = zS - zD end
  66.     D0 = xS + yS + zS
  67.     if D0 < D1 then
  68.       D1 = D0
  69.       ind = bl
  70.     end
  71.   end
  72.   return ind
  73. end
  74.  
  75. local tMove = {
  76.   function() x, x1 = x - 1, x1 - 1 end,
  77.   function() z, z1 = z - 1, z1 - 1 end,
  78.   function() x, x1 = x + 1, x1 + 1 end,
  79.   [0] = function() z, z1 = z + 1, z1 + 1 end
  80. }
  81.  
  82. local function move(side)
  83.   R.swing(0)
  84.   sb, cb = R.swing(side)
  85.   if not sb and cb == 'block' then
  86.     notBroken = notBroken + 1
  87.     report('ERROR: UNBREAKABLE BLOCK!')
  88.     recovery("Attempting to continue anyways! " .. notBroken)
  89.   else
  90.     while R.swing(side) do
  91.     end
  92.   end
  93.   if R.move(side) then
  94.     if side == 0 then
  95.       y = y - 1
  96.     elseif side == 1 then
  97.       y = y + 1
  98.     elseif side == 3 then
  99.       tMove[d]()
  100.     end
  101.   end
  102.   if #tWorld.z ~= 0 then
  103.     for m = 1, #tWorld.z do
  104.       if x == tWorld.x[m] and y == tWorld.y[m] and z == tWorld.z[m] then
  105.         table.remove(tWorld.x, m)
  106.         table.remove(tWorld.y, m)
  107.         table.remove(tWorld.z, m)
  108.         break
  109.       end
  110.     end
  111.   end
  112. end
  113.  
  114. local function turn(cc)
  115.   if not cc then
  116.     cc = false
  117.   end
  118.   if R.turn(cc) then
  119.     if cc then
  120.       d = (d + 1) % 4
  121.     else
  122.       d = (d - 1) % 4
  123.     end
  124.   end
  125. end
  126.  
  127. local function spiral(node_t)
  128.   a_dr, x_dr, z_dr = 1, 0, 0
  129.   while true do
  130.     for i = 1, a_dr do
  131.       if a_dr % 2 == 0 then
  132.         x_dr = x_dr + 1
  133.       else
  134.         x_dr = x_dr - 1
  135.       end
  136.       node_t = node_t - 1
  137.       if node_t == 0 then
  138.         return
  139.       end
  140.     end
  141.     for i = 1, a_dr do
  142.       if a_dr % 2 == 0 then
  143.         z_dr = z_dr + 1
  144.       else
  145.         z_dr = z_dr - 1
  146.       end
  147.       node_t = node_t - 1
  148.       if node_t == 0 then
  149.         return
  150.       end
  151.     end
  152.     a_dr = a_dr + 1
  153.   end
  154. end
  155.  
  156. local function sturn(dT)
  157.   while d ~= dT do
  158.     turn((dT - d) % 4 == 1)
  159.   end
  160. end
  161.  
  162. local function gotot(xt, yt, zt)
  163.   while y ~= yt do
  164.     if y < yt then
  165.       move(1)
  166.     elseif y > yt then
  167.       move(0)
  168.     end
  169.   end
  170.   if x < xt and d ~= 3 then
  171.     sturn(3)
  172.   elseif x > xt and d ~= 1 then
  173.     sturn(1)
  174.   end
  175.   while x ~= xt do
  176.     move(3)
  177.   end
  178.   if z < zt and d ~= 0 then
  179.     sturn(0)
  180.   elseif z > zt and d ~= 2 then
  181.     sturn(2)
  182.   end
  183.   while z ~= zt do
  184.     move(3)
  185.   end
  186. end
  187.  
  188. local function scan(sy)
  189.   tTest = g.scan(-x1, -z1, sy, 8, 8, 1)
  190.   p = 1
  191.   for sz = -z1, 7-z1 do
  192.     for sx = -x1, 7-x1 do
  193.       if tTest[p] >= h_min and tTest[p] <= h_max then
  194.         if sy == 0 and sz == z1 and sx == x1 then
  195.         else
  196.           table.insert(tWorld.x, x+sx)
  197.           table.insert(tWorld.y, y+sy)
  198.           table.insert(tWorld.z, z+sz)
  199.         end
  200.       elseif tTest[p] < -0.3 then
  201.         tWorld.x, tWorld.y, tWorld.z = {}, {}, {}
  202.         bedrock = y
  203.         return false
  204.       end
  205.       p = p + 1
  206.     end
  207.   end
  208. end
  209.  
  210. local function fullness()
  211.   local item
  212.   for slot = 1, IS() do
  213.     if R.count(slot) > 0 then
  214.       if not item then
  215.         item = 1
  216.       else
  217.         item = item + 1
  218.       end
  219.     end
  220.   end
  221.   if item then
  222.     return item/IS()
  223.   else
  224.     return 0
  225.   end
  226. end
  227.  
  228. local function clear_table()
  229.   local tCrafting = {1, 2, 3, 5, 6, 7, 9, 10, 11}
  230.   for slot = 1, 9 do
  231.     if R.count(tCrafting[slot]) > 0 then
  232.       R.select(tCrafting[slot])
  233.       for slot1 = 4, IS()-1 do
  234.         if slot1 == 4 or slot1 == 8 or slot1 > 11 then
  235.           R.transferTo(slot1, 64)
  236.         end
  237.       end
  238.     end
  239.     if R.count(tCrafting[slot]) > 0 then
  240.       report("Couldn't clear crafting slot "..slot.."!")
  241.       return false
  242.     end
  243.   end
  244.   return true
  245. end
  246.  
  247. local function packer()
  248.   if component.isAvailable('crafting') then
  249.     local tCrafting, = {1, 2, 3, 5, 6, 7, 9, 10, 11}
  250.     for slot = IS(), 1, -1 do--bunch up stacks of similar items
  251.       for slot1 = 1, slot-1 do--items bunch towards top of inventory
  252.         if R.count(slot) > 0 then
  253.           item = GS(slot)
  254.           item1 = GS(slot1)
  255.           if not item1 or item.name == item1.name and item.maxSize-item.size ~= 0 then
  256.             R.select(slot)
  257.             R.transferTo(slot1, 64)
  258.           end
  259.         end
  260.       end
  261.     end
  262.     for i = 1, #compressible do--for each compressible item type
  263.       if not clear_table() then--clear crafting table
  264.         return false
  265.       end
  266.       for slot = 4, IS() do--go to each slot not on the crafting table
  267.         if slot == 4 or slot == 8 or slot > 11 then
  268.           if R.count(slot) >= 9 then--which is the current item type
  269.             if GS(slot).name == 'minecraft:'..compressible[i] then
  270.               R.select(slot)
  271.               while R.count() > 0 do
  272.                 for slot1 = 1, 9 do--and take 9 of them at a time and place them
  273.                   R.transferTo(tCrafting[slot1], 1)--into the crafting squares
  274.                 end--then craft as many items as are in the crafting grid
  275.               end
  276.               component.crafting.craft(R.count(11))
  277.               --clear the table again just in case
  278.               clear_table()
  279.             end
  280.           end
  281.         end
  282.       end
  283.       --craft in case there are uncrafted items left over
  284.       component.crafting.craft(R.count(11))
  285.       clear_table()
  286.       --somehow
  287.     end
  288.   end
  289.   return true
  290. end
  291.  
  292. local function dropping(cont, exceptions)
  293.   local function isWaste(n)
  294.     for w = 1, #tWaste do
  295.       if n == 'minecraft:'..tWaste[w] then
  296.         return true
  297.       end
  298.     end
  299.   end
  300.   local function isException()
  301.     if not exceptions then
  302.       return
  303.     end
  304.     for w = 1, #exceptions do
  305.       if n == 'minecraft:'..exceptions[w] then
  306.         return true
  307.       end
  308.     end
  309.   end
  310.   local function drop()
  311.     --[[
  312.       don't drop exceptions
  313.       drop waste
  314.       drop other stuff if there is a chest ("cont")
  315.     ]]
  316.     for slot = 1, IS() do
  317.       if R.count(slot) > 0 then
  318.         R.select(slot)
  319.         if not isException(GS(slot).name) then
  320.           if isWaste(GS(slot).name) then
  321.             R.drop(0)
  322.           else
  323.             if cont then
  324.               if not R.drop(3) then
  325.                 report('ERROR: CHEST IS FULL!')
  326.                 while not R.drop(3) do
  327.                   os.sleep(10)
  328.                 end
  329.               end
  330.             end
  331.           end
  332.         else--is exception
  333.           --never drop exceptions
  334.         end
  335.       end
  336.     end
  337.   end
  338.   local s_cont
  339.   if cont then
  340.     for side = 0, 3 do
  341.       if IC.getInventorySize(3) and IC.getInventorySize(3) > 1 then
  342.         s_cont = true
  343.         drop()
  344.         break
  345.       end
  346.       turn()
  347.     end
  348.     if not s_cont then
  349.       report('ERROR: NEED CHEST ADJACENT TO ROBOT!')
  350.       os.sleep(30)
  351.       dropping(true)
  352.     end
  353.   else
  354.     drop()
  355.   end
  356. end
  357.  
  358. local function refuel()
  359.   if component.isAvailable('generator') then
  360.     for slot = 1, IS() do
  361.       R.select(slot)
  362.       if component.generator.insert(64) then
  363.         gen = true
  364.         energyLevel = computer.energy()/computer.maxEnergy()
  365.         report("Refueled! Items in generator: " .. component.generator.count())
  366.         break
  367.       end
  368.     end
  369.     if gen then
  370.       gen = nil
  371.     else
  372.     report("Couldn't refuel!")
  373.     end
  374.   end
  375. end
  376.  
  377. Hm = function()
  378.   gotot(0, -1, 0)
  379.   move(1)
  380.   refuel()
  381.   dropping()
  382.   --if you can't clear the crafting area,
  383.   --try putting everything in the chest
  384.   --except for compressible stuff
  385.   if not clear_table() then
  386.     dropping(true, compressible)
  387.     --if you still can't clear it
  388.     if not clear_table() then
  389.       report ("ERROR: CAN'T CLEAR CRAFTING AREA!")
  390.       --attempt to clear the crafting area until it works
  391.       dropping(true, compressible)
  392.       while not clear_table() do
  393.         dropping(true, compressible)
  394.       end
  395.     end
  396.   end
  397.   packer()
  398.   dropping(true)
  399.   --[[local status = 0
  400.   for side = 0, 3 do
  401.     if IC.getInventorySize(3) and IC.getInventorySize(3) == 1 then
  402.       while status == 0 do
  403.         if R.durability() ~= 1 then
  404.           IC.equip()
  405.           R.drop(3)
  406.           os.sleep(30)
  407.           R.suck(3)
  408.           IC.equip()
  409.         else
  410.           status = 1
  411.         end
  412.       end
  413.       break
  414.     end
  415.     turn()
  416.   end]]
  417.   timer = 18
  418.   while computer.energy()/computer.maxEnergy() < .95 do
  419.     os.sleep(5)
  420.     timer = timer + 1
  421.     if timer > 23 then
  422.       report ("ERROR: CHARGER EMPTY!")
  423.       timer = 0
  424.     end
  425.   end
  426. end
  427.  
  428. local function recovery(rmsg)
  429.   x_f, y_f, z_f = x, y, z
  430.   Hm()
  431.   if rmsg ~= nil then
  432.     report(rmsg)
  433.   end
  434.   move(0)
  435.   gotot(x_f, y_f, z_f)
  436. end
  437.  
  438. local function CL(set)
  439.   if component.isAvailable('chunkloader') then
  440.     component.chunkloader.setActive(set)
  441.   end
  442. end
  443.  
  444. local function round(num, numDecimalPlaces)
  445.   return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
  446. end
  447.  
  448. local function state()
  449.   if component.isAvailable('generator') then
  450.     if component.generator.count() == 0 then
  451.       refuel()
  452.     end
  453.   end
  454.   if fullness() > 0.95 then
  455.     dropping()
  456.     if fullness() > 0.95 then
  457.       report("Inventory full, recovering!")
  458.       recovery("Recovered from full inventory!")
  459.     end
  460.   end
  461.   if R.durability() ~= nil then
  462.     if R.durability() < 0.05 then
  463.       report('ERROR: PICK DYING!')
  464.       Hm()
  465.     end
  466.   end
  467.   if computer.energy()/computer.maxEnergy() < 0.2 then
  468.     energyLevel = computer.energy()/computer.maxEnergy()
  469.     report("Energy low, recovering! Energy at: " .. round(energyLevel*100, 2) .. "%")
  470.     recovery("Recovered from low energy!")
  471.   end
  472. end
  473.  
  474. local tArgs = {...}
  475. if tArgs[1] then
  476.   node = tonumber(tArgs[1])
  477. end
  478. if tArgs[2] then
  479.   height = tonumber(tArgs[2])
  480. end
  481.  
  482. report("Started mining!")
  483. CL(true)
  484. local test_time = computer.uptime()
  485. move(0)
  486. compass()
  487. for n = 1, node do
  488.   while not bedrock do
  489.     scan(-1)
  490.     if #tWorld.x ~= 0 then
  491.       while #tWorld.x ~= 0 do
  492.         tg = delta(x, y, z)
  493.         gotot(tWorld.x[tg], tWorld.y[tg], tWorld.z[tg])
  494.       end
  495.     else
  496.       if not bedrock then
  497.         move(0)
  498.       end
  499.     end
  500.     state()
  501.     if y == height then
  502.       bedrock = y
  503.     end
  504.   end
  505.   state()
  506.   if n ~= node then
  507.     spiral(n)
  508.     gotot(x_dr*8, math.abs(bedrock)+y-1, z_dr*8)
  509.     x1, z1 = 0, 0
  510.     bedrock = nil
  511.   end
  512. end
  513. Hm()
  514. CL(false)
  515. local min, sec = math.modf((computer.uptime()-test_time)/60)
  516. report('Done: '.. min ..' min. '.. math.ceil(sec*60) ..' sec.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement