jig487

builder

Aug 21st, 2021 (edited)
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1.  
  2. --tArgs = width, height, depth (x,y,z)  dimensions of requested CPU cluster
  3. local tArgs = {...}
  4. if not tArgs[1] or not tArgs[2] or not tArgs[3] then error("All dimensions must be specified! Example: build 2 3 2")
  5. local cX, cY, cZ = tArgs[1],tArgs[2],tArgs[3] --Get dimensions of the CPU cluster to build
  6.  
  7. --Smartly(TM) get a disk from a surrounding disk drive
  8. local function diskSearch()
  9.     for i = 1, 16 do
  10.         local data = turtle.getItemDetail(i)
  11.         if data then
  12.             if data.name == "computercraft:disk" then --Look for a disk
  13.                 print("Found a disk in inventory, slot "..i)
  14.                 print("Looking for a drive in inventory...")
  15.                 for k = 1, 16 do
  16.                     local data2 = turtle.getItemDetail(k)
  17.                     if data2 then
  18.                         if data2.name == "computercraft:disk_drive" then --look for a disk drive
  19.                             print("Found a disk drive in inventory, slot "..k)
  20.                             return
  21.                         end
  22.                     elseif k == 16 then
  23.                         error("Missing disk drive")
  24.                     end
  25.                 end
  26.             end
  27.         elseif i == 16 then
  28.             error("Missing disk")
  29.         end
  30.     end
  31. end
  32.  
  33. --Looks for the given item name as a string and returns true,slotNumber if found, else returns false,nil
  34. local function findItem(itemName)
  35.     for i = 1, 16 do
  36.         local data = turtle.getItemDetail(i)
  37.         if data then
  38.             if data.name == itemName then
  39.                 turtle.select(i)
  40.                 return true, i, data
  41.             end
  42.         elseif i == 16 then
  43.             return false, nil
  44.         end
  45.     end
  46. end
  47.  
  48. local function findBlock(blockName,face)
  49.     face = face or "front"
  50.     local data
  51.     if face == "up" then
  52.         data = turtle.inspectUp()
  53.     elseif face == "front" then
  54.         data = turtle.inspect()
  55.     elseif face == "down" then
  56.         data = turtle.inspectDown()
  57.     else
  58.         error("Face wasn't caught in case checkers")
  59.     end
  60.     if data then
  61.         if data.name == blockName then
  62.             return true
  63.         end
  64.     end
  65.     return false
  66. end
  67.  
  68. --Make sure there are enough turtles
  69. local tC = 0 --total turtle count
  70. for i = 1, 16 do
  71.     local data = turtle.getItemDetail(i)
  72.     if data then
  73.         if data.name == "computercraft:turtle_advanced" then
  74.             tC = tC + data.count
  75.         end
  76.     end
  77. end
  78. local totalT = tArgs[1]*tArgs[2]*tArgs[3]
  79. if tC < totalT then error("I only have "..tC.." turtles! I need at least "..totalT) end
  80.  
  81. --call turn() to turn the turtle. automatically switches turn direction after it's been called.
  82. local turnDir = turtle.turnRight
  83. local function turn()
  84.   turnDir()
  85.   turtle.forward()
  86.   turnDir()
  87.   turnDir = turnDir == turtle.turnRight and turtle.turnLeft or turtle.turnRight
  88. end
  89.  
  90. --Main loop
  91. diskSearch()
  92. turtle.up()
  93. turtle.forward()
  94. for y = 1, cY do
  95.     for x = 1, cX do
  96.         for z = 1, cZ do
  97.             turtle.forward()
  98.  
  99.             local bool,slot = findItem("computercraft:disk_drive")
  100.             if not bool then
  101.                 error("I can't find a disk drive")
  102.             end
  103.             turtle.select(slot)
  104.             turtle.placeDown()
  105.  
  106.             bool,slot = findItem("computercraft:disk")
  107.             if not bool then
  108.                 error("I can't find a disk")
  109.             end
  110.             turtle.select(slot)
  111.             turtle.dropDown()
  112.  
  113.             turtle.back()
  114.             bool,slot = findItem("computercraft:turtle_advanced")
  115.             if not bool then
  116.                 error("I can't find any turtles")
  117.             end
  118.             turtle.select(slot)
  119.             turtle.placeDown()
  120.            
  121.             turtle.forward()
  122.             turtle.digDown()
  123.         end
  124.         turn()
  125.         turtle.forward()
  126.     end
  127.     turtle.up()
  128.     turnDir()
  129.     turnDir()
  130.     turn()
  131. end
  132. turtle.back()
  133. turtle.back()
  134. for i = 1, cY+1 do
  135.     turtle.down()
  136. end
  137. print("CPU cluster complete.")
  138. print("Total CPUs: "..totalT)
  139. print("dimensions:")
  140. print(cX.."x"..cY.."x"..cZ)
Add Comment
Please, Sign In to add comment