info = { type = "image", title = "Moonlit leaves", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Autumn leaves tumbling in a night forest", api = 44, } local tr = ({ sk = { ["Leaves"] = "Lístie", ["Wind"] = "Vietor", ["Calm"] = "Bezvetrie", ["Breeze"] = "Vánok", ["Gusty"] = "Nárazový", ["Storm"] = "Víchrica" }, cs = { ["Leaves"] = "Listí", ["Wind"] = "Vítr", ["Calm"] = "Bezvětří", ["Breeze"] = "Vánek", ["Gusty"] = "Nárazový", ["Storm"] = "Vichřice" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "leaves", label = T("Leaves"), type = "int", min = 3, max = 24, default = 14 }, { key = "wind", label = T("Wind"), type = "int", default = "breeze", opts = { keys = { "calm", "breeze", "gusty", "storm" }, T("Calm"), T("Breeze"), T("Gusty"), T("Storm") } }, } 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, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function disc(cx, cy, r, c, seed) for dy = -r, r do local hw = floor(sqrt(r * r - dy * dy) + (vn(dy + cy * 7, seed + cx) - 0.5) * 1.8 + 0.5) if hw >= 0 then rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3]) end end end local function line(x0, y0, x1, y1, c) local n = max(math.abs(x1 - x0), math.abs(y1 - y0)) for i = 0, n do local t = i / n px(floor(x0 + (x1 - x0) * t + 0.5), floor(y0 + (y1 - y0) * t + 0.5), c[1], c[2], c[3]) end end local BLOBS = { { 12, 32, 11 }, { 24, 27, 10 }, { 6, 38, 8 }, { 22, 40, 9 }, { 45, 35, 11 }, { 56, 30, 9 }, { 37, 40, 8 }, { 55, 42, 8 }, } local TONES = { { 0, 0, 0, { 92, 34, 40 } }, { 1, -1, 2, { 170, 66, 30 } }, { 2, -2, 4, { 228, 116, 32 } }, { 3, -3, 6, { 255, 174, 54 } }, } local function st_sky() for y = 0, 47 do local t = y / 47 rect(0, y, W, 1, floor(5 + 20 * t + 0.5), floor(8 + 26 * t + 0.5), floor(34 + 56 * t + 0.5)) end local MX, MY, MR = 48, 10, 6 for y = MY - 12, MY + 12 do for x = MX - 12, MX + 12 do local d = sqrt((x - MX) ^ 2 + (y - MY) ^ 2) if d <= MR + 0.3 then local d2 = sqrt((x - MX + 3.2) ^ 2 + (y - MY + 2.2) ^ 2) if d2 > MR - 0.8 then px(x, y, 216, 222, 242) end elseif d <= MR + 3 then blend(x, y, 120, 140, 230, 0.16) elseif d <= MR + 6 then blend(x, y, 100, 120, 220, 0.07) end end end for i = 0, 8 do local cx = i * 9 + floor(vn(i, 5) * 4) disc(cx, 46 + floor(vn(i, 6) * 3), 8 + floor(vn(i, 7) * 3), { 22, 18, 54 }, 21) end end local function st_canopy() for k = 1, #TONES do local T_ = TONES[k] for i = 1, #BLOBS do local b = BLOBS[i] local r = b[3] - T_[3] if r >= 2 then disc(b[1] + T_[1], b[2] + T_[2], r, T_[4], 31 + k) end end end end local function st_ground() rect(0, 53, W, H - 53, 10, 8, 32) local bark, lit, br = { 44, 22, 28 }, { 96, 50, 38 }, { 56, 28, 30 } for _, tx in ipairs({ 12, 44 }) do rect(tx, 40, 4, 24, bark[1], bark[2], bark[3]) rect(tx + 3, 40, 1, 24, lit[1], lit[2], lit[3]) rect(tx - 1, 61, 6, 3, bark[1], bark[2], bark[3]) line(tx + 1, 44, tx - 6, 35, br); line(tx + 2, 42, tx + 8, 36, br) line(tx - 3, 38, tx - 5, 33, br); line(tx + 6, 38, tx + 9, 32, br) end for i = 1, 26 do local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, W), 54 + umod(h >> 8, 9) blend(x, y, 214, 100, 34, 0.55) if i % 3 == 0 then blend(x + 1, y, 214, 100, 34, 0.4) end end end local STAGES = { st_sky, st_canopy, st_ground } local STAGE, PART, BG = 1, nil, nil local function build() if STAGE > 1 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() PART = snapshot(0, 0, W, H) STAGE = STAGE + 1 if STAGE > #STAGES then BG = PART end end local STARS = {} for i = 1, 22 do local h = hash32(i * 0xC2B2AE35) local x, y = umod(h, W), 1 + umod(h >> 8, 17) if (x - 48) ^ 2 + (y - 10) ^ 2 > 100 and (y > 9 or x < 22 or x > 34 or i % 2 == 0) then STARS[#STARS + 1] = { x, y, 0.8 + umod(h >> 16, 20) / 10, umod(h >> 20, 628) / 100 } end end local function stars(t) for i = 1, #STARS do local s = STARS[i] local v = sin(t * s[3] + s[4]) if v > 0.55 then px(s[1], s[2], 236, 240, 255) elseif v > -0.3 then px(s[1], s[2], 140, 150, 214) else px(s[1], s[2], 66, 76, 140) end end end local SMALL = { { 0, 0, 1, 0 }, { 0, 0, 1, 1 }, { 0, 0, 0, 1 }, { 1, 0, 0, 1 } } local BIG = { { 0, 0, 1, 0, 2, 0, 1, 1 }, { 0, 0, 1, 0, 1, 1, 2, 1 }, { 1, 0, 1, 1, 1, 2 }, { 2, 0, 1, 0, 1, 1, 0, 1 }, } local COLS = { { 255, 204, 70 }, { 150, 50, 28 }, { 74, 28, 28 }, { 255, 140, 40 } } local LF = {} for i = 1, 24 do local h = hash32(i * 0x6C8E9CF5) local c = COLS[umod(h >> 4, 4) + 1] LF[i] = { x0 = umod(h, W + 16), y0 = umod(h >> 6, H + 16), v = 5 + umod(h >> 12, 70) / 10, vx = (umod(h >> 16, 30) - 12) / 10, amp = 2 + umod(h >> 20, 40) / 10, w = 0.6 + umod(h >> 24, 12) / 10, ph = umod(h >> 8, 628) / 100, spin = 1.1 + umod(h >> 26, 15) / 10, k = 0.6 + umod(h >> 10, 8) / 10, r = c[1], g = c[2], b = c[3], big = i % 2 == 0 } end local GUST = 13 local KW = { calm = 0, breeze = 4, gusty = 9, storm = 15 } local OM = 6.28318 / GUST local function pushed(t) local u = t * OM local s = sin(u) return (2.5 * u - 4 * s + 0.75 * sin(2 * u) + s * s * s / 3) / (8 * OM) end local function leaf(l, t, push) local y = (l.y0 + t * l.v) % (H + 16) - 8 local a = min(1, (y + 8) / 8, (61 - y) / 8) if a <= 0.02 then return end local x = (l.x0 + t * l.vx + l.k * push + l.amp * sin(t * l.w + l.ph)) % (W + 16) - 8 local cs = (l.big and BIG or SMALL)[floor(t * l.spin + l.ph * 2) % 4 + 1] local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local w1, w2, w3, w4 = (1 - fx) * (1 - fy), fx * (1 - fy), (1 - fx) * fy, fx * fy for c = 1, #cs, 2 do local cx, cy = ix + cs[c], iy + cs[c + 1] if w1 > 0.1 then blend(cx, cy, l.r, l.g, l.b, w1 * a) end if w2 > 0.1 then blend(cx + 1, cy, l.r, l.g, l.b, w2 * a) end if w3 > 0.1 then blend(cx, cy + 1, l.r, l.g, l.b, w3 * a) end if w4 > 0.1 then blend(cx + 1, cy + 1, l.r, l.g, l.b, w4 * a) end end end function render() if not BG then build() if not BG then gfx.ready(false) end return end blit_rgb(BG, 0, 0, W, H) local t = frame / FPS stars(t) local push = (KW[cfg.wind] or 4) * pushed(t) for i = 1, cfg.leaves or 14 do leaf(LF[i], t, push) end end