Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- c_select = 1
- p_select = 1
- turtle.select( 1 )
- fuel_slot = 1
- marker_slot = 2
- sapling_slot = 3
- trunk_slot = 4
- rubber_slot = 5
- function do_select( n_select )
- if n_select then
- p_select = c_select or n_select or p_select or 1
- c_select = n_select or c_select or 1
- end
- turtle.select( c_select )
- return c_select
- end
- function get_select()
- return c_select
- end
- select = do_select
- doSelect = do_select
- getSelect = get_select
- function in_table( inspect, table )
- for element_i = 1, #table, 1 do
- if inspect == table[ element_i ] then
- return true
- end
- end
- return false
- end
- function detect_dir( direction )
- direction = direction or "f"
- dir_char = direction:sub(1,1)
- if dir_char == "f" then
- return turtle.detect()
- elseif dir_char == "b" then
- turtle.turnRight()
- turtle.turnRight()
- re = turtle.detect()
- turtle.turnRight()
- turtle.turnRight()
- return re
- elseif dir_char == "u" then
- return turtle.detectUp()
- elseif dir_char == "d" then
- return turtle.detectDown()
- else
- print( "Invalid direction: "..direction )
- return false
- end
- end
- function move_dir( direction )
- direction = direction or "f"
- dir_char = direction:sub(1,1)
- if dir_char == "f" then
- return turtle.forward()
- elseif dir_char == "b" then
- return turtle.back()
- elseif dir_char == "u" then
- return turtle.up()
- elseif dir_char == "d" then
- return turtle.down()
- else
- print( "Invalid direction: "..direction )
- return false
- end
- end
- function move( direction )
- direction = direction or "f"
- fuel_slot = fuel_slot or 1
- verbose = verbose or false
- if not move_dir( direction ) then
- if turtle.getFuelLevel() == 0 then
- if verbose then
- print( "Out of Fuel." )
- end
- do_select( fuel_slot )
- if not turtle.refuel( 1 ) then
- print( "Cannot refuel." )
- print( "Waiting for fuel to continue." )
- while not turtle.refuel( 1 ) do
- sleep( 1 )
- end
- print( "Refueled." )
- elseif verbose then
- print( "Refueled." )
- end
- do_select( p_select )
- return move( direction )
- else
- if verbose then
- print( "Path obstructed." )
- end
- if detect_dir( direction ) then
- if verbose then
- print( "Block found." )
- print( "Cannot move." )
- end
- return false
- else
- print( "Entity blocking path." )
- print( "Waiting to move." )
- while not move_dir( direction ) do
- sleep( 1 )
- end
- end
- end
- end
- return true
- end
- function place_dir (direction )
- direction = direction or "f"
- dir_char = direction:sub(1,1)
- if dir_char == "f" then
- return turtle.place()
- elseif dir_char == "b" then
- turtle.turnRight()
- turtle.turnRight()
- re = turtle.place()
- turtle.turnRight()
- turtle.turnRight()
- return re
- elseif dir_char == "u" then
- return turtle.placeUp()
- elseif dir_char == "d" then
- return turtle.placeDown()
- else
- print( "Invalid direction: "..direction )
- return false
- end
- end
- function place( direction, tSelection )
- direction = direction or "f"
- if tSelection == {} then
- tSelection = nil
- end
- tSelection = tSelection or {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
- verbose = verbose or false
- do_select()
- if not in_table( c_select, tSelection ) then
- do_select( tSelection[1] )
- end
- if not place_dir( direction ) then
- if detect_dir( direction ) then
- if verbose then
- print( "Cannot place in direction." )
- print( "Pre-existing block found in direction." )
- end
- return false
- end
- placed = false
- i_select = c_select
- for selector_i = 1, #tSelection, 1 do
- do_select( tSelection[ selector_i ] )
- if place_dir( direction ) then
- placed = true
- break
- end
- end
- -- Return the currently selected slot to the
- -- initially selected slot
- do_select( i_select )
- if placed then
- return true
- else
- if verbose then
- print( "Could not place block in direction" )
- print( "With any blocks in selected slots." )
- end
- return false
- end
- else
- return true
- end
- return true
- end
- function compare_slots( given_slot )
- turtle.select( given_slot )
- re = {given_slot}
- for i = 1, 16, 1 do
- if i ~= given_slot then
- if turtle.compareTo( i ) then
- table.insert( re, 1, i )
- end
- end
- end
- turtle.select( c_select )
- return re
- end
- function count_items( slot )
- total = 0
- tSlots = compare_slots( slot )
- for i = 1, #tSlots, 1 do
- total = total + turtle.getItemCount( tSlots[i] )
- end
- return total
- end
- function move_to_mark()
- select( marker_slot )
- move()
- inc = 1
- while not turtle.compareDown() do
- move()
- inc = inc + 1
- end
- return inc
- end
- function main()
- while true do
- dist = move_to_mark()
- while dist ~= 1 do
- turtle.turnRight()
- select( trunk_slot )
- if turtle.compare() then
- turtle.dig()
- move()
- select( trunk_slot )
- while turtle.compareUp() do
- turtle.digUp()
- move( "u" )
- end
- while not turtle.detectDown() do
- move( "d" )
- end
- move( "b" )
- end
- if count_items( sapling_slot ) ~= 1 then
- place( "f", compare_slots( sapling_slot ) )
- end
- turtle.turnLeft()
- dist = move_to_mark()
- end
- turtle.turnRight()
- turtle.turnRight()
- move()
- dist = move_to_mark()
- while dist ~= 1 do
- dist = move_to_mark()
- end
- move( "b" )
- turtle.turnLeft()
- if move_to_mark() == 1 then
- break
- end
- turtle.turnLeft()
- end
- turtle.turnRight()
- turtle.turnRight()
- move()
- dist = move_to_mark()
- while dist ~= 1 do
- dist = move_to_mark()
- end
- turtle.turnLeft()
- select( sapling_slot )
- turtle.suck()
- tSaplings = compare_slots( sapling_slot )
- for i = 1, #tSaplings - 1, 1 do
- select( tSaplings[i] )
- turtle.drop( turtle.getItemCount( get_select() ) )
- end
- turtle.turnRight()
- tTrunks = compare_slots( trunk_slot )
- for i = 1, #tTrunks - 1, 1 do
- select( tTrunks[i] )
- turtle.drop( turtle.getItemCount( get_select() ) )
- end
- select( trunk_slot )
- turtle.drop( turtle.getItemCount( get_select() ) - 1 )
- tRubber = compare_slots( rubber_slot )
- for i = 1, #tRubber - 1, 1 do
- select( tRubber[i] )
- turtle.drop( turtle.getItemCount( get_select() ) )
- end
- select( rubber_slot )
- turtle.drop( turtle.getItemCount( get_select() ) - 1 )
- turtle.turnRight()
- select( fuel_slot )
- turtle.suck()
- tFuel = compare_slots( fuel_slot )
- for i = 1, #tFuel - 1, 1 do
- select( tFuel[i] )
- turtle.drop( turtle.getItemCount( get_select() ) )
- end
- turtle.turnRight()
- move()
- turtle.turnLeft()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement