info = { type = "image", title = "Dolphin song", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A dolphin sings on a plinth in sunlit water", api = 44, } local tr = ({ sk = { ["Song tempo"] = "Tempo piesne", ["Light rays"] = "Svetelné lúče" }, cs = { ["Song tempo"] = "Tempo písně", ["Light rays"] = "Světelné paprsky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tempo", label = T("Song tempo"), type = "int", min = 1, max = 5, default = 3 }, { key = "rays", label = T("Light rays"), type = "bool", default = true }, } local rect, px, blend, blend_rect, blit = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_rgb local floor, sin, cos, sqrt, min, max = math.floor, math.sin, math.cos, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local TOP_W, BOT_W = { 86, 172, 178 }, { 44, 108, 126 } local SAND, SAND_L, SAND_D = { 150, 142, 112 }, { 176, 168, 136 }, { 112, 108, 88 } local TOP, TOP_L = { 72, 94, 114 }, { 116, 142, 160 } local LEFT, LEFT_L, RIGHT = { 38, 54, 74 }, { 52, 72, 94 }, { 24, 36, 54 } local MOSS = { 60, 112, 88 } local KELP, KELP_L = { 24, 112, 84 }, { 70, 170, 118 } local D_OUT, D_MID, D_LIT, D_BELLY = { 58, 72, 90 }, { 110, 128, 142 }, { 168, 186, 198 }, { 216, 226, 230 } local KEY = { 255, 0, 255 } local function c3(c) return c[1], c[2], c[3] end local function disc(cx, cy, r, c) local x0, y0 = floor(cx + 0.5), floor(cy + 0.5) local n = floor(r + 0.5) for dy = -n, n do local hw = floor(sqrt(max(0, r * r - dy * dy)) + 0.5) if hw > 0 or dy == 0 then rect(x0 - hw, y0 + dy, 2 * hw + 1, 1, c3(c)) end end end local function ell(cx, cy, rx, ry, r, g, b, a) local q = ry * ry + 0.5 for dy = -ry, ry do local hw = floor(rx * sqrt(1 - dy * dy / q) + 0.5) if a then blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, r, g, b, a) else rect(cx - hw, cy + dy, 2 * hw + 1, 1, r, g, b) end end end local SW, SH = 28, 36 local OX, OY = 21, 13 local ROWS local P0, P1, P2, P3 = { 11, 31 }, { 5, 23 }, { 9, 12 }, { 17, 8 } local RAD = { { 0, 1.2 }, { 0.25, 3 }, { 0.5, 4.6 }, { 0.75, 4.2 }, { 0.9, 3.6 }, { 1, 3.4 } } local function radius(t) for i = 2, #RAD do if t <= RAD[i][1] then local a, b = RAD[i - 1], RAD[i] return a[2] + (b[2] - a[2]) * (t - a[1]) / (b[1] - a[1]) end end return RAD[#RAD][2] end local function spine(t) local u = 1 - t local a, b, c, d = u * u * u, 3 * u * u * t, 3 * u * t * t, t * t * t return a * P0[1] + b * P1[1] + c * P2[1] + d * P3[1], a * P0[2] + b * P1[2] + c * P2[2] + d * P3[2] end local function span(y, x0, x1, c) rect(x0, y, x1 - x0 + 1, 1, c3(c)) end local function build_sprite() rect(0, 0, SW, SH, c3(KEY)) local N = 44 for _, r in ipairs({ { 30, 4, 5 }, { 30, 17, 18 }, { 31, 4, 9 }, { 31, 13, 18 }, { 32, 4, 18 }, { 33, 7, 15 }, { 34, 9, 13 } }) do span(r[1], r[2], r[3], D_OUT) end for _, r in ipairs({ { 31, 5, 8 }, { 31, 14, 17 }, { 32, 6, 16 }, { 33, 9, 14 } }) do span(r[1], r[2], r[3], D_MID) end for i = 0, N do local t = i / N local x, y = spine(t) disc(x, y, radius(t), D_OUT) end disc(19, 7, 3.1, D_OUT) for i = 0, 6 do disc(21 + i * 0.85, 7 - i * 0.45, 1.7 - i * 0.12, D_OUT) end for _, r in ipairs({ { 12, 4, 5 }, { 13, 3, 6 }, { 14, 2, 6 }, { 15, 1, 5 }, { 16, 1, 3 } }) do span(r[1], r[2], r[3], D_OUT) end for _, r in ipairs({ { 13, 14, 16 }, { 14, 15, 18 }, { 15, 16, 19 }, { 16, 17, 20 }, { 17, 18, 20 } }) do span(r[1], r[2], r[3], D_OUT) end for i = 0, N do local t = i / N local x, y = spine(t) local r = radius(t) if r > 1.8 then disc(x - 0.5, y - 0.4, r - 1.0, D_MID) end end disc(18.6, 6.6, 2.1, D_MID) for i = 1, 6 do disc(21 + i * 0.85, 7 - i * 0.45, 1.0 - i * 0.07, D_MID) end for _, r in ipairs({ { 13, 4, 4 }, { 14, 3, 5 }, { 15, 3, 4 } }) do span(r[1], r[2], r[3], D_MID) end for _, r in ipairs({ { 14, 15, 17 }, { 15, 16, 18 }, { 16, 17, 19 } }) do span(r[1], r[2], r[3], D_MID) end for i = 10, 36 do local t = i / N local x, y = spine(t) local r = radius(t) disc(x - r * 0.45, y - 0.6, r * 0.38, D_LIT) disc(x + r * 0.55, y + 0.5, r * 0.42, D_BELLY) end disc(17.3, 5.4, 1.3, D_LIT) for i = 1, 4 do px(21 + i, 7 - floor(i * 0.5), c3(D_BELLY)) end rect(18, 6, 2, 2, 14, 18, 26) px(18, 6, 240, 244, 246) for _, p in ipairs({ { 21, 8 }, { 22, 8 }, { 23, 7 }, { 24, 7 }, { 25, 6 } }) do px(p[1], p[2], 44, 54, 70) end local spr = gfx.snapshot(0, 0, SW, SH) ROWS = {} for y = 0, SH - 1 do ROWS[y] = spr:sub(y * SW * 3 + 1, (y + 1) * SW * 3) end end local BG local function build_bg() for y = 0, 56 do local t = y / 56 rect(0, y, W, 1, floor(TOP_W[1] + (BOT_W[1] - TOP_W[1]) * t + 0.5), floor(TOP_W[2] + (BOT_W[2] - TOP_W[2]) * t + 0.5), floor(TOP_W[3] + (BOT_W[3] - TOP_W[3]) * t + 0.5)) end rect(0, 57, W, 7, c3(BOT_W)) for i = 1, 14 do local h = hash32(i * 0x9E3779B1) local r = 2 + umod(h >> 12, 4) ell(umod(h, W), 2 + umod(h >> 6, 50), r, r, 255, 255, 255, 0.07) end ell(32, 63, 44, 7, c3(SAND)) ell(32, 63, 44, 6, c3(SAND)) for x = 0, W - 1, 2 do local y = 58 + floor(1.2 * sin(x * 0.4)) + umod(hash32(x), 3) px(x, y, c3(SAND_L)); px(x + 1, y + 2, c3(SAND_D)) end ell(34, 61, 21, 3, 20, 70, 82, 0.55) for x = 17, 47 do local dx = x - 32 local hh = floor((15 - (dx < 0 and -dx or dx)) / 2) local y0, y1 = 44 - hh, 44 + hh rect(x, y0, 1, 2 * hh + 1, c3(TOP)) px(x, y0, c3(TOP_L)) if dx < 0 then rect(x, y1 + 1, 1, 10, c3(LEFT)); px(x, y1 + 1, c3(LEFT_L)) elseif dx > 0 then rect(x, y1 + 1, 1, 10, c3(RIGHT)); px(x, y1 + 1, c3(LEFT)) else rect(x, y1 + 1, 1, 10, c3(LEFT_L)) end end for i = 1, 9 do local h = hash32(i * 0x85EBCA6B) local x, y = 18 + umod(h, 28), 53 + umod(h >> 8, 7) if (x < 32 and y > 44 + (15 - (32 - x)) // 2 + 1) or x > 32 then px(x, y, c3(MOSS)) end end ell(36, 45, 9, 3, 20, 30, 44, 0.32) BG = gfx.snapshot(0, 0, W, H) end local RAYS = { { 8, 6, 0.0 }, { 22, 5, 1.9 }, { 40, 7, 4.1 } } local function rays(t) for k = 1, #RAYS do local r = RAYS[k] local a = 0.075 + 0.04 * sin(t * 0.9 + r[3]) local x0 = r[1] + 2 * sin(t * 0.36 + r[3]) for y = 0, 44, 4 do blend_rect(floor(x0 + y * 0.32), y, r[2], 4, 255, 255, 235, a) end end end local function kelp(t) local S = { { 6, 12, 0 }, { 12, 9, 1.7 }, { 52, 10, 3.1 }, { 58, 13, 4.6 } } for k = 1, #S do local s = S[k] for i = 0, s[2] - 1 do local y = 61 - i * 2 local u = i / s[2] local x = s[1] + (2.6 * sin(t * 2.1 - i * 0.5 + s[3])) * u local xi = floor(x) local f = x - xi local c = (i % 4 == 3) and KELP_L or KELP blend_rect(xi - 1, y, 1, 2, c[1], c[2], c[3], 1 - f) rect(xi, y, 1, 2, c3(c)) blend_rect(xi + 1, y, 1, 2, c[1], c[2], c[3], f) end end end local BUB = {} for i = 1, 9 do local h = hash32(i * 0xC2B2AE35) BUB[i] = { x = 4 + umod(h, 56), v = 3.6 + umod(h >> 8, 20) * 0.3, ph = umod(h >> 14, 628) / 100, y0 = umod(h >> 20, 70) } end local function bubbles(t) for i = 1, 9 do local b = BUB[i] local y = 66 - (b.y0 + t * b.v) % 74 if y > -2 and y < 58 then local x = floor(b.x + 1.6 * sin(t * 1.5 + b.ph) + 0.5) local yi = floor(y + 0.5) blend(x, yi - 1, 255, 255, 255, 0.55); blend(x, yi + 1, 255, 255, 255, 0.35) blend(x - 1, yi, 255, 255, 255, 0.35); blend(x + 1, yi, 255, 255, 255, 0.55) end end end local LIFE = 3.67 local function notes(t, gap) local k = floor(t / gap) while k >= 0 and (t - k * gap) < LIFE do if k % 4 ~= 3 then local age = t - k * gap local h = hash32(k * 0x27D4EB2F) local vx = 3 + umod(h, 6) * 0.3 local x = 48 + age * vx + 2.2 * sin(age * 3.3 + umod(h >> 8, 6)) local y = 17 - age * 4.8 local a = min(1, age / 0.33, (LIFE - age) / 1.33) local xi, yi = floor(x + 0.5), floor(y + 0.5) if (h >> 4) & 1 == 0 then blend_rect(xi + 2, yi, 1, 5, 244, 250, 236, a) blend_rect(xi, yi + 4, 3, 2, 244, 250, 236, a) blend_rect(xi + 3, yi + 1, 2, 1, 244, 250, 236, a) px(xi + 4, yi + 2, 244, 250, 236) else blend_rect(xi + 2, yi, 1, 5, 255, 238, 190, a) blend_rect(xi + 5, yi, 1, 4, 255, 238, 190, a) blend_rect(xi + 2, yi, 4, 1, 255, 238, 190, a) blend_rect(xi, yi + 4, 3, 2, 255, 238, 190, a) blend_rect(xi + 3, yi + 3, 3, 2, 255, 238, 190, a) end end k = k - 1 end end local function dolphin(t, gap) local sway = 2.2 * sin(t * 1.8) for y = 0, SH - 1 do local u = (SH - 1 - y) / (SH - 1) local dx = floor(sway * u * u + 0.5) blit(ROWS[y], OX + dx, OY + y, SW, 1, 255, 0, 255) end local k = floor(t / gap) if k % 4 ~= 3 then local age = t - k * gap local o = max(0, 1 - age / 0.4) local h = floor(2.6 * o + 0.5) if h > 0 then local dx = floor(sway * ((SH - 1 - 7) / (SH - 1)) ^ 2 + 0.5) local x, y = OX + dx + 21, OY + 7 rect(x + 1, y + 1, 5, h, 52, 12, 22) rect(x + 2, y + h, 3, 1, 216, 90, 110) rect(x, y + 1 + h, 5, 1, c3(D_MID)) rect(x + 5, y, 1, 1, c3(D_OUT)) end end end function render() if not ROWS then build_sprite(); gfx.clear(0, 0, 0) gfx.ready(false) return end if not BG then build_bg() end local t = frame / FPS local gap = 2.27 - (cfg.tempo or 3) * 0.333 gfx.blit_rgb(BG, 0, 0, W, H) if cfg.rays ~= false then rays(t) end kelp(t) dolphin(t, gap) notes(t, gap) bubbles(t) end