Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Press x in 5 seconds to cancel startup")
- do
- local timer = os.startTimer(5)
- local evt, p
- repeat
- evt, p = os.pullEvent()
- until (evt == "char" and p == "x") or (evt == "timer" and p == timer)
- if evt == "char" then return end
- end
- local startX, startZ -- world coords of centre of mine
- local state
- local miningX, miningZ -- centre-relative coords of next block to mine
- local turtX, turtZ -- centre-relative turtle coords
- local faceX, faceZ = 0, 0
- print("Attempting self-update")
- pcall(function()
- local c = http.get("http://immibis.com/ccfiles/rrts1/mine.lua")
- if not c then print("Update failed") return end
- local d = c.readAll()
- c.close()
- local f = fs.open("startup", "r")
- local d2 = f.readAll()
- f.close()
- if d ~= d2 then
- f = fs.open("startup", "w")
- f.write(d)
- f.close()
- print("Update successful")
- os.reboot()
- while true do coroutine.yield() end
- else
- print("No update required")
- end
- end)
- function onDumpFull()
- -- rednet.send(8, "wrs16 full") -- notify shuttle via long distance relay
- end
- function onFuelEmpty()
- -- rednet.send(8, "wrs16 fuel") -- notify shuttle via long distance relay
- end
- function onDumpFinished()
- -- rednet.send(8, "wrs16 call") -- notify shuttle via long distance relay
- end
- --rednet.open("right")
- local function save()
- if fs.exists("/minestate") then
- if fs.exists("/msbak") then
- fs.delete("/msbak")
- end
- fs.move("/minestate", "/msbak")
- end
- local f = fs.open("/minestate", "w")
- f.writeLine(tostring(startX))
- f.writeLine(tostring(startZ))
- f.writeLine(state)
- f.writeLine(tostring(miningX))
- f.writeLine(tostring(miningZ))
- f.writeLine(tostring(turtX))
- f.writeLine(tostring(turtZ))
- f.writeLine(tostring(faceX))
- f.writeLine(tostring(faceZ))
- f.close()
- end
- local function checkGPS()
- write("GPS is ")
- local x = gps.locate(1)
- if x == nil then
- print("down")
- return false
- else
- print("up")
- return true
- end
- end
- local function gpslocate()
- local x, y, z
- repeat
- x, y, z = gps.locate(2)
- if not x then
- print("GPS failure!")
- end
- until x ~= nil
- return x, z
- end
- local function f()
- while not turtle.forward() do
- if peripheral.getType("front") ~= nil then
- -- Don't mine other turtles
- sleep(0.5)
- else
- if not turtle.dig() then
- turtle.attack()
- end
- end
- end
- turtX = turtX + faceX
- turtZ = turtZ + faceZ
- end
- local function forwardNoDig()
- if turtle.forward() then
- turtX = turtX + faceX
- turtZ = turtZ + faceZ
- save()
- return true
- else
- return false
- end
- end
- local function r()
- turtle.turnRight()
- faceX, faceZ = -faceZ, faceX
- end
- local function l()
- turtle.turnLeft()
- faceX, faceZ = faceZ, -faceX
- end
- local function gpsgetfacing()
- local x1, z1 = gpslocate()
- f()
- local x2, z2 = gpslocate()
- r()
- r()
- f()
- r()
- r()
- return x2 - x1, z2 - z1
- end
- function manualCoordReset()
- print("Enter current turtle coordinates.")
- write("X: ")
- turtX = tonumber(read()) or error("Invalid number", 0)
- write("Z: ")
- turtZ = tonumber(read()) or error("Invalid number", 0)
- turtX = turtX - startX
- turtZ = turtZ - startZ
- print("Enter current turtle direction (+X/-X/+Z/-Z)")
- local dir = read():lower()
- if dir == "-x" then faceX, faceZ = -1, 0
- elseif dir == "+x" then faceX, faceZ = 1, 0
- elseif dir == "-z" then faceX, faceZ = 0, -1
- elseif dir == "+z" then faceX, faceZ = 0, 1
- else error("Invalid direction", 0)
- end
- end
- if fs.exists("/minestate") or fs.exists("/msbak") then
- function readState(filename)
- local f = fs.open(filename,"r")
- startX = tonumber(f.readLine())
- startZ = tonumber(f.readLine())
- state = f.readLine()
- miningX = tonumber(f.readLine())
- miningZ = tonumber(f.readLine())
- turtX = tonumber(f.readLine())
- turtZ = tonumber(f.readLine())
- faceX = tonumber(f.readLine())
- faceZ = tonumber(f.readLine())
- f.close()
- end
- if not pcall(readState, "/minestate") then
- readState("/msbak")
- end
- if checkGPS() then
- turtX, turtZ = gpslocate()
- turtX = turtX - startX
- turtZ = turtZ - startZ
- faceX, faceZ = gpsgetfacing()
- else
- state = "resetpos"
- end
- else
- if not checkGPS() then
- startX, startZ = 0, 0
- manualCoordReset()
- startX, startZ = turtX, turtZ
- else
- startX, startZ = gpslocate()
- turtX, turtZ = 0, 0
- faceX, faceZ = gpsgetfacing()
- end
- turtX, turtZ = 0, 0
- state = "mine"
- miningX, miningZ = -1, -1
- end
- local function face(dx, dz)
- if dx == faceX and dz == faceZ then
- -- do nothing
- elseif dx == faceZ and dz == -faceX then
- l()
- elseif dx == -faceZ and dz == faceX then
- r()
- elseif dx == -faceX and dz == -faceZ then
- l() l()
- else
- error("invalid orientation "..dx..","..dz, 2)
- end
- end
- function go(x, z)
- --print("goto ",x,",",z," from ",turtX,",",turtZ)
- if x > turtX then
- face(1, 0)
- while x > turtX do f() save() end
- elseif x < turtX then
- face(-1, 0)
- while x < turtX do f() save() end
- end
- if z > turtZ then
- face(0, 1)
- while z > turtZ do f() save() end
- elseif z < turtZ then
- face(0, -1)
- while z < turtZ do f() save() end
- end
- assert(x == turtX and z == turtZ, "something fucked up")
- end
- print("At ",turtX,",",turtZ," facing ",faceX,",",faceZ)
- save()
- turtle.select(1)
- while true do
- if state == "mine" then
- go(miningX, miningZ)
- local radius = math.max(math.abs(miningX), math.abs(miningZ))
- --[[
- -- --> +-
- <<
- ^ v
- -+ <-- ++
- ]]
- if miningZ == -radius and miningX ~= radius then
- face(1, 0)
- elseif miningX == radius and miningZ ~= radius then
- face(0, 1)
- elseif miningZ == radius and miningX ~= -radius then
- face(-1, 0)
- elseif miningX == -radius and miningZ ~= -radius then
- face(0, -1)
- else
- error("shouldn't get here, position "..miningX..","..miningZ.." (radius is "..radius..")")
- end
- -- leave a 5x5 platform
- if math.abs(turtX) > 2 or math.abs(turtZ) > 2 then
- turtle.digUp()
- turtle.digDown()
- end
- f()
- if turtX == -radius and turtZ == -radius + 1 then
- -- expand radius
- miningX = turtX - 1
- miningZ = turtZ - 2
- -- otherwise there's a diagonal line of unmined blocks
- if math.abs(turtX) > 2 or math.abs(turtZ) > 2 then
- turtle.digUp()
- turtle.digDown()
- end
- else
- miningX, miningZ = turtX, turtZ
- end
- if turtle.getFuelLevel() < 600 or turtle.getItemCount(16) > 0 then
- if math.abs(turtX) > math.abs(turtZ) then
- state = "dropoff_zfirst"
- else
- state = "dropoff_xfirst"
- end
- end
- save()
- elseif state == "dropoff_zfirst" then
- go(turtX, 0)
- state = "dropoff_last"
- save()
- elseif state == "dropoff_xfirst" then
- go(0, turtZ)
- state = "dropoff_last"
- save()
- elseif state == "dropoff_last" then
- go(0, 0)
- for k=1,16 do
- while turtle.getItemCount(k) > 0 do
- turtle.select(k)
- while not turtle.dropUp() do
- onDumpFull()
- print("Dump chest full")
- sleep(5)
- end
- end
- end
- onDumpFinished()
- while turtle.getFuelLevel() < 2000 do
- turtle.select(1)
- while not turtle.suckDown() do
- print("No fuel in chest!")
- onFuelEmpty()
- sleep(5)
- end
- if not turtle.refuel() then
- print("Found non-fuel item in fuel chest")
- while not turtle.dropUp() do
- print("Dump chest full")
- onDumpFull()
- sleep(5)
- end
- end
- end
- turtle.select(1)
- state = "resume"
- save()
- elseif state == "resume" then
- if math.abs(miningX) > math.abs(miningZ) then
- state = "resume_xfirst"
- else
- state = "resume_zfirst"
- end
- save()
- elseif state == "resume_xfirst" then
- go(miningX, turtZ)
- state = "mine"
- save()
- elseif state == "resume_zfirst" then
- go(turtX, miningZ)
- state = "mine"
- save()
- elseif state == "resetpos" then
- if math.abs(turtX) > math.abs(turtZ) then
- go(turtX, 0)
- go(0, 0)
- else
- go(0, turtZ)
- go(0, 0)
- end
- if checkGPS() then
- turtX, turtZ = gpslocate()
- turtX = turtX - startX
- turtZ = turtZ - startZ
- faceX, faceZ = 0, 0
- faceX, faceZ = gpsgetfacing()
- state = "resume"
- save()
- print("Recovered position from GPS, resuming mining")
- else
- print("No GPS signal to reset position")
- manualCoordReset()
- state = "resume"
- save()
- end
- else
- error("unknown state! "..state)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment