Link712011

Mine opti v2.0.1.3

Jan 5th, 2017
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 35.67 KB | None | 0 0
  1. local build = 49
  2. --[[
  3.         Seriously, who cares of headers ?
  4.         License ? Well, consider that script is on MIT license and do whateaver you want with it. I don't care.
  5.                                     - Link712011
  6. ]]--
  7.  
  8.  
  9. local version = "2.0.1.4"
  10. local robot = require("robot")
  11. local side = require("sides")
  12. local component = require("component")
  13. local shell = require("shell")
  14. local computer = require("computer")
  15.  
  16. local config_filename = "dmp_whitelist.conf"
  17. local stockpile_filename = "stock_whitelist.conf"
  18. local last_changelog=""
  19. local offset = { x = 0, y = 0, z = 0 }
  20. local errno = "unknown error"
  21. local minerals = {}
  22. local diamond_requirered_blocs = {}
  23. local stockpile = {}
  24. local shovel_block = {}
  25. local essentials = {}
  26. local search_update = true
  27. local ender_chest_damage_fuel = 1092 -- yellow yellow yellow
  28. local ender_chest_damage_log = 3822 -- red red red
  29. local ender_chest_damage_dump = 3003 -- blue blue blue
  30. local arg = {...}
  31. local inventory_checker_counter = 0
  32.  
  33.  
  34. if arg and arg[1] and (arg[1] == "-v" or arg[1] == "--version") then
  35.     print(tostring(version))
  36.     os.exit()
  37. end
  38. if arg and arg[1] and (arg[1] == "-b" or arg[1] == "--build") then
  39.     print(tostring(version))
  40.     os.exit()
  41. end
  42. if arg and arg[1] and (arg[1] == "-nu" or arg[1] == "--no-update") then
  43.     search_update = false
  44. end
  45.  
  46. function init_shovel_blocs()
  47.     table.insert(shovel_block, "minecraft:dirt")
  48.     table.insert(shovel_block, "minecraft:gravel")
  49.     table.insert(shovel_block, "minecraft:grass")
  50.     table.insert(shovel_block, "minecraft:sand")
  51.     table.insert(shovel_block, "minecraft:planks")
  52. end
  53. function init_essentials()
  54.     table.insert(essentials, "minecraft:stone_pickaxe")
  55.     table.insert(essentials, "minecraft:diamond_pickaxe")
  56.     table.insert(essentials, "minecraft:coal")
  57.     table.insert(essentials, "minecraft:log")
  58.     table.insert(essentials, "minecraft:planks")
  59.     table.insert(essentials, "minecraft:stick")
  60.     table.insert(essentials, "minecraft:diamond")
  61.     table.insert(essentials, "EnderStorage:enderChest")
  62.     table.insert(essentials, "minecraft:cobblestone")
  63. end
  64. function init_diamond_requirered_blocs()
  65.     table.insert(diamond_requirered_blocs, "minecraft:redstone_ore")
  66.     table.insert(diamond_requirered_blocs, "minecraft:obsidian")
  67.     table.insert(diamond_requirered_blocs, "minecraft:diamond_ore")
  68.     table.insert(diamond_requirered_blocs, "minecraft:gold_ore")
  69.     table.insert(diamond_requirered_blocs, "minecraft:emerald_ore")
  70.     table.insert(diamond_requirered_blocs, "simpleores:mythril_ore")
  71.     table.insert(diamond_requirered_blocs, "simpleores:adamantium_ore")
  72.     table.insert(diamond_requirered_blocs, "simpleores:onyx_ore")
  73.     table.insert(diamond_requirered_blocs, "IC2:blockOreUran")
  74.     table.insert(diamond_requirered_blocs, "IC2:blockOreLead")
  75.     table.insert(diamond_requirered_blocs, "minechem:tile.oreUranium")
  76.     table.insert(diamond_requirered_blocs, "qCraft:quantumore")
  77.     table.insert(diamond_requirered_blocs, "qCraft:quantumoreglowing")
  78.     table.insert(diamond_requirered_blocs, "tc:oreTC")
  79.     table.insert(diamond_requirered_blocs, "ProjRed|Exploration:projectred.exploration.ore")
  80.     table.insert(diamond_requirered_blocs, "ThermalFoundation:Ore")
  81.     table.insert(diamond_requirered_blocs, "SGCraft:naquadahOre")
  82. end
  83. function init()
  84.     if search_update then
  85.         check_update()
  86.     end
  87.     if not get_minerals() then
  88.         print("Can't load config file\n")
  89.         os.exit()
  90.     end
  91.     init_shovel_blocs()
  92.     init_essentials()
  93.     init_diamond_requirered_blocs()
  94.     if not unequip() then
  95.         error("I'm really full. Can't continue.")
  96.     end
  97. end
  98. function my_split(inputstr, sep)
  99.     if sep == nil then
  100.         sep = "%s"
  101.     end
  102.     local t={} ; i=1
  103.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  104.         t[i] = str
  105.         i = i + 1
  106.         if i > 20 then
  107.             return t
  108.         end
  109.     end
  110.     return t
  111. end
  112. function check_update()
  113.     local f
  114.     local tmp
  115.     local t = {}
  116.  
  117.     print("Checking updates...")
  118.     shell.execute("pastebin get FuwV0wEv last_dmp_build.tmp.log")
  119.     f = io.open("last_dmp_build.tmp.log",  "r")
  120.     if not f then
  121.         print("Can't check updates.")
  122.         return false
  123.     end
  124.     tmp = f:read()
  125.     f:close()
  126.     t = my_split(tmp, " ")
  127.     if t[2] == "build" and tonumber(t[4]) and tonumber(t[4]) > build then
  128.         shell.execute("rm dmp")
  129.         shell.execute("mv last_dmp_build.tmp.log dmp")
  130.         shell.execute("rm " .. config_filename)
  131.         print("Update found. Downloaded.")
  132.         shell.execute("dmp --no-update")
  133.         os.exit()
  134.     end
  135.     shell.execute("rm last_dmp_build.tmp.log")
  136.     print("No updates found")
  137.     return true
  138. end
  139. function load_config(name)
  140.     local f
  141.     local i = 1
  142.     local tab = {}
  143.  
  144.     f = io.open(name,"r")
  145.     if not f then
  146.         return false
  147.     end
  148.     for line in f:lines() do
  149.         tab[i] = line
  150.         i = i + 1
  151.     end
  152.     f:close()
  153.     return tab
  154. end
  155. function write_default_config(name, text)
  156.     local f
  157.  
  158.     print(name .. " not foud. Creating default one.")
  159.     f = io.open(name,"w")
  160.     if not f then
  161.         return false
  162.     end
  163.     f:write(text)
  164.     f:close()
  165.     return true
  166. end
  167. function file_exists(name)
  168.     local f
  169.  
  170.     f=io.open(name,"r")
  171.     if f then
  172.         io.close(f)
  173.         return true
  174.     end
  175.     return false
  176. end
  177. function get_minerals()
  178.     if not file_exists(config_filename) and not write_default_config(config_filename, "minecraft:diamond_ore\n" ..
  179.             "minecraft:iron_ore\n" ..
  180.             "minecraft:gold_ore\n" ..
  181.             "minecraft:emerald_ore\n" ..
  182.             "minecraft:lapis_ore\n" ..
  183.             "minecraft:dye\n" ..
  184.             "minecraft:iron_ingot\n" ..
  185.             "minecraft:emerald\n" ..
  186.             "minecraft:diamond\n" ..
  187.             "minecraft:diamond_block\n" ..
  188.             "minecraft:coal_block\n" ..
  189.             "minecraft:lapis_block\n" ..
  190.             "minecraft:redstone_block\n" ..
  191.             "minecraft:glowstone\n" ..
  192.             "minecraft:glowstone_dust\n" ..
  193.             "minecraft:quartz\n" ..
  194.             "minecraft:quartz_ore\n" ..
  195.             "minecraft:quartz_block\n" ..
  196.             "minecraft:chest\n" ..
  197.             "minecraft:diamond_pickaxe\n" ..
  198.             "minecraft:stone_pickaxe\n" ..
  199.             "minecraft:iron_pickaxe\n" ..
  200.             "minecraft:log\n" ..
  201.             "minecraft:planks\n" ..
  202.             "minecraft:stick\n" ..
  203.             "EnderStorage:enderChest\n" ..
  204.             "simpleores:copper_ore\n" ..
  205.             "simpleores:tin_ore\n" ..
  206.             "simpleores:mythril_ore\n" ..
  207.             "simpleores:adamantium_ore\n" ..
  208.             "simpleores:onyx_ore\n" ..
  209.             "IC2:blockOreCopper\n" ..
  210.             "IC2:blockOreTin\n" ..
  211.             "IC2:blockOreUran\n" ..
  212.             "IC2:blockOreLead\n" ..
  213.             "minechem:tile.oreUranium\n" ..
  214.             "qCraft:quantumore\n" ..
  215.             "qCraft:quantumoreglowing\n" ..
  216.             "tc:oreTC\n" ..
  217.             "ProjRed|Exploration:projectred.exploration.ore\n" ..
  218.             "ProjRed|Core:projectred.core.part\n" ..
  219.             "minecraft:redstone\n" ..
  220.             "minecraft:coal_ore\n" ..
  221.             "minecraft:coal\n" ..
  222.             "ThermalFoundation:Ore\n" ..
  223.             "minecraft:redstone_ore\n" ..
  224.             "qCraft:dust\n" ..
  225.             "BigReactors:YelloriteOre\n" ..
  226.             "tc:oreTC\nThermalFoundation:Ore\n" ..
  227.             "SGCraft:naquadahOre\n" ..
  228.             "SGCraft:naquadah\n" ..
  229.             "TConstruct:SearedBrick\n") then
  230.         return false
  231.     end
  232.     if not file_exists(stockpile_filename) and not write_default_config(stockpile_filename,
  233.         "minecraft:diamond_ore\n" ..
  234.                 "minecraft:iron_ore\n" ..
  235.                 "minecraft:gold_ore\n" ..
  236.                 "minecraft:emerald_ore\n" ..
  237.                 "minecraft:lapis_ore\n" ..
  238.                 "minecraft:dye\n" ..
  239.                 "minecraft:iron_ingot\n" ..
  240.                 "minecraft:emerald\n" ..
  241.                 "minecraft:diamond\n" ..
  242.                 "minecraft:diamond_block\n" ..
  243.                 "minecraft:coal_block\n" ..
  244.                 "minecraft:lapis_block\n" ..
  245.                 "minecraft:redstone_block\n" ..
  246.                 "minecraft:glowstone\n" ..
  247.                 "minecraft:quartz_ore\n" ..
  248.                 "minecraft:quartz_block\n") then
  249.         return false
  250.     end
  251.     minerals = load_config(config_filename)
  252.     stockpile = load_config(stockpile_filename)
  253.     if not minerals or not stockpile then
  254.         return false
  255.     end
  256.     print(config_filename .. " loaded !")
  257.     return true
  258. end
  259. function is_in_list(name, list)
  260.     local i = 1
  261.  
  262.     while i <= #list do
  263.         if name == list[i] then
  264.             return true
  265.         end
  266.         i = i + 1
  267.     end
  268.     return false
  269. end
  270. function get_item_from_chest(name, meta, amount)
  271.     local slot
  272.     local data = {}
  273.  
  274.     slot = 1
  275.     if (amount and amount <= 0) or not component.inventory_controller.getInventorySize(side.front) then
  276.         return false
  277.     end
  278.     while slot <= component.inventory_controller.getInventorySize(side.front) do
  279.         data = component.inventory_controller.getStackInSlot(side.front, slot)
  280.         if data and data.name == name and (not meta or data.damage == meta) then
  281.             if amount then
  282.                 component.inventory_controller.suckFromSlot(side.front, slot, amount)
  283.             else
  284.                 component.inventory_controller.suckFromSlot(side.front, slot)
  285.             end
  286.             return true
  287.         end
  288.         slot = slot + 1
  289.     end
  290.     return false
  291. end
  292. function select_item_failsafe(item, trymax, meta)
  293.     local try = 0
  294.  
  295.     while not select_item(item, meta) do
  296.         if meta then
  297.             print("Can't select " .. item .. " with meta: " .. meta .. "\nWaiting 30 secs before retrying.")
  298.         else
  299.             print("Can't select " .. item .. "\nWaiting 30 secs before retrying.")
  300.         end
  301.         try = try + 1
  302.         if try > trymax then
  303.             computer.shutdown()
  304.         end
  305.         os.sleep(30)
  306.     end
  307. end
  308. function place_enderchest(number)
  309.     local try
  310.  
  311.     select_item_failsafe("EnderStorage:enderChest", 3, number)
  312.     try = 0
  313.     while not robot.place() do
  314.         smart_swing(true)
  315.         select_item_failsafe("EnderStorage:enderChest", 3, number)
  316.         if robot.place() then
  317.             break
  318.         end
  319.         print("Can't place enderchest, waiting 5 secs before retrying.")
  320.         try = try + 1
  321.         while not robot.up() do
  322.             smart_swingUp()
  323.             os.sleep(0.5)
  324.         end
  325.         if try > 10 then
  326.             computer.shutdown()
  327.         end
  328.         os.sleep(5)
  329.     end
  330.     return try
  331. end
  332. function get_fuel_from_enderchest(amount)
  333.     local bloc
  334.     local try
  335.  
  336.     robot.turnLeft()
  337.     robot.turnLeft()
  338.     bloc = component.geolyzer.analyze(side.front)
  339.     if not bloc or bloc.metadata ~= ender_chest_damage_fuel then
  340.         try = place_enderchest(ender_chest_damage_fuel)
  341.     end
  342.     if (amount and not get_item_from_chest("minecraft:coal", 1, amount)) or not get_item_from_chest("minecraft:coal", 1) then
  343.         smart_swing()
  344.         robot.turnRight()
  345.         robot.turnRight()
  346.         return false
  347.     end
  348.     smart_swing(true)
  349.     while try > 0 do
  350.         while not robot.down() do
  351.             smart_swingDown()
  352.             os.sleep(0.5)
  353.         end
  354.         try = try - 1
  355.     end
  356.     robot.turnRight()
  357.     robot.turnRight()
  358.     return true
  359. end
  360. function select_fuel()
  361.     while item_amount("minecraft:coal", 1) < 16 do
  362.         if not get_fuel_from_enderchest(amount) then
  363.             print("Fail to retreive fuel from enderchest. Retring in 30 secondes...")
  364.             os.sleep(30)
  365.         end
  366.     end
  367.     select_item("minecraft:coal", 1)
  368.     select_item("minecraft:coal", 1)
  369.     return true
  370. end
  371. function check_fuel()
  372.     local data
  373.     local cleaned
  374.  
  375.     cleaned = false
  376.     if component.generator.count() >= 16 and computer.energy() >= 30000 then
  377.         return true
  378.     end
  379.     while not select_empty_case() do
  380.         clean_inventory()
  381.         cleaned = true
  382.     end
  383.     if component.generator.count() <= 0 then
  384.         select_fuel()
  385.     end
  386.     component.generator.insert()
  387.     while computer.energy() < 40300 do
  388.         print("Need generating fuel before continue. Waiting 10 secs.")
  389.         os.sleep(10)
  390.         if component.generator.count() <= 0 then
  391.             return check_fuel()
  392.         end
  393.     end
  394.     return true
  395. end
  396. function equip_pickaxe(bloc_name, force)
  397.     local pick_name = nil
  398.     local other_pick = nil
  399.     local material
  400.  
  401.     if is_in_list(bloc_name, diamond_requirered_blocs) then
  402.         pick_name = "minecraft:diamond_pickaxe"
  403.         other_pick = "minecraft:stone_pickaxe"
  404.         material = "minecraft:diamond"
  405.     elseif not is_in_list(bloc_name, shovel_block) then
  406.         pick_name = "minecraft:stone_pickaxe"
  407.         other_pick = "minecraft:diamond_pickaxe"
  408.         material = "minecraft:cobblestone"
  409.     end
  410.     if not pick_name then
  411.         return true
  412.     end
  413.     while not select_item(pick_name) do
  414.         if force and select_item(other_pick) then
  415.             break
  416.         end
  417.         print("Can't select pickaxe")
  418.         if not craft_pickaxe(material) then
  419.             print(errno .. "\nWaiting 5 secs before retrying...")
  420.             os.sleep(5)
  421.         end
  422.     end
  423.     component.inventory_controller.equip()
  424.     robot.select(1)
  425.     return true
  426. end
  427. function unequip()
  428.     if not select_empty_case() then
  429.         clean_inventory()
  430.         if not select_empty_case() then
  431.             return false
  432.         end
  433.     end
  434.     component.inventory_controller.equip()
  435.     return true
  436. end
  437. function check_and_extract()
  438.     local success, dir
  439.     success, dir = check_filon()
  440.  
  441.     if success then
  442.         extract_filon(dir)
  443.     end
  444. end
  445. function back_to_origin()
  446.     robot.turnRight()
  447.     while offset.x > 0 do
  448.         while not robot.forward() do
  449.             smart_swing()
  450.         end
  451.         offset.x = offset.x - 1
  452.     end
  453.     robot.turnLeft()
  454.     robot.turnLeft()
  455.     while offset.x < 0 do
  456.         while not robot.forward() do
  457.             smart_swing()
  458.         end
  459.         offset.x = offset.x + 1
  460.     end
  461.     robot.turnRight()
  462.     while offset.y < 0 do
  463.         while not robot.up() do
  464.             smart_swingUp()
  465.         end
  466.         offset.y = offset.y + 1
  467.     end
  468.     while offset.y > 0 do
  469.         while not robot.down() do
  470.             smart_swingDown()
  471.         end
  472.         offset.y = offset.y - 1
  473.     end
  474.     robot.turnLeft()
  475.     robot.turnLeft()
  476.     while offset.z < 0 do
  477.         while not robot.forward() do
  478.             smart_swing()
  479.         end
  480.         offset.z = offset.z + 1
  481.     end
  482.     robot.turnRight()
  483.     robot.turnRight()
  484.     while offset.z > 0 do
  485.         while not robot.forward() do
  486.             smart_swing()
  487.         end
  488.         offset.z = offset.z - 1
  489.     end
  490. end
  491. function select_lava_filler_bloc()
  492.     if select_item("minecraft:netherrack") or select_item("minecraft:stone") or (not is_in_list("minecraft:dirt", minerals) and select_item("minecraft:dirt")) or (item_amount("minecraft:cobblestone") > 10 and ("minecraft:cobblestone")) then
  493.         return true
  494.     end
  495.     return false
  496. end
  497. function check_filon()
  498.     local bloc
  499.  
  500.     bloc = component.geolyzer.analyze(side.left)
  501.     if (bloc.name == "minecraft:flowing_lava" or bloc.name == "minecraft:lava") and select_lava_filler_bloc() then
  502.         robot.turnLeft()
  503.         robot.place()
  504.         robot.turnRight()
  505.     elseif is_in_list(bloc.name, minerals) then
  506.         return true, side.left
  507.     end
  508.     bloc = component.geolyzer.analyze(side.right)
  509.     if (bloc.name == "minecraft:flowing_lava" or bloc.name == "minecraft:lava") and select_lava_filler_bloc() then
  510.         robot.turnRight()
  511.         robot.place()
  512.         robot.turnLeft()
  513.     elseif is_in_list(bloc.name, minerals) then
  514.         return true, side.right
  515.     end
  516.     bloc = component.geolyzer.analyze(side.up)
  517.     if (bloc.name == "minecraft:flowing_lava" or bloc.name == "minecraft:lava") and select_lava_filler_bloc() then
  518.         robot.placeUp()
  519.     elseif is_in_list(bloc.name, minerals) then
  520.         return true, side.up
  521.     end
  522.     bloc = component.geolyzer.analyze(side.down)
  523.     if (bloc.name == "minecraft:flowing_lava" or bloc.name == "minecraft:lava") and select_lava_filler_bloc() then
  524.         robot.placeDown()
  525.     elseif is_in_list(bloc.name, minerals) then
  526.         return true, side.down
  527.     end
  528.     bloc = component.geolyzer.analyze(side.back)
  529.     if (bloc.name == "minecraft:flowing_lava" or bloc.name == "minecraft:lava") and select_lava_filler_bloc() then
  530.         robot.turnRight()
  531.         robot.turnRight()
  532.         robot.place()
  533.         robot.turnLeft()
  534.         robot.turnLeft()
  535.     elseif is_in_list(bloc.name, minerals) then
  536.         return true, side.back
  537.     end
  538.     if robot.detect() then
  539.         bloc = component.geolyzer.analyze(side.front)
  540.         if (bloc.name == "minecraft:flowing_lava" or bloc.name == "minecraft:lava") and select_lava_filler_bloc() then
  541.             robot.place()
  542.         elseif is_in_list(bloc.name, minerals) then
  543.             return true, side.front
  544.         end
  545.     end
  546.     return false, nil
  547. end
  548. function extract_filon(direction)
  549.     local success, dir
  550.  
  551.     if empty_cases_amount() < 11 then
  552.         clean_inventory()
  553.     end
  554.     if direction == side.front then
  555.         while not robot.forward() do
  556.             smart_swing()
  557.         end
  558.         check_and_extract()
  559.         robot.turnRight()
  560.         robot.turnRight()
  561.         while not robot.forward() do
  562.             smart_swing()
  563.         end
  564.         robot.turnLeft()
  565.         robot.turnLeft()
  566.     elseif direction == side.left then
  567.         robot.turnLeft()
  568.         while not robot.forward() do
  569.             smart_swing()
  570.         end
  571.         robot.turnRight()
  572.         check_and_extract()
  573.         robot.turnRight()
  574.         while not robot.forward() do
  575.             smart_swing()
  576.         end
  577.         robot.turnLeft()
  578.     elseif direction == side.right then
  579.         robot.turnRight()
  580.         while not robot.forward() do
  581.             smart_swing()
  582.         end
  583.         robot.turnLeft()
  584.         check_and_extract()
  585.         robot.turnLeft()
  586.         while not robot.forward() do
  587.             smart_swing()
  588.         end
  589.         robot.turnRight()
  590.     elseif direction == side.up then
  591.         while not robot.up() do
  592.             smart_swingUp()
  593.         end
  594.         check_and_extract()
  595.         while not robot.down() do
  596.             smart_swingDown()
  597.         end
  598.     elseif direction == side.down then
  599.         while not robot.down() do
  600.             smart_swingDown()
  601.         end
  602.         check_and_extract()
  603.         while not robot.up() do
  604.             smart_swingUp()
  605.         end
  606.     elseif direction == side.back then
  607.         robot.turnLeft()
  608.         robot.turnLeft()
  609.         while not robot.forward() do
  610.             smart_swing()
  611.         end
  612.         robot.turnRight()
  613.         robot.turnRight()
  614.         check_and_extract()
  615.         while not robot.forward() do
  616.             smart_swing()
  617.         end
  618.     end
  619.     check_and_extract()
  620. end
  621. function select_item(item, meta)
  622.     local slot = 1
  623.     local size = robot.inventorySize()
  624.  
  625.     errno = "unknown error"
  626.     while slot <= size do
  627.         local data = component.inventory_controller.getStackInInternalSlot(slot)
  628.         if data and data.name == item and (not meta or data.damage == meta) then
  629.             robot.select(slot)
  630.             return true
  631.         end
  632.         slot = slot + 1
  633.     end
  634.     errno = "Can't select: " .. item
  635.     if meta then
  636.         errno = errno .. " with metadata: "  .. meta
  637.     end
  638.     return false
  639. end
  640. function select_next_item(slot, item, meta)
  641.     local data
  642.     local size = robot.inventorySize()
  643.  
  644.     while slot <= robot.inventorySize() do
  645.         data = component.inventory_controller.getStackInInternalSlot(slot)
  646.         if data and data.name == item and (not meta or meta == data.damage) then
  647.             robot.select(slot)
  648.             return true
  649.         end
  650.         slot = slot + 1
  651.     end
  652.     return false
  653. end
  654. function repack_item(item, meta)
  655.     local slot = 1
  656.     local data
  657.  
  658.     if item == "minecraft:stone_pickaxe" or item == "minecraft:diamond_pickaxe" then
  659.         return true
  660.     end
  661.     while slot < robot.inventorySize() do
  662.         data = component.inventory_controller.getStackInInternalSlot(slot)
  663.         if not data or ((data.name == item and data.size < 64) and (not meta or meta == data.damage)) then
  664.             if select_next_item(slot + 1, item, meta) then
  665.                 robot.transferTo(slot)
  666.                 data = component.inventory_controller.getStackInInternalSlot(slot)
  667.             else
  668.                 return true
  669.             end
  670.         else
  671.             slot = slot + 1
  672.         end
  673.     end
  674.     return true
  675. end
  676. function clean_inventory()
  677.     local slot
  678.     local data
  679.     local cobble = false
  680.     local garbage = {}
  681.     local try
  682.  
  683.     robot.turnLeft()
  684.     robot.turnLeft()
  685.     if empty_cases_amount() > 10 then
  686.         compact_all()
  687.     end
  688.     try = place_enderchest(ender_chest_damage_dump)
  689.     repack_item("minecraft:cobblestone")
  690.     slot = 1
  691.     while slot <= robot.inventorySize() do
  692.         data = component.inventory_controller.getStackInInternalSlot(slot)
  693.         if data then
  694.             if not is_in_list(data.name, essentials) or data.name == "minecraft:diamond" then
  695.                 if data.name == "minecraft:diamond" and data.size > 3 then
  696.                     dump_slot_to_chest(side.front, slot, data.size - 3)
  697.                 elseif is_in_list(data.name, minerals) and data.name ~= "minecraft:diamond" then
  698.                     dump_slot_to_chest(side.front, slot, data.size)
  699.                 elseif data.name ~= "minecraft:diamond" then
  700.                     table.insert(garbage, slot)
  701.                 end
  702.             elseif is_in_list(data.name, essentials) and data.name ~= "minecraft:cobblestone" then
  703.                 repack_item(data.name, data.damage)
  704.             elseif not cobble and data.name == "minecraft:cobblestone" then
  705.                 cobble = true
  706.             else
  707.                 table.insert(garbage, slot)
  708.             end
  709.         end
  710.         slot = slot + 1
  711.     end
  712.     smart_swing(true)
  713.     while try > 0 do
  714.         while not robot.down() do
  715.             smart_swingDown()
  716.             os.sleep(0.5)
  717.         end
  718.         try = try - 1
  719.     end
  720.     slot = 1
  721.     while slot <= #garbage do
  722.         robot.select(garbage[slot])
  723.         robot.drop()
  724.         slot = slot + 1
  725.     end
  726.     robot.turnRight()
  727.     robot.turnRight()
  728. end
  729. function smart_swing(force)
  730.     local bloc
  731.  
  732.     bloc = component.geolyzer.analyze(side.front)
  733.     if not bloc then
  734.         return true
  735.     end
  736.     equip_pickaxe(bloc.name, force)
  737.     robot.swing()
  738.     if not unequip() then
  739.         return false
  740.     end
  741.     return true
  742. end
  743. function smart_swingUp()
  744.     local bloc
  745.  
  746.     bloc = component.geolyzer.analyze(side.top)
  747.     equip_pickaxe(bloc.name)
  748.     robot.swingUp()
  749.     if not unequip() then
  750.         return false
  751.     end
  752.     return true
  753. end
  754. function smart_swingDown()
  755.     local bloc
  756.  
  757.     bloc = component.geolyzer.analyze(side.bottom)
  758.     equip_pickaxe(bloc.name)
  759.     robot.swingDown()
  760.     if not unequip() then
  761.         return false
  762.     end
  763.     return true
  764. end
  765. function avance()
  766.     errno = "Unknown error"
  767.     check_fuel()
  768.     while item_amount("minecraft:log") < 10 do
  769.         print("No/Few logs left in robot. Retreiving...")
  770.         ender_chest_get_item(ender_chest_damage_log, "minecraft:log")
  771.         os.sleep(2)
  772.     end
  773.     if inventory_checker_counter >= 2 and empty_cases_amount() < 13 then
  774.         clean_inventory()
  775.         inventory_checker_counter = 0
  776.     else
  777.         inventory_checker_counter = inventory_checker_counter + 1
  778.     end
  779.     check_and_extract()
  780.     while not robot.forward() do
  781.         smart_swing()
  782.         os.sleep(0.5)
  783.     end
  784.     while not robot.up() do
  785.         smart_swingUp()
  786.         os.sleep(0.5)
  787.     end
  788.     check_and_extract()
  789.     while not robot.down() do
  790.         smart_swingDown()
  791.         os.sleep(0.5)
  792.     end
  793.     while not robot.down() do
  794.         smart_swingDown()
  795.         os.sleep(0.5)
  796.     end
  797.     check_and_extract()
  798.     while not robot.up() do
  799.         smart_swingUp()
  800.         os.sleep(0.5)
  801.     end
  802. end
  803. function line(i)
  804.     while i > 0 do
  805.         avance()
  806.         i = i - 1
  807.     end
  808.     if robot.detectUp() then
  809.         smart_swingUp()
  810.     end
  811.     if robot.detectDown() then
  812.         smart_swingDown()
  813.     end
  814. end
  815. function turn(bool)
  816.     if bool then
  817.         robot.turnRight()
  818.     else
  819.         robot.turnLeft()
  820.     end
  821. end
  822. function xor_bool(bool)
  823.     if bool then
  824.         return false
  825.     else
  826.         return true
  827.     end
  828. end
  829. function core(xmax, tunmax, dir)
  830.     local bool = true
  831.     local first = true
  832.  
  833.     if dir == "left" then
  834.         bool = false
  835.     end
  836.     while tunmax > 0 do
  837.         line(xmax)
  838.         if not first then
  839.             turn(xor_bool(bool))
  840.             line(4)
  841.             turn(bool)
  842.             turn(bool)
  843.             line(8)
  844.             turn(bool)
  845.         else
  846.             turn(bool)
  847.             line(4)
  848.             turn(bool)
  849.             first = false
  850.         end
  851.         bool = xor_bool(bool)
  852.         tunmax = tunmax - 1
  853.         os.sleep(1)
  854.         print("Tunnel(s) restant(s): " .. tostring(tunmax))
  855.     end
  856. end
  857. function push_item_after_slot(from)
  858.     local slot = from + 1
  859.  
  860.     robot.select(from)
  861.     while slot < robot.inventorySize() and component.inventory_controller.getStackInInternalSlot(slot) do
  862.         slot = slot + 1
  863.     end
  864.     if slot < robot.inventorySize() and robot.transferTo(slot) then
  865.         return true
  866.     end
  867.     return false
  868. end
  869. function free_crafting_table()
  870.     local slot = 1
  871.     local data
  872.     local tried = false
  873.  
  874.     while slot <= 11 do
  875.         if slot ~= 4 and slot ~= 8 then
  876.             if component.inventory_controller.getStackInInternalSlot(slot) and not push_item_after_slot(slot) and not tried then
  877.                 clean_inventory()
  878.                 tried = true
  879.             elseif component.inventory_controller.getStackInInternalSlot(slot) and not push_item_after_slot(slot) then
  880.                 return false
  881.             end
  882.         end
  883.         slot = slot + 1
  884.     end
  885.     return true
  886. end
  887. function compact_all()
  888.     compact_item(9, "minecraft:dye", 4)
  889.     repack_item("minecraft:dye", 4)
  890.     compact_item(9, "minecraft:coal", 0)
  891.     repack_item("minecraft:coal", 0)
  892.     compact_item(9, "minecraft:redstone")
  893.     repack_item("minecraft:redstone")
  894.     compact_item(4, "minecraft:quartz")
  895.     repack_item("minecraft:quartz")
  896.     compact_item(4, "minecraft:glowstone_dust", true)
  897.     repack_item("minecraft:glowstone_dust")
  898. end
  899. function craft_pickaxe(material)
  900.     if item_amount("minecraft:stick") < 2 and not craft_sticks() then
  901.         return false
  902.     end
  903.     if free_crafting_table() then
  904.         place_item_for_craft(material, 1)
  905.         place_item_for_craft(material, 2)
  906.         place_item_for_craft(material, 3)
  907.         place_item_for_craft("minecraft:stick", 5)
  908.         place_item_for_craft("minecraft:stick", 8)
  909.         return component.crafting.craft(1)
  910.     end
  911.     errno = "Error when crafting " .. material
  912.     return false
  913. end
  914. function craft_sticks()
  915.     if item_amount("minecraft:planks") < 2 and not craft_planks() then
  916.         return false
  917.     end
  918.     if free_crafting_table() then
  919.         place_item_for_craft("minecraft:planks", 1)
  920.         place_item_for_craft("minecraft:planks", 4)
  921.         return component.crafting.craft(1)
  922.     end
  923.     errno = "Error when crafting sticks"
  924.     return false
  925. end
  926. function craft_planks()
  927.     if item_amount("minecraft:log") < 1 then
  928.         return false
  929.     end
  930.     if free_crafting_table() then
  931.         place_item_for_craft("minecraft:log", 1)
  932.         return component.crafting.craft(1)
  933.     end
  934.     errno = "Error when crafting sticks"
  935.     return false
  936. end
  937. function pickaxe_materials()
  938.     local wood, material = false, false
  939.     local stick = 0
  940.     local data
  941.     local inv_size = robot.inventorySize()
  942.     local slot = 1
  943.  
  944.     while slot <= inv_size and (not wood or not material) do
  945.         data = component.inventory_controller.getStackInInternalSlot(slot)
  946.         if data and not wood and data.name == "minecraft:stick" then
  947.             stick = data.size + stick
  948.             if stick >= 2 then
  949.                 wood = true
  950.             end
  951.         elseif data and not material and (data.name == "minecraft:cobblestone" or data.name == "minecraft:iron_ingot") and data.size >= 3 then
  952.             material = true
  953.         end
  954.         slot = slot + 1
  955.     end
  956.     if material and not wood then
  957.         return craft_sticks()
  958.     elseif wood and material then
  959.         return true
  960.     end
  961.     return false
  962. end
  963. function empty_cases_amount()
  964.     local slot = 1
  965.     local empty_slots = 0
  966.     local size = robot.inventorySize()
  967.  
  968.     while slot <= size do
  969.         if not component.inventory_controller.getStackInInternalSlot(slot) then
  970.             empty_slots = empty_slots + 1
  971.         end
  972.         slot = slot + 1
  973.     end
  974.     return empty_slots
  975. end
  976. function select_empty_case()
  977.     local slot = 1
  978.  
  979.     while slot <= robot.inventorySize() do
  980.         if not component.inventory_controller.getStackInInternalSlot(slot) then
  981.             robot.select(slot)
  982.             return true
  983.         end
  984.         slot = slot + 1
  985.     end
  986.     return false
  987. end
  988. function select_item_out_of_workbench(item, meta)
  989.     local slot = 4
  990.     local data
  991.     local size = robot.inventorySize()
  992.  
  993.     while slot <= size do
  994.         if slot == 5 or slot == 9 then
  995.             slot = slot + 3
  996.         end
  997.         data = component.inventory_controller.getStackInInternalSlot(slot)
  998.         if data and data.name == item and (not meta or data.damage == meta) then
  999.             robot.select(slot)
  1000.             return true
  1001.         end
  1002.         slot = slot + 1
  1003.     end
  1004.     return false
  1005. end
  1006. function place_item_for_craft(item, to, meta)
  1007.     local craft = {}
  1008.  
  1009.     craft[1] = 1
  1010.     craft[2] = 2
  1011.     craft[3] = 3
  1012.     craft[4] = 5
  1013.     craft[5] = 6
  1014.     craft[6] = 7
  1015.     craft[7] = 9
  1016.     craft[8] = 10
  1017.     craft[9] = 11
  1018.     if to > 9 then
  1019.         print("place_item_for_craft: slot can't be > than 9")
  1020.         os.exit()
  1021.     end
  1022.     if select_item_out_of_workbench(item, meta) and robot.transferTo(craft[to], 1) then
  1023.         return true
  1024.     end
  1025.     return false
  1026. end
  1027. function craft_all_blocs(bloc_size, item, meta)
  1028.     if bloc_size == 9 then
  1029.         while   place_item_for_craft(item, 1, meta) and
  1030.                 place_item_for_craft(item, 2, meta) and
  1031.                 place_item_for_craft(item, 3, meta) and
  1032.                 place_item_for_craft(item, 4, meta) and
  1033.                 place_item_for_craft(item, 5, meta) and
  1034.                 place_item_for_craft(item, 6, meta) and
  1035.                 place_item_for_craft(item, 7, meta) and
  1036.                 place_item_for_craft(item, 8, meta) and
  1037.                 place_item_for_craft(item, 9, meta) do
  1038.             os.sleep(0.1)
  1039.         end
  1040.     elseif bloc_size == 4 then
  1041.         while   place_item_for_craft(item, 1, meta) and
  1042.                 place_item_for_craft(item, 2, meta) and
  1043.                 place_item_for_craft(item, 4, meta) and
  1044.                 place_item_for_craft(item, 5, meta) do
  1045.             os.sleep(0.1)
  1046.         end
  1047.     end
  1048.     return component.crafting.craft()
  1049. end
  1050. function compact_item(bloc_size, item, meta, clean)
  1051.     local amount = item_amount(item, meta)
  1052.  
  1053.     if amount >= bloc_size then
  1054.         if not free_crafting_table() then
  1055.             clean_inventory()
  1056.             if not free_crafting_table() then
  1057.                 return false
  1058.             end
  1059.         end
  1060.         craft_all_blocs(bloc_size, item, meta)
  1061.         if clean then
  1062.             clean_inventory()
  1063.         end
  1064.         return true
  1065.     end
  1066.     return false
  1067. end
  1068. function item_amount(item, meta)
  1069.     local slot = 1
  1070.     local size = robot.inventorySize()
  1071.     local amount = 0
  1072.  
  1073.     while slot <= size do
  1074.         local data = component.inventory_controller.getStackInInternalSlot(slot)
  1075.         if data and data.name == item and (not meta or data.damage == meta) then
  1076.             amount = amount + data.size
  1077.         end
  1078.         slot = slot + 1
  1079.     end
  1080.     return amount
  1081. end
  1082. function stock_minerals()
  1083.     local slot = 1
  1084.  
  1085.     dump_to_ender_chest()
  1086.     if empty_cases_amount() < 15 and select_item("minecraft:chest") then
  1087.         robot.place()
  1088.         while slot < robot.inventorySize() do
  1089.             local data = component.inventory_controller.getStackInInternalSlot(slot)
  1090.  
  1091.             if data and is_in_list(data.name, stockpile) and not is_in_list(data.name, essentials) then
  1092.                 robot.select(slot)
  1093.                 robot.drop()
  1094.             end
  1095.             slot = slot + 1
  1096.         end
  1097.     end
  1098. end
  1099. function get_free_slot_in_chest(chest_side)
  1100.     local slot
  1101.     local inv_size
  1102.     local data
  1103.  
  1104.     slot = 1
  1105.     if not chest_side then
  1106.         chest_side = side.front
  1107.     end
  1108.     inv_size = component.inventory_controller.getInventorySize(chest_side)
  1109.     while slot < inv_size do
  1110.         data = component.inventory_controller.getStackInSlot(chest_side, slot)
  1111.         if not data then
  1112.             return slot
  1113.         end
  1114.         slot = slot + 1
  1115.     end
  1116.     return false
  1117. end
  1118. function dump_slot_to_chest(chest_side, from_slot, amount, to_slot)
  1119.     if not to_slot then
  1120.         to_slot = get_free_slot_in_chest(chest_side)
  1121.         if not to_slot then
  1122.             return false
  1123.         end
  1124.     end
  1125.     robot.select(from_slot)
  1126.     if not amount then
  1127.         component.inventory_controller.dropIntoSlot(side.front, to_slot)
  1128.     else
  1129.         component.inventory_controller.dropIntoSlot(side.front, to_slot, amount)
  1130.     end
  1131.     return true
  1132. end
  1133. function dump_to_ender_chest()
  1134.     local slot = 1
  1135.     local dump_slot
  1136.     local try
  1137.  
  1138.     try = place_enderchest(ender_chest_damage_dump)
  1139.     while slot < robot.inventorySize() do
  1140.         data = component.inventory_controller.getStackInInternalSlot(slot)
  1141.         if data and not is_in_list(data.name, essentials) then
  1142.             if data.name == "minecraft:diamond" and data.size > 3 then
  1143.                 dump_slot_to_chest(side.front, slot, data.size - 3)
  1144.             elseif data.name ~= "minecraft:diamond" then
  1145.                 dump_slot_to_chest(side.front, slot, data.size)
  1146.             end
  1147.         end
  1148.         slot = slot + 1
  1149.     end
  1150.     smart_swing(true)
  1151.     while try > 0 do
  1152.         while not robot.down() do
  1153.             smart_swingDown()
  1154.             os.sleep(0.5)
  1155.         end
  1156.         try = try - 1
  1157.     end
  1158. end
  1159. function ender_chest_get_item(ender_chest, name, id, amount)
  1160.     local try
  1161.  
  1162.     try = place_enderchest(ender_chest)
  1163.     if id then
  1164.         if (amount and not get_item_from_chest(name, id, amount)) or not get_item_from_chest(name, id) then
  1165.             smart_swing()
  1166.             return false
  1167.         end
  1168.         smart_swing()
  1169.         return true
  1170.     end
  1171.     if (amount and not get_item_from_chest(name, id, amount)) or not get_item_from_chest(name, id) then
  1172.         smart_swing()
  1173.         return false
  1174.     end
  1175.     smart_swing(true)
  1176.     while try > 0 do
  1177.         while not robot.down() do
  1178.             smart_swingDown()
  1179.             os.sleep(0.5)
  1180.         end
  1181.         try = try - 1
  1182.     end
  1183.     return true
  1184. end
  1185.  
  1186. local lenght, amount, direction
  1187.  
  1188. init()
  1189.  
  1190. print("Robot energy left:" .. computer.energy())
  1191.  
  1192. print("Tunnel lenght ?")
  1193. lenght = tonumber(io.read())
  1194. print("Amount of parallels tunels ?")
  1195. amount = tonumber(io.read())
  1196. if amount > 1 then
  1197.     print("To the left or right ?")
  1198.     direction = io.read()
  1199.     if direction ~= "left" and direction ~= "right" then
  1200.         error("Unknown direction: " .. direction)
  1201.     end
  1202. else
  1203.     direction = "right"
  1204. end
  1205.  
  1206. core(lenght, amount, direction)
Add Comment
Please, Sign In to add comment