Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Simple Excavation Script with EnderIO Ender Chest Support
- -- Configuration
- local MaxForward = 16
- local MaxRight = 16
- local SkipHoles = 0
- -- Slot configuration
- local EnderChestSlot = 15
- local FuelSlot = 16
- local WorkingSlots = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
- -- Position tracking
- local pos = {0, 0, 0}
- local facing = 0
- local curMessage = ''
- local totalBlocks = 0
- -- Display function
- function tWrite(txt)
- term.clear()
- term.setCursorPos(1, 1)
- if txt then
- term.write(txt)
- else
- term.write(curMessage)
- term.setCursorPos(1, 2)
- term.write("Total blocks mined: " .. totalBlocks)
- end
- end
- -- Count free inventory slots
- function getInventorySpace()
- local freeSlots = 0
- for _, slot in ipairs(WorkingSlots) do
- if turtle.getItemCount(slot) == 0 then
- freeSlots = freeSlots + 1
- end
- end
- return freeSlots
- end
- -- Simple dig function
- function dig(direction)
- local digFunc, detectFunc
- if direction == "forward" then
- digFunc = turtle.dig
- detectFunc = turtle.detect
- elseif direction == "up" then
- digFunc = turtle.digUp
- detectFunc = turtle.detectUp
- elseif direction == "down" then
- digFunc = turtle.digDown
- detectFunc = turtle.detectDown
- else
- return false
- end
- if detectFunc() and digFunc() then
- totalBlocks = totalBlocks + 1
- return true
- end
- return false
- end
- -- Movement functions
- function turnRight(n)
- n = n or 1
- for i = 1, n do
- facing = (facing + 1) % 4
- turtle.turnRight()
- end
- end
- function turnLeft(n)
- n = n or 1
- for i = 1, n do
- facing = (facing - 1) % 4
- turtle.turnLeft()
- end
- end
- function checkFuel(n)
- if turtle.getFuelLevel() < n then
- tWrite('Refueling...')
- turtle.select(FuelSlot)
- while turtle.getFuelLevel() < n do
- turtle.refuel(1)
- end
- tWrite()
- end
- end
- function up(n)
- n = n or 1
- checkFuel(n)
- for i = 1, n do
- if turtle.up() then
- pos[3] = pos[3] + 1
- end
- end
- end
- function down(n)
- n = n or 1
- checkFuel(n)
- for i = 1, n do
- if turtle.down() then
- pos[3] = pos[3] - 1
- end
- end
- end
- function forward(n)
- n = n or 1
- checkFuel(n)
- if facing == 0 then
- for i = 1, n do
- if turtle.forward() then
- pos[2] = pos[2] + 1
- end
- end
- elseif facing == 1 then
- turnLeft()
- for i = 1, n do
- if turtle.forward() then
- pos[2] = pos[2] + 1
- end
- end
- elseif facing == 2 then
- for i = 1, n do
- if turtle.back() then
- pos[2] = pos[2] + 1
- end
- end
- else
- turnRight()
- for i = 1, n do
- if turtle.forward() then
- pos[2] = pos[2] + 1
- end
- end
- end
- end
- function backward(n)
- n = n or 1
- checkFuel(n)
- if facing == 0 then
- for i = 1, n do
- if turtle.back() then
- pos[2] = pos[2] - 1
- end
- end
- elseif facing == 1 then
- turnRight()
- for i = 1, n do
- if turtle.forward() then
- pos[2] = pos[2] - 1
- end
- end
- elseif facing == 2 then
- for i = 1, n do
- if turtle.forward() then
- pos[2] = pos[2] - 1
- end
- end
- else
- turnLeft()
- for i = 1, n do
- if turtle.forward() then
- pos[2] = pos[2] - 1
- end
- end
- end
- end
- function right(n)
- n = n or 1
- checkFuel(n)
- if facing == 0 then
- turnRight()
- for i = 1, n do
- if turtle.forward() then
- pos[1] = pos[1] + 1
- end
- end
- elseif facing == 1 then
- for i = 1, n do
- if turtle.forward() then
- pos[1] = pos[1] + 1
- end
- end
- elseif facing == 2 then
- turnLeft()
- for i = 1, n do
- if turtle.forward() then
- pos[1] = pos[1] + 1
- end
- end
- else
- for i = 1, n do
- if turtle.back() then
- pos[1] = pos[1] + 1
- end
- end
- end
- end
- function left(n)
- n = n or 1
- checkFuel(n)
- if facing == 0 then
- turnLeft()
- for i = 1, n do
- if turtle.forward() then
- pos[1] = pos[1] - 1
- end
- end
- elseif facing == 1 then
- for i = 1, n do
- if turtle.back() then
- pos[1] = pos[1] - 1
- end
- end
- elseif facing == 2 then
- turnRight()
- for i = 1, n do
- if turtle.forward() then
- pos[1] = pos[1] - 1
- end
- end
- else
- for i = 1, n do
- if turtle.forward() then
- pos[1] = pos[1] - 1
- end
- end
- end
- end
- function getTo(nPos)
- if not nPos[1] then nPos[1] = pos[1] end
- if not nPos[2] then nPos[2] = pos[2] end
- if not nPos[3] then nPos[3] = pos[3] end
- local diff = {nPos[1] - pos[1], nPos[2] - pos[2], nPos[3] - pos[3]}
- if diff[1] > 0 then
- right(diff[1])
- elseif diff[1] < 0 then
- left(-diff[1])
- end
- if diff[2] > 0 then
- forward(diff[2])
- elseif diff[2] < 0 then
- backward(-diff[2])
- end
- if diff[3] > 0 then
- up(diff[3])
- elseif diff[3] < 0 then
- down(-diff[3])
- end
- end
- -- EnderIO Ender Chest functions
- function transferToEnderChest()
- curMessage = 'Using ender chest...'
- tWrite()
- -- First dig the block below if there is one
- if turtle.detectDown() then
- if turtle.digDown() then
- totalBlocks = totalBlocks + 1
- end
- end
- turtle.select(EnderChestSlot)
- if turtle.placeDown() then
- local itemsTransferred = 0
- for _, slot in ipairs(WorkingSlots) do
- turtle.select(slot)
- if turtle.getItemCount(slot) > 0 then
- itemsTransferred = itemsTransferred + turtle.getItemCount(slot)
- turtle.dropDown()
- end
- end
- turtle.select(EnderChestSlot)
- turtle.digDown()
- curMessage = 'Transferred ' .. itemsTransferred .. ' items'
- tWrite()
- return true
- else
- curMessage = 'Failed to place ender chest'
- tWrite()
- return false
- end
- end
- -- Mining functions
- function drillSides()
- for i = 0, 3 do
- dig("forward")
- if i ~= 3 then
- turnLeft()
- end
- end
- end
- function ascend()
- curMessage = 'Ascending...'
- tWrite()
- while pos[3] < 0 do
- dig("up")
- up()
- end
- tWrite()
- end
- function drill()
- curMessage = 'Drilling...'
- tWrite()
- while not turtle.detectDown() or turtle.digDown() do
- if turtle.detectDown() and turtle.digDown() then
- totalBlocks = totalBlocks + 1
- end
- down()
- drillSides()
- if getInventorySpace() <= 2 then
- transferToEnderChest()
- end
- end
- ascend()
- tWrite()
- end
- function startingPoint(row)
- return (2 * (row-1)) % 5 + 1
- end
- function drillRow(row)
- getTo({row, startingPoint(row)})
- local willMove = 0
- while (pos[2] + willMove) <= (MaxForward - 2) do
- if getInventorySpace() <= 3 then
- transferToEnderChest()
- end
- if willMove == 0 then
- willMove = 5
- else
- forward(willMove)
- end
- if SkipHoles == 0 then
- drill()
- else
- SkipHoles = SkipHoles - 1
- end
- end
- end
- -- Main execution
- tWrite('Starting excavation...')
- for i = 1, MaxRight - 2 do
- curMessage = 'Excavating row ' .. i
- tWrite()
- drillRow(i)
- end
- tWrite('Excavation complete!')
- tWrite('Total blocks mined: ' .. totalBlocks)
Add Comment
Please, Sign In to add comment