Advertisement
ItsNoah

TurtleFleetCopy

Jun 10th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------
  2. -- Update --
  3. ------------
  4. local git_path = "https://raw.githubusercontent.com/Calame321/TurtleFleet/main/"
  5.  
  6. function update_master()
  7.     while turtle.suckDown( 1 ) do
  8.         turtle.place()
  9.         peripheral.call( "front", "turnOn" )
  10.  
  11.         local valid_signal = false
  12.  
  13.         while not valid_signal do
  14.             os.pullEvent( "redstone" )
  15.  
  16.             if rs.getAnalogueInput( "front", 1 ) then
  17.                 valid_signal = true
  18.             end
  19.         end
  20.  
  21.         turtle.dig()
  22.         turtle.dropUp()
  23.     end
  24.  
  25.     turtle.forward()
  26.     update()
  27. end
  28.  
  29. function update()
  30.     fs.delete( "startup" )
  31.     fs.delete( "TurtleFleet" )
  32.     get_file_from_github( git_path .. "Turtle/advanced_turtle.lua","TurtleFleet/Turtle/advanced_turtle.lua" )
  33.     get_file_from_github( git_path .. "Stations/station.lua","TurtleFleet/Stations/station.lua" )
  34.     get_file_from_github( git_path .. "Stations/treefarm.lua","TurtleFleet/Stations/treefarm.lua" )
  35.     get_file_from_github( git_path .. "startup.lua", "startup" )
  36.  
  37.     rs.setAnalogueOutput( "back", 1 )
  38.     os.sleep( 0.05 )
  39.     rs.setAnalogueOutput( "back", 0 )
  40. end
  41.  
  42. function get_file_from_github( url, file_path )
  43.     local f = fs.open( file_path, "w" )
  44.     local w = http.get( url )
  45.     f.write( w.readAll() )
  46.     f.flush()
  47.     f.close()
  48. end
  49.  
  50. local all_files = {}
  51. all_files[ 1 ] = "TurtleFleet/Turtle/advanced_turtle.lua"
  52. all_files[ 2 ] = "TurtleFleet/Stations/station.lua"
  53. all_files[ 3 ] = "TurtleFleet/Stations/treefarm.lua"
  54.  
  55. for i = 1, #all_files do
  56.     if not fs.exists( all_files[ i ] ) then
  57.         print( "Updating..." )
  58.         update()
  59.         os.reboot()
  60.     end
  61. end
  62.  
  63. ------------
  64. -- config --
  65. ------------
  66. shell.run( "TurtleFleet/Turtle/advanced_turtle.lua" )
  67. Station = dofile( "TurtleFleet/Stations/station.lua" )
  68. TreeFarm = dofile( "TurtleFleet/Stations/treefarm.lua" )
  69.  
  70. -- region
  71. local chunk_per_region = 5 --from center
  72.  
  73. -- Mining
  74. local branch_mine_length = 16 * chunk_per_region
  75.  
  76. -----------
  77. -- Const --
  78. -----------
  79. local SIDES = redstone.getSides()
  80.  
  81. ----------------------------
  82. -- global helper function --
  83. ----------------------------
  84.  
  85. function mysplit( str, sep )
  86.     if sep == nil then
  87.         sep = "%s"
  88.     end
  89.  
  90.     local t = {}
  91.  
  92.     for str in string.gmatch( str, "([^"..sep.."]+)" ) do
  93.         table.insert(t, str)
  94.     end
  95.  
  96.     return t
  97. end
  98.  
  99. function has_value( table, val )
  100.     for i = 1, #table do
  101.         if tostring( table[ i ] ) == tostring( val ) then
  102.             return true
  103.         end
  104.     end
  105.  
  106.     return false
  107. end
  108.  
  109. --------------
  110. -- Settings --
  111. --------------
  112. local map = {}
  113.  
  114. function load_settings()
  115.     if not fs.exists( "map" ) then
  116.         local file = fs.open( "map", "w" )
  117.         file.close()
  118.     end
  119.  
  120.     for line in io.lines( "map" ) do
  121.         if line ~= "" then
  122.             local l = mysplit( line )
  123.             map_add( vector.new( l[ 1 ], l[ 2 ], l[ 3 ] ), l[ 4 ] )
  124.         end
  125.     end
  126. end
  127.  
  128. function save_map()
  129.     local file = fs.open( "map", "w" )
  130.  
  131.     for x, kx in pairs( map ) do
  132.         for y, ky in pairs( kx ) do
  133.             for z, kz in pairs( ky ) do
  134.                 file.writeLine( tostring( x ) .. " " .. tostring( y ) .. " " .. tostring( z ) .. " " .. kz )
  135.             end
  136.         end
  137.     end
  138.  
  139.     file.flush()
  140.     file.close()
  141. end
  142.  
  143. --function log( text )
  144.     --local file = fs.open( "logs", "a" )
  145.     --file.writeLine( text )
  146.     --file.close()
  147. --end
  148.  
  149. ---------
  150. -- Map --
  151. ---------
  152. function map_remove( pos )
  153.     if not map[ pos.x ] or
  154.        not map[ pos.x ][ pos.y ] or
  155.        not map[ pos.x ][ pos.y ][ pos.z ] then
  156.         return
  157.     end
  158.  
  159.     table.remove( map[ pos.x ][ pos.y ], pos.z )
  160. end
  161.  
  162. function map_add( pos, block_name )
  163.     if not map[ pos.x ] then
  164.         map[ pos.x ] = {}
  165.     end
  166.  
  167.     if not map[ pos.x ][ pos.y ] then
  168.         map[ pos.x ][ pos.y ] = {}
  169.     end
  170.  
  171.     print( block_name .. " added for " .. tostring( pos ) )
  172.  
  173.     map[ pos.x ][ pos.y ][ pos.z ] = block_name
  174. end
  175.  
  176. function map_get( pos )
  177.     -- If a value is not set, return nil
  178.     if not map[ pos.x ] or
  179.        not map[ pos.x ][ pos.y ] or
  180.        not map[ pos.x ][ pos.y ][ pos.z ] then
  181.         return nil
  182.     end
  183.  
  184.     return map[ pos.x ][ pos.y ][ pos.z ]
  185. end
  186.  
  187. --------------------
  188. -- A* Pathfinding --
  189. --------------------
  190. local openSet = { start }
  191. local closedSet = {}
  192. local came_from = {}
  193. local gScore = {}
  194. local fScore = {}
  195. local sleep_counter = 0
  196.  
  197. local neibourgh_pos = {
  198.     vector.new(  1,  0,  0 ),
  199.     vector.new( -1,  0,  0 ),
  200.     vector.new(  0,  1,  0 ),
  201.     vector.new(  0, -1,  0 ),
  202.     vector.new(  0,  0,  1 ),
  203.     vector.new(  0,  0, -1 ),
  204. }
  205.  
  206. function reconstruct_path( cameFrom, current )
  207.     total_path = { current }
  208.     while came_from[ current ] do
  209.         current = came_from[ current ]
  210.         table.insert( total_path, 1, current )
  211.     end
  212.     return total_path
  213. end
  214.  
  215. function neibourgh( coord )
  216.     local valid_n = {}
  217.  
  218.     for c = 1, #neibourgh_pos do
  219.         local n_coord = neibourgh_pos[ c ] + coord
  220.         local m = map_get( n_coord )
  221.  
  222.         if m == nil then
  223.             table.insert( valid_n, n_coord )
  224.         end
  225.     end
  226.  
  227.     return valid_n
  228. end
  229.  
  230. function distance_to( v1, v2 )
  231.     local v3 = v1 - v2
  232.     return math.abs( v3.x ) + math.abs( v3.y ) + math.abs( v3.z )
  233. end
  234.  
  235. function get_lowest_f()
  236.     local lowest_f = 9999999999;
  237.     local lowest_node = nil
  238.  
  239.     for o = 1, #openSet do
  240.         local node = openSet[ o ]
  241.         local f = fScore[ node ]
  242.  
  243.         if f < lowest_f then
  244.             lowest_f = f
  245.             lowest_node = node
  246.         end
  247.     end
  248.  
  249.     return lowest_node
  250. end
  251.  
  252. -- A* finds a path from start to goal.
  253. function A_Star( start, goal )
  254.     -- G = Distance from starting node.
  255.     -- H = Distance from end node. ( distance_to( v1, v2 ) )
  256.     -- F = G + H
  257.     openSet = { start } -- array
  258.     closedSet = {} -- array
  259.     came_from = {} -- dictionnary
  260.     gScore = {} -- dictionnary
  261.     fScore = {} -- dictionnary
  262.  
  263.     gScore[ start ] = 0
  264.     fScore[ start ] = distance_to( start, goal )
  265.  
  266.     while table.getn( openSet ) ~= 0 do
  267.         -- avoid 'too long without yeilding'
  268.         if sleep_counter == 20 then
  269.             os.sleep( 0.05 )
  270.             sleep_counter = 0
  271.         end
  272.         sleep_counter = sleep_counter + 1
  273.  
  274.         current = get_lowest_f()
  275.  
  276.         if tostring( current ) == tostring( goal ) then
  277.             return reconstruct_path( came_from, current )
  278.         end
  279.  
  280.         -- Adding current to closed set
  281.         for o = 1, #openSet do
  282.             if tostring( openSet[ o ] ) == tostring( current ) then
  283.                 table.insert( closedSet, current )
  284.                 table.remove( openSet, o )
  285.                 break
  286.             end
  287.         end
  288.  
  289.         local n = neibourgh( current )
  290.  
  291.         -- Log the neibourgh
  292.         local str_n = "Neibourgh: "
  293.         for c = 1, #n do
  294.             str_n = str_n .. tostring( n[ c ] ) .. " | "
  295.         end
  296.  
  297.         for c = 1, #n do
  298.             if not has_value( closedSet, n[ c ] ) then
  299.                 came_from[ n[ c ] ] = current
  300.                 gScore[ n[ c ] ] = gScore[ current ] + 1
  301.                 fScore[ n[ c ] ] = gScore[ n[ c ] ] + distance_to( n[ c ], goal )
  302.  
  303.                 if not has_value( openSet, n[ c ] ) then
  304.                     table.insert( openSet, n[ c ] )
  305.                 end
  306.             end
  307.         end
  308.     end
  309.  
  310.     -- Open set is empty but goal was never reached
  311.     print( "No path found!" )
  312.     return {}
  313. end
  314.  
  315.  
  316.  
  317. ----------------
  318. -- Decoration --
  319. ----------------
  320. function place_floor( direction )
  321.     direction = direction or "down"
  322.     print( "Place floor block in firt slot." )
  323.     print( "Press a key when ready." )
  324.     read()
  325.  
  326.     local floor_block = turtle.getItemDetail( 1 ).name
  327.     local can_continue = true
  328.     local rightTurn = true
  329.  
  330.     while can_continue do
  331.         turtle.digDir( direction )
  332.        
  333.         local block_index = turtle.get_item_index( floor_block )
  334.  
  335.         if block_index == -1 then
  336.             print( "Give me more block please!" )
  337.  
  338.             while block_index == -1 do
  339.                 os.sleep( 1 )
  340.                 block_index = turtle.get_item_index( floor_block )
  341.             end
  342.         end
  343.  
  344.         turtle.select( block_index )
  345.         turtle.placeDir( direction )
  346.  
  347.         if not move( "forward", "minecraft:torch" ) or turtle.is_block_name( "down", floor_block ) then
  348.             if rightTurn then
  349.                 turtle.turnRight()
  350.             else
  351.                 turtle.turnLeft()
  352.             end
  353.            
  354.             if not move( "forward", "minecraft:torch" ) then
  355.                 can_continue = false
  356.             end
  357.  
  358.             if rightTurn then
  359.                 turtle.turnRight()
  360.                 rightTurn = false
  361.             else
  362.                 turtle.turnLeft()
  363.                 rightTurn = true
  364.             end
  365.         end
  366.     end
  367. end
  368.  
  369.  
  370. function place_wall()
  371.     print( "Place floor block in firt slot." )
  372.     print( "Press a key when ready." )
  373.     read()
  374.  
  375.     local wall_block = turtle.getItemDetail( 1 ).name
  376.     local direction = "up"
  377.  
  378.     while true do
  379.         repeat
  380.             turtle.try_refuel()
  381.             turtle.dig_all( "forward" )
  382.             turtle.select( get_item_index( wall_block ) )
  383.             turtle.place()
  384.             moveDir[ direction ]()
  385.         until detectDir[ direction ]()
  386.  
  387.         turtle.dig_all( "forward" )
  388.         turtle.select( get_item_index( wall_block ) )
  389.         turtle.place()
  390.         turtle.turnRight()
  391.  
  392.         if turtle.detect() then
  393.             return
  394.         end
  395.  
  396.         turtle.forward()
  397.         turtle.turnLeft()
  398.  
  399.         if direction == "up" then
  400.             direction = "down"
  401.         else
  402.             direction = "up"
  403.         end
  404.     end
  405. end
  406.  
  407. local initial_aditionnal_up = 5
  408. local last_average_height = 10
  409. local aditionnal_up = 5
  410. local last_height = 0
  411. local height = 0
  412. local torch_counter = 0
  413. local flat_stuff_to_keep = {}
  414. flat_stuff_to_keep[ "minecraft:coal" ] = 1
  415. flat_stuff_to_keep[ "minecraft:charcoal" ] = 1
  416. flat_stuff_to_keep[ "minecraft:torch" ] = 1
  417. flat_stuff_to_keep[ "minecraft:dirt" ] = 2
  418.  
  419.  
  420. function flat_one()
  421.     replace_for_dirt()
  422.     height = 0
  423.     last_height = 0
  424.  
  425.     dig_all_up()
  426.     turtle.force_forward()
  427.     turtle.force_forward()
  428.     dig_all_up()
  429.  
  430.     -- change last height based on the height
  431.     if last_height < last_average_height then
  432.         last_average_height = last_average_height - 1
  433.     else
  434.         last_average_height = last_height
  435.     end
  436.  
  437.     for h = 1, height do
  438.         turtle.force_down()
  439.         turtle.dig()
  440.     end
  441.  
  442.     turtle.force_back()
  443.     replace_for_dirt()
  444.     turtle.force_forward()
  445.     replace_for_dirt()
  446.     turtle.force_forward()
  447.     replace_for_dirt()
  448. end
  449.  
  450. function dig_all_up()
  451.     -- dig up until no more block up or average height reached
  452.     while must_go_up() do
  453.         height = height + 1
  454.         if turtle.detectUp() then
  455.             last_height = height
  456.         end
  457.         turtle.dig()
  458.         turtle.force_up()
  459.     end
  460. end
  461.  
  462.  
  463. function must_go_up()
  464.     if turtle.detect() or turtle.detectUp() or height < last_average_height then
  465.         aditionnal_up = initial_aditionnal_up
  466.         return true
  467.     end
  468.  
  469.     if aditionnal_up > 0 then
  470.         aditionnal_up = aditionnal_up - 1
  471.         return true
  472.     end
  473.  
  474.     aditionnal_up = initial_aditionnal_up
  475.     return false
  476. end
  477.  
  478. function replace_for_dirt()
  479.     if not turtle.is_block_name( "down", "minecraft:grass_block" ) and not turtle.is_block_name( "down", "minecraft:dirt" ) then
  480.         local dirt_index = turtle.get_item_index( "minecraft:dirt" )
  481.  
  482.         if dirt_index > 0 then
  483.             turtle.select( dirt_index )
  484.             turtle.digDown()
  485.             turtle.placeDown()
  486.             turtle.select( 1 )
  487.         end
  488.     end
  489. end
  490.  
  491. function flaten_chunk()
  492.     turtle.force_forward()
  493.     turtle.turnRight()
  494.  
  495.     for x = 1, 16 do
  496.         for y = 1, 4 do
  497.             flat_one()
  498.            
  499.             if turtle.is_inventory_full() then
  500.                 turtle.drop_in_enderchest( flat_stuff_to_keep )
  501.             end
  502.            
  503.             if y < 4 then turtle.force_forward() end
  504.             flat_place_torch()
  505.         end
  506.  
  507.         -- dont need to change row if at the end
  508.         if x < 16 then
  509.             if x % 2 == 0 then
  510.                 turtle.turnRight()
  511.             else
  512.                 turtle.turnLeft()
  513.             end
  514.  
  515.             turtle.force_forward()
  516.  
  517.             if x % 2 == 0 then
  518.                 turtle.turnRight()
  519.             else
  520.                 turtle.turnLeft()
  521.             end
  522.         end
  523.     end
  524.  
  525.     turtle.turnRight()
  526. end
  527.  
  528. function flat_place_torch()
  529.     -- Place a torch
  530.     if torch_counter == 5 then
  531.         torch_counter = 0
  532.  
  533.         local torch_index = turtle.get_item_index( "minecraft:torch" )
  534.  
  535.         if torch_index > 0 then
  536.             turtle.select( torch_index )
  537.             turtle.turn180()
  538.             turtle.place()
  539.             turtle.turn180()
  540.             turtle.select( 1 )
  541.         end
  542.     else
  543.         torch_counter = torch_counter + 1
  544.     end
  545. end
  546.  
  547. function flaten_chunks( number_of_chunk )
  548.     for c = 1, number_of_chunk do
  549.         flaten_chunk()
  550.     end
  551. end
  552.  
  553. ------------
  554. -- Mining --
  555. ------------
  556. function vein_mine( from, block )
  557.     -- up
  558.     if turtle.is_block_name( "up", block ) then
  559.         turtle.force_move( "up" )
  560.         vein_mine( "up", block )
  561.     end
  562.  
  563.     -- forward
  564.     if turtle.is_block_name( "forward", block ) then
  565.         turtle.force_forward()
  566.         vein_mine( "forward", block )
  567.     end
  568.  
  569.     -- down
  570.     if turtle.is_block_name( "down", block ) then
  571.         turtle.force_down()
  572.         vein_mine( "down", block )
  573.     end
  574.    
  575.     -- left
  576.     turtle.turnLeft()
  577.  
  578.     if turtle.is_block_name( "forward", block ) then
  579.         turtle.force_forward()
  580.         vein_mine( "forward", block )
  581.     end
  582.  
  583.     -- right
  584.     turtle.turn180()
  585.    
  586.     if turtle.is_block_name( "forward", block ) then
  587.         turtle.force_forward()
  588.         vein_mine( "forward", block )
  589.     end
  590.  
  591.     turtle.turnLeft()
  592.     turtle.move_reverse( from )
  593. end
  594.  
  595.  
  596. function dig_out( depth, width )
  597.     turtle.force_forward()
  598.     turtle.turnRight()
  599.  
  600.     for x = 1, depth do
  601.         for y = 1, width - 1 do
  602.             turtle.dig_all( "up" )
  603.             turtle.dig_all( "down" )
  604.             turtle.force_forward()
  605.         end
  606.  
  607.         turtle.dig_all( "up" )
  608.         turtle.dig_all( "down" )
  609.        
  610.         -- dont need to change row if at the end
  611.         if x < depth then
  612.             if x % 2 == 0 then
  613.                 turtle.turnRight()
  614.                 turtle.force_forward()
  615.                 turtle.turnRight()
  616.             else
  617.                 turtle.turnLeft()
  618.                 turtle.force_forward()
  619.                 turtle.turnLeft()
  620.             end
  621.            
  622.             turtle.dig_all( "up" )
  623.             turtle.dig_all( "down" )
  624.         end
  625.     end
  626. end
  627.  
  628. function check_ore( direction )
  629.     local ore_tag = "forge:ores"
  630.    
  631.     if turtle.is_block_tag( direction, ore_tag ) then
  632.         local success, data = turtle.inspectDir( direction )
  633.         local ore_name = data.name
  634.  
  635.         for b = 1, #DO_NOT_MINE do
  636.             if ore_name == DO_NOT_MINE[ b ] then
  637.                 return false
  638.             end
  639.         end
  640.  
  641.         turtle.force_move( direction )
  642.         vein_mine( direction, ore_name )
  643.     end
  644.  
  645.     return true
  646. end
  647.  
  648. function mine_branch()
  649.     local found_forbidden_ore = false
  650.     local depth = 0
  651.  
  652.     for i = 1, branch_mine_length do
  653.         depth = depth + 1
  654.         turtle.force_forward()
  655.  
  656.         if not check_ore( "up" ) then found_forbidden_ore = true end
  657.         if not check_ore( "down" ) then found_forbidden_ore = true end
  658.         turtle.turnLeft()
  659.         if not check_ore( "forward" ) then found_forbidden_ore = true end
  660.         turtle.turn180()
  661.         if not check_ore( "forward" ) then found_forbidden_ore = true end
  662.         turtle.turnLeft()
  663.  
  664.         if found_forbidden_ore then print( "FOUND DO_NOT_MINE ORE !!!!" ) break end
  665.     end
  666.  
  667.     for i = 0, depth - 1 do
  668.         turtle.force_move( "back" )
  669.  
  670.         if found_forbidden_ore then
  671.             turtle.digDown()
  672.         end
  673.     end
  674.  
  675.     return found_forbidden_ore
  676. end
  677.  
  678. function empty_inventory()
  679.     for i = 1, 16 do
  680.         local slot = turtle.getItemDetail( i )
  681.  
  682.         if slot and not turtle.is_valid_fuel( slot.name ) then
  683.             turtle.select( i )
  684.            
  685.             if not turtle.drop() then
  686.                 print( "Please, make some place in the chest !!" )
  687.  
  688.                 while not turtle.drop() do
  689.                     os.sleep( 10 )
  690.                 end
  691.             end
  692.         end
  693.     end
  694. end
  695.  
  696. function branch_mining( side )
  697.     local branch_index = 0
  698.  
  699.     for b = 1, branch_mine_length / 4 do
  700.         turtle.turn180()
  701.  
  702.         for i = 1, ( branch_index * 4 ) do
  703.             turtle.force_forward()
  704.         end
  705.  
  706.         if side == "left" then turtle.turnLeft() else turtle.turnRight() end
  707.  
  708.         mine_branch()
  709.  
  710.         if side == "left" then turtle.turnLeft() else  turtle.turnRight() end
  711.        
  712.         for i = 1, ( branch_index * 4 ) do
  713.             turtle.force_forward()
  714.         end
  715.  
  716.         empty_inventory()
  717.         branch_index = branch_index + 1
  718.     end
  719. end
  720.  
  721. local mining_state = "going_down"
  722. local mine_start_position
  723. local mine_level = 6
  724. local mine_setup = false
  725. local mine_layer = 1
  726. local mine_direction = 0
  727.  
  728.  
  729. function setup_mine( mine_position )
  730.     mine_start_position = mine_position
  731.     save_mine()
  732. end
  733.  
  734. function get_mine_y()
  735.     return ( mine_layer * 2 ) + 4
  736. end
  737.  
  738. function get_branch_entrance_pos( branch_index )
  739.     local x = mine_start_position.x + ( ( ( ( mine_layer % 2 ) * 2 ) + 2 ) * ( mine_direction % 2 ) )
  740.     local y = get_mine_y()
  741.     local z = mine_start_position.z + ( ( ( ( mine_layer % 2 ) * 2 ) + 2 ) * ( ( 1 + mine_direction ) % 2 ) )
  742. end
  743.  
  744. function mine()
  745.     load_mine()
  746.  
  747.     if not mine_setup then
  748.         print( "Need to setup the mine." )
  749.         print( "My pos = " .. tostring( pos.coords ) )
  750.         print( "Mine pos = " .. tostring( mine_start_position ) )
  751.         go_to_mine_start()
  752.         turtle.turn( NORTH )
  753.         dig_mine_shaft()
  754.         go_to_output_chest()
  755.         turtle.turn( WEST )
  756.         drop_inventory()
  757.         mine_setup = true
  758.         save_mine()
  759.     end
  760.  
  761.     go_to_mine_start()
  762.     go_down_the_mine()
  763.     turtle.turn( mine_direction )
  764.     find_next_branch()
  765.     --branch_mine()
  766. end
  767.  
  768. function find_next_branch()
  769.     mining_state = "find_next_branch"
  770.     local branch_index = 0
  771.  
  772.     while true do
  773.         -- TODO: Force goto
  774.         turtle.pathfind_to( get_branch_entrance_pos( branch_index ), true )
  775.         turtle.turn( LEFT )
  776.  
  777.         local s, d = turtle.inspect()
  778.  
  779.         if ( not s or d.name ~= "minecraft:cobblestone" ) then
  780.             return true
  781.         end
  782.  
  783.         turtle.turn( RIGHT )
  784.         branch_index = branch_index + 1
  785.  
  786.         if branch_index * 4 >= branch_mine_length then
  787.             return false
  788.         end
  789.     end  
  790.  
  791.     return false
  792. end
  793.  
  794. function go_to_mine_start()
  795.     turtle.pathfind_to( mine_start_position, false )
  796. end
  797.  
  798. function go_to_output_chest()
  799.     local mine_output_position = vector.new( mine_start_position.x, mine_start_position.y, mine_start_position.z - 1 )
  800.     turtle.pathfind_to( mine_output_position, false )
  801. end
  802.  
  803. function dig_mine_shaft()
  804.     turtle.turn( NORTH )
  805.     for i = 1, 58 do
  806.         turtle.force_move( "down" )
  807.         turtle.dig()
  808.     end
  809. end
  810.  
  811. function go_down_the_mine()
  812.     mining_state = "going_down"
  813.     save_mine()
  814.     local mine_level_position = vector.new( mine_start_position.x, 6, mine_start_position.z )
  815.     turtle.pathfind_to( mine_level_position, false )
  816. end
  817.  
  818. function drop_inventory()
  819.     mining_state = "drop_inventory"
  820.     save_mine()
  821.  
  822.     for i = 1, 16 do
  823.         local item = turtle.getItemDetail( i )
  824.         if item and item.count > 0 then
  825.             turtle.select( i )
  826.  
  827.             if item.name == "minecraft:coal" or item.name == "minecraft:charcoal" then
  828.                 turtle.dropUp()
  829.             else
  830.                 local chest_has_place = turtle.drop()
  831.  
  832.                 while not chest_has_place do
  833.                     os.sleep( 5 )
  834.                     chest_has_place = turtle.drop()
  835.                 end
  836.             end
  837.         end
  838.     end
  839.  
  840.     turtle.select( 1 )
  841.     turtle.suckUp()
  842. end
  843.  
  844. function load_mine()
  845.     if not fs.exists( "mine" ) then
  846.         local file = fs.open( "mine", "w" )
  847.         file.close()
  848.     end
  849.  
  850.     local file = fs.open( "mine", "r" )
  851.     mining_state = file.readLine()
  852.     local start_pos_split = mysplit( file.readLine() )
  853.     mine_start_position = vector.new( start_pos_split[ 1 ], start_pos_split[ 2 ], start_pos_split[ 3 ] )
  854.     mine_level = tonumber( file.readLine() )
  855.     mine_setup = "true" == file.readLine()
  856.     mine_layer = tonumber( file.readLine() )
  857. end
  858.  
  859. function save_mine()
  860.     local file = fs.open( "mine", "w" )
  861.     file.writeLine( mining_state )
  862.     print( "Save mine pos: " .. tostring( mine_start_position ) )
  863.     file.writeLine( tostring( mine_start_position.x ) .. " " .. tostring( mine_start_position.y ) .. " " .. tostring( mine_start_position.z ) )
  864.     file.writeLine( tostring( mine_level ) )
  865.     file.writeLine( tostring( mine_setup ) )
  866.     file.writeLine( tostring( mine_layer ) )
  867.     file.flush()
  868.     file.close()
  869. end
  870.  
  871. -------------
  872. -- Farming --
  873. -------------
  874. function rice_farm()
  875.     while true do
  876.         turtle.forward()
  877.         turtle.turnRight()
  878.  
  879.         for x = 1, 16 do
  880.             for y = 1, 15 do
  881.                 local has_rice, rice = turtle.inspectDown()
  882.  
  883.                 if has_rice and rice.state.age == 3 then
  884.                     turtle.digDown()
  885.                 end
  886.  
  887.                 turtle.forward()
  888.             end
  889.  
  890.             local has_rice, rice = turtle.inspectDown()
  891.             if has_rice and rice.state.age == 3 then
  892.                 turtle.digDown()
  893.             end
  894.  
  895.             -- dont need to change row if at the end
  896.             if x < 16 then
  897.                 if x % 2 == 0 then
  898.                     turtle.turnRight()
  899.                     turtle.force_forward()
  900.                     turtle.turnRight()
  901.                 else
  902.                     turtle.turnLeft()
  903.                     turtle.force_forward()
  904.                     turtle.turnLeft()
  905.                 end
  906.             else
  907.                 turtle.turnLeft()
  908.  
  909.                 for i = 1, 16 do
  910.                     turtle.forward()
  911.                 end
  912.  
  913.                 turtle.turn180()
  914.  
  915.                 local rice_index = get_item_index( "rice_panicle" )
  916.                 while rice_index > 0 do
  917.                     turtle.select( rice_index )
  918.                     if not turtle.dropDown() then
  919.                         print( "The chest is full..." )
  920.                         read()
  921.                     end
  922.                     rice_index = get_item_index( "rice_panicle" )
  923.                 end
  924.             end
  925.         end
  926.  
  927.         os.sleep( 120 )
  928.     end
  929. end
  930.  
  931. function cane_farm()
  932.     while true do
  933.         turtle.force_forward()
  934.         turtle.turnRight()
  935.  
  936.         for x = 1, 16 do
  937.             for y = 1, 15 do
  938.                 if turtle.is_block_name( "down", "minecraft:sugar_cane" ) then
  939.                     turtle.digDown()
  940.                 end
  941.  
  942.                 turtle.force_forward()
  943.             end
  944.  
  945.             if turtle.is_block_name( "down", "minecraft:sugar_cane" ) then
  946.                 turtle.digDown()
  947.             end
  948.  
  949.             -- dont need to change row if at the end
  950.             if x < 16 then
  951.                 if x % 2 == 0 then
  952.                     turtle.turnRight()
  953.                     turtle.force_forward()
  954.                     turtle.turnRight()
  955.                 else
  956.                     turtle.turnLeft()
  957.                     turtle.force_forward()
  958.                     turtle.turnLeft()
  959.                 end
  960.             else
  961.                 turtle.turnLeft()
  962.  
  963.                 for i = 1, 16 do
  964.                     turtle.wait_forward()
  965.                 end
  966.  
  967.                 turtle.turn180()
  968.  
  969.                 local index = get_item_index( "sugar_cane" )
  970.                 while index > 0 do
  971.                     turtle.select( index )
  972.                     if not turtle.dropDown() then
  973.                         print( "The chest is full..." )
  974.                         read()
  975.                     end
  976.                     index = get_item_index( "sugar_cane" )
  977.                 end
  978.             end
  979.         end
  980.  
  981.         os.sleep( 222 )
  982.     end
  983. end
  984.  
  985. --------------
  986. -- Coocking --
  987. --------------
  988. local coocking_time = 10
  989. local coal_burn_time = 80
  990.  
  991. local furnace_fuel_ammount = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }
  992.  
  993. function fill_inv()
  994.     if not turtle.suck() then
  995.         print( "The inventory is empty..." )
  996.  
  997.         while not turtle.suck() do
  998.             os.sleep( 60 )
  999.         end
  1000.     end
  1001.  
  1002.  
  1003.     while turtle.suck() do end
  1004.  
  1005.     local item_in_inv = 0
  1006.     for i = 1, 16 do
  1007.         item_in_inv = item_in_inv + turtle.getItemCount( i )
  1008.     end
  1009.  
  1010.     turtle.drop( item_in_inv % 16 )
  1011.     return math.floor( item_in_inv / 16 )
  1012. end
  1013.  
  1014. function drop_remaining_items()
  1015.     if turtle.has_items() then
  1016.         for i = 1, 16 do
  1017.             if turtle.getItemCount( i ) > 0 then
  1018.                 turtle.select( i )
  1019.                 while not turtle.drop() do
  1020.                     os.sleep( 5 )
  1021.                 end
  1022.             end
  1023.         end
  1024.     end
  1025. end
  1026.  
  1027. function refuel_furnace()
  1028.     turtle.turnLeft()
  1029.     turtle.select( 1 )
  1030.     turtle.suck()
  1031.  
  1032.     local need_refuel = turtle.getItemCount( 1 ) < 32
  1033.     turtle.drop()
  1034.  
  1035.     if need_refuel then
  1036.         print( "Refuelling the furnaces.")
  1037.         turtle.turn180()
  1038.         turtle.select( 1 )
  1039.  
  1040.         -- suck all the fuel possible
  1041.         local fuel_to_transfer = fill_inv()
  1042.  
  1043.         if each_fuel == 0 then
  1044.             error( "Not enough fuel !" )
  1045.         end
  1046.  
  1047.         turtle.turnLeft()
  1048.         turtle.forward()
  1049.         turtle.turnLeft()
  1050.  
  1051.         for i = 1, 16 do
  1052.             turtle.forward()
  1053.             turtle.turnLeft()
  1054.            
  1055.             for a = 1, 2 do
  1056.                 turtle.select( get_item_index( "coal" ) )
  1057.                 turtle.transferTo( 16 )
  1058.             end
  1059.  
  1060.             turtle.select( 16 )
  1061.             turtle.drop( fuel_to_transfer )
  1062.             turtle.turnRight()
  1063.         end
  1064.  
  1065.         for i = 1, 16 do
  1066.             turtle.back()
  1067.         end
  1068.  
  1069.         turtle.turnRight()
  1070.         turtle.back()
  1071.  
  1072.         turtle.turnRight()
  1073.         drop_remaining_items()
  1074.         turtle.turnLeft()
  1075.     else
  1076.         turtle.turnRight()
  1077.     end
  1078. end
  1079.  
  1080. function insert_ingerdient()
  1081.     turtle.up()
  1082.     turtle.select( 1 )
  1083.     local item_to_insert = fill_inv()
  1084.     local item = turtle.getItemDetail()
  1085.     turtle.turnLeft()
  1086.    
  1087.     for i = 1, 16 do
  1088.         turtle.forward()
  1089.         for x = 1, 16 do
  1090.             if turtle.getItemCount( x ) > 0 then
  1091.                 turtle.select( x )
  1092.                 turtle.dropDown( item_to_insert )
  1093.             end
  1094.         end
  1095.     end
  1096.  
  1097.     for i = 1, 16 do
  1098.         turtle.back()
  1099.     end
  1100.  
  1101.     turtle.turnRight()
  1102.     drop_remaining_items()
  1103.     turtle.down()
  1104. end
  1105.  
  1106. function empty_furnace()
  1107.     turtle.down()
  1108.     turtle.turnLeft()
  1109.    
  1110.     for i = 1, 16 do
  1111.         turtle.forward()
  1112.         turtle.select( 1 )
  1113.         turtle.suckUp()
  1114.     end
  1115.  
  1116.     for i = 1, 16 do
  1117.         turtle.back()
  1118.     end
  1119.  
  1120.     turtle.turnRight()
  1121.     drop_remaining_items()
  1122.     turtle.up()
  1123. end
  1124.  
  1125. function check_own_fuel()
  1126.     if turtle.getFuelLevel() < 500 then
  1127.         turtle.turnRight()
  1128.         turtle.suck()
  1129.         turtle.refuel()
  1130.         turtle.turnLeft()
  1131.     end
  1132. end
  1133.  
  1134. function start_cooking()
  1135.     while true do
  1136.         check_own_fuel()
  1137.         refuel_furnace()
  1138.         empty_furnace()
  1139.         insert_ingerdient()
  1140.         os.sleep( 80 )
  1141.     end
  1142. end
  1143.  
  1144. ----------------
  1145. -- Fleet Mode --
  1146. ----------------
  1147. local flatten_length = 32
  1148. local is_last = false
  1149. local last_pos
  1150.  
  1151. function check_redstone_option()
  1152.     for s = 1, #SIDES do
  1153.         local redstone_option = rs.getAnalogueInput( SIDES[ s ] )
  1154.  
  1155.         if redstone_option == 7 then
  1156.             rs.setAnalogueOutput( "back", 7 )
  1157.             os.sleep( 0.1 )
  1158.             has_flaten_fleet_setup()
  1159.             rs.setAnalogueOutput( "back", 0 )
  1160.             fleet_flatten()
  1161.             return true
  1162.         elseif redstone_option == 6 then
  1163.             update()
  1164.             rs.setAnalogueOutput( "back", 1 )
  1165.             return true
  1166.         end
  1167.     end
  1168.  
  1169.     return false
  1170. end
  1171.  
  1172. function has_flaten_fleet_setup()
  1173.     local s, d = turtle.inspectUp()
  1174.     local has_chest_up = s and string.find( d.name, "chest" )
  1175.     s, d = turtle.inspectDown()
  1176.     local has_chest_down = s and string.find( d.name, "chest" )
  1177.  
  1178.     if has_chest_up and has_chest_down then
  1179.         for i = 1, 4 do
  1180.             s, d = turtle.inspect()
  1181.             local has_chest_front = s and string.find( d.name, "chest" )
  1182.  
  1183.             if has_chest_front then
  1184.                 turtle.set_position( 0, 0, 0, NORTH )
  1185.                 return true
  1186.             end
  1187.  
  1188.             turtle.turnLeft()
  1189.         end
  1190.  
  1191.         os.reboot()
  1192.     end
  1193.  
  1194.     return false
  1195. end
  1196.  
  1197. function fleet_flatten()
  1198.     turtle.suckUp()
  1199.    
  1200.     if not turtle.suck() then
  1201.         is_last = true
  1202.     end
  1203.  
  1204.     turtle.force_back()
  1205.  
  1206.     if is_last then
  1207.         local paper_index = turtle.get_item_index( "minecraft:paper" )
  1208.         if paper_index > 0 then
  1209.             local paper_detail = turtle.getItemDetail( paper_index, true )
  1210.             flatten_length = tonumber( paper_detail.displayName )
  1211.         end
  1212.    
  1213.     else
  1214.         place_mining_turtle()
  1215.     end
  1216.  
  1217.     turtle.turnLeft()
  1218.     goto_next_free_spot()
  1219.     turtle.turn180()
  1220.    
  1221.     if not is_last then
  1222.         wait_for_start_signal( "back", 10 )
  1223.     end
  1224.  
  1225.     rs.setAnalogueOutput( "front", 10 )
  1226.     os.sleep( 0.05 )
  1227.     rs.setAnalogueOutput( "front", 0 )
  1228.     turtle.turnRight()
  1229.  
  1230.     for y = 1, flatten_length / 4 do
  1231.         flat_one()
  1232.         --flat_empty_inventory()
  1233.         turtle.force_forward()
  1234.     end
  1235. end
  1236.  
  1237. function flat_empty_inventory()
  1238.     if turtle.is_inventory_full() then
  1239.         last_pos = turtle.position()
  1240.         turtle.pathfind_to( vector.new( 0, 0, 0 ), false )
  1241.    
  1242.         local has_fuel = false
  1243.         for i = 1, 16 do
  1244.             local item = turtle.getItemDetail( i )
  1245.  
  1246.             if item then
  1247.                 if item.name == "minecraft:charcoal" then
  1248.                     turtle.select( i )
  1249.                     turtle.suckUp( 64 - item.count )
  1250.                 else
  1251.                     turtle.select( i )
  1252.                     turtle.dropDown()
  1253.                 end
  1254.             end
  1255.         end
  1256.  
  1257.         turtle.pathfind_to( last_pos, false )
  1258.     end
  1259. end
  1260.  
  1261. function wait_for_start_signal( direction, strength )
  1262.     local valid_signal = false
  1263.  
  1264.     while not valid_signal do
  1265.         os.pullEvent( "redstone" )
  1266.  
  1267.         if rs.getAnalogueInput( direction, strength ) then
  1268.             valid_signal = true
  1269.         end
  1270.     end
  1271. end
  1272.  
  1273. function goto_next_free_spot()
  1274.     local found_spot = false
  1275.  
  1276.     while not found_spot do
  1277.         local s, d = turtle.inspectDown()
  1278.  
  1279.         if s and string.find( d.name, "turtle" ) then
  1280.             turtle.force_forward()
  1281.         else
  1282.             turtle.force_down()
  1283.             found_spot = true
  1284.         end
  1285.     end
  1286. end
  1287.  
  1288.  
  1289. function place_mining_turtle()
  1290.     turtle.select( turtle.get_item_index( "computercraft:turtle" ) )
  1291.     turtle.place()
  1292.     rs.setAnalogueOutput( "front", 7 )
  1293.    
  1294.     local paper_index = turtle.get_item_index( "minecraft:paper" )
  1295.     if paper_index > 0 then
  1296.         local paper_detail = turtle.getItemDetail( paper_index, true )
  1297.         flatten_length = tonumber( paper_detail.displayName )
  1298.         turtle.select( paper_index )
  1299.         turtle.drop()
  1300.         turtle.select( 1 )
  1301.     end
  1302.  
  1303.     os.sleep( 0.05 )
  1304.     peripheral.call( "front", "turnOn" )
  1305.     wait_for_start_signal( "front", 7 )
  1306.     rs.setAnalogueOutput( "front", 0 )
  1307.     os.sleep( 0.10 )
  1308. end
  1309.  
  1310. ----------
  1311. -- Menu --
  1312. ----------
  1313. function show_menu()
  1314.     term.clear()
  1315.     load_settings()
  1316.     print( "What should I do ?" )
  1317.     print( "1 - Tree Farm. [optionnal -> farm length]" )
  1318.     print( "2 - Vein Mine. [block name]" )
  1319.     print( "3 - Dig Out. [depth width]" )
  1320.     print( "4 - Place Floor. [ 'up' for ceiling ]" )
  1321.     print( "5 - Place Wall." )
  1322.     print( "6 - Mine Branch." )
  1323.     print( "7 - Flatten 16 x 16. [chunks qty, xtra height]" )
  1324.     print( "8 - Start cooking." )
  1325.     print( "9 - branch mining." )
  1326.     print( "10 - Farm [ 1 = rice, 2 = sugar_cane ]" )
  1327.     local input = read()
  1328.     local args = mysplit( input )
  1329.  
  1330.     if args[ 1 ] == "pos" then
  1331.         turtle.set_position( tonumber( args[ 2 ] ), tonumber( args[ 3 ] ), tonumber( args[ 4 ] ), tonumber( args[ 5 ] ) )
  1332.     elseif args[ 1 ] == "flatone" then
  1333.         flat_one()
  1334.     elseif args[ 1 ] == "goto" then
  1335.         turtle.pathfind_to( vector.new( tonumber( args[ 2 ] ), tonumber( args[ 3 ] ), tonumber( args[ 4 ] ) ), false )
  1336.     elseif args[ 1 ] == "setupMine" then
  1337.         setup_mine( vector.new( tonumber( args[ 2 ] ), tonumber( args[ 3 ] ), tonumber( args[ 4 ] ) ) )
  1338.     elseif args[ 1 ] == "mine" then
  1339.         mine()
  1340.     elseif args[ 1 ] == "update" then
  1341.         update_master()
  1342.     elseif args[ 1 ] == "1" then
  1343.         TreeFarm.start_tree_farm()
  1344.     elseif args[ 1 ] == "2" then
  1345.         vein_mine( "forward", args[ 2 ] )
  1346.     elseif args[ 1 ] == "3" then
  1347.         dig_out( tonumber( args[ 2 ] ), tonumber( args[ 3 ] ) )
  1348.     elseif args[ 1 ] == "4" then
  1349.         place_floor( args[ 2 ] )
  1350.     elseif args[ 1 ] == "5" then
  1351.         place_wall()
  1352.     elseif args[ 1 ] == "6" then
  1353.         mine_branch()
  1354.     elseif args[ 1 ] == "7" then
  1355.         local number_of_chunk = tonumber( args[ 2 ] )
  1356.         if number_of_chunk == nil then number_of_chunk = 1 end
  1357.         local extra_height = tonumber( args[ 3 ] )
  1358.         if extra_height ~= nil then
  1359.             last_average_height = extra_height
  1360.             initial_aditionnal_up = extra_height
  1361.         end
  1362.         if has_flaten_fleet_setup() then
  1363.             fleet_flatten()
  1364.         else
  1365.             flaten_chunks( number_of_chunk )
  1366.         end
  1367.     elseif args[ 1 ] == "8" then
  1368.         start_cooking()
  1369.     elseif args[ 1 ] == "9" then
  1370.         branch_mining( args[ 2 ] )
  1371.     elseif args[ 1 ] == "10" then
  1372.         if args[ 2 ] == "1" then
  1373.             rice_farm()
  1374.         else
  1375.             cane_farm()
  1376.         end
  1377.     else
  1378.         print( "What?... bye." )
  1379.     end
  1380. end
  1381.  
  1382. -- Check if has redstone analog signal
  1383. if not check_redstone_option() then
  1384.     show_menu()
  1385. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement