Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Variables for tracking position
- --will be used in files later
- --for recovery from server reboot
- --initialised to 0 to indicate no movement
- recover = 0
- fwdPos = 0
- columnPos = 0
- depthPos = 0
- orientationPos = 0
- fwdTarget = 0
- columnTarget = 0
- depthTarget = 0
- isRefuelling = 0
- isUnloading = 0
- --function to update all variables
- --for recovery program in file
- --check for enderchests
- function updateFile()
- fs.open("quarryData/","w")
- file.writeLine(recover)
- file.writeLine(fwdPos)
- file.writeLine(columnPos)
- file.writeLine(depthPos)
- file.writeLine(orientationPos)
- file.writeLine(fwdTarget)
- file.writeLine(columnTarget)
- file.writeLine(depthTarget)
- file.writeLine(isRefuelling)
- file.writeline(isUnloading)
- file.close()
- end
- function refuel()
- --check inventory for fuel
- local maxFuel = 200
- --cycles through each slot refuelling
- --using everything in that slot
- --until full
- for i=3, 16 do
- if turtle.getFuelLevel() < maxFuel then
- turtle.select(i)
- turtle.refuel()
- end
- end
- --if not enough, use enderchest
- if turtle.getFuelLevel() < maxFuel then
- --select enderchest, place above
- while turtle.getItemCount(2) ~= 0 do
- sleep(1)
- turtle.select(2)
- turtle.placeUp()
- end
- isRefuelling = 1 --enderchest out
- updateFile()
- --keep grabbing fuel until full
- while turtle.getFuelLevel() < maxFuel do
- if turtle.suckUp() == false then
- break --if no fuel available
- --then stop refueling
- end
- turtle.refuel()
- end
- --replace enderchest
- turtle.digUp()
- isRefuelling = 0
- updateFile()
- end
- --reselect slot 1
- turtle.select(1)
- end
- function unload()
- --places enderchest above
- while turtle.getItemCount(1) ~= 0 do
- sleep(1)
- turtle.select(1)
- turtle.placeUp()
- end
- isUnloading = 1
- updateFile()
- --dumps all items in chest
- for i=3, 16 do
- turtle.select(i)
- turtle.dropUp()
- end
- --reselects slot 1
- turtle.select(1)
- turtle.digUp() --gets enderchest back
- isUnloading = 0
- updateFile()
- end
- function goFwd(dist)
- local minFuel = 10
- for i=1, dist do
- --Refuel if needed
- if turtle.getFuelLevel() < minFuel then
- refuel()
- end
- --move + dig anything infront
- while not turtle.forward() do
- if turtle.getItemCount(16) > 0 then
- unload()
- end
- turtle.dig()
- end
- --update File
- --if moving up a column
- if orientationPos == 0 then
- fwdPos = fwdPos + 1
- updateFile()
- end
- --else if moving along a column
- if orientationPos == 1 then
- columnPos = columnPos + 1
- fwdPos = 0
- updateFile()
- end
- end
- end
- function digRect(N, M)
- --digs an N across M up square
- --dig square except last row
- orientationPos = 0
- updateFile()
- if N > 1 then
- for i=1, (N-1) do
- --on odd rows
- if i % 2 == 1 then
- goFwd((M-1))
- turtle.turnRight()
- orientationPos = 1
- updateFile()
- goFwd(1)
- turtle.turnRight()
- orientationPos = 0
- updateFile()
- else --on even rows
- goFwd((M-1))
- turtle.turnLeft()
- orientationPos = 1
- updateFile()
- goFwd(1)
- turtle.turnLeft()
- orientationPos = 0
- updateFile()
- end
- end
- end
- --finish last row without the dig turn
- --turn so in bottom left of square
- goFwd((M-1))
- turtle.turnRight()
- orientationPos = 1
- updateFile()
- if (N%2) == 1 then
- turtle.turnRight()
- end
- orientationPos = 2
- updateFile()
- end
- function goDown(N)
- local minFuel = 10
- for i=1, N do
- --refuel if needed
- if (turtle.getFuelLevel() < minFuel) then
- refuel()
- end
- --checks for inv space
- if (turtle.getItemCount(16) > 0) then
- unload()
- end
- --then goes down
- turtle.digDown()
- turtle.down()
- depthPos = depthPos - 1
- updateFile()
- end
- end
- function goUp(N)
- --not used in file so far
- local minFuel = 10
- for i=1, N do
- --checks fuel
- if turtle.getFuelLevel() < minFuel then
- refuel()
- end
- --checks for space
- if turtle.getItemCount(16) > 0 then
- unload()
- end
- while not turtle.up() do
- turtle.digUp()
- end
- end
- end
- function quarry(depth, sizeAccr, sizeFwd)
- local temp = 0
- for i=1, depth do
- --dig layer
- goDown(1)
- digRect(sizeAccr, sizeFwd)
- --if went across even num rows
- --flip dimensions for next go
- if (sizeAccr % 2 == 0) then
- temp = sizeAccr
- sizeAccr = sizeFwd
- sizeFwd = temp
- --update file to show this
- temp = fwdTarget
- fwdTarget = columnTarget
- columnTarget = temp
- updateFile()
- end
- orientationPos = 3
- end
- end
- function recover()
- --uses global variables
- --taken from file
- --if it is mid refuel
- if isRefuelling == 1 then
- turtle.select(2)
- turtle.refuel()
- turtle.digUp()
- isRefuelling = 0
- updateFile()
- end
- --if it is mid unload
- if isUnloading == 1 then
- for i=3, 16 do
- turtle.select(i)
- turtle.dropUp()
- end
- isUnloading = 0
- updateFile()
- end
- --OTHERWISE--
- --if we are midway through a
- --forward move, then get to its end
- if ((fwdPos + 1) < fwdTarget) and
- (fwdPos > 0) then
- local tempDist = fwdTarget - (fwdPos + 1)
- goFwd(tempDist)
- end
- --if we are at the end of a column
- --that isnt the last column
- --get to the start of the next one
- if ((fwdPos + 1) == fwdTarget) and
- ((columnPos + 1) < columnTarget) then
- if orientationPos == 0 then
- if (columnPos % 2 == 1) then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- orientationPos = 1
- updateFile()
- end
- goFwd(1)
- if (columnPos % 2 == 0) then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- orientationPos = 0
- updateFile()
- end
- --if we are at the start of a column
- --but facing the wrong way
- --rotate the right way
- if (fwdPos == 0) and
- (orientationPos == 1) then
- if columnPos % 2 == 0 then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- orientationPos = 0
- updateFile()
- end
- --if at start of column and
- --facing the right way
- --dig a rectangle to the end
- if (fwdPos == 0) and
- (orientationPos == 0) then
- local tempCols = columnTarget - columnPos
- digRect(tempCols, fwdTarget)
- end
- --if in corner and orientation != 2 or 3
- if ((columnPos + 1) == columnTarget)
- and ((fwdPos + 1) == fwdTarget)
- and (orientationPos < 2) then
- if orientationPos == 0 then
- turtle.turnRight()
- orientationPos = 1
- updateFile()
- end
- if (columnTarget % 2 == 0) then
- turtle.turnRight()
- end
- orientationPos = 2
- updateFile()
- local tempDepth = depthTarget - depthPos
- if (columnTarget % 2 == 0) then
- local tempMem = fwdTarget
- fwdtarget = columnTarget
- columnTarget = tempMem
- end
- orientationPos = 3
- updateFile()
- quarry(tempDpeth, columnTarget, fwdTarget)
- return 0
- end
- --if square is done, but row/col flip isnt
- if orientationPos == 2 then
- local tempDepth = depthTarget - depthPos
- if (columnTarget % 2 == 0) then
- quarry(tempDepth, fwdTarget, columnTarget)
- else
- quarry(tempDepth, columnTarget, fwdTarget)
- end
- return 0
- end
- --if the entire square is done
- if orientationPos == 3 then
- local tempDepth = depthTarget - depthPos
- quarry(tempDepth, columnTarget, fwdTarget)
- return 0
- end
- end
- --ACTUAL PROGRAM--
- --recover if needed
- --get data from file
- file = fs.open("quarryData/","r")
- local fileData = {}
- local line = file.readLine()
- repeat
- table.insert(fileData,line)
- line = file.readLine()
- until line == nil
- file.close()
- --update global variables with file data
- recover = fileData[1]
- fwdPos = fileData[2]
- columnPos = fileData[3]
- depthPos = fileData[4]
- orientationPos = fileData[5]
- fwdTarget = fileData[6]
- columnTarget = fileData[7]
- depthTarget = fileData[8]
- isRefuelling = fileData[9]
- isUnloading = fileData[10]
- if recover == 1 then
- print("recovering from saved location")
- recoverProg()
- recover = 0
- updateFile()
- end
- term.clear()
- term.setCursorPos(1, 1)
- print("Place item chest in slot 1")
- print("and fuel chest in slot 2")
- print("Then enter how far you would like to")
- print("dig down")
- local depth = read()
- local depthNum = tonumber(depth)
- print("Enter how wide and long the square")
- print("to dig should be")
- local sizeAccr = read()
- local sizeAccrNum = tonumber(sizeAccr)
- local sizeFwd = read()
- local sizeFwdNum = tonumber(sizeFwd)
- columnTarget = sizeAccrNum
- fwdTarget = sizeFwdNum
- depthTarget = depthNum
- recover = 1
- updateFile()
- quarry(depthNum, sizeAccrNum, sizeFwdNum)
- recover = 0
Advertisement
Add Comment
Please, Sign In to add comment