Advertisement
jaklsfjlsak

激光测距采矿 多次扫描修复,自动停止

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