Advertisement
Xentiles

CC: Tweaked - Cylinder Mining Turtle V3

Jan 22nd, 2025 (edited)
260
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.23 KB | Gaming | 0 0
  1. -------------------------------------------
  2. -- On the Advanced Computer:
  3. -- Cylinder Mining Computer Script
  4. -- with Remote Listening via Rednet
  5. -------------------------------------------
  6.  
  7. rednet.open("top")  -- or whichever side your modem is on
  8. while true do
  9.     local senderId, msg, prot = rednet.receive("TurtleMonitor")
  10.     print("["..senderId.."] "..msg)
  11. end
  12.  
  13.  
  14.  
  15. -------------------------------------------
  16. -- On the Advanced Mining Ender Turtle:
  17. -- Cylinder Mining Turtle Script
  18. -- with Remote Reporting via Rednet
  19. -- (Deposit + Return on Low Fuel)
  20. -------------------------------------------
  21.  
  22. --[[
  23. Usage:
  24.   CylinderMiningV3Turtle <innerRadius> <outerRadius> <height>
  25.  
  26. Behavior Summary:
  27.   1. Mines a cylindrical area (outerRadius -> innerRadius) for the specified height.
  28.   2. If the turtle's inventory is full or fuel is below MIN_FUEL_LEVEL,
  29.      it automatically returns to (0,0,0), deposits items, and awaits manual refuel
  30.      (if needed). Then it resumes mining exactly where it left off.
  31.   3. **After completing** all layers, it returns to the origin *one last time*
  32.      to deposit any remaining items, before printing "Cylinder Mining complete!".
  33.  
  34. Rednet Reporting:
  35.   - The turtle sends status messages on protocol "TurtleMonitor"
  36.     to any listening Advanced Computer.
  37.  
  38. Prerequisites:
  39.   - A chest (or container) is directly above (0,0,0).
  40.   - The turtle starts at (0,0,0) on the top layer (y=0).
  41.   - A modem (wired or wireless) is attached to the turtle, and
  42.     an Advanced Computer is listening for rednet messages on
  43.     protocol "TurtleMonitor".
  44. ]]--
  45.  
  46. -------------------------------------------
  47. -- Configuration
  48. -------------------------------------------
  49. local MIN_FUEL_LEVEL = 500  -- Fuel threshold that triggers return to origin
  50. local PROTOCOL = "TurtleMonitor"  -- Rednet protocol
  51.  
  52. -------------------------------------------
  53. -- Rednet Initialization
  54. -------------------------------------------
  55. -- Attempt to open every modem side so we can broadcast
  56. local function openModems()
  57.     for _,side in ipairs({"left","right","top","bottom","front","back"}) do
  58.         if peripheral.getType(side) == "modem" then
  59.             if not rednet.isOpen(side) then
  60.                 rednet.open(side)
  61.             end
  62.         end
  63.     end
  64. end
  65. openModems()
  66.  
  67. -- A helper function to print and also broadcast messages
  68. local function report(msg)
  69.     print(msg)
  70.     if rednet.isOpen() then
  71.         rednet.broadcast(msg, PROTOCOL)
  72.     end
  73. end
  74.  
  75. -------------------------------------------
  76. -- Parse Command Line Arguments
  77. -------------------------------------------
  78. local args = {...}
  79. if #args < 3 then
  80.     report("Usage: CylinderMiningV3Turtle <innerRadius> <outerRadius> <height>")
  81.     report("Example: CylinderMiningV3Turtle 1 5 3")
  82.     return
  83. end
  84.  
  85. local innerCircleRadius = tonumber(args[1])
  86. local outerCircleRadius = tonumber(args[2])
  87. local cylinderHeight    = tonumber(args[3])
  88.  
  89. if not innerCircleRadius or not outerCircleRadius or not cylinderHeight then
  90.     report("All arguments must be valid numbers.")
  91.     return
  92. end
  93.  
  94. if innerCircleRadius < 1 then
  95.     report("Inner radius must be >= 1")
  96.     return
  97. end
  98.  
  99. if outerCircleRadius < innerCircleRadius then
  100.     report("Outer radius must be >= inner radius")
  101.     return
  102. end
  103.  
  104. -------------------------------------------
  105. -- Turtle State Tracking
  106. -------------------------------------------
  107. local xCoord, yCoord, zCoord = 0, 0, 0
  108. -- Orientation index: 1=north,2=east,3=south,4=west
  109. local orientation = 1
  110. local orientations = {"north","east","south","west"}
  111.  
  112. local zDiff = {-1, 0, 1, 0}
  113. local xDiff = { 0, 1, 0,-1}
  114.  
  115. -------------------------------------------
  116. -- Helper Functions
  117. -------------------------------------------
  118. local function round(n)
  119.     return math.floor(n + 0.5)
  120. end
  121.  
  122. local function withinRadius(x, z, r)
  123.     local dist = math.sqrt(x*x + z*z)
  124.     return round(dist) <= r
  125. end
  126.  
  127. -------------------------------------------
  128. -- Orientation & Turning
  129. -------------------------------------------
  130. local function left()
  131.     orientation = orientation - 1
  132.     if orientation < 1 then
  133.         orientation = 4
  134.     end
  135.     turtle.turnLeft()
  136. end
  137.  
  138. local function right()
  139.     orientation = orientation + 1
  140.     if orientation > 4 then
  141.         orientation = 1
  142.     end
  143.     turtle.turnRight()
  144. end
  145.  
  146. local function aboutFace()
  147.     left()
  148.     left()
  149. end
  150.  
  151. local function look(direction)
  152.     local target
  153.     for i, dir in ipairs(orientations) do
  154.         if dir == direction then
  155.             target = i
  156.             break
  157.         end
  158.     end
  159.     while orientation ~= target do
  160.         right()
  161.     end
  162. end
  163.  
  164. -------------------------------------------
  165. -- Movement
  166. -------------------------------------------
  167. local function moveForward()
  168.     while not turtle.forward() do
  169.         turtle.dig()
  170.         turtle.attack()
  171.     end
  172.     xCoord = xCoord + xDiff[orientation]
  173.     zCoord = zCoord + zDiff[orientation]
  174. end
  175.  
  176. local function moveUp()
  177.     while not turtle.up() do
  178.         turtle.digUp()
  179.         turtle.attackUp()
  180.     end
  181.     yCoord = yCoord + 1
  182. end
  183.  
  184. local function moveDown()
  185.     while not turtle.down() do
  186.         turtle.digDown()
  187.         turtle.attackDown()
  188.     end
  189.     yCoord = yCoord - 1
  190. end
  191.  
  192. -------------------------------------------
  193. -- goTo(targetX, targetY, targetZ)
  194. -------------------------------------------
  195. local function goTo(targetX, targetY, targetZ)
  196.     -- Move vertically first
  197.     while yCoord < targetY do
  198.         moveUp()
  199.     end
  200.     while yCoord > targetY do
  201.         moveDown()
  202.     end
  203.  
  204.     -- Move in X
  205.     if xCoord < targetX then
  206.         look("east")
  207.         while xCoord < targetX do
  208.             moveForward()
  209.         end
  210.     elseif xCoord > targetX then
  211.         look("west")
  212.         while xCoord > targetX do
  213.             moveForward()
  214.         end
  215.     end
  216.  
  217.     -- Move in Z
  218.     if zCoord < targetZ then
  219.         look("south")
  220.         while zCoord < targetZ do
  221.             moveForward()
  222.         end
  223.     elseif zCoord > targetZ then
  224.         look("north")
  225.         while zCoord > targetZ do
  226.             moveForward()
  227.         end
  228.     end
  229. end
  230.  
  231. -------------------------------------------
  232. -- Deposit
  233. -------------------------------------------
  234. local function depositAll()
  235.     for slot = 1,16 do
  236.         local count = turtle.getItemCount(slot)
  237.         if count > 0 then
  238.             turtle.select(slot)
  239.             turtle.dropUp()  -- deposit into chest above
  240.         end
  241.     end
  242.     turtle.select(1)
  243. end
  244.  
  245. -------------------------------------------
  246. -- Return to Origin for Fuel/Deposit
  247. -------------------------------------------
  248. local function returnToOriginForResupply()
  249.     -- Save current position & orientation
  250.     local oldX, oldY, oldZ = xCoord, yCoord, zCoord
  251.     local oldOrientation   = orientation
  252.  
  253.     report("Returning to origin for resupply...")
  254.     goTo(0, 0, 0)
  255.     report("Arrived at origin. Depositing items...")
  256.     depositAll()
  257.     report("Deposit complete.")
  258.  
  259.     -- Wait for user to add fuel if below the threshold
  260.     while turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < MIN_FUEL_LEVEL do
  261.         report("Fuel is below "..MIN_FUEL_LEVEL.."! Please add fuel, then press Enter.")
  262.         read()
  263.         if turtle.getFuelLevel() < MIN_FUEL_LEVEL then
  264.             report("Still below fuel threshold. Stopping...")
  265.             return false
  266.         end
  267.     end
  268.  
  269.     report("Resupply complete. Resuming mining...")
  270.     -- Return to old position
  271.     goTo(oldX, oldY, oldZ)
  272.  
  273.     -- Restore orientation
  274.     while orientation ~= oldOrientation do
  275.         right()
  276.     end
  277.     report("Back to mining position. Continuing...")
  278.  
  279.     return true
  280. end
  281.  
  282. -------------------------------------------
  283. -- Checks
  284. -------------------------------------------
  285. -- 1) Check Fuel
  286. local function checkFuel()
  287.     if turtle.getFuelLevel() == "unlimited" then
  288.         return true
  289.     end
  290.     if turtle.getFuelLevel() < MIN_FUEL_LEVEL then
  291.         report("Fuel low! ("..turtle.getFuelLevel()..") Returning to origin...")
  292.         if not returnToOriginForResupply() then
  293.             return false
  294.         end
  295.     end
  296.     return true
  297. end
  298.  
  299. -- 2) Check Inventory
  300. local function checkInventorySpace()
  301.     for i=1,16 do
  302.         if turtle.getItemCount(i) == 0 then
  303.             return true
  304.         end
  305.     end
  306.     report("Inventory full! Returning to origin to deposit...")
  307.     if not returnToOriginForResupply() then
  308.         return false
  309.     end
  310.     return true
  311. end
  312.  
  313. -------------------------------------------
  314. -- Ring-Digging Logic
  315. -------------------------------------------
  316. local function forwardWithinRadius(r)
  317.     local xNext = xCoord + xDiff[orientation]
  318.     local zNext = zCoord + zDiff[orientation]
  319.     return withinRadius(xNext, zNext, r)
  320. end
  321.  
  322. local function gotoEdge(r)
  323.     while forwardWithinRadius(r) do
  324.         if not checkFuel() then return false end
  325.         if not checkInventorySpace() then return false end
  326.         moveForward()
  327.     end
  328.     left()
  329.     return true
  330. end
  331.  
  332. local function digRing(r)
  333.     local startX, startZ = xCoord, zCoord
  334.  
  335.     repeat
  336.         while forwardWithinRadius(r) do
  337.             if not checkFuel() then return end
  338.             if not checkInventorySpace() then return end
  339.             moveForward()
  340.         end
  341.  
  342.         while not forwardWithinRadius(r) do
  343.             left()
  344.         end
  345.  
  346.         if checkFuel() and checkInventorySpace() then
  347.             moveForward()
  348.             right()
  349.         else
  350.             return
  351.         end
  352.     until (xCoord == startX and zCoord == startZ)
  353.  
  354.     -- Return to center
  355.     aboutFace()
  356.     for i=1,r do
  357.         moveForward()
  358.     end
  359.     aboutFace()
  360. end
  361.  
  362. local function digLayer(rInner, rOuter)
  363.     for r = rOuter, rInner, -1 do
  364.         report("Digging ring of radius "..r)
  365.         look("east")
  366.         for i=1, r do
  367.             if not checkFuel() then return end
  368.             if not checkInventorySpace() then return end
  369.             moveForward()
  370.         end
  371.  
  372.         if not gotoEdge(r) then return end
  373.         digRing(r)
  374.     end
  375. end
  376.  
  377. -------------------------------------------
  378. -- Main Cylinder-Digging Routine
  379. -------------------------------------------
  380. local function digCylinder(rInner, rOuter, height)
  381.     for lvl = 1, height do
  382.         report("Starting layer "..lvl.." of "..height)
  383.         digLayer(rInner, rOuter)
  384.         if lvl < height then
  385.             report("Layer "..lvl.." complete, moving down...")
  386.             moveDown()
  387.         end
  388.     end
  389. end
  390.  
  391. -------------------------------------------
  392. -- Main
  393. -------------------------------------------
  394. report("Starting Cylinder Mining with Remote Reporting...")
  395. report("  Inner radius: "..innerCircleRadius)
  396. report("  Outer radius: "..outerCircleRadius)
  397. report("  Height      : "..cylinderHeight)
  398. report("------------------------------------------")
  399. report("Chest above origin for depositing.")
  400. report("Turtle returns to origin if inventory is full or fuel < "..MIN_FUEL_LEVEL)
  401. report("Manual refuel required at origin. Monitoring on protocol: "..PROTOCOL)
  402. report("------------------------------------------")
  403.  
  404. digCylinder(innerCircleRadius, outerCircleRadius, cylinderHeight)
  405.  
  406. report("Depositing final items...")
  407. depositAll()
  408. report("Final deposit complete.")
  409.  
  410. report("Cylinder Mining complete!")
Advertisement
Comments
  • Xentiles
    141 days
    # text 0.10 KB | 0 0
    1. My first updated version: https://pastebin.com/5PTA6E12
    2. Original post: https://pastebin.com/TgDE8CSY
Add Comment
Please, Sign In to add comment
Advertisement