info = { type = "image", title = "Pearl diver", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A diver sinks to the seabed for a pearl shell", api = 33, } local tr = ({ sk = { ["Pace"] = "Tempo", ["Slow"] = "Pomaly", ["Normal"] = "Normálne", ["Fast"] = "Rýchlo", ["Bubbles"] = "Bublinky" }, cs = { ["Pace"] = "Tempo", ["Slow"] = "Pomalu", ["Normal"] = "Normálně", ["Fast"] = "Rychle", ["Bubbles"] = "Bublinky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pace", label = T("Pace"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, T("Slow"), T("Normal"), T("Fast") } }, { key = "bubbles", label = T("Bubbles"), type = "int", min = 0, max = 5, default = 3 }, } local rect, blend_rect, blend = gfx.rect, gfx.blend_rect, gfx.blend local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, max, min = math.floor, math.sin, math.sqrt, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local TAU, PI = 6.28318, 3.14159 local SURF = 16 local DY0, DY1 = 10, 160 local CYC = 600 local PACE = { slow = 70, normal = 100, fast = 140 } local PC, NB = 100, 3 local function frect(x, y, w, h, r, g, b) local x0 = floor(x) local f = x - x0 if f < 0.05 then rect(x0, y, w, h, r, g, b) return end blend_rect(x0, y, 1, h, r, g, b, 1 - f) if w > 1 then rect(x0 + 1, y, w - 1, h, r, g, b) end blend_rect(x0 + w, y, 1, h, r, g, b, f) end local function fdot(x, y, r, g, b, a) local x0, y0 = floor(x), floor(y) local fx, fy = x - x0, y - y0 blend(x0, y0, r, g, b, a * (1 - fx) * (1 - fy)) blend(x0 + 1, y0, r, g, b, a * fx * (1 - fy)) blend(x0, y0 + 1, r, g, b, a * (1 - fx) * fy) blend(x0 + 1, y0 + 1, r, g, b, a * fx * fy) end local function ease(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function lerp(a, b, u) return a + (b - a) * u end local KEYS = { { SURF, 120, 222, 236 }, { 40, 44, 150, 206 }, { 90, 16, 84, 156 }, { 140, 10, 48, 110 }, { 192, 8, 34, 80 } } local function water(y) for i = 1, #KEYS - 1 do local a, b = KEYS[i], KEYS[i + 1] if y <= b[1] then local f = (y - a[1]) / (b[1] - a[1]) return floor(lerp(a[2], b[2], f) + 0.5), floor(lerp(a[3], b[3], f) + 0.5), floor(lerp(a[4], b[4], f) + 0.5) end end local k = KEYS[#KEYS] return k[2], k[3], k[4] end local GY = {} for x = 0, W - 1 do GY[x] = 176 + floor(2 * sin(x * 0.21) + 1.5 * sin(x * 0.07 + 1) + 0.5) end local SHX = 48 local SHY = GY[SHX] + 1 local OY = 0 local function R(x, y, w, h, r, g, b) rect(x, y - OY, w, h, r, g, b) end local function blob(cx, cy, rx, ry, k) local wr, wg, wb = water(cy) for dy = -ry, ry do local y = cy + dy if y >= OY and y < OY + H then local half = floor(rx * sqrt(1 - (dy / ry) ^ 2) + 0.5) R(cx - half, y, 2 * half + 1, 1, floor(wr * 0.5), floor(wg * 0.55 + 4), floor(wb * 0.6 + 4)) if dy < -ry * 0.3 then R(cx + half - 1, y, 1, 1, floor(wr * 0.7), floor(wg * 0.75 + 6), floor(wb * 0.8 + 6)) end end end end local function coral_branch(x, yb, h, c, tip) R(x, yb - h, 2, h, c[1], c[2], c[3]) R(x - 3, yb - h * 0.6 // 1, 3, 1, c[1], c[2], c[3]) R(x - 3, yb - h * 0.6 // 1 - 3, 1, 3, c[1], c[2], c[3]) R(x + 2, yb - h * 0.4 // 1, 3, 1, c[1], c[2], c[3]) R(x + 4, yb - h * 0.4 // 1 - 4, 1, 4, c[1], c[2], c[3]) R(x, yb - h - 1, 2, 1, tip[1], tip[2], tip[3]) R(x - 3, yb - h * 0.6 // 1 - 4, 1, 1, tip[1], tip[2], tip[3]) R(x + 4, yb - h * 0.4 // 1 - 5, 1, 1, tip[1], tip[2], tip[3]) end local function coral_fan(x, yb, h, c, tip) for r = 0, h - 1 do local half = 1 + r * 2 // 3 R(x - half, yb - r - 1, 2 * half + 1, 1, c[1], c[2], c[3]) end R(x - 1 - (h - 1) * 2 // 3, yb - h, 2 * ((h - 1) * 2 // 3) + 3, 1, tip[1], tip[2], tip[3]) R(x, yb - 2, 1, 2, tip[1] // 2, tip[2] // 2, tip[3] // 2) end local function sponge(x, yb, c, tip) R(x, yb - 7, 3, 7, c[1], c[2], c[3]); R(x + 4, yb - 10, 3, 10, c[1], c[2], c[3]); R(x + 8, yb - 5, 3, 5, c[1], c[2], c[3]) R(x, yb - 8, 3, 1, tip[1], tip[2], tip[3]); R(x + 4, yb - 11, 3, 1, tip[1], tip[2], tip[3]); R(x + 8, yb - 6, 3, 1, tip[1], tip[2], tip[3]) end local function kelp(x, yb, len) for i = 0, len - 1, 2 do local off = floor(sin(i * 0.45 + x) * 1.3 + 0.5) R(x + off, yb - i - 2, 1, 2, 22, 112, 96) if i % 4 == 0 then R(x + off + 1, yb - i - 1, 1, 1, 34, 146, 112) end end end local CORAL = { 232, 100, 80 } local CORAL_T = { 255, 168, 128 } local PINK = { 226, 110, 148 } local PINK_T = { 255, 178, 196 } local SPONGE = { 214, 156, 62 } local SPONGE_T = { 250, 206, 110 } local function draw_page(k) OY = k * H for y = max(SURF, OY), OY + H - 1 do R(0, y, W, 1, water(y)) end if k == 0 then for y = 0, SURF - 1 do local f = y / (SURF - 1) rect(0, y, W, 1, floor(lerp(150, 214, f)), floor(lerp(206, 240, f)), floor(lerp(242, 252, f))) end blend_rect(6, 4, 14, 1, 255, 255, 255, 0.55); blend_rect(4, 5, 18, 2, 255, 255, 255, 0.55) blend_rect(8, 7, 12, 1, 255, 255, 255, 0.4) blend_rect(40, 8, 14, 1, 255, 255, 255, 0.55); blend_rect(38, 9, 18, 2, 255, 255, 255, 0.5) rect(0, SURF - 1, W, 1, 236, 250, 255) rect(0, SURF, W, 1, 190, 244, 250) end blob(0, 84, 7, 14); blob(2, 112, 8, 10); blob(1, 150, 9, 16); blob(63, 100, 6, 18); blob(62, 138, 8, 12) blob(63, 168, 7, 8) for x = 0, W - 1 do local top = GY[x] if top < OY + H then R(x, top, 1, 192 - top, 92, 88, 94) R(x, top, 1, 1, 134, 124, 120) R(x, top + 1, 1, 1, 112, 104, 104) end end kelp(5, GY[5], 20); kelp(8, GY[8], 14); kelp(58, GY[58], 22); kelp(61, GY[61], 12); kelp(27, GY[27], 10) coral_branch(13, GY[13] + 1, 12, CORAL, CORAL_T) coral_fan(21, GY[21] + 1, 9, PINK, PINK_T) sponge(52, GY[52] + 1, SPONGE, SPONGE_T) coral_branch(40, GY[40] + 1, 8, PINK, PINK_T) coral_fan(33, GY[33] + 1, 6, CORAL, CORAL_T) for i = 1, 10 do local x = umod(hash32(i * 0x9E3779B1), W) R(x, GY[x] + 2 + i % 3, 2, 1, 70, 66, 74) end end local PG, BUILT = {}, 0 local HAIR, SKIN, MASK, LENS = { 46, 112, 226 }, { 255, 206, 172 }, { 26, 34, 66 }, { 126, 236, 255 } local SUIT, SUIT2, STRIPE = { 26, 38, 70 }, { 46, 64, 108 }, { 255, 110, 86 } local TANK, FIN = { 226, 178, 54 }, { 238, 96, 80 } local function c3(c) return c[1], c[2], c[3] end local function draw_diver(cx, y, arm, kick) frect(cx - 6, y + 6, 2, 7, c3(TANK)) frect(cx - 5, y + 5, 1, 1, 150, 112, 30) frect(cx - 6, y + 2, 3, 5 + kick, c3(HAIR)) frect(cx - 4, y, 7, 4, c3(HAIR)) frect(cx - 4, y + 7, 1, 1, c3(HAIR)) frect(cx - 4, y + 8, 2, 5, c3(SUIT)) frect(cx - 2, y + 13 + kick, 2, 3, c3(SUIT)) frect(cx, y + 14 - kick, 2, 3 - 1, c3(SUIT)) frect(cx - 4, y + 16 + kick, 4, 2, c3(FIN)) frect(cx - 1, y + 17 - kick, 4, 2, c3(FIN)) frect(cx - 3, y + 7, 6, 6, c3(SUIT)) frect(cx - 3, y + 9, 6, 1, c3(STRIPE)) frect(cx - 2, y + 3, 5, 4, c3(SKIN)) frect(cx - 1, y + 3, 5, 3, c3(MASK)) frect(cx, y + 4, 3, 1, c3(LENS)) frect(cx - 3, y + 4, 2, 1, c3(MASK)) frect(cx + 1, y + 6, 2, 1, 236, 176, 60) if arm == 0 then frect(cx + 1, y + 8, 2, 5, c3(SUIT2)); frect(cx + 1, y + 13, 2, 1, c3(SKIN)) elseif arm == 1 then frect(cx + 1, y + 9, 3, 2, c3(SUIT2)); frect(cx + 4, y + 11, 3, 2, c3(SUIT2)); frect(cx + 7, y + 13, 2, 2, c3(SKIN)) else frect(cx + 2, y - 1, 2, 9, c3(SUIT2)); frect(cx + 2, y - 2, 2, 1, c3(SKIN)) end end local function draw_shell(x, yb) frect(x - 3, yb - 2, 7, 2, 232, 198, 190) frect(x - 2, yb, 5, 1, 190, 150, 150) frect(x - 3, yb - 4, 7, 2, 252, 226, 216) frect(x - 2, yb - 5, 5, 1, 252, 226, 216) frect(x - 1, yb - 4, 1, 2, 214, 160, 160); frect(x + 2, yb - 4, 1, 2, 214, 160, 160) frect(x, yb - 3, 2, 2, 255, 255, 250) end local function dpos(t) local cx, y if t < 30 then cx, y = 26, DY0 + floor(t / 20) % 2 elseif t < 210 then local u = (t - 30) / 180 cx = lerp(26, 34, ease(u)) + 5 * sin(t * TAU / 90) * sin(PI * u) y = lerp(DY0, DY1, ease(u)) elseif t < 330 then cx, y = lerp(34, 40, ease((t - 210) / 40)), DY1 elseif t < 570 then local u = (t - 330) / 240 cx = lerp(40, 26, ease(u)) + 5 * sin(t * TAU / 90) * sin(PI * u) y = lerp(DY1, DY0, ease(u)) else cx, y = 26, DY0 + floor(t / 20) % 2 end return cx, floor(y + 0.5) end local function arm_of(t) if t < 250 then return 0 elseif t < 283 then return 1 elseif t < 590 then return 2 end return 0 end local function shell_at(t, cx, y) if t >= 590 then return nil end if t < 275 then return SHX, SHY end if t < 300 then local p = ease((t - 275) / 25) return lerp(SHX, cx + 3, p), floor(lerp(SHY, y - 2, p) - 6 * sin(PI * p) + 0.5) end return cx + 3, y - 2 end local RAY = { { 6, 5, 0.0 }, { 19, 3, 1.7 }, { 33, 6, 3.1 }, { 46, 3, 4.4 }, { 57, 4, 5.5 } } local function draw_rays(t, cam) if cam > 104 then return end for i = 1, #RAY do local r = RAY[i] local a0 = 0.17 * (0.75 + 0.25 * sin(t * TAU / 200 + r[3])) for y = SURF + 4 * (max(cam - SURF, 0) // 4), min(104, cam + H - 1), 4 do local d = y - SURF blend_rect(floor(r[1] + d * 0.24 + 0.5), y - cam, r[2] + d // 30, 4, 236, 252, 255, a0 * (1 - d / 92)) end end end local FISH = { { 34, 0.28, 1 }, { 52, 0.42, -1 }, { 72, 0.56, 1 }, { 96, 0.28, -1 }, { 118, 0.42, 1 }, { 142, 0.56, -1 } } local FPART = { { 1, 0, 4, 2, 1 }, { 2, -1, 2, 1, 1 }, { 2, 2, 2, 1, 2 }, { 0, 0, 1, 2, 1 }, { -1, -1, 1, 4, 1 }, { 4, 0, 1, 1, 3 } } local FCOL = {} for i = 1, #FISH do local wr, wg, wb = water(FISH[i][1]) local d = (FISH[i][1] - 30) / 130 FCOL[i] = { { floor(lerp(255, wr + 40, d)), floor(lerp(176, wg + 20, d)), floor(lerp(100, wb, d * 0.8)) }, { floor(lerp(255, wr + 60, d)), floor(lerp(220, wg + 40, d)), floor(lerp(170, wb + 10, d * 0.8)) }, { 20, 30, 56 } } end local function draw_fish(t, cam) for i = 1, #FISH do local f = FISH[i] local sy = f[1] - cam if sy > -4 and sy < H + 2 then local x = (i * 23 + f[3] * t * f[2]) % (W + 20) - 10 for p = 1, #FPART do local q = FPART[p] local ox = f[3] == 1 and q[1] or (5 - q[1] - q[3] + 1) frect(x + ox, sy + q[2], q[3], q[4], c3(FCOL[i][q[5]])) end end end end local GAP = { 16, 12, 9, 7, 6 } local function draw_bubbles(t, cam) if NB == 0 then return end local gap = GAP[NB] for i = max(0, floor((t - 80) / gap)), floor(t / gap) do local tb = i * gap local cx, y = dpos(tb) if y > SURF + 2 then local age = t - tb local by = y + 6 - age * 0.6 if by > SURF + 1 then local sy = by - cam if sy > -2 and sy < H + 1 then fdot(cx + 4 + 1.2 * sin(age * 0.2 + i), sy, 220, 248, 255, 0.85) end end end end for i = 1, NB * 2 do local h = hash32(i * 0x9E3779B1) local v = (i % 2 == 0) and 0.5 or 0.75 local by = 186 - (umod(h >> 8, 150) + t * v) % 150 local sy = by - cam if sy > -2 and sy < H + 1 then fdot(4 + umod(h, 56) + 1.5 * sin(by * 0.15 + i), sy, 200, 240, 255, 0.6) end end end local function ripple(cx, sy, u) if sy < 0 or sy >= H - 1 then return end local r = u * 9 local a = 0.6 * (1 - u) blend_rect(floor(cx - r), sy, floor(2 * r) + 1, 1, 255, 255, 255, a) blend_rect(floor(cx - r * 0.6), sy + 1, floor(1.2 * r) + 1, 1, 255, 255, 255, a * 0.6) local up = floor(sin(u * PI) * 5) blend(floor(cx - r * 0.8), sy - 2 - up, 255, 255, 255, a) blend(floor(cx + r * 0.8), sy - 2 - up, 255, 255, 255, a) end local function glint(x, y, s) rect(x, y, 1, 1, 255, 255, 255) if s >= 1 then blend(x - 1, y, 255, 255, 255, 0.8); blend(x + 1, y, 255, 255, 255, 0.8) blend(x, y - 1, 255, 255, 255, 0.8); blend(x, y + 1, 255, 255, 255, 0.8) end if s >= 2 then blend(x - 2, y, 255, 255, 255, 0.5); blend(x + 2, y, 255, 255, 255, 0.5) blend(x, y - 2, 255, 255, 255, 0.5); blend(x, y + 2, 255, 255, 255, 0.5) end end function on_settings() PC = PACE[cfg.pace] or 100 NB = cfg.bubbles or 3 end function render() if BUILT < 3 then draw_page(BUILT) BUILT = BUILT + 1 PG[BUILT] = snapshot(0, 0, W, H) end local t = ((frame % 72000) * PC % (CYC * 100)) / 100 local cx, y = dpos(t) local cam = max(0, min(2 * H, y - 26)) for k = 1, BUILT do local py = (k - 1) * H - cam if py > -H and py < H then blit_rgb(PG[k], 0, py, W, H) end end draw_rays(t, cam) if cam < SURF then for i = 1, 8 do local a = sin(t * TAU / 60 + i * 2.1) if a > 0 then blend(4 + umod(hash32(i * 0x85EBCA6B), 56), SURF - cam, 255, 255, 255, a * 0.9) end end end draw_fish(t, cam) draw_bubbles(t, cam) if t > 26 and t < 50 then ripple(26, SURF - cam, (t - 26) / 24) end if t > 548 and t < 572 then ripple(cx, SURF - cam, (t - 548) / 24) end local sy = y - cam draw_diver(cx, sy, arm_of(t), floor(t / 8) % 2) local sx, sb = shell_at(t, cx, y) if sx then draw_shell(sx, sb - cam) if t > 300 and t < 345 then glint(floor(sx + 0.5), sb - cam - 3, floor((1 + sin((t - 300) * 0.5)) * 1.5)) end end end