Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- navi movement utilities 1.01 by Andrakon http://www.youtube.com/user/BadashGames
- --[[ Variable and Function list:
- getPosition() returns the vector "position"; to compare make sure to assign to a variable and use yourvariable:tostring() in the if statement
- setPostion(x,y,z) sets the position, expects numbers
- getFacing() returns a string or nil if its empty
- setFacing(string) set to n,s,e, or w example: setFacing("n")
- getBedrock() returns the number that the turtle will not navigate to or below in the y position to avoid bedrock
- setBedrock(number) sets the postion the turtle will not navigate to or below, incase your bedrock is lower
- fuelCheck(number) checks the fuel level while compairing it to a number given; 320 by default
- facingCheck() ensures the turtle knows what direction it is facing and attempts to figure it out on its own if a GPS is available
- gpsUpdate() updates the vector "position" with gps cordinates; can accept a number for acceptable delay, default 5
- positionCheck() used to check if the y position is 0, if so it will update the position with gps or ask the player to do so
- face(string) orients the turtle, accepts l,r,n,s,e,w, or b for back; usage: face("n") to face north; updates variable "facing"
- move(string) does not break blocks or kill mobs, but allows movement while updating x,y,z cordinates with variable "position"; accepts f,b,u,d; forward by default
- go(string, number) extends move()'s functionality, breaking blocks, attacks mobs in the way. accepts f,b,u,d and an optional number; forward by default, 1 block by default; usage: go("f", 3)
- dig(string), attack(string), detect(string), and compare(string); locally wrapped versions of turtle functions; accepts f,b,u,d; usage detect("b") to detect behind the turtle, it turns around to do so
- getToX(number), getToY(number), and getToZ(number) navigate to specific cordinates given to them; returns number of blocks traveled
- ]]
- -- Variables
- local position = vector.new(0,0,0) -- make sure to update before moving
- local facing = nil -- n,s,e,w make sure to update before moving
- local bedrock = 5 -- will not navigate below this height
- -- vectors used by move() to update vector "position"; don't change these
- local av = vector.new(1, 0, 0) -- add to go east
- local bv = vector.new(0, 1, 0) -- add to go up
- local cv = vector.new(0, 0, 1) -- add to go south
- -- Functions
- -- get and set variables
- function getPosition()
- return position
- end
- function setPosition(sx, sy, sz)
- if type(sx) ~= "number" then error("number expected", 2) end
- if type(sy) ~= "number" then error("number expected", 2) end
- if type(sz) ~= "number" then error("number expected", 2) end
- local tempvar = vector.new(sx, sy, sz)
- position = tempvar
- end
- function getFacing()
- return facing
- end
- function setFacing(someway)
- if type(someway) ~= "string" then error("string expected", 2) end
- facing = someway
- end
- function getBedrock()
- return bedrock
- end
- function setBedrock(set)
- if type(set) ~= "number" then error("number expected", 2) end
- bedrock = set
- end
- -- fuelCheck() checks the fuel level while compairing it to the given ammount
- function fuelCheck(minFuel)
- local minFuel = minFuel or 320
- if type(minFuel) ~= "number" then error("expecting a number", 2) end
- if minFuel > turtle.getFuelLimit() then minFuel = turtle.getFuelLimit() end
- term.clear()
- term.setCursorPos(1,1)
- print("My fuel level is ", turtle.getFuelLevel())
- local saveSlot = turtle.getSelectedSlot()
- turtle.select(16)
- if turtle.getFuelLevel() < minFuel then
- print("Refueling...")
- while turtle.getFuelLevel() < minFuel do
- turtle.refuel(1)
- end
- print("My fuel level is now ", turtle.getFuelLevel())
- turtle.select(saveSlot)
- end
- end
- -- facingCheck() ensures the turtle knows what direction it is facing and attempts to figure it out on its own if a GPS is available
- function facingCheck()
- if facing ~= "e" then
- if facing ~= "w" then
- if facing ~= "s" then
- if facing ~= "n" then
- local pos1 = vector.new(gps.locate(5))
- if pos1.y ~= 0 then
- if turtle.detect() then
- if not turtle.dig() then
- error("Turtle encountered undiggable block while finding orientation")
- os.exit()
- end
- while not turtle.forward() do
- turtle.attack()
- end
- else
- while not turtle.forward() do
- turtle.attack()
- end
- end
- os.sleep(5)
- local pos2 = vector.new(gps.locate(5))
- local victor = pos2 - pos1
- if victor.x == 1 then facing = "e" end
- if victor.x == -1 then facing = "w" end
- if victor.z == 1 then facing = "s" end
- if victor.z == -1 then facing = "n" end
- end
- if facing ~= "e" then
- if facing ~= "w" then
- if facing ~= "s" then
- if facing ~= "n" then
- print("What direction am I facing? n,s,e,w \n")
- input = read()
- if input == "n" or input == "s" or input == "e" or input == "w" then
- facing = input
- term.clear()
- term.setCursorPos(1,1)
- os.sleep(2)
- else
- print("Unnacceptable facing, stoping.\n")
- os.exit()
- end
- end
- end
- end
- end
- end
- end
- end
- end
- return facing
- end
- -- gpsUpdate() updates the vector "position" with gps cordinates
- function gpsUpdate(tm)
- local tm = tm or 5
- if type(tm) ~= "number" then error("number expected", 2) end
- local posGet = vector.new(gps.locate(tm))
- if posGet.y ~= 0 then
- position = posGet
- else
- return false
- end
- end
- -- positionCheck() used to check if the y position is 0, if so it will update the position with gps or ask the player to do so
- function positionCheck()
- if position.y == 0 then gpsUpdate() end
- if position.y == 0 then
- while input ~= "y" do
- term.clear()
- term.setCursorPos(1,1)
- print("What is my x position?")
- local myX = read()
- print("What is my y position?")
- local myY = read()
- print("what is my z position?")
- local myZ = read()
- local newPos = vector.new(myX, myY, myZ)
- print("Is ", newPos, " correct? y/n")
- input = read()
- position = newPos
- end
- end
- return true
- end
- -- faceWrap() is used to keep track of the n,s,e,w facing of the turtle; ment to be used by face()
- local function faceWrap(lr)
- if lr ~= "l" then
- if lr ~= "r" then error("l or r expected", 2) end
- end
- if lr == "l" then
- turtle.turnLeft()
- if facing == "n" then facing = "w"; return end
- if facing == "w" then facing = "s"; return end
- if facing == "s" then facing = "e"; return end
- if facing == "e" then facing = "n"; return end
- end
- if lr == "r" then
- turtle.turnRight()
- if facing == "n" then facing = "e"; return end
- if facing == "e" then facing = "s"; return end
- if facing == "s" then facing = "w"; return end
- if facing == "w" then facing = "n"; return end
- end
- return true
- end
- -- face() orients the turtle, accepts l,r,n,s,e,w, or b for back; usage: face("n") to face north; updates variable "facing"
- function face(way)
- if way ~= "n" then
- if way ~= "s" then
- if way ~= "e" then
- if way ~= "w" then
- if way ~= "l" then
- if way ~= "r" then
- if way ~= "b" then error("l,r,n,s,e,w expected", 2) end
- end
- end
- end
- end
- end
- end
- if way == "l" then faceWrap("l")
- elseif way == "r" then faceWrap("r")
- elseif way == "b" then
- faceWrap("r")
- faceWrap("r")
- elseif way == "n" then
- if facing == "e" then faceWrap("l")
- else while way ~= facing do faceWrap("r") end
- end
- elseif way == "e" then
- if facing == "s" then faceWrap("l")
- else while way ~= facing do faceWrap("r") end
- end
- elseif way == "s" then
- if facing == "w" then faceWrap("l")
- else while way ~= facing do faceWrap("r") end
- end
- elseif way == "w" then
- if facing == "n" then faceWrap("l")
- else while way ~= facing do faceWrap("r") end
- end
- end
- return true
- end
- -- move() does not break blocks or kill mobs, but allows movement while updating x,y,z cordinates with variable vector "position"; accepts f,b,u,d; forward by default
- function move(dir) -- f,b,u,d
- local dir = dir or "f"
- if dir ~= "f" then
- if dir ~= "b" then
- if dir ~= "u" then
- if dir ~= "d" then error("f,b,u,d expected", 2) end
- end
- end
- end
- if dir == "f" then
- if turtle.forward() then
- if facing == "s" then
- position = position + cv
- elseif facing == "w" then
- position = position - av
- elseif facing == "n" then
- position = position - cv
- elseif facing == "e" then
- position = position + av
- else
- error("Wrong Facing Data")
- return false
- end
- else
- return false
- end
- elseif dir == "b" then
- if turtle.back() then
- if facing == "s" then
- position = position - cv
- elseif facing == "w" then
- position = position + av
- elseif facing == "n" then
- position = position + cv
- elseif facing == "e" then
- position = position - av
- else
- error("Wrong Facing Data")
- return false
- end
- else
- return false
- end
- elseif dir == "u" then
- if turtle.up() then
- position = position + bv
- else
- return false
- end
- elseif dir == "d" then
- if position.y > bedrock then
- if turtle.down() then
- position = position - bv
- else
- return false
- end
- else
- return false
- end
- else
- error("Wrong Direction Data")
- return false
- end
- return true
- end
- -- forward, up, and down versions of dig, attack, detect, and compare
- function dig(dir)
- local dir = dir or "f"
- if dir ~= "f" then
- if dir ~= "b" then
- if dir ~= "u" then
- if dir ~= "d" then error("f,b,u,d expected", 2) end
- end
- end
- end
- if dir == "f" then
- if not turtle.dig() then
- return false
- end
- elseif dir == "b" then
- local failState = false
- face("b")
- if not turtle.dig() then
- failState = true
- end
- face("b")
- if failState == true then return false end
- elseif dir == "u" then
- if not turtle.digUp() then
- return false
- end
- elseif dir == "d" then
- if not turtle.digDown() then
- return false
- end
- else
- error("Wrong Direction Data")
- return false
- end
- return true
- end
- function attack(dir)
- local dir = dir or "f"
- if dir ~= "f" then
- if dir ~= "b" then
- if dir ~= "u" then
- if dir ~= "d" then error("f,b,u,d expected", 2) end
- end
- end
- end
- if dir == "f" then
- if not turtle.attack() then
- return false
- end
- elseif dir == "b" then
- local failState = false
- face("b")
- if not turtle.attack() then
- failState = true
- end
- face("b")
- if failState == true then return false end
- elseif dir == "u" then
- if not turtle.attackUp() then
- return false
- end
- elseif dir == "d" then
- if not turtle.attackDown() then
- return false
- end
- else
- error("Wrong Direction Data")
- return false
- end
- return true
- end
- function detect(dir)
- local dir = dir or "f"
- if dir ~= "f" then
- if dir ~= "b" then
- if dir ~= "u" then
- if dir ~= "d" then error("f,b,u,d expected", 2) end
- end
- end
- end
- if dir == "f" then
- if not turtle.detect() then
- return false
- end
- elseif dir == "b" then
- local failState = false
- face("b")
- if not turtle.detect() then
- failState = true
- end
- face("b")
- if failState == true then return false end
- elseif dir == "u" then
- if not turtle.detectUp() then
- return false
- end
- elseif dir == "d" then
- if not turtle.detectDown() then
- return false
- end
- else
- error("Wrong Direction Data")
- return false
- end
- return true
- end
- function compare(dir)
- local dir = dir or "f"
- if dir ~= "f" then
- if dir ~= "b" then
- if dir ~= "u" then
- if dir ~= "d" then error("f,b,u,d expected", 2) end
- end
- end
- end
- if dir == "f" then
- if not turtle.compare() then
- return false
- end
- elseif dir == "b" then
- local failState = false
- face("b")
- if not turtle.compare() then
- failState = true
- end
- face("b")
- if failState == true then return false end
- elseif dir == "u" then
- if not turtle.compareUp() then
- return false
- end
- elseif dir == "d" then
- if not turtle.compareDown() then
- return false
- end
- else
- error("Wrong Direction Data")
- return false
- end
- return true
- end
- -- go() extends move()'s functionality, breaking blocks, attacks mobs in the way. accepts f,b,u,d and an optional number; forward by default, 1 block by default; usage: go("f", 3)
- function go(way, blocks)
- local way = way or "f"
- if way ~= "f" then
- if way ~= "b" then
- if way ~= "u" then
- if way ~= "d" then error("f,b,u,d expected", 2) end
- end
- end
- end
- local blocks = blocks or 1
- if type(blocks) ~= "number" then error("number expected as second number for go(direction, number)", 2) end
- for i = 1 , blocks do
- if detect(way) then
- if not dig(way) then
- -- error("Undiggable Block")
- return false
- end
- while not move(way) do
- attack(way)
- dig(way)
- end
- else
- while not move(way) do
- attack(way)
- dig(way)
- end
- end
- end
- return true
- end
- -- getToX(), getToY(), and getToZ() navigate to specific cordinates given to them
- function getToX(numb)
- if type(numb) ~= "number" then error("expecting a number", 2) end
- numb = position.x - numb
- if numb > 0 then
- face("w")
- go("f", numb)
- elseif numb < 0 then
- numb = math.abs(numb)
- face("e")
- go("f", numb)
- end
- return numb
- end
- function getToY(numb)
- if type(numb) ~= "number" then error("expecting a number", 2) end
- numb = position.y - numb
- if numb > 0 then
- go("d", numb)
- elseif numb < 0 then
- numb = math.abs(numb)
- go("u", numb)
- end
- return numb
- end
- function getToZ(numb)
- if type(numb) ~= "number" then error("expecting a number", 2) end
- numb = position.z - numb
- if numb > 0 then
- face("n")
- go("f", numb)
- elseif numb < 0 then
- numb = math.abs(numb)
- face("s")
- go("f", numb)
- end
- return numb
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement