Pampour

TowerBuilder (Alpha Build)

Aug 19th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | None | 0 0
  1. -- Tower Builder by Robin | robinschoors.com
  2.  
  3. -- Settings (You can change these (: )
  4. local fuel = true       -- Change to false if your turtle doesn't use fuel
  5. local totalSlots = 16-1 -- Change to 9 if you have an older version! Leave the -1 alone please. :P
  6. local tightLoop = 0.25  -- Change this to a higher value if the server lags because of the program
  7. -- Initial variables
  8. local tArgs = {...}
  9. local width, length, height, inventory
  10. local totalBlocksPlaced = 0
  11. local success = false
  12.  
  13. -- Check initial variables
  14. if tArgs[1] == nil or tArgs[2] == nil or tArgs[3] == nil then
  15.   error("Error!\n\tUSAGE:\n\ttowerBuilder <width> <length> <height>\n\te.g. towerBuilder 4 4 8")
  16. else
  17.   width  = tonumber(tArgs[1])
  18.   length = tonumber(tArgs[2])
  19.   height = tonumber(tArgs[3])
  20. end
  21.  
  22. -- Clever functions go here
  23. function hasFuel()
  24.   if fuel == true then
  25.     if turtle.getFuelLevel()<=0 then
  26.       turtle.select(1)
  27.       return false
  28.     else
  29.       return true
  30.     end
  31.   else
  32.     return true
  33.   end
  34. end
  35.  
  36. function autoSelect() -- Automatically selects slot with items
  37.   for i=1,totalSlots do
  38.     if turtle.getItemCount(i)>0 then
  39.       turtle.select(i)
  40.       return true
  41.     end
  42.     i = i+1
  43.   end
  44.   return false
  45. end
  46.  
  47. function clearInventory() -- Clear inventory from anything but the building blocks
  48.   for i=1,totalSlots do
  49.     turtle.select(totalSlots+1)
  50.     if turtle.compareTo(i)==false then
  51.       turtle.select(i)
  52.       turtle.drop()
  53.     end
  54.   end
  55. end
  56.  
  57. function totalBlocksNeeded() -- returns total blocks needed for build
  58.   return width * length * height
  59. end
  60.  
  61. function checkInventory() -- Count all blocks in inventory and stores it in inventory var
  62.   inventory = 0
  63.   for i=1,totalSlots do
  64.     inventory = inventory + turtle.getItemCount(i)
  65.     i = i+1
  66.   end
  67.   return inventory
  68. end
  69.  
  70. function checkBlocksNeeded() -- Returns blocks that need to be added to complete the build
  71.   return totalBlocksNeeded()-inventory-totalBlocksPlaced
  72. end
  73.  
  74. function canPlaceBlock() -- Checks if Turtle can place a block
  75.   checkInventory()
  76.   if inventory == 0 then
  77.     return false
  78.   else
  79.     return true
  80.   end
  81. end
  82.  
  83. function fuelCheckOnMove()
  84.   if fuel==true then
  85.     if hasFuel() == false then
  86.       print "Out of fuel! Put some fuel in my first slot. I'll automatically turn it into energy! :)"
  87.       while hasFuel()==false do
  88.         turtle.refuel()
  89.         hasFuel()
  90.         os.sleep(tightLoop)
  91.       end
  92.       print "Resuming build in 5 seconds..."
  93.       sleep(5)
  94.     end
  95.   end
  96. end
  97.  
  98. function moveUp() -- Move up 1 level Checks for gravel or other players that may be obstructing the path
  99.   fuelCheckOnMove()
  100.   success = false
  101.   while success==false do
  102.     while turtle.detectUp() do
  103.       turtle.digUp()
  104.     end
  105.     success=turtle.up()
  106.   end
  107. end
  108.  
  109. function forward() -- Move forward. Checks for gravel or other players that may be obstructing the path
  110.   fuelCheckOnMove()
  111.   success = false
  112.   while success==false do
  113.     while turtle.detect()==true do
  114.       turtle.dig()
  115.       clearInventory()
  116.     end
  117.     success=turtle.forward()
  118.   end
  119. end
  120.  
  121. function placedBlock() -- Doing some additional calculations after placing a block
  122.   totalBlocksPlaced = totalBlocksPlaced + 1
  123. end
  124.  
  125. function placeDown() -- place block beneath the turtle
  126.   success = false
  127.   while success == false do
  128.     if canPlaceBlock()==false then
  129.       print "Ran out of blocks to place. Will resume after blocks are added to inventory!"
  130.       while inventory<=0 do
  131.         checkInventory()
  132.         sleep(tightLoop)
  133.       end
  134.       print "Resuming build in 5 seconds..."
  135.       sleep(5)
  136.     end
  137.     autoSelect()
  138.     success = turtle.placeDown()
  139.     if turtle.detectDown()==true then
  140.       success = true
  141.     end
  142.   end
  143.   placedBlock()
  144. end
  145.  
  146. function turnRight()
  147.   success = false
  148.   while success==false do
  149.     success = turtle.turnRight()
  150.   end
  151. end
  152.  
  153. function turnLeft()
  154.   success = false
  155.   while success==false do
  156.     success = turtle.turnLeft()
  157.   end
  158. end
  159.  
  160. function buildLength()
  161.   for i=1,length do
  162.     placeDown()
  163.     forward()
  164.   end
  165. end
  166.  
  167. function buildWidth()
  168.   for i=1,width do
  169.     placeDown()
  170.     forward()
  171.   end
  172. end
  173.  
  174. function buildLevel()
  175.   moveUp()
  176.  
  177.   buildLength()
  178.   turnLeft()
  179.   buildWidth()
  180.   turnLeft()
  181.   buildLength()
  182.   turnLeft()
  183.   buildWidth()
  184.   turnLeft()
  185. end
  186.  
  187. function build()
  188.   turnRight()
  189.   for x=1,height do
  190.     buildLevel()
  191.   end
  192. end
  193.  
  194. -- Doing the build
  195. print("I'm gonna need " .. totalBlocksNeeded() .. " to build this. Starting build...\n")
  196. build()
Advertisement
Add Comment
Please, Sign In to add comment