Advertisement
Snowdingerr

CC Tweaked: Constellation mining

Feb 21st, 2025 (edited)
508
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.36 KB | None | 0 0
  1. -- 0 -> North
  2. -- 1 -> East
  3. -- 2 -> South
  4. -- 3 -> West
  5.  
  6. -- start of digging tranche
  7. local tx = -6276;
  8. local ty = -10;
  9. local tz = 1380;
  10. local odir = 3;
  11.  
  12. -- parking info (unload area)
  13. local unloadX, unloadY, unloadZ = -6276, -7, 1379;
  14.  
  15. -- current info
  16. local tdir = 3;
  17. local lbtIndex = 1;
  18. local slbtIndex = 0; -- to not start the row over and over again.
  19.  
  20. function saveData()
  21.     local file = fs.open("save.txt", "w")
  22.     file.writeLine(lbtIndex)
  23.     file.writeLine(slbtIndex)
  24.     file.close()
  25. end
  26.  
  27. function checkData()
  28.     if fs.exists("save.txt") then
  29.         local file = fs.open("save.txt", "r")
  30.         lbtIndex = tonumber(file.readLine())
  31.         slbtIndex = tonumber(file.readLine())
  32.         file.close()
  33.     end
  34.     validateCurrentDirection()
  35. end
  36.  
  37. function checkInventoryFull()
  38.     local filled = false
  39.     for slot = 1, 16 do
  40.         if turtle.getItemSpace(slot) == 0 then
  41.             filled = true
  42.         elseif turtle.getItemCount(slot) == 0 then
  43.             return false
  44.         end
  45.     end
  46.     return filled
  47. end
  48.  
  49. function getCurrentLocation()
  50.     local x, y, z = gps.locate()
  51.     return x, y, z
  52. end
  53.  
  54. function turnToDirection(newDir)
  55.     while tdir ~= newDir do
  56.         local rightDir = getRight(tdir)
  57.         local leftDir = getLeft(tdir)
  58.        
  59.         if rightDir == newDir then
  60.             turtle.turnRight()
  61.             tdir = rightDir
  62.         elseif leftDir == newDir then
  63.             turtle.turnLeft()
  64.             tdir = leftDir
  65.         else
  66.             turtle.turnRight()
  67.             tdir = rightDir
  68.         end
  69.     end
  70. end
  71.  
  72. local function moveUp()
  73.     while not turtle.up() do
  74.         safeMiningUp()
  75.     end
  76. end
  77.  
  78. local function moveDown()
  79.     while not turtle.down() do
  80.         safeMiningDown()
  81.     end
  82. end
  83.  
  84. local function moveForward()
  85.     while not turtle.forward() do
  86.         safeMiningForward()
  87.     end
  88. end
  89.  
  90.  
  91. function validateCurrentDirection()
  92.     local initialX, initialY, initialZ = gps.locate()
  93.     moveForward()
  94.  
  95.     local newX, newY, newZ = gps.locate()
  96.     if newX > initialX then
  97.         tdir = 1 -- East
  98.     elseif newX < initialX then
  99.         tdir = 3 -- West
  100.     elseif newZ > initialZ then
  101.         tdir = 2 -- South
  102.     elseif newZ < initialZ then
  103.         tdir = 0 -- North
  104.     end
  105.     turtle.back()
  106. end
  107.  
  108. function gotoLocation(targetX, targetY, targetZ)
  109.     local currentX, currentY, currentZ = getCurrentLocation()
  110.  
  111.     while currentY < targetY do
  112.         moveUp()
  113.         currentY = currentY + 1
  114.     end
  115.     while currentY > targetY do
  116.         moveDown()
  117.         currentY = currentY - 1
  118.     end
  119.  
  120.     if targetX > currentX then
  121.         turnToDirection(1) -- East
  122.         while currentX < targetX do
  123.             moveForward()
  124.             currentX = currentX + 1
  125.         end
  126.     elseif targetX < currentX then
  127.         turnToDirection(3) -- West
  128.         while currentX > targetX do
  129.             moveForward()
  130.             currentX = currentX - 1
  131.         end
  132.     end
  133.  
  134.     if targetZ > currentZ then
  135.         turnToDirection(2) -- South
  136.         while currentZ < targetZ do
  137.             moveForward()
  138.             currentZ = currentZ + 1
  139.         end
  140.     elseif targetZ < currentZ then
  141.         turnToDirection(0) -- North
  142.         while currentZ > targetZ do
  143.             moveForward()
  144.             currentZ = currentZ - 1
  145.         end
  146.     end
  147. end
  148.  
  149. function safeMiningDown()
  150.     while turtle.detectDown() do
  151.         local success, data = turtle.inspectDown()
  152.         if success and data.name == "computercraft:turtle_advanced" then
  153.             os.sleep(1)
  154.         else
  155.             turtle.digDown()
  156.             return
  157.         end
  158.     end
  159. end
  160.  
  161. function safeMiningUp()
  162.     while turtle.detectUp() do
  163.         local success, data = turtle.inspectUp()
  164.         if success and data.name == "computercraft:turtle_advanced" then
  165.             os.sleep(1)
  166.         else
  167.             turtle.digUp()
  168.             return
  169.         end
  170.     end
  171. end
  172.  
  173. function safeMiningForward()
  174.     while turtle.detect() do
  175.         local success, data = turtle.inspect()
  176.         if success and data.name == "computercraft:turtle_advanced" then
  177.             os.sleep(1)
  178.         else
  179.             turtle.dig()
  180.             return
  181.         end
  182.     end
  183. end
  184.  
  185. function digAndBack()
  186.     local initialX, initialY, initialZ = gps.locate()
  187.  
  188.     while true do
  189.         if turtle.detectDown() and not turtle.digDown() then
  190.             break
  191.         end
  192.         moveDown()
  193.     end
  194.  
  195.     local currentX, currentY, currentZ = gps.locate()
  196.     while currentY < initialY do
  197.         moveUp()
  198.         currentY = currentY + 1
  199.     end
  200. end
  201.  
  202. function moveForwardRetry()
  203.     while not turtle.forward() do
  204.         if not turtle.detectUp() then
  205.             turtle.up()
  206.             os.sleep(2)
  207.             while not turtle.down() do
  208.                 os.sleep(1)
  209.             end
  210.         else
  211.             turtle.down()
  212.             os.sleep(2)
  213.             while not turtle.up() do
  214.                 os.sleep(1)
  215.             end
  216.         end
  217.  
  218.         os.sleep(1)
  219.     end
  220. end
  221.  
  222.  
  223. function turnToSouth()
  224.     while tdir ~= 2 do
  225.         turtle.turnRight()
  226.         tdir = (tdir + 1) % 4
  227.     end
  228. end
  229.  
  230. function parkAndEmpty()
  231.     turnToSouth()
  232.     moveForwardRetry()
  233.     for slot = 1, 16 do
  234.         turtle.select(slot)
  235.         turtle.dropDown()
  236.     end
  237.     moveForwardRetry()
  238. end
  239.  
  240. function trancheDigging(startX, startZ, forward, toDir)
  241.     validateCurrentDirection()
  242.     gotoLocation(startX, ty, startZ)
  243.     turnToDirection(toDir)
  244.     saveData()
  245.  
  246.     while slbtIndex > 0 do
  247.         moveForward()
  248.         slbtIndex = slbtIndex - 1
  249.         forward = forward - 1
  250.     end
  251.  
  252.     while forward > 1 do
  253.         digAndBack()
  254.         moveForward()
  255.         forward = forward - 1
  256.  
  257.         slbtIndex = slbtIndex + 1
  258.         saveData()
  259.  
  260.         if checkInventoryFull() then
  261.             gotoLocation(unloadX, unloadY, unloadZ)
  262.             parkAndEmpty()
  263.             os.reboot()
  264.             return
  265.         end
  266.     end
  267. end
  268.  
  269. function getRight(currentDir)
  270.     return (currentDir + 1) % 4
  271. end
  272.  
  273. function getLeft(currentDir)
  274.     return (currentDir + 3) % 4
  275. end
  276.  
  277. function calculateLine(startX, startZ, initialDir, rightTurns)
  278.     if initialDir == 0 then
  279.         startX = startX - 1
  280.     elseif initialDir == 2 then
  281.         startX = startX + 1
  282.     elseif initialDir == 1 then
  283.         startZ = startZ - 1
  284.     elseif initialDir == 3 then
  285.         startZ = startZ + 1
  286.     end
  287.  
  288.     local lx, lz = startX, startZ;
  289.     local x, z = startX, startZ;
  290.     local dir = initialDir;
  291.     local distance = 2;
  292.    
  293.     for turn = 1, rightTurns do
  294.         lx = x
  295.         lz = z
  296.         if dir == 0 then -- North
  297.             z = z - distance
  298.         elseif dir == 1 then -- East
  299.             x = x + distance
  300.         elseif dir == 2 then -- South
  301.             z = z + distance
  302.         elseif dir == 3 then -- West
  303.             x = x - distance
  304.         end
  305.        
  306.         dir = getRight(dir)
  307.        
  308.         distance = distance + 2
  309.     end
  310.    
  311.     return lx, lz, x, z
  312. end
  313.  
  314. function calculateDirection(x1, z1, x2, z2)
  315.     if x2 > x1 then
  316.         return 1 -- East
  317.     elseif x2 < x1 then
  318.         return 3 -- West
  319.     elseif z2 > z1 then
  320.         return 2 -- South
  321.     elseif z2 < z1 then
  322.         return 0 -- North
  323.     else
  324.         return nil
  325.     end
  326. end
  327.  
  328. function calculateDistance(x1, z1, x2, z2)
  329.     local dx = x2 - x1
  330.     local dz = z2 - z1
  331.     return math.sqrt(dx * dx + dz * dz)
  332. end
  333.  
  334. function main()
  335.     checkData()
  336.     term.clear()
  337.     term.setCursorPos(1,1)
  338.     term.write("turtle #" .. os.getComputerID())
  339.     term.setCursorPos(1,2)
  340.     term.write("fuel: " .. turtle.getFuelLevel())
  341.    
  342.     while true do
  343.         if turtle.getFuelLevel() < 2000 then
  344.             gotoLocation(unloadX, unloadY, unloadZ)
  345.             turtle.up()
  346.             turtle.up()
  347.             turtle.up()
  348.             os.shutdown()
  349.         end
  350.         if checkInventoryFull() then
  351.             gotoLocation(unloadX, unloadY, unloadZ)
  352.             parkAndEmpty()
  353.         end
  354.         local startX, startZ, endX, endZ = calculateLine(tx, tz, odir, lbtIndex)
  355.         trancheDigging(startX, startZ, calculateDistance(startX, startZ, endX, endZ), calculateDirection(startX, startZ, endX, endZ))
  356.         slbtIndex = 0
  357.         lbtIndex = lbtIndex + 1;
  358.         saveData()
  359.     end
  360. end
  361.  
  362. -- 0 -> North
  363. -- 1 -> East
  364. -- 2 -> South
  365. -- 3 -> West
  366. main()
Advertisement
Comments
  • Snowdingerr
    12 hours
    # text 0.13 KB | 0 0
    1. Update : fixed turtles destroying other turtles
    2. Update : fixed invalid path resume
    3. Update : adding tranche resume at last "digDown"
  • Snowdingerr
    10 hours
    # text 0.13 KB | 0 0
    1. Update 4 : Fixed invalid path calculation + Fixed turtle blocking eachothers + simplified first init + Removed hard codded goto.
Add Comment
Please, Sign In to add comment
Advertisement