info = { type = "image", title = "Last leaf", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Last leaf on a snowy branch breaks away", api = 33, } local tr = ({ sk = { ["Wind"] = "Vietor", ["Snow"] = "Sneh", ["Leaf"] = "List", ["Red"] = "Červený", ["Gold"] = "Zlatý", ["Green"] = "Zelený" }, cs = { ["Wind"] = "Vítr", ["Snow"] = "Sníh", ["Leaf"] = "List", ["Red"] = "Červený", ["Gold"] = "Zlatý", ["Green"] = "Zelený" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "wind", label = T("Wind"), type = "int", min = 1, max = 5, default = 3 }, { key = "snow", label = T("Snow"), type = "int", min = 0, max = 5, default = 3 }, { key = "leaf", label = T("Leaf"), type = "int", default = "red", opts = { keys = { "red", "gold", "green" }, T("Red"), T("Gold"), T("Green") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, ceil, sin, cos, exp, sqrt, abs, min, max = math.floor, math.ceil, math.sin, math.cos, math.exp, math.sqrt, math.abs, math.min, math.max local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local T0 = 0 local CYC, TD = 22, 11 local PX, PY = 36, 19 local ELL = 9.5 local LA, LB = 6.5, 4.0 local LEAFS = { red = { light = { 232, 112, 60 }, dark = { 150, 44, 34 }, vein = { 120, 36, 30 } }, gold = { light = { 255, 208, 84 }, dark = { 196, 128, 32 }, vein = { 150, 92, 24 } }, green = { light = { 112, 184, 72 }, dark = { 46, 110, 52 }, vein = { 30, 84, 44 } }, } local LC = LEAFS.red local WIND, NF = 1.25, 24 function on_settings() LC = LEAFS[cfg.leaf] or LEAFS.red WIND = 0.5 + 0.25 * (cfg.wind or 3) NF = (cfg.snow or 3) * 8 end local function mix(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 BG = nil local BARK, BARK_L, BARK_D = { 104, 72, 46 }, { 138, 100, 64 }, { 66, 44, 32 } local SNOW, SNOW_S = { 248, 252, 255 }, { 206, 222, 240 } local function yc(x) return 6 + 0.28 * x end local function th(x) return max(2, 9 - 0.13 * x) end local function twig(x0, y0, x1, y1) local n = max(abs(x1 - x0), abs(y1 - y0)) for i = 0, n do local x, y = floor(x0 + (x1 - x0) * i / n), floor(y0 + (y1 - y0) * i / n) rect(x, y, 2, 2, BARK[1], BARK[2], BARK[3]) px(x, y + 1, BARK_D[1], BARK_D[2], BARK_D[3]) px(x, y - 1, SNOW[1], SNOW[2], SNOW[3]) px(x + 1, y - 1, SNOW[1], SNOW[2], SNOW[3]) end end local function build() for y = 0, H - 1 do rect(0, y, W, 1, mix({ 84, 126, 176 }, { 186, 214, 238 }, y / (H - 1))) end for x = 0, W - 1 do local top = floor(56 + 1.5 * sin(x * 0.12) + sin(x * 0.31 + 1) + 0.5) rect(x, top, 1, H - top, 236, 242, 252) rect(x, top + 3, 1, H - top - 3, 222, 232, 248) rect(x, top + 6, 1, H - top - 6, 208, 220, 242) end for _, s in ipairs({ { 9, 57, 8 }, { 17, 58, 6 }, { 51, 58, 7 }, { 58, 57, 9 } }) do local x, y, h = s[1], s[2], s[3] for k = 0, h - 1 do local w = 1 + k // 2 rect(x - w, y - h + k + 1, 2 * w + 1, 1, 96, 130, 160) end rect(x, y, 1, 2, 72, 92, 112) end for y = 0, 30 do rect(0, y, floor(7 - 0.18 * y), 1, BARK[1], BARK[2], BARK[3]) end for y = 0, 24 do local w = floor(7 - 0.18 * y) rect(0, y, 2, 1, BARK_D[1], BARK_D[2], BARK_D[3]) if w > 3 then px(w - 1, y, BARK_L[1], BARK_L[2], BARK_L[3]) end end for x = 0, 54 do local t = th(x) local y0 = floor(yc(x) - t / 2) local cap = 2 + (hash32(x * 0x9E3779B1) & 1) local h = floor(t + 0.5) local a = max(1, h // 3) rect(x, y0 + 1, 1, a, BARK_L[1], BARK_L[2], BARK_L[3]) rect(x, y0 + 1 + a, 1, h - 2 * a, BARK[1], BARK[2], BARK[3]) rect(x, y0 + 1 + h - a, 1, a, BARK_D[1], BARK_D[2], BARK_D[3]) rect(x, y0 - cap + 1, 1, cap, SNOW[1], SNOW[2], SNOW[3]) px(x, y0 + 1, SNOW_S[1], SNOW_S[2], SNOW_S[3]) end for x = 0, 7 do local y0 = floor(yc(x) - th(x) / 2) rect(x, y0 - 3, 1, 3, SNOW[1], SNOW[2], SNOW[3]) end twig(26, 9, 33, 3) twig(44, 19, 51, 27) BG = gfx.snapshot(0, 0, W, H) end local function leaf(cx, cy, deg, al) local a = deg * 0.0174533 local ca, sa = cos(a), sin(a) local ex = LA * abs(ca) + LB * abs(sa) + 1 local ey = LA * abs(sa) + LB * abs(ca) + 1 local L, D, V = LC.light, LC.dark, LC.vein for y = floor(cy - ey), ceil(cy + ey) do local dy = y + 0.5 - cy for x = floor(cx - ex), ceil(cx + ex) do local dx = x + 0.5 - cx local u = dx * ca + dy * sa if u > -LA and u < LA then local v = dy * ca - dx * sa local s = (u / LA + 1) * 0.5 local w = LB * 4 * s * (1 - s) * (1.3 - 0.6 * s) local e = w - abs(v) + 0.5 if e > 0 then local cov = e > 1 and 1 or e local r, g, b if abs(v) < 0.45 and s > 0.1 and s < 0.92 then r, g, b = V[1], V[2], V[3] else local k = v / (w + 0.3) * 0.5 + 0.5 if k < 0 then k = 0 elseif k > 1 then k = 1 end r, g, b = mix(D, L, k) end cov = cov * al if cov > 0.97 then px(x, y, r, g, b) elseif cov > 0.03 then blend(x, y, r, g, b, cov) end end end end end end local function bump(t, c, w) local u = (t - c) / w; return exp(-u * u) end local function swing(t) local g = 0.9 * bump(t, 4.5, 1.6) + 1.4 * bump(t, 10.4, 1.3) local amp, bias = (0.04 + 0.12 * g) * WIND, 0.09 * g * WIND return bias + amp * (0.7 * sin(t * 3.14) + 0.3 * sin(t * 4.8 + 1)) end local function flakes(ft) for i = 1, NF do local h = hash32(i * 0x9E3779B1) local near = i % 2 == 0 local sp = (near and 11 or 6) * (0.8 + 0.4 * ((h >> 8) & 255) / 255) local x = (h & 63) + 2.5 * sin(ft * 0.7 + ((h >> 16) & 255) / 40) + (near and 1 or 0.5) * ft * 0.6 x = floor(x % (W + 4) - 2 + 0.5) local y = ((h >> 4) & 127) + sp * ft y = y % (H + 6) - 3 local iy = floor(y) local f = y - iy local al = near and 0.95 or 0.55 blend(x, iy, 255, 255, 255, al * (1 - f)) blend(x, iy + 1, 255, 255, 255, al * f) end end function render() if not BG then build() end gfx.blit_rgb(BG, 0, 0, W, H) local ft = (frame + T0) / FPS local t = ft % CYC local STUB = t >= TD if not STUB then local al = min(1, t / 1.5) local th_ = swing(t) local cx, cy = PX + ELL * sin(th_), PY + ELL * cos(th_) local bx, by = cx, cy - LA for i = 0, 3 do blend(floor(PX + (bx - PX) * i / 3 + 0.5), floor(PY + (by - PY) * i / 3 + 0.5), 96, 60, 36, al) end leaf(cx, cy, 90, al) else px(PX, PY, 96, 60, 36); px(PX, PY + 1, 96, 60, 36) local tau = t - TD local th0 = swing(TD) local env = 1 - exp(-tau) local x = PX + ELL * sin(th0) + 2.4 * tau + 8 * env * sin(1.9 * tau) local y = PY + ELL * cos(th0) + 5.0 * tau + 1.2 * env * sin(3.8 * tau) if y < H + 9 then local deg = 90 - 60 * env * cos(1.9 * tau) leaf(x, y, floor(deg / 22.5 + 0.5) * 22.5, 1) end end flakes(ft) end