Advertisement
Andronio12

GeoRobot By Doob

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