BioPrince

room

Oct 5th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --==================================================================================================
  2. --pastebin get Xj7CFYA9 room
  3. --Program to make a turtle dig out a room of dimensions X*Y*Z extending forwards, up, and right from the turtle
  4. --Author: Black_Humor
  5. --Usage: place the turtle at the bottom left corner of the room you want to dig,
  6. --then type room <length> <height> <width>
  7.  
  8. --Note: Avoid doing long even width rooms; the algorithm I'm using is kind of bad for those. Will optimise
  9. --eventually.
  10. --==================================================================================================
  11.  
  12.  
  13. --get variables
  14. local inputs = { ... }
  15. if #inputs ~= 3 then
  16.     print("Usage: <length> <height> <width>")
  17.     return
  18. end
  19.  
  20. local length = tonumber(inputs[1])
  21. local height = tonumber(inputs[2])
  22. local width = tonumber(inputs[3])
  23. if length <= 0 then
  24.     print("ERROR: Length must be greater than 0!")
  25.     return
  26. end
  27. if height <= 0 then
  28.     print("ERROR: Width must be greater than 0!")
  29.     return
  30. end
  31. if width <= 0 then
  32.     print("ERROR: Height must be greater than 0!")
  33.     return
  34. end
  35.  
  36. function tryDig()
  37.     while turtle.detect() do
  38.         turtle.dig()
  39.         sleep(0.1)
  40.     end
  41.     return turtle.forward()
  42. end
  43.  
  44. function tryFuel(amount)
  45.     while turtle.getFuelLevel() <= amount do
  46.         if turtle.refuel() then
  47.             print("Refuelling...")
  48.         else
  49.             print("Not enough fuel!")
  50.             break
  51.         end
  52.     end
  53. end
  54. --declare relevant functions
  55. function digLine(X)
  56.     tryFuel(X)
  57.     dug_X = 1
  58.     while dug_X < X do
  59.         if tryDig() then
  60.         --incrementing here because don't want to incr for each piece of gravel dug
  61.             dug_X = dug_X + 1
  62.             if dug_X >= X then
  63.                 break
  64.             end
  65.         end
  66.        
  67.         --print("[dug_X = ", dug_X, ", X = ", X, "]")
  68.     end
  69. end
  70.  
  71. function digSlice(X, Z)
  72.     print ("Digging slice of length ", X, " and width ", Z)
  73.    
  74.     tryFuel(X*Z)
  75.     dug_Z = 0
  76.     while dug_Z < Z do
  77.         print("Digging line ", dug_Z + 1, "/", Z)
  78.         digLine(X)
  79.         dug_Z = dug_Z + 1
  80.         if dug_Z >= Z then
  81.             break --don't want to do all this other stuff; just end here
  82.         end
  83.        
  84.         if (dug_Z % 2) == 1 then
  85.             turtle.turnRight()
  86.         else
  87.             turtle.turnLeft()
  88.         end
  89.        
  90.         tryDig()
  91.        
  92.         if (dug_Z % 2) == 1 then
  93.             turtle.turnRight()
  94.         else
  95.             turtle.turnLeft()
  96.         --print("[dug_Z = ", dug_Z, ", Z = ", Z, "]")
  97.         end
  98.     end
  99. end
  100.  
  101. function digRoom(X, Y, Z)
  102.     tryFuel(X*Y*Z)
  103.     dug_Y = 0
  104.     while dug_Y < Y do --this condition should actually break the loop almost never
  105.         print("Digging line ", dug_Y + 1, "/", Y)
  106.         digSlice(X, Z)
  107.         --check the condition
  108.         dug_Y = dug_Y + 1
  109.         if dug_Y >= Y then
  110.             break
  111.         end
  112.        
  113.         --figure out which end of the room the turtle is at
  114.         turtle.turnRight()
  115.         if (Z % 2) == 1 then
  116.             turtle.turnRight()         
  117.         end
  118.        
  119.         --now, go up
  120.         while turtle.detectUp() do
  121.             turtle.digUp()
  122.         end
  123.         turtle.up()
  124.        
  125.         if (Z % 2) == 0 then -- if perpendicular, need to reverse x and z
  126.             digSlice(Z, X)
  127.         else
  128.             digSlice(X, Z)
  129.         end
  130.        
  131.         dug_Y = dug_Y + 1
  132.         if dug_Y >= Y then
  133.             break
  134.         end
  135.        
  136.         turtle.turnRight()
  137.         if (Z % 2) == 1 then
  138.             turtle.turnRight()         
  139.         end
  140.        
  141.         --now, go up
  142.         while turtle.detectUp() do
  143.             turtle.digUp()
  144.         end
  145.         turtle.up()
  146.        
  147.         --print("[dug_Y = ", dug_Y, ", Y = ", Y, "]")
  148.     end
  149. end
  150. --main loop
  151. digRoom(length, height, width)
Advertisement
Add Comment
Please, Sign In to add comment