info = { type = "image", title = "Dusk dolphin", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A dolphin leaps across a sunset sea", api = 33, } local tr = ({ sk = { ["Pause"] = "Pauza", ["Sky"] = "Obloha", ["Amber"] = "Jantárová", ["Rose"] = "Ružová", ["Ember"] = "Žeravá" }, cs = { ["Pause"] = "Pauza", ["Sky"] = "Obloha", ["Amber"] = "Jantarová", ["Rose"] = "Růžová", ["Ember"] = "Žhavá" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pause", label = T("Pause"), type = "int", min = 1, max = 10, unit = "s", default = 3 }, { key = "sky", label = T("Sky"), type = "int", default = "amber", opts = { keys = { "amber", "rose", "ember" }, T("Amber"), T("Rose"), T("Ember") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, abs, atan = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.atan local hash32, umod = math.hash32, math.umod local char, unpack, concat = string.char, table.unpack, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local WLY = 36 local SX = 44 local THEMES = { amber = { sky = { { 0, 44, 28, 88 }, { 16, 150, 50, 110 }, { 30, 244, 120, 80 }, { 35, 255, 190, 100 } }, sun = { 255, 236, 170 }, wt = { 150, 60, 80 }, wb = { 12, 16, 56 } }, rose = { sky = { { 0, 54, 30, 100 }, { 16, 170, 60, 130 }, { 30, 248, 110, 120 }, { 35, 255, 170, 150 } }, sun = { 255, 226, 200 }, wt = { 160, 60, 110 }, wb = { 18, 14, 60 } }, ember = { sky = { { 0, 30, 22, 60 }, { 16, 120, 36, 70 }, { 30, 222, 80, 50 }, { 35, 255, 150, 60 } }, sun = { 255, 214, 120 }, wt = { 140, 40, 50 }, wb = { 10, 10, 40 } }, } local TH local function grad(st, y) for i = 1, #st - 1 do local a, b = st[i], st[i + 1] if y <= b[1] then local u = (y - a[1]) / (b[1] - a[1]) return floor(a[2] + (b[2] - a[2]) * u + 0.5), floor(a[3] + (b[3] - a[3]) * u + 0.5), floor(a[4] + (b[4] - a[4]) * u + 0.5) end end local l = st[#st] return l[2], l[3], l[4] end local function water(y) local t = ((y - WLY) / (H - 1 - WLY)) ^ 0.7 local a, b = TH.wt, TH.wb 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 DC = { { 0, 0, 0 }, { 26, 26, 84 }, { 88, 92, 168 }, { 150, 142, 196 }, { 232, 190, 200 }, { 255, 200, 140 }, { 8, 8, 28 } } local PAL_AIR, PAL_SUB local BEND = 1.2 local function prof(u) if u < -10 or u > 10 then return nil end if u < -3 then return 0.8 + (u + 10) / 7 * 2.2 end if u < 1 then return 3.0 + (u + 3) / 4 * 0.5 end if u < 4 then return 3.5 - (u - 1) / 3 * 0.5 end if u < 6 then return 3.0 - (u - 4) / 2 * 1.8 end local r = 1.2 - (u - 6) * 0.15 if u > 9.3 then r = r - (u - 9.3) end return r end local BOUND = { 0.35, -0.05, -0.5 } local function cell(u, b, x, y, kl) local bb = b + BEND * (u / 10) ^ 2 local r = prof(u) if r and abs(bb) <= r then local n = bb / r if u > 4.3 and u < 5.4 and bb > 0.2 and bb < 1.0 then return 6 end if n * kl > 0.55 and abs(n) > 0.72 then return 5 end local band = n > 0.35 and 1 or (n > -0.05 and 2 or (n > -0.5 and 3 or 4)) for i = 1, 3 do if abs(n - BOUND[i]) < 0.1 and (x + y) % 2 == 0 then band = n > BOUND[i] and i + 1 or i band = band > 4 and 4 or band break end end return band end local hf = 0 if u > -2 then hf = 3.8 * (1.8 - u) / 3.8 elseif u > -3.4 then hf = 3.8 * (u + 3.4) / 1.4 end if hf > 3.8 then hf = 3.8 end local rr = prof(u) or 0 if hf > 0 and bb > rr - 0.5 and bb <= rr - 0.5 + hf then return 2 end if u > 0.2 and u < 3.2 and rr > 0 then local hp = 2.6 * (3.2 - u) / 3.0 if bb < -rr + 0.5 and bb >= -rr - hp then return 3 end end if u < -9.5 and u > -12.5 then local hw = 0.9 + (-9.5 - u) if abs(bb) <= hw and not (u < -10.8 and abs(bb) < (-10.8 - u) * 1.1 + 0.2) then return 2 end end return 0 end local POSES = { -60, -30, 0, 30, 60 } local SPR = {} local function pose(i) local s = SPR[i] if s then return s end local a = POSES[i] * PI / 180 local ca, sa = cos(a), sin(a) local kl = -0.55 * sa + 0.83 * ca local rows = {} for y = 0, 29 do local row = {} for x = 0, 29 do local dx, dy = x + 0.5 - 15, y + 0.5 - 15 row[x + 1] = cell(dx * ca - dy * sa, -dx * sa - dy * ca, x, y, kl) end rows[y + 1] = char(unpack(row)) end s = concat(rows) SPR[i] = s return s end local function mix(c, d, t) return floor(c[1] + (d[1] - c[1]) * t + 0.5), floor(c[2] + (d[2] - c[2]) * t + 0.5), floor(c[3] + (d[3] - c[3]) * t + 0.5) end local BG local function build_bg() TH = THEMES[cfg.sky] or THEMES.amber for y = 0, WLY - 1 do rect(0, y, W, 1, grad(TH.sky, y)) end local sc = TH.sun for y = WLY - 14, WLY - 1 do local dy = WLY - y - 0.5 for k = 1, 2 do local rr, a = k == 1 and 13 or 10, k == 1 and 0.1 or 0.14 if dy < rr then local hw = floor(sqrt(rr * rr - dy * dy) + 0.5) blend_rect(SX - hw, y, 2 * hw, 1, sc[1], sc[2], sc[3], a) end end if dy < 7 then local hw = floor(sqrt(49 - dy * dy) + 0.5) rect(SX - hw, y, 2 * hw, 1, sc[1], sc[2], sc[3]) end end for y = WLY, H - 1 do rect(0, y, W, 1, water(y)) end blend_rect(0, WLY, W, 1, sc[1], sc[2], sc[3], 0.35) blend_rect(0, WLY + 1, W, 1, sc[1], sc[2], sc[3], 0.12) for k = 0, 3 do blend_rect(SX - 4 - floor(1.5 * k), WLY + 2 + 5 * k, 9 + 3 * k, 5, sc[1], sc[2], sc[3], 0.2 - 0.045 * k) end BG = gfx.snapshot(0, 0, W, H) local sea = { water(WLY + 10) } PAL_AIR, PAL_SUB = { 0, 0, 0 }, { 0, 0, 0 } for i = 2, #DC do local c = DC[i] PAL_AIR[#PAL_AIR + 1], PAL_AIR[#PAL_AIR + 2], PAL_AIR[#PAL_AIR + 3] = c[1], c[2], c[3] local r, g, b = mix(c, sea, 0.4) PAL_SUB[#PAL_SUB + 1], PAL_SUB[#PAL_SUB + 2], PAL_SUB[#PAL_SUB + 3] = r, g, b end end function on_settings() BG = nil end local VX = 30 local X0, X1 = -16, 80 local XA, XE, XL, XD = 2, 18, 50, 66 local D, HJ = 10, 18 local LJ = XL - XE local M = 4 * HJ / LJ local TRAVEL = (X1 - X0) / VX local function hermite(p0, m0, p1, m1, u, len) local u2, u3 = u * u, u * u * u return (2 * u3 - 3 * u2 + 1) * p0 + (u3 - 2 * u2 + u) * len * m0 + (-2 * u3 + 3 * u2) * p1 + (u3 - u2) * len * m1 end local function path(x) if x < XA then return WLY + D + 0.8 * sin(x * 0.25) end if x < XE then return hermite(WLY + D, 0, WLY, -M, (x - XA) / (XE - XA), XE - XA) end if x < XL then local s = (x - XE) / LJ return WLY - 4 * HJ * s * (1 - s) end if x < XD then return hermite(WLY, M, WLY + D, 0, (x - XL) / (XD - XL), XD - XL) end return WLY + D + 0.8 * sin(x * 0.25 + 1) end local DROPS = {} for i = 1, 10 do local h = hash32(i * 0x9E3779B1) DROPS[i] = { vx = (umod(h, 41) - 20) * 0.9, vy = 16 + umod(h >> 8, 17), j = umod(h >> 16, 3) } end local function splash(x, tau, back) if tau < 0 or tau > 1.0 then return end local sc = TH.sun local fade = 1 - tau for i = 1, 10 do local d = DROPS[i] local vx = d.vx + (back and -8 or 10) local y = WLY - (d.vy * tau - 35 * tau * tau) if y < WLY then blend(floor(x + vx * tau + 0.5), floor(y + 0.5), 255, 238, 214, 0.45 + 0.5 * fade) end end if tau < 0.3 then rect(x - 1, WLY - 3, 3, 3, 255, 226, 196) blend_rect(x - 2, WLY - 2, 5, 2, 255, 226, 196, 0.5) end local w = floor(2 + tau * 16) blend_rect(x - w, WLY, 2 * w, 1, 255, 240, 220, 0.65 * fade) blend_rect(x - w - 2, WLY + 1, 2 * w + 4, 1, sc[1], sc[2], sc[3], 0.3 * fade) end local GL = {} for i = 1, 18 do local h = hash32(i * 0x85EBCA6B) local d = umod(h, 20) GL[i] = { x = SX - 3 - d // 3 + umod(h >> 8, 7 + d // 2), y = WLY + 2 + d, w = d < 8 and 3 or 2, ph = umod(h >> 16, 628) / 100, sp = 0.08 + umod(h >> 22, 6) / 100 } end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local sc = TH.sun local t = frame for i = 1, #GL do local g = GL[i] local a = sin(t * g.sp + g.ph) if a > 0.3 then blend_rect(g.x, g.y, g.w, 1, sc[1], sc[2], sc[3], (a - 0.2) * 0.7) end end local cl = floor((TRAVEL + (cfg.pause or 3)) * FPS) local ts = (frame % cl) / FPS if ts < TRAVEL then local x = X0 + VX * ts local y = path(x) local slope = (path(x + 0.5) - path(x - 0.5)) local ang = atan(-slope, 1) * 180 / PI local k = ang < -45 and 1 or (ang < -15 and 2 or (ang < 15 and 3 or (ang < 45 and 4 or 5))) local s = pose(k) local bx, by = floor(x + 0.5) - 15, floor(y + 0.5) - 15 gfx.clip(0, 0, W, WLY) gfx.blit_indexed(s, PAL_AIR, bx, by, 30, 30, 30, 30, true) gfx.clip(0, WLY, W, H - WLY) gfx.blit_indexed(s, PAL_SUB, bx, by, 30, 30, 30, 30, true) gfx.clip() if y > WLY + 3 then for j = 1, 3 do blend(floor(x - 12 - j * 3), floor(y - 1 + 2 * sin(t * 0.2 + j * 2) + 0.5), 230, 210, 220, 0.4) end end blend_rect(0, WLY, W, 1, sc[1], sc[2], sc[3], 0.2) splash(XE, ts - (XE - X0) / VX, false) splash(XL, ts - (XL - X0) / VX, true) end end