Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_home_coords()
- local home = vector.new(gps.locate(5))
- if not home then
- print("Failed to get my home location.")
- else
- print("My home is at (" .. home.x .. ", " .. home.y .. ", " .. home.z ..")")
- end
- return home
- end
- function get_curr_coords()
- local new_pos = vector.new(gps.locate(5))
- if not new_pos then
- print("Failed to get current location.")
- else
- print("I'm currently at (" .. new_pos.x .. ", " .. new_pos.y .. ", " .. new_pos.z .. ")")
- end
- return new_pos
- end
- function get_curr_disp(home, current)
- return current - home
- end
- function slideLeft()
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- end
- function slidesLeft(amount)
- turtle.turnLeft()
- for i=0,(amount-1),1 do
- turtle.forward()
- end
- turtle.turnRight()
- end
- function slideRight()
- turtle.turnRight()
- turtle.forward()
- turtle.turnLeft()
- end
- function slidesRight(amount)
- turtle.turnRight()
- for i=0,(amount-1),1 do
- turtle.forward()
- end
- turtle.turnLeft()
- end
- function goForwards(num)
- for i=1,num do
- turtle.forward()
- end
- end
- function get_orientation()
- location_1 = vector.new(gps.locate(5, false))
- if not turtle.forward() then
- for j=1,6 do
- if not turtle.forward() then
- turtle.dig()
- else
- break
- end
- end
- end
- location_2 = vector.new(gps.locate(5, false))
- heading = location_2 - location_1
- turtle.back()
- calculation = ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
- print("I AM ORIENTED TOWARDS ", calculation)
- return calculation
- end
- function go_to_location(x, y, z, direction_to_mine)
- hurdle_blocks = {"computercraft:turtle_advanced"}
- go_left = nil
- go_right = nil
- direction_curr = get_orientation()
- direction_start_move = nil
- heading = vector.new(x, y, z)
- current_pos = get_curr_coords()
- displacement = get_curr_disp(current_pos, heading)
- if displacement.x < 0 then
- direction_start_move = 1
- elseif displacement.x > 0 then
- direction_start_move = 3
- elseif displacement.z < 0 then
- direction_start_move = 2
- elseif displacement.z > 0 then
- direction_start_move = 4
- end
- while direction_curr ~= direction_start_move do
- turtle.turnRight()
- direction_curr = get_orientation()
- end
- success, down_block = turtle.inspectDown()
- while down_block.name == hurdle_blocks[1] do
- print(down_block.name, hurdle_blocks[1])
- os.sleep(1)
- success, down_block = turtle.inspectDown()
- end
- turtle.forward()
- for i=1,math.abs(displacement.y) do
- turtle.down()
- end
- if direction_curr == 1 or direction_curr == 3 then
- first = math.abs(displacement.x) - 1
- second = displacement.z
- else
- first = math.abs(displacement.z) - 1
- second = displacement.x
- end
- if direction_curr == 3 or direction_curr == 2 then
- if second < 0 then
- go_left = true
- elseif second > 0 then
- go_right = true
- end
- elseif direction_curr == 1 or direction_curr == 4 then
- if second < 0 then
- go_right = true
- elseif second > 0 then
- go_left = true
- end
- end
- second = math.abs(second)
- for i=1,first do
- turtle.forward()
- success, infront_block = turtle.inspect()
- success, down_block = turtle.inspectDown()
- print(infront_block.name, down_block.name)
- if infront_block.name == hurdle_blocks[1] then
- print("INFRONT")
- turtle.up()
- go_down = false
- end
- if go_down then
- print("GOING DOWN")
- turtle.down()
- end
- if down_block.name == hurdle_blocks[1] then
- print("BELOW")
- go_down = true
- end
- end
- if go_right then
- turtle.turnRight()
- elseif go_left then
- turtle.turnLeft()
- else
- print("I dont know which way to turn...")
- end
- for i=1,second do
- turtle.forward()
- success, infront_block = turtle.inspect()
- success, down_block = turtle.inspectDown()
- print(infront_block.name, down_block.name)
- if infront_block.name == hurdle_blocks[1] then
- print("INFRONT")
- turtle.up()
- go_down = false
- end
- if go_down then
- print("GOING DOWN")
- turtle.down()
- end
- if down_block.name == hurdle_blocks[1] then
- print("BELOW")
- go_down = true
- end
- end
- direction_curr = get_orientation()
- while direction_curr ~= direction_to_mine do
- turtle.turnRight()
- direction_curr = get_orientation()
- end
- end
- function go_home(home, direction_to_mine)
- hurdle_blocks = {"quark:jungle_chest"}
- go_left = nil
- go_right = nil
- direction_curr = get_orientation()
- direction_start_move = nil
- heading = vector.new(home.x, home.y, home.z)
- current_pos = get_curr_coords()
- displacement = get_curr_disp(current_pos, heading)
- while direction_curr ~= direction_to_mine do
- turtle.turnRight()
- direction_curr = get_orientation()
- end
- turtle.turnRight()
- turtle.turnRight()
- if direction_curr == 1 or direction_curr == 3 then
- first = math.abs(displacement.x) - 1
- second = displacement.z
- else
- first = math.abs(displacement.z) - 1
- second = displacement.x
- end
- if direction_curr == 3 or direction_curr == 2 then
- if second < 0 then
- go_right = true
- elseif second > 0 then
- go_left = true
- end
- elseif direction_curr == 1 or direction_curr == 4 then
- if second < 0 then
- go_left = true
- elseif second > 0 then
- go_right = true
- end
- end
- second = math.abs(second)
- for i=1,first+1 do
- turtle.forward()
- end
- if go_right then
- turtle.turnRight()
- elseif go_left then
- turtle.turnLeft()
- else
- print("I dont know which way to turn...")
- end
- for i=1,second do
- success, block_front = turtle.inspect()
- for i=1,#hurdle_blocks do
- if block_front.name == hurdle_blocks[i] then
- turtle.up()
- end
- end
- turtle.forward()
- end
- direction_curr = get_orientation()
- while direction_curr ~= direction_to_mine do
- turtle.turnRight()
- direction_curr = get_orientation()
- end
- end
- function mine_selected_area(home, direction_to_mine, width, height, depth)
- print("MINING SELECTED AREA OF (" .. width .. ", " .. height .. ", " .. depth .. ")")
- go_right = true
- last_x, last_y, last_z = nil, nil, nil
- for i=1,depth do
- for k=1,height-1 do
- for j=1,width-1 do
- if go_right and inventory_check() then
- current_position = get_curr_coords()
- last_x, last_y, last_z = current_position.x, current_position.y, current_position.z
- try_refuel()
- go_home(home, direction_to_mine)
- dump_items()
- go_to_location(last_x, last_y, last_z, direction_to_mine)
- end
- turtle.dig()
- if go_right then
- slideRight()
- else
- slideLeft()
- end
- end
- turtle.dig()
- go_right = not go_right
- if k ~= height-1 then
- turtle.up()
- end
- end
- turtle.down()
- if i ~= depth then
- turtle.forward()
- end
- end
- for i=1,depth-1 do
- turtle.back()
- end
- end
- function dump_items()
- chests = {"quark:jungle_chest"}
- success, block_down = turtle.inspectDown()
- for i=1,#chests do
- if block_down.name == chests[i] then
- for k=1,16 do
- turtle.select(k)
- turtle.dropDown()
- end
- end
- end
- turtle.select(1)
- end
- function try_refuel()
- for i=1,16 do
- turtle.select(i)
- if turtle.refuel() then
- print("REFUELING")
- turtle.refuel()
- end
- end
- turtle.select(1)
- end
- function inventory_check()
- sum = 0
- for i=1,16 do
- turtle.select(i)
- amount_of_items = turtle.getItemCount()
- if amount_of_items > 32 then
- sum = sum + 1
- end
- end
- if sum >= 6 then
- return true
- else
- return false
- end
- end
- function run(x, y, z, mining_direction, width, height, depth)
- --local tArgs = {...}
- home = get_home_coords()
- --x, y, z, = tArgs[1], tArgs[2], tArgs[3]
- --mining_direction = tArgs[4]
- --width, height, depth = tArgs[5], tArgs[6], tArgs[7]
- go_to_location(x, y, z, mining_direction)
- mine_selected_area(home, mining_direction, width, height, depth)
- go_home(home, mining_direction)
- end
- run()
Add Comment
Please, Sign In to add comment