Advertisement
bdstout1617

cgpt_filledCircle

Apr 25th, 2025 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. -- circlefill.lua - Turtle Circle Builder
  2. -- Compatible with CC:Tweaked 1.115.1
  3.  
  4. -- Load config
  5. local config = require("config")
  6.  
  7. -- Directions: north, east, south, west
  8. local directions = {
  9.     {x = 0, z = -1}, -- north
  10.     {x = 1, z = 0},  -- east
  11.     {x = 0, z = 1},  -- south
  12.     {x = -1, z = 0}, -- west
  13. }
  14.  
  15. local dir = 2 -- start facing east
  16. local pos = {x = -config.radius, z = -config.radius} -- starting at bottom-left corner
  17.  
  18. local function turnLeft()
  19.     dir = (dir - 2) % 4 + 1
  20.     turtle.turnLeft()
  21. end
  22.  
  23. local function turnRight()
  24.     dir = dir % 4 + 1
  25.     turtle.turnRight()
  26. end
  27.  
  28. local function forward()
  29.     while turtle.detect() do turtle.dig() end
  30.     while not turtle.forward() do sleep(0.5) end
  31.     pos.x = pos.x + directions[dir].x
  32.     pos.z = pos.z + directions[dir].z
  33. end
  34.  
  35. local function placeBlock()
  36.     for i = config.placeSlotStart, config.placeSlotEnd do
  37.         if turtle.getItemCount(i) > 0 then
  38.             turtle.select(i)
  39.             turtle.placeDown()
  40.             return true
  41.         end
  42.     end
  43.     return false
  44. end
  45.  
  46. local function isInCircle(x, z, r)
  47.     return (x * x + z * z) <= (r * r)
  48. end
  49.  
  50. local function faceDirection(targetDir)
  51.     while dir ~= targetDir do
  52.         turnRight()
  53.     end
  54. end
  55.  
  56. local function countTotalBlocks()
  57.     local count = 0
  58.     for z = -config.radius, config.radius do
  59.         for x = -config.radius, config.radius do
  60.             if isInCircle(x, z, config.radius) then
  61.                 count = count + 1
  62.             end
  63.         end
  64.     end
  65.     return count
  66. end
  67.  
  68. local function countInventoryBlocks()
  69.     local total = 0
  70.     for i = config.placeSlotStart, config.placeSlotEnd do
  71.         total = total + turtle.getItemCount(i)
  72.     end
  73.     return total
  74. end
  75.  
  76. local function estimateFuelNeeded()
  77.     local blocks = countTotalBlocks()
  78.     local moves = blocks + config.radius * 2
  79.     return moves
  80. end
  81.  
  82. local function tryRefuel()
  83.     for i = 1, 16 do
  84.         if turtle.getItemCount(i) > 0 then
  85.             turtle.select(i)
  86.             if turtle.refuel(0) then
  87.                 turtle.refuel()
  88.             end
  89.         end
  90.     end
  91. end
  92.  
  93. local function waitForUser()
  94.     print("Press Enter to continue after refueling or adding blocks...")
  95.     read()
  96. end
  97.  
  98. local function checkResources()
  99.     local requiredBlocks = countTotalBlocks()
  100.     local availableBlocks = countInventoryBlocks()
  101.  
  102.     local requiredFuel = estimateFuelNeeded()
  103.     local availableFuel = turtle.getFuelLevel()
  104.  
  105.     local ok = true
  106.  
  107.     if availableBlocks < requiredBlocks then
  108.         print("Not enough blocks! Needed: " .. requiredBlocks .. ", Available: " .. availableBlocks)
  109.         ok = false
  110.     else
  111.         print("Block check passed: " .. availableBlocks .. "/" .. requiredBlocks)
  112.     end
  113.  
  114.     if availableFuel < requiredFuel then
  115.         print("Attempting auto-refuel...")
  116.         tryRefuel()
  117.         availableFuel = turtle.getFuelLevel()
  118.         if availableFuel < requiredFuel then
  119.             print("Not enough fuel! Needed: " .. requiredFuel .. ", Available: " .. availableFuel)
  120.             ok = false
  121.         else
  122.             print("Fuel check passed after refuel: " .. availableFuel .. "/" .. requiredFuel)
  123.         end
  124.     else
  125.         print("Fuel check passed: " .. availableFuel .. "/" .. requiredFuel)
  126.     end
  127.  
  128.     if not ok then
  129.         waitForUser()
  130.         return checkResources()
  131.     end
  132.  
  133.     return ok
  134. end
  135.  
  136. local function buildFilledCircle()
  137.     local r = config.radius
  138.  
  139.     print("Starting filled circle build with radius " .. r)
  140.  
  141.     for zOffset = -r, r do
  142.         if (zOffset + r) % 2 == 0 then
  143.             faceDirection(2)
  144.             for xOffset = -r, r do
  145.                 if isInCircle(xOffset, zOffset, r) then
  146.                     placeBlock()
  147.                 end
  148.                 if xOffset < r then
  149.                     forward()
  150.                 end
  151.             end
  152.         else
  153.             faceDirection(4)
  154.             for xOffset = r, -r, -1 do
  155.                 if isInCircle(xOffset, zOffset, r) then
  156.                     placeBlock()
  157.                 end
  158.                 if xOffset > -r then
  159.                     forward()
  160.                 end
  161.             end
  162.         end
  163.  
  164.         if zOffset < r then
  165.             faceDirection(3)
  166.             forward()
  167.         end
  168.     end
  169.  
  170.     print("Circle complete!")
  171. end
  172.  
  173. if config.mode == "circlefill" then
  174.     if checkResources() then
  175.         buildFilledCircle()
  176.     else
  177.         print("Cannot start build due to missing resources.")
  178.     end
  179. else
  180.     print("Unknown mode: " .. tostring(config.mode))
  181. end
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement