info = { type = "image", title = "Jingle bell", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "A golden bell swings and rings in the snow", api = 33, } local tr = ({ sk = { ["Background"] = "Pozadie", ["Snow"] = "Sneh" }, cs = { ["Background"] = "Pozadí", ["Snow"] = "Sníh" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bg", label = T("Background"), type = "color", default = "#1E2C5C" }, { key = "snow", label = T("Snow"), type = "bool", default = true }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin, cos, sqrt, exp, PI = math.floor, math.sin, math.cos, math.sqrt, math.exp, math.pi local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local OUT = { 46, 26, 8 } local GOLD = { 252, 196, 36 } local GOLD_L = { 255, 240, 140 } local GOLD_D = { 212, 136, 16 } local LIPC = { 236, 160, 20 } local BAND = { 214, 30, 44 } local BAND_L = { 255, 104, 100 } local BAND_D = { 156, 16, 32 } local CLAP, CLAP_D = { 150, 92, 20 }, { 98, 56, 10 } local PINE, PINE_D = { 30, 120, 62 }, { 18, 82, 44 } local CORD = { 190, 34, 44 } local DRIFT, DRIFT_L = { 196, 212, 240 }, { 246, 250, 255 } local SNOW, SNOW_D = { 240, 246, 255 }, { 168, 186, 226 } local SPARK = { 255, 252, 214 } local function c3(c) return c[1], c[2], c[3] end local function span(xa, xb, y, r, g, b) if xb <= xa then return end local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, xb - xa) return end local s0 = ia if xa > ia then blend(ia, y, r, g, b, ia + 1 - xa); s0 = ia + 1 end if ib > s0 then rect(s0, y, ib - s0, 1, r, g, b) end if xb > ib then blend(ib, y, r, g, b, xb - ib) end end local CX, PY = 32.5, 15 local PIVY = 3 local BW = { 2, 3, 4, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 10, 11, 12, 12 } local CLY = PY + 22 local BOW_ROWS = { ".111.......111.", "13221.....12231", "122221.1.122221", "142222121222241", "144222131222441", ".1442212122441.", ".....12421.....", "....1241421....", "...124...421...", "...121...121...", "...1.1...1.1...", } local BOW = (table.concat(BOW_ROWS):gsub("[.%d]", function(ch) return string.char(ch == "." and 0 or tonumber(ch)) end)) local BOWPAL = { 0, 0, 0, 92, 10, 22, 226, 36, 48, 255, 120, 120, 160, 20, 36 } local BOWR = {} for r = 1, #BOW_ROWS do BOWR[r] = BOW:sub(r * 15 - 14, r * 15) end local CYC = 7 * STEP_HZ local OM = 2 * PI / 1.3 local AMP = 0.28 local LAG = 0.15 local CLAPK, LIM = 1.6, 0.12 local HIT = 0.12 local CLL = 19 local function env(tt) if tt <= 0 then return 0 end return (1 - exp(-tt / 0.3)) * exp(-tt / 2.0) end local FX, FY, FV, FP, FD = {}, {}, {}, {}, {} for i = 1, 34 do local h = hash32(i * 0x9E3779B1 + 5) FX[i], FY[i] = umod(h, W), umod(h >> 8, H) FV[i] = 0.25 + umod(h >> 16, 30) / 100 FP[i] = umod(h >> 20, 628) / 100 FD[i] = FV[i] < 0.38 end local BG, SNOWON = nil, true local BGC = { 30, 44, 92 } function on_settings() local c = cfg.bg or { r = 30, g = 44, b = 92 } BGC = { c.r, c.g, c.b } SNOWON = cfg.snow ~= false BG = nil end local BALLS = { { 6, 4, 226, 48, 56 }, { 19, 5, 250, 200, 60 }, { 45, 5, 200, 214, 236 }, { 58, 4, 226, 48, 56 } } local function build_bg() rect(0, 0, W, H, c3(BGC)) rect(0, 0, W, 2, c3(PINE)) for x = 0, W - 1 do local len = 1 + umod(hash32(x * 0x9E3779B1 + 1), 3) if x % 2 == 0 then rect(x, 2, 1, len, c3(PINE)) else rect(x, 2, 1, len, c3(PINE_D)) end end for i = 1, #BALLS do local b = BALLS[i] rect(b[1], b[2], 2, 2, b[3], b[4], b[5]) px(b[1], b[2], 255, 250, 230) end if SNOWON then for x = 0, W - 1 do local top = 57 + floor(1.3 * sin(x * 0.21 + 0.5) + 0.8 * sin(x * 0.53) + 0.5) rect(x, top, 1, H - top, c3(DRIFT)) px(x, top, c3(DRIFT_L)) end end BG = gfx.snapshot(0, 0, W, H) end local function draw_cord(s) for y = PIVY, PY - 5 do local c = CX + (y + 0.5 - PIVY) * s span(c - 0.5, c + 0.5, y, c3(CORD)) end end local function draw_bow(s) for r = 1, #BOWR do local y = PY - 5 + r gfx.blit_indexed(BOWR[r], BOWPAL, 25 + floor((y + 0.5 - PIVY) * s + 0.5), y, 15, 1, 15, 1, true) end end local function draw_bell(s) local orr, org, orb = OUT[1], OUT[2], OUT[3] local y0 = PY + 2 for v = 0, 18 do local y = y0 + v local c = CX + (y + 0.5 - PIVY) * s local w = BW[v + 1] span(c - w - 1.5, c + w + 1.5, y, orr, org, orb) if v == 14 or v == 15 then span(c - w - 0.5, c + w + 0.5, y, c3(BAND)) rect(floor(c - w + 1.5), y, 1, 1, c3(BAND_L)) rect(floor(c + w - 1.5), y, 2, 1, c3(BAND_D)) elseif v >= 17 then span(c - w - 0.5, c + w + 0.5, y, c3(LIPC)) if v == 17 then rect(floor(c - w + 2.5), y, 2 * w - 5, 1, c3(GOLD_L)) end else span(c - w - 0.5, c + w + 0.5, y, c3(GOLD)) if v >= 2 then rect(floor(c - w + 1.5), y, 1, 1, c3(GOLD_L)) rect(floor(c + w - 1.5), y, 2, 1, c3(GOLD_D)) end end end local c = CX + (y0 + 19.5 - PIVY) * s span(c - 13.5, c + 13.5, y0 + 19, orr, org, orb) end local function draw_clapper(s, ph) local c = CX + (CLY - CLL + 0.5 - PIVY) * s + CLL * sin(ph) span(c - 1.5, c + 1.5, CLY, c3(CLAP)) span(c - 1, c + 1, CLY + 1, c3(CLAP_D)) end local function arc(ox, oy, r, sd, a) local n = floor(r * 0.75) local sr, sg, sb = GOLD_L[1], GOLD_L[2], GOLD_L[3] for dy = -n, n do local x = floor(ox + sd * sqrt(r * r - dy * dy) + 0.5) if a >= 1 then px(x, oy + dy, sr, sg, sb) else blend(x, oy + dy, sr, sg, sb, a) end end end local function draw_strike(tt, s) local q = (OM * (tt - HIT) - PI * 0.5) / PI if q < 0 then return end local k = floor(q) local tk = (PI * 0.5 + k * PI) / OM + HIT local age = (tt - tk) * STEP_HZ if age >= 14 then return end local str = env(tk) if str < 0.22 then return end local sd = (k % 2 == 0) and 1 or -1 local ox, oy = CX + (PY + 15.5 - PIVY) * s + sd * 13, PY + 15 local a = age < 7 and 1 or (14 - age) / 7 arc(ox, oy, 2 + age * 0.6, sd, a) if str > 0.45 and age > 4 then arc(ox, oy, age * 0.6 - 1, sd, a) end local n = 2 + floor(str * 4) local sr, sg, sb = SPARK[1], SPARK[2], SPARK[3] for i = 1, n do local hh = hash32(k * 0x9E3779B1 + i * 0x45D9F3B) local ang = (umod(hh, 100) / 100 - 0.6) * 2.2 local d = 2 + age * (0.7 + umod(hh >> 8, 50) / 100) local x = floor(ox + sd * cos(ang) * d + 0.5) local y = floor(oy - 2 + sin(ang) * d + 0.5) if age < 5 then px(x, y, sr, sg, sb) px(x - 1, y, c3(GOLD)); px(x + 1, y, c3(GOLD)); px(x, y - 1, c3(GOLD)); px(x, y + 1, c3(GOLD)) else blend(x, y, sr, sg, sb, a) end end end local function draw_snow(t) for i = 1, #FX do local y = (FY[i] + t * FV[i]) % (H + 2) - 1 local yi = floor(y) local f = y - yi local x = floor(FX[i] + 1.5 * sin(t * 0.035 + FP[i]) + 0.5) % W local c = FD[i] and SNOW_D or SNOW blend(x, yi, c[1], c[2], c[3], 1 - f) blend(x, yi + 1, c[1], c[2], c[3], f) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS if SNOWON then draw_snow(t) end local tt = ((t + CYC - 42) % CYC) / STEP_HZ local th = AMP * env(tt) * sin(OM * tt) local d = CLAPK * AMP * env(tt - LAG) * sin(OM * (tt - LAG)) - th if d > LIM then d = LIM elseif d < -LIM then d = -LIM end local s = sin(th) draw_cord(s) draw_clapper(s, th + d) draw_bell(s) draw_bow(s) draw_strike(tt, s) end