Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------
- -- On the Advanced Computer:
- -- Cylinder Mining Computer Script
- -- with Remote Listening via Rednet
- -------------------------------------------
- rednet.open("top") -- or whichever side your modem is on
- while true do
- local senderId, msg, prot = rednet.receive("TurtleMonitor")
- print("["..senderId.."] "..msg)
- end
- -------------------------------------------
- -- On the Advanced Mining Ender Turtle:
- -- Cylinder Mining Turtle Script
- -- with Remote Reporting via Rednet
- -- (Deposit + Return on Low Fuel)
- -------------------------------------------
- --[[
- Usage:
- CylinderMiningV3Turtle <innerRadius> <outerRadius> <height>
- Behavior Summary:
- 1. Mines a cylindrical area (outerRadius -> innerRadius) for the specified height.
- 2. If the turtle's inventory is full or fuel is below MIN_FUEL_LEVEL,
- it automatically returns to (0,0,0), deposits items, and awaits manual refuel
- (if needed). Then it resumes mining exactly where it left off.
- 3. **After completing** all layers, it returns to the origin *one last time*
- to deposit any remaining items, before printing "Cylinder Mining complete!".
- Rednet Reporting:
- - The turtle sends status messages on protocol "TurtleMonitor"
- to any listening Advanced Computer.
- Prerequisites:
- - A chest (or container) is directly above (0,0,0).
- - The turtle starts at (0,0,0) on the top layer (y=0).
- - A modem (wired or wireless) is attached to the turtle, and
- an Advanced Computer is listening for rednet messages on
- protocol "TurtleMonitor".
- ]]--
- -------------------------------------------
- -- Configuration
- -------------------------------------------
- local MIN_FUEL_LEVEL = 500 -- Fuel threshold that triggers return to origin
- local PROTOCOL = "TurtleMonitor" -- Rednet protocol
- -------------------------------------------
- -- Rednet Initialization
- -------------------------------------------
- -- Attempt to open every modem side so we can broadcast
- local function openModems()
- for _,side in ipairs({"left","right","top","bottom","front","back"}) do
- if peripheral.getType(side) == "modem" then
- if not rednet.isOpen(side) then
- rednet.open(side)
- end
- end
- end
- end
- openModems()
- -- A helper function to print and also broadcast messages
- local function report(msg)
- print(msg)
- if rednet.isOpen() then
- rednet.broadcast(msg, PROTOCOL)
- end
- end
- -------------------------------------------
- -- Parse Command Line Arguments
- -------------------------------------------
- local args = {...}
- if #args < 3 then
- report("Usage: CylinderMiningV3Turtle <innerRadius> <outerRadius> <height>")
- report("Example: CylinderMiningV3Turtle 1 5 3")
- return
- end
- local innerCircleRadius = tonumber(args[1])
- local outerCircleRadius = tonumber(args[2])
- local cylinderHeight = tonumber(args[3])
- if not innerCircleRadius or not outerCircleRadius or not cylinderHeight then
- report("All arguments must be valid numbers.")
- return
- end
- if innerCircleRadius < 1 then
- report("Inner radius must be >= 1")
- return
- end
- if outerCircleRadius < innerCircleRadius then
- report("Outer radius must be >= inner radius")
- return
- end
- -------------------------------------------
- -- Turtle State Tracking
- -------------------------------------------
- local xCoord, yCoord, zCoord = 0, 0, 0
- -- Orientation index: 1=north,2=east,3=south,4=west
- local orientation = 1
- local orientations = {"north","east","south","west"}
- local zDiff = {-1, 0, 1, 0}
- local xDiff = { 0, 1, 0,-1}
- -------------------------------------------
- -- Helper Functions
- -------------------------------------------
- local function round(n)
- return math.floor(n + 0.5)
- end
- local function withinRadius(x, z, r)
- local dist = math.sqrt(x*x + z*z)
- return round(dist) <= r
- end
- -------------------------------------------
- -- Orientation & Turning
- -------------------------------------------
- local function left()
- orientation = orientation - 1
- if orientation < 1 then
- orientation = 4
- end
- turtle.turnLeft()
- end
- local function right()
- orientation = orientation + 1
- if orientation > 4 then
- orientation = 1
- end
- turtle.turnRight()
- end
- local function aboutFace()
- left()
- left()
- end
- local function look(direction)
- local target
- for i, dir in ipairs(orientations) do
- if dir == direction then
- target = i
- break
- end
- end
- while orientation ~= target do
- right()
- end
- end
- -------------------------------------------
- -- Movement
- -------------------------------------------
- local function moveForward()
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- end
- xCoord = xCoord + xDiff[orientation]
- zCoord = zCoord + zDiff[orientation]
- end
- local function moveUp()
- while not turtle.up() do
- turtle.digUp()
- turtle.attackUp()
- end
- yCoord = yCoord + 1
- end
- local function moveDown()
- while not turtle.down() do
- turtle.digDown()
- turtle.attackDown()
- end
- yCoord = yCoord - 1
- end
- -------------------------------------------
- -- goTo(targetX, targetY, targetZ)
- -------------------------------------------
- local function goTo(targetX, targetY, targetZ)
- -- Move vertically first
- while yCoord < targetY do
- moveUp()
- end
- while yCoord > targetY do
- moveDown()
- end
- -- Move in X
- if xCoord < targetX then
- look("east")
- while xCoord < targetX do
- moveForward()
- end
- elseif xCoord > targetX then
- look("west")
- while xCoord > targetX do
- moveForward()
- end
- end
- -- Move in Z
- if zCoord < targetZ then
- look("south")
- while zCoord < targetZ do
- moveForward()
- end
- elseif zCoord > targetZ then
- look("north")
- while zCoord > targetZ do
- moveForward()
- end
- end
- end
- -------------------------------------------
- -- Deposit
- -------------------------------------------
- local function depositAll()
- for slot = 1,16 do
- local count = turtle.getItemCount(slot)
- if count > 0 then
- turtle.select(slot)
- turtle.dropUp() -- deposit into chest above
- end
- end
- turtle.select(1)
- end
- -------------------------------------------
- -- Return to Origin for Fuel/Deposit
- -------------------------------------------
- local function returnToOriginForResupply()
- -- Save current position & orientation
- local oldX, oldY, oldZ = xCoord, yCoord, zCoord
- local oldOrientation = orientation
- report("Returning to origin for resupply...")
- goTo(0, 0, 0)
- report("Arrived at origin. Depositing items...")
- depositAll()
- report("Deposit complete.")
- -- Wait for user to add fuel if below the threshold
- while turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < MIN_FUEL_LEVEL do
- report("Fuel is below "..MIN_FUEL_LEVEL.."! Please add fuel, then press Enter.")
- read()
- if turtle.getFuelLevel() < MIN_FUEL_LEVEL then
- report("Still below fuel threshold. Stopping...")
- return false
- end
- end
- report("Resupply complete. Resuming mining...")
- -- Return to old position
- goTo(oldX, oldY, oldZ)
- -- Restore orientation
- while orientation ~= oldOrientation do
- right()
- end
- report("Back to mining position. Continuing...")
- return true
- end
- -------------------------------------------
- -- Checks
- -------------------------------------------
- -- 1) Check Fuel
- local function checkFuel()
- if turtle.getFuelLevel() == "unlimited" then
- return true
- end
- if turtle.getFuelLevel() < MIN_FUEL_LEVEL then
- report("Fuel low! ("..turtle.getFuelLevel()..") Returning to origin...")
- if not returnToOriginForResupply() then
- return false
- end
- end
- return true
- end
- -- 2) Check Inventory
- local function checkInventorySpace()
- for i=1,16 do
- if turtle.getItemCount(i) == 0 then
- return true
- end
- end
- report("Inventory full! Returning to origin to deposit...")
- if not returnToOriginForResupply() then
- return false
- end
- return true
- end
- -------------------------------------------
- -- Ring-Digging Logic
- -------------------------------------------
- local function forwardWithinRadius(r)
- local xNext = xCoord + xDiff[orientation]
- local zNext = zCoord + zDiff[orientation]
- return withinRadius(xNext, zNext, r)
- end
- local function gotoEdge(r)
- while forwardWithinRadius(r) do
- if not checkFuel() then return false end
- if not checkInventorySpace() then return false end
- moveForward()
- end
- left()
- return true
- end
- local function digRing(r)
- local startX, startZ = xCoord, zCoord
- repeat
- while forwardWithinRadius(r) do
- if not checkFuel() then return end
- if not checkInventorySpace() then return end
- moveForward()
- end
- while not forwardWithinRadius(r) do
- left()
- end
- if checkFuel() and checkInventorySpace() then
- moveForward()
- right()
- else
- return
- end
- until (xCoord == startX and zCoord == startZ)
- -- Return to center
- aboutFace()
- for i=1,r do
- moveForward()
- end
- aboutFace()
- end
- local function digLayer(rInner, rOuter)
- for r = rOuter, rInner, -1 do
- report("Digging ring of radius "..r)
- look("east")
- for i=1, r do
- if not checkFuel() then return end
- if not checkInventorySpace() then return end
- moveForward()
- end
- if not gotoEdge(r) then return end
- digRing(r)
- end
- end
- -------------------------------------------
- -- Main Cylinder-Digging Routine
- -------------------------------------------
- local function digCylinder(rInner, rOuter, height)
- for lvl = 1, height do
- report("Starting layer "..lvl.." of "..height)
- digLayer(rInner, rOuter)
- if lvl < height then
- report("Layer "..lvl.." complete, moving down...")
- moveDown()
- end
- end
- end
- -------------------------------------------
- -- Main
- -------------------------------------------
- report("Starting Cylinder Mining with Remote Reporting...")
- report(" Inner radius: "..innerCircleRadius)
- report(" Outer radius: "..outerCircleRadius)
- report(" Height : "..cylinderHeight)
- report("------------------------------------------")
- report("Chest above origin for depositing.")
- report("Turtle returns to origin if inventory is full or fuel < "..MIN_FUEL_LEVEL)
- report("Manual refuel required at origin. Monitoring on protocol: "..PROTOCOL)
- report("------------------------------------------")
- digCylinder(innerCircleRadius, outerCircleRadius, cylinderHeight)
- report("Depositing final items...")
- depositAll()
- report("Final deposit complete.")
- report("Cylinder Mining complete!")
Advertisement
Comments
-
- My first updated version: https://pastebin.com/5PTA6E12
- Original post: https://pastebin.com/TgDE8CSY
Add Comment
Please, Sign In to add comment
Advertisement