Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function checkRot(inDir)
- for i = 1, 4 do
- if rotation[i] == inDir then
- return i
- end
- end
- return false
- end
- function turnAroundDir(numDir)
- if numDir <= 2 then
- return numDir + 2
- else
- return numDir - 2
- end
- return false
- end
- local rotation = {"north", "east", "south", "west"}
- if not fs.exists("/.persistance") then
- print("Persistant variable folder not found. \n Creating...")
- fs.makeDir("/.persistance")
- print("Created")
- end
- function store(sName, stuff)
- local filePath = fs.combine("/.persistance", sName)
- if stuff == nil then
- return fs.delete(filePath)
- end
- local handle = fs.open(filePath, "w")
- handle.write(textutils.serialize(stuff))
- handle.close()
- end
- function pull(sName)
- local filePath = fs.combine("/.persistance", sName)
- local handle = fs.open(filePath, "r")
- local stuff = handle.readAll()
- handle.close()
- return textutils.unserialize(stuff)
- end
- if not fs.open(".persistance/turtleCoords", "r") then
- coords = { }
- coords["x"] = 0
- coords["y"] = 0
- coords["z"] = 0
- store("turtleCoords", coords)
- else
- coords = pull("turtleCoords")
- end
- if not fs.open(".persistance/turtleDir", "r") then
- direction = "north"
- store("turtleDir", direction)
- else
- direction = pull("turtleDir")
- end
- if coords["x"] == 0 and coords["z"] == 0 and coords["y"] == 0 then
- local originalDir = direction
- local behindDir = turnAroundDir(checkRot(direction))
- faceTo(behindDir)
- local success, idtable = turtle.inspectDown()
- if success and idtable.name == "minecraft:chest" then
- print("Home chest found")
- faceTo(originalDir)
- else
- error("Need chest behind turtle to start work!")
- faceTo(originalDir)
- end
- end
- function mineAround()
- if turtle.inspectDown() then
- turtle.digDown()
- end
- if turtle.inspectUp() then
- turtle.digUp()
- end
- local returnLoc = direction
- local numReverseLoc = turnAroundDir(checkRot(direction))
- for f = 1, 4 do
- local f2 = turnAroundDir(f)
- if f2 ~= numReverseLoc then
- faceTo(rotation[f])
- local success, idtable = turtle.inspect()
- if success and idtable.name ~= "minecraft:chest" then
- turtle.dig()
- end
- faceTo(returnLoc)
- end
- end
- end
- function testForInv()
- local invSpace = 16
- for i = 1, 16 do
- turtle.select(i)
- if turtle.getItemDetail() then
- invSpace = invSpace - 1
- end
- end
- if invSpace <= 4 then
- return false
- else
- return true
- end
- end
- function forward(dirToGo)
- if dirToGo == "north" then
- coords["z"] = coords["z"] + 1
- store("turtleCoords", coords)
- if direction == "north" then
- turtle.forward()
- elseif direction == "south" then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- elseif direction == "west" then
- turtle.turnRight()
- turtle.forward()
- elseif direction == "east" then
- turtle.turnLeft()
- turtle.forward()
- end
- direction = "north"
- store("turtleDir", direction)
- elseif dirToGo == "east" then
- coords["x"] = coords["x"] + 1
- store("turtleCoords", coords)
- if direction == "north" then
- turtle.turnRight()
- turtle.forward()
- elseif direction == "west" then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- elseif direction == "east" then
- turtle.forward()
- elseif direction == "south" then
- turtle.turnLeft()
- turtle.forward()
- end
- direction = "east"
- store("turtleDir", direction)
- elseif dirToGo == "south" then
- coords["z"] = coords["z"] - 1
- store("turtleCoords", coords)
- if direction == "south" then
- turtle.forward()
- elseif direction == "north" then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- elseif direction == "east" then
- turtle.turnRight()
- turtle.forward()
- elseif direction == "west" then
- turtle.turnLeft()
- turtle.forward()
- end
- direction = "south"
- store("turtleDir", direction)
- elseif dirToGo == "west" then
- coords["x"] = coords["x"] - 1
- store("turtleCoords", coords)
- if direction == "west" then
- turtle.forward()
- elseif direction == "east" then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- elseif direction == "south" then
- turtle.turnRight()
- turtle.forward()
- elseif direction == "north" then
- turtle.turnLeft()
- turtle.forward()
- end
- direction = "west"
- store("turtleDir", direction)
- end
- if dirToGo == "up" then
- coords["y"] = coords["y"] + 1
- store("turtleCoords", coords)
- turtle.up()
- elseif dirToGo == "down" then
- coords["y"] = coords["y"] - 1
- store("turtleCoords", coords)
- turtle.down()
- end
- --fuelTestCount()
- end
- function moveTo(x2, y2, z2)
- if x2 > coords["x"] then
- local x3 = x2 - coords["x"]
- for f = 1, x3 do
- forward("east")
- end
- elseif x2 < coords["x"] then
- local x3 = math.abs((math.abs(x2)) - coords["x"])
- for f = 1, x3 do
- forward("west")
- end
- end
- if y2 > coords["y"] then
- local y3 = y2 - coords["y"]
- for g = 1, y3 do
- forward("up")
- end
- elseif y2 < coords["y"] then
- local y3 = math.abs((math.abs(y2)) - coords["y"])
- for g = 1, y3 do
- forward("down")
- end
- end
- if z2 > coords["z"] then
- local z3 = z2 - coords["z"]
- for h = 1, z3 do
- forward("up")
- end
- elseif z2 < coords["z"] then
- local z3 = math.abs(math.abs(z2) - coords["z"])
- for h = 1, z3 do
- forward("down")
- end
- end
- end
- function faceTo(dirToTurn)
- if dirToTurn == "north" then
- if direction == "south" then
- for i = 1, 2 do turtle.turnRight() end
- elseif direction == "east" then
- turtle.turnLeft()
- elseif direction == "west" then
- turtle.turnRight()
- end
- direction = "north"
- store("turtleDir", direction)
- elseif dirToTurn == "south" then
- if direction == "north" then
- for i = 1, 2 do turtle.turnRight() end
- elseif direction == "east" then
- turtle.turnRight()
- elseif direction == "west" then
- turtle.turnLeft()
- end
- direction = "south"
- store("turtleDir", direction)
- elseif dirToTurn == "east" then
- if direction == "west" then
- for i = 1, 2 do turtle.turnRight() end
- elseif direction == "north" then
- turtle.turnRight()
- elseif direction == "south" then
- turtle.turnLeft()
- end
- direction = "east"
- store("turtleDir", direction)
- elseif dirToTurn == "west" then
- if direction == "east" then
- for i = 1, 2 do turtle.turnRight() end
- elseif direction == "north" then
- turtle.turnLeft()
- elseif direction == "south" then
- turtle.turnRight()
- end
- direction = "west"
- store("turtleDir", direction)
- end
- end
- function dropOff()
- moveTo(0,0,0)
- for i = 1, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- turtle.select(1)
- end
- function mine()
- for x = 0, 15 do
- for z = 1, 15 do
- for y = 0, -4, -1 do
- if not testForInv() then dropOff() end
- mineAround()
- moveto(x,y,z)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment