info = { type = "image", title = "Flooded forest", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Misty blue forest and a still, mirroring flood", api = 44, } local tr = ({ sk = { ["Tone"] = "Odtieň", ["Blue"] = "Modrá", ["Teal"] = "Tyrkys", ["Mist"] = "Hmla", ["Drops"] = "Kvapky", ["Rare"] = "Zriedka", ["Normal"] = "Bežne", ["Often"] = "Často" }, cs = { ["Tone"] = "Odstín", ["Blue"] = "Modrá", ["Teal"] = "Tyrkys", ["Mist"] = "Mlha", ["Drops"] = "Kapky", ["Rare"] = "Zřídka", ["Normal"] = "Běžně", ["Often"] = "Často" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tone", label = T("Tone"), type = "int", default = "blue", opts = { keys = { "blue", "teal" }, T("Blue"), T("Teal") } }, { key = "mist", label = T("Mist"), type = "int", min = 0, max = 3, default = 2 }, { key = "drops", label = T("Drops"), type = "int", default = "normal", opts = { keys = { "rare", "normal", "often" }, T("Rare"), T("Normal"), T("Often") } }, } local rect, px, blend_rect, blit_rgb, snapshot = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ = 36 local TONES = { blue = { skyT = { 96, 154, 196 }, skyH = { 172, 220, 238 }, glow = { 222, 248, 254 }, far = { 68, 122, 160 }, mid = { 44, 96, 130 }, n1 = { 24, 64, 90 }, n2 = { 10, 38, 56 }, n2m = { 18, 58, 80 }, rim = { 64, 134, 164 }, mist = { 178, 224, 238 }, water = { 4, 34, 54 }, canopy = { 8, 32, 50 }, spark = { 130, 236, 232 } }, teal = { skyT = { 84, 156, 168 }, skyH = { 164, 224, 220 }, glow = { 222, 252, 242 }, far = { 62, 124, 130 }, mid = { 40, 96, 102 }, n1 = { 20, 66, 72 }, n2 = { 6, 42, 48 }, n2m = { 14, 62, 68 }, rim = { 58, 142, 140 }, mist = { 172, 228, 222 }, water = { 4, 40, 48 }, canopy = { 6, 36, 44 }, spark = { 156, 250, 206 } }, } local P = TONES.blue local function rgb(c, k) return floor(c[1] * k + 0.5), floor(c[2] * k + 0.5), floor(c[3] * k + 0.5) end local function lerp3(a, b, t) 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 function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function noise(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 MIR = false local function R(x, y, w, h, r, g, b, a) if MIR then y = 2 * HZ - y - h end if a then blend_rect(x, y, w, h, r, g, b, a) else rect(x, y, w, h, r, g, b) end end local function PX(x, y, r, g, b) if MIR then y = 2 * HZ - 1 - y end px(x, y, r, g, b) end local GX, GY = 42, 27 local function sky_row(y) return lerp3(P.skyT, P.skyH, (y / (HZ - 1)) ^ 1.1) end local function s_sky() for y = 0, HZ - 1 do local r, g, b = sky_row(y) R(0, y, W, 1, r, g, b) end local gl = P.glow for k = 0, 6 do local rx = 25 - k * 3.4 local ry = rx * 1.05 for dy = -floor(ry), floor(ry) do local hw = rx * sqrt(1 - (dy / ry) ^ 2) local y = GY + dy if y >= 0 and y < HZ then R(floor(GX - hw), y, floor(2 * hw) + 1, 1, gl[1], gl[2], gl[3], 0.075) end end end end local function veil(a0, a1) for y = 0, HZ - 1 do local r, g, b = lerp3({ sky_row(y) }, P.mist, 0.35) R(0, y, W, 1, r, g, b, a0 + (a1 - a0) * y / (HZ - 1)) end end local function trunks(seed, gap, wmin, wspan, col, corridor) local x = -2 local i = 0 while x < W do i = i + 1 local h = hash32(i * 0x85EBCA6B + seed) local w = wmin + (h & 0xFF) % wspan local inGap = x > GX - 9 and x < GX + 8 if not (inGap and corridor and ((h >> 8) & 3) ~= 0) then R(x, 0, w, HZ, col[1], col[2], col[3]) end x = x + gap + (h >> 12) % (gap // 2 + 1) end end local function s_far() trunks(11, 6, 1, 2, P.far, true); veil(0.3, 0.42) end local function s_mid() trunks(23, 9, 2, 2, P.mid, true); veil(0.2, 0.3) end local function s_n1() trunks(37, 13, 3, 2, P.n1, true) veil(0.1, 0.18) end local function near_trunk(x, w, dir) local n2, n2m, rim = P.n2, P.n2m, P.rim R(x, 0, w, HZ, n2[1], n2[2], n2[3]) local e = dir > 0 and x + w - 1 or x local m = dir > 0 and x + w - 3 or x + 2 R(m, 0, 1, HZ, n2m[1], n2m[2], n2m[3]) R(e, 0, 1, HZ, rim[1], rim[2], rim[3]) for k = 1, 5 do local h = hash32(x * 31 + k) R(x + 1 + (h & 0xFF) % (w - 2), (h >> 8) % 20, 1, 6 + (h >> 16) % 14, n2m[1], n2m[2], n2m[3]) end end local BRX, BRY = 27, 11 local function s_near() near_trunk(-3, 12, 1) near_trunk(54, 11, -1) near_trunk(19, 4, 1) local n2 = P.n2 for x = 8, BRX do local t = (x - 8) / (BRX - 8) local y = floor(16 - 8 * t + 3 * sin(t * 3.0) + 0.5) R(x, y, 1, t < 0.5 and 2 or 1, n2[1], n2[2], n2[3]) end R(15, 12, 1, 3, n2[1], n2[2], n2[3]); R(14, 11, 1, 1, n2[1], n2[2], n2[3]) R(21, 8, 1, 3, n2[1], n2[2], n2[3]); R(22, 7, 1, 1, n2[1], n2[2], n2[3]) local c = P.canopy for x = 0, W - 1 do local alley = max(0, 1 - math.abs(x - GX) / 14) local h = floor(2 + noise(x / 2.2, 5) * 5 + noise(x / 0.9, 9) * 1.5 - alley * 3.5 + 0.5) if h > 0 then R(x, 0, 1, h, c[1], c[2], c[3]) if noise(x / 1.3, 13) > 0.55 then R(x, h, 1, 1, P.n1[1], P.n1[2], P.n1[3]) end end end end local function s_water() local w = P.water for j = 0, H - HZ - 1 do blend_rect(0, HZ + j, W, 1, w[1], w[2], w[3], 0.3 + 0.32 * j / (H - HZ - 1)) end blend_rect(0, HZ, W, 1, P.glow[1], P.glow[2], P.glow[3], 0.22) for i = 1, 7 do local h = hash32(i * 0xC2B2AE35) blend_rect(8 + (h & 0xFF) % 44, HZ + 4 + (h >> 8) % 22, 6 + (h >> 16) % 8, 1, P.rim[1], P.rim[2], P.rim[3], 0.22) end end local STAGES = { s_sky, s_far, s_mid, s_n1, s_near, s_water } local BASE, PART, STAGE = nil, nil, 1 local function build() if STAGE > 1 then blit_rgb(PART, 0, 0, W, H) end local f = STAGES[STAGE] if f ~= s_water then MIR = false; f() MIR = true; f() MIR = false else f() end PART = snapshot(0, 0, W, H) if STAGE == #STAGES then BASE = PART end STAGE = STAGE + 1 end local DROP_T = { rare = 330, normal = 210, often = 120 } local PERIOD, NMIST = 210, 2 function on_settings() P = TONES[cfg.tone] or TONES.blue PERIOD = DROP_T[cfg.drops] or 210 NMIST = cfg.mist or 2 BASE, STAGE = nil, 1 end local MB = {} for i = 1, 8 do local h = hash32(i * 0x27D4EB2F) local water = i % 3 == 0 MB[i] = { y = water and 38 + (h & 7) or 12 + (h >> 4) % 20, len = 22 + (h >> 8) % 20, x0 = (h >> 12) % 100, v = (0.18 + ((h >> 20) & 7) / 20) * (i % 2 == 0 and 1 or -1), a = water and 0.12 or 0.14 } end local function mist_row(xl, xr, y, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end local m = P.mist blend_rect(il + 1, y, ir - il - 1, 1, m[1], m[2], m[3], a) blend_rect(il, y, 1, 1, m[1], m[2], m[3], a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, m[1], m[2], m[3], a * (xr - ir)) end local function ring(cx, cy, rx, a) local ry = max(1, floor(rx * 0.3 + 0.5)) local g = P.glow local prev = 0 for dy = ry, 0, -1 do local hw = floor(rx * sqrt(1 - (dy / ry) ^ 2) + 0.5) local w = hw - prev + 1 blend_rect(cx + prev, cy + dy, w, 1, g[1], g[2], g[3], a) blend_rect(cx - hw, cy + dy, w, 1, g[1], g[2], g[3], a) if dy > 0 then blend_rect(cx + prev, cy - dy, w, 1, g[1], g[2], g[3], a) blend_rect(cx - hw, cy - dy, w, 1, g[1], g[2], g[3], a) end prev = hw end end local SPX = {} for i = 1, 9 do local h = hash32(i * 0x9E3779B1 + 3) SPX[i] = { x = 24 + (h & 0x3F) % 34, y = HZ + 3 + (h >> 8) % 22, w = 1.6 + ((h >> 16) & 7) * 0.4, ph = (h >> 20) % 628 / 100 } end function render() if not BASE then build() if not BASE then gfx.ready(false) return end end blit_rgb(BASE, 0, 0, W, H) local t = frame / FPS for i = 1, NMIST * 2 do local b = MB[i] local L = b.len local x = (b.x0 + t * b.v * 10) % (W + 2 * L) - L mist_row(x + L * 0.25, x + L * 0.75, b.y, b.a * 0.8) mist_row(x, x + L, b.y + 1, b.a) mist_row(x - L * 0.1, x + L * 1.1, b.y + 2, b.a * 0.9) mist_row(x + L * 0.15, x + L * 0.85, b.y + 3, b.a * 0.6) end local sp = P.spark for i = 1, #SPX do local s = SPX[i] local v = sin(t * s.w + s.ph) if v > 0.4 then blend_rect(s.x, s.y, 2, 1, sp[1], sp[2], sp[3], (v - 0.4) * 0.9) end end local k = frame % PERIOD local g = P.glow local SW, FALL, WY, WX = 12, 22, 46, BRX if k < SW then blend_rect(BRX, BRY + 1, 1, 2, g[1], g[2], g[3], 0.25 + 0.6 * k / SW) elseif k < SW + FALL then local u = (k - SW) / FALL local y = floor(BRY + 2 + (WY - BRY - 3) * u * u + 0.5) blend_rect(WX, y, 1, 2, g[1], g[2], g[3], 0.9) end local r1 = k - SW - FALL if r1 >= 0 and r1 < 100 then local u = r1 / 100 ring(WX, WY, 1 + floor(14 * (1 - (1 - u) ^ 2) + 0.5), 0.5 * (1 - u)) end local r2 = r1 - 12 if r2 >= 0 and r2 < 80 then local u = r2 / 80 ring(WX, WY, 1 + floor(9 * (1 - (1 - u) ^ 2) + 0.5), 0.35 * (1 - u)) end end