Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- json = require("json")
- -- IO-Functions
- function fileExists(name)
- local f=io.open(name,"r")
- if f~=nil then io.close(f) return true else return false end
- end
- --- Saves in pFilePath the pTable as a json-document.
- --- Returns the success.
- ---@param pFilePath string pointing to the file.
- ---@param pTable table with all data that should be saved.
- ---@return boolean success of saving.
- function saveToJson(pFilePath, pTable)
- local file, err = io.open(pFilePath, 'w')
- if file then
- file:write(json.encode(pTable))
- file:close()
- return true
- else
- print("error:", err)
- return false
- end
- end
- --- Loads the table from a json-document out of pFilePath.
- --- Returns the table or nil.
- ---@param pFilePath string pointing to the file.
- ---@return table that is represented by the files content.
- function loadFromJson(pFilePath)
- local file, err = io.open(pFilePath, 'r')
- if file then
- local read = file:read("*a")
- file:close()
- return json.decode(read) -- document is in JSON-format, so it decodes back to normal table
- else
- print("error:", err)
- return nil
- end
- saveToJson()
- end
- savePath = "zigZagTunnelsTemp.json"
- -- The turtle has to start on top of a chest which has enough space for all the items that the turtle mines
- -- absolute Positions relative to starting position
- Direction = {
- FORWARD = 1,
- RIGHT = 2,
- BACKWARD = 3,
- LEFT = 4
- }
- saveObject = {
- absPosForward = 0,
- absPosRight = 0,
- absPosUp = 0,
- absDirection = Direction.FORWARD,
- layers = 0,
- tunnelWidth = 0,
- finishedLayers = 0,
- loadingOff = {
- currently = false
- }
- }
- function moveForward()
- if turtle.forward() == false then
- -- Failed to move, see if we can dig our way forward
- while turtle.dig() do
- -- Keep digging till we can't dig any more, in case gravel is falling.
- end
- if turtle.forward() == false then
- print("I am either blocked or out of fuel.")
- return false
- end
- end
- -- turtle moved the right way
- if saveObject.absDirection == Direction.FORWARD then
- saveObject.absPosForward = saveObject.absPosForward + 1
- elseif saveObject.absDirection == Direction.RIGHT then
- saveObject.absPosRight = saveObject.absPosRight + 1
- elseif saveObject.absDirection == Direction.BACKWARD then
- saveObject.absPosForward = saveObject.absPosForward - 1
- elseif saveObject.absDirection == Direction.LEFT then
- saveObject.absPosRight = saveObject.absPosRight - 1
- end
- saveToJson(savePath, saveObject)
- return true
- end
- function moveDown()
- if turtle.down() == false then
- -- Failed to move, see if we can dig our way down
- turtle.digDown()
- if turtle.down() == false then
- print("I am either blocked or out of fuel.")
- return false
- end
- end
- -- turtle moved the right way
- saveObject.absPosUp = saveObject.absPosUp - 1
- saveToJson(savePath, saveObject)
- return true
- end
- function moveUp()
- if turtle.up() == false then
- -- Failed to move, see if we can dig our way down
- while turtle.digUp() do
- -- Keep digging till we can't dig any more, in case gravel is falling.
- end
- if turtle.up() == false then
- print("I am either blocked or out of fuel.")
- return false
- end
- end
- -- turtle moved the right way
- saveObject.absPosUp = saveObject.absPosUp + 1
- saveToJson(savePath, saveObject)
- return true
- end
- function turnRight()
- if (turtle.turnRight() == false) then
- return false
- end
- if saveObject.absDirection == Direction.FORWARD then
- saveObject.absDirection = Direction.RIGHT
- elseif saveObject.absDirection == Direction.RIGHT then
- saveObject.absDirection = Direction.BACKWARD
- elseif saveObject.absDirection == Direction.BACKWARD then
- saveObject.absDirection = Direction.LEFT
- elseif saveObject.absDirection == Direction.LEFT then
- saveObject.absDirection = Direction.FORWARD
- end
- saveToJson(savePath, saveObject)
- return true
- end
- function turnLeft()
- if (turtle.turnLeft() == false) then
- return false
- end
- if saveObject.absDirection == Direction.FORWARD then
- saveObject.absDirection = Direction.LEFT
- elseif saveObject.absDirection == Direction.RIGHT then
- saveObject.absDirection = Direction.FORWARD
- elseif saveObject.absDirection == Direction.BACKWARD then
- saveObject.absDirection = Direction.RIGHT
- elseif saveObject.absDirection == Direction.LEFT then
- saveObject.absDirection = Direction.BACKWARD
- end
- saveToJson(savePath, saveObject)
- return true
- end
- -- Turns the turtle to the saveObject.absDirection pDir relative to its starting saveObject.absDirection
- function turnDirection(pDir)
- if (pDir == Direction.FORWARD) then
- if (saveObject.absDirection == Direction.FORWARD) then -- nothing to do
- elseif (saveObject.absDirection == Direction.RIGHT) then
- turnLeft()
- elseif (saveObject.absDirection == Direction.BACKWARD) then
- turnLeft()
- turnLeft()
- elseif (saveObject.absDirection == Direction.LEFT) then
- turnRight()
- end
- return true
- elseif (pDir == Direction.RIGHT) then
- if (saveObject.absDirection == Direction.FORWARD) then
- turnRight()
- elseif (saveObject.absDirection == Direction.RIGHT) then -- nothing to do
- elseif (saveObject.absDirection == Direction.BACKWARD) then
- turnLeft()
- elseif (saveObject.absDirection == Direction.LEFT) then
- turnLeft()
- turnLeft()
- end
- return true
- elseif (pDir == Direction.BACKWARD) then
- if (saveObject.absDirection == Direction.FORWARD) then
- turnLeft()
- turnLeft()
- elseif (saveObject.absDirection == Direction.RIGHT) then
- turnRight()
- elseif (saveObject.absDirection == Direction.BACKWARD) then -- nothing to do
- elseif (saveObject.absDirection == Direction.LEFT) then
- turnLeft()
- end
- return true
- elseif (pDir == Direction.LEFT) then
- if (saveObject.absDirection == Direction.FORWARD) then
- turnLeft()
- elseif (saveObject.absDirection == Direction.RIGHT) then
- turnLeft()
- turnLeft()
- elseif (saveObject.absDirection == Direction.BACKWARD) then
- turnRight()
- elseif (saveObject.absDirection == Direction.LEFT) then -- nothing to do
- end
- return true
- else
- print("pDir is no valid saveObject.absDirection. Could not orient.")
- return false
- end
- end
- -- The turtle will destroy all Blocks in its way to get there
- -- returns true if successful
- function moveToCoordUp(pCoord)
- -- vertical to pCoord
- if saveObject.absPosUp > pCoord then
- while saveObject.absPosUp > pCoord do
- if (moveDown() == false) then
- return false
- end
- end
- elseif saveObject.absPosUp < pCoord then
- while saveObject.absPosUp < pCoord do
- if (moveUp() == false) then
- return false
- end
- end
- end
- return true
- end
- -- The turtle will destroy all Blocks in its way to get there
- -- returns true if successful
- function moveToCoordForward(pCoord)
- -- forward/backward to pCoord
- -- first orient the right way
- if saveObject.absPosForward > pCoord then
- if (turnDirection(Direction.BACKWARD)) == false then
- return false
- end
- while saveObject.absPosForward > pCoord do -- move until at pCoord
- if (moveForward() == false) then
- return false
- end
- end
- elseif saveObject.absPosForward < pCoord then -- turn to absolute forward
- if (turnDirection(Direction.FORWARD)) == false then
- return false
- end
- while saveObject.absPosForward < pCoord do -- move until at pCoord
- if (moveForward() == false) then
- return false
- end
- end
- end
- return true
- end
- -- The turtle will destroy all Blocks in its way to get there
- -- returns true if successful
- function moveToCoordRight(pCoord)
- -- left/right to pCoord
- -- first orient the right way
- if saveObject.absPosRight > pCoord then
- if (turnDirection(Direction.LEFT)) == false then
- return false
- end
- while saveObject.absPosRight > pCoord do -- move until at pCoord
- if (moveForward() == false) then
- return false
- end
- end
- elseif saveObject.absPosRight < pCoord then -- turn to absolute right
- if (turnDirection(Direction.RIGHT)) == false then
- return false
- end
- while saveObject.absPosRight < pCoord do -- move until at pCoord
- if (moveForward() == false) then
- return false
- end
- end
- end
- return true
- end
- -- First move vertically, then forward/backward and lastly right/left
- -- Destroys blocks in its path
- function moveToStart()
- if (moveToCoordUp(0)) == false then
- return false
- end
- if (moveToCoordRight(0)) == false then
- return false
- end
- if (moveToCoordForward(0)) == false then
- return false
- end
- -- Now the turtle is at (0, 0, 0) relative to its initiating position
- return true
- end
- function loadOff()
- for i = 1, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- turtle.select(1)
- end
- function refuelWithAll()
- for i = 1, 16 do
- if turtle.getFuelLevel() / turtle.getFuelLimit() > 0.8 then
- return
- end
- turtle.select(i)
- turtle.refuel()
- end
- turtle.select(1)
- end
- -- Loads off all items into chest under (0, 0, 0) position and returns to the position the turtle started this function in
- -- It trusts that the block under (0, 0, 0) is a chest or something else that can contain items
- -- Returns whether all items could be disposed or not
- function loadOffAndReturn()
- -- First, loads all information into the saveObject in case the server shuts down.
- if(saveObject.loadingOff.currently == false) then
- saveObject.loadingOff.currently = true
- saveObject.loadingOff.up = saveObject.absPosUp
- saveObject.loadingOff.forward = saveObject.absPosForward
- saveObject.loadingOff.right = saveObject.absPosRight
- saveObject.loadingOff.startDir = saveObject.absDirection
- end
- if (moveToCoordUp(0)) == false then return false end
- if (moveToCoordRight(0)) == false then return false end
- if (moveToCoordForward(0)) == false then return false end
- if (refuelWithAll() == false or loadOff()) == false then return false end
- if (moveToCoordForward(saveObject.loadingOff.forward)) == false then return false end
- if (moveToCoordRight(saveObject.loadingOff.right)) == false then return false end
- if (moveToCoordUp(saveObject.loadingOff.up)) == false then return false end
- if (turnDirection(saveObject.loadingOff.startDir)) == false then return false end
- saveObject.loadingOff = {currently = false} -- replaces the table of loadingOff with new table that only contains the currently = false
- return true -- finished without problems.
- end
- -- The inventory is full when last slot is used by something.
- -- return true if inventory is full, else false
- function ivFull()
- return turtle.getItemCount(16) > 0
- end
- -- Checks the inventory and loads off if necessary
- function handleLoadOff()
- if (ivFull()) then
- loadOffAndReturn()
- end
- end
- function handleForwardAndMakeHumanPath()
- if moveForward() == false then
- print("Could not make forward movement. Stopped early.")
- return false
- end
- handleLoadOff()
- while turtle.digUp() do -- Dig upwards while there is gravel or sand
- end
- handleLoadOff()
- end
- function mineHumanTunnel(pDirection, length)
- turnDirection(pDirection)
- for i = 1, length do -- beginnt mit i = 1 und letzter Durchlauf ist mit i = length
- if handleForwardAndMakeHumanPath() == false then -- Jedes mal wird geminet und dabei überprüft, ob das minen Erfolg hat
- print("Broke mineTunnel.")
- break
- end
- end
- end
- -- BEGINN -------------------------------------------------------------------------------------------------------
- local function mineZigZagTunnelForwardAndLeft(pTunnelWidth)
- mineHumanTunnel(Direction.FORWARD, 3)
- mineHumanTunnel(Direction.LEFT, pTunnelWidth-1)
- saveObject.finishedLayers = saveObject.finishedLayers + 1
- saveToJson(savePath, saveObject)
- end
- local function mineZigZagTunnelForwardAndRight(pTunnelWidth)
- mineHumanTunnel(Direction.FORWARD, 3)
- mineHumanTunnel(Direction.RIGHT, pTunnelWidth-1)
- saveObject.finishedLayers = saveObject.finishedLayers + 1
- saveToJson(savePath, saveObject)
- end
- -- Turtle beginnt unten links, gräbt 3 Blöcke vorwärts, pTunnelWidth-1 nach rechts, 3 vorwärts und wieder pTunnelWidth-1 nach links.
- -- Am Ende steht sie richtung Direction.FORWARD
- local function mineZigZagTunnels(pTunnelWidth)
- mineZigZagTunnelForwardAndRight(pTunnelWidth)
- mineZigZagTunnelForwardAndLeft(pTunnelWidth)
- end
- --- The turtle has to understand where it stopped to start at that position.
- --- if it was loading off, it should continue that.
- local function understandPositionAndContinue()
- saveObject = loadFromJson(savePath)
- if saveObject.loadingOff.currently == true then
- loadOffAndReturn()
- end
- -- Die Turtle ist wieder auf ihrem Platz und bereit, weiterzumachen. Jetzt muss sie wissen, wie sie weiterarbeiten soll.
- -- Wenn die Turtle bei Forward == 0, 3, 6, 9 usw. ist, soll sie seitlich arbeiten.
- -- Ist sie bei Forward == 0, 6, 12 usw. , soll sie nach links graben bis sie Right == -(tunnelWidth-1)/2 erreicht.
- -- Ist sie bei Forward == 3, 9, 15 usw. , soll sie nach rechts graben bis sie Right == (tunnelWidth-1)/2 erreicht.
- -- Hat sie den linkesten/rechtesten Spot erreicht, wird finishedLayers um 1 erhöht und sie macht normal weiter.
- -- Wenn die Turtle nicht bei Forward == 0, 3, 6, usw. ist, dann gäbt sie rückwärts, bis sie bei Forward == 0, 3, 6 usw. ist.
- -- Auch hier macht sie dann normal weiter.
- if (saveObject.absPosForward % 6 == 0) then -- Ist sie bei Forward == 0, 6, 12 usw. , soll sie nach links graben bis sie Right == -(tunnelWidth-1)/2 erreicht.
- turnDirection(Direction.LEFT)
- for i = saveObject.absPosRight, -(saveObject.tunnelWidth-1)/2, -1 do -- Auch noch beim vorletzten Block einmal graben. Danach nicht mehr.
- handleForwardAndMakeHumanPath()
- end
- saveObject.finishedLayers = saveObject.finishedLayers + 1
- elseif (saveObject.absPosForward % 6 == 3) then
- turnDirection(Direction.RIGHT)
- for i = saveObject.absPosRight, (saveObject.tunnelWidth-1)/2, 1 do -- Auch noch beim vorletzten Block einmal graben. Danach nicht mehr.
- handleForwardAndMakeHumanPath()
- end
- saveObject.finishedLayers = saveObject.finishedLayers + 1
- else -- Wenn die Turtle nicht bei Forward == 0, 3, 6, usw. ist, dann gäbt sie rückwärts, bis sie bei Forward == 0, 3, 6 usw. ist.
- turnDirection(Direction.BACKWARD)
- while saveObject.absPosForward % 3 > 0 do
- handleForwardAndMakeHumanPath()
- end
- end
- -- Nun ist noch zu unterscheiden, ob die Turtle links unten oder rechts unten ist.
- if (saveObject.absPosRight > 0 and saveObject.finishedLayers < layers) then
- mineZigZagTunnelForwardAndLeft(saveObject.tunnelWidth)
- end
- -- Zuletzt noch die Tunnel fertig graben, die noch gewünscht sind
- for i = saveObject.finishedLayers, saveObject.layers - 1, 2 do -- Die Schleife wird zuletzt noch einmal mit i = layers - 1 durchgeführt
- mineZigZagTunnels(pTunnelWidth)
- end
- end
- -- Turtle steht auf der Kiste. Gräbt (tunnelWidth-1)/2 nach links. Danach macht die Turtle layers-viele ZigZagTunnels und kehrt zum Anfang zurück.
- local function run(pLayers, pTunnelWidth)
- if(fileExists(savePath)) then
- understandPositionAndContinue()
- else
- mineHumanTunnel(Direction.LEFT, (pTunnelWidth-1)/2)
- saveObject.finishedLayers = saveObject.finishedLayers + 1
- for i = 1, pLayers - 1, 2 do
- mineZigZagTunnels(pTunnelWidth)
- end
- end
- moveToStart()
- loadOff()
- fs.delete(savePath) -- saveFile gets removed to let the script now, that a totally new saveFile has to be created next time
- end
- local tArgs = {...}
- if (fileExists(savePath)) then
- if (#tArgs >= 1) then
- print("Delete "..savePath.." and try again.")
- return
- end
- -- saveObject enthält die nötigen Variablen bereits
- else -- saveObject hat noch keine Werte
- if (#tArgs <= 1 or #tArgs >= 3) then
- print("Usage: <layers> <tunnelWidth>")
- return
- end
- if (tonumber(tArgs[1]) < 1 or tonumber(tArgs[2]) < 1 or tonumber(tArgs[1]) % 2 == 0 or tonumber(tArgs[2]) % 2 == 0) then
- print("Usage: <layers> <tunnelWidth>")
- print("layers and tunnelWidth have to be positive and odd.")
- return
- end
- saveObject.layers = tonumber(tArgs[1])
- saveObject.tunnelWidth = tonumber(tArgs[2])
- end
- run(saveObject.layers, saveObject.tunnelWidth)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement