Advertisement
Guest User

startup

a guest
Aug 14th, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  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.       print(textutils.serialize(state)) -- Debug line to print the entire state object
  71.  
  72.       -- You'll need to modify the below lines once you understand the structure of 'state'
  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.         print("")
  85.         print("Press the 'M' key to emit a laser scan and start the mining lasers with calculated layer offset.")
  86.         for _, mininglaser in pairs(mininglasers) do
  87.           mininglaser.enable(false)
  88.         end
  89.         fs.delete("zero_layers.txt")
  90.         break
  91.       end
  92.     end
  93.   end
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement