info = { type = "image", title = "Sea cave sunset", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Sunset sea seen from a cave, glints and a drip", api = 44, } local tr = ({ sk = { ["Tide"] = "Príliv", ["Drip"] = "Kvapka", ["Gull"] = "Čajka" }, cs = { ["Tide"] = "Příliv", ["Drip"] = "Kapka", ["Gull"] = "Racek" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tide", label = T("Tide"), type = "int", min = 1, max = 5, default = 3 }, { key = "drip", label = T("Drip"), type = "bool", default = true }, { key = "gull", label = T("Gull"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect 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 HZ = 26 local SX, SY = 45, 23 local CX, CY, RX, RY = 39, 42, 24, 33 local DX = 35 local WARM = { 232, 120, 48 } local function lerp(a, b, t) return a + (b - a) * t end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function mix(a, b, t) return rgb(lerp(a[1], b[1], t), lerp(a[2], b[2], t), lerp(a[3], b[3], t)) end local function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function sn(x, seed) local i = floor(x) local f = x - i f = f * f * (3 - 2 * f) return vn(i, seed) * (1 - f) + vn(i + 1, seed) * f end local NX, NYY = {}, {} for x = 0, W - 1 do NX[x] = (sn(x / 3.5, 11) - 0.5) * 0.10 end for y = 0, H - 1 do NYY[y] = (sn(y / 3.0, 23) - 0.5) * 0.10 end local LE, RE = {}, {} local ROCK = { { 8, 14, 28 }, { 12, 22, 40 }, { 18, 32, 54 }, { 10, 18, 34 }, { 24, 42, 66 } } local function fill_rock(x0, x1, y) local x = x0 while x <= x1 do local h = hash32((y * 64 + x) * 0x9E3779B1) local len = 1 + (h & 3) local c = ROCK[1 + umod(h >> 4, 3) + (y // 5 + x // 9) % 2] rect(x, y, min(len, x1 - x + 1), 1, c[1], c[2], c[3]) x = x + len end end local function rim(x, y, d) local k = 1 - (sqrt(d) - 1) * 26 / 5 if k > 0 then local s = y < 12 and 0.65 or (y < 46 and 0.92 or max(0.3, 0.92 - (y - 46) * 0.04)) local c = ROCK[2] px(x, y, mix(c, WARM, k * s)) end end local function rock_rows(ya, yb) for y = ya, yb do local dy = ((y - CY) / RY) ^ 2 + NYY[y] local xa, xb = nil, nil for x = 0, W - 1 do if dy + ((x - CX) / RX) ^ 2 + NX[x] < 1 then xa = x break end end if xa then for x = W - 1, xa, -1 do if dy + ((x - CX) / RX) ^ 2 + NX[x] < 1 then xb = x break end end LE[y], RE[y] = xa, xb if xa > 0 then fill_rock(0, xa - 1, y) end if xb < W - 1 then fill_rock(xb + 1, W - 1, y) end for x = max(0, xa - 6), xa - 1 do rim(x, y, dy + ((x - CX) / RX) ^ 2 + NX[x]) end for x = xb + 1, min(W - 1, xb + 6) do rim(x, y, dy + ((x - CX) / RX) ^ 2 + NX[x]) end else LE[y], RE[y] = 64, -1 fill_rock(0, W - 1, y) for x = 14, W - 1 do local d = dy + ((x - CX) / RX) ^ 2 + NX[x] if d < 1.45 then rim(x, y, d) end end end end end local SKY_T, SKY_H = { 112, 62, 100 }, { 255, 150, 64 } local HAZE, MID, NEAR = { 235, 138, 92 }, { 30, 120, 178 }, { 8, 60, 116 } local function sea_color(y) if y <= 33 then return mix(HAZE, MID, (y - HZ) / 7) end return mix(MID, NEAR, (y - 33) / 30) end local function stage_gradients() for y = 8, HZ - 1 do rect(0, y, W, 1, mix(SKY_T, SKY_H, (y - 8) / (HZ - 9))) end for y = HZ, H - 1 do rect(0, y, W, 1, sea_color(y)) end end local function stage_sun() local GR = 15 for y = SY - GR, HZ - 1 do local sr, sg, sb = mix(SKY_T, SKY_H, (y - 8) / (HZ - 9)) for x = SX - GR, SX + GR do local d = sqrt((x - SX) ^ 2 + (y - SY) ^ 2) if d < GR then local c, k if d < 3.4 then c, k = { 255, 248, 222 }, 1 elseif d < 5 then c, k = { 255, 226, 150 }, 0.9 else c, k = { 255, 200, 110 }, (1 - d / GR) * 0.85 end px(x, y, rgb(lerp(sr, c[1], k), lerp(sg, c[2], k), lerp(sb, c[3], k))) end end end end local function path_rows(ya, yb) for y = ya, yb do local hw = 2 + (y - HZ) * 0.42 local x = SX - hw while x < SX + hw do local h = hash32(y * 0x85EBCA6B + floor(x)) local len = 1 + (h & 3) local a = (0.64 - (y - HZ) * 0.012) * (1 - 0.7 * math.abs(x - SX) / hw) if (h >> 3) & 3 ~= 0 then blend_rect(floor(x), y, len, 1, 255, 200, 100, a) end x = x + len + ((h >> 6) & 1) end end end local function streaks() for i = 1, 80 do local h = hash32(i * 0xC2B2AE35) local y = 28 + umod(h, 34) local len = 2 + (y - HZ) // 5 + ((h >> 10) & 3) local x = umod(h >> 12, W) if i % 2 == 0 then blend_rect(x, y, len, 1, 96, 200, 230, 0.32) else blend_rect(x, y, len, 1, 6, 52, 100, 0.4) end end end local function blob(cx, cy, rx, ry, top, bot, warm) local pts = {} for y = cy - ry, cy + ry do local dy = (y - cy) / ry local hw = rx * sqrt(max(0, 1 - dy * dy)) local x0, x1 = floor(cx - hw + 0.5), floor(cx + hw + 0.5) local t = (y - (cy - ry)) / (2 * ry) rect(x0, y, x1 - x0 + 1, 1, mix(top, bot, t)) if warm and y == cy - ry + 1 then blend_rect(x0 + 1, y, x1 - x0 - 1, 1, WARM[1], WARM[2], WARM[3], 0.75) end pts[y] = x1 end return pts end local STONES_L = { { 18, 58, 9, 5 }, { 23, 61, 4, 2 } } local STONES_R = { { 58, 59, 9, 5 }, { 52, 62, 3, 2 } } local EL, ER = {}, {} local function edges() for y = 53, 63 do EL[y], ER[y] = -1, 99 for _, s in ipairs(STONES_L) do local dy = (y - s[2]) / s[4] if dy >= -1 and dy <= 1 then EL[y] = max(EL[y], floor(s[1] + s[3] * sqrt(1 - dy * dy) + 0.5)) end end for _, s in ipairs(STONES_R) do local dy = (y - s[2]) / s[4] if dy >= -1 and dy <= 1 then ER[y] = min(ER[y], floor(s[1] - s[3] * sqrt(1 - dy * dy) + 0.5)) end end end end local function stage_mid() for x = 17, 31 do rect(x, HZ - 1 - floor(2 * sin((x - 17) / 14 * 3.14) + 0.5), 1, 2 + floor(2 * sin((x - 17) / 14 * 3.14) + 0.5), 30, 44, 84) end for x = 56, 63 do rect(x, HZ - 1 - floor(2 * sin((x - 56) / 8 * 3.14) + 0.5), 1, 2 + floor(2 * sin((x - 56) / 8 * 3.14) + 0.5), 34, 48, 88) end blob(24, 39, 4, 3, { 70, 70, 100 }, { 6, 26, 50 }, true) blob(28, 40, 2, 1, { 60, 64, 96 }, { 6, 26, 50 }) blob(54, 45, 3, 2, { 70, 66, 96 }, { 6, 30, 56 }, true) end local function stage_front() rect(DX - 1, 10, 4, 2, 12, 22, 40) rect(DX, 12, 3, 2, 12, 22, 40) rect(DX, 14, 2, 1, 18, 32, 54) px(DX, 15, WARM[1] * 0.8, WARM[2] * 0.8, WARM[3] * 0.8) for _, s in ipairs(STONES_L) do blob(s[1], s[2], s[3], s[4], { 34, 74, 100 }, { 6, 20, 38 }, true) end for _, s in ipairs(STONES_R) do blob(s[1], s[2], s[3], s[4], { 34, 74, 100 }, { 6, 20, 38 }, true) end edges() end local BG = nil local STAGES = { stage_gradients, stage_sun, function() streaks(); stage_mid(); path_rows(27, 40) end, function() path_rows(41, 54) end, function() rock_rows(0, 15) end, function() rock_rows(16, 31) end, function() rock_rows(32, 47) end, function() rock_rows(48, 63) end, function() stage_front(); BG = gfx.snapshot(0, 0, W, H) end, } local STAGE = 1 local FOAM = { 206, 238, 242 } local COS, SIN = {}, {} for i = 0, 31 do COS[i], SIN[i] = math.cos(i * 0.19635), sin(i * 0.19635) end local function foam(y, t, tide) local s = 0.5 + 0.5 * sin(t * 0.035 + y * 0.7) local L = 1 + s * (1 + tide * 0.9) local n = floor(L) local f = L - n local xl = EL[y] + 1 blend_rect(xl, y, n, 1, FOAM[1], FOAM[2], FOAM[3], 0.5) blend(xl + n, y, FOAM[1], FOAM[2], FOAM[3], 0.5 * f) local s2 = 0.5 + 0.5 * sin(t * 0.035 + y * 0.7 + 2.1) L = 1 + s2 * (1 + tide * 0.9) n = floor(L) f = L - n local xr = ER[y] - 1 blend_rect(xr - n + 1, y, n, 1, FOAM[1], FOAM[2], FOAM[3], 0.5) blend(xr - n, y, FOAM[1], FOAM[2], FOAM[3], 0.5 * f) end local function ring(r, a, cy) if r < 1 then return end local ry = r * 0.32 for i = 0, 31 do blend(floor(DX + r * COS[i] + 0.5), floor(cy + ry * SIN[i] + 0.5), 226, 248, 255, a) end end local GP = { { 0, -1 }, { 0, 0 }, { 0, 1 }, { 0, 0 } } local function gpx(x, y, a) local ix = floor(x) local f = x - ix if ix > LE[y] and ix < RE[y] then blend(ix, y, 70, 32, 52, a * (1 - f)) end if ix + 1 > LE[y] and ix + 1 < RE[y] then blend(ix + 1, y, 70, 32, 52, a * f) end end local function gull(f) local ph = f % 600 if ph > 120 then return end local x = 66 - ph * 0.6 local y = 18 + floor(1.5 * sin(ph * 0.05) + 0.5) local p = GP[(f // 4) % 4 + 1] gpx(x, y, 0.95) gpx(x - 1, y + p[1], 0.9); gpx(x + 1, y + p[1], 0.9) gpx(x - 2, y + p[2], 0.9); gpx(x + 2, y + p[2], 0.9) end local NS = 16 function render() if STAGE <= #STAGES then STAGES[STAGE]() STAGE = STAGE + 1 gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame local pulse = 0.5 + 0.5 * sin(t * 0.07) blend_rect(SX - 10, SY, 21, 1, 255, 240, 200, 0.10 + 0.14 * pulse) blend_rect(SX, SY - 10, 1, 7, 255, 240, 200, 0.08 + 0.12 * pulse) for i = 1, NS do local cl = 40 + (i * 13) % 30 local u = t + i * 17 local h = hash32(i * 0x9E3779B1 + (u // cl) * 0x85EBCA6B) local y = 28 + umod(h, 25) local hw = 1 + (y - HZ) * 0.42 local x = floor(SX + (umod(h >> 8, 1000) / 500 - 1) * hw) local ph = (u % cl) / cl local s = sin(3.1416 * ph) if x > LE[y] and x + 1 < RE[y] then blend_rect(x, y, 1 + (y - HZ) // 14, 1, 255, 246, 218, s * s * 0.95) end end local sh = 0.5 + 0.5 * sin(t * 0.11) blend(15, 55, 255, 255, 255, 0.5 * sh); blend(20, 54, 255, 255, 255, 0.5 * (1 - sh)) blend(56, 56, 255, 255, 255, 0.5 * (1 - sh)); blend(61, 55, 255, 255, 255, 0.5 * sh) local tide = cfg.tide or 3 for y = 55, 62 do foam(y, t, tide) end if cfg.drip ~= false then local f = t % 150 if f < 30 then blend(DX, 16, 120, 200, 232, 0.9 * f / 30) elseif f < 48 then local k = (f - 30) / 18 local y = floor(17 + 40 * k * k) rect(DX, y, 1, 2, 120, 200, 232) else local a = f - 48 if a < 3 then blend(DX - 1, 56, 226, 248, 255, 0.8); blend(DX + 1, 56, 226, 248, 255, 0.8); blend(DX, 55, 226, 248, 255, 0.8) end if a < 52 then ring(1 + a * 0.12, 0.8 * (1 - a / 52), 58) end if a > 8 and a < 60 then ring(1 + (a - 8) * 0.11, 0.55 * (1 - (a - 8) / 52), 58) end end end if cfg.gull ~= false then gull(t) end end