info = { type = "image", title = "Gusty hill at sunset", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Lone tree on a hill, gusts carry autumn leaves", api = 33, } local tr = ({ sk = { ["Leaves"] = "Lístie", ["Gusts"] = "Nárazy vetra" }, cs = { ["Leaves"] = "Listí", ["Gusts"] = "Nárazy větru" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "leaves", label = T("Leaves"), type = "int", min = 0, max = 14, default = 9 }, { key = "gust", label = T("Gusts"), type = "int", min = 1, max = 5, default = 3 }, } 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, max, min = math.floor, math.sin, math.cos, math.sqrt, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local function lerp(a, b, t) return floor(a + (b - a) * t + 0.5) end local CREST = {} for x = 0, W - 1 do local d = x - 14 CREST[x] = floor(43 + (d > 0 and d * 0.2 or -d * 0.1) + 1.4 * sin(x * 0.21) + 0.5) end local TREE_X, TREE_Y = 14, CREST[14] local SKY = { { 0, 122, 44, 70 }, { 22, 226, 98, 58 }, { 46, 255, 180, 88 } } local BG local function disc(cx, cy, r, c1, c2, c3) for dy = -r, r do local hw = floor(sqrt(r * r - dy * dy) + 0.5) local xa, xb = cx - hw, cx + hw local w3 = max(1, (xb - xa + 1) // 3) rect(xa, cy + dy, w3, 1, c1[1], c1[2], c1[3]) rect(xa + w3, cy + dy, xb - xa + 1 - 2 * w3, 1, c2[1], c2[2], c2[3]) rect(xb - w3 + 1, cy + dy, w3, 1, c3[1], c3[2], c3[3]) end end local function build() for y = 0, 47 do local s = y < 22 and 1 or 2 local a, b = SKY[s], SKY[s + 1] local t = (y - a[1]) / (b[1] - a[1]) rect(0, y, W, 1, lerp(a[2], b[2], t), lerp(a[3], b[3], t), lerp(a[4], b[4], t)) end local SX, SY = 50, 38 for dy = -10, 10 do local hw = floor(sqrt(100 - dy * dy) + 0.5) blend_rect(SX - hw, SY + dy, 2 * hw + 1, 1, 255, 190, 96, 0.35) end for dy = -6, 6 do local hw = floor(sqrt(42 - dy * dy) + 0.5) rect(SX - hw, SY + dy, 2 * hw + 1, 1, 255, 238, 156) end for x = 0, W - 1 do local ft = floor(43 + 2 * sin(x * 0.17 + 2) + 1.5 * sin(x * 0.43) + 0.5) rect(x, ft, 1, H - ft, 190, 76, 56) local mt = floor(48 + 3 * sin(x * 0.12 + 1) + 0.5) rect(x, mt, 1, H - mt, 134, 50, 52) end for x = 0, W - 1 do rect(x, CREST[x], 1, H - CREST[x], 78, 30, 38) px(x, CREST[x], 188, 92, 54) end rect(0, 59, W, 5, 46, 20, 30) for y = TREE_Y, TREE_Y - 15, -1 do local x = floor(TREE_X + (TREE_Y - y) * 0.06 + 0.5) local w = y > TREE_Y - 4 and 4 or 3 rect(x - 1, y, w, 1, 48, 22, 28) px(x - 1 + w - 1, y, 118, 52, 36) end local DARK, MID, LIT = { 148, 46, 30 }, { 204, 76, 32 }, { 242, 132, 44 } local D2, M2, L2 = { 112, 36, 30 }, { 176, 60, 32 }, { 226, 108, 40 } local CROWN = { { 8, 24, 6 }, { 21, 23, 7 }, { 14, 14, 8 }, { 5, 15, 4 }, { 23, 14, 4 }, { 14, 22, 7 }, { 10, 8, 4 }, { 19, 8, 4 } } for i, c in ipairs(CROWN) do if i <= 3 then disc(c[1], c[2], c[3], D2, M2, L2) else disc(c[1], c[2], c[3], DARK, MID, LIT) end end for i = 1, 46 do local h = hash32(i * 0x85EBCA6B) local x, y = 3 + umod(h, 24), 5 + umod(h >> 8, 26) if (x - 14) ^ 2 / 144 + (y - 18) ^ 2 / 160 < 1 then if x > 14 then rect(x, y, 2, 1, 255, 176, 72) else rect(x, y, 1, 1, 92, 30, 28) end end end BG = snapshot(0, 0, W, H) end local BL = {} for x = 0, 44, 2 do BL[#BL + 1] = { x, CREST[x] + 1, 3 + umod(hash32(x * 7919), 3), 1 } end for x = 1, W - 1, 3 do BL[#BL + 1] = { x, H, 5 + umod(hash32(x * 104729), 5), 2 } end local OFF = { { 0, 0 }, { 0, 1 }, { 1, 2 }, { 2, 3 } } local GP = 9 local function gust(t) local c = (1 - cos(6.2832 * t / GP)) / 2 return c * c end local function gust_int(t) local w = 6.2832 / GP return (1.5 * t - 2 * sin(w * t) / w + 0.25 * sin(2 * w * t) / w) / 4 end local function draw_blades(kind, t, g) local r, gg, b = 206, 124, 48 if kind == 2 then r, gg, b = 36, 14, 24 end for i = 1, #BL do local q = BL[i] if q[4] == kind then local x, yb, h = q[1], q[2], q[3] local k = 0.25 + 0.5 * sin(t * 1.9 - x * 0.13) + 0.5 * g * (0.6 + 0.4 * sin(t * 3.1 - x * 0.07)) * 3 local L = floor(k * 1.1 + 0.5) L = L < 0 and 0 or (L > 3 and 3 or L) local lo = h // 2 local o = OFF[L + 1] rect(x + o[1], yb - lo, 1, lo, r, gg, b) rect(x + o[2], yb - h, 1, h - lo, r, gg, b) end end end local LF = {} local POSES = { { { 0, 0 }, { 1, 0 }, { 0, 1 } }, { { 0, 0 }, { 1, 0 }, { 1, 1 } }, { { 0, 1 }, { 1, 1 }, { 1, 0 } }, { { 0, 0 }, { 1, 0 }, { 2, 0 } }, } local LCOL = { { 244, 120, 36 }, { 212, 64, 30 }, { 252, 176, 62 } } function on_settings() LF = {} for i = 1, cfg.leaves or 9 do local h = hash32(i * 0x9E3779B1) LF[i] = { x0 = umod(h, W + 16), y0 = umod(h >> 8, H + 16), vb = 3 + umod(h >> 14, 40) / 10, sink = 5 + umod(h >> 18, 50) / 10, ph = umod(h >> 22, 628) / 100, sp = 4 + umod(h >> 26, 4), c = LCOL[i % 3 + 1], } end end local function splat(x, y, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local gx, gy = 1 - fx, 1 - fy blend(ix, iy, r, g, b, gx * gy) if fx > 0.04 then blend(ix + 1, iy, r, g, b, fx * gy) end if fy > 0.04 then blend(ix, iy + 1, r, g, b, gx * fy) end if fx > 0.04 and fy > 0.04 then blend(ix + 1, iy + 1, r, g, b, fx * fy) end end local function draw_leaves(t) local amp = 14 + 9 * (cfg.gust or 3) for i = 1, #LF do local L = LF[i] local tl = t - L.x0 * 0.02 local x = (L.x0 + L.vb * t + amp * gust_int(tl)) % (W + 16) - 8 local y = (L.y0 + L.sink * t + 2.2 * sin(t * 2.3 + L.ph)) % (H + 16) - 8 local P = POSES[(frame // L.sp + i) % 4 + 1] for k = 1, #P do splat(x + P[k][1], y + P[k][2], L.c[1], L.c[2], L.c[3]) end end end function render() if not BG then build() end blit_rgb(BG, 0, 0, W, H) local t = frame / FPS local g = gust(t) * (0.6 + 0.2 * (cfg.gust or 3)) draw_blades(1, t, g) draw_leaves(t) draw_blades(2, t, g) end