Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- circlefill.lua - Turtle Circle Builder
- -- Compatible with CC:Tweaked 1.115.1
- -- Load config
- local config = require("config")
- -- Directions: north, east, south, west
- local directions = {
- {x = 0, z = -1}, -- north
- {x = 1, z = 0}, -- east
- {x = 0, z = 1}, -- south
- {x = -1, z = 0}, -- west
- }
- local dir = 2 -- start facing east
- local pos = {x = -config.radius, z = -config.radius} -- starting at bottom-left corner
- local function turnLeft()
- dir = (dir - 2) % 4 + 1
- turtle.turnLeft()
- end
- local function turnRight()
- dir = dir % 4 + 1
- turtle.turnRight()
- end
- local function forward()
- while turtle.detect() do turtle.dig() end
- while not turtle.forward() do sleep(0.5) end
- pos.x = pos.x + directions[dir].x
- pos.z = pos.z + directions[dir].z
- end
- local function placeBlock()
- for i = config.placeSlotStart, config.placeSlotEnd do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.placeDown()
- return true
- end
- end
- return false
- end
- local function isInCircle(x, z, r)
- return (x * x + z * z) <= (r * r)
- end
- local function faceDirection(targetDir)
- while dir ~= targetDir do
- turnRight()
- end
- end
- local function countTotalBlocks()
- local count = 0
- for z = -config.radius, config.radius do
- for x = -config.radius, config.radius do
- if isInCircle(x, z, config.radius) then
- count = count + 1
- end
- end
- end
- return count
- end
- local function countInventoryBlocks()
- local total = 0
- for i = config.placeSlotStart, config.placeSlotEnd do
- total = total + turtle.getItemCount(i)
- end
- return total
- end
- local function estimateFuelNeeded()
- local blocks = countTotalBlocks()
- local moves = blocks + config.radius * 2
- return moves
- end
- local function tryRefuel()
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.refuel(0) then
- turtle.refuel()
- end
- end
- end
- end
- local function waitForUser()
- print("Press Enter to continue after refueling or adding blocks...")
- read()
- end
- local function checkResources()
- local requiredBlocks = countTotalBlocks()
- local availableBlocks = countInventoryBlocks()
- local requiredFuel = estimateFuelNeeded()
- local availableFuel = turtle.getFuelLevel()
- local ok = true
- if availableBlocks < requiredBlocks then
- print("Not enough blocks! Needed: " .. requiredBlocks .. ", Available: " .. availableBlocks)
- ok = false
- else
- print("Block check passed: " .. availableBlocks .. "/" .. requiredBlocks)
- end
- if availableFuel < requiredFuel then
- print("Attempting auto-refuel...")
- tryRefuel()
- availableFuel = turtle.getFuelLevel()
- if availableFuel < requiredFuel then
- print("Not enough fuel! Needed: " .. requiredFuel .. ", Available: " .. availableFuel)
- ok = false
- else
- print("Fuel check passed after refuel: " .. availableFuel .. "/" .. requiredFuel)
- end
- else
- print("Fuel check passed: " .. availableFuel .. "/" .. requiredFuel)
- end
- if not ok then
- waitForUser()
- return checkResources()
- end
- return ok
- end
- local function buildFilledCircle()
- local r = config.radius
- print("Starting filled circle build with radius " .. r)
- for zOffset = -r, r do
- if (zOffset + r) % 2 == 0 then
- faceDirection(2)
- for xOffset = -r, r do
- if isInCircle(xOffset, zOffset, r) then
- placeBlock()
- end
- if xOffset < r then
- forward()
- end
- end
- else
- faceDirection(4)
- for xOffset = r, -r, -1 do
- if isInCircle(xOffset, zOffset, r) then
- placeBlock()
- end
- if xOffset > -r then
- forward()
- end
- end
- end
- if zOffset < r then
- faceDirection(3)
- forward()
- end
- end
- print("Circle complete!")
- end
- if config.mode == "circlefill" then
- if checkResources() then
- buildFilledCircle()
- else
- print("Cannot start build due to missing resources.")
- end
- else
- print("Unknown mode: " .. tostring(config.mode))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement