Advertisement
BlissSilence

Untitled

Jan 2nd, 2025 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. if not monitor then
  3. print("Monitor not found!")
  4. return
  5. end
  6.  
  7. monitor.setTextScale(0.5)
  8. monitor.clear()
  9.  
  10. local baseUrl = "http://localhost:8080/tiles/tile.png?dimension=minecraft:overworld&mapTypeString=day&y=0&zoom=0&t="
  11. local refreshInterval = 10 -- Refresh map every 10 seconds
  12. local outputPath = "/map.png"
  13. local centerX, centerZ = 0, 0 -- Base tile coordinates
  14.  
  15. local monitorWidth, monitorHeight = monitor.getSize()
  16.  
  17. local ccPalette = {
  18. {240, 240, 240}, {242, 178, 51}, {229, 127, 216}, {153, 178, 242},
  19. {222, 222, 108}, {127, 204, 25}, {242, 102, 189}, {76, 76, 76},
  20. {153, 153, 153}, {76, 153, 178}, {178, 102, 229}, {51, 102, 204},
  21. {127, 102, 76}, {87, 166, 78}, {204, 76, 76}, {25, 25, 25}
  22. }
  23.  
  24. local function colorDistance(r1, g1, b1, r2, g2, b2)
  25. return math.sqrt((r1 - r2) ^ 2 + (g1 - g2) ^ 2 + (b1 - b2) ^ 2)
  26. end
  27.  
  28. local function findClosestColor(r, g, b)
  29. local closest = 1
  30. local minDist = math.huge
  31. for i, color in ipairs(ccPalette) do
  32. local dist = colorDistance(r, g, b, color[1], color[2], color[3])
  33. if dist < minDist then
  34. minDist = dist
  35. closest = i
  36. end
  37. end
  38. return 2 ^ (closest - 1)
  39. end
  40.  
  41. local function fetchTile()
  42. print("Fetching single map tile...")
  43. local tileUrl = baseUrl .. os.epoch("utc") .. "&x=" .. centerX .. "&z=" .. centerZ
  44. local response = http.get(tileUrl)
  45.  
  46. if response then
  47. local file = fs.open(outputPath, "wb")
  48. file.write(response.readAll())
  49. file.close()
  50. print("Tile downloaded successfully.")
  51. else
  52. print("Failed to fetch tile.")
  53. end
  54. end
  55.  
  56. local function parseAndScalePNG(path)
  57. if not fs.exists(path) then
  58. print("PNG file not found.")
  59. return nil
  60. end
  61.  
  62. local file = fs.open(path, "rb")
  63. local data = file.readAll()
  64. file.close()
  65.  
  66. local pixels = {}
  67. local imgSize = 512 -- Assuming the tile is 512x512
  68. local scaleX, scaleY = imgSize / 16, imgSize / 16 -- Scale to 16x16
  69.  
  70. for y = 1, 16 do
  71. pixels[y] = {}
  72. for x = 1, 16 do
  73. local px = math.floor((x - 1) * scaleX) + 1
  74. local py = math.floor((y - 1) * scaleY) + 1
  75. local index = ((py - 1) * imgSize + px) * 3 -- 3 bytes per pixel (R, G, B)
  76. local r = string.byte(data, index) or 0
  77. local g = string.byte(data, index + 1) or 0
  78. local b = string.byte(data, index + 2) or 0
  79.  
  80. pixels[y][x] = findClosestColor(r, g, b)
  81. end
  82. end
  83.  
  84. return pixels
  85. end
  86.  
  87. local function drawPixels(pixels)
  88. if not pixels then
  89. print("No pixel data to draw.")
  90. return
  91. end
  92.  
  93. for y, row in ipairs(pixels) do
  94. for x, color in ipairs(row) do
  95. monitor.setBackgroundColor(color)
  96. monitor.setCursorPos(x, y)
  97. monitor.write(" ")
  98. end
  99. end
  100. print("Tile drawn to monitor.")
  101. end
  102.  
  103. while true do
  104. fetchTile()
  105. local pixels = parseAndScalePNG(outputPath)
  106. drawPixels(pixels)
  107. sleep(refreshInterval)
  108. end
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement