info = { type = "image", title = "Palm fireflies", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Layered palm forest at dusk with fireflies", api = 33, } local ltr = ({ sk = { ["Speed"] = "Rýchlosť", ["Time of day"] = "Denná doba", ["Dusk"] = "Súmrak", ["Night"] = "Noc", ["Fireflies"] = "Svetlušky" }, cs = { ["Speed"] = "Rychlost", ["Time of day"] = "Denní doba", ["Dusk"] = "Soumrak", ["Night"] = "Noc", ["Fireflies"] = "Světlušky" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 3, default = 2 }, { key = "time", label = LT("Time of day"), type = "int", default = "dusk", opts = { keys = { "dusk", "night" }, LT("Dusk"), LT("Night") } }, { key = "flies", label = LT("Fireflies"), type = "int", min = 0, max = 16, default = 10 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, abs, min = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.min local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local TAU = 6.2831853 local function rnd(v) return floor(v + 0.5) end local SKIES = { dusk = { { 0, 38, 34, 98 }, { 16, 86, 52, 128 }, { 30, 170, 76, 122 }, { 40, 238, 124, 92 }, { 53, 255, 196, 120 }, layers = { { 136, 70, 120 }, { 96, 48, 104 }, { 58, 32, 80 }, { 22, 14, 40 } }, }, night = { { 0, 6, 10, 40 }, { 24, 14, 24, 78 }, { 40, 34, 56, 118 }, { 53, 70, 94, 150 }, layers = { { 32, 46, 96 }, { 22, 32, 76 }, { 14, 20, 52 }, { 6, 8, 26 } }, }, } local SKY, MODE = nil, SKIES.dusk local function sky_row(y) for i = 2, #MODE do local a, b = MODE[i - 1], MODE[i] if y <= b[1] then local f = (y - a[1]) / (b[1] - a[1]) return rnd(a[2] + (b[2] - a[2]) * f), rnd(a[3] + (b[3] - a[3]) * f), rnd(a[4] + (b[4] - a[4]) * f) end end local e = MODE[#MODE] return e[2], e[3], e[4] end local function disc(cx, cy, r, cr, cg, cb) for dy = -r, r do local hw = rnd(sqrt(r * r + 0.5 - dy * dy)) rect(cx - hw, cy + dy, 2 * hw, 1, cr, cg, cb) end end local function bake_sky() for y = 0, 53 do local r, g, b = sky_row(y) rect(0, y, W, 1, r, g, b) end if MODE == SKIES.dusk then disc(34, 45, 10, 255, 168, 96) blend_rect(0, 38, W, 10, 255, 176, 104, 0.12) disc(34, 45, 7, 255, 238, 176) else disc(47, 13, 6, 56, 74, 140) disc(47, 13, 4, 236, 240, 255) px(46, 12, 198, 206, 230); px(48, 14, 198, 206, 230); px(47, 15, 210, 216, 238) end SKY = snapshot(0, 0, W, 54) end local LAY = { { y0 = 26, y1 = 54, base = 48, amp = 1.5, k = 2, n = 6, h0 = 9, h1 = 13, fl = 7, thick = 1, fr = 0, spd = 0.125, ph = 2 }, { y0 = 22, y1 = 57, base = 53, amp = 2, k = 3, n = 5, h0 = 15, h1 = 20, fl = 9, thick = 1, fr = 1, spd = 0.25, ph = 2 }, { y0 = 18, y1 = 63, base = 58, amp = 2, k = 2, n = 4, h0 = 22, h1 = 28, fl = 12, thick = 2, fr = 2, spd = 0.5, ph = 1 }, { y0 = 6, y1 = 63, base = 62, amp = 1, k = 3, n = 2, h0 = 42, h1 = 48, fl = 12, thick = 3, fr = 2, spd = 1, ph = 1 }, } local AL = { 80, 56, 34, 14 } for l, L in ipairs(LAY) do L.palms = {} for i = 1, L.n do local h = hash32(l * 7919 + i * 104729) local P = { bx = (floor((i - 0.5) * W / L.n) + umod(h, 7) - 3) % W, h = L.h0 + umod(h >> 8, L.h1 - L.h0 + 1), lean = umod(h >> 14, 9) - 4, v = {} } if L.n == 2 then P.lean = i == 1 and 5 or -5; P.bx = i == 1 and 6 or 58 end for k = 1, 8 do P.v[k] = umod(h >> (k + 3), 100) / 100 end L.palms[i] = P end end local function ground(L, x) return L.base + L.amp * sin(TAU * L.k * x / W) + L.amp * 0.5 * sin(TAU * (2 * L.k + 1) * x / W + 1) end local function palm(L, P, off, shift, r, g, b, k0, k1) local bx = P.bx - shift + off if bx + P.lean < -L.fl - 3 or bx + P.lean > W + L.fl + 3 then return end local gy = rnd(ground(L, P.bx)) local th = L.thick local n = P.h * 2 if k0 == 1 then for i = 0, n do local t = i / n rect(rnd(bx + P.lean * t * t) - th // 2, rnd(gy - P.h * t), th, 1, r, g, b) end end local cx, cy = bx + P.lean, gy - P.h for k = k0, k1 do local ang = AL[(k - 1) % 4 + 1] * 0.01745 local vx, vy = cos(ang) * (k <= 4 and 1 or -1), -sin(ang) local len = L.fl * (0.85 + 0.3 * P.v[k]) local ns = floor(len * 2.2) + 3 for i = 0, ns do local t = i / ns local x, y = rnd(cx + len * vx * t), rnd(cy + len * (vy * t + 1.1 * t * t)) px(x, y, r, g, b) if L.fr > 0 and t > 0.15 then px(x, y + 1, r, g, b) end if L.fr > 1 and i % 2 == 1 and t > 0.3 then px(x, y + 2, r, g, b) end if th > 1 and t < 0.6 then px(x, y - 1, r, g, b) end end end end local TEX = { {}, {}, {}, {} } local function bake_layer(l, ph, a, b, k0, k1) local L = LAY[l] local c = MODE.layers[l] local shift = ph * 0.5 local h = L.y1 - L.y0 + 1 gfx.clear(255, 0, 255) if a > 1 or k0 > 1 then blit_rgb(TEX[l][ph + 1], 0, L.y0, W, h, 255, 0, 255) else for x = 0, W - 1 do local gy = rnd(ground(L, x + shift)) rect(x, gy, 1, L.y1 - gy + 1, c[1], c[2], c[3]) end end for i = a, b do for off = -W, W, W do palm(L, L.palms[i], off, shift, c[1], c[2], c[3], k0, k1) end end TEX[l][ph + 1] = snapshot(0, L.y0, W, h) end local STEPS = { { 0 } } for _, e in ipairs({ { 1, 0, 2 }, { 1, 1, 2 }, { 2, 0, 2 }, { 2, 1, 2 }, { 3, 0, 1 }, { 4, 0, 1 } }) do local L = LAY[e[1]] for a = 1, L.n, e[3] do local b = min(a + e[3] - 1, L.n) if e[1] >= 3 then STEPS[#STEPS + 1] = { e[1], e[2], a, b, 1, 4 } STEPS[#STEPS + 1] = { e[1], e[2], a, b, 5, 8 } else STEPS[#STEPS + 1] = { e[1], e[2], a, b, 1, 8 } end end end local STAGE = 1 function on_settings() MODE = SKIES[cfg.time] or SKIES.dusk SKY, TEX, STAGE = nil, { {}, {}, {}, {} }, 1 end local MUL = { 0.5, 1, 2 } local function layer(l, t) local L = LAY[l] local tex = TEX[l] if not tex[1] then return end local m = L.spd * MUL[cfg.speed or 2] local ix, img if L.ph == 1 then ix, img = floor(t * m) % W, tex[1] else local hp = floor(t * m * 2) ix, img = (hp >> 1) % W, tex[(hp & 1) + 1] if not img then return end end local h = L.y1 - L.y0 + 1 blit_rgb(img, -ix, L.y0, W, h, 255, 0, 255) if ix > 0 then blit_rgb(img, W - ix, L.y0, W, h, 255, 0, 255) end end local NFF = 16 local FF = {} for i = 1, NFF do local h = hash32(i * 0x9E3779B1) FF[i] = { cx = umod(h, W), cy = 28 + umod(h >> 6, 26), ax = 5 + umod(h >> 12, 10), ay = 3 + umod(h >> 16, 6), w1 = 0.008 + umod(h >> 20, 20) / 1500, w2 = 0.011 + umod(h >> 24, 20) / 1500, wb = 0.03 + umod(h >> 4, 20) / 700, p1 = umod(h >> 8, 628) / 100, p2 = umod(h >> 10, 628) / 100, p3 = umod(h >> 14, 628) / 100, } end local function fireflies(t, grp) for i = 1, min(cfg.flies or 10, NFF) do if i % 2 == grp then local f = FF[i] local x = (f.cx + f.ax * sin(t * f.w1 + f.p1)) % W local y = f.cy + f.ay * sin(t * f.w2 + f.p2) local g = 0.5 + 0.5 * sin(t * f.wb + f.p3) local a = g * g * g if a > 0.03 then local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, 200, 255, 120, a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, 200, 255, 120, a * fx * (1 - fy)) blend(ix, iy + 1, 200, 255, 120, a * (1 - fx) * fy) blend(ix + 1, iy + 1, 200, 255, 120, a * fx * fy) local rx, ry = rnd(x), rnd(y) blend_rect(rx - 1, ry, 3, 1, 180, 255, 100, a * 0.2) blend_rect(rx, ry - 1, 1, 3, 180, 255, 100, a * 0.2) end end end end local STARS = {} for i = 1, 14 do local h = hash32(i * 0x85EBCA6B) STARS[i] = { umod(h, W), umod(h >> 8, 30), umod(h >> 16, 628) / 100 } end function render() local s = STEPS[STAGE] if s then if s[1] == 0 then bake_sky() else bake_layer(s[1], s[2], s[3], s[4], s[5], s[6]) end STAGE = STAGE + 1 end blit_rgb(SKY, 0, 0, W, 54) local c2 = MODE.layers[2] rect(0, 54, W, 10, c2[1], c2[2], c2[3]) if MODE == SKIES.night then for i = 1, 14 do local st = STARS[i] blend(st[1], st[2], 236, 240, 255, 0.35 + 0.35 * sin(frame * 0.05 + st[3])) end end layer(1, frame) layer(2, frame) fireflies(frame, 0) layer(3, frame) fireflies(frame, 1) layer(4, frame) end