Advertisement
jaklsfjlsak

dest修复激光测距采矿,自动停止

Aug 14th, 2023 (edited)
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. term.clear()
  2. local laser = peripheral.find("warpdriveLaser")
  3. local mininglasers = {}
  4. local sides = peripheral.getNames()
  5.  
  6. local function saveZeroLayersCount(count)
  7.   local file = fs.open("zero_layers.txt", "w")
  8.   file.write(tostring(count))
  9.   file.close()
  10. end
  11.  
  12. local function getZeroLayersCount()
  13.   if fs.exists("zero_layers.txt") then
  14.     local file = fs.open("zero_layers.txt", "r")
  15.     local count = tonumber(file.readAll())
  16.     file.close()
  17.     return count
  18.   end
  19.   return 0
  20. end
  21.  
  22. for _, side in pairs(sides) do
  23.   if peripheral.getType(side) == "warpdriveMiningLaser" then
  24.     table.insert(mininglasers, peripheral.wrap(side))
  25.   end
  26. end
  27.  
  28. laser.beamFrequency(1420)
  29.  
  30. if not laser then
  31.   print("No warpdriveLaser detected")
  32.   os.exit()
  33. end
  34.  
  35. if #mininglasers == 0 then
  36.   print("No warpdriveMiningLaser detected")
  37.   os.exit()
  38. end
  39.  
  40. print("Press the 'M' key to emit a laser scan and start the mining lasers with calculated layer offset.")
  41.  
  42. -- Loop to wait for key events
  43. while true do
  44.   local event, key = os.pullEvent("key")
  45.  
  46.   if key == 50 then
  47.     local _, laserY, _ = laser.getLocalPosition()
  48.     local targetY = 0
  49.  
  50.     repeat
  51.       laser.emitBeam(0, -1, 0)
  52.       os.sleep(1)
  53.       _, _, targetY = laser.getScanResult()
  54.     until targetY ~= 0
  55.  
  56.     local mineTarget = laserY - targetY - 1
  57.     print("Target is: " .. mineTarget .. " blocks below")
  58.  
  59.     for _, mininglaser in pairs(mininglasers) do
  60.       mininglaser.offset(mineTarget)
  61.       mininglaser.enable(true)
  62.     end
  63.  
  64.     local consecutiveZeroLayers = getZeroLayersCount()
  65.  
  66.     -- Periodically check the mining state
  67.     while true do
  68.       os.sleep(1)
  69.       local state, isActive, energy, currentLayer, mined, total = mininglasers[1].state()
  70.  
  71.       -- You'll need to modify the below lines once you understand the structure of 'state'
  72.  
  73.       if total == 0 then
  74.         consecutiveZeroLayers = consecutiveZeroLayers + 1
  75.       else
  76.         consecutiveZeroLayers = 0
  77.       end
  78.  
  79.       saveZeroLayersCount(consecutiveZeroLayers)
  80.  
  81.       if consecutiveZeroLayers >= 3 then
  82.         print("3 consecutive layers with 0 total blocks detected. Stopping mining.")
  83.         print("")
  84.         print("Press the 'M' key to emit a laser scan and start the mining lasers with calculated layer offset.")
  85.         for _, mininglaser in pairs(mininglasers) do
  86.           mininglaser.enable(false)
  87.         end
  88.         fs.delete("zero_layers.txt")
  89.         break
  90.       end
  91.     end
  92.   end
  93. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement