Advertisement
Link712011

Tree Farm

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