Advertisement
Link712011

dig cube

Aug 19th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. local move = require('movement')
  2. local energy = require('energy')
  3. local inventory = require('inventory')
  4. local computer = require('computer')
  5. local utilitie = require('utilities')
  6. local net = require('net')
  7.  
  8. local component = require('component')
  9. local robot = require('robot')
  10. local side = require('sides')
  11.  
  12. local arg = {...}
  13.  
  14. local CRITICAL_ENERGY_LEVEL = 10000
  15. local CHECK_FUEL_FREQ = 10
  16. local CHECK_INV_FREQ = 32
  17. local PASTE_URL = 'https://pastebin.com/raw/ztYL1hrP'
  18. local FILEPATH = 'dig'
  19.  
  20.  
  21. local offset = {}
  22. local garbage = {}
  23. local check_fuel_act = CHECK_INV_FREQ
  24. local check_inv_act = 0
  25.  
  26. --[[
  27. ----    INVENTORY
  28. --]]
  29.  
  30. function update()
  31.     print('Updating...')
  32.     if net.get_page_to_file(PASTE_URL, FILEPATH) then
  33.         print('Updated successfully.')
  34.         return true
  35.     end
  36.     print('Failed to update.')
  37.     return false
  38. end
  39.  
  40. function equip_pick()
  41.     inventory.equip('ExtraUtilities:destructionpickaxe')
  42. end
  43.  
  44. function place_chest()
  45.     local i
  46.  
  47.     i = 0
  48.     if not inventory.select_item('EnderStorage:enderChest', 3003) then
  49.         return false
  50.     end
  51.     robot.swing()
  52.     while not robot.place() do
  53.         move.move(1, side.down, true)
  54.         robot.swing()
  55.         i = i + 1
  56.     end
  57.     return i
  58. end
  59.  
  60. function get_inventory_map()
  61.     local i
  62.     local size
  63.     local inv_map = {}
  64.     local tmp
  65.  
  66.     size = robot.inventorySize()
  67.     i = 1
  68.     while i <= size do
  69.         tmp = component.inventory_controller.getStackInInternalSlot(i)
  70.         if not tmp then
  71.             return false
  72.         end
  73.         inv_map[#inv_map + 1] = tmp
  74.         i = i + 1
  75.     end
  76.     return inv_map
  77. end
  78.  
  79. function drop_all(inv_map)
  80.     local i
  81.     local tmp_offset
  82.  
  83.     i = 1
  84.     tmp_offset = place_chest()
  85.     if not tmp_offset then
  86.         print('error cant place chest')
  87.         return false
  88.     end
  89.     while i <= #inv_map do
  90.         robot.select(i)
  91.         if utilitie.is_elem_in_list(garbage, inv_map[i].name) then
  92.             robot.dropDown()
  93.         elseif inv_map[i].name ~= 'ExtraUtilities:destructionpickaxe' then
  94.             robot.drop()
  95.         end
  96.         i = i + 1
  97.     end
  98.     robot.swing()
  99.     move.move(tmp_offset, side.up, true)
  100. end
  101.  
  102. function check_inventory()
  103.     local inv_map
  104.  
  105.     if check_inv_act > CHECK_INV_FREQ then
  106.         inv_map = get_inventory_map()
  107.         if inv_map then
  108.             drop_all(inv_map)
  109.         end
  110.         check_inv_act = 0
  111.     else
  112.         check_inv_act = check_inv_act + 1
  113.     end
  114. end
  115.  
  116. --[[
  117. ----    FUEL
  118. --]]
  119.  
  120. function check_fuel()
  121.     if check_fuel_act > CHECK_FUEL_FREQ then
  122.         if computer.energy() < CRITICAL_ENERGY_LEVEL then
  123.             go_refuel()
  124.         end
  125.         check_fuel_act = 0
  126.     else
  127.         check_fuel_act = check_fuel_act + 1
  128.     end
  129. end
  130.  
  131. function go_refuel()
  132.     go_mining_to_refuel()
  133.     energy.wait_charging()
  134.     go_refuel_to_mining(sav)
  135. end
  136.  
  137. function go_mining_to_refuel()
  138.     if offset.side == side.left then
  139.         move.move(offset.z, side.right, true)
  140.     else
  141.         move.move(offset.z, side.left, true)
  142.     end
  143.     move.move(offset.y, side.down, true)
  144.     move.move(offset.x, side.back, true)
  145. end
  146.  
  147. function go_refuel_to_mining()
  148.     move.move(offset.x, side.front, true)
  149.     move.move(offset.y, side.up, true)
  150.     move.move(offset.z, offset.side, true)
  151. end
  152.  
  153. --[[
  154. ----    MINE
  155. --]]
  156.  
  157. function column(maxy, offset_bkp)
  158.     while (offset_bkp == 0 and maxy > offset.y) or (offset_bkp > 0 and offset.y > 0) do
  159.         if offset_bkp == 0 then
  160.             move.move(1, side.up, true)
  161.             offset.y = offset.y + 1
  162.         else
  163.             move.move(1, side.down, true)
  164.             offset.y = offset.y - 1
  165.         end
  166.         check_fuel()
  167.         check_inventory()
  168.     end
  169. end
  170.  
  171. function slice(maxx, maxy, offset_bkp)
  172.     column(maxy, offset.y)
  173.     while (offset_bkp == 0 and maxx > offset.x) or (offset_bkp > 0 and offset.x > 0) do
  174.         if offset_bkp == 0 then
  175.             move.move(1, side.front, true)
  176.             offset.x = offset.x + 1
  177.         else
  178.             move.move(1, side.back, true)
  179.             offset.x = offset.x - 1
  180.         end
  181.         column(maxy, offset.y)
  182.         check_fuel()
  183.         check_inventory()
  184.     end
  185. end
  186.  
  187. function cube(maxx, maxy, maxz)
  188.     local turn_left
  189.  
  190.     turn_left = false
  191.     if offset.side == side.left then
  192.         turn_left = true
  193.     end
  194.     while maxz > offset.z do
  195.         slice(maxx, maxy, offset.x)
  196.         move.move(1, offset.side, true)
  197.         offset.z = offset.z + 1
  198.         check_fuel()
  199.         check_inventory()
  200.     end
  201.     if offset.y > 0 then
  202.         move.move(offset.y, side.down, true)
  203.     end
  204. end
  205.  
  206. --[[
  207. ----    MAIN
  208. --]]
  209.  
  210. function init()
  211.     local dir = {}
  212.  
  213.     dir['left'] = side.left
  214.     dir['right'] = side.right
  215.     offset.x = 0
  216.     offset.y = 0
  217.     offset.z = 0
  218.     offset.side = dir[arg[4]]
  219.  
  220.     garbage[#garbage + 1] = 'minecraft:cobblestone'
  221.     garbage[#garbage + 1] = 'minecraft:stone'
  222.     garbage[#garbage + 1] = 'chisel:concrete'
  223.     garbage[#garbage + 1] = 'minecraft:gravel'
  224. end
  225.  
  226. function core()
  227.     init()
  228.     cube(tonumber(arg[1]), tonumber(arg[2]), tonumber(arg[3]))
  229. end
  230.  
  231. if arg[1] and (arg[1] == '-u' or arg[1] == '--update') then
  232.     return update()
  233. end
  234.  
  235. if not arg[1] or not arg[2] or not arg[3] or (tonumber(arg[3]) > 1 and (not arg[4] or (arg[4] ~= 'left' and arg[4] ~= 'right'))) then
  236.     print('./dig x y z direction')
  237.     return false
  238. end
  239. core()
  240. return true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement