Advertisement
Dima99

CC Room

Jun 9th, 2025
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.52 KB | None | 0 0
  1. -- for Minecraft (ComputerCraft mod)
  2.  
  3. -- config & start
  4.  
  5. local chestsSlot = 16
  6. local delay = 0
  7.  
  8. local distance = 0
  9. local side = 'r'
  10. local useChests = true
  11. local height = 1
  12. local goDown = false
  13. local autoStart = false
  14.  
  15. local function setStart(args) -- [dist] <side> <chests> <auto start> <height)>
  16.     if #args == 0 then
  17.         return false
  18.     end
  19.    
  20.     if tonumber(args[1]) == nil then
  21.         return false
  22.     else
  23.         distance = args[1]
  24.     end
  25.    
  26.     for i = 2, #args do
  27.         a = args[i]
  28.        
  29.         h = tonumber(a)
  30.         if h ~= nil then
  31.             if h < 0 then
  32.                 goDown = true
  33.             end
  34.         height = math.abs(h)
  35.            
  36.         elseif a == 'l' or a == 'r' then
  37.             side = a
  38.         elseif a == 'c' then
  39.             useChests = false
  40.         elseif a == 's' then
  41.             autoStart = true
  42.         elseif a == 'u' then
  43.             goDown = true
  44.         end
  45.        
  46.     end
  47.    
  48.     return true
  49.  
  50. end
  51.  
  52. local function showWarn()
  53.     print("Error")
  54.     print("Digs a box with horizontal edge lenght [d]")
  55.     print("Usage: " .. shell.getRunningProgram() .. " [d] <l/r> <c> <s> <n>")
  56.     print("d - hor. edge lenght")
  57.     print("l/r - left/right")
  58.     print("c - disable inventory management")
  59.     print("s - silent start")
  60.     print("n - number = height/3")
  61.     print("u - go down [goes up by default]")
  62. end
  63.  
  64. local function writeStat(done, total)
  65.     term.clear()
  66.     term.setCursorPos(1,1)
  67.     done = math.ceil(done/total * 100)
  68.     print("Progress "..done.."%")
  69. end
  70.  
  71. -- core
  72.  
  73. local function clearInv()
  74.     if not useChests then
  75.         return
  76.     end
  77.    
  78.     for i = 1, 16 do
  79.         if turtle.getItemCount(i) == 0 then
  80.             return
  81.         end
  82.     end
  83.    
  84.     local backupSlot = turtle.getSelectedSlot()
  85.    
  86.     turtle.select(chestsSlot)
  87.     turtle.placeDown()
  88.     for i = 1, 16 do
  89.         if i ~= chestsSlot then
  90.             turtle.select(i)
  91.             turtle.dropDown()
  92.         end
  93.     end
  94.    
  95.     turtle.select(backupSlot)    
  96. end
  97.  
  98. local function safeDigUp(s)
  99.     while turtle.detectUp() do
  100.         turtle.digUp()
  101.         if not (s == 0 or s == nil) then
  102.             sleep(s)
  103.         end
  104.     end
  105. end
  106.  
  107. local function step(s)
  108.     while turtle.detect() do
  109.         turtle.dig()
  110.         if not (s == 0 or s == nil) then
  111.             sleep(s)
  112.         end
  113.     end
  114.    
  115.     safeDigUp(s)
  116.    
  117.     if turtle.detectDown() then
  118.         turtle.digDown()
  119.     end
  120.    
  121.     clearInv()
  122.    
  123.     while not turtle.forward() do
  124.         sleep(1)
  125.     end
  126.    
  127.     cntr = cntr + 1
  128.    
  129.     return true
  130. end
  131.  
  132. local function line(l)
  133.     for i = 1, l do
  134.         step(delay)
  135.         writeStat(cntr, totalDist)        
  136.     end
  137. end
  138.  
  139. local function tTurn(inverted)
  140.     inv = inverted or false
  141.     if side == 'l' or side == 'L' then
  142.         if not inv then
  143.             turtle.turnLeft()
  144.         else
  145.             turtle.turnRight()
  146.         end
  147.     end
  148.     if side == 'r' or side == 'R' then
  149.         if not inv then
  150.             turtle.turnRight()
  151.         else
  152.             turtle.turnLeft()
  153.         end
  154.     end
  155. end
  156.  
  157. local function room()
  158.    
  159.     distLeft = distance - 1
  160.    
  161.     while distLeft >= 1 do
  162.         line(distLeft)
  163.         tTurn()
  164.         line(distLeft)
  165.         tTurn()
  166.         line(distLeft)
  167.         tTurn()
  168.         distLeft = distLeft - 1
  169.         line(distLeft)
  170.         tTurn()
  171.         if distLeft >= 1 then
  172.             line(1)
  173.         end
  174.         distLeft = distLeft - 1
  175.     end
  176.     turtle.digDown()
  177.     safeDigUp(delay)
  178. end
  179.  
  180. local function home()
  181.     if cntr == 1 then
  182.         return
  183.     end
  184.    
  185.     for i = 1, math.floor( (distance-1)/2) do
  186.         turtle.back()
  187.     end
  188.     tTurn()
  189.     for i = 1, math.floor( distance/2 ) do
  190.         turtle.back()
  191.     end
  192.     tTurn(true)
  193.    
  194.     if goDown then
  195.         turtle.down()
  196.         turtle.digDown()
  197.         turtle.down()
  198.         turtle.digDown()
  199.         turtle.down()
  200.     else
  201.         turtle.up()
  202.         safeDigUp(delay)
  203.         turtle.up()
  204.         safeDigUp(delay)
  205.         turtle.up()
  206.     end
  207. end
  208.  
  209. local function returnToStart()
  210.     for i = 1, math.floor( (distance-1)/2) do
  211.         turtle.back()
  212.     end
  213.     tTurn()
  214.     for i = 1, math.floor( distance/2 ) do
  215.         turtle.back()
  216.     end
  217.     tTurn(true)
  218.     for i = 1, (height-1)*3 do
  219.         if goDown then
  220.             turtle.up()
  221.         else
  222.             turtle.down()
  223.         end
  224.     end
  225. end
  226.  
  227. -- main
  228.  
  229. args = { ... }
  230. cntr = 1
  231.  
  232. if not setStart(args) then
  233.     showWarn()
  234.     return
  235. end
  236.  
  237. if useChests then
  238.     if turtle.getItemCount(chestsSlot) == 0 then
  239.         print("No chests found. Some loot may be lost. Proceed anyway? (y)")
  240.         q = read()
  241.         if not (q == "y" or q == "Y") then
  242.             return
  243.         end
  244.     end
  245. end
  246.  
  247. totalDist = distance * distance * height + ((height-1) * 3) +
  248. (math.floor( (distance-1)/2) + math.floor( distance/2 )) * (height-1)
  249.  
  250. print("Fuel: "..turtle.getFuelLevel().." / "..totalDist)
  251.  
  252. if turtle.getFuelLevel() < totalDist then
  253.     print("\n\nNot enought fuel.\nProgram will be aborted")
  254.     return nil
  255. else
  256.     print(" ...OK")
  257. end
  258.  
  259. if not useChests then
  260.     print("Inventory management disabled")
  261. end
  262.  
  263. if not autoStart then
  264.     print("\nPress <Enter> to start".."\n".."Or hold <Ctrl + T> to abort")
  265.     read()
  266. end
  267.  
  268. for i = 1, height do
  269.     home()
  270.     room()
  271. end
  272.  
  273. returnToStart()
  274.  
  275. term.clear()
  276. term.setCursorPos(1, 1)
  277. print("Done!".."\n".."Total steps: "..tostring(cntr))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement