Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Bridge program. By Mitchfizz05
- local tArgs = {...}
- local function printUsage()
- print("Usage: bridge <length>")
- end
- if #tArgs ~= 1 then
- -- Bad number of arguments.
- printUsage()
- return
- end
- local length = tonumber(tArgs[1])
- if length == nil then
- -- Argument provided not a number.
- printUsage()
- return
- end
- function refuel()
- local refuelled = false
- for i = 15,16 do -- attempt refuel from last two slots
- turtle.select(i)
- if turtle.refuel() then
- refuelled = true -- refuelled
- end
- end
- return refuelled
- end
- local function selectSlot() -- Select next slot with items in it.
- for i = 1,14 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- return true
- end
- end
- return false
- end
- local function forward()
- while not turtle.forward() do
- if turtle.getFuelLevel() == 0 then
- -- No fuel! Request fuel.
- print("Out of fuel! Please provide fuel in slot 15 or 16.")
- while (turtle.getFuelLevel() == 0) do
- refuel() -- attempt refuel
- sleep(0.5)
- end
- print("Thank you")
- else
- -- Probably an obstruction blocking the turtle, attempt to destroy it.
- turtle.attack()
- turtle.dig()
- turtle.digUp()
- end
- sleep(0.5)
- end
- end
- local function build() -- Build bridge segment.
- if not selectSlot() then
- -- Out of items!
- print("Out of building materials! Please provide materials in any slot!")
- while not selectSlot() do
- sleep(0.5)
- end
- print("Thank you")
- end
- turtle.placeDown()
- forward()
- end
- local function returnToHome() -- Return turtle to home base.
- turtle.turnRight()
- turtle.turnRight()
- i = length
- while i > 0 do
- forward()
- i = i - 1
- end
- end
- local function fuelRequirement(length) -- Calculate fuel requirement.
- return (length * 2) + 2
- --[[
- Length times two because we gotta build the length of the bridge then return.
- Plus two for good measure, and the initial forwards action.
- ]]
- end
- local function blockCount() -- Number of blocks in inventory.
- local blocks = 0
- for i = 1,14 do
- blocks = blocks + turtle.getItemCount(i)
- end
- return blocks
- end
- -- Preform checks.
- if blockCount() < length then
- print("Not enough blocks! Are you sure you want to continue? [Y/N]")
- while true do
- local e,key = os.pullEvent("char")
- if key == "y" then
- break -- break loop and continue
- elseif key == "n" then
- return -- exit program
- end
- end
- end
- if turtle.getFuelLevel() < fuelRequirement(length) then
- print("Not enough fuel! Are you sure you want to continue? [Y/N]")
- while true do
- local e,key = os.pullEvent("char")
- if key == "y" then
- break -- break loop and continue
- elseif key == "n" then
- return -- exit program
- end
- end
- end
- -- Run program.
- forward() -- go forward once first
- local i = length
- while i > 0 do
- -- Run iteration
- build()
- i = i - 1
- end
- print("Completed bridge - returning to homebase.")
- returnToHome()
- print("Job complete.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement