Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- My Excavate
- -- DESCRIPTION:
- -- This program will function in a similar manner to the default excavate program, but a little bit more efficiently.
- -- When its inventory is full, it will attempt to place its items in chests behind and above where it started, attempting to place items in the next chest up
- -- if the last one didn't accept one or more items. If it runs out of chests to place into and it still has items, those items will be discarded and when
- -- its inventory is empty (aside from coal in slot 1), it will return to where it left off and continue.
- -- When it is out of coal (don't use any weaker kind of fuel, please), it will attempt to pull more from a chest to the left of where it started. Because it is assumed
- -- that you are using coal for fuel and a relatively low size argument
- -- USAGE: myExcavate range [useBlacklist]
- -- The range argument is self-explanatory.
- -- The useBlacklist argument is optional, and if defined, item names defined in the blacklist (see "BLACKLIST ITEMS HERE" in the code below) will be discarded by the
- -- turtle's inventory when its inventory is full or before it returns to deposit collected items.
- -- pastebin get WgESfSf1 myExcavate
- -- myExcavate 5 1
- local argv = {...}
- local item_blacklist = {}
- local function bl_add(item_name)
- item_blacklist[item_name] = true
- end
- -- BLACKLIST ITEMS HERE
- bl_add("minecraft:cobblestone")
- -----------------------
- local size = tonumber(argv[1])
- local useBlacklist = false
- if argv[2] then
- useBlacklist = true
- end
- local target_x = 1
- local target_z = size
- local target_y = -2
- local curr_x = 1
- local curr_z = size
- local curr_y = 0
- local x_direction = true -- true = +x, false = -x
- local z_direction = false -- true = +z, false = -z
- local FACE_AXIS_ARRAY = {"+z", "+x", "-z", "-x"}
- local FACE_AXIS_MAP = {}
- for f=1, 4 do
- FACE_AXIS_MAP[FACE_AXIS_ARRAY[f]] = f
- end
- local facing = 2
- local function check_fuel()
- if turtle.getFuelLevel() < 80 then
- turtle.select(1)
- turtle.refuel(1)
- end
- end
- local function turn_towards(face)
- local face_index = FACE_AXIS_MAP[face]
- while not (facing == face_index) do
- local diff = facing - face_index
- if (diff == 1) or (diff == -3) then
- turtle.turnLeft()
- facing = facing - 1
- else
- turtle.turnRight()
- facing = facing + 1
- end
- if facing > 4 then
- facing = 1
- elseif facing < 1 then
- facing = 4
- end
- end
- end
- local function move_by(dist)
- while dist > 0 do
- check_fuel()
- if turtle.detect() then
- turtle.dig()
- end
- if turtle.forward() then
- dist = dist - 1
- if facing == 1 then
- curr_z = curr_z + 1
- elseif facing == 2 then
- curr_x = curr_x + 1
- elseif facing == 3 then
- curr_z = curr_z - 1
- elseif facing == 4 then
- curr_x = curr_x - 1
- end
- end
- end
- end
- local function discard_blacklist()
- if not useBlacklist then
- return
- end
- for s=2, 16 do
- local data = turtle.getItemDetail(s)
- if data and item_blacklist[data.name] then
- turtle.select(s)
- turtle.drop()
- end
- end
- turtle.select(1)
- end
- local function inventory_full()
- for s=2, 16 do
- if turtle.getItemCount(s) == 0 then
- return false
- end
- end
- return true
- end
- local function navigate_to_pos(x, y, z)
- if curr_x < x then
- turn_towards("+x")
- move_by(x - curr_x)
- elseif curr_x > x then
- turn_towards("-x")
- move_by(curr_x - x)
- end
- if curr_z < z then
- turn_towards("+z")
- move_by(z - curr_z)
- elseif curr_z > z then
- turn_towards("-z")
- move_by(curr_z - z)
- end
- while curr_y > y do
- if turtle.detectDown() then
- turtle.digDown()
- end
- check_fuel()
- if turtle.down() then
- curr_y = curr_y - 1
- end
- end
- while curr_y < y do
- if turtle.detectUp() then
- turtle.digUp()
- end
- check_fuel()
- if turtle.up() then
- curr_y = curr_y + 1
- end
- end
- end
- local function navigate_to_start()
- navigate_to_pos(1, 0, size)
- end
- local function deposit_items()
- discard_blacklist()
- navigate_to_start()
- turn_towards("-x")
- local deposit_failed = true
- while deposit_failed do
- deposit_failed = false
- for s=2, 16 do
- if turtle.getItemCount(s) > 0 then
- turtle.select(s)
- if not turtle.drop() then
- deposit_failed = true
- end
- end
- end
- if deposit_failed then
- if turtle.detectUp() then
- turtle.digUp()
- end
- curr_y = curr_y + 1
- turtle.up()
- end
- end
- end
- local state = "mining" -- "mining" or "returning"
- local min_y = 0
- while not (turtle.detectDown() and (not turtle.digDown())) do
- min_y = min_y - 1
- curr_y = curr_y - 1
- check_fuel()
- turtle.down()
- end
- min_y = min_y + 4
- navigate_to_start()
- local running = true
- while running do
- if state == "mining" then
- navigate_to_pos(target_x, target_y, target_z)
- if turtle.detectUp() then
- turtle.digUp()
- end
- if turtle.detectDown() then
- turtle.digDown()
- end
- if inventory_full() or (turtle.getItemCount(1) == 0) then
- state = "returning"
- end
- if x_direction then
- if curr_x < size then
- target_x = curr_x + 1
- elseif curr_x == size then
- if z_direction then
- if curr_z == size then
- if curr_y == min_y then
- running = false
- else
- target_y = target_y - 3
- end
- z_direction = false
- else
- target_z = target_z + 1
- end
- else
- if curr_z == 1 then
- if curr_y == min_y then
- running = false
- else
- target_y = target_y - 3
- end
- z_direction = true
- else
- target_z = target_z - 1
- end
- end
- x_direction = false
- end
- else
- if curr_x > 1 then
- target_x = curr_x - 1
- elseif curr_x == 1 then
- if z_direction then
- if curr_z == size then
- if curr_y == min_y then
- running = false
- else
- target_y = target_y - 3
- end
- z_direction = false
- else
- target_z = target_z + 1
- end
- else
- if curr_z == 1 then
- if curr_y == min_y then
- running = false
- else
- target_y = target_y - 3
- end
- z_direction = true
- else
- target_z = target_z - 1
- end
- end
- x_direction = true
- end
- end
- if target_y < min_y then
- target_y = min_y
- end
- elseif state == "returning" then
- deposit_items()
- navigate_to_start()
- turn_towards("+z")
- turtle.select(1)
- turtle.suck()
- state = "mining"
- end
- end
- deposit_items()
- navigate_to_start()
- turn_towards("+x")
- print("Program Complete!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement