info = { type = "image", title = "Goose at dusk", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A big white goose bobs and dives on dusk water", api = 33, } local ltr = ({ sk = { ["Sky"] = "Obloha", ["Dusk"] = "Súmrak", ["Night"] = "Noc", ["Morning"] = "Ráno" }, cs = { ["Sky"] = "Obloha", ["Dusk"] = "Soumrak", ["Night"] = "Noc", ["Morning"] = "Ráno" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "sky", label = LT("Sky"), type = "int", default = 0, opts = { LT("Dusk"), LT("Night"), LT("Morning") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sqrt, sin, cos, abs = math.floor, math.ceil, math.sqrt, math.sin, math.cos, math.abs local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local TAU = 6.28318 local HZ, WL = 30, 50 local PALS = { { sky = { 46, 26, 90, 104, 40, 124, 190, 64, 112, 246, 128, 82 }, sun = { 255, 214, 120 }, water = { 214, 104, 110, 92, 52, 118, 40, 34, 88 }, glint = { 236, 150, 140 }, refl = { 150, 124, 176 }, foam = { 232, 204, 228 }, stars = false }, { sky = { 14, 18, 52, 26, 36, 88, 44, 60, 124, 70, 90, 160 }, sun = { 236, 236, 206 }, water = { 90, 104, 160, 34, 46, 100, 20, 28, 70 }, glint = { 150, 170, 220 }, refl = { 110, 122, 176 }, foam = { 186, 200, 236 }, stars = true }, { sky = { 118, 176, 236, 160, 204, 244, 246, 210, 176, 255, 232, 180 }, sun = { 255, 250, 214 }, water = { 232, 206, 176, 110, 170, 214, 62, 126, 190 }, glint = { 232, 244, 255 }, refl = { 176, 204, 232 }, foam = { 240, 250, 255 }, stars = false }, } local PAL = PALS[1] local function part(x0, x1, y, a1, b1, a2, b2, r, g, b) local rs, rq = x0, 0 for x = x0, x1 do local lo = a1 > x and a1 or x local hi = b1 < x + 1 and b1 or x + 1 local c = hi > lo and hi - lo or 0 if a2 then lo = a2 > x and a2 or x hi = b2 < x + 1 and b2 or x + 1 if hi > lo then c = c + hi - lo end end local q = (c * 4 + 0.5) // 1 if q ~= rq then if rq >= 8 then rect(rs, y, x - rs, 1, r, g, b) elseif rq > 0 then blend_rect(rs, y, x - rs, 1, r, g, b, rq / 8) end rs, rq = x, q end end if rq >= 8 then rect(rs, y, x1 + 1 - rs, 1, r, g, b) elseif rq > 0 then blend_rect(rs, y, x1 + 1 - rs, 1, r, g, b, rq / 8) end end local function aarow(y, a1, b1, a2, b2, r, g, b) if not a1 then a1, b1, a2, b2 = a2, b2, nil, nil end if not a1 then return end local lo, hi, L, R if a2 then lo, hi = floor(a1 < a2 and a1 or a2), ceil(b1 > b2 and b1 or b2) - 1 L, R = ceil(a1 > a2 and a1 or a2), floor(b1 < b2 and b1 or b2) if R > L then rect(L, y, R - L, 1, r, g, b) end else lo, hi, L, R = floor(a1), ceil(b1) - 1, ceil(a1), floor(b1) if R > L then blend_rect(L, y, R - L, 1, r, g, b, 0.5) end end if R > L then if L > lo then part(lo, L - 1, y, a1, b1, a2, b2, r, g, b) end if hi >= R then part(R, hi, y, a1, b1, a2, b2, r, g, b) end else part(lo, hi, y, a1, b1, a2, b2, r, g, b) end end local function pspan(P, n, yc) local lo, hi local x1, y1 = P[n - 1], P[n] for i = 1, n, 2 do local x2, y2 = P[i], P[i + 1] if (y1 <= yc) ~= (y2 <= yc) then local x = x1 + (yc - y1) * (x2 - x1) / (y2 - y1) if not lo then lo, hi = x, x elseif x < lo then lo = x elseif x > hi then hi = x end end x1, y1 = x2, y2 end return lo, hi end local function cx_at(P, i0, i1, yc) local x0, y0, x1, y1 = P[2 * i0 - 1], P[2 * i0], P[2 * i1 - 1], P[2 * i1] if y1 == y0 then return x1 end return x0 + (yc - y0) * (x1 - x0) / (y1 - y0) end local function aapoly(P, ox, oy, r, g, b) local n = #P // 2 local ti, bi, yt, yb = 1, 1, P[2], P[2] for i = 2, n do local y = P[2 * i] if y < yt then yt, ti = y, i end if y > yb then yb, bi = y, i end end local a0, a1 = ti, ti % n + 1 local c0, c1 = ti, (ti - 2) % n + 1 for y = floor(yt + oy), ceil(yb + oy) - 1 do local s1, e1, s2, e2 local yc = y + 0.25 - oy if yc > yt and yc < yb then while P[2 * a1] < yc and a1 ~= bi do a0, a1 = a1, a1 % n + 1 end while P[2 * c1] < yc and c1 ~= bi do c0, c1 = c1, (c1 - 2) % n + 1 end s1, e1 = cx_at(P, a0, a1, yc), cx_at(P, c0, c1, yc) if s1 > e1 then s1, e1 = e1, s1 end s1, e1 = s1 + ox, e1 + ox end yc = yc + 0.5 if yc > yt and yc < yb then while P[2 * a1] < yc and a1 ~= bi do a0, a1 = a1, a1 % n + 1 end while P[2 * c1] < yc and c1 ~= bi do c0, c1 = c1, (c1 - 2) % n + 1 end s2, e2 = cx_at(P, a0, a1, yc), cx_at(P, c0, c1, yc) if s2 > e2 then s2, e2 = e2, s2 end s2, e2 = s2 + ox, e2 + ox end aarow(y, s1, e1, s2, e2, r, g, b) end end local function aacircle(cx, cy, rad, r, g, b) local r2 = rad * rad for y = floor(cy - rad), ceil(cy + rad) - 1 do local a1, b1, a2, b2 local d = y + 0.25 - cy local q = r2 - d * d if q > 0 then q = sqrt(q); a1, b1 = cx - q, cx + q end d = y + 0.75 - cy q = r2 - d * d if q > 0 then q = sqrt(q); a2, b2 = cx - q, cx + q end aarow(y, a1, b1, a2, b2, r, g, b) end end local function hline_a(x0, x1, y, r, g, b, a) if x1 <= x0 or a <= 0.02 then return end local L, R = ceil(x0), floor(x1) if R > L then blend_rect(L, y, R - L, 1, r, g, b, a) end if R >= L then if L > x0 then blend_rect(L - 1, y, 1, 1, r, g, b, a * (L - x0)) end if x1 > R then blend_rect(R, y, 1, 1, r, g, b, a * (x1 - R)) end else blend_rect(L - 1, y, 1, 1, r, g, b, a * (x1 - x0)) end end local BODY = { -25, -17, -19, -13.5, -12, -12, 5, -12, 11, -10.5, 15, -8, 17, -4.5, 18, 0, 17.5, 2, -21, 2, -23.5, -2, -25, -7, -26, -12 } local WING = { -22, -13.5, -10, -11, 3, -10.5, 9, -8.5, 11, -5.5, 7, -3, -4, -2.5, -13, -4, -19, -7.5, -23, -11 } local TIP = { -23, -11, -16, -9, -12, -6.5, -16, -6, -20, -8 } local BEAK = { 3.2, -2.4, 8.5, -1.4, 11, 0.3, 10.2, 1.5, 6, 1.9, 3.2, 2.4 } local NBODY = #BODY local GR, GG, GB = 250, 246, 238 local WR, WG, WB = 206, 200, 222 local TR, TG, TB = 168, 160, 196 local BR, BG_, BB = 250, 146, 48 local ER, EG, EB = 26, 20, 34 local B_X, B_Y = 12, -7.5 local C0X, C0Y, H0X, H0Y = 19, -19, 15, -28 local C1X, C1Y, H1X, H1Y = 25, -7.5, 27, 6 local NQ = {} for q = 1, 4 do NQ[q] = { 0, 0, 0, 0, 0, 0, 0, 0 } end local SX, SY, NX, NY = {}, {}, {}, {} local BK = {} local function build_neck(bx, by, cx, cy, hx, hy) for i = 0, 4 do local s = i / 4 local u = 1 - s SX[i] = u * u * bx + 2 * u * s * cx + s * s * hx SY[i] = u * u * by + 2 * u * s * cy + s * s * hy local tx = 2 * u * (cx - bx) + 2 * s * (hx - cx) local ty = 2 * u * (cy - by) + 2 * s * (hy - cy) local l = sqrt(tx * tx + ty * ty) if l < 1e-6 then l, tx, ty = 1, 0, -1 end local w = 4.2 - 1.3 * s NX[i], NY[i] = -ty / l * w, tx / l * w end for q = 1, 4 do local i, j = q - 1, q local dx, dy = SX[j] - SX[i], SY[j] - SY[i] local l = sqrt(dx * dx + dy * dy) if l < 1e-6 then l = 1 end dx, dy = dx / l, dy / l local e0 = q > 1 and 0.9 or 0 local e1 = q < 4 and 0.9 or 0 local ax, ay = SX[i] - dx * e0, SY[i] - dy * e0 local zx, zy = SX[j] + dx * e1, SY[j] + dy * e1 local Q = NQ[q] Q[1], Q[2] = ax + NX[i], ay + NY[i] Q[3], Q[4] = zx + NX[j], zy + NY[j] Q[5], Q[6] = zx - NX[j], zy - NY[j] Q[7], Q[8] = ax - NX[i], ay - NY[i] end end local function smooth(p) if p <= 0 then return 0 elseif p >= 1 then return 1 end return p * p * (3 - 2 * p) end local function pose(kind, u) if kind == 1 then local e if u < 16 then e = smooth(u / 16) elseif u < 44 then e = 1 else e = 1 - smooth((u - 44) / 20) end local wob = (u >= 16 and u < 44) and 0.6 * sin(u * 0.5) or 0 return C0X + (C1X - C0X) * e, C0Y + (C1Y - C0Y) * e, H0X + (H1X - H0X) * e + wob, H0Y + (H1Y - H0Y) * e, 1.35 * e, 0 end local env = (u >= 0 and u < 36) and sin(3.14159 * u / 36) or 0 local d = 2.4 * sin(u * TAU / 4.5) * env return C0X + d * 0.4, C0Y, H0X + d, H0Y + abs(d) * 0.3, 0.12 * d, 0.35 * d end local BG = nil local SKYB = { 0, 9, 17, 24, HZ } local WATB = { HZ, 34, 42, H } local function grad(B, c, n) for y = B[1], B[n + 1] - 1 do local i, f = 1, 0 for k = 1, n - 1 do local m0, m1 = (B[k] + B[k + 1]) / 2, (B[k + 1] + B[k + 2]) / 2 if y + 0.5 >= m0 then i, f = k, (y + 0.5 - m0) / (m1 - m0) end end if f > 1 then i, f = n - 1, 1 end local j = 3 * i rect(0, y, W, 1, floor(c[j - 2] + (c[j + 1] - c[j - 2]) * f + 0.5), floor(c[j - 1] + (c[j + 2] - c[j - 1]) * f + 0.5), floor(c[j] + (c[j + 3] - c[j]) * f + 0.5)) end end local function build_bg() local P = PAL local s, w = P.sky, P.water grad(SKYB, s, 4) if P.stars then for k = 1, 16 do local h = hash32(k * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, 20) if k % 3 == 0 then px(x, y, 236, 236, 255) else px(x, y, 130, 140, 200) end end end local c = P.sun for y = HZ - 7, HZ - 1 do local d = y + 0.5 - HZ local q = sqrt(49 - d * d) local c0, c1 = ceil(10 - q - 0.5), ceil(10 + q - 0.5) - 1 rect(c0, y, c1 - c0 + 1, 1, c[1], c[2], c[3]) end grad(WATB, w, 3) BG = gfx.snapshot(0, 0, W, H) end function on_settings() PAL = PALS[(cfg.sky or 0) + 1] or PALS[1] BG = nil end local GLX, GLY, GLP = {}, {}, {} for k = 1, 12 do local h = hash32(k * 0x85EBCA6B) GLX[k], GLY[k], GLP[k] = umod(h, 60), HZ + 1 + umod(h >> 8, 32), umod(h >> 16, 27) end local DROPR, DROPG, DROPB = 226, 236, 255 local function drops(seed, cnt, sx, sy, w, spread, up) if w < 0 or w > 50 then return end for j = 1, cnt do local h = hash32((seed * 16 + j) * 0x9E3779B1) local r1 = (h & 0xFF) / 255 local r2 = ((h >> 8) & 0xFF) / 255 local vx = (r1 - 0.5) * spread + 0.2 local vy = -(up + r2 * 0.9) local y = sy + vy * w + 0.075 * w * w if y < WL then px(floor(sx + vx * w + 0.5), floor(y + 0.5), DROPR, DROPG, DROPB) end end end local function ring(x, w, c) if w < 0 or w >= 40 then return end local rx, ry = 2 + w * 0.32, 0.8 + w * 0.08 local a = 0.75 * (1 - w / 40) hline_a(x - rx * 0.7, x + rx * 0.7, WL + floor(ry), c[1], c[2], c[3], a) hline_a(x - rx, x - rx * 0.6, WL + floor(ry * 0.4), c[1], c[2], c[3], a) hline_a(x + rx * 0.6, x + rx, WL + floor(ry * 0.4), c[1], c[2], c[3], a) end local POSE_FPS = 30 local L, EV = 270, 150 local OFF = EV + 34 - 120 local OX = 30 local function draw_head(HX, HY, hd) for q = 1, 4 do aapoly(NQ[q], 0, 0, GR, GG, GB) end aacircle(HX, HY, 4.6, GR, GG, GB) local c, s = cos(hd), sin(hd) for i = 1, #BEAK, 2 do local x, y = BEAK[i], BEAK[i + 1] BK[i], BK[i + 1] = HX + x * c - y * s, HY + x * s + y * c end aapoly(BK, 0, 0, BR, BG_, BB) aacircle(HX + 1.6 * c + 1.4 * s, HY + 1.6 * s - 1.4 * c, 1.0, ER, EG, EB) end local NOPS, BOPS = {}, {} local REC, RN = {}, 0 local CODE, CR, CG, CB = {}, {}, {}, {} local real_rect, real_blend = rect, blend_rect local schar, sbyte = string.char, string.byte local REC_Y = 40 local function code(r, g, b) local key = r * 65536 + g * 256 + b local c = CODE[key] if not c then c = #CR + 1 CR[c], CG[c], CB[c] = r, g, b CODE[key] = c end return c end local PARTS = {} local function rec_op(x, y, w, c, q) REC[RN + 1], REC[RN + 2], REC[RN + 3], REC[RN + 4], REC[RN + 5] = x + 8, y, w, c, q RN = RN + 5 if RN == 40 then PARTS[#PARTS + 1] = schar(table.unpack(REC, 1, 40)) RN = 0 end end local function rec_rect(x, y, w, h, r, g, b) rec_op(x, y, w, code(r, g, b), 8) end local function rec_blend(x, y, w, h, r, g, b, a) rec_op(x, y, w, code(r, g, b), floor(a * 8 + 0.5)) end local function record(q, neck) rect, blend_rect = rec_rect, rec_blend RN, PARTS = 0, {} local oy = REC_Y + q / 4 if neck then build_neck(B_X + OX, B_Y + oy, C0X + OX, C0Y + oy, H0X + OX, H0Y + oy) draw_head(H0X + OX, H0Y + oy, 0) else aapoly(BODY, OX, oy, GR, GG, GB) aapoly(WING, OX, oy, WR, WG, WB) aapoly(TIP, OX, oy, TR, TG, TB) end rect, blend_rect = real_rect, real_blend if RN > 0 then PARTS[#PARTS + 1] = schar(table.unpack(REC, 1, RN)) end local s = table.concat(PARTS) PARTS = {} return s end local function replay(s, dx, dy) for i = 1, #s, 5 do local x, y, w, c, q = sbyte(s, i, i + 4) if q >= 8 then rect(x - 8 + dx, y + dy, w, 1, CR[c], CG[c], CB[c]) elseif q > 0 then blend_rect(x - 8 + dx, y + dy, w, 1, CR[c], CG[c], CB[c], q / 8) end end end local REFQ, RT = {}, {} local function refl_spans(oy, HX, HY, T) for k = 0, 13 do local yc = WL - 0.5 - k local lo, hi = pspan(BODY, NBODY, yc - oy) T[4 * k + 1], T[4 * k + 2] = lo and lo + OX or false, hi and hi + OX or false lo, hi = nil, nil for q = 1, 4 do local a, b = pspan(NQ[q], 8, yc) if a then if not lo or a < lo then lo = a end if not hi or b > hi then hi = b end end end local d = yc - HY if d > -4.6 and d < 4.6 then local q = sqrt(21.16 - d * d) if not lo or HX - q < lo then lo = HX - q end if not hi or HX + q > hi then hi = HX + q end end T[4 * k + 3], T[4 * k + 4] = lo or false, hi or false end return T end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local P = PAL local f = frame * POSE_FPS // gfx.FPS local tc, n = (f + OFF) % L, (f + OFF) // L local kind = (n % 3 == 2) and 2 or 1 local u = tc - EV local dur = kind == 1 and 64 or 36 local live = u >= 0 and u < dur local cx, cy, hx, hy, hd, wig = C0X, C0Y, H0X, H0Y, 0, 0 if live then cx, cy, hx, hy, hd, wig = pose(kind, u) end local oyf = WL + 0.9 * sin(f * TAU / 78) + 0.4 * sin(f * TAU / 47 + 1.3) local iy = floor(oyf) local q = floor((oyf - iy) * 4) local oy = iy + q / 4 local ox = OX + wig local HX, HY = hx + ox, hy + oy local sc, gc = P.sun, P.glint for i = 0, 3 do local w = 1 + (f // 5 + i) % 3 rect(10 - w, HZ + 1 + 2 * i, 2 * w, 1, sc[1], sc[2], sc[3]) end for k = 1, 12 do local ph = (f + GLP[k] * 7) // 9 if ph % 3 ~= 0 then rect(GLX[k], GLY[k], 2 + ph % 2, 1, gc[1], gc[2], gc[3]) end end local T if live then build_neck(B_X + ox, B_Y + oy, cx + ox, cy + oy, HX, HY) T = refl_spans(oy, HX, HY, RT) else T = REFQ[q] if not T then build_neck(B_X + OX, B_Y + oy, C0X + OX, C0Y + oy, HX, HY) T = refl_spans(oy, HX, HY, {}) REFQ[q] = T end end local rc = P.refl for k = 0, 13 do if (k + f // 6) % 5 ~= 4 then local dx = floor(sin(k * 1.3 + f * 0.19) * 1.3 + 0.5) for m = 1, 3, 2 do local lo, hi = T[4 * k + m], T[4 * k + m + 1] if lo then local c0, c1 = ceil(lo - 0.5) + dx, ceil(hi - 0.5) - 1 + dx if c1 >= c0 then rect(c0, WL + k, c1 - c0 + 1, 1, rc[1], rc[2], rc[3]) end end end end end local fc = P.foam local lo, hi = pspan(BODY, NBODY, WL - 0.5 - oy) lo, hi = lo + OX, hi + OX for r = 0, 1 do local d = (f * 0.15 + r * 5) % 10 local a = 0.6 * (1 - d / 10) local y = WL + 1 + floor(d * 0.35) + r hline_a(lo - 4 - d * 1.2, lo - 1 - d * 1.2, y, fc[1], fc[2], fc[3], a) hline_a(hi + 1 + d * 1.2, hi + 4 + d * 1.2, y, fc[1], fc[2], fc[3], a) end if kind == 1 then local _, _, x1 = pose(1, 11) local _, _, x2 = pose(1, 49) ring(x1 + OX, u - 11, fc) ring(x2 + OX, u - 49, fc) end gfx.clip(0, 0, W, WL) local b = BOPS[q] if not b then b = record(q, false); BOPS[q] = b end replay(b, floor(wig + 0.5), iy - REC_Y) if live then draw_head(HX, HY, hd) else local o = NOPS[q] if not o then o = record(q, true); NOPS[q] = o end replay(o, 0, iy - REC_Y) end gfx.clip() aarow(WL, lo - 1, hi + 1, lo - 1, hi + 1, fc[1], fc[2], fc[3]) if live and kind == 1 then local nlo, nhi for k = 1, 4 do local a, b = pspan(NQ[k], 8, WL - 0.5) if a then if not nlo or a < nlo then nlo = a end if not nhi or b > nhi then nhi = b end end end if nlo and nlo > hi then aarow(WL, nlo - 1, nhi + 1, nlo - 1, nhi + 1, fc[1], fc[2], fc[3]) end end if kind == 1 then local _, _, x, y = pose(1, 50) drops(n * 8 + 1, 8, x + OX, y + oy - 3, u - 50, 2.4, 0.9) else for k = 1, 3 do local u0 = 1 + 7 * k local _, _, x, y = pose(2, u0) drops(n * 8 + k, 3, x + OX, y + oy - 2, u - u0, 3.4, 0.5) end end end