Advertisement
TechManDylan

ChatGPT BuildCylinder

Jan 6th, 2023
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. -- This script will make a turtle build a hollow cylinder with the given radius and height
  2.  
  3. -- Define the function to build the cylinder
  4. function buildCylinder(radius, height)
  5.   -- Set the radius of the cylinder
  6.   local radius = radius
  7.  
  8.   -- Set the height of the cylinder
  9.   local height = height
  10.  
  11.   -- Move the turtle to the starting position
  12.   turtle.up()
  13.   turtle.turnLeft()
  14.   turtle.turnLeft()
  15.   for i=1,radius do
  16.     turtle.forward()
  17.   end
  18.   turtle.turnRight()
  19.  
  20.   -- Define a function to check if the turtle has blocks in its inventory
  21.   function checkInventory()
  22.     for i=1,16 do
  23.       turtle.select(i)
  24.       if turtle.getItemCount() > 0 then
  25.         -- If blocks are found in any slot, return true
  26.         return true
  27.       end
  28.     end
  29.     -- If no blocks are found in any slot, return false
  30.     return false
  31.   end
  32.  
  33.   -- Build the top layer of the cylinder
  34.   for i=1,radius*2 do
  35.     -- Check if the turtle has blocks in its inventory before trying to place a block
  36.     if checkInventory() then
  37.       turtle.placeDown()
  38.     else
  39.       -- If the turtle is out of blocks, spin in a circle and print a message until blocks are added
  40.       while not checkInventory() do
  41.         turtle.turnRight()
  42.         turtle.turnRight()
  43.         turtle.turnRight()
  44.         turtle.turnRight()
  45.         print("Need more blocks")
  46.         sleep(5)
  47.       end
  48.     end
  49.     turtle.forward()
  50.   end
  51.   turtle.turnRight()
  52.   turtle.forward()
  53.   turtle.turnRight()
  54.  
  55.   -- Build the sides of the cylinder
  56.   for j=2,height do
  57.     for i=1,radius*2+1 do
  58.       -- Check if the turtle has blocks in its inventory before trying to place a block
  59.       if checkInventory() then
  60.         turtle.placeDown()
  61.       else
  62.         -- If the turtle is out of blocks, spin in a circle and print a message until blocks are added
  63.         while not checkInventory() do
  64.           turtle.turnRight()
  65.           print("Need more blocks")
  66.           sleep(5)
  67.         end
  68.       end
  69.       turtle.forward()
  70.     end
  71.     turtle.turnRight()
  72.     turtle.forward()
  73.     turtle.turnRight()
  74.   end
  75.  
  76.   -- Return the turtle to the starting position
  77.   turtle.turnRight()
  78.   for i=1,radius+1 do
  79.     turtle.forward()
  80.   end
  81.   turtle.turnLeft()
  82. end
  83.  
  84. -- Test the function by building a cylinder with radius 5 and height 10
  85. buildCylinder(5, 10)
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement