Advertisement
VADemon

Obsidian tower

Oct 8th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.44 KB | None | 0 0
  1. -- Usage: tower <width / length> {direction [r|l]} {height}
  2. -- height is automatically adjustable or if you decide to set it, start counting from the floor
  3.  
  4. -- v1.1: turtle's movement cant be disturbed by falling blocks or creeps
  5.  
  6. local dig = function ()
  7.     if turtle.detect() then
  8.         return turtle.dig()
  9.     end
  10.     return false
  11. end
  12.  
  13. local digUp = function ()
  14.     if turtle.detectUp() then
  15.         return turtle.digUp()
  16.     end
  17.     return false
  18. end
  19.  
  20. local digDown = function ()
  21.     if turtle.detectDown() then
  22.         return turtle.digDown()
  23.     end
  24.     return false
  25. end
  26.  
  27. local forward = function()
  28.     if turtle.forward() == false then
  29.        
  30.         while turtle.forward() == false do
  31.             turtle.dig()
  32.         end
  33.        
  34.     end
  35. end
  36.  
  37. local function digRow(length)
  38.     for i = 1, length do
  39.         dig()
  40.         digDown()
  41.         digUp()
  42.         forward()
  43.     end
  44.     digUp()
  45.     digDown()
  46. end
  47.  
  48. local function digLevel(a, b)
  49.     if not a then a = 1 end
  50.     if not b then b = args[1] end
  51.    
  52.     for i = a, b do
  53.         digRow( args[1] - 1)
  54.         if i~=b then -- don't turn in the last row
  55.             turn()
  56.         else
  57.             turtle.turnLeft(); turtle.turnLeft()
  58.             reverseDirection()
  59.         end
  60.     end
  61. end
  62.  
  63. function turn()
  64.     turtle[ nextTurn ]()
  65.     dig()
  66.     forward()
  67.     turtle[ nextTurn ]()
  68.     reverseDirection()
  69. end
  70.  
  71. function reverseDirection()
  72.     if nextTurn ~= "" then
  73.         if nextTurn == "turnRight" then
  74.             nextTurn = "turnLeft"
  75.            
  76.             return true
  77.         else
  78.             nextTurn = "turnRight"
  79.            
  80.             return true
  81.         end
  82.     else
  83.         print("nextTurn variable not set! :(")
  84.         error()
  85.        
  86.         return false
  87.     end
  88. end
  89.  
  90. function detectLayer() -- is there one more layer above? || moving 2 blocks up
  91.     result = false
  92.     for i = 1, 3 do
  93.         if (turtle.detectUp() and turtle.digUp() and turtle.up()) or (turtle.up() and turtle.detectUp() or turtle.down() and false) then -- more lulz
  94.             result = true
  95.         else
  96.             break
  97.         end
  98.     end
  99.     print(tostring(result))
  100.     return result
  101. end
  102.  
  103. function isEven(n)
  104.     if n%2 == 0 then
  105.         return true
  106.     end
  107.     return false
  108. end
  109.  
  110. args = {...}
  111.  
  112. args[1] = tonumber(args[1])
  113. nextTurn = ""
  114.  
  115.     if args[1] == nil then
  116.         print("First argument must be a number!")
  117.         error()
  118.     end
  119.    
  120.     if args[2] == "r" or args[2] == "l" then
  121.        
  122.     else
  123.         print("Second argument is invalid! It can be either l or r")
  124.     end
  125.    
  126.     args[3] = tonumber(args[3])
  127.     if args[3] ~= nil then
  128.         height = 2
  129.        
  130.         function detectLayer ( height )
  131.             if height + 2 < args[3] then
  132.            
  133.                 for i = 1, 3 do
  134.                     digUp(); turtle.up()
  135.                 end
  136.                 return true
  137.                
  138.             else
  139.                 return false
  140.             end
  141.         end
  142.        
  143.     else
  144.         height = 2
  145.     end
  146.  
  147. print(args[2])
  148. print("Size: "..args[1] .. "x" ..args[1] .. ", direction: ".. (tostring(args[2])~="nil" and args[2] or "not set")) -- for the lulz, without any if
  149.  
  150.  
  151. sleep(2)
  152. print("\nBeginning...")
  153.  
  154.  
  155. turtle.up()
  156.  
  157. digRow( args[1] ) -- 1
  158.  
  159.  
  160. if args[2] == "r" then
  161.             nextTurn = "turnRight"
  162.         else
  163.             nextTurn = "turnLeft"
  164. end
  165. if nextTurn == "" then
  166.     print("nexturn="..nextTurn)
  167.     turtle.turnRight() -- where to continue?
  168.     if turtle.detect() then
  169.         nextTurn = "turnRight"
  170.         turtle.turnLeft()
  171.     else
  172.         turtle.turnLeft(); turtle.turnLeft()
  173.         if turtle.detect() then
  174.             nextTurn = "turnLeft"
  175.             turtle.turnRight()
  176.         else
  177.             print("Can't find the path around the corner!\n\nAborting!")
  178.             error()
  179.         end
  180.     end
  181. end
  182.    
  183. turn()
  184.  
  185. digLevel(2, args[1])
  186.  
  187. while detectLayer(height) do
  188.     reverseDirection()
  189.     digLevel(1, args[1])
  190.     height = height + 2
  191. end
  192.  
  193. print("Height: " .. height)
  194.  
  195. while height > 0 do
  196.     turtle.down()
  197.     height = height - 1
  198. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement