info = { type = "image", title = "Mallard", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A mallard swims, bobs and dives with ripples", api = 33, } local tr = ({ sk = { ["Diving"] = "Potápanie", ["never"] = "nikdy", ["sometimes"] = "občas", ["often"] = "často", ["Head colour"] = "Farba hlavy" }, cs = { ["Diving"] = "Potápění", ["never"] = "nikdy", ["sometimes"] = "občas", ["often"] = "často", ["Head colour"] = "Barva hlavy" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "dive", label = T("Diving"), type = "int", default = 1, opts = { T("never"), T("sometimes"), T("often") } }, { key = "head", label = T("Head colour"), type = "color", default = "#22A062" }, } local rect, px = gfx.rect, gfx.px local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local W, H = gfx.W, gfx.H local function c3(c) return c[1], c[2], c[3] end local function ell(cx, cy, rx, ry, c) for y = floor(cy - ry + 0.5), floor(cy + ry - 0.5) do local dy = (y + 0.5 - cy) / ry local k = 1 - dy * dy if k > 0 then local hw = rx * sqrt(k) local x0 = floor(cx - hw + 0.5) rect(x0, y, floor(cx + hw + 0.5) - x0, 1, c[1], c[2], c[3]) end end end local function rell(cx, cy, a, b, phi, c) local cs, sn = cos(phi), sin(phi) local a2, b2 = a * a, b * b local A = cs * cs / a2 + sn * sn / b2 local k1 = 2 * (cs * sn / a2 - sn * cs / b2) local k2 = sn * sn / a2 + cs * cs / b2 local hh = sqrt(a2 * sn * sn + b2 * cs * cs) local r, g, bl = c[1], c[2], c[3] for y = floor(cy - hh), floor(cy + hh) do local dy = y + 0.5 - cy local B, C = k1 * dy, k2 * dy * dy - 1 local disc = B * B - 4 * A * C if disc > 0 then local sq = sqrt(disc) local x0 = floor(cx + (-B - sq) / (2 * A) + 0.5) local x1 = floor(cx + (-B + sq) / (2 * A) + 0.5) if x1 > x0 then rect(x0, y, x1 - x0, 1, r, g, bl) end end end end local SKY1, SKY2 = { 150, 206, 255 }, { 188, 232, 255 } local WATER, WATER_L, WATER_D = { 64, 146, 210 }, { 112, 188, 240 }, { 44, 116, 188 } local REED, REED_D, CAT = { 70, 160, 84 }, { 40, 120, 70 }, { 130, 76, 50 } local PAD, PAD_D = { 84, 178, 92 }, { 52, 138, 76 } local BODY, WING, CHEST = { 190, 184, 180 }, { 130, 118, 114 }, { 156, 88, 66 } local SPEC, TAIL, FOOT = { 50, 104, 214 }, { 40, 40, 54 }, { 255, 146, 36 } local WHITE, BILL, EYE = { 250, 250, 252 }, { 255, 198, 48 }, { 20, 20, 28 } local HEAD = { 34, 160, 98 } local WL = 42 local BG = nil local function build_bg() rect(0, 0, W, 20, c3(SKY1)); rect(0, 20, W, 12, c3(SKY2)) for x = 0, W - 1, 2 do px(x, 19, c3(SKY2)) end ell(52.5, 9.5, 7, 7, { 255, 244, 190 }); ell(52.5, 9.5, 5, 5, { 255, 226, 110 }) rect(0, 26, W, 6, 96, 176, 110) rect(0, 30, W, 2, c3(REED_D)) rect(0, 32, W, H - 32, c3(WATER)) rect(0, 32, W, 2, c3(WATER_L)) rect(0, 54, W, 10, c3(WATER_D)) for x = 0, W - 1, 2 do px(x, 53, c3(WATER_D)) end for _, x in ipairs({ 2, 5, 8, 58, 61 }) do local top = 12 + (x * 7) % 9 rect(x, top + 4, 1, 36 - top, c3(REED)) rect(x, top, 1, 5, c3(CAT)) rect(x + 1, top + 14, 3, 1, c3(REED_D)); rect(x - 3, top + 20, 3, 1, c3(REED_D)) end BG = gfx.snapshot(0, 0, W, H) end local PADS = { { 10, 49, 5 }, { 40, 58, 6 }, { 52, 47, 4 } } local function draw_pads(f) local sh = f // 4 for i = 1, #PADS do local p = PADS[i] local x = (p[1] - sh) % (W + 16) - 8 ell(x, p[2], p[3], 1.8, PAD) rect(x - 1, p[2], 3, 1, c3(PAD_D)) if i == 2 then px(x + 1, p[2] - 2, 255, 140, 190); px(x + 2, p[2] - 2, 255, 200, 220) end end end local function draw_cloud(f) local x = ((f // 9) + 20) % (W + 30) - 22 ell(x + 8, 7, 8, 2.5, { 255, 255, 255 }); ell(x + 5, 5, 4, 2.5, { 255, 255, 255 }); ell(x + 11, 5.5, 4, 2, { 255, 255, 255 }) end local function ring(cx, age) if age < 0 or age > 70 then return end local rx = 3 + age * 0.3 local ry = 1 + rx * 0.22 local c = age < 40 and WATER_L or { 90, 170, 232 } local yc = WL + 3 local ri = rx - 1.6 local ryi = ry - 1 for y = floor(yc - ry), floor(yc + ry) do local dy = (y + 0.5 - yc) / ry local k = 1 - dy * dy if k > 0 then local xo = rx * sqrt(k) local dyi = ryi > 0 and (y + 0.5 - yc) / ryi or 2 local ki = 1 - dyi * dyi if ki > 0.04 then local xi = ri * sqrt(ki) local x0 = floor(cx - xo + 0.5) rect(x0, y, floor(cx - xi + 0.5) - x0, 1, c3(c)) local x1 = floor(cx + xi + 0.5) rect(x1, y, floor(cx + xo + 0.5) - x1, 1, c3(c)) else local x0 = floor(cx - xo + 0.5) rect(x0, y, floor(cx + xo + 0.5) - x0, 1, c3(c)) end end end end local CX, CY0 = 30, 38 local function duck(f, th, cy, kick, bob) local cs, sn = cos(th), sin(th) local function P(dx, dy) return CX + cs * dx - sn * dy, cy + sn * dx + cs * dy end local x, y if th > 0.5 then x, y = P(-14, 5); rell(x, y, 3.2, 1.6, th + kick, FOOT) x, y = P(-11, 7); rell(x, y, 3.2, 1.6, th - kick, FOOT) end x, y = P(-17, -3); rell(x, y, 4.5, 2.6, th - 0.5, TAIL) x, y = P(0, 0); rell(x, y, 16, 9, th, BODY) x, y = P(-3, -2); rell(x, y, 10.5, 4.8, th, WING) x, y = P(-5, 0); rell(x, y, 4.5, 1.2, th, SPEC) x, y = P(10, 0); rell(x, y, 7, 7, th, CHEST) x, y = P(13, -8 + bob); rell(x, y, 3.6, 6, th, HEAD) x, y = P(13, -3 + bob); rell(x, y, 4.4, 1.4, th, WHITE) x, y = P(15, -14 + bob); rell(x, y, 6, 5.5, th, HEAD) x, y = P(22, -12 + bob); rell(x, y, 5, 2.1, th + 0.15, BILL) x, y = P(17.5, -15 + bob) px(floor(x), floor(y), c3(WHITE)); px(floor(x + 1), floor(y), c3(EYE)) end local STEP_HZ = 30 local SW = { 0, 210, 100 } function on_settings() local c = cfg.head or { r = 34, g = 160, b = 98 } HEAD = { c.r, c.g, c.b } BG = nil end local function smooth(k) return k * k * (3 - 2 * k) end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame * STEP_HZ // gfx.FPS draw_cloud(f) draw_pads(f) local mode = cfg.dive or 1 local sw = SW[mode + 1] local cyc = sw > 0 and (sw + 100) or 1000000 local t = f % cyc local th, cy, kick, bob = 0, CY0, 0, 0 if sw > 0 and t >= sw then local u = t - sw if u < 16 then th = 1.75 * smooth(u / 16); cy = CY0 + 2 * smooth(u / 16) elseif u < 62 then th = 1.75; cy = CY0 + 2 + ((u // 6) % 2); kick = 0.5 * sin(u * 0.45) elseif u < 80 then th = 1.75 * (1 - smooth((u - 62) / 18)); cy = CY0 + 2 - 2 * smooth((u - 62) / 18) else bob = 0 end end if th == 0 then bob = ((f // 7) % 2 == 0) and 0 or 1; cy = CY0 + (((f // 14) % 2 == 0) and 0 or 1) end local base = f - f % 45 ring(CX + 2, f - base) ring(CX + 2, f - base + 45) if sw > 0 then ring(CX + 6, t - sw) ring(CX + 6, t - sw - 62) end gfx.clip(0, 0, W, WL) duck(f, th, cy, kick, bob) gfx.clip() local d = (f // 10) % 2 rect(CX - 14 + d, WL, 6, 1, c3(WATER_L)); rect(CX + 4 - d, WL, 7, 1, c3(WATER_L)); rect(CX + 16 + d, WL, 5, 1, c3(WATER_L)) rect(CX - 9 - d, WL + 1, 5, 1, c3(WATER)); rect(CX + 8 + d, WL + 1, 6, 1, c3(WATER)) if sw > 0 and t >= sw + 60 and t < sw + 76 then local a = t - sw - 60 for k = 1, 5 do local x = CX + 12 + (k - 3) * 3 + (k - 3) * a // 3 local y = WL - 2 - (4 * a - a * a // 4) // 3 - k % 2 px(x, y, c3(WHITE)) end end end