Advertisement
GRxRedZero

Create Room / Tunnel

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