info = { type = "image", title = "Wheel of fortune", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "Prize wheel spins, ticks and lands on a winner", api = 33, } local STEP_HZ = 30 local FR = 0 local tr = ({ sk = { ["Segments"] = "Segmenty", ["Palette"] = "Paleta", ["Rainbow"] = "Dúha", ["Candy"] = "Cukrík", ["Casino"] = "Kasíno", ["Pastel"] = "Pastelová" }, cs = { ["Segments"] = "Segmenty", ["Palette"] = "Paleta", ["Rainbow"] = "Duha", ["Candy"] = "Bonbon", ["Casino"] = "Kasino", ["Pastel"] = "Pastelová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Segments"), type = "int", min = 4, max = 12, default = 8 }, { key = "pal", label = T("Palette"), type = "int", default = 0, opts = { T("Rainbow"), T("Candy"), T("Casino"), T("Pastel") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, exp, abs, atan = math.floor, math.sin, math.cos, math.exp, math.abs, math.atan local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local PI, TAU = 3.14159265, 6.28318531 local HALFPI = PI / 2 local K = 240 local CX, CY = 32, 34 local GX, GY, GS = CX - 28, CY - 28, 56 local RPEG = 25.5 local GRID do local QV, RING = {}, {} local QK = K // 4 for j = 0, 27 do for i = 0, 27 do local dx, dy = i + 0.5, j + 0.5 local d2 = dx * dx + dy * dy local v if d2 >= 784 then v = 0 elseif d2 >= 729 then v = 242 elseif d2 >= 576 then v = 241 elseif d2 >= 538 then v = 245 elseif d2 < 4.84 then v = 244 elseif d2 < 17.64 then v = 243 end local k = j * 28 + i if v then RING[k] = v elseif j <= i then QV[k] = floor(atan(dy, dx) / TAU * K) else QV[k] = QK - 1 - floor(atan(dx, dy) / TAU * K) end end end local rows = {} local LR, LL, UR, UL = {}, {}, {}, {} for j = 0, 27 do for i = 0, 27 do local k = j * 28 + i local v = RING[k] local lr, ll, ur, ul if v then lr, ll, ur, ul = v, v, v, v else local b = QV[k] lr, ll, ur, ul = b + 1, 2 * QK - b, K - b, 2 * QK + b + 1 end LR[i + 1], LL[28 - i], UR[i + 1], UL[28 - i] = lr, ll, ur, ul end rows[29 + j] = string.char(table.unpack(LL, 1, 28)) .. string.char(table.unpack(LR, 1, 28)) rows[28 - j] = string.char(table.unpack(UL, 1, 28)) .. string.char(table.unpack(UR, 1, 28)) end GRID = table.concat(rows) end local PAL = {} for i = 1, 246 * 3 do PAL[i] = 0 end local function setp(i, r, g, b) PAL[3 * i + 1], PAL[3 * i + 2], PAL[3 * i + 3] = r, g, b end setp(241, 226, 172, 58) setp(242, 140, 90, 28) setp(243, 236, 190, 76) setp(244, 255, 244, 200) setp(245, 44, 30, 40) local function hsv(h, s, v) local h6 = h * 6 local i = floor(h6) % 6 local f = h6 - floor(h6) 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 CANDY = { 255, 84, 148, 255, 238, 244, 84, 196, 250, 255, 204, 60 } local N, SC, BRIGHT = 8, {}, {} function on_settings() N = cfg.n or 8 local p = cfg.pal or 0 SC, BRIGHT = {}, {} for i = 0, N - 1 do local r, g, b if p == 0 then r, g, b = hsv(i / N, 0.9, 1) elseif p == 3 then r, g, b = hsv(i / N, 0.42, 1) elseif p == 1 then local k = i % 4 if i == N - 1 and N > 1 and k == 0 then k = 1 end r, g, b = CANDY[3 * k + 1], CANDY[3 * k + 2], CANDY[3 * k + 3] else if i == N - 1 and N % 2 == 1 then r, g, b = 24, 150, 64 elseif i % 2 == 0 then r, g, b = 220, 32, 44 else r, g, b = 34, 32, 40 end end SC[3 * i + 1], SC[3 * i + 2], SC[3 * i + 3] = r, g, b BRIGHT[i] = r + g + b > 600 end end local function paint(rot, win, flash) local S = TAU / N local st = N / K local q = 0.5 * st - rot / S local edge = 0.9 * st for k = 1, K do local fl = q // 1 local fr = q - fl local o = 3 * k if fr < edge or fr > 1 - edge then PAL[o + 1], PAL[o + 2], PAL[o + 3] = 44, 30, 40 else local s = fl % N if flash and s == win then if BRIGHT[s] then PAL[o + 1], PAL[o + 2], PAL[o + 3] = 255, 196, 30 else PAL[o + 1], PAL[o + 2], PAL[o + 3] = 255, 255, 255 end else local c = 3 * s PAL[o + 1], PAL[o + 2], PAL[o + 3] = SC[c + 1], SC[c + 2], SC[c + 3] end end q = q + st end end local function splat(x, y, r, g, b) x, y = x - 0.5, y - 0.5 local x0, y0 = floor(x), floor(y) local fx, fy = x - x0, y - y0 local w = (1 - fx) * (1 - fy) if w > 0.06 then blend_rect(x0, y0, 1, 1, r, g, b, w) end w = fx * (1 - fy) if w > 0.06 then blend_rect(x0 + 1, y0, 1, 1, r, g, b, w) end w = (1 - fx) * fy if w > 0.06 then blend_rect(x0, y0 + 1, 1, 1, r, g, b, w) end w = fx * fy if w > 0.06 then blend_rect(x0 + 1, y0 + 1, 1, 1, r, g, b, w) end end local PTR = { -3.5, -1.5, 3.5, -1.5, 3.5, 1.5, 0.8, 9, -0.8, 9, -3.5, 1.5 } local PV_X, PV_Y = 32, 1.5 local PP = {} local function pspan(P, n, yc) local lo, hi local x1, y1 = P[n - 1], P[n] for i = 1, n, 2 do local x2, y2 = P[i], P[i + 1] if (y1 <= yc) ~= (y2 <= yc) then local x = x1 + (yc - y1) * (x2 - x1) / (y2 - y1) if not lo then lo, hi = x, x elseif x < lo then lo = x elseif x > hi then hi = x end end x1, y1 = x2, y2 end return lo, hi end local function pointer(phi) local c, s = cos(phi), sin(phi) local y0, y1 = 99, -99 for i = 1, #PTR, 2 do local x, y = PTR[i], PTR[i + 1] local X, Y = PV_X + x * c - y * s, PV_Y + x * s + y * c PP[i], PP[i + 1] = X, Y if Y < y0 then y0 = Y end if Y > y1 then y1 = Y end end local n = #PP for y = math.ceil(y0 - 0.5), math.ceil(y1 - 0.5) - 1 do local a, b = pspan(PP, n, y + 0.5) if a then local x0, x1 = math.ceil(a - 0.5), math.ceil(b - 0.5) - 1 if x1 >= x0 then rect(x0, y, x1 - x0 + 1, 1, 226, 36, 48) if x1 > x0 then px(x1, y, 150, 18, 30) end end end end rect(31, 0, 2, 2, 255, 226, 120) end local LC, TA, TD, BLINK = 330, 15, 195, 45 local OFF = 30 local function rest_angle(n) local h = hash32((n + 7919) * 0x9E3779B1) local s = umod(h, N) local fr = 0.22 + umod(h >> 8, 57) / 100 return (1.5 * PI - (s + fr) * (TAU / N)) % TAU end local BG = nil local function build_bg() rect(0, 0, W, H, 22, 20, 54) rect(24, 61, 16, 3, 120, 78, 26) rect(20, 63, 24, 1, 90, 58, 20) BG = gfx.snapshot(0, 0, W, H) end function render() FR = frame * STEP_HZ // gfx.FPS if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = FR + OFF local n, t = f // LC, f % LC local r0 = rest_angle(n - 1) local turns = 3 + umod(hash32(n * 0x85EBCA6B), 2) local TH = turns * TAU + (rest_angle(n) - r0) % TAU local v0 = 2 * TH / (TA + TD) local rot, v if t < TA then rot, v = 0.5 * (v0 / TA) * t * t, v0 * t / TA elseif t < TA + TD then local u = t - TA rot, v = 0.5 * v0 * TA + v0 * u - 0.5 * (v0 / TD) * u * u, v0 * (1 - u / TD) else rot, v = TH, 0 end rot = r0 + rot local S = TAU / N local qp = (1.5 * PI - rot) / S local win = floor(qp) % N local tb = t - TA - TD local flash = tb >= 0 and tb < BLINK and (tb // 4) % 2 == 0 paint(rot, win, flash) gfx.blit_indexed(GRID, PAL, GX, GY, GS, GS, GS, GS, true) for j = 0, N - 1 do local a = j * S + rot splat(CX + RPEG * cos(a), CY + RPEG * sin(a), 255, 255, 255) end local g = qp - floor(qp) local DEL = 2.3 / RPEG local b = g * S local a = (1 - g) * S local phi = 0 if b < DEL and v > 0 then phi = -0.55 * (1 - b / DEL) elseif v > 1e-4 then local u = a / v if u < 40 then phi = -0.55 * exp(-u / 5) * cos(u * 0.9) end end pointer(phi) for i = 0, 9 do local y = 4 + 6 * i local on if tb >= 0 and tb < BLINK then on = flash else on = (i + (FR // 4)) % 3 == 0 end if on then rect(0, y, 2, 2, 255, 222, 120) rect(62, y, 2, 2, 255, 222, 120) else rect(0, y, 2, 2, 96, 72, 50) rect(62, y, 2, 2, 96, 72, 50) end end end