info = { type = "image", title = "Star spin", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "A golden star spins over turning rays", api = 33, } local ltr = ({ sk = { ["Ray colour"] = "Farba lúčov", ["Spin speed"] = "Rýchlosť otáčania" }, cs = { ["Ray colour"] = "Barva paprsků", ["Spin speed"] = "Rychlost otáčení" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "rays", label = LT("Ray colour"), type = "color", default = "#D62830" }, { key = "speed", label = LT("Spin speed"), type = "int", min = 1, max = 3, default = 2 }, } local rect = gfx.rect local floor, sqrt, sin, cos, atan = math.floor, math.sqrt, math.sin, math.cos, math.atan local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local S, RO, RI = 44, 21, 8.5 local UX, UY = {}, {} for i = 0, 4 do UX[i], UY[i] = cos(-1.570796 + i * TAU / 5), sin(-1.570796 + i * TAU / 5) end local C36, S36 = cos(0.628319), sin(0.628319) local STAR local NX, NY, NZ = {}, {}, {} local MIRROR = {} do local HC = 9 for i = 0, 4 do for side = 1, 2 do local k = i * 2 + side local sg = side == 1 and -1 or 1 local a = -1.570796 + i * TAU / 5 + sg * 0.628319 local tx, ty = RO * UX[i], RO * UY[i] local vx, vy = RI * cos(a), RI * sin(a) local ax, ay, az = tx, ty, -HC local bx, by, bz = vx, vy, -HC local nx, ny, nz = ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx if nz < 0 then nx, ny, nz = -nx, -ny, -nz end local l = sqrt(nx * nx + ny * ny + nz * nz) NX[k], NY[k], NZ[k] = nx / l, ny / l, nz / l MIRROR[k] = ((5 - i) % 5) * 2 + (3 - side) end end local LEN = sqrt((RI * C36 - RO) ^ 2 + (RI * S36) ^ 2) local R2 = (RO + 0.5) * (RO + 0.5) local rows, row = {}, {} for y = 0, S - 1 do local py = y + 0.5 - S / 2 for x = 0, S // 2 - 1 do local px = x + 0.5 - S / 2 local id = 0 if px * px + py * py < R2 then local bi = floor((atan(py, px) + 2.199115) / 1.256637) % 5 local ux, uy = UX[bi], UY[bi] local v = ux * py - uy * px local q = v < 0 and -v or v local cr = (RI * C36 - RO) * q - (RI * S36) * (px * ux + py * uy - RO) if cr >= 0 then id = cr < LEN and 11 or bi * 2 + (v < 0 and 1 or 2) end end row[x + 1] = id row[S - x] = (id == 0 or id == 11) and id or MIRROR[id] end rows[y + 1] = string.char(table.unpack(row, 1, S)) end STAR = table.concat(rows) end local TONES = { { 132, 62, 8 }, { 198, 112, 16 }, { 242, 168, 30 }, { 255, 214, 74 }, { 255, 246, 178 }, } local SPAL = { 0, 0, 0 } for k = 1, 11 do SPAL[k * 3 + 1], SPAL[k * 3 + 2], SPAL[k * 3 + 3] = 96, 28, 10 end local LX, LY, LZ = -0.42, -0.58, 0.70 local function star_palette(cs, sn) for k = 1, 10 do local m, back = k, cs < 0 if back then m = MIRROR[k] end local nx, ny, nz = NX[m], NY[m], NZ[m] if back then nz = -nz end local rx, rz = nx * cs + nz * sn, -nx * sn + nz * cs local b = rx * LX + ny * LY + rz * LZ local ti = b < 0.12 and 1 or b < 0.38 and 2 or b < 0.6 and 3 or b < 0.8 and 4 or 5 local c = TONES[ti] SPAL[k * 3 + 1], SPAL[k * 3 + 2], SPAL[k * 3 + 3] = c[1], c[2], c[3] end end local NS, BAND = 240, 10 local RQ = 16 local SECT do local NQ = NS // 4 local T = {} for k = 1, NQ - 1 do T[k] = math.tan(k * TAU / NS) end local BR, row = {}, {} for qy = 0, RQ - 1 do local s = NQ - 1 for qx = 0, RQ - 1 do local r = (qy + 0.5) / (qx + 0.5) while s > 0 and T[s] > r do s = s - 1 end row[qx + 1] = s + 1 end BR[qy + 1] = string.char(table.unpack(row, 1, RQ)) end local mBL, mTL, mTR = {}, {}, {} for k = 0, NS // 4 - 1 do local ch = string.char(k + 1) mBL[ch] = string.char(NS // 2 - k) mTL[ch] = string.char(NS // 2 + k + 1) mTR[ch] = string.char(NS - k) end local rows = {} for y = 0, 2 * RQ - 1 do if y >= RQ then local r = BR[y - RQ + 1] rows[y + 1] = r:gsub(".", mBL):reverse() .. r else local r = BR[RQ - y] rows[y + 1] = r:gsub(".", mTL):reverse() .. r:gsub(".", mTR) end end SECT = table.concat(rows) end local RPAL = { 0, 0, 0 } local RA, RB = { 214, 40, 48 }, { 168, 22, 36 } local function ray_palette(rot) local ir = floor(rot) local f = rot - ir for k = 0, NS - 1 do local p = k + ir local band = (p // BAND) % 2 local c1 = band == 0 and RA or RB local r, g, b = c1[1], c1[2], c1[3] if p % BAND == BAND - 1 and f > 0 then local c2 = band == 0 and RB or RA r, g, b = floor(r + (c2[1] - r) * f), floor(g + (c2[2] - g) * f), floor(b + (c2[3] - b) * f) end local o = k * 3 + 4 RPAL[o], RPAL[o + 1], RPAL[o + 2] = r, g, b end end function on_settings() local c = cfg.rays or { r = 214, g = 40, b = 48 } RA = { c.r, c.g, c.b } RB = { c.r * 4 // 5, c.g * 3 // 5, c.b * 3 // 4 } end local SPX, SPY = {}, {} for i = 0, 5 do local a = -1.570796 + (i + 0.5) * TAU / 6 + 0.3 local h = hash32(i * 0x9E3779B1) local r = 25 + umod(h, 5) SPX[i + 1], SPY[i + 1] = floor(32 + r * cos(a)), floor(32 + r * sin(a)) end local SIZE = { 0, 1, 2, 3, 2, 1, 0 } local function sparkle(x, y, L) if L == 0 then gfx.px(x, y, 255, 240, 190); return end rect(x - L, y, 2 * L + 1, 1, 255, 236, 150) rect(x, y - L, 1, 2 * L + 1, 255, 236, 150) gfx.px(x, y, 255, 255, 255) end function render() local t = frame * STEP_HZ // FPS ray_palette(t * 0.5) gfx.blit_indexed(SECT, RPAL, 0, 0, W, H, 2 * RQ, 2 * RQ) local spin = ({ 96, 72, 50 })[cfg.speed or 2] or 72 local cyc = spin + 54 local f = t % cyc local th = 0 if f < spin then local u = f / spin th = TAU * u * u * (3 - 2 * u) end local cs, sn = cos(th), sin(th) star_palette(cs, sn) local w = floor(S * (cs < 0 and -cs or cs) / 2 + 0.5) * 2 if w < 2 then w = 2 end gfx.blit_indexed(STAR, SPAL, 32 - w // 2, 32 - S // 2, w, S, S, S, true) for i = 1, #SPX do local k = (t + i * 17) % 40 if k < #SIZE then sparkle(SPX[i], SPY[i], SIZE[k + 1]) end end if f >= spin then local k = (f - spin) // 3 if k < #SIZE then sparkle(25, 22, SIZE[k + 1]) end end end