Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Miner
- local SLOT_FEUL = 1
- 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
- turtle.turnRight()
- end
- table.remove(vals, 1)
- return unpack(vals)
- end
- turtle.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 = try_with_rotation(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
- 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
- refuel_from_chest(SLOT_FEUL)
- return true
- end
- return false
- end
- 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_down_until(condition)
- local retval = false
- while not condition() do
- local block_exists = turtle.inspectDown()
- if block_exists then
- assert(turtle.digDown(), "Could not dig down")
- end
- assert(turtle.down(), "Could not move down")
- retval = true
- end
- return retval
- end
- function mine_into_ground()
- return mine_down_until(function()
- local v_state = inspect_vertical_simple()
- return v_state[1] == true and v_state[2] == true
- end)
- end
- function mine_into_shaft()
- local v_state = inspect_vertical_simple()
- if vstate_[1] == true and v_state[2] == true and v_state[3] == false then
- assert(turtle.digDown(), "Could not dig down")
- assert(turtle.down(), "Could not move down")
- assert(turtle.dig(), "Could not dig forwards")
- assert(turtle.forward(), "Could not move forward")
- return true
- end
- return false
- end
- function mine_forward_shaft()
- turtle.digUp() -- allowed to fail
- turtle.digDown() -- allowed to fail
- local success = turtle.dig()
- assert(success, "Unable to dig forward")
- success = turtle.forward()
- assert(success, "Unable to move forward after mining forward")
- return true
- end
- function loop_logic()
- while true do
- logic_funcs = {handle_fuel, mine_into_ground, mine_into_shaft, mine_forward_shaft}
- for _, func in ipairs(logic_funcs) do
- if func() then
- break
- end
- end
- print("Hit enter to continue...")
- io.read()
- end
- end
- loop_logic()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement