Advertisement
Guest User

rubber_trees

a guest
Apr 24th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.01 KB | None | 0 0
  1. c_select = 1
  2. p_select = 1
  3. turtle.select( 1 )
  4.  
  5. fuel_slot = 1
  6. marker_slot = 2
  7. sapling_slot = 3
  8. trunk_slot = 4
  9. rubber_slot = 5
  10.  
  11. function do_select( n_select )
  12.   if n_select then
  13.     p_select = c_select or n_select or p_select or 1
  14.     c_select = n_select or c_select or 1
  15.   end
  16.   turtle.select( c_select )
  17.   return c_select
  18. end
  19.  
  20. function get_select()
  21.   return c_select
  22. end
  23.  
  24. select = do_select
  25. doSelect = do_select
  26. getSelect = get_select
  27.  
  28. function in_table( inspect, table )
  29.   for element_i = 1, #table, 1 do
  30.     if inspect == table[ element_i ] then
  31.       return true
  32.     end
  33.   end
  34.   return false
  35. end
  36.  
  37. function detect_dir( direction )
  38.   direction = direction or "f"
  39.   dir_char = direction:sub(1,1)
  40.   if dir_char == "f" then
  41.     return turtle.detect()
  42.   elseif dir_char == "b" then
  43.     turtle.turnRight()
  44.     turtle.turnRight()
  45.     re = turtle.detect()
  46.     turtle.turnRight()
  47.     turtle.turnRight()
  48.     return re
  49.   elseif dir_char == "u" then
  50.     return turtle.detectUp()
  51.   elseif dir_char == "d" then
  52.     return turtle.detectDown()
  53.   else
  54.     print( "Invalid direction: "..direction )
  55.     return false
  56.   end
  57. end
  58.  
  59. function move_dir( direction )
  60.   direction = direction or "f"
  61.   dir_char = direction:sub(1,1)
  62.   if dir_char == "f" then
  63.     return turtle.forward()
  64.   elseif dir_char == "b" then
  65.     return turtle.back()
  66.   elseif dir_char == "u" then
  67.     return turtle.up()
  68.   elseif dir_char == "d" then
  69.     return turtle.down()
  70.   else
  71.     print( "Invalid direction: "..direction )
  72.     return false
  73.   end
  74. end
  75.  
  76. function move( direction )
  77.   direction = direction or "f"
  78.   fuel_slot = fuel_slot or 1
  79.   verbose = verbose or false
  80.   if not move_dir( direction ) then
  81.     if turtle.getFuelLevel() == 0 then
  82.       if verbose then
  83.         print( "Out of Fuel." )
  84.       end
  85.       do_select( fuel_slot )
  86.       if not turtle.refuel( 1 ) then
  87.         print( "Cannot refuel." )
  88.         print( "Waiting for fuel to continue." )
  89.         while not turtle.refuel( 1 ) do
  90.           sleep( 1 )
  91.         end
  92.         print( "Refueled." )
  93.       elseif verbose then
  94.         print( "Refueled." )
  95.       end
  96.       do_select( p_select )
  97.       return move( direction )
  98.     else
  99.       if verbose then
  100.         print( "Path obstructed." )
  101.       end
  102.       if detect_dir( direction ) then
  103.         if verbose then
  104.           print( "Block found." )
  105.           print( "Cannot move." )
  106.         end
  107.         return false
  108.       else
  109.         print( "Entity blocking path." )
  110.         print( "Waiting to move." )
  111.         while not move_dir( direction ) do
  112.           sleep( 1 )
  113.         end
  114.       end
  115.     end
  116.   end
  117.   return true
  118. end
  119.  
  120. function place_dir (direction )
  121.   direction = direction  or "f"
  122.   dir_char = direction:sub(1,1)
  123.   if dir_char == "f" then
  124.     return turtle.place()
  125.   elseif dir_char == "b" then
  126.     turtle.turnRight()
  127.     turtle.turnRight()
  128.     re = turtle.place()
  129.     turtle.turnRight()
  130.     turtle.turnRight()
  131.     return re
  132.   elseif dir_char == "u" then
  133.     return turtle.placeUp()
  134.   elseif dir_char == "d" then
  135.     return turtle.placeDown()
  136.   else
  137.     print( "Invalid direction: "..direction )
  138.     return false
  139.   end
  140. end
  141.  
  142. function place( direction, tSelection )
  143.   direction = direction or "f"
  144.   if tSelection == {} then
  145.     tSelection = nil
  146.   end
  147.   tSelection = tSelection or {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
  148.   verbose = verbose or false
  149.  
  150.   do_select()
  151.  
  152.   if not in_table( c_select, tSelection ) then
  153.     do_select( tSelection[1] )
  154.   end
  155.  
  156.   if not place_dir( direction ) then
  157.     if detect_dir( direction ) then
  158.       if verbose then
  159.         print( "Cannot place in direction." )
  160.         print( "Pre-existing block found in direction." )
  161.       end
  162.       return false
  163.     end
  164.     placed = false
  165.     i_select = c_select
  166.     for selector_i = 1, #tSelection, 1 do
  167.       do_select( tSelection[ selector_i ] )
  168.       if place_dir( direction ) then
  169.         placed = true
  170.         break
  171.       end
  172.     end
  173.    
  174.     -- Return the currently selected slot to the
  175.     -- initially selected slot
  176.    
  177.     do_select( i_select )
  178.    
  179.     if placed then
  180.       return true
  181.     else
  182.       if verbose then
  183.         print( "Could not place block in direction" )
  184.         print( "With any blocks in selected slots." )
  185.       end
  186.       return false
  187.     end
  188.   else
  189.     return true
  190.   end
  191.   return true
  192. end
  193.  
  194. function compare_slots( given_slot )
  195.   turtle.select( given_slot )
  196.   re = {given_slot}
  197.   for i = 1, 16, 1 do
  198.     if i ~= given_slot then
  199.       if turtle.compareTo( i ) then
  200.         table.insert( re, 1, i )
  201.       end
  202.     end
  203.   end
  204.   turtle.select( c_select )
  205.   return re
  206. end
  207.  
  208. function count_items( slot )
  209.   total = 0
  210.   tSlots = compare_slots( slot )
  211.   for i = 1, #tSlots, 1 do
  212.     total = total + turtle.getItemCount( tSlots[i] )
  213.   end
  214.   return total
  215. end
  216.  
  217. function move_to_mark()
  218.   select( marker_slot )
  219.   move()
  220.   inc = 1
  221.   while not turtle.compareDown() do
  222.     move()
  223.     inc = inc + 1
  224.   end
  225.   return inc
  226. end
  227.  
  228. function main()
  229.   while true do
  230.     dist = move_to_mark()
  231.     while dist ~= 1 do
  232.       turtle.turnRight()
  233.       select( trunk_slot )
  234.       if turtle.compare() then
  235.         turtle.dig()
  236.         move()
  237.         select( trunk_slot )
  238.         while turtle.compareUp() do
  239.           turtle.digUp()
  240.           move( "u" )
  241.         end
  242.         while not turtle.detectDown() do
  243.           move( "d" )
  244.         end
  245.         move( "b" )
  246.       end
  247.       if count_items( sapling_slot ) ~= 1 then
  248.         place( "f", compare_slots( sapling_slot ) )
  249.       end
  250.       turtle.turnLeft()
  251.       dist = move_to_mark()
  252.     end
  253.     turtle.turnRight()
  254.     turtle.turnRight()
  255.     move()
  256.     dist = move_to_mark()
  257.     while dist ~= 1 do
  258.       dist = move_to_mark()
  259.     end
  260.     move( "b" )
  261.     turtle.turnLeft()
  262.     if move_to_mark() == 1 then
  263.       break
  264.     end
  265.     turtle.turnLeft()
  266.   end
  267.   turtle.turnRight()
  268.   turtle.turnRight()
  269.   move()
  270.   dist = move_to_mark()
  271.   while dist ~= 1 do
  272.     dist = move_to_mark()
  273.   end
  274.   turtle.turnLeft()
  275.  
  276.   select( sapling_slot )
  277.   turtle.suck()
  278.   tSaplings = compare_slots( sapling_slot )
  279.   for i = 1, #tSaplings - 1, 1 do
  280.     select( tSaplings[i] )
  281.     turtle.drop( turtle.getItemCount( get_select() ) )
  282.   end
  283.  
  284.   turtle.turnRight()
  285.   tTrunks = compare_slots( trunk_slot )
  286.   for i = 1, #tTrunks - 1, 1 do
  287.     select( tTrunks[i] )
  288.     turtle.drop( turtle.getItemCount( get_select() ) )
  289.   end
  290.   select( trunk_slot )
  291.   turtle.drop( turtle.getItemCount( get_select() ) - 1 )
  292.  
  293.   tRubber = compare_slots( rubber_slot )
  294.   for i = 1, #tRubber - 1, 1 do
  295.     select( tRubber[i] )
  296.     turtle.drop( turtle.getItemCount( get_select() ) )
  297.   end
  298.   select( rubber_slot )
  299.   turtle.drop( turtle.getItemCount( get_select() ) - 1 )
  300.  
  301.   turtle.turnRight()
  302.   select( fuel_slot )
  303.   turtle.suck()
  304.   tFuel = compare_slots( fuel_slot )
  305.   for i = 1, #tFuel - 1, 1 do
  306.     select( tFuel[i] )
  307.     turtle.drop( turtle.getItemCount( get_select() ) )
  308.   end
  309.  
  310.   turtle.turnRight()
  311.   move()
  312.   turtle.turnLeft()
  313. end
  314.  
  315. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement