Advertisement
Oeed

Dapper Map Viewer

Jun 24th, 2016
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.83 KB | None | 0 0
  1. local g, b = colours.green, colours.blue
  2. local logo = {{0,0,0,b,g,g,g,g,g,b},{0,b,b,g,g,g,g,g,g,g,b,b},{b,b,b,b,g,g,g,g,g,b,b,b,b},{b,g,b,b,b,g,g,g,b,b,b,b,g},{b,b,b,b,b,g,b,b,b,b,b,b,g},{b,b,b,b,b,b,g,g,b,b,b,b,b},{b,b,b,b,b,g,g,g,g,g,b,b,b},{0,b,b,b,b,b,g,g,g,g,b,b},{0,0,0,b,b,b,b,g,g,b}}
  3.  
  4. local width, height = term.getSize()
  5. term.setBackgroundColour(colours.black)
  6. term.clear()
  7.  
  8. local logoWidth, logoHeight = 13, 9
  9. local logoY = math.ceil((height - logoHeight) / 2) - 2
  10. paintutils.drawImage(logo, math.ceil((width - logoWidth) / 2), logoY)
  11.  
  12. local splashText = "A Rather Dapper Mapper"
  13. term.setCursorPos(1 + math.ceil((width - #splashText) / 2), logoY + logoHeight + 2)
  14. term.setBackgroundColour(colours.black)
  15. term.setTextColor(colours.white)
  16. term.write(splashText)
  17.  
  18. term.setCursorPos(math.ceil((width - 7) / 2), logoY + logoHeight + 3)
  19. term.setTextColor(colours.lightBlue)
  20. term.write("by oeed")
  21. sleep(2)
  22. term.setBackgroundColour(colours.black)
  23. term.clear()
  24.  
  25. local DAPPER_CLIENT_REQUEST_CHANNEL = 4261
  26. local DAPPER_CLIENT_DATA_CHANNEL = 4262
  27. local DAPPER_CLIENT_REQUEST_MAP = "mapdata"
  28.  
  29. local modem = peripheral.find("modem")
  30. if not modem or not modem.isWireless() then
  31.     error("Please connect a wireless modem and re-run the program.", 0)
  32. end
  33.  
  34. modem.open(DAPPER_CLIENT_REQUEST_CHANNEL)
  35. modem.open(DAPPER_CLIENT_DATA_CHANNEL)
  36.  
  37. local bannerText = "Connecting..."
  38. local pixels
  39. local positionX, positionY, positionZ
  40. local mapX, mapZ
  41. local isPositionLocked = true
  42. local direction = "o"
  43. local zoom = 1
  44. local directionColour = colours.white
  45. local needsMapRedraw = true
  46. local function location()
  47.     local x, y, z = gps.locate(2)
  48.     if not x or not y or not z then
  49.         bannerText = "No GPS Signal"
  50.         return positionX, positionY, positionZ
  51.     end
  52.     if bannerText == "No GPS Signal" then
  53.         bannerText = nil
  54.     end
  55.     return math.floor(x + 0.5), math.floor(y + 0.5), math.floor(z + 0.5)
  56. end
  57.  
  58. positionX, positionY, positionZ = location()
  59. mapX = positionX
  60. mapZ = positionZ
  61.  
  62. local buffer = {}
  63. for x = 1, width do
  64.     buffer[x] = {}
  65. end
  66. local function drawMap()
  67.     if bannerText then
  68.         term.setCursorPos(1 + math.ceil((width - #bannerText) / 2), 1)
  69.         term.setBackgroundColour(colours.white)
  70.         term.clearLine()
  71.         term.setTextColour(colours.red)
  72.         term.write(bannerText)
  73.         for x = 1, width do
  74.             buffer[x][1] = nil
  75.         end
  76.     end
  77.     if pixels then
  78.         local xPixel = math.floor(mapX - (zoom * width) / 2)
  79.         local zPixelStart = math.floor(mapZ - (zoom * height) / 2)
  80.         local directionX, directionY = math.floor((positionX - xPixel) / zoom + 0.5), math.floor((positionZ - zPixelStart) / zoom + 0.5)
  81.         for x = 1, width do
  82.             local xPixels = pixels[xPixel]
  83.             local zPixel = zPixelStart
  84.             local xBuffer = buffer[x]
  85.             for y = 1, height do
  86.                 if not bannerText or y > 1 then
  87.                     local pixel = xPixels and xPixels[zPixel]
  88.                     local character, textColour, backgroundColour = " ", colours.black, colours.black
  89.                     if pixel then
  90.                         character = pixel[1]
  91.                         textColour = pixel[2]
  92.                         backgroundColour = pixel[3]
  93.                     end
  94.                     if x == directionX and y == directionY then
  95.                         textColour = directionColour
  96.                         character = direction
  97.                     end
  98.                     local bufferPixel = xBuffer[y]
  99.                     if not bufferPixel or bufferPixel[1] ~= character or bufferPixel[2] ~= textColour or bufferPixel[2] ~= backgroundColour then
  100.                         xBuffer[y] = {character, textColour, backgroundColour}
  101.                         term.setCursorPos(x, y)
  102.                         term.setTextColour(textColour)
  103.                         term.setBackgroundColour(backgroundColour)
  104.                         term.write(character)
  105.                     end
  106.                 end
  107.                 zPixel = zPixel + zoom
  108.             end
  109.             xPixel = xPixel + zoom
  110.         end
  111.     end
  112.     needsMapRedraw = false
  113. end
  114.  
  115. modem.transmit(DAPPER_CLIENT_REQUEST_CHANNEL, DAPPER_CLIENT_DATA_CHANNEL, DAPPER_CLIENT_REQUEST_MAP)
  116. local mapTimeoutTime = os.clock() + 2
  117. local redrawTime = os.clock() + 0.05
  118. local updatePositionTime = os.clock() + 1
  119. local mapUpdateTime = os.clock() + 30
  120. local directionFlashTime = os.clock() + 0.5
  121. local updateTimer = os.startTimer(0.05)
  122. local dragX, dragY
  123. local didDrag = false
  124. while true do
  125.     local eventDetails = {os.pullEvent()}
  126.     local event = eventDetails[1]
  127.     if event == "timer" then
  128.         local timer = eventDetails[2]
  129.         if timer == updateTimer then
  130.             local clock = os.clock()
  131.             if mapTimeoutTime and mapTimeoutTime <= clock then
  132.                 bannerText = "No Map Server Signal"
  133.             end
  134.             if updatePositionTime <= clock then
  135.                 local oldPositionX, oldPositionZ = positionX, positionZ
  136.                 positionX, positionY, positionZ = location()
  137.                 updatePositionTime = os.clock() + 1
  138.                 if positionX ~= oldPositionX or positionZ ~= oldPositionZ then
  139.                     local xDiff, zDiff = positionX - oldPositionX, positionZ - oldPositionZ
  140.                     if math.abs(zDiff) >= math.abs(xDiff) then
  141.                         if zDiff < 0 then
  142.                             direction = "^"
  143.                         else
  144.                             direction = "v"
  145.                         end
  146.                     else
  147.                         if xDiff < 0 then
  148.                             direction = "<"
  149.                         else
  150.                             direction = ">"
  151.                         end
  152.                     end
  153.                     needsMapRedraw = true
  154.                 end
  155.                 if isPositionLocked then
  156.                     mapX, mapZ = positionX, positionZ
  157.                 end
  158.             end
  159.             if directionFlashTime <= clock then
  160.                 directionColour = (directionColour == colours.grey) and colours.white or colours.grey
  161.                 directionFlashTime = os.clock() + 0.5
  162.                 needsMapRedraw = true
  163.             end
  164.             if redrawTime <= clock then
  165.                 if needsMapRedraw then
  166.                     drawMap()
  167.                 end
  168.                 redrawTime = os.clock() + 0.05
  169.             end
  170.             if mapUpdateTime <= clock then
  171.                 modem.transmit(DAPPER_CLIENT_REQUEST_CHANNEL, DAPPER_CLIENT_DATA_CHANNEL, DAPPER_CLIENT_REQUEST_MAP)
  172.                 mapTimeoutTime = os.clock() + 2
  173.                 mapUpdateTime = os.clock() + 30
  174.             end
  175.             updateTimer = os.startTimer(0.05)
  176.         end
  177.     elseif event == "modem_message" then
  178.         local event, side, senderChannel, replyChannel, message, distance = unpack(eventDetails)
  179.         if senderChannel == DAPPER_CLIENT_DATA_CHANNEL then
  180.             needsMapRedraw = true
  181.             pixels = message
  182.             mapUpdateTime = os.clock() + 50
  183.             mapTimeoutTime = false
  184.             if bannerText == "No Map Server Signal" or bannerText == "Connecting..." then
  185.                 bannerText = nil
  186.             end
  187.         end
  188.     elseif event == "key" then
  189.         local key = eventDetails[2]
  190.         if key == keys.minus then
  191.             zoom = math.min(zoom + 1, 10)
  192.             needsMapRedraw = true
  193.         elseif key == keys.equals then
  194.             zoom = math.max(zoom - 1, 1)
  195.             needsMapRedraw = true
  196.         elseif key == keys.space then
  197.             isPositionLocked = true
  198.             mapX, mapZ = positionX, positionZ
  199.             needsMapRedraw = true
  200.         end
  201.     elseif event == "mouse_click" then
  202.         dragX, dragY = eventDetails[3], eventDetails[4]
  203.         didDrag = false
  204.     elseif event == "mouse_drag" then
  205.         local x, y = eventDetails[3], eventDetails[4]
  206.         isPositionLocked = false
  207.         mapX = mapX + (dragX - x) * zoom
  208.         mapZ = mapZ + (dragY - y) * zoom
  209.         dragX, dragY = x, y
  210.         needsMapRedraw = true
  211.         didDrag = true
  212.     elseif event == "mouse_up" and not didDrag then
  213.         dragX, dragY = eventDetails[3], eventDetails[4]
  214.     end
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement