info = { type = "image", title = "Glowing pumpkin", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "A carved pumpkin glowing in changing colours", api = 44, } local tr = ({ sk = { ["Colours"] = "Farby", ["Halloween"] = "Halloween", ["Rainbow"] = "Dúha", ["Orange only"] = "Len oranžová", ["Speed"] = "Rýchlosť", ["Flame"] = "Plameň" }, cs = { ["Colours"] = "Barvy", ["Halloween"] = "Halloween", ["Rainbow"] = "Duha", ["Orange only"] = "Jen oranžová", ["Speed"] = "Rychlost", ["Flame"] = "Plamen" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "mode", label = T("Colours"), type = "int", default = "halloween", opts = { keys = { "halloween", "rainbow", "orange" }, T("Halloween"), T("Rainbow"), T("Orange only") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "flame", label = T("Flame"), type = "int", min = 0, max = 3, default = 2 }, } local floor, sin, cos, sqrt, max, min, abs = math.floor, math.sin, math.cos, math.sqrt, math.max, math.min, math.abs local hash32, byte = math.hash32, string.byte local W, H, FPS = gfx.W, gfx.H, gfx.FPS local CXP, CYP, RXP, RYP = 32, 33, 27, 20 local FLOOR_Y = 47 local RC, RS = {}, {} for i = 1, 257 do local th = 10 * math.asin((i - 129) / 128) + 3.14159265 RC[i], RS[i] = cos(th), sin(th) end local function in_tri(x, y, ax, ay, bx, by, cx, cy) local d1 = (x - bx) * (ay - by) - (ax - bx) * (y - by) local d2 = (x - cx) * (by - cy) - (bx - cx) * (y - cy) local d3 = (x - ax) * (cy - ay) - (cx - ax) * (y - ay) return not ((d1 < 0 or d2 < 0 or d3 < 0) and (d1 > 0 or d2 > 0 or d3 > 0)) end local function in_mouth(x, y) local d = (x - 32) / 17 if d <= -1 or d >= 1 then return false end local d2 = d * d local yc, t = 45 - 7 * d2, 1 + 8 * (1 - d2) ^ 0.6 if y < yc - t / 2 or y > yc + t / 2 then return false end if x >= 24 and x < 28 and y < yc + 0.5 then return false end if x >= 37 and x < 41 and y > yc - 0.5 then return false end return true end local function is_cut(x, y) if y < 38 then if y < 19.5 then return false end if x < 25 then return in_tri(x, y, 11, 30, 24, 31, 20, 20) end if x > 39 then return in_tri(x, y, 53, 30, 40, 31, 44, 20) end return x > 27.5 and x < 36.5 and in_tri(x, y, 32, 31, 28, 37, 36, 37) end return x > 14.5 and x < 49.5 and in_mouth(x, y) end local RX0, RWD, RY0, RH = 8, 49, 16, 37 local DIST, COVR, COVS = {}, {}, nil local ct = {} local function cut_rows(ya, yb) for y = ya, yb do local base = (y - RY0) * RWD for x = 0, RWD - 1 do local xx, yy = RX0 + x, y local n = (is_cut(xx + 0.25, yy + 0.25) and 1 or 0) + (is_cut(xx + 0.75, yy + 0.25) and 1 or 0) + (is_cut(xx + 0.25, yy + 0.75) and 1 or 0) + (is_cut(xx + 0.75, yy + 0.75) and 1 or 0) ct[x + 1] = n DIST[base + x + 1] = n >= 2 and 0 or 300 end COVR[y - RY0 + 1] = string.char(table.unpack(ct, 1, RWD)) end end local function dt_fwd() COVS = table.concat(COVR) COVR = nil for y = 0, RH - 1 do for x = 0, RWD - 1 do local i = y * RWD + x + 1 local d = DIST[i] if d > 0 then local v if x > 0 then v = DIST[i - 1] + 3; if v < d then d = v end end if y > 0 then v = DIST[i - RWD] + 3; if v < d then d = v end if x > 0 then v = DIST[i - RWD - 1] + 4; if v < d then d = v end end if x < RWD - 1 then v = DIST[i - RWD + 1] + 4; if v < d then d = v end end end DIST[i] = d end end end end local function dt_bwd() for y = RH - 1, 0, -1 do for x = RWD - 1, 0, -1 do local i = y * RWD + x + 1 local d = DIST[i] if d > 0 then local v if x < RWD - 1 then v = DIST[i + 1] + 3; if v < d then d = v end end if y < RH - 1 then v = DIST[i + RWD] + 3; if v < d then d = v end if x < RWD - 1 then v = DIST[i + RWD + 1] + 4; if v < d then d = v end end if x > 0 then v = DIST[i + RWD - 1] + 4; if v < d then d = v end end end DIST[i] = d end end end end local LX, LY, LZ = -0.46, -0.52, 0.72 local HX, HY, HZ = -0.248, -0.280, 0.927 local RB, R1, R2 = {}, {}, {} local bt, m1, m2 = {}, {}, {} local function shade_rows(ya, yb) for y = ya, yb do local ys = y + 0.5 local ey = (ys - CYP) / RYP local ey2 = ey * ey local sq = ey2 < 1 and sqrt(1 - ey2) or 0 for x = 0, W - 1 do local xs = x + 0.5 local r, g, b, a1, a2 = 5, 6, 14, 0, 0 if y >= FLOOR_Y then r, g, b = 34, 28, 38 local gx, gy = (xs - 32) / 36, (ys - FLOOR_Y) / 17 local dn = sqrt(gx * gx + gy * gy) if dn < 1 then a1 = 0.62 * (1 - dn) end elseif ys > 4.5 and ys < 14.5 then local tt = 14 - ys local xc = 33 + tt * 0.35 local hw = 3.8 - tt * 0.12 + (tt < 2 and (2 - tt) * 0.9 or 0) local u = (xs - xc) / hw if u > -1 and u < 1 then local lam = max(0, sqrt(1 - u * u) * 0.6 - u * 0.5) local s = 0.30 + 0.60 * lam if ys < 6.5 then s = s * 0.6 end r, g, b = 150 * s, 138 * s, 62 * s end end local ex = (xs - CXP) / RXP local rr = ex * ex + ey2 local cov = rr < 1.1 and (1 - sqrt(rr)) * 23 + 0.5 or 0 if cov > 0 then if cov > 1 then cov = 1 end local nz = sqrt(max(0, 1 - rr)) local u = sq > 0 and ex / sq or 0 local idx = floor(max(-1, min(1, u)) * 128 + 129.5) local sth = RS[idx] local rib = 0.5 + 0.5 * RC[idx] local nx = ex + 0.20 * sth local lam = max(0, nx * LX + ey * LY + nz * LZ) local sp = nx * HX + ey * HY + nz * HZ local spec = 0 if sp > 0 then local s2 = sp * sp local s4 = s2 * s2 spec = s4 * s4 * s4 * s2 end local shd = (0.28 + 0.56 * lam * (0.62 + 0.38 * rib)) * (0.80 + 0.20 * rib) local rim = 1 - nz rim = rim * rim * rim * (0.5 + 0.5 * ex) local sr = 240 * shd + 76 * spec + 22 * rim local sg = (98 + 36 * rib) * shd + 70 * spec + 36 * rim local sb = 14 * shd + 54 * spec + 76 * rim local sa1, sa2 = 0, 0 local dx, dy = x - RX0, y - RY0 local inr = dx >= 0 and dx < RWD and dy >= 0 and dy < RH local d = inr and DIST[dy * RWD + dx + 1] / 3 or 99 if d < 6 then sa1 = 0.45 * (1 - d / 6) end local cv = inr and byte(COVS, dy * RWD + dx + 1) / 4 or 0 if cv > 0 then sa1 = sa1 + (1 - sa1) * cv local cxs, cys, ax, by if ys >= 38 then cxs, cys, ax, by = 32, 45, 17, 5.5 elseif abs(xs - 32) < 6.5 and ys > 29 then cxs, cys, ax, by = 32, 34.5, 4.5, 4.5 elseif xs < 32 then cxs, cys, ax, by = 18, 27, 8, 7 else cxs, cys, ax, by = 46, 27, 8, 7 end local px, py = (xs - cxs) / ax, (ys - cys) / by local dn = sqrt(px * px + py * py) if dn < 1.25 then sa2 = cv * 0.6 * (1 - 0.8 * dn / 1.25) end end r, g, b = r + (sr - r) * cov, g + (sg - g) * cov, b + (sb - b) * cov a1, a2 = a1 + (sa1 - a1) * cov, a2 * (1 - cov) + sa2 * cov end local o = x * 3 r, g, b = r + 0.5, g + 0.5, b + 0.5 if r > 255 then r = 255 end if g > 255 then g = 255 end if b > 255 then b = 255 end bt[o + 1], bt[o + 2], bt[o + 3] = r // 1, g // 1, b // 1 m1[x + 1], m2[x + 1] = (a1 * 255 + 0.5) // 1, (a2 * 255 + 0.5) // 1 end RB[y + 1] = string.char(table.unpack(bt, 1, 192)) R1[y + 1] = string.char(table.unpack(m1, 1, 64)) R2[y + 1] = string.char(table.unpack(m2, 1, 64)) end end local BASE, MG, MC = nil, nil, nil local STAGES = {} for y = 16, 52, 3 do STAGES[#STAGES + 1] = function() cut_rows(y, min(y + 2, 52)) end end STAGES[#STAGES + 1] = dt_fwd STAGES[#STAGES + 1] = dt_bwd STAGES[#STAGES + 1] = function() shade_rows(0, 7) end STAGES[#STAGES + 1] = function() shade_rows(8, 15) end for y = 16, 57, 2 do STAGES[#STAGES + 1] = function() shade_rows(y, min(y + 1, 57)) end end STAGES[#STAGES + 1] = function() shade_rows(58, 63) end STAGES[#STAGES + 1] = function() BASE, MG, MC = table.concat(RB), table.concat(R1), table.concat(R2) DIST, COVS, RB, R1, R2 = nil, nil, nil, nil, nil end local ST = 1 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)) i = i % 6 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 r * 255, g * 255, b * 255 end local HUES = { 24, 120, 280, 384 } local PERIOD = { 60, 40, 26, 16, 9 } local AMP = { 0, 0.6, 1.0, 1.5 } local function flame(t) local n = 0.5 * sin(t * 13.1) + 0.3 * sin(t * 21.7 + 1.3) + 0.2 * sin(t * 5.3 + 0.4) local j = (hash32(floor(t * 9) * 0x9E3779B1) & 0xFF) / 255 return max(0, min(1, 0.55 + 0.35 * n + (j - 0.5) * 0.4)) end function render() if not BASE then gfx.clear(5, 6, 14) STAGES[ST]() ST = ST + 1 gfx.ready(false) return end local t = frame / FPS local mode = cfg.mode or "halloween" local ph = (t / PERIOD[cfg.speed or 3]) % 1 local q = flame(t) local hue if mode == "rainbow" then hue = 24 + ph * 360 elseif mode == "orange" then hue = 20 + 10 * q else local u = ph * 3 local seg = min(2, floor(u)) local k = max(0, min(1, (u - seg - 0.3) / 0.4)) hue = HUES[seg + 1] + (HUES[seg + 2] - HUES[seg + 1]) * k * k * (3 - 2 * k) end local lr, lg, lb = hsv(hue, 0.96, 1) local amp = AMP[(cfg.flame or 2) + 1] gfx.blit_rgb(BASE, 0, 0, W, H) gfx.blend_mask(MG, lr, lg, lb, 0, 0, W, H, W, H, 1 - 0.20 * amp * (1 - q)) gfx.blend_mask(MC, lr + (255 - lr) * 0.45, lg + (255 - lg) * 0.45, lb + (255 - lb) * 0.45, 0, 0, W, H, W, H, 1 - 0.45 * amp * (1 - q)) end