Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------Save And Load---------------
- local save = function(data, name)
- if not fs.exists('/data') then
- fs.makeDir('/data')
- end
- local file = fs.open('/data/'..name, 'w')
- file.write(textutils.serialize(data))
- file.close()
- end
- local load = function(name)
- if fs.exists('/data/'..name) then
- local file = fs.open('/data/'..name, 'r')
- data = textutils.unserialize(file.readAll())
- file.close()
- end
- return data
- end
- --------------------------------------------
- local tArgs = {...}
- local length
- local height
- local width
- if not fs.exists('/data/mineSize') then
- if #tArgs ~= 3 then
- print("Required Length, Height and Width")
- return
- end
- length = tonumber(tArgs[1])
- height = tonumber(tArgs[2])
- width = tonumber(tArgs[3])
- else
- savedData = load('mineSize')
- continueFromLast = true
- length = tonumber(savedData[1])
- height = tonumber(savedData[2])
- width = tonumber(savedData[3])
- end
- local currentDirectionFacing
- local lastKnownDirectionFacing
- local currentXValue
- local currentYValue
- local currentZValue
- local lastKnownXValue
- local lastKnownYValue
- local lastKnownZValue
- local minXValue
- local minYValue
- local minZValue
- local maxXValue
- local maxYValue
- local maxZValue
- if length == nil or height == nil or width == nil then
- print("Invalid Dimensions")
- return
- end
- if length < 3 or height < 3 or width < 3 then
- if width < 3 then
- print("Length must be 3 or more")
- end
- if height < 3 then
- print("Height must be 3 or more")
- end
- if length < 3 then
- print("Width must be 3 or more")
- end
- return
- end
- if width % 2 == 0 then
- print("Width must be an odd number")
- return
- end
- if height % 2 == 0 then
- print("Current Bug Detected:")
- print("Height set to Even Number")
- print("The turtle will return after the Second Last Layer")
- end
- ---------------Misc Functions---------------
- --22.03
- local calcDirection = function(d)
- if d > 3 then
- d = 0
- end
- if d < 0 then
- d = 3
- end
- return d
- end
- local updateVariable = function(x, y, z, d)
- if x ~= nil and x ~= 0 then
- currentXValue = currentXValue+x
- end
- if y ~= nil and y ~= 0 then
- currentYValue = currentYValue+y
- end
- if z ~= nil and z ~= 0 then
- currentZValue = currentZValue+z
- end
- if d ~= nil then
- currentDirectionFacing = calcDirection(currentDirectionFacing+d)
- end
- save({currentXValue,currentYValue,currentZValue,currentDirectionFacing},"currentLocation")
- end
- local dig = function(digFront, digTop, digBottom)
- if digFront ~= false then
- turtle.dig()
- end
- if digTop ~= false then
- turtle.digUp()
- end
- if digBottom ~= false then
- turtle.digDown()
- end
- end
- local inventoryCheck = function()
- if turtle.getItemCount(16) >= 1 then
- return true
- else
- return false
- end
- end
- local refuelCheck = function()
- while turtle.getFuelLevel() <= 1 do
- turtle.select(1)
- turtle.refuel(1)
- end
- end
- local emptyInventory = function()
- for i = 2, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- end
- --------------------------------------------
- ------------------Movement------------------
- -- Moving
- local forwardX = function(digFront, digTop, digBottom)
- refuelCheck()
- local continueLoop = false
- while continueLoop == false do
- while turtle.detect() == true do
- dig(digFront, digTop, digBottom)
- end
- continueLoop = turtle.forward()
- end
- updateVariable(1,0,0)
- end
- local backX = function(digFront, digTop, digBottom)
- refuelCheck()
- local continueLoop = false
- while continueLoop == false do
- while turtle.detect() == true do
- dig(digFront, digTop, digBottom)
- end
- continueLoop = turtle.forward()
- end
- updateVariable(-1,0,0)
- end
- local forwardZ = function(digFront, digTop, digBottom)
- refuelCheck()
- local continueLoop = false
- while continueLoop == false do
- while turtle.detect() == true do
- dig(digFront, digTop, digBottom)
- end
- continueLoop = turtle.forward()
- end
- updateVariable(0,0,1)
- end
- local backZ = function(digFront, digTop, digBottom)
- refuelCheck()
- local continueLoop = false
- while continueLoop == false do
- while turtle.detect() == true do
- dig(digFront, digTop, digBottom)
- end
- continueLoop = turtle.forward()
- end
- updateVariable(0,0,-1)
- end
- local up = function(digFront, digTop, digBottom)
- refuelCheck()
- local continueLoop = false
- while continueLoop == false do
- while turtle.detectUp() == true do
- dig(digFront, digTop, digBottom)
- end
- continueLoop = turtle.up()
- end
- updateVariable(0,1,0)
- end
- local down = function(digFront, digTop, digBottom)
- refuelCheck()
- local continueLoop = false
- while continueLoop == false do
- while turtle.detectDown() == true do
- dig(digFront, digTop, digBottom)
- end
- continueLoop = turtle.down()
- end
- updateVariable(0,-1,0)
- end
- local moveToCenterX = function()
- while currentXValue ~= maxXValue/2 do
- if currentDirectionFacing == 1 then
- forwardX()
- elseif currentDirectionFacing == 3 then
- backX()
- end
- end
- end
- local moveToMaxY = function()
- while currentYValue ~= maxYValue do
- up()
- end
- end
- local moveToMinX = function()
- while currentXValue ~= minXValue do
- backX()
- end
- end
- local moveToMinZ = function()
- while currentZValue ~= minZValue do
- backZ()
- end
- end
- local moveToLastZ = function()
- while currentZValue ~= lastKnownZValue do
- if currentZValue < lastKnownZValue then
- forwardZ()
- else
- backZ()
- end
- end
- end
- local moveToLastX = function()
- while currentXValue ~= lastKnownXValue do
- if currentXValue < lastKnownXValue then
- forwardX()
- else
- backX()
- end
- end
- end
- local moveToLastY = function()
- while currentYValue ~= lastKnownYValue do
- if currentYValue > lastKnownYValue then
- down()
- elseif currentYValue < lastKnownYValue then
- up()
- end
- end
- end
- -- Turning
- local left = function()
- turtle.turnLeft()
- updateVariable(0,0,0,-1)
- end
- local right = function()
- turtle.turnRight()
- updateVariable(0,0,0,1)
- end
- local turnToCenterX = function()
- if currentXValue > maxXValue/2 then
- while currentDirectionFacing ~= 3 do
- left()
- end
- else
- while currentDirectionFacing ~= 1 do
- right()
- end
- end
- end
- local turnToMinX = function()
- while currentDirectionFacing ~= 3 do
- if currentDirectionFacing == 0 then
- left()
- else
- right()
- end
- end
- end
- local turnToMaxX = function()
- while currentDirectionFacing ~= 1 do
- if currentDirectionFacing == 2 then
- left()
- else
- right()
- end
- end
- end
- local turnToMinZ = function()
- while currentDirectionFacing ~= 2 do
- if currentDirectionFacing == 3 then
- left()
- else
- right()
- end
- end
- end
- local turnToMaxZ = function()
- while currentDirectionFacing ~= 0 do
- if currentDirectionFacing == 1 then
- left()
- else
- right()
- end
- end
- end
- local turnToLastX = function()
- if currentXValue > lastKnownXValue then
- turnToMinX()
- elseif currentXValue < lastKnownXValue then
- turnToMaxX()
- end
- end
- local turnToLastDirection = function()
- while currentDirectionFacing ~= lastKnownDirectionFacing do
- left()
- end
- end
- --------------------------------------------
- ----------------GPS Function----------------
- local locateLastPosition = function()
- savedLocation = load('currentLocation')
- minXValue = 0
- minYValue = 1
- minZValue = 0
- maxXValue = width-1
- maxYValue = height
- maxZValue = length-1
- currentXValue = tonumber(savedLocation[1])
- currentYValue = tonumber(savedLocation[2])
- currentZValue = tonumber(savedLocation[3])
- lastKnownXValue = currentXValue
- lastKnownYValue = currentYValue
- lastKnownZValue = currentZValue
- currentDirectionFacing = tonumber(savedLocation[4])
- lastKnownDirectionFacing = currentDirectionFacing
- end
- local locateInitialPosition = function()
- minXValue = 0
- minYValue = 1
- minZValue = 0
- maxXValue = width-1
- maxYValue = height
- maxZValue = length-1
- currentXValue = maxXValue/2
- currentYValue = maxYValue
- currentZValue = minZValue
- lastKnownXValue = currentXValue
- lastKnownYValue = currentYValue
- lastKnownZValue = currentZValue
- currentDirectionFacing = 0
- end
- --------------------------------------------
- --------------Return Functions--------------
- local returnToFirstCorner = function()
- turnToMinZ()
- moveToMinZ()
- turnToMinX()
- moveToMinX()
- end
- local returnToLastPosition = function()
- turnToMaxZ()
- moveToLastZ()
- turnToLastX()
- moveToLastX()
- moveToLastY()
- turnToLastDirection()
- end
- local returnToChest = function()
- lastKnownDirectionFacing = currentDirectionFacing
- lastKnownXValue = currentXValue
- lastKnownYValue = currentYValue
- lastKnownZValue = currentZValue
- moveToMaxY()
- turnToCenterX()
- moveToCenterX()
- turnToMinZ()
- moveToMinZ()
- emptyInventory()
- end
- --------------------------------------------
- ----------------Mine Function---------------
- local mineStripForwardZ = function()
- turnToMaxZ()
- while currentZValue ~= maxZValue do
- if inventoryCheck() == true then
- returnToChest()
- returnToLastPosition()
- end
- dig()
- forwardZ()
- end
- end
- local mineStripBackZ = function()
- turnToMinZ()
- while currentZValue ~= minZValue do
- if inventoryCheck() == true then
- returnToChest()
- returnToLastPosition()
- end
- dig()
- backZ()
- end
- end
- local nextStrip = function()
- turnToMaxX()
- dig()
- forwardX()
- end
- local nextLayer = function()
- local currentYValueClone = currentYValue
- if currentYValue == minYValue+1 then
- down(false,false,false)
- elseif currentYValue-2 <= minYValue then
- while currentYValue ~= minYValue+1 do
- down(false,false,true)
- end
- else
- while currentYValue ~= currentYValueClone-2 do
- down(false,true,true)
- end
- end
- end
- --------------------------------------------
- ----------------Main Function---------------
- local mineLayerHandler = function()
- save({length,height,width},"mineSize")
- turnToMinX()
- moveToMinX()
- while currentYValue ~= minYValue+1 do
- while currentXValue < maxXValue do
- mineStripForwardZ()
- nextStrip()
- mineStripBackZ()
- nextStrip()
- end
- if currentXValue == maxXValue then
- mineStripForwardZ()
- turtle.digUp()
- turtle.digDown()
- end
- returnToFirstCorner()
- nextLayer()
- end
- returnToChest()
- right()
- right()
- fs.delete("/data/mineSize")
- fs.delete("/data/currentLocation")
- end
- -- turning to max z
- -- moving to max z
- -- turning to max x
- -- moving forward one
- -- turning to min z
- -- continuing mining
- local mineLayerHandlerContinueFromLast = function()
- while currentYValue ~= minYValue+1 do
- if currentDirectionFacing == 2 then
- mineStripBackZ()
- nextStrip()
- end
- while currentXValue < maxXValue do
- mineStripForwardZ()
- nextStrip()
- mineStripBackZ()
- nextStrip()
- end
- if currentXValue == maxXValue then
- mineStripForwardZ()
- turtle.digUp()
- turtle.digDown()
- end
- returnToFirstCorner()
- nextLayer()
- end
- returnToChest()
- right()
- right()
- fs.delete("/data/mineSize")
- fs.delete("/data/currentLocation")
- end
- if continueFromLast ~= true then
- locateInitialPosition()
- mineLayerHandler()
- else
- locateLastPosition()
- mineLayerHandlerContinueFromLast()
- end
- --------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement