info = { type = "app", title = "Sky", author = "BRRRO", license = "MIT", desc = "Sunrise, sunset and day length", category = "outdoors", api = 43, } permissions = { "location" } txt = { notime = "NO TIME", noloc = "Set the location in the web UI", loading = "LOADING", nowifi = "NO WIFI", offline = "OFFLINE", scene = "Scene", scene_o = { "sun arc", "day dial" }, wx = "Weather", wx_h = "Clouds, rain and snow in the sun arc, from open-meteo.com", ev = { [0] = "EQUINOX", "SOLSTICE", "EQUINOX", "SOLSTICE" } } for k, v in pairs(({ sk = { notime = "BEZ CASU", noloc = "Nastav polohu vo webe", loading = "NACITAVAM", nowifi = "BEZ WIFI", scene = "Scéna", scene_o = { "oblúk slnka", "ciferník dňa" }, wx = "Počasie", wx_h = "Oblaky, dážď a sneh v oblúku slnka, z open-meteo.com", ev = { [0] = "ROVNODENNOST", "SLNOVRAT", "ROVNODENNOST", "SLNOVRAT" } }, cs = { notime = "BEZ CASU", noloc = "Nastav polohu na webu", loading = "NACITAM", nowifi = "BEZ WIFI", scene = "Scéna", scene_o = { "oblouk slunce", "ciferník dne" }, wx = "Počasí", wx_h = "Mraky, déšť a sníh v oblouku slunce, z open-meteo.com", ev = { [0] = "ROVNODENNOST", "SLUNOVRAT", "ROVNODENNOST", "SLUNOVRAT" } }, })[lang] or {}) do txt[k] = v end settings = { { key = "scene", type = "int", default = "arc", label = txt.scene, opts = { keys = { "arc", "dial" }, table.unpack(txt.scene_o) } }, { key = "weather", type = "bool", default = true, label = txt.wx, hint = txt.wx_h }, } sources = { wx = { url = "https://api.open-meteo.com", limit = 60, fields = { "current.weather_code", "current.cloud_cover", "current.wind_speed_10m", "current.wind_direction_10m" }, demo = [[{"current":{"weather_code":2,"cloud_cover":45,"wind_speed_10m":14.0,"wind_direction_10m":290}}]] }, } local W, H = gfx.W, gfx.H local FPS = gfx.FPS local clear, px, blend, rect, blend_rect = gfx.clear, gfx.px, gfx.blend, gfx.rect, gfx.blend_rect local text, text_center, text_bounds = gfx.text, gfx.text_center, gfx.text_bounds local blit_rgb = gfx.blit_rgb local F_SMALL = { font = "3x5" } local hash32, umod = math.hash32, math.umod local floor, sqrt, min, max, abs = math.floor, math.sqrt, math.min, math.max, math.abs local sin, cos, asin, acos, atan = math.sin, math.cos, math.asin, math.acos, math.atan local fmod, format = math.fmod, string.format local char, concat = string.char, table.concat local PI = math.pi local function lround(v) if v >= 0 then return floor(v + 0.5) else return -floor(-v + 0.5) end end local ARC = { 255, 236, 170 } local SUN_CORE = { 255, 246, 190 } local SUN_EDGE = { 255, 180, 60 } local LEN_COL = { 255, 252, 240 } local DELTA_UP = { 180, 255, 200 } local DELTA_DN = { 255, 200, 240 } local RISE_COL = { 255, 196, 90 } local SET_COL = { 255, 132, 70 } local STAR_HI = { 225, 235, 255 } local STAR_LO = { 150, 165, 205 } local MSG_COL = { 255, 250, 232 } 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 BOLT_C = { 255, 255, 230 } local FAR_BASE, NEAR_BASE = { 92, 118, 150 }, { 46, 84, 62 } 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 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 t, u, p, q = a[2], b[2], a[3], b[3] return floor(t[1] + (u[1] - t[1]) * f), floor(t[2] + (u[2] - t[2]) * f), floor(t[3] + (u[3] - t[3]) * f), floor(p[1] + (q[1] - p[1]) * f), floor(p[2] + (q[2] - p[2]) * f), floor(p[3] + (q[3] - p[3]) * f) end end local t, p = SKY[1][2], SKY[1][3] return t[1], t[2], t[3], p[1], p[2], p[3] end local function pal_hour(h, rise, set, polar) if polar then return rise == set and 0 or 13 end if h < rise then return h / rise * 6.5 elseif h < set then return 6.5 + (h - rise) / (set - rise) * 12.5 end return 19 + (h - set) / (24 - set) * 5 end local CUM = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 } local function yday_of(t) local d = CUM[t.month] + t.day local y = t.year if t.month > 2 and (y % 4 == 0 and (y % 100 ~= 0 or y % 400 == 0)) then d = d + 1 end return d end local function sun_times(yday1, lat, lon, off_s) local D2R = PI / 180.0 local B = 360.0 * (yday1 - 81) / 365.0 local eot = 9.87 * sin(2 * B * D2R) - 7.53 * cos(B * D2R) - 1.5 * sin(B * D2R) local decl = asin(0.39795 * cos(0.98563 * (yday1 - 173) * D2R)) local noon = 12.0 - lon / 15.0 + off_s / 3600.0 - eot / 60.0 local cosH = (sin(-0.833 * D2R) - sin(lat * D2R) * sin(decl)) / (cos(lat * D2R) * cos(decl)) if cosH > 1.0 then return noon, noon, true, decl / D2R end if cosH < -1.0 then return 0.0, 24.0, true, decl / D2R end local Hh = acos(cosH) / D2R / 15.0 return noon - Hh, noon + Hh, false, decl / D2R end local sc = { yday = -1 } local function sun_today(yday1, lat, lon, off_s) if yday1 ~= sc.yday or off_s ~= sc.off or lat ~= sc.lat or lon ~= sc.lon then local decl sc.rise, sc.set, sc.polar, decl = sun_times(yday1, lat, lon, off_s) sc.alt = 90 - abs(lat - decl) local yr, ys = sun_times(yday1 > 1 and yday1 - 1 or 365, lat, lon, off_s) sc.dmin = lround(((sc.set - sc.rise) - (ys - yr)) * 60.0) sc.yday, sc.off, sc.lat, sc.lon = yday1, off_s, lat, lon sc.gen = (sc.gen or 0) + 1 end return sc.rise, sc.set, sc.polar, sc.dmin end local function sun_lon(unix) local n = (unix - 946728000) / 86400 local g = (357.528 + 0.9856003 * n) * PI / 180 return fmod(280.46 + 0.9856474 * n + 1.915 * sin(g) + 0.020 * sin(2 * g), 360) end local function solar_event(t) if sc.ev_gen ~= sc.gen then local mid = t.unix - (t.h * 3600 + t.m * 60 + t.s) local a, b = sun_lon(mid), sun_lon(mid + 86400) sc.ev = (b < a and 0) or (floor(a / 90) ~= floor(b / 90) and floor(b / 90) % 4) or nil sc.ev_gen = sc.gen end return sc.ev end local EV_COL = { 255, 212, 90 } local function draw_rays(x, y, t, k) for i = 0, 7 do local a = t * 0.5 + i * PI / 4 for r = 5, 6 do blend(floor(x + r * cos(a) + 0.5), floor(y + r * sin(a) + 0.5), SUN_EDGE[1], SUN_EDGE[2], SUN_EDGE[3], k * (r == 5 and 0.8 or 0.45)) end end end local function draw_delta(y, dmin, ev) if ev and (frame // (4 * FPS)) % 2 == 1 then text_center(y, txt.ev[ev], EV_COL[1], EV_COL[2], EV_COL[3], F_SMALL) else local c = dmin >= 0 and DELTA_UP or DELTA_DN text_center(y, format("%+dM", dmin), c[1], c[2], c[3], F_SMALL) end end local function fmt_hm(h) local m = lround(h * 60.0) % 1440 if m < 0 then m = m + 1440 end return format("%d:%02d", m // 60, m % 60) end local function draw_stars(f, n, place) for i = 0, n - 1 do local sh = hash32(i * 0x9E3779B1 + 0x9E3779B9) local life = 10 * FPS + umod(sh, 20 * FPS) local off = umod(sh, life) local age = (f + off) % life local gen = (f + off) // life local ph = hash32(sh ~ (gen * 0x85EBCA6B)) local v = sin(PI * age / life) * (0.78 + 0.22 * sin(fmod(f / FPS * 3 + i * 1.7, 6.28318))) local c = v > 0.62 and STAR_HI or (v > 0.32 and STAR_LO or nil) if c then local x, y = place(ph) if x then px(x, y, c[1], c[2], c[3]) end end end end local function draw_tri(x, y, up, c) for r = 0, 2 do local w = r for i = -w, w do px(x + 1 + i, up and y + r or y + 4 - r, c[1], c[2], c[3]) end end rect(x + 1, up and y + 3 or y, 1, 2, c[1], c[2], c[3]) end local function draw_times(y, edge, rise, set) draw_tri(edge + 1, y, true, RISE_COL) text(edge + 6, y, fmt_hm(rise), RISE_COL[1], RISE_COL[2], RISE_COL[3], F_SMALL) local v = fmt_hm(set) local _, hi = text_bounds(v, F_SMALL) text(W - 1 - edge - 6 - hi, y, v, SET_COL[1], SET_COL[2], SET_COL[3], F_SMALL) draw_tri(W - 1 - edge - 3, y, false, SET_COL) end local function daylen_text(rise, set) local m = lround((set - rise) * 60.0); return format("%dH%02d", m // 60, m % 60) end local RUN_S = 1.4 local function run_in(ts) local k = data.arrive and min(1, ts / RUN_S) or 1; return 1 - (1 - k) ^ 3 end local function draw_sun(x, y, a, t) local cx, cy = lround(x), lround(y) local g = 0.6 * a * (0.8 + 0.2 * sin(t * PI / 2)) for dy = -3, 3 do for dx = -3, 3 do local r2 = dx * dx + dy * dy if r2 > 5 and r2 <= 10 then blend(cx + dx, cy + dy, SUN_EDGE[1], SUN_EDGE[2], SUN_EDGE[3], g) elseif r2 <= 5 then local c = r2 <= 1 and SUN_CORE or SUN_EDGE; blend(cx + dx, cy + dy, c[1], c[2], c[3], a) end end end end local DRIZZLE = { [51] = 8, [53] = 12, [55] = 16, [56] = 8, [57] = 14 } local RAIN = { [61] = 16, [63] = 28, [65] = 42, [66] = 16, [67] = 30, [80] = 18, [81] = 30, [82] = 44 } local SNOW = { [71] = 14, [73] = 24, [75] = 36, [77] = 12, [85] = 22, [86] = 36 } local function wx_kind(code) if code >= 95 then return "storm", 40 end if code == 45 or code == 48 then return "fog", 0 end if SNOW[code] then return "snow", SNOW[code] end if RAIN[code] or DRIZZLE[code] then return "rain", RAIN[code] or DRIZZLE[code] end return nil, 0 end local function weather() local c = web.wx and web.wx.current if not (cfg.weather and c and c.weather_code and data.src.wx.age < 3 * 3600) then return nil end return c end local CLOUD_N = 7 local CLOUDS = {} for k = 1, CLOUD_N do local h = hash32(k * 0x9E3779B1 + 77) local w, ht = 12 + umod(h, 8), 6 + umod(h >> 4, 2) local bumps = {} for b = 1, 2 + umod(h >> 7, 2) do local hb = hash32(h + b * 0x85EBCA6B) bumps[b] = { 2.5 + umod(hb, 1000) / 1000 * (w - 5), ht - 2.5, 2.2 + umod(hb >> 10, 1000) / 1000 * 1.8 } 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 or cell[i - w] == 0) then cell[i] = 1 elseif cell[i] == 1 then cell[i] = 2 end end CLOUDS[k] = { w = w, h = ht, cell = cell, y = 18 + umod(h >> 12, 16), x0 = umod(h >> 16, 64), v = 0.75 + umod(h >> 20, 50) / 100 } end local cloud_key local function tint_clouds(light, key) if key == cloud_key then return end 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(light[1] * k)), max(1, floor(light[2] * k)), max(1, floor(light[3] * 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 local FAR_H, NEAR_H = {}, {} for x = 0, W - 1 do local e = 0.45 + 0.55 * sin(PI * x / (W - 1)) FAR_H[x] = max(1, floor(e * (4.5 + 1.8 * sin(x * 0.11 + 1.3) + 1.2 * sin(x * 0.27 + 0.4) + 0.6 * sin(x * 0.53 + 2.1)) + 0.5)) NEAR_H[x] = max(1, floor(e * (2.2 + 1.3 * sin(x * 0.17 + 2.6) + 0.8 * sin(x * 0.41 + 0.9)) + 0.5)) end local HORIZON = 52 local ARC_HMAX = 30 local ARC_X0, ARC_X1 = 3, 60 local LEN_Y, DELTA_Y = 4, 13 local TIME_Y = HORIZON + 3 local arc_h, arc_gen = ARC_HMAX, nil local function arc_xy(u) return ARC_X0 + u * (ARC_X1 - ARC_X0), HORIZON - arc_h * sin(u * PI) end local ARC_STEP = 6 local ARC_DOTS = {} local function build_arc(alt) arc_h, arc_gen, ARC_DOTS = max(4, ARC_HMAX * sin(alt * PI / 180)), sc.gen, {} local N, acc, len = 400, { 0 }, 0 local lx, ly = arc_xy(0) for i = 1, N do local x, y = arc_xy(i / N) len = len + sqrt((x - lx) ^ 2 + (y - ly) ^ 2) acc[i + 1], lx, ly = len, x, y end local n = lround(len / ARC_STEP) if n % 2 == 0 then n = n + 1 end local j = 1 for k = 0, (n - 1) // 2 do local s = k * len / n while acc[j + 1] < s do j = j + 1 end local u = (j - 1 + (s - acc[j]) / max(1e-6, acc[j + 1] - acc[j])) / N local x, y = arc_xy(u) local ix, iy = floor(x + 0.5), lround(y) ARC_DOTS[#ARC_DOTS + 1] = { ix, iy } ARC_DOTS[#ARC_DOTS + 1] = { 63 - ix, iy } end end local FLY_WIN = 60 local BIRD_C = { 40, 44, 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, vx, ws, day, kind, back) local win, fw = win_at(FLY_WIN) local h = hash32(win * 0x9E3779B1 + 91) if umod(h, 3) == 0 or kind == "storm" then return end local calm = day and not kind 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 + 12 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 local y = 20 + umod(h >> 10, 12) if pick == 0 then local x = right and -6 + p or W + 6 - p local up = floor(t * 6) % 2 == 0 for _, o in ipairs({ { 0, 0 }, { -4 * dir, -2 }, { -4 * dir, 2 } }) do local bx, by = floor(x + o[1]), y + o[2] dot(bx, up and by or by + 1, BIRD_C); dot(bx + 1, up and by + 1 or by, BIRD_C); dot(bx + 2, up and by or by + 1, BIRD_C) end elseif pick == 1 then local x = floor(right and -7 + p or W - p) local by = y - 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) if day 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 mixc(a, b, t) return a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t end local function lightning() local win, fw = win_at(3) local h = hash32(win * 0x9E3779B1 + 5) if umod(h, 3) ~= 0 then return 0 end local f = (fw - umod(h >> 4, 200) / 100) * FPS if f >= 0 and f < 3 then return 0.55, 8 + umod(h >> 12, 48), h end if f >= 3 and f < 6 then return 0.2, 8 + umod(h >> 12, 48), h end return 0 end local function draw_arc_scene(ts, nowH, rise, set, polar, dmin, day, ev) if arc_gen ~= sc.gen then build_arc(sc.alt) end local wx = weather() local kind, drops, cover, ws, wd = nil, 0, 0, 0, 270 if wx then kind, drops = wx_kind(wx.weather_code) cover, ws, wd = wx.cloud_cover or 0, wx.wind_speed_10m or 0, wx.wind_direction_10m or 270 end local ov = min(1, max(0, (cover - 40) / 60)) * 0.65 if kind then ov = max(ov, kind == "storm" and 0.8 or 0.5) end local tr, tg, tb, br, bg, bb = sky_colors(pal_hour(nowH, rise, set, polar)) local light = min(1, max(0.25, (0.3 * br + 0.59 * bg + 0.11 * bb) / 180)) local oc = { mixc(OVERCAST_N, OVERCAST_D, light) } tr, tg, tb = mixc({ tr, tg, tb }, oc, ov) br, bg, bb = mixc({ br, bg, bb }, oc, ov) local flash, bx, bh = 0 if kind == "storm" then flash, bx, bh = lightning() end if flash > 0 then tr, tg, tb = mixc({ tr, tg, tb }, BOLT_C, flash) br, bg, bb = mixc({ br, bg, bb }, BOLT_C, flash) end for y = 0, HORIZON - 1 do local f = y / (HORIZON - 1) rect(0, y, W, 1, floor(tr + (br - tr) * f), floor(tg + (bg - tg) * f), floor(tb + (bb - tb) * f)) end if not day and ov < 0.5 then draw_stars(frame, floor(24 * (1 - 2 * ov)), function(ph) local x, y = umod(ph, W), 1 + (ph >> 9) % (HORIZON - 3) if y <= DELTA_Y + 6 and x >= 12 and x <= 51 then return nil end local u = (x - ARC_X0) / (ARC_X1 - ARC_X0) if u >= -0.05 and u <= 1.05 and abs(y - (HORIZON - arc_h * sin(max(0, min(1, u)) * PI))) < 3 then return nil end return x, y end) end if not polar then local dc = ev and EV_COL or ARC for _, d in ipairs(ARC_DOTS) do px(d[1], d[2], dc[1], dc[2], dc[3]) end if day then local x, y = arc_xy((nowH - rise) / (set - rise) * run_in(ts)) if ev then draw_rays(x, y, frame / FPS, 1 - 0.7 * ov) end draw_sun(x, y, 1 - 0.7 * ov, frame / FPS) end end local t = frame / FPS local vx = ws / 10 * sin(wd * PI / 180) vx = max(-5, min(5, abs(vx) < 0.3 and (vx < 0 and -0.3 or 0.3) or vx)) local ncl = min(CLOUD_N, floor(cover / 100 * CLOUD_N + 0.5)) if kind then ncl = max(ncl, 4) end draw_flyer(t, vx, ws, day, kind, true) if ncl > 0 then local cl = { mixc({ 250, 250, 255 }, { br, bg, bb }, 0.3) } cl = { cl[1] * max(light, 0.3), cl[2] * max(light, 0.3), cl[3] * max(light, 0.3) } cl = { mixc(cl, oc, ov * 0.6) } tint_clouds(cl, floor(cl[1] / 4) * 4096 + floor(cl[2] / 4) * 64 + floor(cl[3] / 4)) for k = 1, ncl do local c = CLOUDS[k] blit_rgb(c.ras, floor((c.x0 + t * vx * c.v) % (W + c.w)) - c.w, c.y, c.w, c.h, 0, 0, 0) end end draw_flyer(t, vx, ws, day, kind, false) if flash > 0 then local x = bx for y = 30, HORIZON - FAR_H[bx] do if y % 3 == 0 then x = x + umod(hash32(bh + y), 3) - 1 end px(x, y, BOLT_C[1], BOLT_C[2], BOLT_C[3]) end end local fr, fg, fb = mixc({ FAR_BASE[1] * light, FAR_BASE[2] * light, FAR_BASE[3] * light }, { br, bg, bb }, 0.35) local nr, ng, nb = mixc({ NEAR_BASE[1] * light, NEAR_BASE[2] * light, NEAR_BASE[3] * light }, { br, bg, bb }, 0.12) for x = 0, W - 1 do rect(x, HORIZON - FAR_H[x], 1, FAR_H[x], floor(fr), floor(fg), floor(fb)) rect(x, HORIZON - NEAR_H[x], 1, NEAR_H[x], floor(nr), floor(ng), floor(nb)) if kind == "snow" then blend(x, HORIZON - FAR_H[x], SNOW_C[1], SNOW_C[2], SNOW_C[3], 0.75) blend(x, HORIZON - NEAR_H[x], SNOW_C[1], SNOW_C[2], SNOW_C[3], 0.55) end end rect(0, HORIZON, W, 1, floor(nr), floor(ng), floor(nb)) rect(0, HORIZON + 1, W, 1, floor(nr * 0.5), floor(ng * 0.5), floor(nb * 0.5)) rect(0, HORIZON + 2, W, 1, floor(nr * 0.2), floor(ng * 0.2), floor(nb * 0.2)) if kind == "rain" or kind == "storm" then local slant = vx / 45 for i = 0, drops - 1 do local h = hash32(i * 0x9E3779B1 + 31) local near = i % 3 == 0 local s = t * ((near and 52 or 36) + umod(h, 12)) + umod(h >> 5, 64) local n = floor(s / (HORIZON - 20)) local y = 20 + s - n * (HORIZON - 20) local x = floor(umod(hash32(h + n * 0x85EBCA6B), W) + (y - 20) * slant) % W y = floor(y) if near then px(x, y, RAIN_C[1], RAIN_C[2], RAIN_C[3]) blend(x, y - 1, RAIN_C[1], RAIN_C[2], RAIN_C[3], 0.6) blend(x, y - 2, RAIN_C[1], RAIN_C[2], RAIN_C[3], 0.3) else blend(x, y, RAIN_C[1], RAIN_C[2], RAIN_C[3], 0.6) blend(x, y - 1, RAIN_C[1], RAIN_C[2], RAIN_C[3], 0.25) end end elseif kind == "snow" then for i = 0, drops - 1 do local h = hash32(i * 0x9E3779B1 + 57) local v = 6 + umod(h, 5) local s = t * v + umod(h >> 5, 64) local n = floor(s / (HORIZON - 20)) local y = s - n * (HORIZON - 20) local x = floor(umod(hash32(h + n * 0x85EBCA6B), W) + y / v * vx * 0.6 + 1.5 * sin(t * 1.3 + i)) % W if i % 3 == 0 then px(x, floor(y) + 20, SNOW_C[1], SNOW_C[2], SNOW_C[3]) else blend(x, floor(y) + 20, SNOW_C[1], SNOW_C[2], SNOW_C[3], 0.7) end end elseif kind == "fog" then for y = 26, HORIZON + 1 do local a = 0.6 * ((y - 26) / (HORIZON - 26)) ^ 1.2 * (0.9 + 0.1 * sin(t * 0.5 + y * 0.4)) blend_rect(0, y, W, 1, FOG_C[1], FOG_C[2], FOG_C[3], min(0.6, a)) end end if not polar then text_center(LEN_Y, daylen_text(rise, set), LEN_COL[1], LEN_COL[2], LEN_COL[3]) draw_delta(DELTA_Y, dmin, ev) end draw_times(TIME_Y, 2, rise, set) end local CX, CY, R_OUT, R_IN = 31.5, 31.5, 30, 25.5 local R_MID = (R_OUT + R_IN) / 2 local function hour_xy(h, r) local a = h / 24 * 2 * PI return CX - r * sin(a), CY + r * cos(a) end local RING_I, RING_H, RING_A, RING_T = {}, {}, {}, {} do for y = 0, H - 1 do for x = 0, W - 1 do local dx, dy = x - CX, y - CY local d = sqrt(dx * dx + dy * dy) if d >= R_IN - 0.5 and d <= R_OUT + 0.5 then local n = #RING_I + 1 RING_I[n] = y * W + x + 1 RING_H[n] = fmod(atan(-dx, dy) / (2 * PI) * 24 + 24, 24) RING_A[n] = min(1, d - (R_IN - 0.5), (R_OUT + 0.5) - d) RING_T[n] = (d - R_IN) / (R_OUT - R_IN) end end end end local function lift(r, g, b) local m = max(r, g, b) if m >= 80 or m == 0 then return r, g, b end local k = 80 / m return floor(r * k), floor(g * k), floor(b * k) end local dial, dial_gen local function build_dial(rise, set, polar) local row, rows, black, n = {}, {}, char(0, 0, 0), 1 for y = 0, H - 1 do for x = 1, W do row[x] = black end while n <= #RING_I and RING_I[n] <= (y + 1) * W do local tr, tg, tb, br, bg, bb = sky_colors(pal_hour(RING_H[n], rise, set, polar)) tr, tg, tb = lift(tr, tg, tb) br, bg, bb = lift(br, bg, bb) local f, a = 1 - RING_T[n], RING_A[n] row[RING_I[n] - y * W] = char(floor((tr + (br - tr) * f) * a), floor((tg + (bg - tg) * f) * a), floor((tb + (bb - tb) * f) * a)) n = n + 1 end rows[y + 1] = concat(row) end dial, dial_gen = concat(rows), sc.gen end local function night_at(h, rise, set, polar) if polar then return rise == set end return h < rise - 0.3 or h > set + 0.3 end local function draw_dial_scene(ts, nowH, rise, set, polar, dmin, ev) if dial_gen ~= sc.gen then build_dial(rise, set, polar) end blit_rgb(dial, 0, 0, W, H, 0, 0, 0) draw_stars(frame, 24, function(ph) local n = umod(ph, #RING_I) + 1 if RING_T[n] < 0.2 or RING_T[n] > 0.8 or not night_at(RING_H[n], rise, set, polar) then return nil end local i = RING_I[n] - 1 return i % W, i // W end) if not polar then for _, e in ipairs({ { rise, RISE_COL }, { set, SET_COL } }) do for r = R_IN + 1.5, R_OUT + 0.5, 0.5 do local x, y = hour_xy(e[1], r) blend(floor(x + 0.5), floor(y + 0.5), e[2][1], e[2][2], e[2][3], 0.9) end end end local h = fmod(rise + fmod(nowH - rise + 24, 24) * run_in(ts), 24) local k = night_at(h, rise, set, polar) and 0.5 or 1 local x, y = hour_xy(h, R_MID) if ev then draw_rays(x, y, frame / FPS, k) end for dy = -4, 4 do for dx = -4, 4 do local d = sqrt((floor(x) + dx + 0.5 - x) ^ 2 + (floor(y) + dy + 0.5 - y) ^ 2) if d <= 2.6 then local c = d <= 1.2 and SUN_CORE or SUN_EDGE px(floor(x) + dx, floor(y) + dy, floor(c[1] * k), floor(c[2] * k), floor(c[3] * k)) elseif d <= 4 then blend(floor(x) + dx, floor(y) + dy, SUN_EDGE[1], SUN_EDGE[2], SUN_EDGE[3], k * 0.55 * (4 - d) / 1.4) end end end draw_times(21, 9, rise, set) if not polar then text_center(28, daylen_text(rise, set), LEN_COL[1], LEN_COL[2], LEN_COL[3]) draw_delta(38, dmin, ev) end end local want0, lat0, lon0 local function order(l) local want = cfg.weather and cfg.scene ~= "dial" if want == want0 and l.lat == lat0 and l.lon == lon0 then return end want0, lat0, lon0 = want, l.lat, l.lon if not want then net.stop("wx") return end net.fetch("wx", string.format("/v1/forecast?latitude=%.2f&longitude=%.2f" .. "¤t=weather_code,cloud_cover,wind_speed_10m,wind_direction_10m", l.lat, l.lon), 900) end function render() clear(0, 0, 0) local t, l = data.time, data.loc if not (t and t.ok) then local n = data.net local why = (n and n.internet == false) and txt.offline or ((n and n.up) and txt.loading or txt.nowifi) text_center(24, why, MSG_COL[1], MSG_COL[2], MSG_COL[3]) text_center(35, txt.notime, MSG_COL[1], MSG_COL[2], MSG_COL[3], F_SMALL) return end if not (l and l.ok) then quit(txt.noloc) end order(l) local nowH = t.h + t.m / 60.0 local rise, set, polar, dmin = sun_today(yday_of(t), l.lat, l.lon, (t.utc_off or 0) * 60) local ev = solar_event(t) if cfg.scene == "dial" then draw_dial_scene(frame / FPS, nowH, rise, set, polar, dmin, ev) else draw_arc_scene(frame / FPS, nowH, rise, set, polar, dmin, (not polar) and nowH >= rise and nowH <= set, ev) end end