info = { type = "image", title = "Wreath bells", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "Golden bells swing below a twinkling wreath", api = 44, } local tr = ({ sk = { ["Lights"] = "Svetlá", ["Colourful"] = "Farebná", ["Warm white"] = "Teplá biela", ["Swing"] = "Hojdanie" }, cs = { ["Lights"] = "Světla", ["Colourful"] = "Barevná", ["Warm white"] = "Teplá bílá", ["Swing"] = "Houpání" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "lights", label = T("Lights"), type = "int", default = 0, opts = { T("Colourful"), T("Warm white") } }, { key = "swing", label = T("Swing"), type = "int", min = 0, max = 3, default = 2 }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin, cos, sqrt, exp = math.floor, math.sin, math.cos, math.sqrt, math.exp local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local function c3(c) return c[1], c[2], c[3] end local WOOD1, WOOD2 = { 158, 98, 60 }, { 142, 86, 52 } local GAP, GRAIN = { 98, 58, 36 }, { 124, 74, 44 } local G_OUT, GREEN = { 22, 70, 40 }, { 50, 132, 64 } local G_LT, G_DK = { 104, 186, 88 }, { 32, 98, 50 } local BERRY, BERRY_D = { 222, 34, 52 }, { 150, 20, 40 } local BERRY_HI = { 255, 170, 180 } local RIB, RIB_D = { 214, 38, 50 }, { 150, 22, 38 } local RIB_L, RIB_O = { 255, 120, 120 }, { 92, 16, 28 } local GOLD, GOLD_L = { 238, 180, 42 }, { 255, 236, 128 } local GOLD_D, B_OUT = { 186, 120, 26 }, { 104, 58, 20 } local CX, CY, R, RI = 31.5, 26, 20, 10 local BG, STAGE = nil, 0 local function art(x0, y0, rows, pal) for r = 1, #rows do local s = rows[r] local n, i = #s, 1 while i <= n do local ch = s:byte(i) local j = i while j < n and s:byte(j + 1) == ch do j = j + 1 end local c = pal[ch] if c then rect(x0 + i - 1, y0 + r - 1, j - i + 1, 1, c[1], c[2], c[3]) end i = j + 1 end end end local BOW = { ".ooo.........ooo.", "oRRRoo.....ooRRRo", "oRLRRRo...oRRRLRo", "oRLRRRRo.oRRRRLRo", "oRRRRRRRoRRRRRRRo", "oDRRRRRoRoRRRRRDo", ".oDDDooRRRooDDDo.", "..ooooRRRRRoooo..", "....oRRooRRDo....", "...oRRo..oRRDo...", "..oRDo....oDRDo..", "..oDo......oDDo..", "...o........oo...", } local BOW_PAL = { [("o"):byte()] = RIB_O, [("R"):byte()] = RIB, [("D"):byte()] = RIB_D, [("L"):byte()] = RIB_L } local BERRIES = { { 200, 15 }, { 250, 16 }, { 330, 14 }, { 20, 16 }, { 60, 14 }, { 105, 15 }, { 150, 16 }, { 175, 13 }, { 300, 17 } } local BX, BY = {}, {} for i = 1, #BERRIES do local a = BERRIES[i][1] * TAU / 360 BX[i] = floor(CX + BERRIES[i][2] * cos(a) + 0.5) BY[i] = floor(CY + BERRIES[i][2] * sin(a) + 0.5) end local NL = 16 local LX, LY = {}, {} for i = 1, NL do local a = (i - 0.5) * TAU / NL local rr = (i % 2 == 0) and 12.5 or 17.5 LX[i] = floor(CX + rr * cos(a) + 0.5) LY[i] = floor(CY + rr * sin(a) + 0.5) end local function build_bg() for k = 0, 7 do rect(k * 8, 0, 8, H, c3((k % 2 == 0) and WOOD1 or WOOD2)) end for k = 1, 7 do rect(k * 8, 0, 1, H, c3(GAP)) end for i = 1, 40 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, H) if x % 8 ~= 0 then rect(x, y, 1, 2 + umod(h >> 16, 4), c3(GRAIN)) end end for pass = 1, 2 do for y = CY - R, CY + R do local dy = y - CY local ow = sqrt(R * R - dy * dy) + (umod(hash32(y * 0x85EBCA6B), 3) - 1) * 0.5 local a, b = floor(CX - ow + 0.5), floor(CX + ow + 0.5) local segs if dy * dy < RI * RI then local iw = sqrt(RI * RI - dy * dy) segs = { a, floor(CX - iw), floor(CX + iw) + 1, b } else segs = { a, b } end for k = 1, #segs, 2 do local l, r = segs[k], segs[k + 1] if r >= l then if pass == 1 then rect(l - 1, y - 1, r - l + 3, 3, c3(G_OUT)) else rect(l, y, r - l + 1, 1, c3(GREEN)) end end end end end BG = gfx.snapshot(0, 0, W, H) end local function build_bg2() for i = 1, 260 do local h = hash32(i * 0x9E3779B1) local a = umod(h, 6283) / 1000 local rr = RI + 1.8 + umod(h >> 13, 70) / 10 local x, y = floor(CX + rr * cos(a) + 0.5), floor(CY + rr * sin(a) + 0.5) if i % 2 == 0 then px(x, y, c3(G_LT)); px(x + 1, y - 1, c3(G_LT)) else px(x, y, c3(G_DK)); px(x - 1, y + 1, c3(G_DK)) end end for i = 1, #BERRIES do local x, y = BX[i], BY[i] rect(x, y, 2, 2, c3(BERRY)); px(x + 2, y + 1, c3(BERRY)); px(x + 1, y + 2, c3(BERRY_D)) px(x + 2, y + 2, c3(BERRY_D)); px(x - 1, y + 1, c3(BERRY_D)) end art(23, 1, BOW, BOW_PAL) BG = gfx.snapshot(0, 0, W, H) end local MULTI = { { 255, 70, 70 }, { 255, 214, 70 }, { 80, 170, 255 }, { 130, 255, 130 }, { 255, 130, 220 } } local WARM = { { 255, 214, 140 } } local LCOL, LDIM = {}, {} function on_settings() local set = (cfg.lights == 1) and WARM or MULTI for i = 1, NL do local c = set[(i - 1) % #set + 1] LCOL[i] = c LDIM[i] = { c[1] * 45 // 100, c[2] * 45 // 100, c[3] * 45 // 100 } end end local function lights(t) for i = 1, NL do local lv = umod(hash32(i * 0x9E3779B1 + ((t + i * 5) // 7) * 0x85EBCA6B), 5) local x, y = LX[i], LY[i] if lv == 0 then px(x, y, c3(LDIM[i])) else local c = LCOL[i] px(x, y, c[1], c[2], c[3]) if lv >= 3 then blend(x - 1, y, c[1], c[2], c[3], 0.35); blend(x + 1, y, c[1], c[2], c[3], 0.35) blend(x, y - 1, c[1], c[2], c[3], 0.35); blend(x, y + 1, c[1], c[2], c[3], 0.35) end end end end local function berries(t) for i = 1, #BERRIES do if sin(t * 0.09 + i * 2.1) > 0.55 then px(BX[i], BY[i], c3(BERRY_HI)) end end end local BELL = { 1, 2, 3, 3, 3, 3, 4, 4, 5, 5 } local RIBL = 5 local function brow(xc, y, hw, c, hi) local n = 2 * hw + 3 local L = xc - hw - 1 local ix = floor(L) local f = L - ix rect(ix + 1, y, n - 1, 1, c[1], c[2], c[3]) if hi then blend(ix + 2, y, GOLD_L[1], GOLD_L[2], GOLD_L[3], 1 - f) blend(ix + 3, y, GOLD_L[1], GOLD_L[2], GOLD_L[3], f) end blend(ix, y, B_OUT[1], B_OUT[2], B_OUT[3], 1 - f) blend(ix + 1, y, B_OUT[1], B_OUT[2], B_OUT[3], f) blend(ix + n - 1, y, B_OUT[1], B_OUT[2], B_OUT[3], 1 - f) blend(ix + n, y, B_OUT[1], B_OUT[2], B_OUT[3], f) end local P, OMEGA = 200, TAU / 40 local function angle(u, A) if u < 0 then return 0 end local env = (u < 10) and (u / 10) or 1 return A * env * exp(-u / 70) * sin(OMEGA * u) end local function bell(pxv, pyv, u, A) local th = angle(u, A) local s = sin(th) for d = 0, RIBL - 1 do local x = pxv + s * d local ix = floor(x) local f = x - ix blend(ix, pyv + d, RIB[1], RIB[2], RIB[3], 1 - f) blend(ix + 1, pyv + d, RIB[1], RIB[2], RIB[3], f) end local top = pyv + RIBL brow(pxv + s * (RIBL - 1), top - 1, 0, B_OUT, false) for k = 1, #BELL do local d = RIBL + k - 1 local c = (k >= #BELL - 1) and GOLD_D or GOLD brow(pxv + s * d, top + k - 1, BELL[k], c, k >= 3 and k <= #BELL - 2) end local dl = RIBL + #BELL brow(pxv + s * dl, top + #BELL, 4, B_OUT, false) local sc = sin(angle(u - 5, A) * 1.2) local x = pxv + sc * (dl + 1) local ix = floor(x) local f = x - ix for y = top + #BELL + 1, top + #BELL + 2 do blend(ix, y, GOLD_D[1], GOLD_D[2], GOLD_D[3], 1 - f) blend(ix + 1, y, GOLD_D[1], GOLD_D[2], GOLD_D[3], f) end end function render() if STAGE == 0 then build_bg(); STAGE = 1; gfx.ready(false) elseif STAGE == 1 then gfx.blit_rgb(BG, 0, 0, W, H); build_bg2(); STAGE = 2 end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS lights(t) berries(t) local A = (cfg.swing or 2) * 0.11 bell(23, 42, (t + 40) % P, A) bell(40, 42, (t + 34) % P, A * 0.8) end