info = { type = "image", title = "Reef growth", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Time-lapse: bare seabed grows into a reef", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Fish"] = "Ryby", ["Corals"] = "Koraly", ["Mixed"] = "Zmiešané", ["Warm"] = "Teplé", ["Cool"] = "Chladné" }, cs = { ["Speed"] = "Rychlost", ["Fish"] = "Ryby", ["Corals"] = "Korály", ["Mixed"] = "Smíšené", ["Warm"] = "Teplé", ["Cool"] = "Chladné" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "fish", label = T("Fish"), type = "bool", default = true }, { key = "tint", label = T("Corals"), type = "int", default = "mixed", opts = { keys = { "mixed", "warm", "cool" }, T("Mixed"), T("Warm"), T("Cool") } }, } local rect, blend_rect, blend = gfx.rect, gfx.blend_rect, gfx.blend local floor, sin, sqrt, abs, min = math.floor, math.sin, math.sqrt, math.abs, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local SAND = 53 local CYC = 76 local PALE = { 196, 192, 182 } local function newgrid(w, h) local g = {}; for i = 1, w * h do g[i] = 0 end; return g end local function put(g, w, h, x, y, c) if x >= 0 and x < w and y >= 0 and y < h then g[y * w + x + 1] = c end end local function str(g) local parts = {} for i = 1, #g, 64 do parts[#parts + 1] = string.char(table.unpack(g, i, min(i + 63, #g))) end return table.concat(parts) end local GEN = {} GEN.stag = function(stage, var) local w, h = 23, 26 local g = newgrid(w, h) local L, depth = ({ 5, 8, 11, 15 })[stage], ({ 0, 1, 1, 2 })[stage] local function branch(x, y, dx, len, d) for i = 1, len do y = y - 1; x = x + dx local ix, iy = floor(x + 0.5), floor(y + 0.5) put(g, w, h, ix, iy, i == len and 2 or 1) if i <= 2 then put(g, w, h, ix + 1, iy, 1) end if d > 0 and i == floor(len * 0.55) then branch(x, y, dx - 0.4, floor(len * 0.7), d - 1) branch(x, y, dx + 0.4, floor(len * 0.7), d - 1) end end end local lean = var == 0 and 0.12 or -0.12 branch(11, h - 1, lean, L, depth) if stage >= 2 then branch(14, h - 1, 0.25 + lean, floor(L * 0.7), depth > 0 and depth - 1 or 0) end if stage >= 3 then branch(7, h - 1, -0.25 + lean, floor(L * 0.8), depth > 0 and depth - 1 or 0) end return str(g), w, h end GEN.dome = function(stage, var) local rx = ({ 2, 3, 5, 7 })[stage] local ry = floor(rx * 0.7 + 0.5) + 1 local w, h = 2 * rx + 1, ry + 1 local g = newgrid(w, h) for x = 0, w - 1 do local d = (x - rx) / (rx + 0.5) local top = floor(ry * sqrt(1 - d * d) + 0.5) for k = 0, top - 1 do local c = 1 if k == top - 1 then c = 2 elseif umod(hash32((x * 7 + k * 13 + var) * 0x9E3779B1), 5) == 0 then c = 3 end put(g, w, h, x, h - 1 - k, c) end end return str(g), w, h end GEN.fan = function(stage) local w, h = 15, 20 local g = newgrid(w, h) for y = h - 3, h - 1 do put(g, w, h, 7, y, 1) end for k = 1, ({ 4, 8, 12, 17 })[stage] do local hw, y = floor(k * 0.38) + 1, h - 3 - k for x = -hw, hw do if x == -hw or x == hw then put(g, w, h, 7 + x, y, 2) elseif (x + k) % 2 == 0 then put(g, w, h, 7 + x, y, 1) end end end return str(g), w, h end GEN.anem = function(stage, sw) local w, h = 19, 15 local g = newgrid(w, h) local nt, Lt, sh = ({ 3, 5, 7, 9 })[stage], ({ 3, 4, 6, 8 })[stage], ({ 2, 3, 4, 5 })[stage] for y = h - sh, h - 1 do for x = 8, 10 do put(g, w, h, x, y, 1) end end for i = 1, nt do local a = (i - (nt + 1) / 2) / ((nt + 1) / 2) for s = 1, Lt do local f = s / Lt put(g, w, h, floor(9 + a * (1 + f * Lt * 0.55) + sw * f * f * 1.5 + 0.5), floor(h - sh - s * (1 - 0.25 * abs(a)) + 0.5), s == Lt and 2 or 1) end end return str(g), w, h end GEN.grass = function(stage, sw) local w, h = 9, 26 local g = newgrid(w, h) local hh = ({ 6, 11, 17, 25 })[stage] for b = 0, 2 do local hb = floor(hh * ({ 0.7, 1, 0.82 })[b + 1]) for y = 0, hb - 1 do local k = y / hb put(g, w, h, floor(2 + b * 2 + (b - 1) * 1.2 * k + sw * 1.6 * k * k + 0.5), h - 1 - y, y == hb - 1 and 2 or 1) end end return str(g), w, h end GEN.tube = function(stage) local w, h = 11, 13 local g = newgrid(w, h) for b = 0, 2 do local hb = floor(({ 3, 6, 9, 12 })[stage] * ({ 0.8, 1, 0.65 })[b + 1]) for y = 0, hb - 1 do for x = 0, 2 do put(g, w, h, b * 4 + x, h - 1 - y, x == 0 and 2 or 1) end end put(g, w, h, b * 4 + 1, h - hb, 3) end return str(g), w, h end local SP = {} local function shape(kind, stage, var) local key = kind .. stage .. var local s = SP[key] if not s then local str_, w, h = GEN[kind](stage, var) s = { str_, w, h } SP[key] = s end return s end local WARM = { { { 232, 96, 86 }, { 255, 170, 120 }, { 150, 48, 56 } }, { { 244, 150, 50 }, { 255, 214, 110 }, { 160, 84, 30 } }, { { 236, 70, 120 }, { 255, 160, 190 }, { 140, 36, 76 } } } local COOL = { { { 150, 96, 214 }, { 206, 170, 255 }, { 84, 52, 140 } }, { { 60, 170, 196 }, { 150, 230, 240 }, { 30, 100, 130 } }, { { 96, 120, 230 }, { 170, 190, 255 }, { 54, 70, 150 } } } local GREEN = { { 60, 170, 90 }, { 150, 224, 120 }, { 30, 100, 60 } } local ELEM = { { "grass", 12, 0.04 }, { "tube", 0, 0.12 }, { "grass", 26, 0.16 }, { "stag", 3, 0.20 }, { "dome", 21, 0.08 }, { "fan", 38, 0.30 }, { "anem", 28, 0.42 }, { "grass", 52, 0.38 }, { "stag", 44, 0.52 }, { "dome", 52, 0.62 }, } local PAL = {} for i = 1, #ELEM do PAL[i] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } end local VIV = {} local ROCK = { 0, 0, 0, 96, 104, 116, 146, 154, 166, 62, 68, 80 } local ROCKS = { { 4, 4, 2 }, { 41, 3, 5 } } function on_settings() local tint = cfg.tint or "mixed" for i, e in ipairs(ELEM) do if e[1] == "grass" then VIV[i] = GREEN elseif tint == "warm" then VIV[i] = WARM[i % 3 + 1] elseif tint == "cool" then VIV[i] = COOL[i % 3 + 1] else VIV[i] = (i % 2 == 0 and WARM or COOL)[(i // 2) % 3 + 1] end end end local FISH = { clown = { w = 6, cols = { { 244, 110, 26 }, { 244, 110, 26 }, { 246, 246, 240 }, { 20, 20, 30 } }, parts = { { 1, 0, 4, 4, 1 }, { 3, 0, 1, 4, 3 }, { 4, 1, 1, 1, 4 } }, tail = { { { 0, 1, 1, 2, 2 } }, { { 0, 0, 1, 4, 2 } } } }, tang = { w = 10, cols = { { 36, 110, 230 }, { 255, 214, 60 }, { 20, 24, 60 }, { 10, 10, 24 } }, parts = { { 3, 0, 4, 1, 1 }, { 2, 1, 6, 4, 1 }, { 3, 5, 4, 1, 1 }, { 8, 2, 1, 2, 1 }, { 5, 1, 1, 4, 3 }, { 3, -1, 3, 1, 2 }, { 8, 1, 1, 1, 4 } }, tail = { { { 0, 1, 2, 4, 2 } }, { { 0, 0, 2, 6, 2 } } } }, fly = { w = 7, cols = { { 255, 220, 60 }, { 255, 220, 60 }, { 30, 30, 40 }, { 10, 10, 24 } }, parts = { { 2, 0, 3, 1, 1 }, { 1, 1, 5, 4, 1 }, { 2, 5, 3, 1, 1 }, { 5, 1, 1, 4, 3 }, { 5, 2, 1, 1, 4 } }, tail = { { { 0, 2, 1, 2, 2 } }, { { 0, 1, 1, 4, 2 } } } }, school = { w = 4, cols = { { 196, 214, 236 }, { 120, 150, 196 } }, parts = { { 1, 0, 3, 2, 1 } }, tail = { { { 0, 0, 1, 2, 2 } }, { { 0, 0, 1, 1, 2 } } } }, } local function draw_fish(f, ox, oy, dir, wag, a) local function part(p) local c = f.cols[p[5]] local x = dir > 0 and ox + p[1] or ox + f.w - p[1] - p[3] if a >= 1 then rect(x, oy + p[2], p[3], p[4], c[1], c[2], c[3]) else blend_rect(x, oy + p[2], p[3], p[4], c[1], c[2], c[3], a) end end for i = 1, #f.parts do part(f.parts[i]) end part(f.tail[wag + 1][1]) end local function fish(f, x, y, dir, t) local ix = floor(x) local fr = x - ix local wag = (floor(t * 6 + x * 0.3)) % 2 if fr < 0.1 then draw_fish(f, ix, y, dir, wag, 1) elseif fr > 0.9 then draw_fish(f, ix + 1, y, dir, wag, 1) else draw_fish(f, ix, y, dir, wag, 1 - fr); draw_fish(f, ix + 1, y, dir, wag, fr) end end local function leg(t, t0, dur, n) local u = t - t0 if u < 0 then return nil end local k = floor(u / dur) if k >= n then return nil end local fr = (u - k * dur) / dur if k % 2 == 0 then return -12 + fr * (W + 24), 1 end return W + 12 - fr * (W + 24), -1 end local function lerp(a, b, f) return a + (b - a) * f end function render() local sp = ({ 0.6, 0.8, 1, 1.4, 2 })[cfg.speed or 3] or 1 local t = (frame / FPS * sp + 38) % CYC local P if t < 3 then P = 0 elseif t < 37 then P = (t - 3) / 34 elseif t < 47 then P = 1 elseif t < 73 then P = 1 - (t - 47) / 26 else P = 0 end local cf = P ^ 0.8 for y = 0, SAND - 1 do local k = y / (SAND - 1) rect(0, y, W, 1, floor(lerp(lerp(110, 74, k), lerp(66, 22, k), cf)), floor(lerp(lerp(138, 98, k), lerp(178, 92, k), cf)), floor(lerp(lerp(146, 108, k), lerp(222, 158, k), cf))) end local sr, sg, sb = floor(lerp(168, 226, cf)), floor(lerp(160, 200, cf)), floor(lerp(138, 150, cf)) rect(0, SAND, W, H - SAND, sr, sg, sb) rect(0, SAND + 4, W, 1, sr - 14, sg - 14, sb - 12) rect(0, SAND + 8, W, 1, sr - 14, sg - 14, sb - 12) rect(9, SAND + 2, 18, 1, sr + 12, sg + 12, sb + 10) rect(36, SAND + 6, 20, 1, sr + 12, sg + 12, sb + 10) for i = 1, #ROCKS do local r = ROCKS[i] local s = shape("dome", r[3] == 2 and 3 or 4, r[3]) gfx.blit_indexed(s[1], ROCK, r[1], SAND + 3 - s[3], s[2], s[3], s[2], s[3], true) end local anem_stage = 0 for i = 1, #ELEM do local e = ELEM[i] local pr = (P - e[3]) / 0.22 local stage = pr <= 0 and 0 or min(4, floor(pr * 4) + 1) if stage > 0 then local sw = 0 if e[1] == "anem" or e[1] == "grass" then sw = ({ 0, 1, 0, -1 })[(frame // 12 + i) % 4 + 1] end local var = (e[1] == "anem" or e[1] == "grass") and sw or i % 2 local s = shape(e[1], stage, var) local pal, viv = PAL[i], VIV[i] or GREEN for c = 1, 3 do for k = 1, 3 do pal[c * 3 + k] = floor(PALE[k] + (viv[c][k] - PALE[k]) * cf + 0.5) end end gfx.blit_indexed(s[1], pal, e[2], SAND + 3 - s[3], s[2], s[3], s[2], s[3], true) if e[1] == "anem" then anem_stage = stage end end end if cfg.fish ~= false then if anem_stage >= 3 then for k = 0, 1 do local x = 28 + 6 + 8 * sin(t * (0.8 + 0.15 * k) + k * 2.2) local dir = (math.cos(t * (0.8 + 0.15 * k) + k * 2.2) >= 0) and 1 or -1 fish(FISH.clown, x - 3, SAND - 12 + 2 * sin(t * 1.7 + k) + k * 3, dir, t) end end local x, dir = leg(t, 22, 13, 3) if x then fish(FISH.tang, x, 20 + 5 * sin(t * 0.7), dir, t) end x, dir = leg(t, 31, 15, 2) if x then fish(FISH.fly, x, 34 + 3 * sin(t * 0.9), dir, t) end x, dir = leg(t, 27, 10, 3) if x then for k = 0, 5 do local o = (k % 3) * 5 + (k // 3) * 3 fish(FISH.school, x - dir * o, 10 + (k % 2) * 3 + (k // 2) * 2 + sin(t * 1.3 + k) * 1.5, dir, t) end end end for i = 1, 7 do local h = hash32(i * 0x85EBCA6B) local x = (umod(h, W) + t * 1.5) % W local y = (umod(h >> 8, SAND) - t * 2) % SAND local ix, iy = floor(x), floor(y) local fx = x - ix blend(ix, iy, 230, 246, 255, 0.5 * (1 - fx)) blend(ix + 1, iy, 230, 246, 255, 0.5 * fx) end end