info = { type = "image", title = "Bathtub duck", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Rubber duck bobbing in a bath, dripping tap", api = 44, } local ltr = ({ sk = { ["Tiles"] = "Kachličky", ["Mint"] = "Mätová", ["Rose"] = "Ružová", ["Sky"] = "Nebeská", ["Lemon"] = "Citrónová", ["Duck"] = "Kačička", ["Bubbles"] = "Bublinky" }, cs = { ["Tiles"] = "Kachličky", ["Mint"] = "Mátová", ["Rose"] = "Růžová", ["Sky"] = "Nebeská", ["Lemon"] = "Citrónová", ["Duck"] = "Kachnička", ["Bubbles"] = "Bublinky" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "tiles", label = LT("Tiles"), type = "int", default = "mint", opts = { keys = { "mint", "rose", "sky", "lemon" }, LT("Mint"), LT("Rose"), LT("Sky"), LT("Lemon") } }, { key = "duck", label = LT("Duck"), type = "color", default = "#FFC81E" }, { key = "bubbles", label = LT("Bubbles"), type = "int", min = 0, max = 8, default = 5 }, } local rect, px, blend_rect, blend = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend local blit_rgb, blit_indexed, blend_mask, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.blend_mask, gfx.snapshot local floor, sin, cos, exp, sqrt, max = math.floor, math.sin, math.cos, math.exp, math.sqrt, math.max local hash32, umod = math.hash32, math.umod local char, concat = string.char, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.2831853 local WY = 42 local TUB_L, TUB_R = 8, 56 local TX = 46 local TILES = { mint = { { 176, 226, 208 }, { 240, 196, 210 }, { 236, 248, 242 } }, rose = { { 248, 204, 214 }, { 200, 228, 236 }, { 252, 240, 242 } }, sky = { { 176, 214, 244 }, { 250, 226, 168 }, { 236, 244, 252 } }, lemon = { { 250, 236, 168 }, { 186, 226, 206 }, { 252, 248, 226 } }, } local WATER_T, WATER_B = { 122, 208, 238 }, { 66, 164, 224 } local function mix(a, b, t) return a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function h15(a, b) return hash32(a * 7919 + b * 104729) & 0x7FFF end local PART, BG = nil, nil local TP = TILES.mint local function tiles(ra, rb) local base, band, grout = TP[1], TP[2], TP[3] if ra == 0 then rect(0, 0, W, 58, rgb(grout[1], grout[2], grout[3])) end for r = ra, rb do for c = 0, 7 do local k = h15(r, c) % 9 - 4 local b = (r == 2) and band or base rect(c * 8, r * 8, 7, 7, rgb(b[1] + k, b[2] + k, b[3] + k)) rect(c * 8, r * 8, 7, 1, rgb(b[1] + k + 10, b[2] + k + 10, b[3] + k + 10)) px(c * 8 + 1, r * 8 + 1, 255, 255, 255) end end end local function floor_tiles() local base = TP[1] rect(0, 58, W, 6, rgb(mix(base, { 120, 130, 150 }, 0.3))) for c = 0, 7 do rect(c * 8 + (c % 2) * 4, 60, 4, 4, rgb(mix(base, { 120, 130, 150 }, 0.45))) end rect(0, 58, W, 1, 255, 255, 255) end local function tub() rect(TUB_L, 35, TUB_R - TUB_L, WY - 35, 226, 236, 250) rect(TUB_L, 35, TUB_R - TUB_L, 1, 196, 210, 234) for y = WY, 52 do rect(TUB_L, y, TUB_R - TUB_L, 1, rgb(mix(WATER_T, WATER_B, (y - WY) / 11))) end blend_rect(TUB_L, WY, 3, 11, 20, 90, 170, 0.22); blend_rect(TUB_R - 3, WY, 3, 11, 20, 90, 170, 0.22) rect(2, 33, 60, 2, 255, 255, 255) rect(2, 35, 6, 22, 238, 242, 250); rect(56, 35, 6, 22, 238, 242, 250) rect(2, 35, 1, 22, 200, 208, 230); rect(61, 35, 1, 22, 200, 208, 230) rect(7, 35, 1, 18, 214, 222, 238); rect(56, 35, 1, 18, 214, 222, 238) rect(3, 53, 58, 5, 244, 246, 252) rect(8, 53, 48, 1, 206, 216, 236) rect(3, 57, 58, 1, 192, 202, 226) rect(8, 58, 5, 3, 236, 204, 128); rect(51, 58, 5, 3, 236, 204, 128) rect(8, 58, 5, 1, 255, 236, 176); rect(51, 58, 5, 1, 255, 236, 176) end local function foam_blob(cx, cy, r) for y = floor(cy - r), floor(cy + r) do for x = floor(cx - r), floor(cx + r) do local dx, dy = x + 0.5 - cx, y + 0.5 - cy if dx * dx + dy * dy <= r * r then local t = (-dx * 0.55 - dy * 0.7) / r if t > 0.35 then px(x, y, 255, 255, 255) elseif t > -0.35 then px(x, y, 238, 248, 255) else px(x, y, 196, 222, 242) end end end end end local function foam() local FOAM = { { 10, 3.6 }, { 14, 3 }, { 8.5, 2.6 }, { 12, 2.2 }, { 54, 3.4 }, { 51.5, 2.4 }, { 56, 2.4 }, { 36, 1.8 }, { 41, 1.6 }, { 30, 1.4 } } for _, f in ipairs(FOAM) do foam_blob(f[1], WY - f[2] * 0.4, f[2]) end end local function tap() rect(TX - 1, 10, 18, 3, 214, 224, 240); rect(TX - 1, 10, 18, 1, 244, 250, 255); rect(TX - 1, 12, 18, 1, 150, 168, 200) rect(TX - 1, 13, 3, 6, 214, 224, 240); rect(TX - 1, 13, 1, 6, 244, 250, 255); rect(TX + 1, 13, 1, 6, 150, 168, 200) rect(TX, 19, 1, 1, 150, 168, 200) rect(54, 7, 3, 3, 246, 150, 176); rect(53, 8, 5, 1, 246, 150, 176); px(54, 7, 255, 200, 214) end local STAGE = 1 local STAGES local function build() if STAGE > 1 and STAGE <= 6 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() if STAGE <= 5 then PART = snapshot(0, 0, W, H) end if STAGE == 6 then BG = snapshot(0, 0, W, H) end STAGE = STAGE + 1 end local DW, DH, DOX, DOY = 32, 22, 15, 12 local PAL = { 0, 0, 0 } local DUCK = {} local ANG = { -0.13, 0, 0.13 } local function set_palette(c) local base = { c.r, c.g, c.b } local function add(r, g, b) PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = rgb(r, g, b) end PAL = { 0, 0, 0 } add(base[1], base[2], base[3]) add(base[1] + (255 - base[1]) * 0.45, base[2] + (255 - base[2]) * 0.45, base[3] + (255 - base[3]) * 0.45) add(mix(base, { 214, 120, 30 }, 0.4)); add(mix(base, { 176, 84, 26 }, 0.7)) add(255, 138, 40); add(214, 94, 24); add(30, 24, 30); add(255, 255, 255) add(mix(base, { 214, 120, 30 }, 0.22)); add(255, 150, 130) end local LXD, LYD = -0.55, -0.7 local function shade(nx, ny) local t = nx * LXD + ny * LYD if t > 0.5 then return 2 elseif t > -0.2 then return 1 elseif t > -0.62 then return 3 end return 4 end local function duck_px(u, v) if math.abs(u - 8.2) < 0.9 and math.abs(v + 7.2) < 0.9 then local su, sv = u - 8.6, v + 7.8 if su * su + sv * sv < 0.3 then return 8 end return 7 end local bu, bv = (u - 12.6) / 3.4, (v + 4.4) / 1.9 if bu * bu + bv * bv <= 1 then return v > -4.4 and 6 or 5 end local hu, hv = (u - 6.2) / 5.2, (v + 6.2) / 5.2 if hu * hu + hv * hv <= 1 then local cu, cv = u - 7.2, v + 3.6 if cu * cu + cv * cv < 1.7 then return 10 end return shade(hu, hv) end local wu, wv = (u + 1.5) / 5.2, (v - 2.8) / 3.1 if wu * wu + wv * wv <= 1 then if wu * LXD + wv * LYD > 0.45 then return 1 end return 9 end local yu, yv = u / 10.4, (v - 3.2) / 6.8 if yu * yu + yv * yv <= 1 then return shade(yu, yv) end local tu, tv = (u + 10.2) / 3.4, (v + 0.8) / 2.6 if tu * tu + tv * tv <= 1 then return shade(tu, tv) end return 0 end local DG = {} local function bake_duck(i, ya, yb) local a = ANG[i] local ca, sa = cos(a), sin(a) local g = DG[i] or {} DG[i] = g for y = ya, yb do local r = {} for x = 0, DW - 1 do local dx, dy = x + 0.5 - DOX, y + 0.5 - DOY - 4 r[x] = duck_px(dx * ca + dy * sa, -dx * sa + dy * ca + 4) end g[y] = r end if yb < DH - 1 then return end local rows, sil, top = {}, {}, {} for y = 0, DH - 1 do local c, s, t = {}, {}, {} for x = 0, DW - 1 do local v = g[y][x] c[x + 1] = char(v) s[x + 1] = v ~= 0 and "\255" or "\0" t[x + 1] = (v ~= 0 and (y == 0 or g[y - 1][x] == 0)) and "\255" or "\0" end rows[y + 1], sil[y + 1], top[y + 1] = concat(c), concat(s), concat(t) end DUCK[i] = { s = concat(rows), sil = concat(sil), top = concat(top) } DG[i] = nil end STAGES = { function() tiles(0, 2) end, function() tiles(3, 6) end, floor_tiles, tub, foam, tap, function() bake_duck(1, 0, 10) end, function() bake_duck(1, 11, DH - 1) end, function() bake_duck(2, 0, 10) end, function() bake_duck(2, 11, DH - 1) end, function() bake_duck(3, 0, 10) end, function() bake_duck(3, 11, DH - 1) end, } function on_settings() TP = TILES[cfg.tiles] or TILES.mint local c = cfg.duck or { r = 255, g = 200, b = 30 } set_palette(c) BG, STAGE = nil, 1 DUCK, DG = {}, {} end local RING = {} local function ring(r) local m = RING[r] if m then return m end local rows = {} for y = 0, 2 * r do local c = {} for x = 0, 2 * r do local d = sqrt((x - r) ^ 2 + (y - r) ^ 2) c[x + 1] = char(max(0, floor((1 - math.abs(d - r) / 0.75) * 255))) end rows[y + 1] = concat(c) end m = concat(rows) RING[r] = m return m end local ARC = {} local function arc(r) local m = ARC[r] if m then return m end local rows = {} for y = 0, r do local c = {} for x = 0, 2 * r do local d = sqrt((x - r) ^ 2 + y * y) c[x + 1] = char(max(0, floor((1 - math.abs(d - r) / 0.8) * 255))) end rows[y + 1] = concat(c) end m = concat(rows) ARC[r] = m return m end local NB = 8 local BUB = {} for i = 1, NB do local h = hash32(i * 0x9E3779B1) BUB[i] = { P = 3.6 + umod(h, 16) / 10, off = umod(h >> 5, 50) / 10 } end local function bubble(x, y, r, a) if r <= 1 then blend(x, y, 236, 250, 255, 0.8 * a) else blend_mask(ring(r), 226, 244, 255, x - r, y - r, 2 * r + 1, 2 * r + 1, 2 * r + 1, 2 * r + 1, 0.75 * a) blend(x - floor(r * 0.5), y - floor(r * 0.5) - 1, 255, 255, 255, 0.9 * a) end end local DP, HIT, SPD = 3.6, 1.82, 12 local function ring_arc(rf, a) local r0 = floor(rf) local f = rf - r0 if r0 >= 1 and r0 <= 24 and a * (1 - f) > 0.02 then blend_mask(arc(r0), 236, 250, 255, TX - r0, WY, 2 * r0 + 1, r0 + 1, 2 * r0 + 1, r0 + 1, a * (1 - f)) end if r0 + 1 <= 24 and a * f > 0.02 then blend_mask(arc(r0 + 1), 236, 250, 255, TX - r0 - 1, WY, 2 * r0 + 3, r0 + 2, 2 * r0 + 3, r0 + 2, a * f) end end local function put_duck(sp, x, fy) local iy = floor(fy) local ay = fy - iy if ay < 0.94 then blend_mask(sp.top, PAL[4], PAL[5], PAL[6], x, iy, DW, DH, DW, DH, 1 - ay) end blit_indexed(sp.s, PAL, x, iy + 1, DW, DH, DW, DH, true) gfx.clip(TUB_L, WY, TUB_R - TUB_L, 12) blend_mask(sp.sil, 140, 206, 238, x, iy + 1, DW, DH, DW, DH, 0.42) gfx.clip() end function render() if STAGE <= #STAGES then if BG then blit_rgb(BG, 0, 0, W, H) end build() gfx.ready(false) return end local t = frame / FPS blit_rgb(BG, 0, 0, W, H) for i = 1, cfg.bubbles or 5 do local b = BUB[i] local n = floor((t + b.off) / b.P) local tau = (t + b.off) % b.P local x = TUB_L + 3 + h15(n, i) % (TUB_R - TUB_L - 6) local r = tau < 0.4 and 1 or (tau < 0.9 and 2 or 3) if tau < b.P - 0.35 then bubble(x, WY - r, r, tau < 0.2 and tau / 0.2 or 1) else local k = (tau - (b.P - 0.35)) / 0.35 local rr = 3 + floor(k * 3) blend_mask(ring(rr), 236, 250, 255, x - rr, WY - 3 - rr, 2 * rr + 1, 2 * rr + 1, 2 * rr + 1, 2 * rr + 1, 0.55 * (1 - k)) end end local tau = t % DP local bump = 0 if tau > HIT then local d = (tau - HIT) - 10 / SPD bump = 0.9 * exp(-(d * d) / 0.18) * sin((tau - HIT) * 9) end local up = 1.1 * sin(TAU * t / 2.8) + 0.4 * sin(TAU * t / 1.7 + 1) + bump local tilt = cos(TAU * t / 2.8) + 0.5 * bump local pose = tilt > 0.4 and 3 or (tilt < -0.4 and 1 or 2) if DUCK[pose] then put_duck(DUCK[pose], 22 - DOX, WY - 16 - up) end for k = 0, 11 do local x = TUB_L + k * 4 blend_rect(x, WY, 4, 1, 236, 250, 255, 0.3 + 0.28 * sin(x * 0.5 - t * 3)) blend_rect(x, WY + 1, 4, 1, 40, 120, 200, 0.14 + 0.1 * sin(x * 0.7 + t * 2.4)) end if tau < 1.4 then local s = tau / 1.4 blend_rect(TX, 19, 1, 1, 200, 240, 255, 0.5 + 0.5 * s) if s > 0.5 then blend_rect(TX, 20, 1, 1, 200, 240, 255, (s - 0.5) * 2) end elseif tau < HIT then local u = tau - 1.4 local yf = 20 + 130 * u * u local iy = floor(yf) local f = yf - iy blend_rect(TX, iy, 1, 1, 214, 246, 255, 1 - f) blend_rect(TX, iy + 1, 1, 1, 214, 246, 255, 1) blend_rect(TX, iy + 2, 1, 1, 214, 246, 255, f) else local u = tau - HIT gfx.clip(TUB_L, WY, TUB_R - TUB_L, 11) ring_arc(1 + u * SPD, 0.7 * max(0, 1 - u / 1.7)) if u > 0.35 then ring_arc(1 + (u - 0.35) * SPD, 0.5 * max(0, 1 - (u - 0.35) / 1.5)) end gfx.clip() if u < 0.25 then blend_rect(TX - 1, WY - 2, 3, 1, 236, 250, 255, 0.8 * (1 - u * 4)) blend_rect(TX, WY - 3, 1, 1, 236, 250, 255, 0.8 * (1 - u * 4)) end end end