Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Miner
- local SLOT_FUEL_CHEST = 1
- local SLOT_DEPOSIT_CHEST = 2
- local SLOTS_EMPTY = 14
- local MINESHAFT_WIDTH = 10
- --------- BEGIN NAVIGATION ---------
- local rel_x, rel_y, rel_z = 0, 0, 0
- local rel_bearing = 0
- local MAX_REL_Z = 100
- function forward()
- if not turtle.forward() then
- return false
- end
- if rel_bearing == 0 then
- rel_y = rel_y + 1
- elseif rel_bearing == 1 then
- rel_x = rel_x + 1
- elseif rel_bearing == 2 then
- rel_y = rel_y - 1
- elseif rel_bearing == 3 then
- rel_x = rel_x - 1
- end
- return true
- end
- function back()
- if not turtle.back() then
- return false
- end
- if rel_bearing == 0 then
- rel_y = rel_y - 1
- elseif rel_bearing == 1 then
- rel_x = rel_x - 1
- elseif rel_bearing == 2 then
- rel_y = rel_y + 1
- elseif rel_bearing == 3 then
- rel_x = rel_x + 1
- end
- return true
- end
- function turnLeft()
- if not turtle.turnLeft() then
- return false
- end
- rel_bearing = (rel_bearing - 1) % 4
- return true
- end
- function turnRight()
- if not turtle.turnRight() then
- return false
- end
- rel_bearing = (rel_bearing + 1) % 4
- return true
- end
- function up()
- assert(rel_z + 1 <= MAX_REL_Z, "Attempted to move up past MAX_REL_Z")
- if not turtle.up() then
- return false
- end
- rel_z = rel_z + 1
- return true
- end
- function down()
- if not turtle.down() then
- return false
- end
- rel_z = rel_z - 1
- return true
- end
- --------- END NAVIGATION ---------
- --------- BEGIN FUEL ---------
- function get_place_dig_suck_drop_in_empty_space()
- local success, data = turtle.inspect()
- if not success then
- return turtle.place, turtle.dig, turtle.suck, turtle.drop
- end
- success, data = turtle.inspectUp()
- if not success then
- return turtle.placeUp, turtle.digUp, turtle.suckUp, turtle.dropUp
- end
- success, data = turtle.inspectDown()
- if not success then
- return turtle.placeDown, turtle.digDown, turtle.suckDown, turtle.dropDown
- end
- error("No empty space in front, above, or below the turtle")
- end
- function try_with_rotation(func)
- for i=1,4 do
- vals = {pcall(func)}
- if vals[1] then
- for j = 1,(i-1) do
- turnRight()
- end
- table.remove(vals, 1)
- return unpack(vals)
- else
- print(vals[2])
- end
- turnLeft()
- end
- error("Could not execute function in any orientation")
- end
- function refuel_from_chest(slot)
- print("Refueling from chest in slot " .. tostring(slot) .. ". Fuel: " .. tostring(turtle.getFuelLevel()) .. "/" .. tostring(turtle.getFuelLimit()))
- local place, dig, suck, drop = get_place_dig_suck_drop_in_empty_space()
- local prev_slot = turtle.getSelectedSlot()
- turtle.select(slot)
- local success = place()
- assert(success, "Could not place chest")
- while turtle.getFuelLevel() ~= turtle.getFuelLimit() do
- success = suck()
- assert(success, "Could not obtain fuel from chest")
- success = turtle.refuel()
- assert(success, "Could not use item obtained from chest as fuel")
- end
- turtle.select(slot)
- success = dig()
- assert(success, "Could not remove chest")
- print("Finished refueling. Fuel: " .. tostring(turtle.getFuelLevel()) .. "/" .. tostring(turtle.getFuelLimit()))
- turtle.select(prev_slot)
- end
- function handle_fuel()
- if turtle.getFuelLevel() / turtle.getFuelLimit() < 0.1 then
- try_with_rotation(function()
- refuel_from_chest(SLOT_FUEL_CHEST)
- end)
- return true
- end
- return false
- end
- --------- END FUEL ---------
- --------- BEGIN MINE ---------
- function inspect_vertical()
- local _, up_data = turtle.inspectUp()
- local _, front_data = turtle.inspect()
- local _, down_data = turtle.inspectDown()
- return {down_data, front_data, up_data}
- end
- function inspect_vertical_simple()
- local up_data = turtle.inspectUp()
- local front_data = turtle.inspect()
- local down_data = turtle.inspectDown()
- return {down_data, front_data, up_data}
- end
- function mine_forward_one()
- local i = 0
- while not forward() do
- turtle.dig()
- assert (i < 300, "Failed to move forward down the mineshaft")
- i = i + 1
- end
- return true
- end
- function mine_up_one()
- local i = 0
- while not up() do
- turtle.digUp()
- assert(i < 300, "Failed to move up the mineshaft")
- i = i + 1
- end
- return true
- end
- function mine_down_until(condition)
- local i = 0
- while not condition(i) do
- local block_exists = turtle.inspectDown()
- if block_exists then
- assert(turtle.digDown(), "Could not dig down")
- end
- assert(down(), "Could not move down")
- i = i + 1
- end
- return i
- end
- function mine_up_until(condition)
- local i = 0
- while not condition(i) do
- mine_up_one()
- i = i + 1
- end
- return i
- end
- function mine_forward_shaft()
- turtle.digUp() -- allowed to fail
- turtle.digDown() -- allowed to fail
- return mine_forward_one()
- end
- local z_heading_up = false
- function maintain_bounds()
- if rel_y == MINESHAFT_WIDTH and rel_bearing == 0 or rel_y == 1 and rel_bearing == 2 then
- local at_end_of_z_func = function()
- return not turtle.digDown() and turtle.inspectDown()
- end
- local mine_vert_3_func = function()
- return pcall(function()
- return mine_down_until(function(i)
- return i == 3
- end)
- end)
- end
- if z_heading_up then
- at_end_of_z_func = function()
- return rel_z == 0
- end
- mine_vert_3_func = function()
- return pcall(function()
- return mine_up_until(function(i)
- return i == 3
- end)
- end)
- end
- end
- local turnFunc = turnRight
- if rel_bearing == 2 then
- turnFunc = turnLeft
- end
- if at_end_of_z_func() then
- assert(turnFunc(), "Could not face towards next mineshaft")
- mine_forward_one()
- assert(turnFunc(), "Could not orient the turtle along the next mineshaft")
- z_heading_up = not z_heading_up
- else
- assert(turnFunc(), "Could not orient the turtle back towards the current mineshaft")
- assert(turnFunc(), "Could not orient the turtle into the current mineshaft")
- mine_vert_3_func()
- end
- return true
- end
- return false
- end
- --------- END MINE ---------
- --------- BEGIN DEPOSIT ---------
- function get_num_empty_slots()
- local empty_slots = SLOTS_EMPTY
- for i=1,16 do
- if i ~= SLOT_DEPOSIT_CHEST and i ~= SLOT_FUEL_CHEST and turtle.getItemCount(i) ~= 0 then
- empty_slots = empty_slots - 1
- end
- end
- return empty_slots
- end
- function deposit_to_chest(slot)
- print("Depositing into chest in slot " .. tostring(slot) .. ". Empty slots: " .. tostring(get_num_empty_slots()))
- local place, dig, suck, drop = get_place_dig_suck_drop_in_empty_space()
- local prev_slot = turtle.getSelectedSlot()
- turtle.select(slot)
- assert(place(), "Could not place chest")
- for i=1,16 do
- if i ~= SLOT_DEPOSIT_CHEST and i ~= SLOT_FUEL_CHEST and turtle.getItemCount(i) ~= 0 then
- turtle.select(i)
- assert(drop(), "Could not insert item into chest")
- end
- end
- turtle.select(slot)
- assert(dig(), "Could not remove chest")
- print("Finished depositing into chest in slot " .. tostring(slot) .. ". Empty slots: " .. tostring(get_num_empty_slots()))
- turtle.select(prev_slot)
- end
- function handle_inventory()
- if get_num_empty_slots() < 2 then
- try_with_rotation(function()
- deposit_to_chest(SLOT_DEPOSIT_CHEST)
- end)
- return true
- end
- return false
- end
- --------- END DEPOSIT ---------
- function loop_logic()
- while true do
- logic_funcs = {handle_fuel, handle_inventory, maintain_bounds, mine_forward_shaft}
- for i, func in ipairs(logic_funcs) do
- if func() then
- print("Took action " .. i)
- break
- end
- end
- --print("Hit enter to continue...")
- --io.read()
- end
- end
- loop_logic()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement