Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- A ComputerCraft script for mining in a straight line forward.
- local tArgs = {...}
- local depth = 0
- local distance = 0
- local MAX_DISTANCE
- if tonumber(tArgs[1]) == nil then
- MAX_DISTANCE = 8
- else
- MAX_DISTANCE = tonumber(tArgs[1])
- end
- local success, lastDistance, lastDepth
- local function unloadInventory()
- local i
- print('Emptying inventory...')
- -- TODO: check for chest?
- turtle.turnLeft()
- turtle.turnLeft()
- for i = 1,16 do
- turtle.select(i)
- turtle.drop(64)
- end
- turtle.turnLeft()
- turtle.turnLeft()
- end
- local function refuelIfNeeded()
- local i, itemData, coalNeeded
- coalNeeded = math.floor((turtle.getFuelLimit() - turtle.getFuelLevel()) / 80)
- for i = 1,16 do
- itemData = turtle.getItemDetail(i)
- if itemData ~= nil and itemData[name] == 'minecraft:coal' and coalNeeded > 0 then
- print('Refueling, coal needed = ' .. coalNeeded)
- turtle.refuel(coalNeeded)
- coalNeeded = math.floor((turtle.getFuelLimit() - turtle.getFuelLevel()) / 80)
- end
- end
- end
- local function returnHome()
- local i
- print('Returning home...')
- for i = 1,depth do
- turtle.up()
- depth = depth - 1
- end
- for i = 1,distance do
- turtle.back()
- distance = distance - 1
- end
- unloadInventory()
- end
- local function mustUnloadInventory()
- -- check for free inventory space
- refuelIfNeeded() -- clear out any coal inventory
- local filledUp = true
- for i = 1,16 do
- if turtle.getItemCount(i) == 0 then
- filledUp = false
- end
- end
- if filledUp then
- print('Must unload inventory.')
- lastDistance = distance
- lastDepth = depth
- returnHome()
- if lastDepth + lastDistance < turtle.getFuelLevel() then
- for i = 1,lastDistance do
- turtle.forward()
- distance = distance + 1
- end
- for i = 1,lastDepth do
- turtle.down()
- depth = depth + 1
- end
- end
- end
- return false
- end
- local function mustRefuel()
- refuelIfNeeded()
- if depth + distance >= turtle.getFuelLevel() then
- print('Not enough fuel to continue.')
- returnHome()
- return true
- end
- return false
- end
- local function main()
- local i, blockData
- while true do
- -- fuel check
- if mustRefuel() then break end
- -- dig left, right, down
- turtle.turnLeft()
- if turtle.inspect() ~= false then turtle.dig() end
- turtle.turnRight()
- if mustUnloadInventory() then break end
- turtle.turnRight()
- if turtle.inspect() ~= false then turtle.dig() end
- turtle.turnLeft()
- if mustUnloadInventory() then break end
- if turtle.inspectDown() ~= false then turtle.digDown() end
- if mustUnloadInventory() then break end
- blockExists, blockData = turtle.inspectDown()
- if blockExists == true and blockData ~= nil and blockData['name'] == 'minecraft:bedrock' then
- -- hit bedrock, return home
- print('Hit bedrock')
- for i = 1,depth do
- turtle.up()
- depth = depth - 1
- end
- if distance == MAX_DISTANCE - 1 then
- print('Reached max distance.')
- returnHome()
- break
- end
- -- move forward
- if turtle.inspect() ~= false then turtle.dig() end
- if mustUnloadInventory() then break end
- turtle.forward()
- distance = distance + 1
- if mustRefuel() then break end
- else
- -- go down
- turtle.down()
- depth = depth + 1
- if mustRefuel() then break end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment