Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- north = 1
- south = 2
- west = 3
- east = 4
- northDir = {0, 1}
- southDir = {0, -1}
- eastDir = {1, 0}
- westDir = {-1, 0}
- startz = 70
- startx = 0
- starty = 0
- x = 0
- y = 0
- z = startz
- fuelStack = 1
- yieldStart = 4
- -- start at facing north
- currentDir = north
- local mov = {}
- local rot = {}
- local dig = {}
- mov.forward = function(times)
- local boolean v = false
- for i=1,times do
- if turtle.detect() == false then
- while not turtle.forward() do
- sleep(1)
- end
- v = true
- else
- v = false
- return v
- end
- end
- return v
- end
- mov.back = function(times)
- local boolean v = false
- for i=1,times do
- while not turtle.back() do
- sleep(1)
- end
- v = true
- end
- return v
- end
- mov.up = function(times)
- local boolean v = false
- for i=1,times do
- if turtle.detectUp() == false then
- while not turtle.up() do
- sleep(1)
- end
- v = true
- else
- v = false
- return v
- end
- end
- return v
- end
- mov.down = function(times)
- local boolean v = false
- print("mov.down");
- for i=1,times do
- if turtle.detectDown() == false then
- print("!detectDown");
- while not turtle.down() do
- sleep(1)
- end
- v = true
- else
- print("detectDown");
- v = false
- return v
- end
- end
- return v
- end
- mov.place = function()
- while not turtle.place() do
- sleep(1)
- end
- return true
- end
- rot.right = function()
- while not turtle.turnRight() do
- sleep(1)
- end
- if RotatePosition == "FORWARD" then
- RotatePosition = "RIGHT"
- elseif RotatePosition == "LEFT" then
- RotatePosition = "FORWARD"
- end
- filewriteline("BranchMineData", 5, RotatePosition)
- return true
- end
- rot.left = function()
- while not turtle.turnLeft() do
- sleep(1)
- end
- if RotatePosition == "FORWARD" then
- RotatePosition = "LEFT"
- elseif RotatePosition == "RIGHT" then
- RotatePosition = "FORWARD"
- end
- filewriteline("BranchMineData", 5, RotatePosition)
- return true
- end
- dig.forward = function()
- if turtle.detect() then
- while not turtle.dig() do
- sleep(1)
- end
- return true
- else
- print("No Block to mine forward")
- return false
- end
- end
- dig.up = function()
- if turtle.detectUp() then
- while not turtle.digUp() do
- sleep(1)
- end
- return true
- else
- print("No Block to mine up")
- return false
- end
- end
- dig.down = function()
- if turtle.detectDown() then
- while not turtle.digDown() do
- sleep(1)
- end
- return true
- else
- print("No Block to mine down")
- return false
- end
- end
- function DigMoveForward()
- if mov.forward(1) == false then
- dig.forward()
- sleep(0.5)
- DigMoveForward()
- end
- end
- function DigMoveUp()
- if mov.up(1) == false then
- dig.up()
- sleep(0.5)
- DigMoveUp()
- else
- z = z + 1
- end
- end
- function DigMoveDown()
- print("moving down")
- if mov.down(1) == false then
- print("failed moving down - digging")
- dig.down()
- sleep(0.5)
- print("trying again")
- DigMoveDown()
- else
- print("aucceeeded")
- z = z - 1
- end
- end
- function faceEast()
- if(currentDir == north) then
- turtle.turnRight()
- elseif(currentDir == west) then
- turtle.turnRight()
- turtle.turnRight()
- elseif(currentDir == south) then
- turtle.turnLeft()
- end
- currentDir = east;
- end
- function faceNorth()
- if(currentDir == east) then
- turtle.turnLeft()
- elseif(currentDir == south) then
- turtle.turnRight()
- turtle.turnRight()
- elseif(currentDir == west) then
- turtle.turnRight()
- end
- currentDir = north;
- end
- function faceSouth()
- if(currentDir == east) then
- turtle.turnRight()
- elseif(currentDir == north) then
- turtle.turnRight()
- turtle.turnRight()
- elseif(currentDir == west) then
- turtle.turnLeft()
- end
- currentDir = south;
- end
- function faceWest()
- if(currentDir == south) then
- turtle.turnRight()
- elseif(currentDir == east) then
- turtle.turnRight()
- turtle.turnRight()
- elseif(currentDir == north) then
- turtle.turnLeft()
- end
- currentDir = west;
- end
- function forward()
- DigMoveForward()
- if(currentDir == east) then
- x = x + 1
- elseif(currentDir == north) then
- y = y - 1
- elseif(currentDir == south) then
- y = y + 1
- elseif(currentDir == west) then
- x = x - 1
- end
- end
- function goWest()
- faceWest()
- forward()
- end
- function goEast()
- faceEast()
- forward()
- end
- function goNorth()
- faceNorth()
- forward()
- end
- function goSouth()
- faceSouth()
- forward()
- end
- function goUp()
- DigMoveUp()
- end
- function goDown()
- DigMoveDown()
- end
- local function readLines(sPath)
- local file = fs.open(sPath, "r") -- open the file
- if file then -- check if it's open
- local tLines = {} -- table to store the lines
- local sLine = file.readLine() -- read a line from the file
- while sLine do -- while there's a line in the file
- table.insert(tLines, sLine) -- add it to the table
- sLine = file.readLine() -- get the next line
- end
- file.close() -- close the file
- return tLines -- return the table with the lines
- end
- return nil -- there was an error opening the file, return nil to let the user know
- end
- local function writeLines(sPath, tLines)
- local file = fs.open(sPath, "w") -- open the file
- if file then -- check if the file is open
- for _, sLine in ipairs(tLines) do -- for each line in the table
- file.writeLine(sLine) -- write the line
- end
- file.close() -- close the file
- end
- end
- function filereadline(filename, line)
- local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
- if not tLines then -- if there was an error
- return nil -- return nil/error
- end
- return tLines[line] -- return the line
- end
- function filewriteline(filename, line, text)
- local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
- if tLines then -- if there was no error
- tLines[line] = text -- set the line
- writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
- end
- end
- function goto(tx, ty, tz)
- local dx = tx - x
- local dy = ty - y
- local dz = tz - z
- -- if dz ~= 0 then
- while z ~= tz do
- print(z .. " - " .. tz)
- if z > tz then
- goDown()
- else
- goUp()
- end
- if(not testFuel()) then
- returnToRefuel()
- end
- end
- -- end
- -- if dx ~= 0 then
- while x ~= tx do
- print(x .. " - " .. tx)
- if x > tx then
- goWest()
- else
- goEast()
- end
- if(not testFuel()) then
- returnToRefuel()
- end
- end
- -- end
- -- if dy ~= 0 then
- while y ~= ty do
- print(y .. " - " .. ty)
- if y < ty then
- goSouth()
- else
- goNorth()
- end
- if(not testFuel()) then
- returnToRefuel()
- end
- end
- -- end
- end
- function testFuel()
- local dist = math.abs(startx - x) + math.abs(starty - y) + math.abs(startz - z);
- local fuel = estimateFuel();
- if(fuel < dist + 10) then
- return false;
- end
- return true
- end
- function estimateFuel()
- local a = turtle.getItemCount(1)
- local b = turtle.getItemCount(2)
- local c = turtle.getItemCount(3)
- local steps = (a + b + c) * 96
- return steps + turtle.getFuelLevel();
- end
- function returnToRefuel()
- local ox = x
- local oy = y
- local oz = z
- goto(startx, starty, startz)
- refuelStation();
- goto(ox, oy, oz)
- end
- function refuelStation()
- faceSouth()
- turtle.select(1)
- turtle.suck()
- turtle.select(2)
- turtle.suck()
- turtle.select(3)
- turtle.suck()
- turtle.select(1)
- turtle.refuel()
- turtle.suck()
- turtle.select(yieldStart)
- end
- refuelStation()
- goto (0, 0, 5)
- for i = 0, 100 do
- for y=0, -64 do
- goto (startx+i, y, 5)
- goto (startx+i, y, 6)
- end
- end
- goto (0, 0, startz)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement