Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Build simple bridge
- Slot 1-15: Building block
- Slot 16: fuel
- --]]
- -- Parameters
- -- Variables
- local itemBlockSlot = 0
- local fuelSlot = 16
- local collected = 0
- -- Process commandline
- local tArgs = { ... }
- if #tArgs < 0 then
- print( "Usage: SimpleBridge <length>" )
- print( "======================================" )
- print( "")
- print( "Expected content in inventory of turtle:" )
- print( "Slot 1 to 15: Block to us" )
- print( "Slot 16: Fuel")
- return
- end
- local length = tonumber( tArgs[1] )
- if length < 1 then
- print( "length must be 1+" )
- return
- end
- local function checkSlot(slotNr,description)
- if turtle.getItemCount(slotNr)==0 then
- print("Expected item '"..description.."' in slot "..slotNr..".")
- return false
- end
- return true
- end
- -- Check slots
- if not checkSlot(1,"BuildingBlocks") then return end
- if not checkSlot(fuelSlot,"fuel") then return end
- -- Main Code
- local function collect()
- collected = collected + 1
- if math.fmod(collected, 100) == 0 then
- print( "Mined "..collected.." items." )
- end
- end
- local function refuel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" or fuelLevel > 0 then
- return
- end
- local function tryRefuel()
- if turtle.getItemCount(fuelSlot) > 0 then
- turtle.select(fuelSlot)
- if turtle.refuel(1) then
- return true
- end
- end
- turtle.select(1)
- return false
- end
- if not tryRefuel() then
- print( "Add more fuel in slot "..fuelSlot.." to continue." )
- while not tryRefuel() do
- sleep(1)
- end
- print( "Resuming Tunnel." )
- end
- end
- local function tryDig()
- while turtle.detect() do
- if turtle.dig() then
- collect()
- sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryDigUp()
- while turtle.detectUp() do
- if turtle.digUp() then
- collect()
- sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryDigDown()
- while turtle.detectDown() do
- if turtle.digDown() then
- collect()
- sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryUp()
- refuel()
- while not turtle.up() do
- if turtle.detectUp() then
- if not tryDigUp() then
- return false
- end
- elseif turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function tryDown()
- refuel()
- while not turtle.down() do
- if turtle.detectDown() then
- if not tryDigDown() then
- return false
- end
- elseif turtle.attackDown() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function tryForward()
- refuel()
- while not turtle.forward() do
- if turtle.detect() then
- if not tryDig() then
- return false
- end
- elseif turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function DumpStatistics()
- print( "Statistics:")
- print( string.rep("=",20))
- print( "Mined "..collected.." blocks total." )
- print( string.rep("=",20))
- end
- local function checkOrSwapItemBlockSlot()
- while (turtle.getItemCount(itemBlockSlot)==0) do
- itemBlockSlot=itemBlockSlot+1
- if itemBlockSlot==16 then return false end
- end
- turtle.select(itemBlockSlot)
- return true
- end
- local function buildBridgeSection()
- if not checkOrSwapItemBlockSlot() then return false end
- if not tryForward() then return false end
- turtle.digDown()
- turtle.digUp()
- turtle.placeDown()
- turtle.turnLeft()
- turtle.dig()
- turtle.place()
- turtle.turnRight()
- turtle.turnRight()
- turtle.dig()
- turtle.place()
- turtle.turnLeft()
- return true
- end
- local function mine()
- for n=1,length do
- if not buildBridgeSection() then return false end
- end
- return true
- end
- if mine() then
- print( "Job complete." )
- else
- print( "Job aborted prematurely.")
- end
- DumpStatistics()
Advertisement
Add Comment
Please, Sign In to add comment