Advertisement
DizzyFoxkit

cc_quarry

Jan 10th, 2025 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.17 KB | Gaming | 0 0
  1.  
  2. -- Start of my quarry 'script'.
  3. -- Learning Lua so this may be rough.
  4. -- Idea is to just mine out a Z * X size cube until the turtle hits bottom.
  5.  
  6. --[[ things to worry about:
  7. -- Fuel levels
  8. -- Inventory levels
  9. -- distance from home
  10. ]]--
  11.  
  12.  
  13. local __update = false -- Making use of an autoupdater script I found, by ensuring this script is always up to date, and only updates upon request instead of whenever I run it. (May actually end up making something akin to apt, yum, etc.)
  14.  
  15. local __version = 0.01
  16. -- Some of these are future proof definitions. Small program, and small inefficiencies like extra memory shouldn't hog server resources _too_ much. If I start killing the server I can adjust.
  17. local __home  = 0
  18. local __returnhome = false
  19. local currentX = 0
  20. local currentY = 0
  21. local currentZ = 0
  22.  
  23. local __paused = false
  24. local __direction = "north"
  25.  
  26. local function turnWest()
  27.   if __direction == "north" then
  28.     turtle.turnLeft()
  29.     __direction = "west"
  30.   end
  31.   if __direction == "east" then
  32.     turtle.turnRight()
  33.     turtle.turnRight()
  34.     __direction = "west"
  35.   end
  36.   if __direction == "south" then
  37.     turtle.turnRight()
  38.     __direction = "west"
  39.   end
  40. end
  41.  
  42. local function turnSouth()
  43.   if __direction == "north" then
  44.     turtle.turnRight()
  45.     turtle.turnRight()
  46.   elseif __direction == "east" then
  47.     turtle.turnRight()
  48.   elseif __direction == "west" then
  49.     turtle.turnLeft()
  50.   end
  51. end
  52.  
  53. local function distanceToHome() -- Simple distance calculation.
  54.   return currentX + currentY + currentZ
  55. end
  56.  
  57. local function safeFuelLevel(currentX, currentY, currentZ)
  58.   local safeFuelLevel = true
  59.   local safetyCushion = 10
  60.   if turtle.getFuelLevel() <= (distanceToHome() + safetyCushion) then
  61.     safeFuelLevel = false
  62.     print("Debug: fuel level safety trigger flagged.")
  63.   end
  64.   return safeFuelLevel
  65. end
  66.  
  67. local function tryDropItems()
  68.   for i = 2, 16 do
  69.     turtle.select(i)
  70.     if not turtle.dropUp() then
  71.       -- If we can't drop, we're not at an inventory
  72.       return false
  73.     end
  74.   end
  75.   turtle.select(1)
  76.   return true
  77. end
  78.  
  79. local function returnHome()
  80.   while currentX > 0 do
  81.     if __direction ~= "west" then
  82.       turtle.turnRight()
  83.     end
  84.     turtle.forward()
  85.   end
  86.  
  87.   while currentY > 0 do
  88.     if __direction ~= "south" then
  89.       turnSouth()
  90.     end
  91.     turtle.forward()
  92.   end
  93.  
  94.   while currentZ > 0 do
  95.     turtle.up()
  96.   end
  97.  
  98.   turnSouth()
  99.   if tryDropItems() ~= true then
  100.     print("Danger Will Robinson! We don't have an inventory!")
  101.   end
  102. end
  103.  
  104. local function inventoryNotFull()
  105.   if turtle.getItemCount(16) > 0 then
  106.     print("Debug: inventory level safety trigger flagged.")
  107.     return false
  108.   else return true
  109.   end
  110. end
  111.  
  112. local function fuelAndInventorySafe()
  113.   print("Debug: fuelAndInventorySafe function called.")
  114.   print("Debug: inventoryNotFull() status: " .. tostring(inventoryNotFull()))
  115.   print("Debug: safeFuelLevel() status: " .. tostring(safeFuelLevel(currentX, currentY, currentZ)))
  116.   if inventoryNotFull() ~= true then
  117.     __returnhome = true
  118.     returnHome()
  119.   end
  120.   if safeFuelLevel(currentX, currentY, currentZ) ~= true then
  121.     __returnhome = true
  122.     returnHome()
  123.   end
  124.   return true
  125. end
  126.  
  127. local function quarry(width, length) --width = x, length = y
  128.   if width == nil then
  129.     width = 0
  130.   end
  131.   if length == nil then
  132.     length = 0
  133.   end
  134.  
  135.   print("Debug: Sanity Checks")
  136.   local inventoryStatus = inventoryNotFull()
  137.   local fuelStatus = safeFuelLevel(currentX, currentY, currentZ)
  138.   print("Debug: Inventory status: " .. tostring(inventoryStatus))
  139.   print("Debug: Fuel status: " .. tostring(fuelStatus))
  140.   print("Debug: width: " .. tostring(width))
  141.   print("Debug: length: " .. tostring(length))
  142.   print("Debug: currentX: " .. tostring(currentX))
  143.   print("Debug: currentY: " .. tostring(currentY))
  144.   print("Debug: currentZ: " .. tostring(currentZ))
  145.  
  146.   local reverseY = false
  147.   local reverseX = false
  148.  
  149.   --[ loop flow
  150.   -- 1. Check if something is in front
  151.   -- 1a. if something is in front. Mine it
  152.   -- 2. Move forward, increment currentY
  153.   -- 3. Repeat 1 and 2 until currentY equals length
  154.   -- 4. turn right if currentX % 2 = 0, else turn left
  155.   -- 4a. repeat step one and 2
  156.   -- 4b. repeat step 4 & 4a
  157.   -- 5. repeat steps 1-4 until currentX equals width
  158.   -- 6. Detect down
  159.   -- 6a. If something is down, mine it
  160.   -- 6b. Move down
  161.   -- 6c. move 180 degrees.
  162.   -- 7. Repeat.
  163.   -- ]
  164.   --
  165.   -- Home logic flow
  166.   -- Fuel? -> Home Distance? -> Full inventory?
  167.   while __returnhome ~= true do
  168.     -- Check for fuel and inventory here?
  169.     -- Sanity check
  170.     print("Debug: __returnhome loop call")
  171.  
  172.     while currentX < width and currentX >= 0 do
  173.       -- Sanity check
  174.       print("Debug: width loop call")
  175.  
  176.       -- Fuel and Inventory check.
  177.       --
  178.       if not fuelAndInventorySafe() then
  179.         print("Debug: __returnhome flag triggered in width loop")
  180.         __returnhome = true
  181.         break
  182.       end
  183.  
  184.       --Debug insanity, while loop is not being entered even though it has the _exact_ same logic as the previous one.
  185.       print("Debug: Y condition check:")
  186.       print("currentY < length: " .. tostring(currentY < length))
  187.       print("currentY >= 0: " .. tostring(currentY >= 0))
  188.       print("Combined condition: " .. tostring(currentY < length and currentY >= 0))
  189.  
  190.       while currentY < length and currentY >= 0 do
  191.         -- Sanity Debug check
  192.         print("Debug: length loop call")
  193.  
  194.         -- Fuel and Inventory check.
  195.         --
  196.  
  197.         if not fuelAndInventorySafe() then
  198.           print("Debug: __returnhome flag triggered in length loop")
  199.           __returnhome = true
  200.           returnHome()
  201.           break
  202.         end
  203.  
  204.         local success, data = turtle.inspect()
  205.         if success then
  206.           if data.name == "minecraft:bedrock" then
  207.             __returnhome = true
  208.             returnHome()
  209.             break
  210.           end
  211.  
  212.           turtle.dig()
  213.           turtle.forward()
  214.         else turtle.forward()
  215.         end
  216.  
  217.         if reverseY == false then
  218.           currentY = currentY + 1
  219.         else currentY = currentY - 1
  220.         end
  221.       end
  222.  
  223.       print("Debug: Preparing turn. Current direction: " .. __direction)
  224.       if currentX % 2 == 0 then
  225.         print("Debug: Turning right")
  226.         turtle.turnRight()
  227.         __direction = "east"
  228.       else turtle.turnLeft()
  229.         print("Debug: Turning left")
  230.         __direction = "west"
  231.       end
  232.       local success, data = turtle.inspect()
  233.       if success then
  234.         if data.name == "minecraft:bedrock" then
  235.           __returnhome = true
  236.           returnHome()
  237.           break
  238.         end
  239.         turtle.dig()
  240.         turtle.forward()
  241.       else turtle.forward()
  242.       end
  243.       print("Debug: Preparing second turn. Current direction: " .. __direction)
  244.       if currentX % 2 == 0 then
  245.         print("Debug: Turning right part 2")
  246.         turtle.turnRight()
  247.         __direction = "south"
  248.       else turtle.turnLeft()
  249.         print("Debug: Turning left part 2")
  250.         __direction = "north"
  251.       end
  252.       if reverseX == false then
  253.         currentX = currentX + 1
  254.       else currentX = currentX - 1
  255.       end
  256.       if reverseY == false then
  257.         reverseY = true
  258.       else reverseY = false
  259.       end
  260.       local success, data = turtle.inspect()
  261.       if success then
  262.         if data.name == "minecraft:bedrock" then
  263.           __returnhome =  true
  264.           returnHome()
  265.           break
  266.         end
  267.         turtle.dig()
  268.         turtle.forward()
  269.       else turtle.forward()
  270.       end
  271.     end
  272.    
  273.     local success, data = turtle.inspectDown()
  274.     if success then
  275.       if data.name == "minecraft:bedrock" then
  276.         __returnhome = true
  277.         returnHome()
  278.         break
  279.       end
  280.       turtle.digDown()
  281.       turtle.down()
  282.     else turtle.down()
  283.     end
  284.     currentZ = currentZ + 1
  285.     turtle.turnRight()
  286.     turtle.turnRight()
  287.     if __direction == "north" then
  288.       __direction = "south"
  289.     else __direction = "north"
  290.     end
  291.     if reverseX == false then
  292.       reverseX = true
  293.     else reverseX = false
  294.     end
  295.     if reverseY == false then
  296.       reverseY = true
  297.     else reverseY = false
  298.     end
  299.   end
  300. end
  301.  
  302. quarry(16, 16)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement