Advertisement
Link712011

Mine in cube - space only

Aug 31st, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.32 KB | None | 0 0
  1. local robot = require("robot")
  2. local side = require("sides")
  3. local component = require("component")
  4.  
  5. local offset = { x = 0, y = 0, z = 0 }
  6. local errno = "unknown error"
  7. local minerals = {}
  8. local unwanted = {}
  9.  
  10. function init()
  11.     get_minerals()
  12.     get_unwanted_minerals()
  13. end
  14. function move_up(x)
  15.     if not x then
  16.         local x = 1
  17.     end
  18.     local i = 0
  19.    
  20.     while i < x do
  21.         if not robot.up() then
  22.             smart_swingUp()
  23.         else
  24.             i = i + 1
  25.         end
  26.        
  27.     end
  28. end
  29. function move_down(x)
  30.     if not x then
  31.         local x = 1
  32.     end
  33.     local i = 0
  34.    
  35.     while i < x do
  36.         if not robot.down() then
  37.             smart_swingDown()
  38.         else
  39.             i = i + 1
  40.         end
  41.        
  42.     end
  43. end
  44. function get_minerals()
  45.     table.insert(minerals, "minecraft:no_one")
  46. end
  47. function get_unwanted_minerals()
  48.     table.insert(unwanted, "minecraft:diamond_ore")
  49.     table.insert(unwanted, "minecraft:iron_ore")
  50.     table.insert(unwanted, "minecraft:gold_ore")
  51.     table.insert(unwanted, "minecraft:quartz_ore")
  52.     table.insert(unwanted, "minecraft:emerald_ore")
  53.     table.insert(unwanted, "minecraft:lapis_ore")
  54.     table.insert(unwanted, "minecraft:redstone_ore")
  55.     table.insert(unwanted, "minecraft:obsidian")
  56. end
  57. function is_mineral(bloc)
  58.     local i = 1
  59.    
  60.     while i <= #minerals do
  61.         if bloc == minerals[i] then
  62.             return true
  63.         end
  64.         i = i + 1
  65.     end
  66.     return false
  67. end
  68. function is_unwanted_mineral(bloc)
  69.     local i = 1
  70.    
  71.     while i <= #unwanted do
  72.         if bloc == unwanted[i] then
  73.             return true
  74.         end
  75.         i = i + 1
  76.     end
  77.     return false
  78. end
  79. function equip_diamond_pickaxe()
  80.     if select_item("minecraft:diamond_pickaxe") and component.inventory_controller.equip() then
  81.         robot.select(1)
  82.         return true
  83.     end
  84.     robot.select(1)
  85.     return false
  86. end
  87. function equip_classic_pickaxe()
  88.     if not select_item("minecraft:stone_pickaxe") and not select_item("minecraft:iron_pickaxe") and not craft_pickaxe() then
  89.         print(errno)
  90.         while robot.down() do end
  91.         os.exit()
  92.         return false
  93.     elseif not select_item("minecraft:stone_pickaxe") and not select_item("minecraft:iron_pickaxe") then
  94.         print(errno)
  95.         os.exit()
  96.         return false
  97.     end
  98.     component.inventory_controller.equip()
  99.     robot.select(1)
  100.     return true
  101. end
  102. function unequip()
  103.     if not select_empty_case() then
  104.         clean_inventory()
  105.         if not select_empty_case() then
  106.             return false
  107.         end
  108.     end
  109.     component.inventory_controller.equip()
  110.     return true
  111. end
  112. function check_and_extract()
  113.     local success, dir
  114.     success, dir = check_filon()
  115.    
  116.     if success then
  117.         extract_filon(dir)
  118.     end
  119. end
  120. function back_to_origin()
  121.     robot.turnRight()
  122.     while offset.x > 0 do
  123.         while not robot.forward() do
  124.             smart_swing()
  125.         end
  126.         offset.x = offset.x - 1
  127.     end
  128.     robot.turnLeft()
  129.     robot.turnLeft()
  130.     while offset.x < 0 do
  131.         while not robot.forward() do
  132.             smart_swing()
  133.         end
  134.         offset.x = offset.x + 1
  135.     end
  136.     robot.turnRight()
  137.     while offset.y < 0 do
  138.         while not robot.up() do
  139.             smart_swingUp()
  140.         end
  141.         offset.y = offset.y + 1
  142.     end
  143.     while offset.y > 0 do
  144.         while not robot.down() do
  145.             smart_swingDown()
  146.         end
  147.         offset.y = offset.y - 1
  148.     end
  149.     robot.turnLeft()
  150.     robot.turnLeft()
  151.     while offset.z < 0 do
  152.         while not robot.forward() do
  153.             smart_swing()
  154.         end
  155.         offset.z = offset.z + 1
  156.     end
  157.     robot.turnRight()
  158.     robot.turnRight()
  159.     while offset.z > 0 do
  160.         while not robot.forward() do
  161.             smart_swing()
  162.         end
  163.         offset.z = offset.z - 1
  164.     end
  165. end
  166. function check_filon()
  167.     local bloc
  168.    
  169.     bloc = component.geolyzer.analyze(side.left)
  170.     if is_mineral(bloc.name) then
  171.         return true, side.left
  172.     end
  173.     bloc = component.geolyzer.analyze(side.right)
  174.     if is_mineral(bloc.name) then
  175.         return true, side.right
  176.     end
  177.     bloc = component.geolyzer.analyze(side.up)
  178.     if is_mineral(bloc.name) then
  179.         return true, side.up
  180.     end
  181.     bloc = component.geolyzer.analyze(side.down)
  182.     if is_mineral(bloc.name) then
  183.         return true, side.down
  184.     end
  185.     bloc = component.geolyzer.analyze(side.back)
  186.     if is_mineral(bloc.name) then
  187.         return true, side.back
  188.     end
  189.     bloc = component.geolyzer.analyze(side.front)
  190.     if is_mineral(bloc.name) then
  191.         return true, side.front
  192.     end
  193.     return false, nil
  194. end
  195. function extract_filon(direction)
  196.     local success, dir
  197.    
  198.     if is_inventory_full() then
  199.         clean_inventory()
  200.     end
  201.     if direction == side.front then
  202.         while not robot.forward() do
  203.             smart_swing()
  204.         end
  205.         check_and_extract()
  206.         robot.turnRight()
  207.         robot.turnRight()
  208.         while not robot.forward() do
  209.             smart_swing()
  210.         end
  211.         robot.turnLeft()
  212.         robot.turnLeft()
  213.     elseif direction == side.left then
  214.         robot.turnLeft()
  215.         while not robot.forward() do
  216.             smart_swing()
  217.         end
  218.         robot.turnRight()
  219.         check_and_extract()
  220.         robot.turnRight()
  221.         while not robot.forward() do
  222.             smart_swing()
  223.         end
  224.         robot.turnLeft()
  225.     elseif direction == side.right then
  226.         robot.turnRight()
  227.         while not robot.forward() do
  228.             smart_swing()
  229.         end
  230.         robot.turnLeft()
  231.         check_and_extract()
  232.         robot.turnLeft()
  233.         while not robot.forward() do
  234.             smart_swing()
  235.         end
  236.         robot.turnRight()
  237.     elseif direction == side.up then
  238.         while not robot.up() do
  239.             smart_swingUp()
  240.         end
  241.         check_and_extract()
  242.         while not robot.down() do
  243.             smart_swingDown()
  244.         end
  245.     elseif direction == side.down then
  246.         while not robot.down() do
  247.             smart_swingDown()
  248.         end
  249.         check_and_extract()
  250.         while not robot.up() do
  251.             smart_swingUp()
  252.         end
  253.     elseif direction == side.back then
  254.         robot.turnLeft()
  255.         robot.turnLeft()
  256.         while not robot.forward() do
  257.             smart_swing()
  258.         end
  259.         robot.turnRight()
  260.         robot.turnRight()
  261.         check_and_extract()
  262.         while not robot.forward() do
  263.             smart_swing()
  264.         end
  265.     end
  266.     check_and_extract()
  267. end
  268. function select_item(item)
  269.     local slot = 1
  270.     local size = robot.inventorySize()
  271.    
  272.     errno = "unknown error"
  273.     while slot <= size do
  274.         local data = component.inventory_controller.getStackInInternalSlot(slot)
  275.         if data and data.name == item then
  276.             robot.select(slot)
  277.             return true
  278.         end
  279.         slot = slot + 1
  280.     end
  281.     errno = "Can't select: " .. item
  282.     return false
  283. end
  284. function select_next_item(slot, item)
  285.     local data
  286.     local size = robot.inventorySize()
  287.    
  288.     while slot <= robot.inventorySize() do
  289.         data = component.inventory_controller.getStackInInternalSlot(slot)
  290.         if data and data.name == item then
  291.             robot.select(slot)
  292.             return true
  293.         end
  294.         slot = slot + 1
  295.     end
  296.     return false
  297. end
  298. function repack_item(item)
  299.     local slot = 1
  300.     local data
  301.     local size = robot.inventorySize()
  302.    
  303.     while slot < size do
  304.         data = component.inventory_controller.getStackInInternalSlot(slot)
  305.         if not data or (data.name == item and data.size < 64) then
  306.             if select_next_item(slot + 1, item) then
  307.                 robot.transferTo(slot)
  308.                 data = component.inventory_controller.getStackInInternalSlot(slot)
  309.             else
  310.                 return true
  311.             end
  312.         end
  313.         if data and ((data.name == item and data.size == 64) or data.name ~= item) then
  314.             slot = slot + 1
  315.         end
  316.     end
  317.     return true
  318. end
  319. function clean_inventory()
  320.     local slot = 1
  321.     local data
  322.     local cobble = false
  323.    
  324.     repack_item("minecraft:cobblestone")
  325.     while slot <= robot.inventorySize() do
  326.         data = component.inventory_controller.getStackInInternalSlot(slot)
  327.         if data and (is_mineral(data.name) or data.name == "minecraft:diamond" or data.name == "minecraft:emerald" or data.name == "minecraft:iron_ingot" or (data.name == "minecraft:dye" and data.damage == 4) or data.name == "minecraft:diamond_pickaxe" or data.name == "minecraft:stone_pickaxe" or data.name == "minecraft:iron_pickaxe" or data.name == "minecraft:log" or data.name == "minecraft:planks" or data.name == "minecraft:stick") then
  328.             repack_item(data.name)
  329.         elseif data and not cobble and data.name == "minecraft:cobblestone" then
  330.             cobble = true
  331.         elseif data then
  332.             robot.select(slot)
  333.             robot.drop()
  334.         end
  335.         slot = slot + 1
  336.     end
  337. end
  338. function smart_swing()
  339.     local bloc
  340.    
  341.     bloc = component.geolyzer.analyze(side.front)
  342.     if is_mineral(bloc.name) or is_unwanted_mineral(bloc.name) then
  343.         if not equip_diamond_pickaxe() then
  344.             -- error("No diamond pickaxe found !")
  345.             return false
  346.         end
  347.     else
  348.         if not equip_classic_pickaxe() then
  349.             -- error("No pickaxe can be crafted: " .. errno)
  350.             return false
  351.         end
  352.     end
  353.     robot.swing()
  354.     if not unequip() then
  355.         return false
  356.     end
  357.     return true
  358. end
  359. function smart_swingUp()
  360.     local bloc
  361.    
  362.     bloc = component.geolyzer.analyze(side.top)
  363.     if is_mineral(bloc.name) or is_unwanted_mineral(bloc.name) then
  364.         if not equip_diamond_pickaxe() then
  365.             error("No diamond pickaxe found !")
  366.             return false
  367.         end
  368.     else
  369.         if not equip_classic_pickaxe() then
  370.             error("No pickaxe can be crafted: " .. errno)
  371.             return false
  372.         end
  373.     end
  374.     robot.swingUp()
  375.     if not unequip() then
  376.         return false
  377.     end
  378.     return true
  379. end
  380. function smart_swingDown()
  381.     local bloc
  382.    
  383.     bloc = component.geolyzer.analyze(side.bottom)
  384.     if is_mineral(bloc.name) or is_unwanted_mineral(bloc.name) then
  385.         if not equip_diamond_pickaxe() then
  386.             error("No diamond pickaxe found !")
  387.             return false
  388.         end
  389.     else
  390.         if not equip_classic_pickaxe() then
  391.             error("No pickaxe can be crafted: " .. errno)
  392.             return false
  393.         end
  394.     end
  395.     robot.swingDown()
  396.     if not unequip() then
  397.         return false
  398.     end
  399.     return true
  400. end
  401. function is_inventory_full()
  402.     local slot = 1
  403.     local data
  404.    
  405.     while slot <= robot.inventorySize() do
  406.         data = component.inventory_controller.getStackInInternalSlot(slot)
  407.         if not data then
  408.             return false
  409.         end
  410.         slot = slot + 1
  411.     end
  412.     return true
  413. end
  414. function avance()
  415.     if is_inventory_full() then
  416.         clean_inventory()
  417.     end
  418.     while not robot.forward() do
  419.         smart_swing()
  420.     end
  421. end
  422. function line(i)
  423.     while i > 0 do
  424.         avance()
  425.         i = i - 1
  426.     end
  427. end
  428. function turn(bool)
  429.     if bool then
  430.         robot.turnRight()
  431.     else
  432.         robot.turnLeft()
  433.     end
  434. end
  435. function xor_bool(bool)
  436.     if bool then
  437.         return false
  438.     else
  439.         return true
  440.     end
  441. end
  442. function dig_slice(xmax, ymax)
  443.     local boule = false
  444.     local ymax_bkp = ymax
  445.    
  446.     while ymax > 0 do
  447.         line(xmax)
  448.         robot.turnLeft()
  449.         robot.turnLeft()
  450.         boule = xor_bool(boule)
  451.         if ymax == 1 and boule == true then
  452.             line(xmax)
  453.             robot.turnLeft()
  454.             robot.turnLeft()
  455.         elseif ymax ~= 1 then
  456.             move_up(1)
  457.         end
  458.         ymax = ymax - 1
  459.     end
  460.     move_down(ymax_bkp - 1)
  461. end
  462. function core(xmax, ymax, zmax, dir)
  463.     while zmax > 0 do
  464.         dig_slice(xmax, ymax)
  465.         if zmax > 1 and dir == "left" then
  466.             robot.turnLeft()
  467.             line(1)
  468.             robot.turnRight()
  469.         elseif zmax > 1 then
  470.             robot.turnRight()
  471.             line(1)
  472.             robot.turnLeft()
  473.         end
  474.         zmax = zmax - 1
  475.     end
  476. end
  477. function push_item_after_slot(from)
  478.     local slot = from + 1
  479.    
  480.     robot.select(from)
  481.     while slot < robot.inventorySize() and component.inventory_controller.getStackInInternalSlot(slot) do
  482.         slot = slot + 1
  483.     end
  484.     if slot < robot.inventorySize() and robot.transferTo(slot) then
  485.         return true
  486.     end
  487.     return false
  488. end
  489. function free_crafting_table()
  490.     local slot = 1
  491.     local data
  492.    
  493.     while slot <= 11 do
  494.         if slot ~= 4 and slot ~= 8 then
  495.             if component.inventory_controller.getStackInInternalSlot(slot) and not push_item_after_slot(slot) then
  496.                 return false
  497.             end
  498.         end
  499.         slot = slot + 1
  500.     end
  501.     return true
  502. end
  503. function craft_pickaxe()
  504.     local material
  505.     local i = 1
  506.    
  507.     if empty_cases_amount() <= 10 then
  508.         clean_inventory()
  509.     end
  510.     if empty_cases_amount() >= 10 and pickaxe_materials() and free_crafting_table() then
  511.         if select_item("minecraft:iron_ingot") then
  512.             material = "minecraft:iron_ingot"
  513.         elseif select_item("minecraft:cobblestone") then
  514.             material = "minecraft:cobblestone"
  515.         else
  516.             return false
  517.         end
  518.         if select_next_item(1, "minecraft:stick") and robot.transferTo(6, 1) and select_item("minecraft:stick") and (robot.select() ~= 6 or select_next_item(7, "minecraft:stick")) and robot.transferTo(10, 1) then
  519.             while i <= 3 do
  520.                 if not select_next_item(4, material) or not robot.transferTo(i, 1) then
  521.                     return false
  522.                 end
  523.                 i = i + 1
  524.             end
  525.             if material == "minecraft:iron_ingot" then
  526.                 return component.crafting.craft(), "minecraft:iron_pickaxe"
  527.             elseif material == "minecraft:iron_ingot" then
  528.                 return component.crafting.craft(), "minecraft:stone_pickaxe"
  529.             end
  530.             return component.crafting.craft(), nil
  531.         end
  532.     end
  533.     errno = "No crafing components or no empty space"
  534.     return false
  535. end
  536. function craft_sticks()
  537.     if not free_crafting_table() then
  538.         return false
  539.     end
  540.     if not select_item("minecraft:planks") and not craft_planks() then
  541.         return false
  542.     end
  543.     if not free_crafting_table() then
  544.         return false
  545.     end
  546.     if select_item("minecraft:planks") then
  547.         robot.transferTo(1, 1)
  548.         if select_next_item(3, "minecraft:planks") then
  549.             robot.transferTo(5, 1)
  550.         else
  551.             return false
  552.         end
  553.         return component.crafting.craft(1)
  554.     end
  555.     return false
  556.    
  557. end
  558. function craft_planks()
  559.     if not free_crafting_table() or not select_item("minecraft:log") then
  560.         return false
  561.     end
  562.     if robot.transferTo(1, 1) then
  563.         return component.crafting.craft(1)
  564.     end
  565.     return false
  566. end
  567. function pickaxe_materials()
  568.     local wood, material = false, false
  569.     local stick = 0
  570.     local data
  571.     local inv_size = robot.inventorySize()
  572.     local slot = 1
  573.    
  574.     while slot <= inv_size and (not wood or not material) do
  575.         data = component.inventory_controller.getStackInInternalSlot(slot)
  576.         if data and not wood and data.name == "minecraft:stick" then
  577.             stick = data.size + stick
  578.             if stick >= 2 then
  579.                 wood = true
  580.             end
  581.         elseif data and not material and (data.name == "minecraft:cobblestone" or data.name == "minecraft:iron_ingot") and data.size >= 3 then
  582.             material = true
  583.         end
  584.         slot = slot + 1
  585.     end
  586.     if material and not wood then
  587.         return craft_sticks()
  588.     elseif wood and material then
  589.         return true
  590.     end
  591.     return false
  592. end
  593. function empty_cases_amount()
  594.     local slot = 1
  595.     local empty_slots = 0
  596.     local size = robot.inventorySize()
  597.    
  598.     while slot <= size do
  599.         if not component.inventory_controller.getStackInInternalSlot(slot) then
  600.             empty_slots = empty_slots + 1
  601.         end
  602.         slot = slot + 1
  603.     end
  604.     return empty_slots
  605. end
  606. function select_empty_case()
  607.     local slot = 1
  608.  
  609.     while slot <= robot.inventorySize() do
  610.         if not component.inventory_controller.getStackInInternalSlot(slot) then
  611.             robot.select(slot)
  612.             return true
  613.         end
  614.         slot = slot + 1
  615.     end
  616.     return false
  617. end
  618.  
  619. local arg = {...}
  620. if not arg or not arg[1] or not arg[2] or not arg[3] or not arg[4] or arg[4] ~= "left" and arg[4] ~= "right" then
  621.     print("mic [x max] [y max] [z max] [left or right]")
  622.     os.exit()
  623. end
  624. init()
  625. core(tonumber(arg[1]), tonumber(arg[2]), tonumber(arg[3]), arg[4])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement