info = { type = "image", title = "Snow globe shake", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "A hand shakes a snow globe at an evening window", api = 44, } local tr = ({ sk = { ["Inside"] = "Vnútri", ["House"] = "Domček", ["Fir tree"] = "Jedlička", ["Snowman"] = "Snehuliak" }, cs = { ["Inside"] = "Uvnitř", ["House"] = "Domek", ["Fir tree"] = "Jedlička", ["Snowman"] = "Sněhulák" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "motif", label = T("Inside"), type = "int", default = 0, opts = { T("House"), T("Fir tree"), T("Snowman") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, exp, abs, max, min = math.floor, math.sin, math.cos, math.sqrt, math.exp, math.abs, math.max, math.min local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local function lerp(a, b, t) return a + (b - a) * t end local function rnd(v) return floor(v + 0.5) end local function hs(a, b) return hash32(a * 7919 + b * 104729) & 0x7FFF end local function sm(x) x = max(0, min(1, x)); return x * x * (3 - 2 * x) end local CX, CY, R = 31.5, 28.5, 17 local GX0, GY0, GW, GH = 14, 11, 36, 46 local KEY = { 0, 0, 0 } local GROUND = 37 local MOTIF_NOW, GLOBE, BG local function ground_y(x) local d = (x - CX) / 14.7 return GROUND - 1.6 * max(0, 1 - d * d) end local function motif_house() rect(26, 30, 12, 8, 196, 98, 84) rect(34, 30, 4, 8, 156, 70, 76) rect(27, 22, 2, 4, 150, 66, 70) for k = 0, 6 do rect(31 - k, 22 + k, 2 * k + 2, 1, k < 2 and 246 or 92, k < 2 and 250 or 56, k < 2 and 255 or 92) end rect(25, 29, 14, 1, 70, 42, 78) for k = 2, 6 do rect(31 - k, 22 + k, 1, 1, 236, 244, 255); rect(32 + k, 22 + k, 1, 1, 236, 244, 255) end rect(28, 31, 4, 4, 255, 214, 112) rect(29, 32, 2, 2, 255, 238, 170) rect(30, 31, 1, 4, 150, 80, 64); rect(28, 33, 4, 1, 150, 80, 64) rect(34, 33, 2, 5, 92, 46, 56) end local function motif_tree() local rows = { { 21, 1 }, { 22, 3 }, { 23, 5 }, { 24, 3 }, { 25, 5 }, { 26, 7 }, { 27, 9 }, { 28, 5 }, { 29, 7 }, { 30, 9 }, { 31, 11 }, { 32, 13 }, { 33, 7 }, { 34, 9 }, { 35, 11 }, { 36, 13 }, { 37, 15 } } for _, r in ipairs(rows) do local w = r[2] rect(rnd(32 - w / 2), r[1], w, 1, 28, 104, 76) rect(rnd(32 - w / 2), r[1], max(1, w // 3), 1, 54, 140, 96) end for _, s in ipairs({ { 31, 22 }, { 29, 26 }, { 33, 30 }, { 28, 34 }, { 35, 36 } }) do rect(s[1], s[2], 2, 1, 246, 250, 255) end rect(31, 38, 2, 2, 110, 70, 48) rect(31, 19, 2, 2, 255, 226, 96); px(31, 18, 255, 240, 160); px(32, 21, 255, 240, 160) end local function motif_snowman() local function ball(cx, cy, r, c1, c2) for y = cy - r, cy + r do local hw = sqrt(r * r - (y - cy) * (y - cy) + 0.5) local x0, x1 = rnd(cx - hw), rnd(cx + hw) rect(x0, y, x1 - x0 + 1, 1, c1[1], c1[2], c1[3]) rect(x1 - 1, y, 2, 1, c2[1], c2[2], c2[3]) end end rect(23, 26, 4, 1, 96, 60, 40); rect(36, 25, 4, 1, 96, 60, 40) px(22, 25, 96, 60, 40); px(40, 24, 96, 60, 40) ball(31.5, 35, 5, { 246, 250, 255 }, { 190, 208, 240 }) ball(31.5, 28, 4, { 246, 250, 255 }, { 190, 208, 240 }) ball(31.5, 22, 3, { 250, 252, 255 }, { 196, 212, 242 }) rect(29, 25, 6, 1, 214, 40, 60) rect(33, 26, 2, 3, 214, 40, 60) rect(28, 18, 8, 1, 36, 32, 52); rect(29, 14, 6, 4, 36, 32, 52) rect(29, 17, 6, 1, 190, 50, 70) px(30, 21, 30, 30, 40); px(33, 21, 30, 30, 40) rect(31, 22, 3, 1, 255, 140, 40) px(31, 28, 30, 30, 40); px(31, 31, 30, 30, 40) end local MOTIFS = { motif_house, motif_tree, motif_snowman } local GSTEP = 1 local function build_globe(m) if GSTEP == 1 then gfx.clear(KEY[1], KEY[2], KEY[3]) for y = 44, 56 do local w = 2 * rnd(11 + (y - 44) * 0.42) local x0 = 32 - w // 2 local r, g, b = 146, 36, 70 if y >= 47 and y <= 48 then r, g, b = 232, 180, 70 elseif y == 56 then r, g, b = 84, 20, 44 elseif y <= 46 then r, g, b = 190, 150, 70 end rect(x0, y, w, 1, r, g, b) if y >= 49 and y < 56 then rect(x0, y, w // 4, 1, 176, 52, 88) rect(x0 + w - w // 4, y, w // 4, 1, 108, 26, 56) elseif y == 47 then rect(x0, y, w, 1, 255, 224, 124) end end for y = 12, 45 do local dy = y - CY local hw = sqrt(R * R - dy * dy) local x0, x1 = floor(CX - hw + 0.5), floor(CX + hw - 0.5) local t = min((y - 12) / 25, 1) rect(x0, y, x1 - x0 + 1, 1, rnd(lerp(66, 150, t)), rnd(lerp(112, 196, t)), rnd(lerp(212, 248, t))) end GSTEP = 2 elseif GSTEP == 2 then for x = 15, 48 do local iy = rnd(ground_y(x)) for y = iy, 45 do local dx, dy = x - CX, y - CY if dx * dx + dy * dy <= R * R - 4 then local t = (y - iy) / 8 rect(x, y, 1, 1, rnd(lerp(248, 178, t)), rnd(lerp(251, 200, t)), 255) end end end MOTIFS[m + 1]() for x = 20, 43 do local gy = rnd(ground_y(x)) px(x, gy, 248, 251, 255) if x > 24 and x < 40 then px(x, gy - 1, 248, 251, 255) end end GSTEP = 3 else local ro, ri = (R + 0.4) * (R + 0.4), (R - 1.1) * (R - 1.1) for y = 11, 46 do local dy = y - CY local ho2, hi2 = ro - dy * dy, ri - dy * dy if ho2 > 0 then local ho = sqrt(ho2) local hi = hi2 > 0 and sqrt(hi2) or -1 for x = floor(CX - ho + 0.5), floor(CX + ho) do local dx = x - CX if abs(dx) > hi then local t = max(0, min(1, (dx + dy) / (R * 1.6) * 0.5 + 0.5)) px(x, y, rnd(lerp(236, 130, t)), rnd(lerp(248, 164, t)), rnd(lerp(255, 222, t))) end end end end GLOBE = gfx.snapshot(GX0, GY0, GW, GH) GSTEP, MOTIF_NOW = 0, m end end local function build_room() rect(0, 0, W, H, 88, 70, 108) rect(4, 2, 56, 44, 58, 40, 56) for y = 4, 43 do local t = (y - 4) / 40 rect(6, y, 52, 1, rnd(lerp(28, 176, t * t)), rnd(lerp(36, 130, t)), rnd(lerp(104, 182, t))) end rect(6, 22, 52, 2, 58, 40, 56) for _, h in ipairs({ { 6, 12, 34 }, { 17, 9, 36 }, { 45, 11, 35 }, { 51, 8, 32 } }) do rect(h[1], h[3], h[2], 44 - h[3], 40, 36, 78) rect(h[1], h[3] - 1, h[2], 1, 206, 218, 246) if h[2] > 8 then rect(h[1] + 3, h[3] + 3, 1, 2, 255, 200, 110) end end for _, c in ipairs({ { 9, 7 }, { 10, 7 }, { 8, 8 }, { 9, 8 }, { 8, 9 }, { 8, 10 }, { 9, 10 }, { 9, 11 }, { 10, 11 } }) do px(c[1], c[2], 236, 238, 252) end for _, s in ipairs({ { 20, 7 }, { 43, 6 }, { 52, 12 }, { 36, 14 }, { 12, 17 }, { 50, 19 } }) do px(s[1], s[2], 210, 216, 246) end rect(4, 44, 56, 2, 150, 110, 86) rect(0, 46, W, 18, 138, 90, 62) rect(0, 46, W, 1, 168, 118, 84) for _, y in ipairs({ 51, 57, 62 }) do rect(0, y, W, 1, 112, 70, 50) end for _, p in ipairs({ { 12, 47 }, { 40, 52 }, { 22, 58 }, { 52, 63 }, { 4, 53 } }) do rect(p[1], p[2], 1, 4, 112, 70, 50) end BG = gfx.snapshot(0, 0, W, H) end local NF = 56 local FX, FY, FR, FPHI, FG, FW, FPH, FRX, FRY = {}, {}, {}, {}, {}, {}, {}, {}, {} for i = 1, NF do local a, r = TAU * (hs(i, 1) % 1000) / 1000, 2 + (hs(i, 2) % 1000) / 1000 * 11 FR[i], FPHI[i] = r, a FX[i], FY[i] = CX + r * cos(a), 13 + (hs(i, 7) % 1000) / 1000 * 19 FG[i] = 1.8 + (hs(i, 3) % 100) / 100 * 1.8 FW[i] = 0.7 + (hs(i, 4) % 100) / 100 * 0.6 FPH[i] = TAU * (hs(i, 5) % 100) / 100 FRX[i] = CX - 14 + (hs(i, 6) % 28) FRY[i] = ground_y(FRX[i]) end local OX, OY, OP = {}, {}, {} for i = 1, 14 do OX[i], OY[i], OP[i] = hs(i, 11) % 60, hs(i, 12) % 40, hs(i, 13) % 100 end local function flake(x, y, a) local ix, iy = rnd(x), rnd(y) local ox, oy = x - ix, y - iy blend(ix, iy, 255, 255, 255, a) if abs(oy) > 0.2 then blend(ix, iy + (oy > 0 and 1 or -1), 255, 255, 255, 0.7 * abs(oy)) end if abs(ox) > 0.2 then blend(ix + (ox > 0 and 1 or -1), iy, 255, 255, 255, 0.7 * abs(ox)) end end local CYCLE = 16 * FPS function on_settings() MOTIF_NOW = nil; GSTEP = 1 end function render() if not BG then build_room(); gfx.ready(false) return end if GSTEP > 0 then build_globe(cfg.motif or 0); gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) local t = (frame % CYCLE) / FPS local T0, T1 = 2.6, 4.6 for i = 1, 14 do local y = (OY[i] + 7 * (frame / FPS) + OP[i]) % 40 local x = OX[i] + 3 * sin(frame / FPS * 0.9 + OP[i]) if y < 40 then local iy = floor(y) blend(rnd(x) + 6, iy + 4, 236, 240, 255, 0.5 * (1 - (y - iy))) blend(rnd(x) + 6, iy + 5, 236, 240, 255, 0.5 * (y - iy)) end end local gx, gy = 0, 0 if t >= T0 and t < T1 then local env = min(1, (t - T0) / 0.25) * (1 - (t - T0) / (T1 - T0) * 0.6) gx = rnd(env * 3.4 * sin(TAU * 2.7 * (t - T0))) gy = rnd(env * 0.9 * sin(TAU * 5.4 * (t - T0))) end blend_rect(15 + gx, 55, 34, 3, 40, 22, 20, 0.3) blend_rect(18 + gx, 58, 28, 1, 40, 22, 20, 0.16) gfx.blit_rgb(GLOBE, GX0 + gx, GY0 + gy, GW, GH, KEY[1], KEY[2], KEY[3]) local fl = 0.2 + 0.05 * sin(t * 7.3) + 0.03 * sin(t * 12.9) if MOTIF_NOW == 0 then blend_rect(27 + gx, 37 + gy, 10, 1, 255, 206, 120, fl) blend_rect(29 + gx, 38 + gy, 6, 1, 255, 206, 120, fl * 0.7) elseif MOTIF_NOW == 1 then local c = floor(t * 2.4) % 3 local lc = { { 255, 80, 90 }, { 255, 226, 100 }, { 110, 190, 255 } } for k, p in ipairs({ { 32, 23 }, { 30, 28 }, { 34, 28 }, { 28, 33 }, { 36, 33 }, { 31, 36 } }) do local q = lc[(k + c) % 3 + 1] px(p[1] + gx, p[2] + gy, q[1], q[2], q[3]) end end local tau = t - T1 for i = 1, NF do local x, y, vis local jx = 2.2 * sin(9 * t + FPH[i]) local jy = 1.6 * sin(11 * t + FPH[i] * 2) if t >= T0 and t < T1 then local s = sm((t - T0) / 0.7) x, y, vis = lerp(FRX[i], FX[i], s) + jx * s, lerp(FRY[i], FY[i], s) + jy * s, s > 0.02 elseif t >= T1 then local j = max(0, 1 - tau / 1.4) local th = FW[i] * 2.2 * 2.5 * (1 - exp(-tau / 2.5)) x = CX + FR[i] * cos(FPHI[i] + th) + jx * j y = FY[i] + jy * j + FG[i] * tau + 0.8 * sin(tau * 1.7 + FPH[i]) * exp(-tau / 6) vis = y < ground_y(x) - 0.3 end if vis then local ddx, ddy = x - CX, y - CY local d = sqrt(ddx * ddx + ddy * ddy) if d > 14.5 then x, y = CX + ddx * 14.5 / d, CY + ddy * 14.5 / d end flake(x + gx, y + gy, 0.95) end end for _, p in ipairs({ { 21, 18 }, { 22, 16 }, { 23, 15 }, { 25, 13 }, { 27, 13 } }) do blend(p[1] + gx, p[2] + gy, 255, 255, 255, 0.8) end blend(20 + gx, 21 + gy, 255, 255, 255, 0.5); blend(21 + gx, 19 + gy, 255, 255, 255, 0.6) local off = 30 if t >= 1.2 and t < T0 then off = 30 * (1 - sm((t - 1.2) / 1.2)) elseif t >= T0 and t < 4.6 then off = 0 elseif t >= 4.6 and t < 5.9 then off = 30 * sm((t - 4.6) / 1.3) end if off < 29.5 then local hx, hy = 41 + rnd(off) + (off == 0 and gx or 0), 47 + (off == 0 and gy or 0) local grip = off < 2 for k = 0, 9 do rect(hx + 11, hy - 1 + k, W - hx, 1, k < 2 and 224 or 190, k < 2 and 84 or 48, k < 2 and 96 or 66) end rect(hx + 9, hy, 2, 8, 238, 232, 224) rect(hx + 3, hy + 1, 7, 7, 244, 190, 152) rect(hx + 3, hy + 6, 7, 2, 214, 148, 118) for k = 0, 3 do rect(hx + (grip and 0 or 1) - 0, hy + 1 + k * 2, grip and 4 or 3, 1, 244, 190, 152) end for k = 0, 3 do rect(hx + (grip and 0 or 1), hy + 2 + k * 2, grip and 4 or 3, 1, 206, 140, 112) end rect(hx + 3, hy - 1, 5, 2, 250, 200, 164) end end