info = { type = "image", title = "Dancing duckling", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Fluffy duckling dancing among soap bubbles", api = 33, } local tr = ({ sk = { ["Dance speed"] = "Rýchlosť tanca", ["Bubbles"] = "Bubliny", ["Backdrop"] = "Kulisa", ["Sunny yellow"] = "Slnečná žltá", ["Peach"] = "Broskyňová", ["Mint"] = "Mätová", ["Sky blue"] = "Nebeská modrá" }, cs = { ["Dance speed"] = "Rychlost tance", ["Bubbles"] = "Bubliny", ["Backdrop"] = "Kulisa", ["Sunny yellow"] = "Slunečná žluť", ["Peach"] = "Broskvová", ["Mint"] = "Mátová", ["Sky blue"] = "Nebeská modř" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Dance speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "bubbles", label = T("Bubbles"), type = "int", min = 0, max = 12, default = 7 }, { key = "back", label = T("Backdrop"), type = "int", default = "yellow", opts = { keys = { "yellow", "peach", "mint", "sky" }, T("Sunny yellow"), T("Peach"), T("Mint"), T("Sky blue") } }, } local rect, px, blend_rect, blend = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend local blit_rgb, blit_indexed, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.snapshot local floor, ceil, sin, cos, sqrt, min, max = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local char, unpack, concat = string.char, table.unpack, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PI = 3.14159265 local YK = { { 0, 190, 108, 4 }, { 0.35, 240, 162, 14 }, { 0.6, 252, 198, 32 }, { 0.8, 255, 222, 70 }, { 1, 255, 248, 190 } } local OK = { { 0, 168, 62, 4 }, { 0.5, 236, 112, 12 }, { 0.8, 255, 152, 32 }, { 1, 255, 208, 126 } } local function ramp(keys, t) for i = 1, #keys - 1 do local a, b = keys[i], keys[i + 1] if t <= b[1] then local u = (t - a[1]) / (b[1] - a[1]) return a[2] + (b[2] - a[2]) * u, a[3] + (b[3] - a[3]) * u, a[4] + (b[4] - a[4]) * u end end local l = keys[#keys] return l[2], l[3], l[4] end local PAL = { 0, 0, 0 } for j = 0, 3 do for k = 0, 19 do local r, g, b = ramp(YK, k / 19) local a = j * 0.17 PAL[#PAL + 1] = floor(r + (255 - r) * a + 0.5) PAL[#PAL + 1] = floor(g + (112 - g) * a + 0.5) PAL[#PAL + 1] = floor(b + (72 - b) * a + 0.5) end end for k = 0, 19 do local r, g, b = ramp(OK, k / 19) PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end for _, c in ipairs({ 150, 34, 28, 232, 96, 88 }) do PAL[#PAL + 1] = c end local MOUTH, TONGUE = 101, 102 local LX, LY, LZ = -0.46, -0.58, 0.67 local HX, HY, HZ = -0.252, -0.317, 0.914 local function shade(nx, ny, nz, spec) local d = (nx * LX + ny * LY + nz * LZ + 0.3) / 1.3 if d < 0 then d = 0 end local t = 0.2 + 0.6 * d if ny > 0.35 then t = t + 0.22 * (ny - 0.35) * (1 - d) end local s = nx * HX + ny * HY + nz * HZ if s > 0.8 then local s2 = s * s; s2 = s2 * s2; s2 = s2 * s2; s2 = s2 * s2 t = t + spec * 0.55 * s2 end t = t * (0.74 + 0.26 * nz) if t > 1 then t = 1 elseif t < 0 then t = 0 end return floor(t * 19 + 0.5) end local function grid(w, h) local g = {} for i = 1, w * h do g[i] = 0 end return g end local function ell(g, w, h, cx, cy, rx, ry, ang, bi, spec, occ, flat, ya, yb) local c, s = cos(ang), sin(ang) local ex, ey = sqrt((rx * c) ^ 2 + (ry * s) ^ 2), sqrt((rx * s) ^ 2 + (ry * c) ^ 2) for y = max(ya or 0, floor(cy - ey)), min(yb or h - 1, ceil(cy + ey)) do for x = max(0, floor(cx - ex)), min(w - 1, ceil(cx + ex)) do local dx, dy = x + 0.5 - cx, y + 0.5 - cy local u, v = (dx * c + dy * s) / rx, (dy * c - dx * s) / ry local r2 = u * u + v * v if r2 <= 1 then if flat then g[y * w + x + 1] = flat else local nz = sqrt(1 - r2) local k = shade(u * c - v * s, u * s + v * c, nz, spec) if occ then k = max(0, k - floor(occ(x, y) * 19 + 0.5)) end g[y * w + x + 1] = bi + k end end end end end local function blush(g, w, cx, cy, rx, ry) for y = floor(cy - ry), ceil(cy + ry) do for x = floor(cx - rx), ceil(cx + rx) do local i = y * w + x + 1 local v = g[i] if v and v >= 1 and v <= 80 then local dx, dy = (x + 0.5 - cx) / rx, (y + 0.5 - cy) / ry local j = min(3, floor((1 - dx * dx - dy * dy) * 4)) local cur = (v - 1) // 20 if j > cur then g[i] = v + 20 * (j - cur) end end end end end local function slices(g, w, h, cuts) local out = { w = w } for i = 1, #cuts do local a, b = cuts[i], (cuts[i + 1] or h) - 1 local rows = {} for y = a, b do rows[#rows + 1] = char(unpack(g, y * w + 1, y * w + w)) end out[i] = { s = concat(rows), y = a, h = b - a + 1 } end return out end local HEAD, BODY, FOOT = nil, nil, nil local WING, BEAK = { {}, {} }, {} local HG, BGRID local function head_a() HG = grid(28, 27) ell(HG, 28, 27, 14, 15.5, 12, 10.5, 0, 1, 1, nil, nil, 0, 12) end local function head_b() local w, h = 28, 27 ell(HG, w, h, 14, 15.5, 12, 10.5, 0, 1, 1, nil, nil, 13, h - 1) ell(HG, w, h, 15.4, 3.4, 1.5, 3.4, 0.35, 1, 0.6) ell(HG, w, h, 12.6, 4.2, 1.2, 2.6, -0.3, 1, 0.6) blush(HG, w, 5.8, 19.8, 3.9, 2.7) blush(HG, w, 22.2, 19.8, 3.9, 2.7) HEAD = slices(HG, w, h, { 0, 10, 20 }) end local function chest_shadow(x, y) local u = (x - 15) / 12 local yb = -5.5 + 10.5 * sqrt(max(0, 1 - u * u)) return 0.34 * max(0, min(1, 1 - (y - yb) / 6)) end local function body_part(ya, yb) if not BGRID then BGRID = grid(30, 30) end ell(BGRID, 30, 30, 15, 15, 12.8, 13.8, 0, 1, 0.6, chest_shadow, nil, ya, yb) if yb == 29 then BODY = slices(BGRID, 30, 30, { 0, 6, 12, 18, 24 }) end end local function build_foot() local w, h = 11, 5 local g = grid(w, h) ell(g, w, h, 5.5, 2, 5.2, 1.8, 0, 81, 0.3) ell(g, w, h, 2, 3, 1.6, 1.3, 0, 81, 0.3) ell(g, w, h, 5.5, 3.2, 1.6, 1.3, 0, 81, 0.3) ell(g, w, h, 9, 3, 1.6, 1.3, 0, 81, 0.3) FOOT = slices(g, w, h, { 0 })[1] FOOT.w = w end local NWING = 7 local function wing(side, i) local sp = WING[side][i] if sp then return sp end local phi = (-50 + 18 * (i - 1)) * PI / 180 local dx, dy = side == 1 and -cos(phi) or cos(phi), -sin(phi) local rx, ry = 5.4, 2.9 local ang = math.atan(dy, dx) local c, s = cos(ang), sin(ang) local ex, ey = sqrt((rx * c) ^ 2 + (ry * s) ^ 2), sqrt((rx * s) ^ 2 + (ry * c) ^ 2) local cx, cy = 3.8 * dx, 3.8 * dy local x0, y0 = floor(cx - ex), floor(cy - ey) local w, h = ceil(cx + ex) - x0 + 1, ceil(cy + ey) - y0 + 1 local g = grid(w, h) ell(g, w, h, cx - x0, cy - y0, rx, ry, ang, 21, 0.5) sp = slices(g, w, h, { 0 })[1] sp.w, sp.ox, sp.oy = w, x0, y0 WING[side][i] = sp return sp end local function beak(k) local sp = BEAK[k] if sp then return sp end local w, h = 11, 10 local g = grid(w, h) if k == 0 then ell(g, w, h, 5.5, 5.3, 3, 1.3, 0, 81, 0.3) ell(g, w, h, 5.5, 3, 4.6, 2.5, 0, 81, 0.5) elseif k == 1 then ell(g, w, h, 5.5, 6.4, 3.2, 1.7, 0, 81, 0.3) ell(g, w, h, 5.5, 5.2, 2.9, 1.4, 0, 0, 0, nil, MOUTH) ell(g, w, h, 5.5, 2.8, 4.6, 2.4, 0, 81, 0.5) else ell(g, w, h, 5.5, 7.6, 3.4, 1.9, 0, 81, 0.3) ell(g, w, h, 5.5, 5.6, 3.3, 2.5, 0, 0, 0, nil, MOUTH) ell(g, w, h, 5.5, 7, 1.9, 1, 0, 0, 0, nil, TONGUE) ell(g, w, h, 5.5, 2.4, 4.6, 2.3, 0, 81, 0.5) end sp = slices(g, w, h, { 0 })[1] sp.w = w BEAK[k] = sp return sp end local STAGES = { head_a, head_b, function() body_part(0, 9) end, function() body_part(10, 19) end, function() body_part(20, 29) end, build_foot, } local STAGE = 1 local BACKS = { yellow = { { 250, 184, 10 }, { 240, 142, 6 }, { 255, 218, 70 } }, peach = { { 252, 172, 152 }, { 238, 118, 98 }, { 255, 218, 190 } }, mint = { { 74, 204, 172 }, { 34, 150, 132 }, { 156, 238, 210 } }, sky = { { 84, 164, 242 }, { 40, 108, 214 }, { 164, 218, 255 } }, } local BG = nil local function bake_bg() local B = BACKS[cfg.back] or BACKS.yellow local a, b, gl = B[1], B[2], B[3] for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5)) end for ring = 1, 10 do local r = 36 - ring * 2.4 for y = 0, H - 1, 2 do local u = (y + 1 - 26) / r if u > -1 and u < 1 then local hw = floor(r * sqrt(1 - u * u)) blend_rect(32 - hw, y, 2 * hw, 2, gl[1], gl[2], gl[3], 0.075) end end end BG = snapshot(0, 0, W, H) end function on_settings() BG = nil end local BUB = {} for i = 1, 12 do local h = hash32(i * 0x9E3779B1) BUB[i] = { x = 4 + umod(h, 56), y0 = umod(h >> 6, 80), v = 5 + umod(h >> 12, 9), w = 0.03 + umod(h >> 16, 4) / 100, ph = umod(h >> 18, 628) / 100, r = 1 + umod(h >> 22, 3) } end local function draw_bubble(x, y, r) if r == 1 then blend(x, y, 255, 255, 255, 0.45) elseif r == 2 then blend_rect(x - 1, y - 2, 3, 1, 255, 255, 255, 0.4) blend_rect(x - 1, y + 2, 3, 1, 255, 255, 255, 0.4) blend_rect(x - 2, y - 1, 1, 3, 255, 255, 255, 0.4) blend_rect(x + 2, y - 1, 1, 3, 255, 255, 255, 0.4) blend(x - 1, y - 1, 255, 255, 255, 0.85) else blend_rect(x - 1, y - 3, 3, 1, 255, 255, 255, 0.4) blend_rect(x - 1, y + 3, 3, 1, 255, 255, 255, 0.4) blend_rect(x - 3, y - 1, 1, 3, 255, 255, 255, 0.4) blend_rect(x + 3, y - 1, 1, 3, 255, 255, 255, 0.4) blend(x - 2, y - 2, 255, 255, 255, 0.4); blend(x + 2, y - 2, 255, 255, 255, 0.4) blend(x - 2, y + 2, 255, 255, 255, 0.4); blend(x + 2, y + 2, 255, 255, 255, 0.4) blend_rect(x - 2, y - 1, 1, 2, 255, 255, 255, 0.85) end end local EYE_W = { 1, 2, 2, 2, 2, 1 } local function eye(cx, top, closed) if closed then rect(cx - 1, top + 2, 3, 1, 78, 36, 10) px(cx - 2, top + 3, 78, 36, 10); px(cx + 2, top + 3, 78, 36, 10) return end for r = 1, 6 do rect(cx - EYE_W[r], top + r - 1, 2 * EYE_W[r] + 1, 1, 26, 14, 10) end rect(cx - 1, top + 4, 3, 1, 96, 48, 14) rect(cx - 1, top + 5, 3, 1, 132, 72, 22) rect(cx - 1, top + 1, 2, 2, 255, 255, 255) px(cx + 1, top + 4, 255, 240, 225) end function render() if not BG then bake_bg() return end blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS for i = 1, cfg.bubbles or 7 do local b = BUB[i] local y = floor((H + 8) - (b.y0 + b.v * t / STEP_HZ) % (H + 16) + 0.5) draw_bubble(floor(b.x + 2 * sin(t * b.w + b.ph) + 0.5), y, b.r) end if STAGE <= #STAGES then STAGES[STAGE]() STAGE = STAGE + 1 return end local ph = t * 2 * PI / (96 / (0.5 + 0.5 * (cfg.speed or 3))) local sw = sin(ph) local bx = 2.5 * sw local by = 0.6 - 1.4 * sw * sw local liftL, liftR = 3.2 * max(0, sw) ^ 2, 3.2 * max(0, -sw) ^ 2 local shx = 32 + bx * 0.4 blend_rect(floor(shx - 11.5 + 0.5), 55, 23, 1, 120, 50, 0, 0.1) blend_rect(floor(shx - 14.5 + 0.5), 56, 29, 1, 120, 50, 0, 0.16) blend_rect(floor(shx - 15.5 + 0.5), 57, 31, 2, 120, 50, 0, 0.2) blend_rect(floor(shx - 13.5 + 0.5), 59, 27, 1, 120, 50, 0, 0.14) blend_rect(floor(shx - 9.5 + 0.5), 60, 19, 1, 120, 50, 0, 0.08) local fl = FOOT blit_indexed(fl.s, PAL, floor(32 - 6.8 - 5 + 0.35 * liftL + 0.5), 55 - floor(liftL + 0.5), fl.w, fl.h, fl.w, fl.h, true) blit_indexed(fl.s, PAL, floor(32 + 6.8 - 5 - 0.35 * liftR + 0.5), 55 - floor(liftR + 0.5), fl.w, fl.h, fl.w, fl.h, true) local byi = floor(26 + by + 0.5) for i = 1, #BODY do local sl = BODY[i] local ox = bx * (0.7 + 0.3 * (i - 1) / 4) blit_indexed(sl.s, PAL, floor(17 + ox + 0.5), byi + sl.y, BODY.w, sl.h, BODY.w, sl.h, true) end local wxo = bx * 0.85 local function flap(side, p, x) local phi = 8 + 50 * sin(p) local i = max(1, min(NWING, floor((phi + 50) / 18 + 1.5))) local sp = wing(side, i) blit_indexed(sp.s, PAL, floor(x + wxo + sp.ox + 0.5), floor(37 + by + sp.oy + 0.5), sp.w, sp.h, sp.w, sp.h, true) end flap(1, ph - 1.0, 32 - 10) flap(2, ph + PI - 1.0, 32 + 10) local hx = 1.8 * sin(ph - 0.8) local tilt = 1.5 * sin(ph - 1.4) local hy = floor(5 + by + 0.5) local hox = {} for i = 1, #HEAD do local sl = HEAD[i] hox[i] = floor(18 + hx + (i == 1 and tilt * 0.5 or 0) + 0.5) blit_indexed(sl.s, PAL, hox[i], hy + sl.y, HEAD.w, sl.h, HEAD.w, sl.h, true) end local m, k = t % 96, 0 if m < 48 then local s = sin(m * 0.5236) k = s > 0.55 and 2 or (s > 0 and 1 or 0) end local bk = beak(k) blit_indexed(bk.s, PAL, hox[3] + 8, hy + 18, bk.w, bk.h, bk.w, bk.h, true) local c = t % 150 local both = c >= 22 and c < 26 local wink = c >= 100 and c < 112 eye(hox[2] + 8, hy + 12, both) eye(hox[2] + 20, hy + 12, both or wink) end