info = { type = "image", title = "Glow cap", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A big mushroom glows and lets sparks rise", api = 44, } local tr = ({ sk = { ["Glow colour"] = "Farba žiary", ["Rainbow cycle"] = "Cyklus dúhy", ["Sparks"] = "Iskry" }, cs = { ["Glow colour"] = "Barva záře", ["Rainbow cycle"] = "Cyklus duhy", ["Sparks"] = "Jiskry" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "glow", label = T("Glow colour"), type = "color", default = "#FF6EC8" }, { key = "cycle", label = T("Rainbow cycle"), type = "bool", default = true }, { key = "sparks", label = T("Sparks"), type = "int", min = 0, max = 24, default = 14 }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local GROUND_C, MOSS = { 24, 44, 62 }, { 46, 104, 84 } local GROUND_Y = 55 local CX = 31.5 local function hsv(h, s, v) h = (h % 360) / 60 local i = floor(h) local f = h - i local p, q, t = v * (1 - s), v * (1 - s * f), v * (1 - s * (1 - f)) local r, g, b if i == 0 then r, g, b = v, t, p elseif i == 1 then r, g, b = q, v, p elseif i == 2 then r, g, b = p, v, t elseif i == 3 then r, g, b = p, q, v elseif i == 4 then r, g, b = t, p, v else r, g, b = v, p, q end return floor(r * 255 + 0.5), floor(g * 255 + 0.5), floor(b * 255 + 0.5) end local function hue_of(r, g, b) local mx = math.max(r, g, b) local mn = math.min(r, g, b) if mx == mn then return 330 end local d = mx - mn local h if mx == r then h = ((g - b) / d) % 6 elseif mx == g then h = (b - r) / d + 2 else h = (r - g) / d + 4 end return h * 60 end local CELLS = {} local function set(x, y, v) if x >= 0 and x < 64 and y >= 0 and y < 64 then CELLS[y][x + 1] = v end end local SPOTS = { { 22, 15, 3.2 }, { 36, 13, 2.6 }, { 30, 22, 3.8 }, { 44, 21, 2.6 }, { 15, 24, 2.4 }, { 50, 26, 1.8 }, { 26, 28, 1.6 } } local function build_stem() for y = 0, 63 do local row = {} for x = 1, 64 do row[x] = 0 end CELLS[y] = row end for y = 31, GROUND_Y - 1 do local hw if y < 44 then hw = 4.2 + 1.2 * ((44 - y) / 11) ^ 2 else hw = 4.2 + 3.2 * ((y - 44) / 10) ^ 2 end local x0, x1 = floor(CX - hw + 0.5), floor(CX + hw + 0.5) for x = x0, x1 do local v = 9 if x >= x1 - 1 then v = 10 elseif x <= x0 then v = 11 end set(x, y, v) end if y >= 37 and y <= 39 then for x = x0 - 2, x1 + 2 do set(x, y, y == 39 and 10 or 12) end end end end local function build_cap(y0, y1) for y = y0, y1 do for x = 9, 55 do local dx = x - CX local ex = (dx / 22) * (dx / 22) local v = 0 if y <= 30 then local dy = (y - 30.5) / 19.5 local e = ex + dy * dy if e <= 1 then if e > 0.86 then v = 1 elseif (dx > 2 and e > 0.55) or (y >= 26 and e > 0.5) then v = 3 elseif dx < -2 and y < 24 and e > 0.45 then v = 4 else v = 2 end if v ~= 1 then for k = 1, #SPOTS do local s = SPOTS[k] local d2 = (x - s[1]) * (x - s[1]) + (y - s[2]) * (y - s[2]) if d2 <= s[3] * s[3] then v = d2 <= s[3] * s[3] * 0.25 and 6 or 5 end end end end elseif y <= 34 then local dy = (y - 30.5) / 4.5 local e = ex + dy * dy if e <= 1 then v = e > 0.8 and 1 or (x % 2 == 0 and 7 or 8) end end if v > 0 then set(x, y, v) end end end end local function build_small() local function small(cx, capy, rx, ry) for y = capy - ry + 1, capy do local hw = floor(rx * sqrt(1 - ((capy - y + 0.5) / ry) ^ 2) + 0.5) for x = cx - hw, cx + hw do set(x, y, (y == capy - ry + 1 or x < cx - hw + 2) and 15 or 13) end end for y = capy + 1, GROUND_Y - 1 do set(cx, y, 14); set(cx + 1, y, 14) end end small(10, 50, 6, 4); small(54, 51, 5, 3); small(19, 53, 3, 2); small(46, 53, 3, 2) local rows = {} for y = 0, 63 do rows[y + 1] = string.char(table.unpack(CELLS[y], 1, 64)) end CELLS = table.concat(rows) end local M1, M2 = nil, nil local M1X, M1Y, M1W, M1H = 1, 3, 62, 36 local M2X, M2Y, M2W, M2H = 4, 55, 56, 9 local function build_masks() local rows, row = {}, {} for j = 0, M1H - 1 do local y = M1Y + j for i = 1, M1W do local dx = M1X + i - 1 - CX local dy = (y - 30.5) / (y <= 30 and 19.5 or 4.5) local e = (dx / 22) * (dx / 22) + dy * dy local v = 0 if e > 1 then if e <= 1.2544 then v = 170 elseif e <= 1.5876 then v = 100 elseif e <= 1.96 then v = 50 end end row[i] = v end rows[j + 1] = string.char(table.unpack(row, 1, M1W)) end M1 = table.concat(rows) rows, row = {}, {} for j = 0, M2H - 1 do local y = M2Y + j for i = 1, M2W do local dx = (M2X + i - 1 - CX) / 28 local dy = (y - 57) / 6 local e = dx * dx + dy * dy local v = 0 if e <= 0.3 then v = 120 elseif e <= 0.65 then v = 70 elseif e <= 1 then v = 34 end row[i] = v end rows[j + 1] = string.char(table.unpack(row, 1, M2W)) end M2 = table.concat(rows) end local BG = nil local function build_bg() rect(0, 0, W, H, 0, 0, 0) for i = 1, 22 do local h = hash32(i * 0x9E3779B1) local y = umod(h >> 8, GROUND_Y - 2) px(umod(h, W), y, 70, 82, 120) end rect(0, GROUND_Y, W, H - GROUND_Y, GROUND_C[1], GROUND_C[2], GROUND_C[3]) rect(0, GROUND_Y, W, 1, MOSS[1], MOSS[2], MOSS[3]) BG = gfx.snapshot(0, 0, W, H) end local SV, SL, SO, SH = {}, {}, {}, {} for i = 1, 24 do local h = hash32(i * 0xC2B2AE35) SV[i] = 3 + umod(h, 10) * 0.3 SL[i] = 6 + umod(h >> 8, 400) / 100 SO[i] = umod(h >> 4, 1333) / 100 SH[i] = h end local PAL = {} for i = 1, 48 do PAL[i] = 0 end local function setc(i, r, g, b) PAL[i * 3 + 1], PAL[i * 3 + 2], PAL[i * 3 + 3] = r, g, b end local H0, CYCLE, N_SP = 330, true, 14 function on_settings() local c = cfg.glow or { r = 255, g = 110, b = 200 } H0 = hue_of(c.r, c.g, c.b) if cfg.cycle == nil then CYCLE = true else CYCLE = cfg.cycle end N_SP = cfg.sparks or 14 end local GRASS = { 3, 7, 13, 22, 26, 38, 42, 49, 57, 60 } local stage = 0 function render() if stage < 6 then stage = stage + 1 gfx.ready(false) if stage == 1 then build_bg() return end gfx.blit_rgb(BG, 0, 0, W, H) if stage == 2 then build_stem() elseif stage == 3 then build_cap(10, 22) elseif stage == 4 then build_cap(23, 35) elseif stage == 5 then build_small() else build_masks() end return end local t = frame / FPS gfx.blit_rgb(BG, 0, 0, W, H) local h = CYCLE and H0 + t * 18 or H0 local p = 0.5 + 0.5 * sin(t * TAU / 3.2) local v = 0.90 + 0.10 * p setc(1, hsv(h + 8, 0.80, 0.55)) setc(2, hsv(h, 0.45, v)) setc(3, hsv(h + 10, 0.66, 0.80)) setc(4, hsv(h, 0.22, 1.0)) setc(5, hsv(h, 0.10, 1.0)) setc(6, hsv(h, 0.03 + 0.05 * (1 - p), 1.0)) setc(7, hsv(h + 14, 0.80, 0.36)) setc(8, hsv(h + 14, 0.70, 0.52)) setc(9, hsv(h, 0.14, 0.97)) setc(10, hsv(h, 0.34, 0.76)) setc(11, hsv(h, 0.08, 1.0)) setc(12, hsv(h, 0.34, 0.90)) setc(13, hsv(h + 24, 0.50, 0.78 + 0.1 * p)) setc(14, hsv(h, 0.14, 0.80)) setc(15, hsv(h + 24, 0.26, 1.0)) local gr, gg, gb = hsv(h, 0.78, 1.0) local k = 0.6 + 0.4 * p gfx.blend_mask(M1, gr, gg, gb, M1X, M1Y, M1W, M1H, M1W, M1H, k) gfx.blend_mask(M2, gr, gg, gb, M2X, M2Y, M2W, M2H, M2W, M2H, k) gfx.blit_indexed(CELLS, PAL, 0, 0, 64, 64, 64, 64, true) local sr, sg, sb = hsv(h, 0.28, 1.0) for i = 1, N_SP do local q = t + SO[i] local L = SL[i] local pass, u = floor(q / L), q % L local hh = hash32(pass * 0x9E3779B1 + SH[i]) local x = 4 + umod(hh, 56) + 2.5 * sin(u * 1.35 + (hh >> 12 & 63) * 0.1) local y = GROUND_Y - 1 - u * SV[i] * 1.6 local a = 1.0 if u < 0.5 then a = a * u / 0.5 elseif u > L * 0.7 then a = a * (L - u) / (L * 0.3) end a = a * (0.8 + 0.2 * sin(u * 12 + i)) local xi, yi = floor(x), floor(y) local fx, fy = x - xi, y - yi blend(xi, yi, sr, sg, sb, a * (1 - fx) * (1 - fy)) blend(xi + 1, yi, sr, sg, sb, a * fx * (1 - fy)) blend(xi, yi + 1, sr, sg, sb, a * (1 - fx) * fy) blend(xi + 1, yi + 1, sr, sg, sb, a * fx * fy) end for i = 1, #GRASS do local x = GRASS[i] local ht = 3 + (x % 3) local lean = 1.4 * sin(t * 1.8 + x * 0.2) local ym = GROUND_Y - ht // 2 - 1 px(x, GROUND_Y - 1, 40, 110, 88) px(x + floor(lean * 0.5 + 0.5), ym, 52, 138, 104) px(x + floor(lean + 0.5), GROUND_Y - ht, 110, 210, 160) end end