daily pastebin goal
68%
SHARE
TWEET

Ladders 1.1.0

civilwargeeky Dec 9th, 2013 1,816 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Version 1.1.0
  2. local neededFuel = 256*2 --Max World height and back
  3.  
  4. local oldGetFuel = turtle.getFuelLevel
  5. if type(turtle.getFuelLevel()) ~= "number" then
  6.   turtle.getFuelLevel = function() return math.huge end
  7. end
  8.  
  9. term.clear()
  10. term.setCursorPos(1,1)
  11. print("This program will place ladders straight down on the block it's facing\n")
  12. print("It will also attempt to fill gaps with a material\n")
  13. print("Press any key to continue to fueling section")
  14. os.pullEvent("char")
  15. if turtle.getFuelLevel() < neededFuel then
  16.   turtle.select(16)
  17.   print("Place fuel in bottom right")
  18.   repeat
  19.     print("Needed Fuel: ",neededFuel)
  20.     print("Current Fuel: ",turtle.getFuelLevel())
  21.     while not(turtle.refuel(0)) do --Wait until usable fuel items
  22.       sleep(1)
  23.     end
  24.     turtle.refuel(1)
  25.   until turtle.getFuelLevel() > neededFuel
  26.   term.clear(); term.setCursorPos(1,1)
  27.   print("Current Fuel: ",turtle.getFuelLevel())
  28. else
  29.   term.clear(); term.setCursorPos(1,1)
  30.   print("Sufficient Fuel")
  31. end
  32. print("\nPlace ladders in the top left")
  33. print("And place any filler material beside the ladders")
  34.  
  35. print("\nPlease place all ladders and filler blocks in now. Ladders must be in first slot, but the rest doesn't matter")
  36. print("Press any key when done")
  37. os.pullEvent("char")
  38.  
  39. local slots = {} --Format is [1] Amount of items, [2] Is slot ladders
  40. local numLadders, numFiller = 0,0
  41. for i=16,1, -1 do
  42.   turtle.select(i)
  43.   slots[i] = {turtle.getItemCount(i), turtle.compareTo(1)}
  44.   if slots[i][2] then --This gets a count of how much material the turtle has
  45.     numLadders = numLadders + slots[i][1]
  46.   else
  47.     numFiller = numFiller + slots[i][1]
  48.   end
  49. end
  50.  
  51. local selectedSlot = 1 --Currently selected slot
  52. local function nextLadderSlot()
  53.   if numLadders > 0 then
  54.     for i=selectedSlot+1, 16 do --Cycle through slots
  55.       if turtle.getItemCount(i) > 0 and slots[i][2] then
  56.         turtle.select(i)
  57.         selectedSlot = i
  58.         return true
  59.       end
  60.     end
  61.   end
  62.   return false --If it knows there are no ladders left, or there aren't any for some other reason
  63. end
  64. local function placeFiller()
  65.   local flag = false
  66.   if numFiller > 0 then
  67.     for i=1, 16 do
  68.       if turtle.getItemCount(i) > 0 and not slots[i][2] then --If slot is filler and has stuff in it
  69.         turtle.select(i) --The slot with filler
  70.         if turtle.place() then numFiller = numFiller - 1; flag = true end
  71.         turtle.select(selectedSlot) --The one with ladders
  72.         break
  73.       end
  74.     end
  75.   end
  76.   return flag --Will be true or false
  77. end
  78.      
  79.  
  80. local function generic(move, dig, attack)
  81.   if turtle.getFuelLevel() == 0 then error("Out of fuel",0) end
  82.   while not move() do
  83.     dig()
  84.     attack()
  85.   end
  86.   return true
  87. end
  88. local function forward() return generic(turtle.forward,turtle.dig,turtle.attack) end
  89. local function up() return generic(turtle.up, turtle.digUp, turtle.attackUp) end
  90.  
  91. local movedDown = 0
  92.  
  93. local function updateDisplay()
  94.   term.clear(); term.setCursorPos(1,1)
  95.   print("Gone down ",movedDown," blocks")
  96.   print("Still have ",numLadders," ladders left")
  97.   print("And ",numFiller," filler blocks left")
  98.   print(turtle.getFuelLevel()," Fuel Left")
  99.   print("Block Below? ", tostring(turtle.detectDown()))
  100. end
  101.  
  102. forward() --Get in to hole
  103. turtle.turnLeft()
  104. turtle.turnLeft()
  105.  
  106. repeat --This is where it actually goes into a hole
  107.   while not turtle.down() do turtle.attackDown() end
  108.   movedDown = movedDown + 1
  109.   if turtle.getItemCount(selectedSlot) == 0 then
  110.     if not nextLadderSlot() then break end --If there are no more ladders, then leave
  111.   end
  112.   if turtle.placeUp() then
  113.     numLadders = numLadders - 1
  114.   end
  115.   if not turtle.detect() then placeFiller() end  
  116.   updateDisplay()
  117. until turtle.detectDown() or numLadders == 0
  118.  
  119. if not turtle.back() then
  120.   turtle.turnRight()
  121.   turtle.turnRight()
  122.   forward()
  123.   turtle.turnRight()
  124.   turtle.turnRight()
  125. end
  126. turtle.place() --Place ladder at bottom
  127. updateDisplay()
  128.  
  129. for i=1, movedDown do --Getting back up. Up will automatically break blocks
  130.   up()
  131. end
  132.  
  133. forward() --Getting back to start pos
  134. forward()
  135. turtle.turnRight()
  136. turtle.turnRight()
  137.  
  138. turtle.getFuelLevel = oldGetFuel --Cleanup
  139. print("Done")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top