info = { type = "image", title = "Duck family", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Duck family swims across a rainy pond", api = 33, } local ltr = ({ sk = { ["Rain"] = "Dážď", ["Ducklings"] = "Káčatká" }, cs = { ["Rain"] = "Déšť", ["Ducklings"] = "Kachňata" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "rain", label = LT("Rain"), type = "bool", default = true }, { key = "n", label = LT("Ducklings"), type = "int", min = 3, max = 5, default = 4 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, ceil, sin, min, max = math.floor, math.ceil, math.sin, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local SHORE, WATER0 = 26, 29 local WL = 41 local SPD = 0.27 local function mix(a, b, t) return 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 local function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function noise(x, seed) local i = floor(x) local f = x - i f = f * f * (3 - 2 * f) return vn(i, seed) * (1 - f) + vn(i + 1, seed) * f end local BG, STAGE = nil, 1 local SKY0, SKY1 = { 96, 110, 124 }, { 172, 186, 190 } local WTOP, WBOT = { 124, 154, 150 }, { 42, 90, 82 } local function scene() for y = 0, SHORE - 1 do rect(0, y, W, 1, mix(SKY0, SKY1, y / SHORE)) end for i = 1, 5 do local y = 2 + i * 3 for x = 0, W - 1, 4 do local a = noise(x / 9 + i * 7, 3) - 0.35 if a > 0 then blend_rect(x, y, 4, 2, 70, 84, 100, a * 0.5) end end end for x = 0, W - 1 do local ty = 20 + floor(noise(x / 2.6, 9) * 4 + 0.5) - (x % 5 == 0 and 1 or 0) rect(x, ty, 1, SHORE - ty, 112, 136, 130) end rect(0, SHORE, W, 3, 88, 122, 104) for y = WATER0, H - 1 do rect(0, y, W, 1, mix(WTOP, WBOT, (y - WATER0) / (H - 1 - WATER0))) end blend_rect(0, WATER0, W, 3, 60, 96, 88, 0.45) end local function ripples() for i = 1, 46 do local h = hash32(i * 0x85EBCA6B) local y = WATER0 + 2 + floor((umod(h >> 8, 100) / 100) ^ 1.3 * 31) local len = 2 + (y - WATER0) // 7 if umod(h >> 16, 2) == 0 then blend_rect(umod(h, W), y, len, 1, 190, 214, 208, 0.28) else blend_rect(umod(h, W), y, len, 1, 30, 76, 68, 0.3) end end end local function grid(w, h) local g = { w = w, h = h } for y = 1, h do g[y] = {} end return g end local function ellipse(g, cx, cy, rx, ry, ramp) for y = floor(cy - ry), ceil(cy + ry) do if g[y + 1] then for x = floor(cx - rx), ceil(cx + rx) do local dx, dy = (x + 0.5 - cx) / rx, (y + 0.5 - cy) / ry if dx * dx + dy * dy <= 1 and x >= 0 and x < g.w then local t = dy * 0.8 - dx * 0.2 g[y + 1][x + 1] = t < -0.4 and ramp[1] or (t < 0.3 and ramp[2] or ramp[3]) end end end end end local function dot(g, x, y, w, h, c) for j = y, y + h - 1 do for i = x, x + w - 1 do if g[j + 1] and i >= 0 and i < g.w then g[j + 1][i + 1] = c end end end end local function runs(g, flip) local rows = {} for y = 1, g.h do local r, x = {}, 1 local row = g[y] while x <= g.w do local c = row[x] local x1 = x while x1 < g.w and row[x1 + 1] == c do x1 = x1 + 1 end if c then if flip then r[#r + 1] = { g.w - x1, g.w - x + 1, c[1], c[2], c[3] } else r[#r + 1] = { x - 1, x1, c[1], c[2], c[3] } end end x = x1 + 1 end if flip then for i = 1, #r // 2 do r[i], r[#r + 1 - i] = r[#r + 1 - i], r[i] end end for i = 1, #r - 1 do r[i].adj = r[i][2] == r[i + 1][1] end rows[y] = r end return rows end local function sprite(g) return { w = g.w, h = g.h, r = runs(g, false), l = runs(g, true) } end local EYE, BEAK_M, BEAK_D, SPEC = { 30, 24, 28 }, { 238, 150, 42 }, { 240, 140, 30 }, { 70, 104, 184 } local MAMA, DUCK do local M = { { 176, 122, 74 }, { 146, 96, 56 }, { 104, 68, 42 } } local MW = { { 128, 84, 50 }, { 108, 70, 42 }, { 86, 56, 34 } } local MH = { { 150, 100, 62 }, { 126, 82, 50 }, { 96, 62, 40 } } local g = grid(19, 10) ellipse(g, 1.5, 4, 2, 1.7, M) ellipse(g, 7.5, 6, 7, 3.7, M) ellipse(g, 12, 4.4, 2.2, 2.6, M) ellipse(g, 7, 6.2, 3.8, 1.9, MW) ellipse(g, 13.5, 2.6, 2.7, 2.5, MH) dot(g, 16, 3, 3, 1, BEAK_M); dot(g, 16, 4, 2, 1, BEAK_M) dot(g, 14, 2, 1, 1, EYE) dot(g, 5, 7, 4, 1, SPEC) MAMA = sprite(g) local Y = { { 255, 238, 128 }, { 252, 208, 56 }, { 214, 160, 30 } } local YW = { { 246, 200, 60 }, { 232, 180, 40 }, { 200, 146, 26 } } g = grid(12, 7) ellipse(g, 4.5, 4.4, 4.2, 2.5, Y) ellipse(g, 8, 2.3, 2.2, 2.2, Y) ellipse(g, 4.2, 4.3, 2.4, 1.2, YW) dot(g, 10, 2, 2, 1, BEAK_D) dot(g, 9, 1, 1, 1, EYE) DUCK = sprite(g) end local function draw_sprite(S, x, y, left) local ix = floor(x) local fx = x - ix local rows = left and S.l or S.r for r = 1, S.h do local runs, yy = rows[r], y + r - 1 for j = 1, #runs do local q = runs[j] local x0, x1 = ix + q[1], ix + q[2] if fx < 0.05 then rect(x0, yy, x1 - x0, 1, q[3], q[4], q[5]) else local ext = q.adj and 1 or 0 if x1 - x0 - 1 + ext > 0 then rect(x0 + 1, yy, x1 - x0 - 1 + ext, 1, q[3], q[4], q[5]) end blend_rect(x0, yy, 1, 1, q[3], q[4], q[5], 1 - fx) if ext == 0 then blend_rect(x1, yy, 1, 1, q[3], q[4], q[5], fx) end end end end end local function col1(x, y, h, c, a) local ix = floor(x) local fx = x - ix blend_rect(ix, y, 1, h, c[1], c[2], c[3], (1 - fx) * a) if fx > 0.03 then blend_rect(ix + 1, y, 1, h, c[1], c[2], c[3], fx * a) end end local function col2(x, y, h, c) local ix = floor(x) local fx = x - ix blend_rect(ix, y, 1, h, c[1], c[2], c[3], 1 - fx) rect(ix + 1, y, 1, h, c[1], c[2], c[3]) if fx > 0.03 then blend_rect(ix + 2, y, 1, h, c[1], c[2], c[3], fx) end end local function soft_row(xl, xr, y, a, c) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, 1, c[1], c[2], c[3], a) blend_rect(il, y, 1, 1, c[1], c[2], c[3], a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, c[1], c[2], c[3], a * (xr - ir)) end local MID, FG = {}, {} for i = 1, 14 do local h = hash32(i * 0x9E3779B1) MID[i] = { x0 = umod(h, 72), h = 4 + umod(h >> 8, 4) } end local FGX, FGH = { 2, 11, 19, 31, 38, 47, 55, 63 }, { 14, 20, 12, 24, 16, 19, 13, 22 } local DASH = {} for i = 1, 10 do local h = hash32(i * 0xC2B2AE35) local y = WATER0 + 3 + umod(h, 30) DASH[i] = { y = y, len = 3 + (y - WATER0) // 5, x0 = umod(h >> 8, 80), v = (2 + (y - WATER0) * 0.35) / 30, a = 0.22 + umod(h >> 16, 10) / 100 } end local DROPS = {} for i = 1, 30 do local h = hash32(i * 0x85EBCA6B + 11) DROPS[i] = { P = 48 + umod(h, 33), off = umod(h >> 8, 80) } end function on_settings() STAGE, BG = 1, nil end local RINGC = { 210, 228, 224 } local FOAM, STALK = { 190, 214, 206 }, { 66, 104, 74 } local REED_D, REED_L, HEAD = { 34, 70, 50 }, { 52, 92, 62 }, { 112, 72, 42 } local function wiggle(f) return 1.6 * sin(f * 0.045) end function render() if STAGE == 1 then scene(); ripples(); BG = snapshot(0, 0, W, H); STAGE = 2 end blit_rgb(BG, 0, 0, W, H) local n = cfg.n or 4 local dist = {} for k = 1, n do dist[k] = 14 + 12 * (k - 1) end local LA, PAUSE = (82 + dist[n]) / SPD, 45 local LB = (80 + dist[n]) / SPD local CYC = floor(LA + PAUSE + LB + PAUSE) local f = (frame + 150) % CYC local dir, cx, fl = 0, 0, 0 if f < LA then dir, cx, fl = 1, -12 + SPD * f, f elseif f >= LA + PAUSE and f < LA + PAUSE + LB then dir, cx, fl = -1, 74 - SPD * (f - LA - PAUSE), f - LA - PAUSE end for i = 1, #DASH do local d = DASH[i] local x = (d.x0 - frame * d.v) % (W + 8) - 4 soft_row(x, x + d.len, d.y, d.a, FOAM) end local rain = cfg.rain ~= false if rain then for i = 1, #DROPS do local d = DROPS[i] local cyc = (frame + d.off) // d.P local p = (frame + d.off) % d.P local hh = hash32(i * 0x27D4EB2F + cyc * 0x9E3779B1) local yl = WATER0 + 1 + umod(hh >> 8, 33) local F = (yl + 4) / 1.6 if p >= F and p < F + 8 then local x = umod(hh, W) local age = p - F local r = 1 + floor(age * 0.5) local a = 0.5 * (1 - age / 8) blend_rect(x - 2 * r + 1, yl - 1, 4 * r - 1, 1, RINGC[1], RINGC[2], RINGC[3], a) blend_rect(x - 2 * r + 1, yl + 1, 4 * r - 1, 1, RINGC[1], RINGC[2], RINGC[3], a) blend(x - 2 * r, yl, RINGC[1], RINGC[2], RINGC[3], a) blend(x + 2 * r, yl, RINGC[1], RINGC[2], RINGC[3], a) end end end for i = 1, #MID do local m = MID[i] col1((m.x0 - frame * 0.0733) % 72 - 4, WATER0 - m.h, m.h, STALK, 0.9) end if dir ~= 0 then local left = dir < 0 for k = n, 0, -1 do local S = k == 0 and MAMA or DUCK local off = k == 0 and 0 or dist[k] local c = cx - dir * off local yw = WL + floor(wiggle(fl - off / SPD + k * 11) + 0.5) + (k == 0 and 0 or floor(0.7 * sin(frame * 0.09 + k * 1.3) + 0.5)) local x = c - S.w / 2 local back = c - dir * (S.w / 2 - 1) local big = k == 0 soft_row(min(back, back - dir * (big and 9 or 5)), max(back, back - dir * (big and 9 or 5)), yw + 1, big and 0.3 or 0.25, FOAM) soft_row(min(back - dir * (big and 7 or 4), back - dir * (big and 16 or 9)), max(back - dir * (big and 7 or 4), back - dir * (big and 16 or 9)), yw + 2, big and 0.22 or 0.15, FOAM) if big then soft_row(min(back - dir * 3, back - dir * 11), max(back - dir * 3, back - dir * 11), yw - 1, 0.22, FOAM) soft_row(min(back - dir * 14, back - dir * 21), max(back - dir * 14, back - dir * 21), yw + 3, 0.14, FOAM) end blend_rect(floor(x) + 1, yw, S.w - 2, 1, 22, 58, 52, 0.4) draw_sprite(S, x, yw - S.h, left) end end for i = 1, #FGX do local x = (FGX[i] - frame * 0.47) % 72 - 4 local h = FGH[i] col2(x, H - h, h, i % 2 == 0 and REED_L or REED_D) col2(x, H - h - 4, 4, HEAD) end if rain then for i = 1, #DROPS do local d = DROPS[i] local cyc = (frame + d.off) // d.P local p = (frame + d.off) % d.P local hh = hash32(i * 0x27D4EB2F + cyc * 0x9E3779B1) local yl = WATER0 + 1 + umod(hh >> 8, 33) local y = -4 + 1.6 * p if y < yl then blend_rect(umod(hh, W) + floor(y * 0.15), floor(y), 1, 3, 200, 216, 228, 0.6) end end end end