info = { type = "image", title = "Leaves in lamplight", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Leaves fall through lamplight into a puddle", api = 33, } local tr = ({ sk = { ["Leaves"] = "Listy", ["Lamp light"] = "Svetlo lampy", ["Warm"] = "Teplé", ["Amber"] = "Jantárové", ["Cool"] = "Studené", ["Fall speed"] = "Rýchlosť pádu" }, cs = { ["Leaves"] = "Listy", ["Lamp light"] = "Světlo lampy", ["Warm"] = "Teplé", ["Amber"] = "Jantarové", ["Cool"] = "Studené", ["Fall speed"] = "Rychlost pádu" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Leaves"), type = "int", min = 3, max = 12, default = 8 }, { key = "lamp", label = T("Lamp light"), type = "int", default = "warm", opts = { keys = { "warm", "amber", "cool" }, T("Warm"), T("Amber"), T("Cool") } }, { key = "fall", label = T("Fall speed"), type = "int", min = 1, max = 3, default = 2 }, } local rect, blend_rect, blend = gfx.rect, gfx.blend_rect, gfx.blend local floor, sin, cos, sqrt, max, min, abs = math.floor, math.sin, math.cos, math.sqrt, math.max, math.min, math.abs local hash32, umod = math.hash32, math.umod local char, concat = string.char, table.concat local W, H = gfx.W, gfx.H local LAMPS = { warm = { 255, 200, 110 }, amber = { 255, 160, 60 }, cool = { 226, 236, 255 } } local SKY, WALL, WALL_CAP = { 7, 11, 28 }, { 16, 22, 40 }, { 30, 38, 66 } local GROUND, PUD = { 22, 26, 44 }, { 28, 38, 70 } local LX, LY = 16.5, 14 local PCX, PCY, PRX, PRY = 21, 57, 15, 4 local SC = { 0.75, 1, 1.4 } local FLOAT = 110 local function hw(y) return 3 + (y - LY) * 0.55 end local function ell(cx, cy, rx, ry, c, a) local ry5 = ry + 0.5 for dy = -floor(ry), floor(ry) do local w = floor(rx * sqrt(1 - (dy / ry5) ^ 2) + 0.5) if a then blend_rect(cx - w, cy + dy, 2 * w + 1, 1, c[1], c[2], c[3], a) else rect(cx - w, cy + dy, 2 * w + 1, 1, c[1], c[2], c[3]) end end end local BG local function build_bg(lamp) rect(0, 0, W, H, SKY[1], SKY[2], SKY[3]) for i = 1, 9 do local h = hash32(i * 0x9E3779B1) rect(30 + umod(h, 33), umod(h >> 8, 28), 1, 1, 58, 68, 110) end rect(0, 40, W, 10, WALL[1], WALL[2], WALL[3]) rect(0, 40, W, 1, WALL_CAP[1], WALL_CAP[2], WALL_CAP[3]) for x = 5, W - 1, 12 do rect(x, 41, 1, 9, 11, 15, 30) end rect(0, 50, W, 14, GROUND[1], GROUND[2], GROUND[3]) rect(0, 50, W, 1, 40, 46, 74); rect(0, 51, W, 1, 30, 36, 60) rect(0, 62, W, 1, 16, 19, 34) ell(PCX, PCY, PRX, PRY, PUD) local layers = { { 1.0, 0.05 }, { 0.78, 0.055 }, { 0.55, 0.06 } } for _, ly in ipairs(layers) do for y = LY + 1, 62 do local w = hw(y) * ly[1] blend_rect(floor(LX - w + 0.5), y, floor(2 * w + 0.5), 1, lamp[1], lamp[2], lamp[3], ly[2] * (1 - 0.4 * (y - LY) / 48)) end end ell(17, 56, 22, 7, lamp, 0.05); ell(17, 56, 16, 5, lamp, 0.05); ell(17, 56, 10, 3, lamp, 0.05) rect(8, 14, 2, 36, 22, 26, 42); rect(6, 47, 6, 3, 26, 30, 48) rect(8, 12, 6, 2, 26, 30, 48); rect(12, 11, 10, 3, 28, 32, 52); rect(13, 11, 8, 1, 60, 66, 100) ell(17, 15, 6, 3, lamp, 0.16); ell(17, 15, 3, 2, lamp, 0.28) rect(13, 14, 8, 1, 255, 244, 204) return gfx.snapshot(0, 0, W, H) end local TONES = { { { 240, 96, 40 }, { 206, 60, 28 }, { 120, 30, 16 } }, { { 255, 176, 50 }, { 236, 128, 24 }, { 150, 70, 16 } }, { { 255, 226, 96 }, { 232, 178, 40 }, { 150, 110, 24 } }, } local LV = { 1.0, 0.82, 0.62, 0.34, 0.2, 0.12 } local PALS, SPR = {}, {} local LAMPC = LAMPS.warm local function pal(hue, lvl, k, target) local key = ((hue * 8 + lvl) * 8 + k) * 2 + (target == PUD and 1 or 0) local p = PALS[key] if p then return p end p = { 0, 0, 0 } for tn = 1, 3 do local b = TONES[hue][tn] for ch = 1, 3 do local v = b[ch] * LV[lvl] * (0.55 + 0.45 * LAMPC[ch] / 255) + 4 + ch * 2 p[#p + 1] = floor(v + (target[ch] - v) * k / 4 + 0.5) end end PALS[key] = p return p end local function sprite(k, qx, qy) local key = ((k + 4) * 4 + qx) * 4 + qy local s = SPR[key] if s then return s end local th = k * 0.2182 local c, sn = cos(th), sin(th) local cx, cy = 4 + qx / 4, 4 + qy / 4 local cells = {} for j = 0, 8 do for i = 0, 8 do local dx, dy = i + 0.5 - cx, j + 0.5 - cy local u = dx * c + dy * sn local v = -dx * sn + dy * c local t = 0 if u < -3.0 and u > -3.9 then if abs(v) < 0.5 then t = 3 end elseif u >= -3.0 and u < 3.6 then local e = 1 - ((u - 0.3) / 3.3) ^ 2 if e > 0 and abs(v) <= 1.65 * sqrt(e) then t = (abs(v) < 0.45 and u > -2.2) and 3 or (dy < 0 and 1 or 2) end end cells[#cells + 1] = char(t) end end s = concat(cells) SPR[key] = s return s end local FLAT = concat({ string.rep(char(0), 3), string.rep(char(1), 3), string.rep(char(0), 3), char(0), string.rep(char(2), 3), char(3), string.rep(char(2), 3), char(0) }) local function lvl_at(x, y) if y <= 12 then return 6 end local r = abs(x - LX) / hw(y) if r < 0.55 then return 1 elseif r < 0.78 then return 2 elseif r < 1 then return 3 elseif r < 1.25 then return 4 end return 6 end local LEAF = {} for i = 1, 12 do local h = hash32(i * 0x85EBCA6B) local dark = i % 5 == 0 local v = 0.30 + umod(h, 15) / 100 local yl = dark and (54 + umod(h >> 4, 8)) or (55 + umod(h >> 4, 5)) LEAF[i] = { dark = dark, v = v, yl = yl, hue = 1 + i % 3, x0 = dark and (46 + umod(h >> 8, 12)) or (15 + umod(h >> 8, 13)), A = 2.5 + umod(h >> 12, 20) / 10, w = 6.2832 / (70 + umod(h >> 16, 40)), ph = umod(h >> 20, 628) / 100, B = 1 + umod(h >> 24, 10) / 10, wy = 6.2832 / (50 + umod(h >> 26, 30)), py = umod(h >> 5, 628) / 100, o = umod(h >> 9, 400), } LEAF[i].Tf = (yl + 7) / v end function on_settings() LAMPC = LAMPS[cfg.lamp] or LAMPS.warm PALS, BG = {}, nil end local function in_puddle(x, y) return ((x - PCX) / PRX) ^ 2 + ((y - PCY) / PRY) ^ 2 <= 1 end local function ring(cx, cy, a) if a < 0 or a > 44 then return end local rx = 1.5 + a * 0.2 local ry = rx * 0.36 local al = 0.5 * (1 - a / 44) local n = max(8, floor(rx * 3)) for q = 0, n - 1 do local ang = q * 6.2832 / n local px, py = floor(cx + rx * cos(ang) + 0.5), floor(cy + ry * sin(ang) + 0.5) if in_puddle(px, py) then blend(px, py, 196, 204, 230, al) end end end function render() if not BG then BG = build_bg(LAMPC) end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame local t = f * SC[cfg.fall or 2] for r = 0, 5 do local wv = 1 + umod(hash32(r * 7 + f // 4), 2) blend_rect(17 - wv // 2, 55 + r, wv + 1, 1, LAMPC[1], LAMPC[2], LAMPC[3], 0.5 - 0.06 * r) blend_rect(14, 55 + r, 7, 1, LAMPC[1], LAMPC[2], LAMPC[3], 0.1) end for m = 0, 1 do local a = f * (0.09 + 0.03 * m) + m * 2.4 local x = LX + (5 + m) * cos(a) * (1 + 0.25 * sin(f * 0.031 + m)) local y = 16 + 3.2 * sin(a * 1.4 + m) local xi, yi = floor(x), floor(y) local fx, fy = x - xi, y - yi blend(xi, yi, 255, 246, 214, 0.85 * (1 - fx) * (1 - fy)); blend(xi + 1, yi, 255, 246, 214, 0.85 * fx * (1 - fy)) blend(xi, yi + 1, 255, 246, 214, 0.85 * (1 - fx) * fy); blend(xi + 1, yi + 1, 255, 246, 214, 0.85 * fx * fy) end for i = 1, cfg.n or 8 do local L = LEAF[i] local ph = (t + L.o) % (L.Tf + FLOAT) local target = L.dark and GROUND or PUD if ph < L.Tf then local y = -7 + L.v * ph + L.B * sin(L.wy * ph + L.py) if y > L.yl then y = L.yl end local th = L.w * ph + L.ph local x = L.x0 - L.A * cos(th) local ix, iy = floor(x), floor(y) local k = floor(0.85 * sin(th) / 0.2182 + 0.5) gfx.blit_indexed(sprite(k, floor((x - ix) * 4), floor((y - iy) * 4)), pal(L.hue, lvl_at(x, y), 0, target), ix - 4, iy - 4, 9, 9, 9, 9, true) else local a = ph - L.Tf local xl = L.x0 - L.A * cos(L.w * L.Tf + L.ph) if not L.dark then ring(xl, L.yl + 1, a) ring(xl, L.yl + 1, a - 10) end local k = a > FLOAT - 20 and floor((a - (FLOAT - 20)) / 5) + 1 or 0 if k > 4 then k = 4 end gfx.blit_indexed(FLAT, pal(L.hue, lvl_at(xl, L.yl), k, target), floor(xl) - 4, L.yl, 9, 2, 9, 2, true) end end end