info = { type = "image", title = "Brass diving helmet", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Brass helmet on the seabed, bubbles and fish", api = 44, } local ltr = ({ sk = { ["Bubbles"] = "Bublinky", ["Fish"] = "Rybky", ["Caustics"] = "Odlesky" }, cs = { ["Bubbles"] = "Bublinky", ["Fish"] = "Rybky", ["Caustics"] = "Odlesky" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "bubbles", label = LT("Bubbles"), type = "int", min = 0, max = 10, default = 6 }, { key = "fish", label = LT("Fish"), type = "int", min = 0, max = 8, default = 5 }, { key = "caustic", label = LT("Caustics"), type = "int", min = 0, max = 4, default = 2 }, } local rect, px, blend_rect, blend = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend local blit_rgb, snapshot, blend_mask = gfx.blit_rgb, gfx.snapshot, gfx.blend_mask local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, 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 FY = 36 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 W_TOP, W_BOT = { 6, 34, 118 }, { 34, 124, 214 } local S_FAR, S_NEAR = { 156, 214, 212 }, { 84, 160, 180 } local function water() for y = 0, FY - 1 do rect(0, y, W, 1, rgb(mix(W_TOP, W_BOT, y / (FY - 1)))) end local PIL = { { 40, 14, 9 }, { 49, 22, 6 }, { 56, 10, 8 } } for _, p in ipairs(PIL) do for x = p[1] - p[3], p[1] + p[3] do local h = floor(p[2] * (1 - math.abs(x - p[1]) / p[3])) if h > 0 then for y = FY - h, FY - 1, 2 do blend_rect(x, y, 1, 2, 8, 32, 98, 0.5 - 0.3 * (y - (FY - h)) / h) end end end end for k = 0, 1 do for y = 0, FY - 1 do blend_rect(floor(6 + k * 26 + y * 0.45), y, 5 + k * 2, 1, 200, 240, 255, 0.05) end end for y = FY, H - 1 do rect(0, y, W, 1, rgb(mix(S_FAR, S_NEAR, ((y - FY) / (H - 1 - FY)) ^ 0.8))) end end local function sand() for i = 1, 22 do local h = hash32(i * 0x85EBCA6B) local y = FY + 1 + umod(h, H - FY - 2) local len = 6 + umod(h >> 8, 16) local x = umod(h >> 14, W) blend_rect(x, y, len, 1, 255, 255, 255, 0.1) blend_rect(x + 1, y + 1, len - 2, 1, 6, 60, 110, 0.1) end for i = 0, 5 do local x = 49 + i * 3 local hh = 4 + umod(hash32(i * 0x9E3779B1), 5) for k = 0, hh do local sw = floor(1.3 * sin(k * 0.8 + i) + 0.5) px(x + sw, 62 - k, 34 + k * 3, 150 + k * 3, 128 - k * 2) end end for k = 0, 4 do local rx, ry = 20 - k * 2.6, 4 - k * 0.7 for y = 58 - floor(ry), 58 + floor(ry) do local u = (y + 0.5 - 58.5) / ry if u > -1 and u < 1 then local hw = floor(rx * sqrt(1 - u * u)) blend_rect(25 - hw, y, hw * 2, 1, 4, 44, 96, 0.14) end end end end local SX0, SY0, SW, SH = 5, 24, 34, 36 local CX, CY, R = 17.0, 16.5, 13.5 local KEY = { 255, 0, 254 } local BRK = { { 0, 38, 26, 12 }, { 0.3, 104, 70, 18 }, { 0.6, 200, 148, 34 }, { 0.85, 250, 214, 100 }, { 1, 255, 244, 190 } } local function ramp(t) if t > 1 then t = 1 elseif t < 0 then t = 0 end for i = 1, #BRK - 1 do local a, b = BRK[i], BRK[i + 1] if t <= b[1] then local u = (t - a[1]) / (b[1] - a[1]) return a[2] + (b[2] - a[2]) * u, a[3] + (b[3] - a[3]) * u, a[4] + (b[4] - a[4]) * u end end return 255, 244, 190 end local LX, LY, LZ = -0.5, -0.62, 0.6 local HX, HY, HZ = -0.28, -0.347, 0.895 local BOUNCE, GRAZE = { 74, 170, 190 }, { 30, 96, 150 } local function brass(nx, ny, nz, spec, lift) local d = nx * LX + ny * LY + nz * LZ if d < 0 then d = 0 end local t = 0.14 + 0.78 * d + (lift or 0) local r, g, b = ramp(t) local s = nx * HX + ny * HY + nz * HZ if s > 0.85 then local s2 = s * s; s2 = s2 * s2; s2 = s2 * s2; s2 = s2 * s2; s2 = s2 * s2 local k = spec * s2 r, g, b = r + (255 - r) * k, g + (250 - g) * k, b + (214 - b) * k end if ny > 0.2 then local w = 0.36 * (ny - 0.2) / 0.8 r, g, b = r + (BOUNCE[1] - r) * w, g + (BOUNCE[2] - g) * w, b + (BOUNCE[3] - b) * w end local q = 1 - nz local gz = q * q * q * 0.55 return r + (GRAZE[1] - r) * gz, g + (GRAZE[2] - g) * gz, b + (GRAZE[3] - b) * gz end local FXN, FYN, FZN = 0.5, -0.09, 0.86 local WCX, WCY = CX + R * FXN, CY + R * FYN local GXN, GYN, GZN = -0.6, -0.06, 0.8 local function glass(gx, gy) local r, g, b = mix({ 10, 24, 44 }, { 40, 86, 112 }, (gy + 1) / 2) local hx, hy = (gx + 0.45) / 0.55, (gy + 0.5) / 0.35 local q = hx * hx + hy * hy if q < 1 then local a = 0.55 * (1 - q) r, g, b = r + (200 - r) * a, g + (230 - g) * a, b + (245 - b) * a end if gx * gx + gy * gy > 0.72 then r, g, b = r + 22, g + 36, b + 40 end return r, g, b end local function helm_px(x, y) local fx, fy = x + 0.5, y + 0.5 local dx, dy = fx - CX, fy - CY if dx * dx + dy * dy <= R * R then local nx, ny = dx / R, dy / R local nz = sqrt(max(0, 1 - nx * nx - ny * ny)) local dot = nx * FXN + ny * FYN + nz * FZN if dot > 0.865 then return glass((fx - WCX) / 6.4, (fy - WCY) / 7.4) end if dot > 0.80 then local ux, uy = fx - WCX, fy - WCY local l = sqrt(ux * ux + uy * uy) + 0.001 return brass(nx, ny, nz, 0.8, 0.26 * (-0.6 * ux - 0.8 * uy) / l) end local d2 = nx * GXN + ny * GYN + nz * GZN if d2 > 0.955 then return glass((fx - (CX + R * GXN)) / 3, (fy - (CY + R * GYN)) / 3) end if d2 > 0.93 then return brass(nx, ny, nz, 0.8, 0.12) end return brass(nx, ny, nz, 1.0) end if x >= 9 and x <= 13 and y >= 1 and y <= 6 then local u = (fx - 11.5) / 2.5 if y == 1 then return 40, 28, 14 end return brass(u, 0, sqrt(max(0, 1 - u * u)), 0.6, y == 2 and 0.2 or 0) end local u = (fx - CX) / 16 if y >= 27 and u > -1 and u < 1 then local bottom = 33.2 + 1.6 * sqrt(1 - u * u) if fy <= bottom then local lift = fy < 28.6 and 0.14 or -0.07 * (fy - 28.6) local ao = max(0, 1 - sqrt(dx * dx + dy * dy - R * R + 1) / 5) return brass(u, 0, sqrt(1 - u * u), 0.5, lift - 0.12 * ao) end end end local HELM, HPART = nil, nil local function helm_rows(ya, yb) for y = ya, yb do for x = 0, SW - 1 do local r, g, b = helm_px(x, y) if r then px(SX0 + x, SY0 + y, rgb(r, g, b)) end end end end local function bolts() for k = 0, 9 do local a = k * 0.6283 + 0.3 local bx, by = WCX + 6.7 * math.cos(a), WCY + 7.6 * sin(a) local ix, iy = floor(bx), floor(by) local dx, dy = ix + 0.5 - CX, iy + 0.5 - CY if dx * dx + dy * dy < (R - 1) ^ 2 then px(SX0 + ix, SY0 + iy, 255, 232, 140) px(SX0 + ix + 1, SY0 + iy + 1, 90, 58, 12) end end for i = 0, 6 do local bx = 3 + i * 4.6 local u = (bx - CX) / 16 px(SX0 + floor(bx), SY0 + 31, rgb(brass(u, 0, sqrt(max(0, 1 - u * u)), 1, 0.3))) end end local CM = { {}, {} } local function caustic_rows(m, ya, yb) local rows = CM[m] local A, B = 6.283185 * 6 / 64, 6.283185 * 2 / 28 local ph = m == 1 and 0 or 1.9 for y = ya, yb do local c = {} for x = 0, 63 do local v = sin(A * x + 1.7 * sin(B * y + ph)) + sin(6.283185 * 3 / 28 * y + 1.5 * sin(A * 0.5 * x + ph * 2)) local k = 1 - min(1, math.abs(v) * 1.1) c[x + 1] = char(floor(k * k * 255)) end rows[y + 1] = concat(c) end if yb == 27 then CM[m] = concat(rows) end end local BG, PART = nil, nil local STAGE = 1 local function helm_stage(ya, yb, first) return function() if first then rect(SX0, SY0, SW, SH, KEY[1], KEY[2], KEY[3]) else blit_rgb(HPART, SX0, SY0, SW, SH) end helm_rows(ya, yb) if yb == SH - 1 then bolts() end HPART = snapshot(SX0, SY0, SW, SH) if yb == SH - 1 then HELM = HPART end blit_rgb(BG, 0, 0, W, H) end end local STAGES = { water, sand, function() caustic_rows(1, 0, 13) end, function() caustic_rows(1, 14, 27) end, function() caustic_rows(2, 0, 13) end, function() caustic_rows(2, 14, 27) end, helm_stage(0, 5, true), helm_stage(6, 11), helm_stage(12, 17), helm_stage(18, 23), helm_stage(24, 29), helm_stage(30, SH - 1), } local function build() local st = STAGE if st == 2 then blit_rgb(PART, 0, 0, W, H) end STAGES[st]() if st == 1 then PART = snapshot(0, 0, W, H) end if st == 2 then BG = snapshot(0, 0, W, H) end STAGE = st + 1 end local BX, BY = 16, 24 local NB = 10 local BUB = {} for i = 1, NB do local h = hash32(i * 0x9E3779B1) BUB[i] = { ph = (i - 1) / NB, life = 4.2 + umod(h, 20) / 10, w = 2 + umod(h >> 6, 20) / 10, p0 = umod(h >> 12, 628) / 100 } end local function bubble(x, y, r, a) if r == 1 then blend(x, y, 230, 248, 255, 0.7 * a) elseif r == 2 then blend_rect(x - 1, y - 2, 3, 1, 230, 248, 255, 0.45 * a) blend_rect(x - 1, y + 2, 3, 1, 230, 248, 255, 0.45 * a) blend_rect(x - 2, y - 1, 1, 3, 230, 248, 255, 0.45 * a) blend_rect(x + 2, y - 1, 1, 3, 230, 248, 255, 0.45 * a) blend(x - 1, y - 1, 255, 255, 255, 0.9 * a) else blend_rect(x - 1, y - 3, 3, 1, 230, 248, 255, 0.45 * a) blend_rect(x - 1, y + 3, 3, 1, 230, 248, 255, 0.45 * a) blend_rect(x - 3, y - 1, 1, 3, 230, 248, 255, 0.45 * a) blend_rect(x + 3, y - 1, 1, 3, 230, 248, 255, 0.45 * a) blend(x - 2, y - 2, 230, 248, 255, 0.4 * a); blend(x + 2, y - 2, 230, 248, 255, 0.4 * a) blend(x - 2, y + 2, 230, 248, 255, 0.4 * a); blend(x + 2, y + 2, 230, 248, 255, 0.4 * a) blend_rect(x - 2, y - 1, 1, 2, 255, 255, 255, 0.85 * a) end end local FISH = {} for i = 1, 8 do local h = hash32(i * 0x27D4EB2F) FISH[i] = { y = 5 + umod(h, 27), v = 5 + umod(h >> 5, 40) / 5, x0 = umod(h >> 10, 100), d = umod(h >> 16, 2) * 2 - 1, dim = 0.62 + umod(h >> 18, 38) / 100, ph = umod(h >> 22, 628) / 100 } end local function seg(xf, y, len, r, g, b, a) r, g, b = floor(r), floor(g), floor(b) local il, ir = floor(xf), floor(xf + len) if ir <= il then blend_rect(il, y, 1, 1, r, g, b, a * len) return end if ir - il > 1 then blend_rect(il + 1, y, ir - il - 1, 1, r, g, b, a) end blend_rect(il, y, 1, 1, r, g, b, a * (1 - (xf - il))) blend_rect(ir, y, 1, 1, r, g, b, a * (xf + len - ir)) end local function fish(xf, y, d, k, wag) local bx = d > 0 and xf + 1 or xf seg(bx + 1, y - 1, 3, 190 * k, 146 * k + 10, 40, 1) seg(bx, y, 5, 236 * k, 206 * k + 6, 70, 1) seg(bx + 1, y + 1, 3, 255 * k, 238 * k + 6, 150, 1) local xt = d > 0 and xf or xf + 5 seg(xt, y - 1 + wag, 1.2, 214 * k, 170 * k, 50, 1) seg(xt, y + wag, 1.2, 214 * k, 170 * k, 50, 1) blend(floor(d > 0 and bx + 4 or bx + 0.5), y, 20, 22, 40, 0.9) 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) local cs = (cfg.caustic or 2) * 0.16 if cs > 0 then local o1 = floor(t * 5) % 64 local o2 = floor(t * 3.4) % 64 local a1, a2 = cs * (0.8 + 0.5 * sin(t * 0.9)), cs * (0.8 + 0.5 * sin(t * 0.9 + 3.14)) blend_mask(CM[1], 190, 246, 255, o1, FY, 64, 28, 64, 28, a1) blend_mask(CM[1], 190, 246, 255, o1 - 64, FY, 64, 28, 64, 28, a1) blend_mask(CM[2], 190, 246, 255, -o2, FY, 64, 28, 64, 28, a2) blend_mask(CM[2], 190, 246, 255, 64 - o2, FY, 64, 28, 64, 28, a2) end blit_rgb(HELM, SX0, SY0, SW, SH, KEY[1], KEY[2], KEY[3]) for i = 1, cfg.bubbles or 6 do local b = BUB[i] local p = (t / b.life + b.ph) % 1 local yf = BY - 52 * p local iy = floor(yf) local f = yf - iy local x = floor(BX + (0.8 + 4 * p) * sin(t * b.w + b.p0) + 0.5) local r = p < 0.35 and 1 or (p < 0.7 and 2 or 3) local fade = p > 0.85 and (1 - p) / 0.15 or 1 bubble(x, iy, r, (1 - f) * fade) bubble(x, iy + 1, r, f * fade) end for i = 1, cfg.fish or 5 do local f = FISH[i] local span = W + 16 local x = (f.x0 + t * f.v * f.d) % span - 8 fish(x, f.y, f.d, f.dim, (floor(t * 5 + f.ph) % 2)) end end