Advertisement
LDDestroier

GPS Minimap v1.2.4

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