Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Self-tracking System created by Latias1290.
- -- Original code stolen from:http://www.computercraft.info/wiki/Turtle_GPS_self-tracker_expansion_(tutorial) (04/29/2016)
- -- Original code has been modified to be more useful. (supports loading and saving)
- local xPos, yPos, zPos = nil
- face = 1 -- 1 is west, 2 is south, 3 is east, 0 is north
- cal = false
- local fileLoc = "MatthewC/appdata/turtleLoc" -- The name of the file.
- local debug = false -- Stores whether or not we are in debug mode (if true, print our location after every move)
- local slowPrint = false -- Stores whether to use slowprint or fast print when printing location
- function setDebug(val)
- debug = val
- end
- function getDebug(val)
- return debug
- end
- function setSlowPrint(val)
- slowPrint = val
- end
- function getSlowPrint()
- return slowPrint
- end
- -- Converts a string direction into a face EG N or NORTH (case insensitive) gets converted to 0
- function convertStringToFace(f)
- if string.upper(f) == "N" or string.upper(f) == "NORTH" then
- return 0
- elseif string.upper(f) == "W" or string.upper(f) == "WEST" then
- return 1
- elseif string.upper(f) == "S" or string.upper(f) == "SOUTH" then
- return 2
- elseif string.upper(f) == "E" or string.upper(f) == "EAST" then
- return 3
- else
- return nil
- end
- end
- -- Converts an int into a direction EG 0 : North, 1: West, 2: South, 3: East
- function convertFaceToString(f)
- if f == 0 then
- return "N"
- elseif f == 1 then
- return "W"
- elseif f == 2 then
- return "S"
- elseif f == 3 then
- return "E"
- else
- return nil
- end
- end
- function getLocationAsString()
- if not cal then
- return "Location: Not Calibrated!"
- end
- return "Location: x="..xPos..", y="..yPos..", z="..zPos..", Direction="..convertFaceToString(face)
- end
- function printLocation()
- if slowPrint then
- textutils.slowPrint(getLocationAsString())
- else
- print(getLocationAsString())
- end
- return printVal
- end
- function saveLocation() -- save the location of the turtle, for persistence.
- if not cal then
- return false
- end
- local h = fs.open(fileLoc, "w")
- h.writeLine(xPos)
- h.writeLine(yPos)
- h.writeLine(zPos)
- h.writeLine(face)
- h.close()
- return true
- end
- function loadLocation() -- Load the location of the turtle, for persistence.
- local h = fs.open(fileLoc, "r")
- if not h then
- return false
- end
- local h = fs.open(fileLoc, "r")
- xPos = tonumber(h.readLine())
- yPos = tonumber(h.readLine())
- zPos = tonumber(h.readLine())
- face = tonumber(h.readLine())
- cal = true
- h.close()
- return true
- end
- function setLocation(face) -- get gps using other computers
- xPos, yPos, zPos = gps.locate()
- face = convertStringToFace(direction)
- cal = true
- saveLocation()
- end
- function manSetLocation(x, y, z, direction) -- manually set location
- xPos = x
- yPos = y
- zPos = z
- face = convertStringToFace(direction)
- cal = true
- saveLocation()
- end
- function getLocation() -- return the location
- if not xPos then
- return nil
- else
- return xPos, yPos, zPos, convertFaceToString(face)
- end
- end
- function isCalibrated() -- Return whether or not the api is calibrated
- return cal
- end
- function turnLeft() -- turn left
- if not turtle.turnLeft() then
- return false
- end
- if face == 0 then
- face = 1
- elseif face == 1 then
- face = 2
- elseif face == 2 then
- face = 3
- elseif face == 3 then
- face = 0
- end
- saveLocation()
- return true
- end
- function turnRight() -- turn right
- if not turtle.turnRight() then
- return false
- end
- if face == 0 then
- face = 3
- elseif face == 1 then
- face = 0
- elseif face == 2 then
- face = 1
- elseif face == 3 then
- face = 2
- end
- saveLocation()
- return true
- end
- function turnToOrientation(direction) -- Turn to a specified N, E, S or W direction. Returns true if successful, or false it not successul.
- inFace = convertStringToFace(direction)
- if not inFace then
- return false
- end
- -- Special case where we are already facing the correct direction
- if inFace == face then
- return true
- end
- -- Special cases where we just turn once.
- -- Cases for turning left
- if (inFace == 0 and face == 3) or (inFace == 3 and face == 2) or (inFace == 2 and face == 1) or (inFace == 1 and face == 0) then
- return turnLeft()
- -- Cases for turning right
- elseif (inFace == 0 and face == 1) or (inFace == 1 and face == 2) or (inFace == 2 and face == 3) or (inFace == 3 and face == 0) then
- return turnRight()
- end
- -- If we get to this point, we are guaranteed to need to turn exactly twice, so lets do so.
- turnSuccess = turnLeft()
- if not turnSuccess then
- return false
- end
- return turnLeft()
- end
- function forward() -- go forward
- if not turtle.forward() then
- return false
- end
- if cal == true then
- if face == 0 then
- zPos = zPos - 1
- elseif face == 1 then
- xPos = xPos - 1
- elseif face == 2 then
- zPos = zPos + 1
- elseif face == 3 then
- xPos = xPos + 1
- end
- else
- print("Not Calibrated.")
- end
- saveLocation()
- printLocation()
- return true
- end
- function back() -- go back
- if not turtle.back() then
- return false
- end
- if cal == true then
- if face == 0 then
- zPos = zPos + 1
- elseif face == 1 then
- xPos = xPos + 1
- elseif face == 2 then
- zPos = zPos - 1
- elseif face == 3 then
- xPos = xPos - 1
- end
- else
- print("Not Calibrated.")
- end
- saveLocation()
- printLocation()
- return true
- end
- function up() -- go up
- if not turtle.up() then
- return false
- end
- if cal == true then
- yPos = yPos + 1
- else
- print("Not Calibrated.")
- end
- saveLocation()
- printLocation()
- return true
- end
- function down() -- go down
- if not turtle.down() then
- return false
- end
- if cal == true then
- yPos = yPos - 1
- else
- print("Not Calibrated.")
- end
- saveLocation()
- printLocation()
- return true
- end
- function jump() -- perform a jump. useless? yup!
- turtle.up()
- turtle.down()
- end
- loadLocation() -- Try to load the location of the turtle, if it's already cached.
Add Comment
Please, Sign In to add comment