Advertisement
Guest User

bridge

a guest
Jan 27th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. -- Bridge program. By Mitchfizz05
  2.  
  3. local tArgs = {...}
  4.  
  5. local function printUsage()
  6.     print("Usage: bridge <length>")
  7. end
  8.  
  9. if #tArgs ~= 1 then
  10.     -- Bad number of arguments.
  11.     printUsage()
  12.     return
  13. end
  14.  
  15. local length = tonumber(tArgs[1])
  16.  
  17. if length == nil then
  18.     -- Argument provided not a number.
  19.     printUsage()
  20.     return
  21. end
  22.  
  23. function refuel()
  24.     local refuelled = false
  25.     for i = 15,16 do -- attempt refuel from last two slots
  26.         turtle.select(i)
  27.         if turtle.refuel() then
  28.             refuelled = true -- refuelled
  29.         end
  30.     end
  31.    
  32.     return refuelled
  33. end
  34.  
  35. local function selectSlot() -- Select next slot with items in it.
  36.     for i = 1,14 do
  37.         if turtle.getItemCount(i) > 0 then
  38.             turtle.select(i)
  39.             return true
  40.         end
  41.     end
  42.  
  43.     return false
  44. end
  45.  
  46. local function forward()
  47.     while not turtle.forward() do
  48.         if turtle.getFuelLevel() == 0 then
  49.             -- No fuel! Request fuel.
  50.             print("Out of fuel! Please provide fuel in slot 15 or 16.")
  51.             while (turtle.getFuelLevel() == 0) do
  52.                 refuel() -- attempt refuel
  53.                 sleep(0.5)
  54.             end
  55.             print("Thank you")
  56.         else
  57.             -- Probably an obstruction blocking the turtle, attempt to destroy it.
  58.             turtle.attack()
  59.             turtle.dig()
  60.             turtle.digUp()
  61.         end
  62.        
  63.         sleep(0.5)
  64.     end
  65. end
  66.  
  67. local function build() -- Build bridge segment.
  68.     if not selectSlot() then
  69.         -- Out of items!
  70.         print("Out of building materials! Please provide materials in any slot!")
  71.         while not selectSlot() do
  72.             sleep(0.5)
  73.         end
  74.         print("Thank you")
  75.     end
  76.     turtle.placeDown()
  77.     forward()
  78. end
  79.  
  80. local function returnToHome() -- Return turtle to home base.
  81.     turtle.turnRight()
  82.     turtle.turnRight()
  83.     i = length
  84.     while i > 0 do
  85.         forward()
  86.         i = i - 1
  87.     end
  88. end
  89.  
  90. local function fuelRequirement(length) -- Calculate fuel requirement.
  91.     return (length * 2) + 2
  92.     --[[
  93.         Length times two because we gotta build the length of the bridge then return.
  94.         Plus two for good measure, and the initial forwards action.
  95.     ]]
  96. end
  97.  
  98. local function blockCount() -- Number of blocks in inventory.
  99.     local blocks = 0
  100.     for i = 1,14 do
  101.         blocks = blocks + turtle.getItemCount(i)
  102.     end
  103.     return blocks
  104. end
  105.  
  106. -- Preform checks.
  107. if blockCount() < length then
  108.     print("Not enough blocks! Are you sure you want to continue? [Y/N]")
  109.     while true do
  110.         local e,key = os.pullEvent("char")
  111.         if key == "y" then
  112.             break -- break loop and continue
  113.         elseif key == "n" then
  114.             return -- exit program
  115.         end
  116.     end
  117. end
  118.  
  119. if turtle.getFuelLevel() < fuelRequirement(length) then
  120.     print("Not enough fuel! Are you sure you want to continue? [Y/N]")
  121.     while true do
  122.         local e,key = os.pullEvent("char")
  123.         if key == "y" then
  124.             break -- break loop and continue
  125.         elseif key == "n" then
  126.             return -- exit program
  127.         end
  128.     end
  129. end
  130.  
  131.  
  132. -- Run program.
  133. forward() -- go forward once first
  134.  
  135. local i = length
  136. while i > 0 do
  137.     -- Run iteration
  138.     build()
  139.    
  140.     i = i - 1
  141. end
  142.  
  143. print("Completed bridge - returning to homebase.")
  144. returnToHome()
  145.  
  146. print("Job complete.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement