Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- north = 1
- south = 2
- west = 3
- east = 4
- ignoreStart = 14
- northDir = {0, 1}
- southDir = {0, -1}
- eastDir = {1, 0}
- westDir = {-1, 0}
- startz = 0
- startx = 0
- starty = 0
- x = 0
- y = 0
- z = startz
- fuelStack = 1
- yieldStart = 1
- -- 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
- local doit = true
- 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
- --doDump()
- --doUpDownCheck()
- 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
- --doDump()
- --doUpDownCheck()
- 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
- --doDump()
- --doUpDownCheck()
- end
- -- end
- end
- function gotoreverse(tx, ty, tz)
- local dx = tx - x
- local dy = ty - y
- local dz = tz - z
- -- 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
- -- doUpDownCheck()
- --doDump()
- 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
- -- doUpDownCheck()
- --doDump()
- end
- -- end
- -- 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
- --doUpDownCheck()
- --doDump()
- end
- -- end
- end
- function testFuel()
- local dist = math.abs(startx - x) + math.abs(starty - y) + math.abs(startz - z);
- local fuel = estimateFuel()
- print("Estimated distance: "..dist)
- if(fuel < dist + 10) then
- return false
- end
- return true
- end
- function estimateFuel()
- return turtle.getFuelLevel();
- end
- function returnToRefuel()
- local ox = x
- local oy = y
- local oz = z
- goto(startx, starty, startz)
- refuelStation();
- turtle.refuel()
- goto(ox, oy, oz)
- end
- function refuelStation()
- faceSouth()
- turtle.select(1)
- turtle.suck()
- turtle.refuel()
- turtle.select(yieldStart)
- faceNorth()
- end
- print("Input Width")
- width = tonumber(read())
- print("Input Breadth")
- breadth = tonumber(read())
- print("Input Height")
- height = tonumber(read())
- print ("Start at top? y/n")
- fromTop = (read() == "y")
- print ("Start at right? y/n")
- fromRight = (read() == "y")
- altz = false
- alty = false
- altx = false
- for(cx = 0, width-1 do
- for(cz = 0, height-1 do
- for cy = 0, breadth-1 do
- forward()
- end
- turnLeft()
- turnLeft()
- if startTop
- if alt then
- goUp()
- else
- goDown()
- end
- else
- if not alt then
- goUp()
- else
- goDown()
- end
- end
- alty = not alty
- end
- altz = not altz
- end
- alt = false
- for cx=0, width-1 do
- for cy=0, breadth-1 do
- for cz=0, height-1 do
- local zz = cz;
- local yy = cy;
- local xx = cx;
- if fromTop then zz = -zz; end
- if not fromRight then yy = -cy; end
- if alt then
- goto(xx, (breadth - yy)-1, zz)
- else
- goto(xx, yy, zz)
- end
- end
- end
- alt = not alt
- end
- goto(0, 0, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement