View difference between Paste ID: wPGmwVYk and DYw3Ebdv
SHOW: | | - or go back to the newest paste.
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
print("Press the 'S' key to stop mining and reboot.")
42-
-- Loop to wait for key events
42+
43
local miningActive = false
44-
  local event, key = os.pullEvent("key")
44+
45
while true do
46-
  if key == 50 then
46+
  local _, key = os.pullEvent("key")
47
  if key == 31 then -- S key
48
    print("Stopping mining lasers and rebooting...")
49
    for _, mininglaser in pairs(mininglasers) do
50
      mininglaser.enable(false)
51
    end
52
    os.reboot()
53
  elseif key == 50 and not miningActive then -- M key
54
    local _, laserY, _ = laser.getLocalPosition()
55
    local targetY = 0
56
57
    repeat
58
      laser.emitBeam(0, -1, 0)
59
      os.sleep(1)
60
      _, _, targetY = laser.getScanResult()
61
    until targetY ~= 0
62
63
    local mineTarget = laserY - targetY - 1
64
    print("Target is: " .. mineTarget .. " blocks below")
65
66-
    -- Periodically check the mining state
66+
67
      mininglaser.offset(mineTarget)
68
      mininglaser.enable(true)
69
    end
70
    miningActive = true
71-
      -- You'll need to modify the below lines once you understand the structure of 'state'
71+
72
    local consecutiveZeroLayers = getZeroLayersCount()
73
74
    while true do
75
      os.sleep(1)
76
      local state, isActive, energy, currentLayer, mined, total = mininglasers[1].state()
77
78
      if total == 0 then
79
        consecutiveZeroLayers = consecutiveZeroLayers + 1
80
      else
81
        consecutiveZeroLayers = 0
82
      end
83
84
      saveZeroLayersCount(consecutiveZeroLayers)
85
86
      if consecutiveZeroLayers >= 3 then
87
        print("3 consecutive layers with 0 total blocks detected. Stopping mining.")
88
        print("")
89
        print("Press the 'M' key to emit a laser scan and start the mining lasers with calculated layer offset.")
90
        for _, mininglaser in pairs(mininglasers) do
91
          mininglaser.enable(false)
92
        end
93-
end
93+
        miningActive = false
94
        fs.delete("zero_layers.txt")
95
        break
96
      end
97
    end
98
  end
99
end
100