Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("left")
- local fuelSlot = 16
- local mining = false
- local distance = 0
- local progress = 0
- local _fileName = "mining.dat"
- function fuel()
- if turtle.getFuelLevel() < 10 then
- turtle.select(fuelSlot)
- turtle.refuel(1)
- turtle.select(1)
- end
- if turtle.getFuelLevel() < 10 then
- for i = 1, 16 do
- turtle.select(i)
- turtle.refuel(1)
- end
- turtle.select(1)
- end
- end
- function checkProgress()
- if progress >= distance then
- mining = false
- end
- end
- function createPacket(msg)
- return {id = os.getComputerID(), msg = msg}
- end
- function receiveCommand()
- local e, id, msg = os.pullEvent("rednet_message")
- if msg.command == "status" then
- rednet.send(id, createPacket({work=mining,dist=distance,prog=progress}))
- end
- if msg.command == "start" then
- mining = true
- distance = msg.msg
- progress = 0
- end
- if msg.command == "stop" then
- mining = false
- progress = 0
- distance = 0
- saveData()
- os.reboot()
- end
- if msg.command == "turn" then
- turtle.turnLeft()
- end
- if msg.command == "dump" then
- dumpContent()
- end
- if msg.command == "down" then
- turtle.down()
- end
- print(id..": "..textutils.serialize(msg))
- end
- function dumpContent()
- for i = 1, 15 do
- turtle.select(i)
- turtle.dropUp(64)
- end
- turtle.select(1)
- end
- function loadData()
- if fs.exists(_fileName) then
- local file = fs.open(_fileName, "r")
- local data = textutils.unserialize(file.readAll())
- if data ~= nil then
- progress = data.progress
- distance = data.distance
- mining = data.mining
- end
- file.close()
- end
- end
- function saveData()
- local file = fs.open(_fileName, "w")
- local data = {mining=mining, progress=progress, distance=distance}
- file.write(textutils.serialize(data))
- file.close()
- end
- function dig()
- if mining then
- fuel()
- turtle.dig()
- while not turtle.forward() do turtle.dig() end
- turtle.placeDown()
- turtle.digUp()
- turtle.up()
- turtle.digUp()
- turtle.down()
- progress = progress + 1
- checkProgress()
- saveData()
- end
- sleep(.5)
- end
- loadData()
- if distance > 0 then
- dig()
- end
- receiveCommand()
- while true do
- parallel.waitForAny(receiveCommand, dig)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement