Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 'Map' by EldidiStroyrr (LDDestroier)
- -- This program can be copied, and it can be modified, but it
- -- can NOT be copied, modified slightly, and then claimed as your own.
- -- You must give me, EldidiStroyrr, credit for the creation and
- -- only reference yourself as some guy who changed a bit.
- parentDir = "/" .. fs.getDir(shell.getRunningProgram())
- waypointFile = "/" .. fs.combine(parentDir, ".waypoints")
- mapConfigFile = "/" .. fs.combine(parentDir, "/.map_conf")
- function updateMapProgram()
- local mapName = shell.getRunningProgram()
- local updateFile = http.get("http://pastebin.com/raw.php?i=G7SmdMKD").readAll()
- local file = fs.open(mapName, "w")
- file.write(updateFile)
- file.close()
- return fs.getSize(mapName)
- end
- scr_x, scr_y = term.getSize()
- local tArg = {...}
- if tArg[1] ~= nil then
- if tArg[1] == "cc" then
- if fs.exists(mapConfigFile) then
- fs.delete(mapConfigFile)
- print("Config cleared.")
- else
- print("No config to clear.")
- end
- return
- elseif tArg[1] == "cw" then
- if fs.exists(waypointFile) then
- fs.delete(waypointFile)
- print("Waypoints cleared.")
- else
- print("No waypoints to clear.")
- end
- return
- elseif tArg[1] == "update" then
- local mapName = shell.getRunningProgram()
- write("Updating " .. mapName .. "...")
- local fileSize = updateMapProgram()
- print("done (got " .. fs.getSize(mapName) .. " bytes)")
- return true
- end
- if tArg[1] ~= "mon" then
- term.redirect(term.native())
- end
- end
- mapColors = {
- colors.gray, --Background color
- colors.white, --Border color
- colors.yellow, --Waypoint color
- colors.red, --Off-screen waypoint text color
- colors.lightBlue, --Player color
- colors.black, --Command screen text color
- colors.lightGray, --Command screen background color
- colors.red --Background color if not connected to a GPS server
- }
- directionSensitivity = 0.6
- mapCorners = { --Top left corner, bottom right corner
- {
- 2,
- 2,
- },
- {
- scr_x - 1,
- scr_y - 6,
- }
- }
- corner1 = mapCorners[1]
- corner2 = mapCorners[2]
- midPoint = {
- math.floor((corner1[1] + corner2[1]) / 2),
- math.floor((corner1[2] + corner2[2]) / 2)
- }
- mapDimensions = {
- corner2[1] - corner1[1],
- corner2[2] - corner1[2]
- }
- if term.isColor() then
- colormode = true
- else
- colormode = false
- end
- displayStuffs = true
- isConnected = true
- arrows = {
- ">",
- "v",
- "<",
- "^"
- }
- playerChar = youChar
- function getConfig()
- if not fs.exists(waypointFile) then
- file = fs.open(waypointFile, "w")
- file.write({})
- file.close()
- end
- file = fs.open(waypointFile, "r")
- contents = file.readAll()
- contents = textutils.unserialize(contents)
- file.close()
- waypoints = {}
- for a = 1, #contents do
- table.insert(waypoints, contents[a])
- end
- if not fs.exists(mapConfigFile) then
- file = fs.open(mapConfigFile, "w")
- file.writeLine("-" .. "- Do not break the format of this config.")
- file.writeLine("-" .. "- This is for Eldidi's Map. Do 'map cc' to fix broken config.")
- file.writeLine("spoofMode = false -" .. "-boolean, set true if testing program in emulator. Replaces gps movement detection with arrow keys. Default: false")
- file.writeLine("scaleFactor = 0.25 -" .. "-number, the zoom factor for the minimap. Lower means wider range. Does not affect GPS range. Default: 0.25")
- file.writeLine("labelMode = true -" .. "-boolean, true if waypoint labels are their names, false if their distances. Default: true")
- file.writeLine("youChar = 'O' -" .. "-string, the character which defaultly represents you in the map. is overwritten by arrows, so this is useless come to think of it. Default: 'O'")
- file.writeLine("waypointChar = '*' -" .. "-string, the character that waypoints on the map are represented by. Default: '*'")
- file.writeLine("blankChar = ' ' -" .. "-string, the blank character that the inside of the map is made of. Default: ' '")
- file.writeLine("refreshSleep = 0.4 -" .. "-number, the delay between each GPS request. Default: 0.4")
- file.writeLine("autoUpdate = false -" .. "-boolean, whether or not Map automatically updates to the latest version. this is reccommended FALSE because new updates could break the program into a state where it can't update again.")
- file.close()
- end
- shell.run(mapConfigFile)
- return contents
- end
- function setConfig(pointname, mode, x, y, z)
- config = getConfig()
- file = fs.open(waypointFile, "w")
- if mode == "delete" then
- for a = 1, #config do
- point = config[a]
- if point[1] == pointname then
- table.remove(config, a)
- file.writeLine(textutils.serialize(config))
- file.close()
- getConfig()
- return true
- end
- end
- file.close()
- return false
- elseif mode == "add" then
- if x == nil or y == nil or z == nil then
- file.close()
- return false
- end
- table.insert(config, {pointname, x, y, z})
- file.writeLine(textutils.serialize(config))
- file.close()
- end
- getConfig()
- file.close()
- end
- function refreshOtherConfig()
- file = fs.open(mapConfigFile, "r")
- configFile = file.readAll()
- configFile = textutils.unserialize(configFile)
- file.close()
- file = fs.open(mapConfigFile, "w")
- settings = {
- "spoofMode = " .. tostring(spoofMode),
- "scaleFactor = " .. tostring(scaleFactor),
- "labelMode = " .. tostring(labelMode),
- "youChar = '" .. tostring(youChar) .. "'",
- "waypointChar = '" .. tostring(waypointChar) .. "'",
- "blankChar = '" .. tostring(blankChar) .. "'",
- "refreshSleep = " .. tostring(refreshSleep)
- }
- for a = 1, #settings do
- file.writeLine(settings[a])
- end
- file.close()
- getConfig()
- end
- function setDefaultConfig()
- if not fs.exists(waypointFile) then
- waypoints = {
- {
- "Test 1",
- 2,
- 62,
- 6
- },
- {
- "Test 2",
- 5,
- 70,
- 3
- }
- }
- file = fs.open(waypointFile, "w")
- file.writeLine(textutils.serialize(waypoints))
- file.close()
- end
- end
- function round(num, idp)
- local mult=10^(idp or 0)
- return math.floor(num * mult + 0.5 ) / mult
- end
- function setDefaultBackgroundColor()
- if isConnected then
- if colormode then
- term.setBackgroundColor(mapColors[1])
- else
- term.setBackgroundColor(colors.black)
- end
- else
- if colormode then
- term.setBackgroundColor(mapColors[8])
- else
- term.setBackgroundColor(colors.white)
- end
- end
- end
- function setDefaultTextColor()
- if colormode then
- term.setTextColor(mapColors[2])
- else
- if isConnected then
- term.setTextColor(colors.white)
- else
- term.setTextColor(colors.white)term.setTextColor(colors.black)
- end
- end
- end
- function setDefaultColors()
- setDefaultTextColor()
- setDefaultBackgroundColor()
- end
- function getCoordinates()
- if spoofMode then
- isConnected = true
- return {fakeX, fakeY, fakeZ}
- else
- local coord_x, coord_y, coord_z = gps.locate(1)
- if coord_x == nil then
- if displayStuffs then
- term.setCursorPos(2,1)
- if colormode then
- term.setTextColor(colors.lightGray)
- else
- term.setTextColor(colors.white)
- end
- end
- if isConnected then
- if displayStuffs then
- print("GPS not found...searching")
- end
- local coord_x, coord_y, coord_z = gps.locate(3)
- if displayStuffs then
- if coord_x == nil then
- term.clearLine()
- end
- end
- end
- end
- if coord_x == nil then
- isConnected = false
- return {0, 0, 0}
- else
- term.setCursorPos(1,1)
- term.clearLine()
- isConnected = true
- return {coord_x, coord_y, coord_z}
- end
- end
- end
- function clearMap()
- if displayStuffs then
- setDefaultColors()
- local buffX = corner1[1] + 1
- local mapLengthInner = (corner2[1]) - (corner1[1]) - 1
- local longBlankChar1 = string.rep(blankChar, mapLengthInner)
- for y = corner1[2] + 1, corner2[2] - 1 do
- term.setCursorPos(buffX,y)
- write(longBlankChar1)
- end
- local buffX = corner1[1] - 1
- for y = corner1[2], corner2[2] do
- term.setCursorPos(buffX,y)
- write(" ") --I do " " because 'blankChar' is for the inside of the map.
- end
- local buffX = corner2[1] + 1
- for y = corner1[2], corner2[2] do
- term.setCursorPos(buffX,y)
- write(" ") --Same here.
- end
- if isConnected then
- buffY = corner1[2] - 1
- term.setCursorPos(1,buffY)
- term.clearLine()
- end
- end
- return
- end
- function drawBorder()
- --Drawing border. Always is drawn first.
- if displayStuffs and isConnected then
- if colormode then
- term.setTextColor(mapColors[2])
- term.setBackgroundColor(mapColors[2])
- else
- if isConnected then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.white)
- else
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.black)
- end
- end
- local mapLength = corner2[1] - corner1[1]
- local longMapChar = string.rep("#", mapLength)
- term.setCursorPos(corner1[1],corner1[2])
- write(longMapChar)
- term.setCursorPos(corner1[1],corner2[2])
- write(longMapChar)
- for b = corner1[2], corner2[2] do
- term.setCursorPos(corner1[1],b)
- write("#")
- end
- for b = corner1[2], corner2[2] do
- if b ~= midPoint[2] then
- term.setCursorPos(corner2[1],b)
- write("#")
- end
- end
- if colormode then
- term.setTextColor(mapColors[7])
- term.setBackgroundColor(mapColors[2])
- else
- if isConnected then
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- else
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- end
- term.setCursorPos(midPoint[1],corner1[2])
- write("Z")
- term.setCursorPos(corner2[1],midPoint[2])
- write("X")
- end
- return
- end
- function renderMap()
- isConnected = true
- getCoordinates()
- setDefaultColors()
- term.clear()
- drawBorder()
- renderCommands()
- while true do
- if displayStuffs then
- wasConnected = true
- repeat
- poses = getCoordinates()
- if not isConnected then
- wasConnected = false
- if displayStuffs then
- if colormode then
- term.setBackgroundColor(colors.red)
- else
- term.setBackgroundColor(colors.white)
- end
- term.clear()
- renderCommands()
- repeat
- poses = getCoordinates()
- sleep(0)
- until isConnected == true
- end
- end
- until isConnected == true
- if isConnected then
- setDefaultColors()
- if not wasConnected then
- wasConnected = true
- term.clear()
- renderCommands()
- end
- end
- end
- oldCoord_x = posX
- oldCoord_y = posY
- oldCoord_z = posZ
- posX = poses[1]
- posY = poses[2]
- posZ = poses[3]
- if oldCoord_x ~= nil then
- if math.abs(oldCoord_x - posX) > directionSensitivity or math.abs(oldCoord_z - posZ) > directionSensitivity then
- if math.abs(oldCoord_x - posX) > math.abs(oldCoord_z - posZ) and oldCoord_x - posX > 0 then
- direction_long = "west"
- direction = "west"
- elseif math.abs(oldCoord_x - posX) > math.abs(oldCoord_z - posZ) and oldCoord_x - posX < 0 then
- direction_long = "east"
- direction = "east"
- elseif math.abs(oldCoord_z - posZ) > math.abs(oldCoord_x - posX) and oldCoord_z - posZ > 0 then
- direction_lat = "south"
- direction = "south"
- elseif math.abs(oldCoord_z - posZ) > math.abs(oldCoord_x - posX) and oldCoord_z - posZ < 0 then
- direction_lat = "north"
- direction = "north"
- end
- end
- end
- if displayStuffs then
- setDefaultColors()
- if (oldCoord_x ~= posX or oldCoord_z ~= posZ) then
- drawBorder()
- clearMap()
- end
- if hadCleared then
- drawBorder()
- clearMap()
- end
- hadCleared = false
- for a = 1, #waypoints do
- point = waypoints[a]
- wayX = point[2]
- wayY = point[3]
- wayZ = point[4]
- oldItemX = itemX
- oldItemZ = itemZ
- itemX = math.ceil((midPoint[1] + (wayX - posX) * scaleFactor))
- itemZ = math.floor((midPoint[2] + (posZ - wayZ) * scaleFactor))
- term.setCursorPos(itemX,itemZ)
- if itemX > corner1[1] and itemX < corner2[1] and itemZ < corner2[2] and itemZ > corner1[2] then
- if isConnected then
- if colormode then
- term.setTextColor(mapColors[3])
- else
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- write(waypointChar)
- else
- if colormode then
- term.setTextColor(mapColors[8])
- else
- term.setTextColor(colors.black)
- end
- end
- else
- if colormode then
- term.setTextColor(mapColors[4])
- term.setBackgroundColor(mapColors[2])
- else
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- if itemX <= corner1[1] and itemZ <= corner2[2] and itemZ >= corner1[2] then
- term.setCursorPos(corner1[1],itemZ)
- write("<")
- elseif itemX <= corner1[1] and itemZ <= corner1[2] then
- term.setCursorPos(corner1[1],corner1[2])
- if itemX * -1 >= itemZ * -1 then
- write("<")
- else
- write("^")
- end
- elseif itemX <= corner1[1] and itemZ >= corner2[2] then
- term.setCursorPos(corner1[1],corner2[2])
- if itemX * -1 >= itemZ then
- write("<")
- else
- write("v")
- end
- elseif itemX >= corner2[1] and itemZ <= corner2[2] and itemZ >= corner1[2] then
- term.setCursorPos(corner2[1],itemZ)
- write(">")
- elseif itemX >= corner2[1] and itemZ <= corner1[2] then
- term.setCursorPos(corner2[1],corner1[2])
- if itemX >= itemZ * -1 then
- write(">")
- else
- write("^")
- end
- elseif itemX >= corner2[1] and itemZ >= corner2[2] then
- term.setCursorPos(corner2[1],corner2[2])
- if itemX >= itemZ then
- write(">")
- else
- write("v")
- end
- elseif itemX >= corner1[1] and itemX <= corner2[1] and itemZ <= corner1[2] then
- term.setCursorPos(itemX,corner1[2])
- write("^")
- elseif itemX >= corner1[1] and itemX <= corner2[1] and itemZ >= corner2[2] then
- term.setCursorPos(itemX,corner2[2])
- write("v")
- end
- setDefaultColors()
- end
- if colormode then
- term.setTextColor(mapColors[3])
- else
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- if itemZ < corner2[2] + 2 then
- if labelMode == true then
- pointName = point[1]
- pointMidX = math.ceil(itemX - (string.len(pointName) / 2))
- pointMidZ = itemZ - 1
- term.setCursorPos(pointMidX,pointMidZ)
- elseif labelMode == false then
- pointName = tostring(round(math.sqrt((point[2]-posX)^2 + (point[3]-posY)^2 + (point[4]-posZ)^2), 2))
- pointMidX = itemX - math.floor(string.len(pointName) / 2)
- pointMidZ = itemZ - 1
- term.setCursorPos(pointMidX,pointMidZ)
- end
- if pointMidX + string.len(pointName) > scr_x then
- subLength = (scr_x - pointMidX)
- subName = string.sub(pointName, 1, subLength)
- if pointMidX <= scr_x then
- write(subName)
- end
- else
- write(pointName)
- end
- end
- end
- term.setCursorPos(midPoint[1],midPoint[2])
- if colormode then
- term.setTextColor(mapColors[5])
- else
- if isConnected then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- else
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- end
- if direction == "east" then
- playerChar = ">"
- elseif direction == "west" then
- playerChar = "<"
- elseif direction == "south" then
- playerChar = "v"
- elseif direction == "north" then
- playerChar = "^"
- else
- playerChar = "O"
- end
- write(playerChar)
- end
- if displayStuffs then
- itemX = corner1[1] + 1
- itemY = corner2[2] + 1
- term.setCursorPos(itemX,itemY)
- term.clearLine()
- if colormode then
- term.setTextColor(colors.white)
- end
- write("Scale: ")
- if colormode then
- term.setTextColor(colors.orange)
- end
- write(scaleFactor .. "x")
- end
- if not spoofMode then
- sleep(refreshSleep)
- else
- sleep(0)
- end
- end
- end
- function renderCommands()
- commands = {
- "(S)et scale",
- "(L)abel/distance",
- "(C)reate waypoint",
- "(D)elete waypoint",
- "E(x)it",
- "C(o)nf",
- "(H)elp"
- }
- if not isConnected then
- term.setCursorPos(2,1)
- if colormode then
- term.setTextColor(colors.gray)
- else
- term.setTextColor(colors.black)
- end
- gpsNotFoundString = "GPS not found."
- print(gpsNotFoundString .. string.rep(" ", scr_x-string.len(gpsNotFoundString)-2))
- end
- startX = 2
- startY = corner2[2] + 1
- commandX = startX
- commandY = startY + 1
- if displayStuffs then
- term.setCursorPos(startX,startY)
- term.clearLine()
- term.setCursorPos(commandX,commandY)
- if colormode then
- term.setTextColor(mapColors[6])
- term.setBackgroundColor(mapColors[7])
- else
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- term.clearLine()
- for f = 1, #commands do
- comPosX, comPosZ = term.getCursorPos()
- scr_x, scr_y = term.getSize()
- if scr_x - comPosX <= string.len(commands[f]) + 1 then
- startY = startY + 1
- term.setCursorPos(startX,startY)
- term.clearLine()
- end
- write(commands[f])
- if f ~= #commands then
- write(", ")
- end
- end
- end
- return
- end
- function displayHelp()
- term.setCursorPos(1,2)
- if colormode then
- term.setTextColor(colors.orange)
- end
- print(" 'Map' is a GPS minimap program made by EldidiStroyrr (LDDestroier).")
- setDefaultColors()
- print("* 'map cc' clears config.")
- print("* 'map cw' clears waypoints.")
- print("* A red screen means no access to a GPS server.")
- print("* Deleting waypoints deletes the earliest waypoint made of that name.")
- print("* Pressing numpad '+' or '-' will zoom in and out without a prompt.")
- print("* Pressing 'S' will prompt you to change scale.")
- sleep(0.1)
- if colormode then
- term.setTextColor(colors.lightGray)
- end
- print("")
- print("Press a key to continue.")
- sleep(0)
- local event, key = os.pullEvent("key")
- return
- end
- function setWaypoint()
- term.setCursorPos(1,2)
- defaultNewPointName = "way" --Not used, in favor of a way to cancel.
- print("Set waypoint.")
- print("Call it what?")
- write(">")
- newName = tostring(read())
- if newName == "" then
- print("Cancelling.")
- sleep(0.2)
- return
- end
- print("What X? (default: here)")
- write(">")
- newX = tonumber(read())
- if newX == nil then
- newX = math.floor(posX)
- end
- print("What Y? (default: here)")
- write(">")
- newY = tonumber(read())
- if newY == nil then
- newY = math.floor(posY)
- end
- print("What Z? (default: here)")
- write(">")
- newZ = tonumber(read())
- if newZ == nil then
- newZ = math.floor(posZ)
- end
- setConfig(newName, "add", newX, newY, newZ)
- refreshOtherConfig()
- print("Waypoint '" .. newName .. "' set!")
- getConfig()
- sleep(0.2)
- end
- function setScale()
- term.setCursorPos(1,2)
- print("Set scale factor. Currently " .. scaleFactor .. ".")
- repeat
- newScale = nil
- write(">")
- newScaleRaw = read()
- if tostring(newScaleRaw) == "" then
- print("Cancelling.")
- sleep(0.2)
- return
- end
- newScale = tonumber(newScaleRaw)
- if newScale == nil then
- print("That's not a number!")
- end
- until type(newScale) == "number"
- scaleFactor = newScale
- print("Scale factor set to " .. scaleFactor .. ".")
- refreshOtherConfig()
- getConfig()
- sleep(0.2)
- return true
- end
- function deleteWaypoint()
- term.setCursorPos(1,2)
- print("Delete what waypoint?")
- for a = 1, #waypoints do
- point = waypoints[a]
- print(" -'" .. point[1] .. "'")
- end
- write(">")
- delName = tostring(read())
- for a = 1, #waypoints do
- point = waypoints[a]
- if point[1] == delName then
- setConfig(delName, "delete")
- print("Deleted '" .. delName .. "'.")
- refreshOtherConfig()
- getConfig()
- sleep(0.2)
- return true
- end
- end
- print("Invalid waypoint '" .. delName .. "'!")
- sleep(0.2)
- return false
- end
- function keyPress()
- while true do
- displayStuffs = true
- local event, key = os.pullEvent("key")
- key = tostring(keys.getName(key))
- displayStuffs = false
- if key == "c" then
- displayStuffs = false
- setDefaultColors()
- term.clear()
- sleep(0)
- setWaypoint()
- term.clear()
- sleep(0)
- displayStuffs = true
- clearMap()
- drawBorder()
- renderCommands()
- end
- if key == "l" then
- if labelMode == true then
- labelMode = false
- elseif labelMode == false then
- labelMode = true
- end
- refreshOtherConfig()
- clearMap()
- drawBorder()
- end
- if key == "s" then
- displayStuffs = false
- setDefaultColors()
- term.clear()
- sleep(0)
- setScale()
- term.clear()
- sleep(0)
- displayStuffs = true
- clearMap()
- drawBorder()
- renderCommands()
- end
- if key == "d" then
- displayStuffs = false
- setDefaultColors()
- term.clear()
- sleep(0)
- deleteWaypoint()
- term.clear()
- sleep(0)
- displayStuffs = true
- clearMap()
- drawBorder()
- renderCommands()
- end
- if spoofMode then
- if key == "left" then
- fakeX = fakeX - 1
- sleep(0.07)
- end
- if key == "right" then
- fakeX = fakeX + 1
- sleep(0.07)
- end
- if key == "down" then
- fakeZ = fakeZ - 1
- sleep(0.07)
- end
- if key == "up" then
- fakeZ = fakeZ + 1
- sleep(0.07)
- end
- end
- if key == "h" then
- displayStuffs = false
- setDefaultColors()
- term.clear()
- sleep(0)
- displayHelp()
- term.clear()
- sleep(0)
- displayStuffs = true
- clearMap()
- drawBorder()
- renderCommands()
- end
- if key == "o" then
- displayStuffs = false
- setDefaultBackgroundColor()
- term.clear()
- sleep(0)
- shell.run("edit " .. mapConfigFile)
- sleep(0)
- displayStuffs = true
- setDefaultColors()
- getConfig()
- term.clear()
- clearMap()
- drawBorder()
- renderCommands()
- end
- if key == "numPadAdd" or key == "add" then
- scaleFactor = scaleFactor * 1.1
- displayStuffs = true
- refreshOtherConfig()
- clearMap()
- drawBorder()
- elseif key == "minus" or key == "numPadSubtract" then
- scaleFactor = scaleFactor / 1.1
- displayStuffs = true
- refreshOtherConfig()
- clearMap()
- drawBorder()
- end
- if key == "x" then
- sleep(0)
- return
- end
- end
- end
- getConfig()
- fakeX = 3 --Used if spoofMode == true, if not then these are set in the event that it is later set true.
- fakeY = 62
- fakeZ = 5
- if autoUpdate then
- write("Automatically updating Map...")
- updateMapProgram()
- print("up to date!")
- end
- parallel.waitForAny(renderMap, keyPress)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- print("Thanks for using Map!")
- print("(code: G7SmdMKD)")
- sleep(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement