Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local blacklist = {
- "minecraft:stone";
- }
- local MODE_START = 0
- local MODE_VEIN = 1
- local MOVE_SHAFT = 2
- local SIDE_LEFT = 0
- local SIDE_RIGHT = 1
- local PASS_FIRST = 0
- local PASS_SECOND = 1
- local VEIN_LEN = 16;
- local state = {
- -- x, y, z
- relativePos = {0, 0, 0};
- -- 0: forward
- -- 1: left
- -- 2: back
- -- 3: right
- orientation = 0;
- mode = MODE_START;
- vein_len = 0;
- vein_side = SIDE_LEFT;
- vein_pass = PASS_FIRST;
- shaft_len = 0;
- }
- local function blacklisted(detail)
- if not detail or not detail.name then return false end
- for i,v in ipairs(blacklist) do
- if v == detail.name then
- return true
- end
- end
- return false
- end
- local function load_state()
- local file = fs.open("state", "r")
- if not file then return end
- print("Loading previous state")
- local t_state = textutils.unserialize(file.readAll())
- if not t_state then
- print("Failed to deserialize state")
- else
- state = t_state
- end
- file.close()
- end
- local function save_state()
- local file = fs.open("state", "w")
- file.write(textutils.serialize(state))
- file.close()
- end
- local function tryOp(op, force, force_op)
- if force then
- if not force_op then
- force_op = function() end
- end
- while not op() do force_op() end
- return true
- end
- return op()
- end
- local function left(force)
- if tryOp(turtle.turnLeft, force) then
- state.orientation = state.orientation + 1
- if state.orientation == 4 then
- state.orientation = 0
- end
- return true
- end
- return false
- end
- local function right(force)
- if tryOp(turtle.turnRight, force) then
- state.orientation = state.orientation - 1
- if state.orientation == -1 then
- state.orientation = 3
- end
- return true
- end
- return false
- end
- local function up(force)
- if tryOp(turtle.up, force, turtle.digUp) then
- state.relativePos[1] = state.relativePos[1] + 1
- return true
- end
- return false
- end
- local function down(force)
- local success
- if tryOp(turtle.down, force, turtle.digDown) then
- state.relativePos[1] = state.relativePos[1] - 1
- return true
- end
- return false
- end
- local function dig(force)
- return tryOp(turtle.dig, force)
- end
- local function digUp(force)
- return tryOp(turtle.digUp, force)
- end
- local function digDown(force)
- return tryOp(turtle.digDown, force)
- end
- local function forward(force)
- local slot, amount
- if state.orientation == 0 then
- slot = 1
- amount = 1
- elseif state.orientation == 1 then
- slot = 3
- amount = 1
- elseif state.orientation == 2 then
- slot = 1
- amount = 1
- elseif state.orientation == 3 then
- slot = 3
- amount = -1
- end
- if tryOp(turtle.forward, force, turtle.dig) then
- state.relativePos[slot] = state.relativePos[slot] + amount
- return true
- end
- return false
- end
- local function back(force)
- local slot, amount
- if state.orientation == 0 then
- slot = 1
- amount = 1
- elseif state.orientation == 1 then
- slot = 3
- amount = 1
- elseif state.orientation == 2 then
- slot = 1
- amount = 1
- elseif state.orientation == 3 then
- slot = 3
- amount = -1
- end
- if tryOp(turtle.back, force, function() left(true) left(true) dig() left(true) left(true) end) then
- state.relativePos[slot] = state.relativePos[slot] - amount
- end
- end
- load_state()
- while true do
- if state.mode == MODE_START then
- print("Starting miner...")
- state.mode = MODE_SHAFT
- elseif state.mode == MODE_SHAFT then
- forward(true)
- digUp()
- digDown()
- state.shaft_len = state.shaft_len + 1
- if state.shaft_len == 3 then
- state.mode = MODE_VEIN
- left(true)
- down(true)
- forward(true)
- state.shaft_len = 0
- state.vein_side = SIDE_LEFT
- state.vein_pass = PASS_FIRST
- end
- elseif state.mode == MODE_VEIN then
- dig()
- forward(true)
- -- Check top/bottom...
- if not blacklisted(table.pack(turtle.inspectUp())[2]) then
- digUp()
- end
- if not blacklisted(table.pack(turtle.inspectDown())[2]) then
- digDown()
- end
- state.vein_len = state.vein_len + 1
- if state.vein_len == VEIN_LEN then
- state.vein_len = 0
- if state.vein_pass == PASS_FIRST then
- print("Performing second vein pass")
- up(true)
- up(true)
- up(true)
- left(true)
- left(true)
- state.vein_pass = PASS_SECOND
- else
- if state.vein_side == SIDE_LEFT then
- print("Switching to next side")
- down(true)
- down(true)
- down(true)
- --forward(true)
- state.vein_side = SIDE_RIGHT
- else
- print("Switching to shaft mode")
- right(true)
- down(true)
- down(true)
- right(true)
- state.mode = MODE_SHAFT
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement