info = { type = "image", title = "Spore glow", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A glowing mushroom sheds drifting spores", api = 44, } local tr = ({ sk = { ["Mushroom"] = "Huba", ["Blue"] = "Modrá", ["Violet"] = "Fialová", ["Green"] = "Zelená", ["Amber"] = "Jantárová", ["Spores"] = "Výtrusy" }, cs = { ["Mushroom"] = "Houba", ["Blue"] = "Modrá", ["Violet"] = "Fialová", ["Green"] = "Zelená", ["Amber"] = "Jantarová", ["Spores"] = "Výtrusy" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "shroom", label = T("Mushroom"), type = "int", default = "blue", opts = { keys = { "blue", "violet", "green", "amber" }, T("Blue"), T("Violet"), T("Green"), T("Amber") } }, { key = "spores", label = T("Spores"), type = "int", min = 3, max = 24, default = 12 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot, blit_idx, blend_mask = gfx.blit_rgb, gfx.snapshot, gfx.blit_indexed, gfx.blend_mask 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 TAU = 6.28318 local THEMES = { blue = { cap = { 22,40,120, 36,74,176, 66,124,226, 120,180,248, 190,226,255, 225,244,255, 14,24,76, 30,56,130 }, glow = { 90, 160, 255 }, spore = { 190, 226, 255 }, stemL = { 240, 176, 120 }, stemD = { 176, 110, 76 } }, violet = { cap = { 60,24,120, 96,46,170, 150,86,218, 196,140,244, 236,200,255, 250,232,255, 30,12,70, 60,28,120 }, glow = { 190, 120, 255 }, spore = { 240, 210, 255 }, stemL = { 250, 210, 230 }, stemD = { 170, 120, 160 } }, green = { cap = { 14,76,60, 22,120,84, 50,176,110, 110,226,150, 190,250,200, 230,255,230, 8,44,40, 16,78,60 }, glow = { 110, 255, 170 }, spore = { 210, 255, 220 }, stemL = { 240, 240, 170 }, stemD = { 160, 170, 100 } }, amber = { cap = { 130,48,12, 190,86,16, 236,138,30, 252,190,70, 255,226,130, 255,244,200, 70,24,8, 120,50,14 }, glow = { 255, 180, 70 }, spore = { 255, 236, 170 }, stemL = { 240, 224, 196 }, stemD = { 170, 150, 124 } }, } local TH = THEMES.blue local PAL = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } local CAPW, CAPH = 40, 24 local CAPX, CAPY = 12, 14 local SPOTS = { { -9, -8, 2.6 }, { -2, -12, 2 }, { 8, -9, 3 }, { 12, -3, 2 }, { -13, -2, 2 }, { 2, -4, 2.4 } } local function cap_cell(x, y) local dx, dy = x + 0.5 - 20, y + 0.5 - 18 if dy < 0 then local e = sqrt((dx / 20) ^ 2 + (dy / 18) ^ 2) if e > 1 then return 0 end for _, s in ipairs(SPOTS) do if (dx - s[1]) ^ 2 + (dy - s[2]) ^ 2 < s[3] * s[3] then return 6 end end if e > 0.93 then return 1 end local eh = sqrt(((dx + 4) / 20) ^ 2 + ((dy + 6) / 18) ^ 2) return eh < 0.3 and 5 or eh < 0.5 and 4 or eh < 0.75 and 3 or 2 end local e = sqrt((dx / 20) ^ 2 + (dy / 6) ^ 2) if e > 1 then return 0 end return (e > 0.85 or x % 3 == 0) and 8 or 7 end local CAP, GRASS, HALO local CAP_ROWS = {} local function cap_grid(y0, y1) local r = {} for y = y0, y1 do for x = 0, CAPW - 1 do r[x + 1] = cap_cell(x, y) end CAP_ROWS[y + 1] = string.char(table.unpack(r, 1, CAPW)) end if y1 == CAPH - 1 then CAP = table.concat(CAP_ROWS) end end local function ell_mask(rx, ry, peak) local rows, half = {}, {} for y = 0, ry - 1 do for x = 0, rx - 1 do local e = sqrt(((x + 0.5 - rx) / rx) ^ 2 + ((y + 0.5 - ry) / ry) ^ 2) half[x + 1] = floor(peak * max(0, 1 - e)) end local s = string.char(table.unpack(half, 1, rx)) rows[y + 1] = s .. s:reverse() end for y = ry + 1, 2 * ry do rows[y] = rows[2 * ry + 1 - y] end return table.concat(rows) end local function hill_y(x) return 47 + 7 * ((x - 30) / 34) ^ 2 end local SKY_R, SKY_G = 44, 32 local function sky() for y = 0, 47 do rect(0, y, W, 1, SKY_R, SKY_G, floor(120 + 60 * y / 47 + 0.5)) end rect(0, 48, W, 8, SKY_R, SKY_G, 180) for i = 1, 30 do local h = hash32(i * 0x9E3779B1) px(umod(h, W), umod(h >> 8, 40), 120, 128, 208) end end local function hill() for x = 0, W - 1 do local y = floor(hill_y(x)) rect(x, y, 1, 1, 42, 122, 72) rect(x, y + 1, 1, 3, 26, 88, 58) rect(x, y + 4, 1, H - y - 4, 18, 64, 46) end for i = 1, 24 do local h = hash32(i * 0x85EBCA6B) local x = umod(h, W) px(x, floor(hill_y(x)) - 1 + umod(h >> 8, 2) * 3, 60, 150, 84) end end local function stem() local L, D = TH.stemL, TH.stemD for y = 33, 50 do local hw = floor(3 + (y - 33) * 0.1 + 0.5) rect(32 - hw, y, hw, 1, L[1], L[2], L[3]) rect(32, y, hw + 1, 1, D[1], D[2], D[3]) rect(32 - hw, y, 1, 1, L[1] * 0.85, L[2] * 0.85, L[3] * 0.85) end rect(26, 49, 13, 1, 42, 122, 72) rect(24, 50, 17, 3, 26, 88, 58) for i = 0, 9 do px(25 + i * 1.6, 48 + i % 2, 60, 150, 84) end for _, s in ipairs({ { 9, 51 }, { 53, 53 } }) do local c = TH.cap rect(s[1], s[2], 2, 3, L[1], L[2], L[3]) rect(s[1] - 2, s[2] - 2, 6, 2, c[4 * 3 - 2], c[4 * 3 - 1], c[4 * 3]) rect(s[1] - 1, s[2] - 3, 4, 1, c[5 * 3 - 2], c[5 * 3 - 1], c[5 * 3]) end end local STAGES = { sky, hill, function() cap_grid(0, 11) end, function() cap_grid(12, CAPH - 1) end, function() GRASS = ell_mask(26, 9, 200) end, function() HALO = ell_mask(26, 18, 200) end } local STAGE, BG0, BG, STEM_OK = 1, nil, nil, false function on_settings() TH = THEMES[cfg.shroom] or THEMES.blue STEM_OK = false end local NSP = 24 local SPH, SPR, SPP = {}, {}, {} for i = 1, NSP do local h = hash32(i * 0xC2B2AE35) SPH[i], SPR[i], SPP[i] = umod(h, 1000) / 1000, 6 + umod(h >> 10, 50) / 10, umod(h >> 16, 628) / 100 end local NST, STX, STY, STW, STF = 9, {}, {}, {}, {} for i = 1, NST do local h = hash32(i * 0x27D4EB2F) STX[i], STY[i], STW[i], STF[i] = umod(h, W), umod(h >> 8, 36), 1 + umod(h >> 14, 25) / 10, umod(h >> 20, 628) / 100 end local function spore(i, t, sr, sg, sb) local v = t / SPR[i] + SPH[i] local n = floor(v) local u = v - n local h = hash32(i * 0x9E3779B1 + n * 0x85EBCA6B) local x = 15 + umod(h, 34) + 5 * sin(u * 5 + SPP[i]) + (umod(h >> 8, 2) * 2 - 1) * 7 * u local y = 40 - u * (24 + umod(h >> 10, 16)) local a = sin(u * 3.1416) a = min(1, 1.5 * a * (0.35 + 0.65 * a) * (0.75 + 0.25 * sin(t * 6 + SPP[i]))) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, sr, sg, sb, a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, sr, sg, sb, a * fx * (1 - fy)) blend(ix, iy + 1, sr, sg, sb, a * (1 - fx) * fy) blend(ix + 1, iy + 1, sr, sg, sb, a * fx * fy) blend_rect(ix - 1, iy - 1, 3, 3, sr, sg, sb, a * 0.12) end function render() if STAGE <= #STAGES then if BG0 then blit_rgb(BG0, 0, 0, W, H) end STAGES[STAGE]() if STAGE <= 2 then BG0 = snapshot(0, 0, W, H) end STAGE = STAGE + 1 gfx.ready(false) return end if not STEM_OK then blit_rgb(BG0, 0, 0, W, H) stem() BG, STEM_OK = snapshot(0, 0, W, H), true end local t = frame / FPS blit_rgb(BG, 0, 0, W, H) local p = 0.5 + 0.5 * sin(t * 1.3) local g, cp = TH.glow, TH.cap blend_mask(HALO, 150, 110, 255, 6, 8, 52, 36, 52, 36, 0.3 + 0.35 * p) blend_mask(GRASS, g[1], g[2], g[3], 6, 42, 52, 18, 52, 18, 0.25 + 0.3 * p) local k = 0.82 + 0.28 * p for i = 1, 24 do PAL[i + 3] = min(255, floor(cp[i] * k)) end blit_idx(CAP, PAL, CAPX, CAPY, CAPW, CAPH, CAPW, CAPH, true) for i = 1, NST do local v = sin(t * STW[i] + STF[i]) if v > 0.3 then blend(STX[i], STY[i], 210, 220, 255, (v - 0.3) * 1.2) end end local s = TH.spore for i = 1, min(NSP, cfg.spores or 12) do spore(i, t, s[1], s[2], s[3]) end end