View difference between Paste ID: qk57Ey5f and jqiA1idj
SHOW: | | - or go back to the newest paste.
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-
    -- Get the laser's own position
30+
31
  os.exit()
32
end
33
34
if #mininglasers == 0 then
35-
      -- Emit a laser scan in the Y- direction (0, -1, 0)
35+
36
  os.exit()
37-
      os.sleep(1) -- Brief pause to allow processing
37+
38
39-
    until targetY ~= 0 -- Repeat until a non-zero result is obtained
39+
40
41-
    -- Calculate the layerOffset
41+
42
while true do
43
  local event, key = os.pullEvent("key")
44-
    -- Print the target
44+
45
  if key == 50 then
46
    local _, laserY, _ = laser.getLocalPosition()
47-
    -- Configure the mining lasers to use the mineTarget as the layerOffset
47+
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