info = { type = "app", title = "Weather", author = "BRRRO", license = "MIT", desc = "Weather and air quality", category = "outdoors", api = 43, } permissions = { "location" } local NOLOC = ({ sk = "Nastav polohu vo webe", cs = "Nastav polohu na webu" })[lang] or "Set the location in the web UI" local T = ({ sk = { clock = "Čas a dátum", clock_h = "Vypni, keď sú v profile aj hodiny", back = "Podklad času", back_h = "Keď sa čas a dátum na krajine zle čítajú", back_o = { "žiadny", "obrys", "dymový pás" }, loading = "NACITAVAM", nowifi = "BEZ WIFI", offline = "OFFLINE", console = "POZRI KONZOLU" }, cs = { clock = "Čas a datum", clock_h = "Vypni, když jsou v profilu i hodiny", back = "Podklad času", back_h = "Když se čas a datum na krajině špatně čtou", back_o = { "žádný", "obrys", "kouřový pás" }, loading = "NACITAM", nowifi = "BEZ WIFI", offline = "OFFLINE", console = "VIZ KONZOLI" } })[lang] or { clock = "Time and date", clock_h = "Turn off when the profile also has a clock", back = "Time backing", back_h = "When the time and date are hard to read over the landscape", back_o = { "none", "outline", "smoke band" }, loading = "LOADING", nowifi = "NO WIFI", offline = "OFFLINE", console = "SEE CONSOLE" } settings = { { key = "clock", type = "bool", default = true, label = T.clock, hint = T.clock_h }, { key = "back", type = "int", default = "none", label = T.back, hint = T.back_h, opts = { keys = { "none", "outline", "band" }, table.unpack(T.back_o) } }, } sources = { wx = { url = "https://api.open-meteo.com", limit = 60, fields = { "current.temperature_2m", "current.weather_code", "current.cloud_cover", "current.wind_speed_10m", "current.wind_direction_10m", "daily.temperature_2m_max", "daily.temperature_2m_min", "daily.precipitation_sum" }, demo = [[{"current":{"temperature_2m":21.4,"weather_code":2,"cloud_cover":35,"wind_speed_10m":14.0,"wind_direction_10m":290}, "daily":{"temperature_2m_max":[24.0],"temperature_2m_min":[12.0],"precipitation_sum":[0.0]}}]] }, air = { url = "https://air-quality-api.open-meteo.com", limit = 60, fields = { "current.pm2_5", "current.european_aqi" }, demo = [[{"current":{"pm2_5":9.2,"european_aqi":38}}]] }, } local W, H = gfx.W, gfx.H local px, rect, blend, blend_rect = gfx.px, gfx.rect, gfx.blend, gfx.blend_rect local blit_rgb = gfx.blit_rgb local text, text_center, text_width = gfx.text, gfx.text_center, gfx.text_width local F_BIG = { font = "9x15b" } local INK_L = { w = 1, align = "left", pad = 0 } local INK_R = { w = 1, align = "right", pad = 0 } local AMBER_LIGHT, AMBER_DARK = {255,214,120}, {40,20,4} local OUT_L = { w = 1, align = "left", pad = 0, outline = AMBER_DARK } local OUT_R = { w = 1, align = "right", pad = 0, outline = AMBER_DARK } local floor, sin, cos, sqrt, fmod, abs, min, max = math.floor, math.sin, math.cos, math.sqrt, math.fmod, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local fmt, char, concat = string.format, string.char, table.concat local PI = math.pi local FPS = gfx.FPS local function lround(v) if v >= 0 then return floor(v + 0.5) else return -floor(-v + 0.5) end end local function clamp(v, lo, hi) if v < lo then return lo elseif v > hi then return hi end return v end local function mix(ar, ag, ab, br, bg, bb, t) return floor(ar + (br - ar) * t), floor(ag + (bg - ag) * t), floor(ab + (bb - ab) * t) end local function bl1(v, c, a) return floor(v + (c - v) * a) end local SKY_H, ARC_TOP, HORIZON = 28, 5, 26 local PANEL_DIM = 0.68 local PANEL_BG = {4,6,16} local NIGHT_MIX = {12,14,34} local SUN_HI, SUN_LO = {255,250,190}, {255,140,20} local MOON, STAR = {235,240,255}, {225,235,255} local OVERCAST_N, OVERCAST_D = {44,48,64}, {158,164,178} local RAIN_C, SNOW_C, FOG_C = {205,235,255}, {250,253,255}, {176,182,196} local TEMP_C = {255,250,232} local MAX_C, MIN_C, PRECIP_C = {255,190,120}, {150,200,255}, {150,205,255} local SKY = { { 0.0, {10,10,48}, {30,30,96} }, { 4.5, {40,30,110}, {120,70,150} }, { 6.5, {60,120,235}, {255,140,90} }, { 9.0, {40,140,255}, {170,225,255} }, { 13.0, {20,110,245}, {140,215,255} }, { 17.0, {45,130,235}, {215,205,220} }, { 19.0, {170,55,110}, {255,110,20} }, { 20.5, {55,25,95}, {215,45,45} }, { 22.0, {10,10,48}, {30,30,96} }, { 24.0, {10,10,48}, {30,30,96} }, } local SEASON = { W = { {126,146,182}, {186,204,230}, {226,236,248}, {24,62,52}, {150,120,90}, 0.00, 1.00 }, S = { {96,142,102}, {126,178,98}, {134,186,100}, {30,84,54}, {168,215,118}, 1.00, 0.00 }, L = { {46,106,70}, {68,142,80}, {78,154,80}, {20,72,44}, {74,166,74}, 1.00, 0.00 }, J = { {122,96,62}, {162,124,66}, {154,120,62}, {34,76,50}, {232,126,42}, 0.85, 0.00 }, } local SEASON_KEYS = { { 15, "W" }, { 105, "S" }, { 196, "L" }, { 288, "J" }, { 380, "W" } } local AUTUMN_TINTS = { {232,126,42}, {214,74,52}, {242,186,66}, {198,104,40} } local TREES = { {3,"c",10}, {11,"d",5}, {19,"c",7} } local FLOWER_COL = { {255,245,235}, {255,225,90}, {245,170,205} } local FLOWER = { {7,1}, {15,2}, {23,0}, {30,2}, {35,1}, {40,0}, {52,2}, {59,1}, {62,0}, {46,1} } local RIPPLE = { {-11,-2,6}, {2,-1,8}, {-5,1,5}, {6,2,7} } local FG_TREE = { {55,12}, {61,9}, {48,7} } local CUM = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 } local WX_CLEAR, WX_CLOUD, WX_FOG, WX_RAIN, WX_SNOW, WX_THUNDER = 0, 1, 2, 3, 4, 5 local function wx_kind(code) if code == 45 or code == 48 then return WX_FOG end if (code >= 71 and code <= 77) or code == 85 or code == 86 then return WX_SNOW end if code >= 95 then return WX_THUNDER end if (code >= 51 and code <= 67) or (code >= 80 and code <= 82) then return WX_RAIN end if code == 2 or code == 3 then return WX_CLOUD end return WX_CLEAR end local function yday_of(t) local leap = (t.year % 4 == 0 and t.year % 100 ~= 0) or t.year % 400 == 0 local d = CUM[t.month] + t.day if leap and t.month > 2 then d = d + 1 end return d end local season_cache = { yday = -1, pal = nil } local function season_raw(yday1) local y = (yday1 >= 15) and yday1 or (yday1 + 365) for i = 1, 4 do local a, b = SEASON_KEYS[i], SEASON_KEYS[i + 1] if y >= a[1] and y <= b[1] then local f = (y - a[1]) / (b[1] - a[1]) local A, B = SEASON[a[2]], SEASON[b[2]] local o = {} for k = 1, 5 do local r, g, bb = mix(A[k][1], A[k][2], A[k][3], B[k][1], B[k][2], B[k][3], f) o[k] = { r, g, bb } end o[6] = A[6] + (B[6] - A[6]) * f o[7] = A[7] + (B[7] - A[7]) * f return o end end return SEASON.W end local function season_at(yday1) if season_cache.yday ~= yday1 then season_cache.pal, season_cache.yday = season_raw(yday1), yday1 end return season_cache.pal end local function sky_colors(h) for i = 1, 9 do local a, b = SKY[i], SKY[i + 1] if h >= a[1] and h <= b[1] then local f = (h - a[1]) / (b[1] - a[1]) local tr, tg, tb = mix(a[2][1], a[2][2], a[2][3], b[2][1], b[2][2], b[2][3], f) local br, bg, bb = mix(a[3][1], a[3][2], a[3][3], b[3][1], b[3][2], b[3][3], f) return tr, tg, tb, br, bg, bb end end return SKY[1][2][1], SKY[1][2][2], SKY[1][2][3], SKY[1][3][1], SKY[1][3][2], SKY[1][3][3] end local function pal_hour(h, s) if not s then return h end if s.polar then return s.up and 13 or 0 end if h < s.rise then return h / s.rise * 6.5 elseif h < s.set then return 6.5 + (h - s.rise) / (s.set - s.rise) * 12.5 end return 19 + (h - s.set) / (24 - s.set) * 5 end local function darkness(br, bg, bb) return clamp((120.0 - (0.3 * br + 0.6 * bg + 0.1 * bb)) / 90.0, 0.0, 1.0) end local function sun_arc(h, s) if not s then return 0.5, true end local rise, set = s.rise, s.set if s.up then return (h - rise) / (set - rise), true end local night_len = 24.0 - (set - rise) if night_len < 0.01 then return 0.5, false end if h < rise then return (h + 24.0 - set) / night_len, false end return (h - set) / night_len, false end local HILL = { [0] = {}, [1] = {}, [2] = {} } for x = 0, W - 1 do HILL[0][x] = SKY_H - 13 + lround(3.0 * sin(x * 0.10)) HILL[1][x] = SKY_H - 8 + lround(2.0 * sin(x * 0.17 + 1.6)) HILL[2][x] = SKY_H - 4 + lround(1.5 * sin(x * 0.23 + 3.0)) end local function hill_y(x, layer) return HILL[layer][x] end local SCENE, scene_key = nil, nil local function draw_tree(t, pal, snow, dark, autumn, idx, sway) local tx, kind, size = t[1], t[2], t[3] local gy = hill_y(tx, 1) local kr, kg, kb = mix(62, 48, 38, NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark) if kind == "c" then local cr, cg, cb = mix(pal[4][1], pal[4][2], pal[4][3], NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark) for r = 0, size - 1 do local w = 1 + floor(r * 0.62) local x = tx + lround(sway * (1 - r / size)) rect(x - w, gy - size + r, 2 * w + 1, 1, cr, cg, cb) if snow > 0.4 and r % 3 == 0 then blend(x - w, gy - size + r, 255, 255, 255, snow * 0.8) blend(x + w, gy - size + r, 255, 255, 255, snow * 0.8) end end rect(tx, gy, 1, 2, kr, kg, kb) else for k = 0, size - 1 do px(tx, gy - k, kr, kg, kb) end tx = tx + lround(sway * 0.7) if pal[6] < 0.25 then local br_ = { {-1,0}, {1,0}, {-2,1}, {2,1}, {0,-1} } for i = 1, 5 do px(tx + br_[i][1], gy - size + br_[i][2], kr, kg, kb) end else local r = 2 + floor(size * 0.5) for yy = -r, r do for xx = -r, r do local d = xx * xx + yy * yy * 1.4 local hp = hash32((t[1] + xx) * 73 + (yy + 9) * 151 + idx) if d <= r * r and (d < (r - 1) * (r - 1) or umod(hp, 3) ~= 0) then local c = autumn and AUTUMN_TINTS[umod(hp >> 3, 4) + 1] or pal[5] local k = 0.72 + 0.28 * umod(hp >> 6, 3) / 2 - (yy < 0 and 0.12 or 0) local cr, cg, cb = mix(floor(c[1] * k), floor(c[2] * k), floor(c[3] * k), NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark) px(tx + xx, gy - size - yy - 1, cr, cg, cb) end end end end end end local function draw_house(x, baseY, snow, dark, night) local wr, wg, wb = mix(238, 222, 194, NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark * 0.7) local rr, rg, rb = mix(208, 108, 76, NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark * 0.7) local dr, dg, db = mix(110, 78, 54, NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark * 0.5) local ir, ig, ib if night > 0.35 then ir, ig, ib = 255, 225, 130 else ir, ig, ib = mix(150, 185, 210, NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark * 0.5) end local wallW, wallH = 11, 5 local top = baseY - wallH + 1 rect(x + 1, top, wallW - 2, wallH, wr, wg, wb) for i = 0, 3 do local k = 3 - i rect(x + k, top - 4 + i, wallW - 2 * k, 1, rr, rg, rb) if snow > 0.4 then blend(x + k, top - 4 + i, 255, 255, 255, snow * 0.85) blend(x + wallW - 1 - k, top - 4 + i, 255, 255, 255, snow * 0.85) end end rect(x + 2, top + 1, 3, 2, ir, ig, ib) rect(x + 7, top + 1, 2, wallH - 1, dr, dg, db) end local TREE_ST = {} local function draw_landscape(yday1, night, cl, snow_wx, frozen, windy, sbr, sbg, sbb, bottom) local pal = season_at(yday1) local snow = pal[7] if snow_wx and snow < 0.75 then snow = 0.75 end local dark = 0.45 * night + 0.18 * cl local autumn = (yday1 >= 250 and yday1 <= 330) for layer = 0, 1 do local s = layer == 1 and pal[2] or pal[1] local cr, cg, cb = mix(s[1], s[2], s[3], NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark) for x = 0, W - 1 do local y0 = hill_y(x, layer) if SKY_H > y0 then rect(x, y0, 1, SKY_H - y0, cr, cg, cb) end if snow > 0.3 then blend(x, y0, 255, 255, 255, snow * (layer == 1 and 0.9 or 0.6)) if snow > 0.7 then blend(x, y0 + 1, 255, 255, 255, 0.45) end end end end local gr, gg, gb = mix(pal[3][1], pal[3][2], pal[3][3], NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark) for x = 0, W - 1 do local y0 = hill_y(x, 2) if bottom > y0 then rect(x, y0, 1, bottom - y0, gr, gg, gb) end end local m1r, m1g, m1b = mix(pal[3][1], pal[3][2], pal[3][3], 255, 255, 255, 0.22) local tr, tg, tb = mix(m1r, m1g, m1b, NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark) if snow < 0.5 then for x = 0, W - 1, 3 do px(x, hill_y(x, 2), tr, tg, tb) end local span = bottom - SKY_H - 3 if span < 1 then span = 1 end for i = 0, 25 do local ty = SKY_H + 2 + (i * 5) % span if ty < bottom then px((i * 7 + 2) % W, ty, tr, tg, tb) end end end if pal[6] > 0.5 and not autumn then for i = 1, 10 do local f = FLOWER[i] local fy = hill_y(f[1], 2) + 1 local c = FLOWER_COL[f[2] + 1] if fy < bottom then blend(f[1], fy, c[1], c[2], c[3], 0.85 * pal[6] * (1.0 - dark * 0.6)) end end local span = bottom - SKY_H - 4 if span < 1 then span = 1 end for i = 0, 11 do local fy = SKY_H + 3 + (i * 7) % span local c = FLOWER_COL[i % 3 + 1] if fy < bottom then blend((i * 11 + 4) % W, fy, c[1], c[2], c[3], 0.7 * pal[6]) end end end local PX, PY, PRX, PRY = 44, SKY_H - 1, 20, 5 local w1r, w1g, w1b = mix(sbr, sbg, sbb, 30, 70, 130, 0.45) local wr, wg, wb = mix(w1r, w1g, w1b, NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark * 0.8) if snow > 0.5 then wr, wg, wb = mix(wr, wg, wb, 208, 228, 244, snow * 0.85) elseif frozen then wr, wg, wb = mix(wr, wg, wb, 196, 222, 240, 0.7 * (1.0 - dark * 0.5)) end for yy = -PRY, PRY do local y = PY + yy if y >= 0 and y < bottom then local x0, x1 = nil, nil for xx = -PRX, PRX do if (xx * xx) / (PRX * PRX) + (yy * yy) / (PRY * PRY) <= 1.0 then if not x0 then x0 = xx end x1 = xx end end if x0 then rect(PX + x0, y, x1 - x0 + 1, 1, wr, wg, wb) end end end local rmr, rmg, rmb if snow > 0.5 then rmr, rmg, rmb = 245, 250, 255 else rmr, rmg, rmb = mix(wr, wg, wb, 255, 255, 255, 0.45) end if PY - PRY >= 0 and PY - PRY < bottom then for xx = -PRX + 3, PRX - 3 do blend(PX + xx, PY - PRY, rmr, rmg, rmb, 0.7) end end for i = 1, frozen and 0 or 4 do local r = RIPPLE[i] if PY + r[2] >= 0 and PY + r[2] < bottom then for k = 0, r[3] - 1 do blend(PX + r[1] + k, PY + r[2], rmr, rmg, rmb, 0.35) end end end if bottom > SKY_H then local f1r, f1g, f1b = mix(pal[4][1], pal[4][2], pal[4][3], 255, 255, 255, 0.42) local fr, fg, fb = mix(f1r, f1g, f1b, NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark * 0.7) local kr, kg, kb = mix(92, 68, 50, NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark * 0.5) for i = 1, 3 do local t = FG_TREE[i] for r = 0, t[2] - 1 do local w = 1 + floor(r * 0.55) rect(t[1] - w, bottom - 2 - t[2] + r, 2 * w + 1, 1, fr, fg, fb) if snow > 0.4 and r % 3 == 0 then blend(t[1] - w, bottom - 2 - t[2] + r, 255, 255, 255, snow * 0.7) blend(t[1] + w, bottom - 2 - t[2] + r, 255, 255, 255, snow * 0.7) end end rect(t[1], bottom - 2, 1, 2, kr, kg, kb) end draw_house(2, bottom - 2, snow, dark, night) end TREE_ST.pal, TREE_ST.snow, TREE_ST.dark, TREE_ST.autumn = pal, snow, dark, autumn if not windy then for i = 1, 3 do draw_tree(TREES[i], pal, snow, dark, autumn, i - 1, 0) end end if autumn then local span = bottom - SKY_H + 2 if span < 1 then span = 1 end for i = 0, 25 do local lx = (i * 19 + 3) % W local ly = hill_y(lx, 2) + 1 + (i * 3) % span local c = AUTUMN_TINTS[i % 4 + 1] if ly < bottom then blend(lx, ly, c[1], c[2], c[3], 0.75) end end end end local BOAT = { "..#..", "..##.", "..###", "+++++", ".+++." } local BIRD_UP = { "#...#", ".###." } local BIRD_FLAT = { "##.##", "..#.." } local function draw_boat(t, yday1, kind, frozen) if kind == WX_SNOW or season_at(yday1)[7] > 0.5 then return end local bx, by = 30, SKY_H - 5 if not frozen then bx = 29 + lround(1 + sin(fmod(t * 0.7, 6.28318))) by = by + floor(t / 0.73) % 2 end for r = 1, #BOAT do local row = BOAT[r] for c = 1, 5 do local ch = row:sub(c, c) if ch == "#" then px(bx + c - 1, by + r - 1, 248, 248, 252) elseif ch == "+" then px(bx + c - 1, by + r - 1, 190, 70, 60) end end end end local FLY_WIN = 60 local BALLOON = { "..aba..", ".ababa.", "abababa", "abababa", ".ababa.", "..aba..", "...a...", "..r.r..", "..ccc.." } local BAL_C = { a = { 230, 64, 60 }, b = { 255, 204, 70 }, r = { 90, 70, 60 }, c = { 150, 100, 60 } } local PLANE_C = { 236, 238, 244 } local TRAIL = 14 local function dot(x, y, c) px(x, y, c[1], c[2], c[3]) end local function win_at(len) local t = data.time return t.unix // len, t.unix % len + t.ms / 1000 end local function draw_flyer(t, night, kind, vx, ws, back) local win, fw = win_at(FLY_WIN) local h = hash32(win * 0x9E3779B1 + 91) if umod(h, 3) == 0 or kind == WX_THUNDER then return end local calm = night < 0.55 and kind ~= WX_RAIN and kind ~= WX_SNOW and kind ~= WX_FOG local pick = umod(hash32(h + 1), 3) if (pick == 0 and not calm) or (pick == 1 and not (calm and ws < 20)) then pick = 2 end if back == (pick == 0) then return end local right = vx >= 0 if pick == 2 then right = umod(h >> 8, 2) == 0 end local dir = right and 1 or -1 local v, len = 8, W + 18 if pick == 1 then v, len = max(2, min(3, abs(vx))), W + 8 elseif pick == 2 then v, len = 10, W + 6 + TRAIL end local dur = len / v local f = fw - umod(h >> 4, max(1, floor(FLY_WIN - dur))) if f < 0 or f > dur then return end local p = f * v if pick == 0 then local cr, cg, cb = mix(40, 44, 60, 130, 140, 165, night) for i = 0, 1 do local x = (right and -8 + p or W + 3 - p) - i * 6 * dir local y = 6 + i * 3 + lround(1.5 * sin(fmod(t * 3 + i, 6.28318))) local g = ((floor(t * 7.5) + i) % 2 == 1) and BIRD_UP or BIRD_FLAT for r = 1, 2 do local row = g[r] for c = 1, 5 do if row:sub(c, c) == "#" then px(floor(x) + c - 1, y + r - 1, cr, cg, cb) end end end end elseif pick == 1 then local x = floor(right and -7 + p or W - p) local by = 2 + umod(h >> 10, 4) + floor(sin(t * 0.8) + 0.5) for r = 1, #BALLOON do local row = BALLOON[r] for c = 1, 7 do local col = BAL_C[row:sub(c, c)] if col then dot(x + c - 1, by + r - 1, col) end end end else local x = floor(right and -6 + p or W + 5 - p) local y = 3 + umod(h >> 10, 5) if night < 0.55 then for i = 1, TRAIL do blend(x - i * dir, y + 1, 255, 255, 255, 0.45 * (1 - i / (TRAIL + 1))) end for i = 0, 4 do dot(x - i * dir, y + 1, PLANE_C) end dot(x - 4 * dir, y, PLANE_C) else dot(x, y, { 255, 60, 50 }) if floor(t * FPS) % FPS < 3 then dot(x - 2 * dir, y, { 255, 255, 255 }) end end end end local function draw_ice_glints(t, night) for i = 0, 2 do local s = t / 1.7 + i * 0.37 local n = floor(s) local k = (s - n) * 1.7 / 0.5 if k < 1 then local h = hash32(n * 0x9E3779B1 + i * 977) local yy = umod(h, 2) - 1 local half = floor(20 * sqrt(1 - (yy * yy) / 25.0)) - 2 local x = 44 - half + umod(h >> 4, 2 * half + 1) local a = (1 - abs(2 * k - 1)) * (1 - 0.4 * night) blend(x, SKY_H - 1 + yy, 255, 255, 255, a) if a > 0.6 then blend(x - 1, SKY_H - 1 + yy, 255, 255, 255, a * 0.4); blend(x + 1, SKY_H - 1 + yy, 255, 255, 255, a * 0.4) blend(x, SKY_H - 2 + yy, 255, 255, 255, a * 0.4) end end end end local CLOUD_N = 7 local CLOUDS = {} for k = 1, CLOUD_N do local h = hash32(k * 0x9E3779B1 + 77) local w, ht = 9 + umod(h, 6), 4 + umod(h >> 4, 2) local bumps = {} for b = 1, 2 + umod(h >> 7, 2) do local hb = hash32(h + b * 0x85EBCA6B) bumps[b] = { 2 + umod(hb, 1000) / 1000 * (w - 4), ht - 2, 1.6 + umod(hb >> 10, 1000) / 1000 * 1.2 } end local cell = {} for y = 0, ht - 1 do for x = 0, w - 1 do local inside = y >= ht - 2 and x >= 1 and x <= w - 2 for _, b in ipairs(bumps) do if (x + 0.5 - b[1]) ^ 2 + (y + 0.5 - b[2]) ^ 2 <= b[3] * b[3] then inside = true end end cell[y * w + x + 1] = inside and (y == ht - 1 and 3 or 1) or 0 end end for i = 1, w * ht do if cell[i] == 1 and i > w and cell[i - w] ~= 0 then cell[i] = 2 end end CLOUDS[k] = { w = w, h = ht, cell = cell, y = 1 + umod(h >> 12, 7), x0 = umod(h >> 16, 64), v = 0.75 + umod(h >> 20, 50) / 100 } end local cloud_key local function draw_clouds(tday, vx, n, r, g, b) local key = floor(r / 4) * 4096 + floor(g / 4) * 64 + floor(b / 4) if key ~= cloud_key then local col = { char(0, 0, 0) } for i = 1, 3 do local k = ({ 1, 0.86, 0.7 })[i] col[i + 1] = char(max(1, floor(r * k)), max(1, floor(g * k)), max(1, floor(b * k))) end for _, c in ipairs(CLOUDS) do local out = {} for i = 1, #c.cell do out[i] = col[c.cell[i] + 1] end c.ras = concat(out) end cloud_key = key end for k = 1, n do local c = CLOUDS[k] blit_rgb(c.ras, floor((c.x0 + tday * vx * c.v) % (W + c.w)) - c.w, c.y, c.w, c.h, 0, 0, 0) end end local SOCK_X = 58 local SOCK_C = { {255,110,40}, {240,240,240} } local function draw_windsock(t, vx, ws, dark) local gy = hill_y(SOCK_X, 1) local top = gy - 8 local pr, pg, pb = mix(120, 110, 100, NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark) rect(SOCK_X, top, 1, gy - top + 1, pr, pg, pb) local dir = vx < 0 and -1 or 1 local a = clamp(ws / 30, 0, 1) * PI / 2 for i = 1, 5 do local fl = (ws > 8) and 0.7 * i / 5 * sin(t * (4 + ws / 6) - i * 0.9) or 0 local x = SOCK_X + dir * max(1, lround(i * sin(a))) local y = top + lround(i * cos(a) + fl) local c = SOCK_C[(i - 1) % 2 + 1] local r, g, b = mix(c[1], c[2], c[3], NIGHT_MIX[1], NIGHT_MIX[2], NIGHT_MIX[3], dark * 0.8) px(x, y, r, g, b) if i <= 2 then px(x, y + 1, r, g, b) end end end local BAND = { {80,200,120}, {180,215,80}, {245,200,60}, {245,150,50}, {235,90,90}, {185,90,195} } local AQI_LIM = { 20, 40, 60, 80, 100 } local PM_LIM = { 10, 20, 25, 50, 75 } local function band_color(v, lim) for i = 1, 5 do if v < lim[i] then return BAND[i] end end return BAND[6] end local RING = { {1,0}, {2,0}, {3,0}, {0,1}, {4,1}, {0,2}, {4,2}, {0,3}, {4,3}, {1,4}, {2,4}, {3,4} } local DROP = { {1,0}, {1,1}, {0,2}, {2,2}, {0,3}, {2,3}, {1,4} } local FLAKE = { {2,0}, {2,1}, {0,2}, {1,2}, {2,2}, {3,2}, {4,2}, {2,3}, {2,4}, {0,0}, {4,0}, {0,4}, {4,4} } local function stamp(tab, x, y, c) for i = 1, #tab do px(x + tab[i][1], y + tab[i][2], c[1], c[2], c[3]) end end local function draw_arrow(x, y, up, c) if up then px(x + 2, y, c[1], c[2], c[3]); rect(x + 1, y + 1, 3, 1, c[1], c[2], c[3]); rect(x, y + 2, 5, 1, c[1], c[2], c[3]) else rect(x, y, 5, 1, c[1], c[2], c[3]); rect(x + 1, y + 1, 3, 1, c[1], c[2], c[3]); px(x + 2, y + 2, c[1], c[2], c[3]) end end local function draw_scene(tr, tg, tb, br, bg, bb, night, cl, is_day, elev, bx, by, yday1, kind, frozen, windy) for y = 0, SKY_H - 1 do local r, g, b = mix(tr, tg, tb, br, bg, bb, (y / (SKY_H - 1)) ^ 0.85) rect(0, y, W, 1, r, g, b) end if night > 0.15 and cl < 0.7 then local vis = 1.0 - cl / 0.7 local band = SKY_H - 11 if band < 4 then band = 4 end for i = 0, 17 do local sx, sy = (i * 23 + 5) % W, (i * 11 + 1) % band local a = night * (0.30 + 0.70 * ((i * 7) % 5) / 4.0) * vis blend(sx, sy, STAR[1], STAR[2], STAR[3], a) if i % 5 == 0 and a > 0.6 then blend(sx - 1, sy, STAR[1], STAR[2], STAR[3], a * 0.35) blend(sx + 1, sy, STAR[1], STAR[2], STAR[3], a * 0.35) blend(sx, sy - 1, STAR[1], STAR[2], STAR[3], a * 0.35) blend(sx, sy + 1, STAR[1], STAR[2], STAR[3], a * 0.35) end end end if is_day and cl < 0.75 then local str = 0.55 * (1.0 - elev) * (1.0 - cl / 0.75) if str > 0.02 then local lr, lg, lb = mix(br, bg, bb, SUN_LO[1], SUN_LO[2], SUN_LO[3], 0.55) for y = 0, SKY_H - 1 do local dy = (y - SKY_H) * 0.9 for x = 0, W - 1 do local dx = (x - bx) * 0.55 local dist = sqrt(dx * dx + dy * dy) if dist < 26.0 then local f = 1.0 - dist / 26.0 blend(x, y, lr, lg, lb, str * f * f) end end end end end if cl < 0.7 then if is_day then local kr, kg, kb = mix(SUN_LO[1], SUN_LO[2], SUN_LO[3], SUN_HI[1], SUN_HI[2], SUN_HI[3], elev) for yy = -8, 8 do if by + yy < SKY_H then for xx = -8, 8 do local dist = sqrt(xx * xx + yy * yy) if dist <= 4.2 then px(bx + xx, by + yy, kr, kg, kb) elseif dist <= 8.0 then blend(bx + xx, by + yy, kr, kg, kb, 0.38 * (1.0 - (dist - 4.2) / 3.8)) end end end end else local m = data.moon local ph = m and m.phase or 0.25 local c = cos(ph * 2 * PI) local right = (ph < 0.5) ~= (data.loc.lat < 0) for yy = -6, 6 do if by + yy < SKY_H then local xr = sqrt(max(0.01, 4.2 * 4.2 - yy * yy)) for xx = -6, 6 do local dist = sqrt(xx * xx + yy * yy) if dist <= 4.2 then local q = xx / xr if (right and q >= c) or ((not right) and q <= -c) then px(bx + xx, by + yy, MOON[1], MOON[2], MOON[3]) else blend(bx + xx, by + yy, MOON[1], MOON[2], MOON[3], 0.16) end elseif dist <= 6.2 and m then blend(bx + xx, by + yy, MOON[1], MOON[2], MOON[3], 0.22 * m.lit) end end end end end end draw_landscape(yday1, night, cl, kind == WX_SNOW, frozen, windy, br, bg, bb, H) if kind == WX_FOG then for y = 0, SKY_H - 1 do blend_rect(0, y, W, 1, FOG_C[1], FOG_C[2], FOG_C[3], 0.15 + 0.7 * (y / (SKY_H - 1)) ^ 1.4) end end end local F_GATE = { font = "4x6" } local function gate(s, y, r, g, b) if s.ok then return true end r, g, b = r or 255, g or 250, b or 232 text_center(y, data.net.mode ~= "online" and T.offline or not data.net.up and T.nowifi or s.code ~= 0 and "ERR " .. s.code or T.loading, r, g, b) if s.code ~= 0 then text_center(y + 10, T.console, r, g, b, F_GATE) end return false end local lat0, lon0 local function order(l) if l.lat == lat0 and l.lon == lon0 then return end lat0, lon0 = l.lat, l.lon net.fetch("wx", fmt("/v1/forecast?latitude=%.2f&longitude=%.2f" .. "¤t=temperature_2m,weather_code,cloud_cover,wind_speed_10m,wind_direction_10m" .. "&daily=temperature_2m_max,temperature_2m_min,precipitation_sum&timezone=auto&forecast_days=1", l.lat, l.lon), 900) net.fetch("air", fmt("/v1/air-quality?latitude=%.2f&longitude=%.2f¤t=pm2_5,european_aqi&timezone=auto", l.lat, l.lon), 1800) end function render() if not (data.loc and data.loc.ok) then quit(NOLOC) end order(data.loc) local t = data.time local have_time = t and t.ok local h = have_time and (t.h + t.m / 60.0) or 12.0 local yday1 = have_time and yday_of(t) or 196 local wx = web.wx local air = web.air local wx_ok = wx ~= nil local code = wx_ok and (wx.current and wx.current.weather_code or 0) or 0 local cloud = wx_ok and (wx.current and wx.current.cloud_cover or 0) or 0 local frozen = wx_ok and wx.current ~= nil and (wx.current.temperature_2m or 1) <= 0 local tr, tg, tb, br, bg, bb = sky_colors(pal_hour(h, have_time and data.sun)) local at, is_day = sun_arc(h, have_time and data.sun) local elev = sin(PI * at) local kind = wx_ok and wx_kind(code) or WX_CLEAR local cl = wx_ok and clamp(cloud / 100.0, 0.0, 1.0) or 0.0 if kind == WX_RAIN or kind == WX_SNOW or kind == WX_THUNDER then if cl < 0.85 then cl = 0.85 end end if kind == WX_FOG then if cl < 0.55 then cl = 0.55 end end local ovr, ovg, ovb = mix(OVERCAST_N[1], OVERCAST_N[2], OVERCAST_N[3], OVERCAST_D[1], OVERCAST_D[2], OVERCAST_D[3], 1.0 - darkness(br, bg, bb)) tr, tg, tb = mix(tr, tg, tb, ovr, ovg, ovb, cl * 0.75) br, bg, bb = mix(br, bg, bb, ovr, ovg, ovb, cl * 0.75) local night = darkness(br, bg, bb) local day = 1.0 - night local bx = lround(4 + at * (W - 9)) local by = lround(HORIZON - elev * (HORIZON - ARC_TOP)) local ws = wx_ok and (wx.current and wx.current.wind_speed_10m) or 0 local vx = ws / 10 * sin((wx_ok and (wx.current and wx.current.wind_direction_10m) or 270) * PI / 180) vx = max(-5, min(5, abs(vx) < 0.3 and (vx < 0 and -0.3 or 0.3) or vx)) local windy = ws >= 25 local key = fmt("%d|%d|%d|%d|%d|%d|%s|%s", floor(h * 60), yday1, code, cloud, bx, by, tostring(frozen), tostring(windy)) if key ~= scene_key then draw_scene(tr, tg, tb, br, bg, bb, night, cl, is_day, elev, bx, by, yday1, kind, frozen, windy) SCENE = gfx.snapshot(0, 0, W, H) scene_key = key else blit_rgb(SCENE, 0, 0, W, H) end local ts = frame / FPS local tday = t.unix % 86400 + t.ms / 1000 local dark = 0.45 * night + 0.18 * cl if windy then local dir = vx < 0 and -1 or 1 local amp = clamp((ws - 15) / 15, 0, 1.6) * (0.75 + 0.25 * sin(tday * 0.4)) for i = 1, 3 do draw_tree(TREES[i], TREE_ST.pal, TREE_ST.snow, TREE_ST.dark, TREE_ST.autumn, i - 1, amp * (0.6 * dir + 0.45 * sin(tday * (1.7 + 0.3 * i) + i * 1.9))) end end draw_windsock(ts, vx, ws, dark) draw_flyer(ts, night, kind, vx, ws, true) local ncl = wx_ok and min(CLOUD_N, floor(cl * CLOUD_N + 0.5)) or (day > 0.6 and 2 or 0) if kind == WX_RAIN or kind == WX_SNOW or kind == WX_THUNDER then ncl = max(ncl, 5) end if ncl > 0 then local light = max(0.3, 1 - 0.75 * night) local cr, cg, cb = mix(250, 250, 255, br, bg, bb, 0.3) cr, cg, cb = mix(floor(cr * light), floor(cg * light), floor(cb * light), ovr, ovg, ovb, cl * 0.5) draw_clouds(tday, vx, ncl, cr, cg, cb) end draw_flyer(ts, night, kind, vx, ws, false) if kind == WX_RAIN or kind == WX_THUNDER then local fast = (kind == WX_THUNDER) and 25 or 0 local sl = vx < 0 and -1 or 1 for i = 0, 37 do local h = hash32(i * 0x9E3779B1 + 31) local near = i % 3 == 0 local s = ts * ((near and 135 or 100) + fast + umod(h, 20)) + umod(h >> 5, 64) local n = floor(s / (SKY_H + 8)) local hc = hash32(h + n * 0x85EBCA6B) local x = umod(hc, W) local y = floor(s - n * (SKY_H + 8)) - 4 if near then for k = 0, 3 do blend(x + sl * (k // 2), y + k, RAIN_C[1], RAIN_C[2], RAIN_C[3], 0.85 - 0.15 * k) end elseif y < SKY_H - 3 - umod(hc >> 8, 10) then for k = 0, 1 do blend(x, y + k, RAIN_C[1], RAIN_C[2], RAIN_C[3], 0.55 - 0.2 * k) end end end if kind == WX_THUNDER then local win, fw = win_at(3) local hb = hash32(win * 0x9E3779B1 + 5) local fl = (fw - umod(hb >> 4, 200) / 100) * FPS if umod(hb, 3) == 0 and fl >= 0 and fl < 3 then blend_rect(0, 0, W, SKY_H, 255, 255, 245, 0.55) end end elseif kind == WX_SNOW then for i = 0, 29 do local h = hash32(i * 0x9E3779B1 + 57) local v = 22 + umod(h, 14) local s = ts * v + umod(h >> 5, 64) local n = floor(s / (SKY_H + 4)) local y = s - n * (SKY_H + 4) local x = floor(umod(hash32(h + n * 0x85EBCA6B), W) + 3.0 * sin(ts * 2.4 + i) + y / v * vx * 0.6) % W y = floor(y) - 2 if i % 3 == 0 then blend(x, y, SNOW_C[1], SNOW_C[2], SNOW_C[3], 0.95) blend(x + 1, y, SNOW_C[1], SNOW_C[2], SNOW_C[3], 0.55) blend(x, y + 1, SNOW_C[1], SNOW_C[2], SNOW_C[3], 0.55) else blend(x, y, SNOW_C[1], SNOW_C[2], SNOW_C[3], 0.7) end end end if frozen and kind ~= WX_SNOW and season_at(yday1)[7] <= 0.5 then draw_ice_glints(ts, night) end draw_boat(ts, yday1, kind, frozen) blend_rect(0, SKY_H, W, H - SKY_H, PANEL_BG[1], PANEL_BG[2], PANEL_BG[3], PANEL_DIM) local lr, lg, lb = mix(br, bg, bb, 255, 255, 255, 0.30) rect(0, SKY_H, W, 1, lr, lg, lb) if have_time and cfg.clock then local hm if t.fmt12 then hm = fmt("%d:%02d", t.h12, t.m) .. (t.pm and "p" or "a") else hm = fmt("%02d:%02d", t.h, t.m) end local ds = fmt("%02d.%02d", t.day, t.month) local ty = SKY_H - 8 if cfg.back == "band" then blend_rect(0, ty - 1, W, 1, PANEL_BG[1], PANEL_BG[2], PANEL_BG[3], 0.25) blend_rect(0, ty, W, 7, PANEL_BG[1], PANEL_BG[2], PANEL_BG[3], 0.5) blend_rect(0, ty + 7, W, 1, PANEL_BG[1], PANEL_BG[2], PANEL_BG[3], 0.25) end local o = cfg.back == "outline" text(1, ty, hm, AMBER_LIGHT[1], AMBER_LIGHT[2], AMBER_LIGHT[3], o and OUT_L or INK_L) text(W - 2, ty, ds, AMBER_LIGHT[1], AMBER_LIGHT[2], AMBER_LIGHT[3], o and OUT_R or INK_R) end if not gate(data.src.wx, 34) then return end if not wx then return end local cur = wx.current or {} local daily = wx.daily or {} local k = data.arrive and min(1, frame / FPS / 1.4) or 1 local tstr = fmt("%d", lround((cur.temperature_2m or 0) * (1 - (1 - k) ^ 3))) local T_X, T_INK_Y = 2, 31 text(T_X, T_INK_Y, tstr, TEMP_C[1], TEMP_C[2], TEMP_C[3], F_BIG) stamp(RING, T_X + text_width(tstr, F_BIG) + 2, T_INK_Y, TEMP_C) local tmax = (daily.temperature_2m_max or {})[1] or 0 local tmin = (daily.temperature_2m_min or {})[1] or 0 draw_arrow(44, 32, true, MAX_C); text(51, 30, fmt("%d", lround(tmax)), MAX_C[1], MAX_C[2], MAX_C[3]) draw_arrow(44, 40, false, MIN_C); text(51, 38, fmt("%d", lround(tmin)), MIN_C[1], MIN_C[2], MIN_C[3]) local acur = air and air.current or nil if acur then local pm = acur.pm2_5 or 0 local c = band_color(pm, PM_LIM) text(2, 47, "PM " .. fmt("%d", lround(pm)), c[1], c[2], c[3]) end local precip = (daily.precipitation_sum or {})[1] or 0 if precip < 0 then precip = 0 end if precip > 0.05 then local ps = (precip < 10.0) and fmt("%.1f", precip) or fmt("%d", lround(precip)) local snowing = kind == WX_SNOW local w = (snowing and 5 or 3) + 2 + text_width(ps) local x = W - w - 2 if snowing then stamp(FLAKE, x, 48, PRECIP_C); x = x + 7 else stamp(DROP, x, 48, PRECIP_C); x = x + 5 end text(x, 47, ps, PRECIP_C[1], PRECIP_C[2], PRECIP_C[3], INK_L) end if acur then local aqi = acur.european_aqi or 0 local c = band_color(aqi, AQI_LIM) text_center(55, "AQI " .. fmt("%d", aqi), c[1], c[2], c[3]) end end