info = { type = "image", title = "Wrapped candy", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "A striped candy spins, shines and sparkles", api = 33, } local tr = ({ sk = { ["Stripes"] = "Pruhy", ["Spin"] = "Rotácia", ["slow"] = "pomaly", ["normal"] = "normálne", ["fast"] = "rýchlo", ["Sparkles"] = "Iskričky" }, cs = { ["Stripes"] = "Pruhy", ["Spin"] = "Rotace", ["slow"] = "pomalu", ["normal"] = "normálně", ["fast"] = "rychle", ["Sparkles"] = "Jiskřičky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "stripe", label = T("Stripes"), type = "color", default = "#E8203C" }, { key = "spin", label = T("Spin"), type = "int", default = 1, opts = { T("slow"), T("normal"), T("fast") } }, { key = "sparkle", label = T("Sparkles"), type = "bool", default = true }, } local rect, px, blend, brect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, asin = math.floor, math.sin, math.cos, math.sqrt, math.asin local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.2831853 local BGC, HALO2, HALO1 = { 52, 34, 102 }, { 60, 40, 116 }, { 70, 48, 132 } local TABLE, TABLE_L, SHADOW = { 88, 60, 146 }, { 120, 90, 176 }, { 58, 38, 110 } local P = 36 local CW, CH = 54, 24 local LCX, LCY, RX, RY = 27, 12, 14, 10 local X0, Y0 = 5, 18 local NPAIR = 5 local FANL = 12 local CELLS local RUNS = {} do local abs = math.abs local PW = CW + 2 local ins, FT, FH = {}, {}, {} for i = 1, (CH + 2) * PW + 2 do ins[i] = 0 end for y = 0, CH - 1 do local ady = abs(y + 0.5 - LCY) local dy = (y + 0.5 - LCY) / RY local dy2 = dy * dy local jag = 0.07 * (y % 3) local base = (y + 1) * PW + 2 for x = 0, CW - 1 do local xe = x + 0.5 local dx = (xe - LCX) / RX if dx * dx + dy2 <= 1 then ins[base + x] = 1 else local t if xe < LCX - RX - 0.5 then t = (LCX - RX - 0.5 - xe) / FANL elseif xe > LCX + RX + 0.5 then t = (xe - LCX - RX - 0.5) / FANL end if t and t <= 1 - jag then local h = 1.4 + 7.4 * t if ady <= h then ins[base + x] = 2; FT[base + x] = t; FH[base + x] = h end end end end end local grid = {} local hl_x, hl_y = LCX - 5, LCY - 4 for y = 0, CH - 1 do local row, run, ra = {}, {}, nil local base = (y + 1) * PW + 2 local dy = (y + 0.5 - LCY) / RY for x = 0, CW - 1 do local i = base + x local k = ins[i] local v = 0 if k ~= 0 then if ins[i - 1] == 0 or ins[i + 1] == 0 or ins[i - PW] == 0 or ins[i + PW] == 0 then v = k == 1 and 1 or 3 elseif k == 1 then local dx = (x + 0.5 - LCX) / RX local lam = -0.4 * dx - 0.55 * dy + 0.73 * sqrt(1 - dx * dx - dy * dy) local lvl = lam > 0.72 and 0 or (lam > 0.4 and 1 or 2) local ps = (asin(dy) + 1.4 * dx) * NPAIR / TAU ps = ps - floor(ps) v = 4 + lvl * P + floor(ps * P) % P local ex, ey = (x + 0.5 - hl_x) / 2.8, (y + 0.5 - hl_y) / 1.6 if ex * ex + ey * ey <= 1 then v = 2 end else local vv = (y + 0.5 - LCY) / FH[i] local ps = (asin(vv) + 0.3 * FT[i]) * NPAIR / TAU ps = ps - floor(ps) v = 4 + 3 * P + floor(ps * P) % P end end row[x + 1] = v if v ~= 0 then if not ra then ra = x end elseif ra then run[#run + 1] = ra; run[#run + 1] = x; ra = nil end end if ra then run[#run + 1] = ra; run[#run + 1] = CW end RUNS[y] = run grid[y + 1] = string.char(table.unpack(row, 1, CW)) end CELLS = table.concat(grid) end local MASTER = {} local PAL = {} local function push(t, r, g, b) t[#t + 1] = r; t[#t + 1] = g; t[#t + 1] = b end local function build_master(c) local function mix(r, g, b, k) if k >= 0 then return floor(r + (255 - r) * k), floor(g + (255 - g) * k), floor(b + (255 - b) * k) end return floor(r * (1 + k)), floor(g * (1 + k)), floor(b * (1 + k)) end local reds = { { mix(c.r, c.g, c.b, 0.33) }, { c.r, c.g, c.b }, { mix(c.r, c.g, c.b, -0.42) } } local whites = { { 255, 246, 250 }, { 238, 204, 222 }, { 188, 142, 172 } } MASTER = {} for lvl = 1, 3 do local m, n = {}, 0 local R, W2 = reds[lvl], whites[lvl] for j = 0, 2 * P - 1 do local c1 = (j % P) < P // 2 and R or W2 m[n + 1], m[n + 2], m[n + 3] = c1[1], c1[2], c1[3] n = n + 3 end MASTER[lvl] = m end local m, n = {}, 0 for j = 0, 2 * P - 1 do local q = j % P local r, g, b = 250, 248, 255 if q < P // 5 then r, g, b = 168, 160, 208 elseif q < P // 2 then r, g, b = 236, 234, 252 end m[n + 1], m[n + 2], m[n + 3] = r, g, b n = n + 3 end MASTER[4] = m PAL = {} push(PAL, 0, 0, 0); push(PAL, 104, 8, 40); push(PAL, 255, 255, 255); push(PAL, 196, 184, 226) for _ = 1, 4 * P do push(PAL, 0, 0, 0) end end local LAST_S = -1 local function set_shift(s) if s == LAST_S then return end LAST_S = s for k = 1, 4 do table.move(MASTER[k], s * 3 + 1, (s + P) * 3, 13 + (k - 1) * P * 3, PAL) end end local STEPS = { 1 / 12, 1 / 7, 1 / 4 } local SPIN = 1 / 7 local SPARK = true function on_settings() local c = cfg.stripe or { r = 232, g = 32, b = 60 } build_master(c) LAST_S = -1 SPIN = STEPS[(cfg.spin or 1) + 1] or 1 / 7 SPARK = cfg.sparkle ~= false end local BG = nil local function disc(cx, cy, r, c) for y = cy - r, cy + r do local dy = y + 0.5 - cy local q = r * r - dy * dy if q > 0 then local hw = floor(sqrt(q) + 0.5) rect(cx - hw, y, 2 * hw, 1, c[1], c[2], c[3]) end end end local function build_bg() rect(0, 0, W, H, BGC[1], BGC[2], BGC[3]) disc(32, 30, 29, HALO2) disc(32, 30, 21, HALO1) rect(0, 46, W, 18, TABLE[1], TABLE[2], TABLE[3]) rect(0, 46, W, 1, TABLE_L[1], TABLE_L[2], TABLE_L[3]) BG = gfx.snapshot(0, 0, W, H) end local GLOSS_PER, GLOSS_LEN = 138, 30 local function draw_gloss(u, y0) local bx = -8 + u * (CW + 16) for y = 0, CH - 1 do local run = RUNS[y] local xc = bx - (y - LCY) * 0.9 local xa, xb = xc - 2.2, xc + 2.2 for i = 1, #run, 2 do local a, b = run[i], run[i + 1] local s, e = xa > a and xa or a, xb < b and xb or b if e > s then local fs, fe = floor(s + 0.5), floor(e + 0.5) if fe > fs then brect(X0 + fs, y0 + y, fe - fs, 1, 255, 255, 255, 0.55) end end end end end local function star(x, y, s, r, g, b) local L = floor(1 + 3 * s + 0.5) px(x, y, 255, 255, 255) for d = 1, L do local a = 0.95 - 0.8 * d / (L + 1) blend(x - d, y, r, g, b, a); blend(x + d, y, r, g, b, a) blend(x, y - d, r, g, b, a); blend(x, y + d, r, g, b, a) end end local NSPK = 6 local function draw_sparkles(ts) for j = 0, NSPK - 1 do local per = 2.0 + j * 0.37 local tt = ts + j * 0.9 local k = floor(tt / per) local u = (tt / per - k) / 0.3 if u < 1 then local h = hash32(k * 131 + j * 0x9E3779B1) local x, y = 5 + umod(h, 54), 10 + umod(h >> 8, 36) star(x, y, sin(3.14159265 * u), 255, 244, 196) end end end function render() if not BG then build_bg() end if #PAL == 0 then build_master({ r = 232, g = 32, b = 60 }) end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local ts = t / STEP_HZ local bob = floor(1.3 * sin(ts * 1.6) + 0.5) local y0 = Y0 + bob local sw = 16 - bob * 1.1 local sy = 52 for y = sy - 2, sy + 2 do local dy = (y + 0.5 - sy) / 2.6 local q = 1 - dy * dy if q > 0 then local hw = sw * sqrt(q) rect(floor(32 - hw + 0.5), y, floor(2 * hw + 0.5), 1, SHADOW[1], SHADOW[2], SHADOW[3]) end end local s = floor(t * SPIN / STEP_HZ * NPAIR * P) % P set_shift(s) gfx.blit_indexed(CELLS, PAL, X0, y0, CW, CH, CW, CH, true) local g = t % GLOSS_PER if g < GLOSS_LEN then local u = g / GLOSS_LEN draw_gloss(u, y0) local xc = -8 + u * (CW + 16) - (LCY - 4 - LCY) * 0.9 local near = 1 - math.abs(xc - (LCX - 5)) / 7 if near > 0 then star(X0 + LCX - 5, y0 + LCY - 4, near, 255, 255, 255) end end if SPARK then draw_sparkles(ts) end end