Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.find("monitor")
- if not monitor then
- print("Monitor not found!")
- return
- end
- monitor.setTextScale(0.5)
- monitor.clear()
- local baseUrl = "http://localhost:8080/tiles/tile.png?dimension=minecraft:overworld&mapTypeString=day&y=0&zoom=0&t="
- local refreshInterval = 10 -- Refresh map every 10 seconds
- local outputPath = "/map.png"
- local centerX, centerZ = 0, 0 -- Base tile coordinates
- local monitorWidth, monitorHeight = monitor.getSize()
- local ccPalette = {
- {240, 240, 240}, {242, 178, 51}, {229, 127, 216}, {153, 178, 242},
- {222, 222, 108}, {127, 204, 25}, {242, 102, 189}, {76, 76, 76},
- {153, 153, 153}, {76, 153, 178}, {178, 102, 229}, {51, 102, 204},
- {127, 102, 76}, {87, 166, 78}, {204, 76, 76}, {25, 25, 25}
- }
- local function colorDistance(r1, g1, b1, r2, g2, b2)
- return math.sqrt((r1 - r2) ^ 2 + (g1 - g2) ^ 2 + (b1 - b2) ^ 2)
- end
- local function findClosestColor(r, g, b)
- local closest = 1
- local minDist = math.huge
- for i, color in ipairs(ccPalette) do
- local dist = colorDistance(r, g, b, color[1], color[2], color[3])
- if dist < minDist then
- minDist = dist
- closest = i
- end
- end
- return 2 ^ (closest - 1)
- end
- local function fetchTile()
- print("Fetching single map tile...")
- local tileUrl = baseUrl .. os.epoch("utc") .. "&x=" .. centerX .. "&z=" .. centerZ
- local response = http.get(tileUrl)
- if response then
- local file = fs.open(outputPath, "wb")
- file.write(response.readAll())
- file.close()
- print("Tile downloaded successfully.")
- else
- print("Failed to fetch tile.")
- end
- end
- local function parseAndScalePNG(path)
- if not fs.exists(path) then
- print("PNG file not found.")
- return nil
- end
- local file = fs.open(path, "rb")
- local data = file.readAll()
- file.close()
- local pixels = {}
- local imgSize = 512 -- Assuming the tile is 512x512
- local scaleX, scaleY = imgSize / 16, imgSize / 16 -- Scale to 16x16
- for y = 1, 16 do
- pixels[y] = {}
- for x = 1, 16 do
- local px = math.floor((x - 1) * scaleX) + 1
- local py = math.floor((y - 1) * scaleY) + 1
- local index = ((py - 1) * imgSize + px) * 3 -- 3 bytes per pixel (R, G, B)
- local r = string.byte(data, index) or 0
- local g = string.byte(data, index + 1) or 0
- local b = string.byte(data, index + 2) or 0
- pixels[y][x] = findClosestColor(r, g, b)
- end
- end
- return pixels
- end
- local function drawPixels(pixels)
- if not pixels then
- print("No pixel data to draw.")
- return
- end
- for y, row in ipairs(pixels) do
- for x, color in ipairs(row) do
- monitor.setBackgroundColor(color)
- monitor.setCursorPos(x, y)
- monitor.write(" ")
- end
- end
- print("Tile drawn to monitor.")
- end
- while true do
- fetchTile()
- local pixels = parseAndScalePNG(outputPath)
- drawPixels(pixels)
- sleep(refreshInterval)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement