Advertisement
karelvysinka

Turtle builds a cube, a cuboid

Apr 10th, 2023 (edited)
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.88 KB | None | 0 0
  1. -- Kód pro stavění krychle nebo kvádru pomocí Turtle v Minecraftu
  2.  
  3. local args = {...}
  4. local width = tonumber(args[1])
  5. local height = tonumber(args[2])
  6. local length = tonumber(args[3])
  7.  
  8. if width == nil or height == nil or length == nil then
  9.     print("Usage: build <width> <height> <length>")
  10.     return
  11. end
  12.  
  13. local currentX, currentY, currentZ = 0, 0, 0
  14. local totalBlocks = width * height * length
  15.  
  16. -- Uložení velikostí do souboru
  17. local file = fs.open("dimensions.txt", "w")
  18. file.writeLine(width)
  19. file.writeLine(height)
  20. file.writeLine(length)
  21. file.close()
  22.  
  23. -- Funkce pro doplnění paliva
  24. local function refuel()
  25.     print("Čekám na doplnění paliva...")
  26.     while turtle.getFuelLevel() < totalBlocks do
  27.         for i = 1, 16 do
  28.             turtle.select(i)
  29.             if turtle.refuel(0) then
  30.                 turtle.refuel()
  31.             end
  32.         end
  33.         os.sleep(1)
  34.     end
  35.     print("Palivo doplněno.")
  36. end
  37.  
  38. -- Funkce pro doplnění itemů
  39. local function getItems()
  40.     turtle.turnLeft()
  41.     turtle.turnLeft()
  42.     while not turtle.detect() do
  43.         turtle.forward()
  44.     end
  45.     for i = 1, 16 do
  46.         turtle.select(i)
  47.         turtle.suck()
  48.     end
  49.     turtle.turnRight()
  50.     turtle.turnRight()
  51. end
  52.  
  53. -- Kontrola coalu ve slotu
  54. local function checkCoal()
  55.     for i = 1, 16 do
  56.         turtle.select(i)
  57.         local currentItem = turtle.getItemDetail()
  58.         if currentItem and currentItem.name == "minecraft:coal" then
  59.             turtle.drop()
  60.             getItems()
  61.         end
  62.     end
  63. end
  64.  
  65. -- Funkce pro pohyb
  66. local function moveForward()
  67.     if turtle.detect() then
  68.         turtle.dig()
  69.     end
  70.     turtle.forward()
  71. end
  72.  
  73. local function moveUp()
  74.     if turtle.detectUp() then
  75.         turtle.digUp()
  76.     end
  77.     turtle.up()
  78. end
  79.  
  80. local function moveDown()
  81.     if turtle.detectDown() then
  82.         turtle.digDown()
  83.     end
  84.     turtle.down()
  85. end
  86.  
  87. -- Stavění
  88. local function build()
  89.     for y = 1, height do
  90.         for z = 1, length do
  91.             for x = 1, width do
  92.                 if turtle.getFuelLevel() < 1 then
  93.                     refuel()
  94.                 end
  95.                
  96.                 checkCoal()
  97.                 turtle.placeDown()
  98.                
  99.                 if x < width then
  100.                     moveForward()
  101.                 end
  102.                
  103.                 currentX = currentX + 1
  104.             end
  105.            
  106.             if z < length then
  107.                 if z % 2 == 0 then
  108.                     turtle.turnRight()
  109.                     moveForward()
  110.                     turtle.turnRight()
  111.                 else
  112.                     turtle.turnLeft()
  113.                     moveForward()
  114.                     turtle.turnLeft()
  115.                 end
  116.                 currentZ = currentZ + 1
  117.             end
  118.         end
  119.        
  120.         if y < height then
  121.             moveUp()
  122.             turtle.turnRight()
  123.             turtle.turnRight()
  124.             currentY = currentY + 1
  125.             currentZ = 0
  126.         end
  127.     end
  128. end
  129.  
  130. -- Pozastavení stavění
  131. local function pause()
  132.     while true do
  133.         local event, key = os.pullEvent("key")
  134. if key == keys.p then
  135. break
  136. end
  137. end
  138. end
  139. -- Uložení souřadnic do souboru
  140. local function saveCoordinates()
  141. local file = fs.open("coordinates.txt", "w")
  142. file.writeLine(currentX)
  143. file.writeLine(currentY)
  144. file.writeLine(currentZ)
  145. file.close()
  146. end
  147.  
  148. -- Načtení souřadnic ze souboru
  149. local function loadCoordinates()
  150. if fs.exists("coordinates.txt") then
  151. local file = fs.open("coordinates.txt", "r")
  152. currentX = tonumber(file.readLine())
  153. currentY = tonumber(file.readLine())
  154. currentZ = tonumber(file.readLine())
  155. file.close()
  156. end
  157. end
  158.  
  159. -- Hlavní program
  160. loadCoordinates()
  161. print("Stavění krychle/kvádru o rozměrech " .. width .. "x" .. height .. "x" .. length)
  162.  
  163. while true do
  164. parallel.waitForAny(build, pause)
  165. saveCoordinates()
  166. end
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement