Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- local laser = peripheral.find("warpdriveLaser")
- local mininglasers = {}
- local sides = peripheral.getNames()
- local function saveZeroLayersCount(count)
- local file = fs.open("zero_layers.txt", "w")
- file.write(tostring(count))
- file.close()
- end
- local function getZeroLayersCount()
- if fs.exists("zero_layers.txt") then
- local file = fs.open("zero_layers.txt", "r")
- local count = tonumber(file.readAll())
- file.close()
- return count
- end
- return 0
- end
- for _, side in pairs(sides) do
- if peripheral.getType(side) == "warpdriveMiningLaser" then
- table.insert(mininglasers, peripheral.wrap(side))
- end
- end
- laser.beamFrequency(1420)
- if not laser then
- print("No warpdriveLaser detected")
- os.exit()
- end
- if #mininglasers == 0 then
- print("No warpdriveMiningLaser detected")
- os.exit()
- end
- print("Press the 'M' key to emit a laser scan and start the mining lasers with calculated layer offset.")
- -- Loop to wait for key events
- while true do
- local event, key = os.pullEvent("key")
- if key == 50 then
- local _, laserY, _ = laser.getLocalPosition()
- local targetY = 0
- repeat
- laser.emitBeam(0, -1, 0)
- os.sleep(1)
- _, _, targetY = laser.getScanResult()
- until targetY ~= 0
- local mineTarget = laserY - targetY - 1
- print("Target is: " .. mineTarget .. " blocks below")
- for _, mininglaser in pairs(mininglasers) do
- mininglaser.offset(mineTarget)
- mininglaser.enable(true)
- end
- local consecutiveZeroLayers = getZeroLayersCount()
- -- Periodically check the mining state
- while true do
- os.sleep(1)
- local state, isActive, energy, currentLayer, mined, total = mininglasers[1].state()
- print(textutils.serialize(state)) -- Debug line to print the entire state object
- -- You'll need to modify the below lines once you understand the structure of 'state'
- if total == 0 then
- consecutiveZeroLayers = consecutiveZeroLayers + 1
- else
- consecutiveZeroLayers = 0
- end
- saveZeroLayersCount(consecutiveZeroLayers)
- if consecutiveZeroLayers >= 3 then
- print("3 consecutive layers with 0 total blocks detected. Stopping mining.")
- print("")
- print("Press the 'M' key to emit a laser scan and start the mining lasers with calculated layer offset.")
- for _, mininglaser in pairs(mininglasers) do
- mininglaser.enable(false)
- end
- fs.delete("zero_layers.txt")
- break
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement