Advertisement
LDDestroier

GPS Minimap 1.3

Jun 4th, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- 'Map' by EldidiStroyrr (LDDestroier)
  2. -- This program can be copied, and it can be modified, but it
  3. -- can NOT be copied, modified slightly, and then claimed as your own.
  4. -- You must give me, EldidiStroyrr, credit for the creation and
  5. -- only reference yourself as some guy who changed a bit.
  6.  
  7. parentDir = "/" .. fs.getDir(shell.getRunningProgram())
  8. waypointFile = "/" .. fs.combine(parentDir, ".waypoints")
  9. mapConfigFile = "/" .. fs.combine(parentDir, "/.map_conf")
  10.  
  11. local mapVersion = "1.3" --Version of Map. Must be string.
  12. if string.sub(shell.dir(), 1, 4) == "rom/" or shell.dir() == "rom" then
  13.     waypointFile = ("/.waypoints")
  14.     mapConfigFile = ("/.map_conf")
  15. end
  16.  
  17. function updateMapProgram()
  18.     local mapName = shell.getRunningProgram()
  19.     local updateFile = http.get("http://pastebin.com/raw.php?i=x9ajKSc0").readAll()
  20.     local file = fs.open(mapName, "w")
  21.     file.write(updateFile)
  22.     file.close()
  23.     return fs.getSize(mapName)
  24. end
  25.  
  26. scr_x, scr_y = term.getSize()
  27. local tArg = {...}
  28. if tArg[1] ~= nil then
  29.     if tArg[1] == "cc" then
  30.         if fs.exists(mapConfigFile) then
  31.             fs.delete(mapConfigFile)
  32.             print("Config cleared.")
  33.         else
  34.             print("No config to clear.")
  35.         end
  36.         return
  37.     elseif tArg[1] == "cw" then
  38.         if fs.exists(waypointFile) then
  39.             fs.delete(waypointFile)
  40.             print("Waypoints cleared.")
  41.         else
  42.             print("No waypoints to clear.")
  43.         end
  44.         return
  45.     elseif tArg[1] == "update" then
  46.         write("Updating " .. shell.getRunningProgram() .. "...")
  47.         local mapName = shell.getRunningProgram()
  48.         local fileSize = updateMapProgram()
  49.         print("done (got " .. fs.getSize(mapName) .. " bytes)")
  50.         return true
  51.     end
  52.     if tArg[1] ~= "mon" then
  53.         term.redirect(term.native())
  54.     end
  55. end
  56. mapColors = {
  57.     colors.gray, --Background color
  58.     colors.white, --Border color
  59.     colors.yellow, --Waypoint color
  60.     colors.red, --Off-screen waypoint text color
  61.     colors.lightBlue, --Player color
  62.     colors.black, --Command screen text color
  63.     colors.lightGray, --Command screen background color
  64.     colors.red --Background color if not connected to a GPS server
  65. }
  66.  
  67. directionSensitivity = 0.6
  68. mapCorners = { --Top left corner, bottom right corner
  69.     {
  70.         2,
  71.         2,
  72.     },
  73.     {
  74.         scr_x - 1,
  75.         scr_y - 6,
  76.     }
  77. }
  78. corner1 = mapCorners[1]
  79. corner2 = mapCorners[2]
  80. midPoint = {
  81.     math.floor((corner1[1] + corner2[1]) / 2),
  82.     math.floor((corner1[2] + corner2[2]) / 2)
  83. }
  84. mapDimensions = {
  85.     corner2[1] - corner1[1],
  86.     corner2[2] - corner1[2]
  87. }
  88.  
  89. if term.isColor() then
  90.     colormode = true
  91. else
  92.     colormode = false
  93. end
  94. displayStuffs = true
  95. isConnected = true
  96. arrows = {
  97.     ">",
  98.     "v",
  99.     "<",
  100.     "^"
  101. }
  102.  
  103. playerChar = youChar
  104.  
  105. function getConfig()
  106.     if not fs.exists(waypointFile) then
  107.         file = fs.open(waypointFile, "w")
  108.         file.write({})
  109.         file.close()
  110.     end
  111.     file = fs.open(waypointFile, "r")
  112.     contents = file.readAll()
  113.     contents = textutils.unserialize(contents)
  114.     file.close()
  115.     waypoints = {}
  116.     for a = 1, #contents do
  117.         table.insert(waypoints, contents[a])
  118.     end
  119.    
  120.     if not fs.exists(mapConfigFile) then
  121.         file = fs.open(mapConfigFile, "w")
  122.         file.writeLine("-" .. "- Do not break the format of this config.")
  123.         file.writeLine("-" .. "- This is for Eldidi's Map. Do 'map cc' to fix broken config.")
  124.         file.writeLine("spoofMode = false -" .. "-boolean, set true if testing program in emulator. Replaces gps movement detection with arrow keys. Default: false")
  125.         file.writeLine("scaleFactor = 0.25 -" .. "-number, the zoom factor for the minimap. Lower means wider range. Does not affect GPS range. Default: 0.25")
  126.         file.writeLine("labelMode = true -" .. "-boolean, true if waypoint labels are their names, false if their distances. Default: true")
  127.         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'")
  128.         file.writeLine("waypointChar = '*' -" .. "-string, the character that waypoints on the map are represented by. Default: '*'")
  129.         file.writeLine("blankChar = ' ' -" .. "-string, the blank character that the inside of the map is made of. Default: ' '")
  130.         file.writeLine("refreshSleep = 0.4 -" .. "-number, the delay between each GPS request. Default: 0.4")
  131.         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.")
  132.         file.close()
  133.     end
  134.     shell.run(mapConfigFile)   
  135.     return contents
  136. end
  137.  
  138. function setConfig(pointname, mode, x, y, z)
  139.     config = getConfig()
  140.     file = fs.open(waypointFile, "w")
  141.     if mode == "delete" then
  142.         for a = 1, #config do
  143.             point = config[a]
  144.             if point[1] == pointname then
  145.                 table.remove(config, a)
  146.                 file.writeLine(textutils.serialize(config))
  147.                 file.close()
  148.                 getConfig()
  149.                 return true
  150.             end
  151.         end
  152.         file.close()
  153.         return false
  154.     elseif mode == "add" then
  155.         if x == nil or y == nil or z == nil then
  156.             file.close()
  157.             return false
  158.         end
  159.         table.insert(config, {pointname, x, y, z})
  160.         file.writeLine(textutils.serialize(config))
  161.         file.close()
  162.     elseif mode == "rename" then
  163.         for a = 1, #config do
  164.             point = config[a]
  165.             if point[1] == pointname then
  166.                 table.remove(point, 1)
  167.                 table.insert(point, 1, x)
  168.                 file.writeLine(textutils.serialize(config))
  169.                 file.close()
  170.                 getConfig()
  171.                 return true
  172.             end
  173.         end
  174.         file.close()
  175.         return false
  176.     end
  177.     getConfig()
  178.     file.close()
  179. end
  180.  
  181. function refreshOtherConfig()
  182.     file = fs.open(mapConfigFile, "r")
  183.     configFile = file.readAll()
  184.     configFile = textutils.unserialize(configFile)
  185.     file.close()
  186.    
  187.     file = fs.open(mapConfigFile, "w")
  188.     settings = {
  189.         "spoofMode = " .. tostring(spoofMode),
  190.         "scaleFactor = " .. tostring(scaleFactor),
  191.         "labelMode = " .. tostring(labelMode),
  192.         "youChar = '" .. tostring(youChar) .. "'",
  193.         "waypointChar = '" .. tostring(waypointChar) .. "'",
  194.         "blankChar = '" .. tostring(blankChar) .. "'",
  195.         "refreshSleep = " .. tostring(refreshSleep),
  196.         "autoUpdate = " .. tostring(autoUpdate),
  197.     }
  198.     for a = 1, #settings do
  199.         file.writeLine(settings[a])
  200.     end
  201.     file.close()
  202.     getConfig()
  203. end
  204.  
  205. function setDefaultConfig()
  206.     if not fs.exists(waypointFile) then
  207.         waypoints = {
  208.             {
  209.                 "Test 1",
  210.                 2,
  211.                 62,
  212.                 6
  213.             },
  214.             {
  215.                 "Test 2",
  216.                 5,
  217.                 70,
  218.                 3
  219.             }
  220.         }
  221.         file = fs.open(waypointFile, "w")
  222.         file.writeLine(textutils.serialize(waypoints))
  223.         file.close()
  224.     end
  225. end
  226.  
  227. function round(num, idp)
  228.     local mult=10^(idp or 0)
  229.     return math.floor(num * mult + 0.5 ) / mult
  230. end
  231.  
  232. function setDefaultBackgroundColor()
  233.     if isConnected then
  234.         if colormode then
  235.             term.setBackgroundColor(mapColors[1])
  236.         else
  237.             term.setBackgroundColor(colors.black)
  238.         end
  239.     else
  240.         if colormode then
  241.             term.setBackgroundColor(mapColors[8])
  242.         else
  243.             term.setBackgroundColor(colors.white)
  244.         end
  245.     end
  246. end
  247.  
  248. function setDefaultTextColor()
  249.     if colormode then
  250.         term.setTextColor(mapColors[2])
  251.     else
  252.         if isConnected then
  253.             term.setTextColor(colors.white)
  254.         else
  255.             term.setTextColor(colors.black)
  256.         end
  257.     end
  258. end
  259.  
  260. function setDefaultColors()
  261.     setDefaultTextColor()
  262.     setDefaultBackgroundColor()
  263. end
  264.  
  265. function getCoordinates()
  266.     if spoofMode then
  267.         isConnected = true
  268.         return {fakeX, fakeY, fakeZ}
  269.     else
  270.         local coord_x, coord_y, coord_z = gps.locate(1)
  271.         if coord_x == nil then
  272.             if displayStuffs then
  273.                 term.setCursorPos(2,1)
  274.                 if colormode then
  275.                     term.setTextColor(colors.lightGray)
  276.                 else
  277.                     term.setTextColor(colors.white)
  278.                 end
  279.             end
  280.             if isConnected then
  281.                 if displayStuffs then
  282.                     print("GPS not found...searching")
  283.                 end
  284.                 local coord_x, coord_y, coord_z = gps.locate(3)
  285.                 if displayStuffs then
  286.                     if coord_x == nil then
  287.                         term.clearLine()
  288.                     end
  289.                 end
  290.             end
  291.         end
  292.         if coord_x == nil then
  293.             isConnected = false
  294.             return {0, 0, 0}
  295.         else
  296.             term.setCursorPos(1,1)
  297.             term.clearLine()
  298.             isConnected = true
  299.             return {coord_x, coord_y, coord_z}
  300.         end
  301.     end
  302. end
  303.  
  304. function clearMap()
  305.     if displayStuffs then
  306.         setDefaultColors()
  307.         local buffX = corner1[1] + 1
  308.         local mapLengthInner = (corner2[1]) - (corner1[1]) - 1
  309.         local longBlankChar1 = string.rep(blankChar, mapLengthInner)
  310.         for y = corner1[2] + 1, corner2[2] - 1 do
  311.             term.setCursorPos(buffX,y)
  312.             write(longBlankChar1)
  313.         end
  314.         local buffX = corner1[1] - 1
  315.         for y = corner1[2], corner2[2] do
  316.             term.setCursorPos(buffX,y)
  317.             write(" ") --I do " " because 'blankChar' is for the inside of the map.
  318.         end
  319.         local buffX = corner2[1] + 1
  320.         for y = corner1[2], corner2[2] do
  321.             term.setCursorPos(buffX,y)
  322.             write(" ") --Same here.
  323.         end
  324.         if isConnected then
  325.             buffY = corner1[2] - 1
  326.             term.setCursorPos(1,buffY)
  327.             term.clearLine()
  328.         end
  329.     end
  330.     return
  331. end
  332.  
  333. function drawBorder()
  334.     --Drawing border. Always is drawn first.
  335.     if displayStuffs and isConnected then
  336.         if colormode then
  337.             term.setTextColor(mapColors[2])
  338.             term.setBackgroundColor(mapColors[2])
  339.         else
  340.             if isConnected then
  341.                 term.setTextColor(colors.white)
  342.                 term.setBackgroundColor(colors.white)
  343.             else
  344.                 term.setTextColor(colors.black)
  345.                 term.setBackgroundColor(colors.black)
  346.             end
  347.         end
  348.         local mapLength = corner2[1] - corner1[1]
  349.         local longMapChar = string.rep("#", mapLength)
  350.         term.setCursorPos(corner1[1],corner1[2])
  351.         write(longMapChar)
  352.         term.setCursorPos(corner1[1],corner2[2])
  353.         write(longMapChar)
  354.         for b = corner1[2], corner2[2] do
  355.             term.setCursorPos(corner1[1],b)
  356.             write("#")
  357.         end
  358.         for b = corner1[2], corner2[2] do
  359.             if b ~= midPoint[2] then
  360.                 term.setCursorPos(corner2[1],b)
  361.                 write("#")
  362.             end
  363.         end
  364.         if colormode then
  365.             term.setTextColor(mapColors[7])
  366.             term.setBackgroundColor(mapColors[2])
  367.         else
  368.             if isConnected then
  369.                 term.setTextColor(colors.black)
  370.                 term.setBackgroundColor(colors.white)
  371.             else
  372.                 term.setTextColor(colors.white)
  373.                 term.setBackgroundColor(colors.black)
  374.             end
  375.         end
  376.         term.setCursorPos(midPoint[1],corner1[2])
  377.         write("Z")
  378.         term.setCursorPos(corner2[1],midPoint[2])
  379.         write("X")
  380.         setDefaultColors()
  381.     end
  382.     return
  383. end
  384.  
  385. function drawScaleIndicator()
  386.     if displayStuffs then
  387.         itemX = corner1[1] + 1
  388.         itemY = corner2[2] + 1
  389.         term.setCursorPos(itemX,itemY)
  390.         setDefaultColors()
  391.         term.clearLine()
  392.         if colormode then
  393.             term.setTextColor(colors.white)
  394.         end
  395.         write("Scale: ")
  396.         if colormode then
  397.             term.setTextColor(colors.orange)
  398.         end
  399.         write(scaleFactor .. "x")
  400.     end
  401. end
  402.  
  403. function renderMap()
  404.     isConnected = true
  405.     getCoordinates()
  406.     setDefaultColors()
  407.     term.clear()
  408.     drawBorder()
  409.     renderCommands()
  410.     while true do
  411.         if displayStuffs then
  412.             wasConnected = true
  413.             repeat
  414.                 poses = getCoordinates()
  415.                 if not isConnected then
  416.                     wasConnected = false
  417.                     if displayStuffs then
  418.                         if colormode then
  419.                             term.setBackgroundColor(colors.red)
  420.                         else
  421.                             term.setBackgroundColor(colors.white)
  422.                         end
  423.                         term.clear()
  424.                         renderCommands()
  425.                         repeat
  426.                             poses = getCoordinates()
  427.                             sleep(0)
  428.                         until isConnected == true
  429.                     end
  430.                 end
  431.             until isConnected == true
  432.             if isConnected then
  433.                 setDefaultColors()
  434.                 if not wasConnected then
  435.                     wasConnected = true
  436.                     term.clear()
  437.                     renderCommands()
  438.                     drawBorder()
  439.                 end
  440.             end
  441.         end
  442.         oldCoord_x = posX
  443.         oldCoord_y = posY
  444.         oldCoord_z = posZ
  445.         posX = poses[1]
  446.         posY = poses[2]
  447.         posZ = poses[3]
  448.         if oldCoord_x ~= nil then
  449.             if math.abs(oldCoord_x - posX) > directionSensitivity or math.abs(oldCoord_z - posZ) > directionSensitivity then
  450.                 if math.abs(oldCoord_x - posX) > math.abs(oldCoord_z - posZ) and oldCoord_x - posX > 0 then
  451.                     direction_long = "west"
  452.                     direction = "west"
  453.                 elseif math.abs(oldCoord_x - posX) > math.abs(oldCoord_z - posZ) and oldCoord_x - posX < 0 then
  454.                     direction_long = "east"
  455.                     direction = "east"
  456.                 elseif math.abs(oldCoord_z - posZ) > math.abs(oldCoord_x - posX) and oldCoord_z - posZ > 0 then
  457.                     direction_lat = "south"
  458.                     direction = "south"
  459.                 elseif math.abs(oldCoord_z - posZ) > math.abs(oldCoord_x - posX) and oldCoord_z - posZ < 0 then
  460.                     direction_lat = "north"
  461.                     direction = "north"
  462.                 end
  463.             end
  464.         end
  465.         if displayStuffs then
  466.             setDefaultColors()
  467.             if (oldCoord_x ~= posX or oldCoord_z ~= posZ) then
  468.                 drawBorder()
  469.                 clearMap()
  470.             end
  471.             if hadCleared then
  472.                 drawBorder()
  473.                 clearMap()
  474.             end
  475.             hadCleared = false
  476.             for a = 1, #waypoints do
  477.                 point = waypoints[a]
  478.                 wayX = point[2]
  479.                 wayY = point[3]
  480.                 wayZ = point[4]
  481.                
  482.                 oldItemX = itemX
  483.                 oldItemZ = itemZ
  484.                
  485.                 itemX = math.ceil((midPoint[1] + (wayX - posX) * scaleFactor))
  486.                 itemZ = math.floor((midPoint[2] + (posZ - wayZ) * scaleFactor))
  487.                
  488.                 term.setCursorPos(itemX,itemZ)
  489.                 if itemX > corner1[1] and itemX < corner2[1] and itemZ < corner2[2] and itemZ > corner1[2] then
  490.                     if not (itemX == midPoint[1] and itemZ == midPoint[2]) then
  491.                         if isConnected then
  492.                             if colormode then
  493.                                 term.setTextColor(mapColors[3])
  494.                             else
  495.                                 term.setTextColor(colors.white)
  496.                                 term.setBackgroundColor(colors.black)
  497.                             end
  498.                             write(waypointChar)
  499.                             else
  500.                             if colormode then
  501.                                 term.setTextColor(mapColors[8])
  502.                             else
  503.                                 term.setTextColor(colors.black)
  504.                             end
  505.                         end
  506.                     end
  507.                 else
  508.                     if colormode then
  509.                         term.setTextColor(mapColors[4])
  510.                         term.setBackgroundColor(mapColors[2])
  511.                     else
  512.                         term.setTextColor(colors.white)
  513.                         term.setBackgroundColor(colors.black)
  514.                     end
  515.                     if itemX <= corner1[1] and itemZ <= corner2[2] and itemZ >= corner1[2] then
  516.                         term.setCursorPos(corner1[1],itemZ)
  517.                         write("<")
  518.                     elseif itemX <= corner1[1] and itemZ <= corner1[2] then
  519.                         term.setCursorPos(corner1[1],corner1[2])
  520.                         if itemX * -1 >= itemZ * -1 then
  521.                             write("<")
  522.                         else
  523.                             write("^")
  524.                         end
  525.                     elseif itemX <= corner1[1] and itemZ >= corner2[2] then
  526.                         term.setCursorPos(corner1[1],corner2[2])
  527.                         if itemX * -1 >= itemZ then
  528.                             write("<")
  529.                         else
  530.                             write("v")
  531.                         end
  532.                     elseif itemX >= corner2[1] and itemZ <= corner2[2] and itemZ >= corner1[2] then
  533.                         term.setCursorPos(corner2[1],itemZ)
  534.                         write(">")
  535.                     elseif itemX >= corner2[1] and itemZ <= corner1[2] then
  536.                         term.setCursorPos(corner2[1],corner1[2])
  537.                         if itemX >= itemZ * -1 then
  538.                             write(">")
  539.                         else
  540.                             write("^")
  541.                         end
  542.                     elseif itemX >= corner2[1] and itemZ >= corner2[2] then
  543.                         term.setCursorPos(corner2[1],corner2[2])
  544.                         if itemX >= itemZ then
  545.                             write(">")
  546.                         else
  547.                             write("v")
  548.                         end
  549.                     elseif itemX >= corner1[1] and itemX <= corner2[1] and itemZ <= corner1[2] then
  550.                         term.setCursorPos(itemX,corner1[2])
  551.                         write("^")
  552.                     elseif itemX >= corner1[1] and itemX <= corner2[1] and itemZ >= corner2[2] then
  553.                         term.setCursorPos(itemX,corner2[2])
  554.                         write("v")
  555.                     end
  556.                     setDefaultColors()
  557.                 end
  558.                 if colormode then
  559.                     term.setTextColor(mapColors[3])
  560.                 else
  561.                     term.setTextColor(colors.white)
  562.                     term.setBackgroundColor(colors.black)
  563.                 end
  564.                 if itemZ < corner2[2] + 2 then
  565.                     if labelMode == true then
  566.                         pointName = point[1]
  567.                         pointMidX = math.ceil(itemX - (string.len(pointName) / 2))
  568.                         pointMidZ = itemZ - 1
  569.                         term.setCursorPos(pointMidX,pointMidZ)
  570.                     elseif labelMode == false then
  571.                         pointName = tostring(round(math.sqrt((point[2]-posX)^2 + (point[3]-posY)^2 + (point[4]-posZ)^2), 2))
  572.                         pointMidX = itemX - math.floor(string.len(pointName) / 2)
  573.                         pointMidZ = itemZ - 1
  574.                         term.setCursorPos(pointMidX,pointMidZ)
  575.                     end
  576.                     if pointMidX + string.len(pointName) >= scr_x then
  577.                         subLength = (scr_x - pointMidX) + 1
  578.                         subName = string.sub(pointName, 1, subLength)
  579.                         if pointMidX <= scr_x + 1 then
  580.                             pointName = subName
  581.                         end
  582.                     end
  583.                     if pointMidX <= scr_x then
  584.                         for i = 1, #pointName do
  585.                             labelPosX, labelPosY = term.getCursorPos()
  586.                             if ((labelPosY == corner1[2] or labelPosY == corner2[2]) or (labelPosX == corner1[1] or labelPosX == corner2[1])) and ((labelPosX >= corner1[1] and labelPosX <= corner2[1]) and (labelPosY >= corner1[2] and labelPosY <= corner2[2])) then
  587.                                 if colormode then
  588.                                     term.setTextColor(mapColors[1])
  589.                                     term.setBackgroundColor(mapColors[2])
  590.                                 else
  591.                                     term.setTextColor(colors.white)
  592.                                     term.setBackgroundColor(colors.black)
  593.                                 end
  594.                             else
  595.                                 if colormode then
  596.                                     term.setTextColor(mapColors[3])
  597.                                     term.setBackgroundColor(mapColors[1])
  598.                                 else
  599.                                     term.setTextColor(colors.white)
  600.                                     term.setBackgroundColor(colors.black)
  601.                                 end
  602.                             end
  603.                             write(string.sub(pointName, i, i))
  604.                         end
  605.                         setDefaultColors()
  606.                     end
  607.                 end
  608.             end
  609.             term.setCursorPos(midPoint[1],midPoint[2])         
  610.             if colormode then
  611.                 term.setTextColor(mapColors[5])
  612.             else
  613.                 if isConnected then
  614.                     term.setTextColor(colors.white)
  615.                     term.setBackgroundColor(colors.black)
  616.                 else
  617.                     term.setTextColor(colors.white)
  618.                     term.setBackgroundColor(colors.black)
  619.                 end
  620.             end
  621.             if direction == "east" then
  622.                 playerChar = ">"
  623.             elseif direction == "west" then
  624.                 playerChar = "<"
  625.             elseif direction == "south" then
  626.                 playerChar = "v"
  627.             elseif direction == "north" then
  628.                 playerChar = "^"
  629.             else
  630.                 playerChar = "O"
  631.             end
  632.             write(playerChar)
  633.         end
  634.         if not spoofMode then
  635.             sleep(refreshSleep)
  636.         else
  637.             sleep(0)
  638.         end
  639.     end
  640. end
  641.  
  642. function renderCommands()
  643.     commands = {
  644.         "(L)abel/distance",
  645.         "(M)ore Options",
  646.         "(H)elp",
  647.         "E(x)it",
  648.     }
  649.     if not isConnected then
  650.         term.setCursorPos(2,1)
  651.         if colormode then
  652.             term.setTextColor(colors.gray)
  653.         else
  654.             term.setTextColor(colors.black)
  655.         end
  656.         gpsNotFoundString = "GPS not found."
  657.         print(gpsNotFoundString .. string.rep(" ", scr_x-string.len(gpsNotFoundString)-2))
  658.     end
  659.     startX = 2
  660.     startY = corner2[2] + 1
  661.     commandX = startX
  662.     commandY = startY + 1
  663.     if displayStuffs then
  664.         term.setCursorPos(startX,startY)
  665.         term.clearLine()
  666.         term.setCursorPos(commandX,commandY)
  667.         if colormode then
  668.             term.setTextColor(mapColors[6])
  669.             term.setBackgroundColor(mapColors[7])
  670.         else
  671.             term.setTextColor(colors.white)
  672.             term.setBackgroundColor(colors.black)
  673.         end
  674.         term.clearLine()
  675.         for f = 1, #commands do
  676.             comPosX, comPosY = term.getCursorPos()
  677.             scr_x, scr_y = term.getSize()
  678.             if scr_x - comPosX <= string.len(commands[f]) + 1 then
  679.                 term.setCursorPos(startX,comPosY+1)
  680.                 term.clearLine()
  681.             end
  682.             write(commands[f])
  683.             if f ~= #commands then
  684.                 write(", ")
  685.             end
  686.         end
  687.         drawScaleIndicator()
  688.     end
  689.     return
  690. end
  691.  
  692. function displayHelp()
  693.     term.setCursorPos(1,2)
  694.     if colormode then
  695.         term.setTextColor(colors.orange)
  696.     end
  697.     print(" 'Map v1.3' is a GPS minimap program made by EldidiStroyrr (LDDestroier).")
  698.     setDefaultColors()
  699.     print(" * 'map cc' clears config.")
  700.     print(" * 'map cw' clears waypoints.")
  701.     print(" * A red screen means no access to a GPS server.")
  702.     print(" * Pressing numpad '+' or '-' will zoom in and out without a prompt.")
  703.     print(" * Do 'map update' to update Map.")
  704.     sleep(0.1)
  705.     if colormode then
  706.         term.setTextColor(colors.lightGray)
  707.     end
  708.     print("")
  709.     print("Press a key to continue.")
  710.     sleep(0)
  711.     local event, key = os.pullEvent("key")
  712.     return
  713. end
  714. function setWaypoint()
  715.     term.setCursorPos(1,2)
  716.     defaultNewPointName = "way" --Not used, in favor of a way to cancel.
  717.     print("Set waypoint.")
  718.     print("Call it what?")
  719.     write(">")
  720.     newName = tostring(read())
  721.     if newName == "" then
  722.         print("Cancelling.")
  723.         sleep(0.2)
  724.         return
  725.     end
  726.     print("What X? (default: here)")
  727.     write(">")
  728.     newX = tonumber(read())
  729.     if newX == nil then
  730.         newX = math.floor(posX)
  731.     end
  732.     print("What Y? (default: here)")
  733.     write(">")
  734.     newY = tonumber(read())
  735.     if newY == nil then
  736.         newY = math.floor(posY)
  737.     end
  738.     print("What Z? (default: here)")
  739.     write(">")
  740.     newZ = tonumber(read())
  741.     if newZ == nil then
  742.         newZ = math.floor(posZ)
  743.     end
  744.     setConfig(newName, "add", newX, newY, newZ)
  745.     refreshOtherConfig()
  746.     print("Waypoint '" .. newName .. "' set!")
  747.     getConfig()
  748.     sleep(0.2)
  749. end
  750.  
  751. function setScale()
  752.     term.setCursorPos(1,2)
  753.     print("Set scale factor. Currently " .. scaleFactor .. ".")
  754.     repeat
  755.         newScale = nil
  756.         write(">")
  757.         newScaleRaw = read()
  758.         if tostring(newScaleRaw) == "" then
  759.             print("Cancelling.")
  760.             sleep(0.2)
  761.             return
  762.         end
  763.         newScale = tonumber(newScaleRaw)
  764.         if newScale == nil then
  765.             print("That's not a number!")
  766.         end
  767.     until type(newScale) == "number"
  768.     scaleFactor = newScale
  769.     print("Scale factor set to " .. scaleFactor .. ".")
  770.     refreshOtherConfig()
  771.     getConfig()
  772.     sleep(0.2)
  773.     return true
  774. end
  775.  
  776. function deleteWaypoint()
  777.     term.setCursorPos(1,2)
  778.     print("Delete what waypoint?")
  779.     if colormode then
  780.         term.setTextColor(colors.orange)
  781.     end
  782.     for a = 1, #waypoints do
  783.         point = waypoints[a]
  784.         print(" -'" .. point[1] .. "'")
  785.     end
  786.     if colormode then
  787.         term.setTextColor(colors.white)
  788.     end
  789.     write(">")
  790.     delName = tostring(read())
  791.     for a = 1, #waypoints do
  792.         point = waypoints[a]
  793.         if point[1] == delName then
  794.             setConfig(delName, "delete")
  795.             print("Deleted '" .. delName .. "'.")
  796.             refreshOtherConfig()
  797.             getConfig()
  798.             sleep(0.2)
  799.             return true
  800.         end
  801.     end
  802.     print("Invalid waypoint '" .. delName .. "'!")
  803.     sleep(0.2)
  804.     return false
  805. end
  806.  
  807. function renameWaypoint()
  808.     term.setCursorPos(1,2)
  809.     print("Rename which waypoint?")
  810.     if colormode then
  811.         term.setTextColor(colors.orange)
  812.     end
  813.     for a = 1, #waypoints do
  814.         point = waypoints[a]
  815.         print(" -'" .. point[1] .. "'")
  816.     end
  817.     if colormode then
  818.         term.setTextColor(colors.white)
  819.     end
  820.     write(">")
  821.     renName = tostring(read())
  822.     print("Rename to what?")
  823.     write(">")
  824.     renOutput = tostring(read())
  825.     for a = 1, #waypoints do
  826.         point = waypoints[a]
  827.         if point[1] == renName then
  828.             setConfig(renName, "rename", renOutput)
  829.             print("Renamed '" .. renName .. "' to '" .. renOutput .. "'.")
  830.             refreshOtherConfig()
  831.             getConfig()
  832.             sleep(0.2)
  833.             return true
  834.         end
  835.     end
  836.     print("Invalid waypoint '" .. renName .. "'!")
  837.     sleep(0.2)
  838.     return false
  839. end
  840.  
  841. function redrawSceen()
  842. --  setDefaultColors()
  843. --  term.clear()
  844.     drawBorder()
  845. --  clearMap()
  846. --  renderCommands()
  847. end
  848.  
  849. function waypointSettings()
  850.     repeat
  851.         sleep(0)
  852.         setDefaultColors()
  853.         term.setCursorPos(1,2)
  854.         print(" Map Options:")
  855.         term.setCursorPos(1,3)
  856.         for a = 1, scr_x do
  857.             write("-")
  858.         end
  859.         term.setCursorPos(1,5)
  860.         if colormode then
  861.             term.setTextColor(colors.orange)
  862.         end
  863.         print(" * (C)reate Waypoint")
  864.         print(" * (D)elete Waypoint")
  865.         print(" * (R)ename Waypoint")
  866.         print(" * Edit C(o)nfig")
  867.         print(" * (S)et Scale")
  868.         print(" * E(x)it")
  869.         event, key = os.pullEvent("key")
  870.         key = keys.getName(key)
  871.     until string.find("cdrosx", key)
  872.     if key == "d" then
  873.         displayStuffs = false
  874.         setDefaultColors()
  875.         term.clear()
  876.         sleep(0)
  877.         deleteWaypoint()
  878.         term.clear()
  879.         sleep(0)
  880.         displayStuffs = true
  881.         redrawSceen()
  882.         return
  883.     end
  884.     if key == "r" then
  885.         displayStuffs = false
  886.         setDefaultColors()
  887.         term.clear()
  888.         sleep(0)
  889.         renameWaypoint()
  890.         term.clear()
  891.         sleep(0)
  892.         displayStuffs = true
  893.         redrawSceen()
  894.         return
  895.     end
  896.     if key == "s" then
  897.         displayStuffs = false
  898.         setDefaultColors()
  899.         term.clear()
  900.         sleep(0)
  901.         setScale()
  902.         term.clear()
  903.         sleep(0)
  904.         displayStuffs = true
  905.         redrawSceen()
  906.         drawScaleIndicator()
  907.         return
  908.     end
  909.     if key == "o" then
  910.         displayStuffs = false
  911.         setDefaultBackgroundColor()
  912.         term.clear()
  913.         sleep(0)
  914.         shell.run("edit " .. mapConfigFile)
  915.         sleep(0)
  916.         displayStuffs = true
  917.         getConfig()
  918.         setDefaultColors()
  919.         term.clear()
  920.         redrawSceen()
  921.         return
  922.     end
  923.     if key == "c" then
  924.         displayStuffs = false
  925.         setDefaultColors()
  926.         term.clear()
  927.         sleep(0)
  928.         setWaypoint()
  929.         term.clear()
  930.         sleep(0)
  931.         displayStuffs = true
  932.         redrawSceen()
  933.     end
  934.     if key == "x" then
  935.         sleep(0)
  936.         return
  937.     end
  938.     print("Cancelling.")
  939.     sleep(0.1)
  940.     return
  941. end
  942.  
  943. function keyPress()
  944.     displayStuffs = true
  945.     while true do
  946.         local event, key = os.pullEvent("key")
  947.         key = tostring(keys.getName(key))
  948.         if key == "l" then
  949.             if labelMode == true then
  950.                 labelMode = false
  951.             elseif labelMode == false then
  952.                 labelMode = true
  953.             end
  954.             refreshOtherConfig()
  955.             drawBorder()
  956.             clearMap()
  957.         end
  958.         if spoofMode then
  959.             if key == "left" then
  960.                 fakeX = fakeX - 1
  961.                 sleep(0.07)
  962.             end
  963.             if key == "right" then
  964.                 fakeX = fakeX + 1
  965.                 sleep(0.07)
  966.             end
  967.             if key == "down" then
  968.                 fakeZ = fakeZ - 1
  969.                 sleep(0.07)
  970.             end
  971.             if key == "up" then
  972.                 fakeZ = fakeZ + 1
  973.                 sleep(0.07)
  974.             end
  975.         end
  976.         if key == "h" then
  977.             displayStuffs = false
  978.             setDefaultColors()
  979.             term.clear()
  980.             sleep(0)
  981.             displayHelp()
  982.             term.clear()
  983.             sleep(0)
  984.             displayStuffs = true
  985.             clearMap()
  986.             drawBorder()
  987.             renderCommands()
  988.         end
  989.         if key == "m" then
  990.             displayStuffs = false
  991.             setDefaultColors()
  992.             term.clear()
  993.             sleep(0)
  994.             waypointSettings()
  995.             term.clear()
  996.             sleep(0)
  997.             displayStuffs = true
  998.             clearMap()
  999.             drawBorder()
  1000.             renderCommands()
  1001.         end
  1002.         if key == "numPadAdd" or key == "add" then
  1003.             scaleFactor = scaleFactor * 1.1
  1004.             displayStuffs = true
  1005.             refreshOtherConfig()
  1006.             clearMap()
  1007.             drawBorder()
  1008.             drawScaleIndicator()
  1009.         elseif key == "minus" or key == "numPadSubtract" then
  1010.             scaleFactor = scaleFactor / 1.1
  1011.             displayStuffs = true
  1012.             refreshOtherConfig()
  1013.             clearMap()
  1014.             drawBorder()
  1015.             drawScaleIndicator()
  1016.         end
  1017.         if key == "x" then
  1018.             sleep(0)
  1019.             return
  1020.         end
  1021.     end
  1022. end
  1023.  
  1024. getConfig()
  1025.  
  1026. fakeX = 3 --Used if spoofMode == true, if not then these are set in the event that it is later set true.
  1027. fakeY = 62
  1028. fakeZ = 5
  1029.  
  1030. if autoUpdate then
  1031.     write("Automatically updating Map...")
  1032.     updateMapProgram()
  1033.     print("up to date!")
  1034. end
  1035.  
  1036. parallel.waitForAny(renderMap, keyPress)
  1037.  
  1038. term.setTextColor(colors.white)
  1039. term.setBackgroundColor(colors.black)
  1040. term.clear()
  1041. term.setCursorPos(1,1)
  1042. print("Thanks for using Map v" .. mapVersion .. "!")
  1043. print("(code: 4RfTdBFk)")
  1044. sleep(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement