-- platform_builder.lua -- Improved version - builds platforms and returns for more blocks local t = turtle -- === Settings === local CHECK_FUEL = true -- whether to check fuel local REFUEL_LEVEL = 50 -- minimum fuel level -- === State === local x, y = 0, 0 -- current position local direction = 0 -- 0=east, 1=north, 2=west, 3=south -- === Movement === local function turnLeft() t.turnLeft() direction = (direction + 1) % 4 end local function turnRight() t.turnRight() direction = (direction + 3) % 4 end local function face(targetDir) while direction ~= targetDir do if (direction - targetDir) % 4 == 1 then turnRight() else turnLeft() end end end local function forwardStep() -- Check fuel if CHECK_FUEL and t.getFuelLevel() < REFUEL_LEVEL then print("⛽ Low fuel! Trying to refuel...") for slot = 1, 16 do t.select(slot) if t.refuel(0) then -- Check if item can be used as fuel t.refuel(1) -- Refuel with one item print("✅ Refueled from slot " .. slot) break end end end -- Move forward local attempts = 0 while attempts < 5 do -- Max 5 attempts if t.forward() then -- Update coordinates if direction == 0 then x = x + 1 elseif direction == 1 then y = y + 1 elseif direction == 2 then x = x - 1 else y = y - 1 end return true elseif t.detect() then t.dig() else sleep(0.5) end attempts = attempts + 1 end print("❌ Cannot move forward!") return false end local function moveTo(targetX, targetY) -- First move along X, then along Y local dx = targetX - x local dy = targetY - y if dx > 0 then face(0) for i = 1, dx do forwardStep() end elseif dx < 0 then face(2) for i = 1, -dx do forwardStep() end end if dy > 0 then face(1) for i = 1, dy do forwardStep() end elseif dy < 0 then face(3) for i = 1, -dy do forwardStep() end end end local function goHome() print("🔄 Returning home...") moveTo(0, 0) face(0) -- Face east by default end -- === Block management === local function countBlocks() local total = 0 for slot = 1, 16 do total = total + t.getItemCount(slot) end return total end local function selectAnyBlock() for slot = 1, 16 do if t.getItemCount(slot) > 0 then t.select(slot) return true end end return false end local function waitForBlocks() print("📦 Blocks exhausted! Returning for resupply...") local savedX, savedY, savedDir = x, y, direction goHome() print("⏳ Waiting for blocks to be added...") while countBlocks() == 0 do print("📦 No blocks in inventory! Waiting...") sleep(3) end print("✅ Blocks available! Returning to work...") moveTo(savedX, savedY) face(savedDir) end local function placeBlockDown() if not t.detectDown() then if not selectAnyBlock() then waitForBlocks() selectAnyBlock() -- Select block after resupply end if not t.placeDown() then print("❌ Cannot place block! Trying again...") sleep(0.5) return false end end return true end -- === Building functions === local function buildSquare(size, fill) print("🟦 Building square " .. size .. "x" .. size) local half = math.floor(size / 2) for row = -half, half - (size % 2 == 0 and 1 or 0) do moveTo(-half, row) face(0) -- Face east for col = -half, half - (size % 2 == 0 and 1 or 0) do if fill or col == -half or col == half or row == -half or row == half then placeBlockDown() end if col < half - (size % 2 == 0 and 1 or 0) then forwardStep() end end end goHome() end local function buildRectangle(width, height, fill) print("🟦 Building rectangle " .. width .. "x" .. height) local halfW = math.floor(width / 2) local halfH = math.floor(height / 2) for row = -halfH, halfH - (height % 2 == 0 and 1 or 0) do moveTo(-halfW, row) face(0) for col = -halfW, halfW - (width % 2 == 0 and 1 or 0) do if fill or col == -halfW or col == halfW or row == -halfH or row == halfH then placeBlockDown() end if col < halfW - (width % 2 == 0 and 1 or 0) then forwardStep() end end end goHome() end local function buildCircle(radius, fill) print("🔵 Building circle with radius " .. radius) for row = -radius, radius do local width = math.floor(math.sqrt(radius * radius - row * row)) if width > 0 then moveTo(-width, row) face(0) for col = -width, width do local distance = math.sqrt(col * col + row * row) if fill or (distance >= radius - 0.5 and distance <= radius + 0.5) then placeBlockDown() end if col < width then forwardStep() end end end end goHome() end -- === Simple interface === local function main() print("=== 🏗️ PLATFORM BUILDER ===") print("Available shapes:") print("1 - Square") print("2 - Rectangle") print("3 - Circle") print("========================") -- Shape selection local choice while true do print("Choose shape (1-3):") choice = tonumber(read()) if choice and choice >= 1 and choice <= 3 then break end print("❌ Invalid choice! Try again.") end -- Fill option local fill = false print("Fill completely? (y/n):") local fillInput = read() if fillInput and string.lower(fillInput) == "y" then fill = true end -- Building parameters if choice == 1 then print("Square side size:") local size = tonumber(read()) if size and size > 0 then buildSquare(size, fill) else print("❌ Invalid size!") end elseif choice == 2 then print("Rectangle width:") local width = tonumber(read()) print("Rectangle height:") local height = tonumber(read()) if width and height and width > 0 and height > 0 then buildRectangle(width, height, fill) else print("❌ Invalid dimensions!") end elseif choice == 3 then print("Circle radius:") local radius = tonumber(read()) if radius and radius > 0 then buildCircle(radius, fill) else print("❌ Invalid radius!") end end print("✅ Construction completed!") print("📍 Turtle returned to start position") end -- Start program main()