info = { type = "image", title = "Alarm clock 6:00", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Alarm rings at 6:00, a hand slaps it, dawn", api = 44, } local tr = ({ sk = { ["Wake-up hour"] = "Hodina budíčka", ["Display colour"] = "Farba displeja" }, cs = { ["Wake-up hour"] = "Hodina budíku", ["Display colour"] = "Barva displeje" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "hour", label = T("Wake-up hour"), type = "int", min = 2, max = 9, default = 6, unit = "h" }, { key = "led", label = T("Display colour"), type = "color", default = "#FF4A3C" }, } local rect, px, blend_rect, blit = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_indexed local floor, sin, cos, sqrt, min = math.floor, math.sin, math.cos, math.sqrt, math.min local W, H = gfx.W, gfx.H local TAU = 6.28318 local CYCLE = 630 local function ease(u) return u * u * (3 - 2 * u) end local function lit(r, g, b, k) return floor(r * (0.5 + 0.5 * k) + 0.5), floor(g * (0.55 + 0.45 * k) + 0.5), min(255, floor(b * (0.85 + 0.15 * k) + 20 * (1 - k) + 0.5)) end local function mix(a, b, k) return floor(a + (b - a) * k + 0.5) end local WALL_N, WALL_D = { 34, 38, 86 }, { 196, 150, 132 } local SKY_N = { { 6, 10, 40 }, { 24, 34, 86 } } local SKY_D = { { 84, 104, 176 }, { 255, 168, 96 } } local function grid(w, h) local g = {}; for i = 1, w * h do g[i] = 0 end; return g end local function put(g, w, x, y, i) if x >= 0 and x < w and y >= 0 then g[y * w + x + 1] = i end end local function chars(g, w, h) local rows = {} for y = 0, h - 1 do rows[y + 1] = string.char(table.unpack(g, y * w + 1, y * w + w)) end return table.concat(rows) end local function hand(g, w, cx, cy, deg, len, i) local a = deg * TAU / 360 for s = 0, len * 2 do put(g, w, floor(cx + sin(a) * s / 2 + 0.5), floor(cy - cos(a) * s / 2 + 0.5), i) end end local BW, BH = 21, 25 local function body(hour_deg, min_deg) local g = grid(BW, BH) for y = 0, 20 do for x = 0, 20 do local dx, dy = x - 10, y - 10 local d = sqrt(dx * dx + dy * dy) if d <= 10.4 then g[y * BW + x + 1] = d > 8.3 and (dx + dy >= 7 and 2 or 1) or 3 end end end for _, p in ipairs({ { 10, 4 }, { 16, 10 }, { 10, 16 }, { 4, 10 } }) do put(g, BW, p[1], p[2], 4) end hand(g, BW, 10, 10, hour_deg, 3.5, 4); hand(g, BW, 10, 10, min_deg, 5.5, 4) for k = 0, 6 do put(g, BW, 4 - k // 3, 17 + k, 7); put(g, BW, 16 + k // 3, 17 + k, 7) end return chars(g, BW, BH) end local LW, LH = 23, 8 local function bells() local g = grid(LW, LH) for _, c in ipairs({ 4, 18 }) do for y = 2, 6 do for x = c - 3, c + 3 do local dx, dy = x - c, y - 6 if dx * dx + dy * dy <= 11.5 then g[y * LW + x + 1] = (y == 6 or dx > 1) and 6 or 5 end end end end for y = 1, 5 do put(g, LW, 11, y, 6) end for x = 10, 12 do put(g, LW, x, 0, 5) end return chars(g, LW, LH) end local HW, HH = 7, 36 local function slapper() local g = grid(HW, HH) for y = 0, 25 do for x = 1, 5 do g[y * HW + x + 1] = (y // 3) % 2 == 0 and 1 or 2 end end for y = 26, 27 do for x = 1, 5 do g[y * HW + x + 1] = 3 end end for y = 28, 35 do local a = (y == 28 or y == 35) and 2 or (y == 29 or y == 34) and 1 or 0 for x = a, HW - 1 - a do g[y * HW + x + 1] = 4 end end for y = 31, 33 do for x = 2, 4, 2 do g[y * HW + x + 1] = 5 end end return chars(g, HW, HH) end local SEG = { [0] = "abcdef", "bc", "abdeg", "abcdg", "bcfg", "acdfg", "acdefg", "abc", "abcdefg", "abcdfg" } local SEGR = { a = { 1, 0, 3, 1 }, b = { 4, 1, 1, 3 }, c = { 4, 5, 1, 3 }, d = { 1, 8, 3, 1 }, e = { 0, 5, 1, 3 }, f = { 0, 1, 1, 3 }, g = { 1, 4, 3, 1 } } local DIGIT = {} local function build_digits() for d = 0, 9 do local g = grid(5, 9) for s in SEG[d]:gmatch(".") do local r = SEGR[s] for y = r[2], r[2] + r[4] - 1 do for x = r[1], r[1] + r[3] - 1 do g[y * 5 + x + 1] = 1 end end end DIGIT[d] = chars(g, 5, 9) end end local HOUR, LED = 6, { 255, 74, 60 } local BODY_A, BODY_B, BELLS, SLAP, DISP local DIGX = { 36, 45, 51 } function on_settings() HOUR = cfg.hour or 6 local c = cfg.led or { r = 255, g = 74, b = 60 } LED = { c.r, c.g, c.b } BODY_A, BODY_B, DISP = nil, nil, nil end local BASE_CLOCK = { { 0, 0, 0 }, { 214, 44, 52 }, { 150, 28, 40 }, { 250, 244, 232 }, { 44, 36, 44 }, { 236, 190, 70 }, { 170, 120, 40 }, { 70, 60, 76 } } local BASE_HAND = { { 0, 0, 0 }, { 96, 116, 206 }, { 70, 88, 170 }, { 240, 240, 250 }, { 238, 188, 156 }, { 200, 140, 112 } } local PAL_CLOCK, PAL_HAND, KQ = {}, {}, -1 local function palettes(kq) if kq == KQ then return end KQ = kq local k = kq / 32 for i, c in ipairs(BASE_CLOCK) do local r, g, b = lit(c[1], c[2], c[3], k) PAL_CLOCK[i * 3 - 2], PAL_CLOCK[i * 3 - 1], PAL_CLOCK[i * 3] = r, g, b end for i, c in ipairs(BASE_HAND) do local r, g, b = lit(c[1], c[2], c[3], k) PAL_HAND[i * 3 - 2], PAL_HAND[i * 3 - 1], PAL_HAND[i * 3] = r, g, b end end local FLIP, RING0, SLAP0 = 120, 126, 270 local function daylight(t) if t < 330 then return 0 end if t < 540 then return ease((t - 330) / 210) end if t < 600 then return 1 end return 1 - ease((t - 600) / 30) end local GLASS_X, GLASS_Y, GLASS_W, GLASS_H = 38, 5, 20, 21 local SKYSNAP, SKYK = nil, -1 local function sky(kq) if kq == SKYK then return end SKYK = kq local k = kq / 32 for y = 0, GLASS_H - 1 do local t = y / (GLASS_H - 1) local r = mix(mix(SKY_N[1][1], SKY_N[2][1], t), mix(SKY_D[1][1], SKY_D[2][1], t), k) local g = mix(mix(SKY_N[1][2], SKY_N[2][2], t), mix(SKY_D[1][2], SKY_D[2][2], t), k) local b = mix(mix(SKY_N[1][3], SKY_N[2][3], t), mix(SKY_D[1][3], SKY_D[2][3], t), k) rect(GLASS_X, GLASS_Y + y, GLASS_W, 1, r, g, b) end SKYSNAP = gfx.snapshot(GLASS_X, GLASS_Y, GLASS_W, GLASS_H) end local function sun(cy, k) local r = 4.2 local cr, cg, cb = 255, mix(206, 236, k), mix(120, 170, k) for y = GLASS_Y, GLASS_Y + GLASS_H - 1 do local dy = y + 0.5 - cy if dy * dy < r * r then local hw = sqrt(r * r - dy * dy) local x0 = 42.5 - hw local ix = floor(x0 + 0.5) if hw > 0.6 then rect(ix, y, floor(42.5 + hw + 0.5) - ix, 1, cr, cg, cb) end end end end local function digits(a, b, c, col, colon) local pal = { 0, 0, 0, col[1], col[2], col[3] } blit(DIGIT[a], pal, DIGX[1], 37, 5, 9, 5, 9, true) blit(DIGIT[b], pal, DIGX[2], 37, 5, 9, 5, 9, true) blit(DIGIT[c], pal, DIGX[3], 37, 5, 9, 5, 9, true) if colon then rect(42, 39, 2, 2, col[1], col[2], col[3]); rect(42, 43, 2, 2, col[1], col[2], col[3]) end end local function display_base() rect(33, 34, 27, 14, 26, 22, 30); rect(33, 34, 27, 1, 70, 62, 80) local dim = { 0, 0, 0, LED[1] // 4, LED[2] // 4, LED[3] // 4 } for i = 1, 3 do blit(DIGIT[8], dim, DIGX[i], 37, 5, 9, 5, 9, true) end rect(42, 39, 2, 2, dim[4], dim[5], dim[6]); rect(42, 43, 2, 2, dim[4], dim[5], dim[6]) DISP = gfx.snapshot(33, 34, 27, 14) end local SOUND_A = { { 5, 16 }, { 3, 19 }, { 2, 22 }, { 3, 25 } } local SOUND_B = { { 4, 14 }, { 2, 17 }, { 1, 21 }, { 2, 24 } } local function ready() if not DIGIT[0] then build_digits() elseif not BELLS then BELLS, SLAP = bells(), slapper() elseif not BODY_A then BODY_A = body((HOUR - 1) * 30 + 29.5, 354) elseif not BODY_B then BODY_B = body(HOUR * 30, 0) elseif not DISP then display_base() else return true end end function render() local ok = ready() local t = frame % CYCLE local k = daylight(t) local kq = floor(k * 32 + 0.5) k = kq / 32 palettes(kq) sky(kq) rect(0, 0, W, 50, mix(WALL_N[1], WALL_D[1], k), mix(WALL_N[2], WALL_D[2], k), mix(WALL_N[3], WALL_D[3], k)) gfx.blit_rgb(SKYSNAP, GLASS_X, GLASS_Y, GLASS_W, GLASS_H) if k > 0.05 then gfx.clip(GLASS_X, GLASS_Y, GLASS_W, GLASS_H) sun(29 - 13 * ease(min(1, (t - 330) / 210 + 0.0)), k) gfx.clip() end local hr, hg, hb = lit(40, 60, 70, k) rect(GLASS_X, 23, GLASS_W, 3, hr, hg, hb); rect(40, 22, 7, 1, hr, hg, hb); rect(50, 21, 6, 2, hr, hg, hb) if k < 0.5 then for i, s in ipairs({ { 40, 7 }, { 44, 12 }, { 52, 8 }, { 55, 16 }, { 42, 18 } }) do if (t // 20 + i) % 5 ~= 0 then px(s[1], s[2], 220, 224, 255) end end end local fr, fg, fb = lit(150, 106, 74, k) rect(36, 3, 24, 2, fr, fg, fb); rect(36, 26, 24, 2, fr, fg, fb); rect(36, 5, 2, 21, fr, fg, fb); rect(58, 5, 2, 21, fr, fg, fb) rect(47, 5, 2, 21, fr, fg, fb); rect(38, 14, 20, 1, fr, fg, fb) rect(34, 28, 28, 2, fr, fg, fb) local tr_, tg, tb = lit(160, 110, 72, k) rect(0, 50, W, 3, tr_, tg, tb) local br, bg, bb = lit(122, 82, 56, k) rect(0, 53, W, 11, br, bg, bb); rect(4, 55, 56, 7, tr_, tg, tb) rect(4, 55, 56, 1, br, bg, bb); rect(31, 58, 2, 2, lit(236, 190, 90, k)) if not ok then gfx.ready(false) return end local dx, dy, bx = 0, 0, 0 local ringing = t >= RING0 and t < SLAP0 if ringing then local i = t - RING0 local u = (i % 14) / 14 dy = -floor(12 * u * (1 - u) + 0.5) dx = ((i // 2) % 2) * 2 - 1 bx = (i % 2) * 2 - 1 elseif t >= SLAP0 and t < SLAP0 + 6 then dy = 1 end blit(t < FLIP and BODY_A or BODY_B, PAL_CLOCK, 7 + dx, 25 + dy, BW, BH, BW, BH, true) blit(BELLS, PAL_CLOCK, 6 + dx + bx, 18 + dy, LW, LH, LW, LH, true) if ringing then local set = ((t - RING0) // 3) % 2 == 0 and SOUND_A or SOUND_B for i = 1, 4 do px(set[i][1] + dx, set[i][2] + dy, 255, 214, 110); px(34 - set[i][1] + dx, set[i][2] + dy, 255, 214, 110) end end gfx.blit_rgb(DISP, 33, 34, 27, 14) local col = LED if (t >= FLIP and t < FLIP + 6) or (ringing and ((t - RING0) // 4) % 2 == 0) then col = { 255, 236, 200 } end if t < FLIP then digits(HOUR - 1, 5, 9, col, t % 30 < 18) else digits(HOUR, 0, 0, col, t % 30 < 18 or ringing) end local hy if t >= 240 and t < SLAP0 then local u = (t - 240) / 30 hy = -36 + 24 * u * u elseif t >= SLAP0 and t < SLAP0 + 6 then hy = -11 elseif t >= 285 and t < 315 then hy = -12 - 24 * ease((t - 285) / 30) elseif t >= SLAP0 and t < 285 then hy = -12 end if hy then blit(SLAP, PAL_HAND, 14, floor(hy), HW, HH, HW, HH, true) end end