info = { type = "app", title = "City clock", author = "BRRRO", license = "MIT", desc = "Exact time over NTP", category = "time", api = 42, } permissions = { "location" } local W, H = gfx.W, gfx.H local clear, px, blend, rect = gfx.clear, gfx.px, gfx.blend, gfx.rect local text, text_center, text_width = gfx.text, gfx.text_center, gfx.text_width local SHADOW = { 6, 6, 14 } local F_BIG = { font = "9x15b" } local F_BLINK = { font = "9x15b", hide = nil, shadow = SHADOW } local O_DAY = { shadow = SHADOW } local CELL = { cell = true } local AM_PM = { w = 1, align = "right", pad = 0 } local blit_rgb = gfx.blit_rgb local floor, sin, cos, sqrt, fmod, abs = math.floor, math.sin, math.cos, math.sqrt, math.fmod, math.abs local hash32 = math.hash32 local fmt, char, concat, find = string.format, string.char, table.concat, string.find 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 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 clamp(v, lo, hi) if v < lo then return lo elseif v > hi then return hi end return v end local function bl1(v, c, a) return floor(v + (c - v) * a) end local umod = math.umod local SKY_H, ARC_TOP, HORIZON = 28, 5, 26 local SUN_HI, SUN_LO = { 255, 250, 190 }, { 255, 140, 20 } local MOON = { 235, 240, 255 } local STAR = { 225, 235, 255 } local WINDOW = { 255, 200, 60 } local TIME_R, TIME_G, TIME_B = 255, 250, 232 local DAY_R, DAY_G, DAY_B = 220, 228, 245 local DATE_R, DATE_G, DATE_B = 150, 162, 190 local COMET_TAIL = 7 local TRAIL_F = 0.45 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 FAR = { { 2, 7, 7, 0 }, { 11, 5, 10, 0 }, { 18, 9, 6, 2 }, { 29, 6, 9, 0 }, { 37, 7, 7, 0 }, { 46, 5, 11, 3 }, { 53, 8, 6, 0 }, { 62, 4, 8, 0 } } local NEAR = { { 0, 6, 11, 0 }, { 7, 5, 17, 1 }, { 15, 8, 13, 2 }, { 25, 6, 20, 3 }, { 33, 9, 15, 0 }, { 44, 5, 19, 1 }, { 51, 8, 12, 2 }, { 60, 4, 16, 0 } } local DAYS = ({ sk = { "NEDEĽA", "PONDELOK", "UTOROK", "STREDA", "ŠTVRTOK", "PIATOK", "SOBOTA" }, cs = { "NEDĚLE", "PONDĚLÍ", "ÚTERÝ", "STŘEDA", "ČTVRTEK", "PÁTEK", "SOBOTA" }, })[lang] or { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" } 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 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) local lum = 0.3 * br + 0.6 * bg + 0.1 * bb return clamp((120.0 - lum) / 90.0, 0.0, 1.0) end local function sun_arc(h, rise, set, up) if 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 local t if h < rise then t = (h + 24.0 - set) / night_len else t = (h - set) / night_len end return t, false end local SKYR, sky_key = nil, nil local ROW = {} local bloom_on, bloom_r, bloom_g, bloom_b, bloom_str, bloom_bx = false, 0, 0, 0, 0, 0 local function bloom_alpha(x, y) local dx, dy = (x - bloom_bx) * 0.55, (y - SKY_H) * 0.9 local dist = sqrt(dx * dx + dy * dy) if dist >= 26.0 then return 0 end local f = 1.0 - dist / 26.0 return bloom_str * f * f end local function build_sky(tr, tg, tb, br, bg, bb, is_day, bx, elev) bloom_str = is_day and 0.55 * (1.0 - elev) or 0 bloom_on = is_day and bloom_str >= 0.02 bloom_bx = bx if bloom_on then bloom_r, bloom_g, bloom_b = mix(br, bg, bb, SUN_LO[1], SUN_LO[2], SUN_LO[3], 0.55) end local rows = {} for y = 0, SKY_H - 1 do local r, g, b = mix(tr, tg, tb, br, bg, bb, (y / (SKY_H - 1)) ^ 0.85) ROW[y] = { r, g, b } local row = {} if bloom_on then for x = 0, W - 1 do local a = bloom_alpha(x, y) if a > 0 then row[x + 1] = char(bl1(r, bloom_r, a), bl1(g, bloom_g, a), bl1(b, bloom_b, a)) else row[x + 1] = char(r, g, b) end end else local c = char(r, g, b) for x = 0, W - 1 do row[x + 1] = c end end rows[y + 1] = concat(row) end SKYR = concat(rows) end local STAR_N, STAR_SPAN, STAR_SEED = 18, 17, 0x9E3779B9 local function draw_stars(f, amp) for i = 0, STAR_N - 1 do local sh = hash32(i * 0x9E3779B1 + STAR_SEED) 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 x = umod(ph, W) local y = (ph >> 9) % STAR_SPAN local k = sin(3.14159 * age / life) local tw = 0.78 + 0.22 * sin(fmod(f * 0.10 + i * 1.7, 6.28318)) local a = amp * k * k * tw if bloom_on then local rw = ROW[y] local r, g, b = bl1(rw[1], STAR[1], a), bl1(rw[2], STAR[2], a), bl1(rw[3], STAR[3], a) local ba = bloom_alpha(x, y) if ba > 0 then r, g, b = bl1(r, bloom_r, ba), bl1(g, bloom_g, ba), bl1(b, bloom_b, ba) end px(x, y, r, g, b) else blend(x, y, STAR[1], STAR[2], STAR[3], a) end end end local CLOUDS = { { 7, 8, 15 }, { 13, 40, 19 }, { 4, 46, 11 } } local function draw_clouds(br, bg, bb, day, night) local by_night = night > day local cr, cg, cb, k if by_night then cr, cg, cb = mix(br, bg, bb, MOON[1], MOON[2], MOON[3], 0.30); k = (night - 0.5) * 1.4 else cr, cg, cb = mix(br, bg, bb, 255, 255, 255, 0.55); k = (day - 0.5) * 1.5 end if k <= 0.02 then return end for _, c in ipairs(CLOUDS) do for i = 0, c[3] - 1 do local a = k * sin(PI * (i + 0.5) / c[3]) blend(c[2] + i, c[1], cr, cg, cb, a) blend(c[2] + i, c[1] + 1, cr, cg, cb, a * 0.55) end end end local function draw_sun(bx, by, elev) local cr, cg, cb = 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, cr, cg, cb) elseif dist <= 8.0 then blend(bx + xx, by + yy, cr, cg, cb, 0.38 * (1.0 - (dist - 4.2) / 3.8)) end end end end end local function draw_moon(bx, by) for yy = -6, 6 do if by + yy < SKY_H then for xx = -6, 6 do local dist = sqrt(xx * xx + yy * yy) local cut = sqrt((xx + 3) * (xx + 3) + (yy - 1) * (yy - 1)) if cut > 4.0 then if dist <= 4.2 then px(bx + xx, by + yy, MOON[1], MOON[2], MOON[3]) elseif dist <= 6.2 then blend(bx + xx, by + yy, MOON[1], MOON[2], MOON[3], 0.22) end end end end end end local function win_at(len) local t = data.time return t.unix // len, t.unix % len + t.ms / 1000 end local function guest(len, dur, chance, salt) local win, s = win_at(len) local h = hash32(win * 0x9E3779B1 + salt) if umod(h, 4) >= chance then return nil end local p = (s - umod(h >> 8, (len - dur) * 10) / 10) / dur if p < 0 or p >= 1 then return nil end return p, hash32(h + 1) end local PLANE_R = { "#.......", "#######+", "..###..." } local PLANE_L = { ".......#", "+#######", "...###.." } local function draw_plane(f, night) local t, h = guest(60, 10, 3, 1) if not t then return end local to_right = h & 1 == 0 local x = lround(-10 + t * (W + 20)) if not to_right then x = W - 8 - x end local br, bg, bb = mix(225, 232, 245, 90, 100, 130, night * 0.7) local hr, hg, hb = mix(br, bg, bb, 255, 255, 255, 0.5) local map = to_right and PLANE_R or PLANE_L for r = 1, 3 do local row = map[r] for c = 1, 8 do local ch = row:sub(c, c) if ch == "#" then px(x + c - 1, 4 + r - 1, br, bg, bb) elseif ch == "+" then px(x + c - 1, 4 + r - 1, hr, hg, hb) end end end local tr_r, tr_g, tr_b = mix(br, bg, bb, 255, 255, 255, 0.3) for i = 1, 10 do local tx = to_right and (x - i) or (x + 7 + i) blend(tx, 5, tr_r, tr_g, tr_b, 0.42 * (1.0 - i / 11.0)) end if night > 0.5 and (f // (FPS // 4)) % 2 == 1 then px(to_right and x + 7 or x, 5, 255, 90, 70) end end local CAR = { "..+++..", ".#####.", "#######", ".-...-." } local CAR_BODY = { { 225, 80, 70 }, { 80, 150, 235 }, { 240, 200, 80 } } local function draw_street(night) local t, h = guest(20, 6, 3, 2) if not t then return end local to_right = h & 1 == 0 local x = lround(-8 + t * (W + 16)) if not to_right then x = W - 7 - x end local kb = CAR_BODY[umod(h >> 1, 3) + 1] local br, bg, bb = mix(kb[1], kb[2], kb[3], 30, 34, 60, night * 0.45) local y = SKY_H - 4 for r = 1, 4 do local row = CAR[r] for c = 1, 7 do local ch = row:sub(c, c) if ch == "#" then px(x + c - 1, y + r - 1, br, bg, bb) elseif ch == "+" then px(x + c - 1, y + r - 1, 190, 220, 245) elseif ch == "-" then px(x + c - 1, y + r - 1, 28, 28, 34) end end end px(to_right and x + 6 or x, y + 2, 255, 236, 170) px(to_right and x or x + 6, y + 2, 200, 60, 50) end local function draw_building(b, cr, cg, cb, rim, rr, rg, rb) local bx, bw, bh, typ = b[1], b[2], b[3], b[4] local ty = SKY_H - bh rect(bx, ty, bw, SKY_H - ty, cr, cg, cb) if typ == 1 then rect(bx + bw // 2, ty - 4, 1, 4, cr, cg, cb) elseif typ == 2 then rect(bx + 1, ty - 2, bw - 2, 2, cr, cg, cb) elseif typ == 3 then for i = 0, 2 do rect(bx + bw // 2 - i, ty - 3 + i, 2 * i + 1, 1, cr, cg, cb) end end if rim ~= 0 then local ex = (rim < 0) and bx or (bx + bw - 1) for y = ty, SKY_H - 1 do blend(ex, y, rr, rg, rb, 0.42) end for x = bx, bx + bw - 1 do blend(x, ty, rr, rg, rb, 0.28) end end end local function draw_skyline(br, bg, bb, day, night, bx, rim_on, f) local rr, rg, rb = mix(SUN_LO[1], SUN_LO[2], SUN_LO[3], SUN_HI[1], SUN_HI[2], SUN_HI[3], 0.35) local rim_dir = (bx < W // 2) and -1 or 1 local m1, m2, m3 = mix(br, bg, bb, 40, 40, 90, 0.72) local fr, fg, fb = mix(m1, m2, m3, 92, 122, 178, day * 0.6) for _, b in ipairs(FAR) do draw_building(b, fr, fg, fb, 0, rr, rg, rb) end local nr, ng, nb = mix(16, 12, 44, 46, 62, 104, day) for _, b in ipairs(NEAR) do local rim = (rim_on and abs(b[1] + b[2] / 2.0 - bx) < 34.0) and rim_dir or 0 draw_building(b, nr, ng, nb, rim, rr, rg, rb) if night > 0.45 then local ty = SKY_H - b[3] local wa = night * 0.95 for wy = ty + 2, SKY_H - 3, 3 do local wx = b[1] + 1 while wx + 1 < b[1] + b[2] - 1 do local hh = hash32(wy * W + wx) if umod(hh, 5) < 3 and (f // 100 + umod(hh >> 4, 9)) % 9 < 6 then blend(wx, wy, WINDOW[1], WINDOW[2], WINDOW[3], wa); blend(wx + 1, wy, WINDOW[1], WINDOW[2], WINDOW[3], wa) blend(wx, wy + 1, WINDOW[1], WINDOW[2], WINDOW[3], wa); blend(wx + 1, wy + 1, WINDOW[1], WINDOW[2], WINDOW[3], wa) end wx = wx + 3 end end end end end local function draw_comet(lr, lg, lb, sec) local head = floor(W * (sec / 60.0)) local dr, dg, db = mix(lr, lg, lb, 0, 0, 0, 0.82) local tr, tg, tb = mix(lr, lg, lb, 0, 0, 0, TRAIL_F) rect(0, SKY_H, W, 1, dr, dg, db) if head > 0 then rect(0, SKY_H, head, 1, tr, tg, tb) end for i = COMET_TAIL, 1, -1 do local x = head - i if x >= 0 then blend(x, SKY_H, 255, 190, 80, 1.0 - i / (COMET_TAIL + 1)) end end px(head, SKY_H, 255, 255, 255) end local RUN_S = 1.0 function render() clear(0, 0, 0) local t, s = data.time, data.sun if not (t and t.ok and s) then text_center(30, "LOADING", TIME_R, TIME_G, TIME_B) return end local h = t.h + t.m / 60.0 local tr, tg, tb, br, bg, bb = sky_colors(pal_hour(h, s)) local night = darkness(br, bg, bb) local day = 1.0 - night local arc, is_day = sun_arc(h, s.rise, s.set, s.up) local elev = sin(PI * arc) local bx = lround(4 + arc * (W - 9)) local by = lround(HORIZON - elev * (HORIZON - ARC_TOP)) local key = fmt("%d,%d,%d,%d,%d,%d,%s,%d,%.6f", tr, tg, tb, br, bg, bb, tostring(is_day), bx, elev) if key ~= sky_key then build_sky(tr, tg, tb, br, bg, bb, is_day, bx, elev); sky_key = key end blit_rgb(SKYR, 0, 0, W, SKY_H) local f = ((t.h * 60 + t.m) * 60 + t.s) * FPS + t.ms * FPS // 1000 if night > 0.15 then draw_stars(f, night) end draw_clouds(br, bg, bb, day, night) if is_day then draw_sun(bx, by, elev) else draw_moon(bx, by) end draw_plane(f, night) draw_skyline(br, bg, bb, day, night, bx, is_day and elev < 0.45, f) draw_street(night) local lr, lg, lb = mix(br, bg, bb, 255, 255, 255, 0.35) local k = data.arrive and math.min(1, frame / (RUN_S * FPS)) or 1 draw_comet(lr, lg, lb, (t.s + t.ms / 1000) * (1 - (1 - k) * (1 - k))) local pr, pg, pb = mix(12, 12, 40, 24, 32, 58, day) rect(0, SKY_H + 1, W, H - SKY_H - 1, pr, pg, pb) local hm if t.fmt12 then hm = fmt("%d:%02d", t.h12, t.m) else hm = fmt("%02d:%02d", t.h, t.m) end F_BLINK.hide = (t.s % 2 == 1) and find(hm, ":", 1, true) or nil text((W - text_width(hm, F_BIG)) // 2, 31, hm, TIME_R, TIME_G, TIME_B, F_BLINK) if t.fmt12 then text(W - 1, 39, t.pm and "PM" or "AM", TIME_R, TIME_G, TIME_B, AM_PM) end text_center(47, DAYS[(t.wday % 7) + 1], DAY_R, DAY_G, DAY_B, O_DAY) text_center(55, fmt("%02d.%02d.%04d", t.day, t.month, t.year), DATE_R, DATE_G, DATE_B, CELL) end