info = { type = "image", title = "Leaf dew", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A leaf sways in the wind, a dewdrop runs down", api = 33, } local tr = ({ sk = { ["Wind"] = "Vietor", ["Visitor"] = "Návštevník", ["Ladybug"] = "Lienka", ["Caterpillar"] = "Húsenica", ["None"] = "Žiadne", ["Leaf colour"] = "Farba lístia" }, cs = { ["Wind"] = "Vítr", ["Visitor"] = "Návštěvník", ["Ladybug"] = "Beruška", ["Caterpillar"] = "Housenka", ["None"] = "Žádné", ["Leaf colour"] = "Barva listí" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "wind", label = T("Wind"), type = "int", min = 0, max = 5, default = 3 }, { key = "bug", label = T("Visitor"), type = "int", default = "ladybug", opts = { keys = { "ladybug", "caterpillar", "none" }, T("Ladybug"), T("Caterpillar"), T("None") } }, { key = "leaf", label = T("Leaf colour"), type = "color", default = "#2E9E4F" }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sin, cos, sqrt = math.floor, math.ceil, math.sin, math.cos, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.2831853 local SKY = { 74, 150, 228 } local TWIG, TWIG_L, TWIG_D = { 108, 72, 46 }, { 156, 108, 72 }, { 70, 46, 32 } local DROP, DROP_L = { 176, 228, 255 }, { 255, 255, 255 } local BX, BY = 14, 4 local L = 54 local S = 13 local RIM, MID, LIT, VEIN = { 28, 90, 44 }, { 46, 158, 79 }, { 96, 196, 108 }, { 128, 214, 138 } function on_settings() local c = cfg.leaf or { r = 46, g = 158, b = 79 } MID = { c.r, c.g, c.b } RIM = { c.r * 55 // 100, c.g * 55 // 100, c.b * 55 // 100 } LIT = { c.r + (255 - c.r) * 28 // 100, c.g + (255 - c.g) * 28 // 100, c.b + (255 - c.b) * 28 // 100 } VEIN = { c.r + (255 - c.r) * 50 // 100, c.g + (255 - c.g) * 50 // 100, c.b + (255 - c.b) * 50 // 100 } end local function c3(c) return c[1], c[2], c[3] end local BG = nil local function build_bg() rect(0, 0, W, H, c3(SKY)) rect(0, 2, 30, 3, c3(TWIG)) rect(0, 2, 30, 1, c3(TWIG_L)) rect(0, 4, 30, 1, c3(TWIG_D)) rect(25, 5, 2, 2, c3(TWIG_D)) rect(30, 3, 2, 2, c3(TWIG_D)) BG = gfx.snapshot(0, 0, W, H) end local function sstep(x) if x <= 0 then return 0 elseif x >= 1 then return 1 end return x * x * (3 - 2 * x) end local function lens(bx, by, ax, ay, nx, ny, uc, Lc, s, vs, r, g, b0, aa) local d = (Lc * Lc / 4 - s * s) / (2 * s) local R = d + s local R2 = R * R local mx, my = bx + uc * ax + vs * nx, by + uc * ay + vs * ny local c1x, c1y = mx + d * nx, my + d * ny local c2x, c2y = mx - d * nx, my - d * ny local ya, yb = my - Lc * 0.5 * ay, my + Lc * 0.5 * ay if ya > yb then ya, yb = yb, ya end local ex = s * (ny < 0 and -ny or ny) + 1 for y = floor(ya - ex), ceil(yb + ex) do local yc = y + 0.5 local dy1, dy2 = yc - c1y, yc - c2y local q1, q2 = R2 - dy1 * dy1, R2 - dy2 * dy2 if q1 > 0 and q2 > 0 then local h1, h2 = sqrt(q1), sqrt(q2) local lo, hi = c1x - h1, c1x + h1 local lo2, hi2 = c2x - h2, c2x + h2 if lo2 > lo then lo = lo2 end if hi2 < hi then hi = hi2 end if hi > lo then if aa then local a, b = ceil(lo), floor(hi) if b > a then rect(a, y, b - a, 1, r, g, b0) if a - lo > 0.12 then blend_rect(a - 1, y, 1, 1, r, g, b0, a - lo) end if hi - b > 0.12 then blend_rect(b, y, 1, 1, r, g, b0, hi - b) end else blend_rect(floor(lo), y, 1, 1, r, g, b0, hi - lo) end else local i1, i2 = floor(lo + 0.5), floor(hi + 0.5) if i2 > i1 then rect(i1, y, i2 - i1, 1, r, g, b0) end end end end end end local ax, ay, nx, ny = 0, 1, 1, 0 local function loc(u, v) return BX + u * ax + v * nx, BY + u * ay + v * ny end local function vein(u0, du, dv, n) for j = 1, n do local x, y = loc(u0 + du * j, dv * j) px(floor(x), floor(y), c3(VEIN)) end end local function bug_pos(f) local p = f % 24 local u, mv if p < 8.67 then u, mv = 40 + (14 - 40) * sstep(p / 8.67), 1 elseif p < 11 then u, mv = 14, 1 elseif p < 19.67 then u, mv = 14 + (40 - 14) * sstep((p - 11) / 8.67), -1 else u, mv = 40, -1 end return u, mv end local function ladybug(f) local u, mv = bug_pos(f) local x, y = loc(u, 5 + 1.5 * sin(u * 0.4)) x, y = x - 1.5, y - 1.5 local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy rect(ix + 1, iy + 1, 2, 2, 214, 40, 40) blend_rect(ix, iy + 1, 1, 2, 214, 40, 40, 1 - fx) blend_rect(ix + 3, iy + 1, 1, 2, 214, 40, 40, fx) blend_rect(ix + 1, iy, 2, 1, 214, 40, 40, 1 - fy) blend_rect(ix + 1, iy + 3, 2, 1, 214, 40, 40, fy) px(floor(x + 1), floor(y + 1), 24, 20, 28) px(floor(x + 2.5), floor(y + 2), 24, 20, 28) local hx, hy = loc(u - 2.4 * mv, 5 + 1.5 * sin(u * 0.4)) px(floor(hx), floor(hy), 24, 20, 28) end local function caterpillar(f) local u, mv = bug_pos(f) for i = 5, 0, -1 do local x, y = loc(u + mv * (2.4 * i + sin(f * 6 - i * 0.9)), 6 + 1.2 * sin(u * 0.3)) x, y = floor(x), floor(y) if i == 0 then rect(x, y, 2, 2, 120, 64, 30) px(x, y, 20, 20, 20) else rect(x, y, 2, 2, i % 2 == 0 and 196 or 150, i % 2 == 0 and 232 or 204, i % 2 == 0 and 70 or 44) end end end function render() if not BG then build_bg() end local f = frame / FPS gfx.blit_rgb(BG, 0, 0, W, H) local wind = cfg.wind or 3 local gust = 0.5 + 0.5 * sin(f * TAU / 11) gust = gust * gust * (3 - 2 * gust) local cw = 1.2 + wind * 0.9 local c1 = (f * cw + 40) % (W + 40) - 30 local c2 = (f * cw * 0.6 + 8) % (W + 40) - 30 blend_rect(floor(c1) + 3, 44, 14, 1, 255, 255, 255, 0.5); blend_rect(floor(c1), 45, 20, 2, 255, 255, 255, 0.5) blend_rect(floor(c2) + 2, 56, 10, 1, 255, 255, 255, 0.4); blend_rect(floor(c2), 57, 14, 2, 255, 255, 255, 0.4) if wind > 0 then for i = 0, 2 do local x = (f * (18 + wind * 7.5) + i * 31) % (W + 24) - 12 local y = 12 + i * 17 blend_rect(floor(x), y, 8, 1, 255, 255, 255, 0.18 + 0.3 * gust) end end local A = wind * 0.065 local th = 0.45 + A * (gust * 0.6 + 0.4) * (sin(f * TAU / 3.6) + 0.35 * sin(f * TAU / 2.03 + 1.3)) + A * gust * 0.5 local s, c = sin(th), cos(th) ax, ay, nx, ny = s, c, c, -s for u = -1, 3 do local x, y = loc(u, 0) px(floor(x), floor(y), 76, 120, 52) end lens(BX, BY, ax, ay, nx, ny, L * 0.5 + 2, L, S, 0, RIM[1], RIM[2], RIM[3], true) lens(BX, BY, ax, ay, nx, ny, L * 0.5 + 2, L - 5, S - 2.5, 0, MID[1], MID[2], MID[3], false) lens(BX, BY, ax, ay, nx, ny, L * 0.5 + 1, L - 18, 5.5, 4.5, LIT[1], LIT[2], LIT[3], false) local vx = (ax < 0 and -ax or ax) > ay for u = 3, L - 3, 2 do local x, y = loc(u, 0) if vx then rect(floor(x), floor(y), 2, 1, c3(VEIN)) else rect(floor(x), floor(y), 1, 2, c3(VEIN)) end end for k = 0, 4 do local u0 = 10 + 8 * k local n = 5 - (k + 1) // 3 vein(u0, 0.8, 0.7, n) vein(u0, 0.8, -0.7, n) end local p = f % 9 if p < 0.67 then local x, y = loc(8, 0) if p > 0.2 then rect(floor(x), floor(y), 2, 2, c3(DROP)) else px(floor(x), floor(y), c3(DROP)) end elseif p < 5 then local t = (p - 0.67) / 4.33 local x, y = loc(8 + (L - 12) * t * t, 0) rect(floor(x), floor(y), 2, 2, c3(DROP)) px(floor(x), floor(y), c3(DROP_L)) elseif p < 5.83 then local x, y = loc(L - 4, 0) local g = (p - 5) / 0.83 rect(floor(x), floor(y), 3, 2 + floor(g * 1.99), c3(DROP)) px(floor(x), floor(y), c3(DROP_L)) elseif p < 7.17 then local t = p - 5.83 local x, y = loc(L - 4, 0) local yy = y + 1 + 27 * t * t rect(floor(x), floor(yy), 2, 3, c3(DROP)) px(floor(x), floor(yy), c3(DROP_L)) end local bug = cfg.bug or "ladybug" if bug == "ladybug" then ladybug(f) elseif bug == "caterpillar" then caterpillar(f) end end