info = { type = "image", title = "Split-flap scoreboard", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Flip-digit stadium scoreboard, goals and cheers", api = 44, } local tr = ({ sk = { ["Digits"] = "Číslice", ["Confetti"] = "Konfety" }, cs = { ["Digits"] = "Číslice", ["Confetti"] = "Konfety" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "digits", label = T("Digits"), type = "color", default = "#FFC038" }, { key = "confetti", label = T("Confetti"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, max, min = math.floor, math.sin, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local CYCLE, FLIP, PARTY = 720, 10, 110 local HOME = { { 0, 0 }, { 120, 1 }, { 480, 2 }, { 660, 0 } } local AWAY = { { 0, 0 }, { 300, 1 }, { 660, 0 } } local GOALS = { 120, 300, 480 } local BG, FRAME, TILE_T, TILE_B, HINGE = { 8, 12, 34 }, { 56, 64, 78 }, { 38, 40, 48 }, { 32, 34, 42 }, { 10, 10, 14 } local TX = { 6, 36 } local DY, YH = 19, 29 local SEGR = { a = { 1, 0, 14, 3 }, b = { 13, 2, 3, 8 }, c = { 13, 10, 3, 8 }, d = { 1, 17, 14, 3 }, e = { 0, 10, 3, 8 }, f = { 0, 2, 3, 8 }, g = { 1, 8, 14, 3 } } local SEGS = { [0] = "abcdef", "bc", "abdeg", "abcdg", "bcfg", "acdfg", "acdefg", "abc", "abcdefg", "abcdfg" } local DIG = {} for d = 0, 9 do DIG[d] = {} for s in SEGS[d]:gmatch(".") do DIG[d][#DIG[d] + 1] = SEGR[s] end end local COL = { 255, 192, 56 } local CONFETTI = true local PERSON = {} do local n = 0 for row = 0, 2 do for i = 0, (row == 1 and 8 or 9) do n = n + 1 local h = hash32(n * 0x9E3779B1) PERSON[n] = { x = 2 + 6 * i + (row == 1 and 3 or 0), y = 46 + row * 6, team = umod(h, 4) + 1, skin = umod(h >> 4, 4) + 1, ph = umod(h >> 8, 3) } end end end local TEAM = { { 200, 40, 48 }, { 40, 90, 200 }, { 224, 224, 236 }, { 240, 200, 50 } } local SKIN = { { 240, 196, 160 }, { 214, 160, 120 }, { 170, 112, 80 }, { 120, 78, 56 } } local function board() rect(0, 0, W, H, BG[1], BG[2], BG[3]) rect(5, 0, 10, 5, 30, 34, 46); rect(49, 0, 10, 5, 30, 34, 46) rect(2, 6, 60, 38, FRAME[1], FRAME[2], FRAME[3]) rect(4, 8, 56, 34, 16, 44, 104) for i = 0, 1 do rect(TX[i + 1], 17, 22, 12, TILE_T[1], TILE_T[2], TILE_T[3]) rect(TX[i + 1], 29, 22, 12, TILE_B[1], TILE_B[2], TILE_B[3]) end local F = { font = "3x5" } local top, bot = gfx.text_ink("RED", F) local y = 9 + (7 - (bot - top + 1)) // 2 - top gfx.text(TX[1], y, "RED", 255, 96, 96, { font = "3x5", w = 22, align = "center" }) gfx.text(TX[2], y, "BLUE", 110, 170, 255, { font = "3x5", w = 22, align = "center" }) for _, y0 in ipairs({ 24, 32 }) do rect(31, y0, 2, 2, 220, 228, 250) end for x = 4, 59, 4 do px(x, 7, 120, 92, 40); px(x, 42, 120, 92, 40) end for y = 11, 39, 4 do px(3, y, 120, 92, 40); px(60, y, 120, 92, 40) end end local function crowd() rect(0, 44, W, 20, 14, 16, 34) for _, p in ipairs(PERSON) do local t, s = TEAM[p.team], SKIN[p.skin] rect(p.x, p.y + 3, 5, 3, t[1], t[2], t[3]) rect(p.x + 1, p.y, 3, 3, s[1], s[2], s[3]) rect(p.x + 1, p.y, 3, 1, 40, 30, 28) end end local STAGES = { board, crowd } local SNAP, STAGE = nil, 0 function on_settings() local c = cfg.digits or { r = 255, g = 192, b = 56 } COL = { c.r, c.g, c.b } CONFETTI = cfg.confetti ~= false end local function half(d, tx, which, s, shade) local r, g, b = floor(COL[1] * shade), floor(COL[2] * shade), floor(COL[3] * shade) local x0 = tx + 3 for _, sg in ipairs(DIG[d]) do local y1, y2 if which == 1 then y1, y2 = max(DY + sg[2], DY), min(DY + sg[2] + sg[4], YH) else y1, y2 = max(DY + sg[2], YH), min(DY + sg[2] + sg[4], DY + 20) end if y2 > y1 then local ya, yb if which == 1 then ya, yb = floor(YH - (YH - y1) * s + 0.5), floor(YH - (YH - y2) * s + 0.5) else ya, yb = floor(YH + (y1 - YH) * s + 0.5), floor(YH + (y2 - YH) * s + 0.5) end if yb > ya then rect(x0 + sg[1], ya, sg[3], yb - ya, r, g, b) end end end end local function flap_bg(tx, which, s, shade) local c = which == 1 and TILE_T or TILE_B local h = floor(12 * s + 0.5) if h < 1 then return end rect(tx, which == 1 and YH - h or YH, 22, h, floor(c[1] * shade), floor(c[2] * shade), floor(c[3] * shade)) end local FLAP_S = { 0.75, 0.4, 0.12, 0.4, 0.75 } local FLAP_SHADE = { 0.85, 0.65, 0.45, 0.65, 0.85 } local function digit(tx, cur, prev, p) if not p then half(cur, tx, 1, 1, 1); half(cur, tx, 2, 1, 1) else local ph = p // 2 + 1 half(cur, tx, 1, 1, 1); half(prev, tx, 2, 1, 1) if ph <= 3 then flap_bg(tx, 1, FLAP_S[ph], FLAP_SHADE[ph]); half(prev, tx, 1, FLAP_S[ph], FLAP_SHADE[ph]) else flap_bg(tx, 2, FLAP_S[ph], FLAP_SHADE[ph]); half(cur, tx, 2, FLAP_S[ph], FLAP_SHADE[ph]) end end rect(tx, YH - 1, 22, 1, HINGE[1], HINGE[2], HINGE[3]) end local function score(steps, t) local i = 1 while steps[i + 1] and steps[i + 1][1] <= t do i = i + 1 end local s = steps[i] if i > 1 and t < s[1] + FLIP then return s[2], steps[i - 1][2], t - s[1] end return s[2] end local CONF_N = 36 local CX, CDL, CV, CPH, CC = {}, {}, {}, {}, {} local CCOL = { { 255, 72, 72 }, { 255, 210, 60 }, { 70, 200, 255 }, { 120, 255, 120 }, { 255, 120, 220 }, { 255, 255, 255 } } for i = 1, CONF_N do local h = hash32(i * 0x85EBCA6B) CX[i], CDL[i], CV[i] = 3 + umod(h, 58), umod(h >> 6, 40), 0.55 + umod(h >> 12, 50) / 100 CPH[i], CC[i] = umod(h >> 18, 628) / 100, umod(h >> 22, 6) + 1 end local function lights(u) for bank = 0, 1 do local on = u < 0 or u >= 45 or (u // 3) % 2 == bank for j = 0, 2 do for k = 0, 1 do local x, y = (bank == 0 and 6 or 50) + j * 3, 1 + k * 2 if on then blend_rect(x - 1, y - 1, 3, 3, 255, 240, 190, 0.25); px(x, y, 255, 244, 200) else px(x, y, 96, 90, 80) end end end end end local function cheer(u) if u >= 0 and u < 45 then for i = 0, 15 do if (u // 2 + i) % 3 == 0 then if i < 8 then local x, y = 4 + (i % 4) * 16, i < 4 and 7 or 42 px(x, y, 255, 214, 80); px(x + 4, y, 255, 214, 80) else local x, y = i < 12 and 3 or 60, 11 + (i % 4) * 8 px(x, y, 255, 214, 80); px(x, y + 4, 255, 214, 80) end end end end if u < 0 or u >= 100 then return end for _, p in ipairs(PERSON) do local m = (u // 4 + p.ph) % 3 local s = SKIN[p.skin] if m ~= 2 then rect(p.x, p.y - 2, 1, 5, s[1], s[2], s[3]) end if m ~= 1 then rect(p.x + 4, p.y - 2, 1, 5, s[1], s[2], s[3]) end end end local function confetti(u) for i = 1, CONF_N do local dt = u - 6 - CDL[i] if dt >= 0 then local y = -3 + dt * CV[i] if y < H then local c = CCOL[CC[i]] local x = floor(CX[i] + 3 * sin(dt * 0.12 + CPH[i])) if (dt // 4) % 2 == 0 then rect(x, floor(y), 2, 1, c[1], c[2], c[3]) else rect(x, floor(y), 1, 2, c[1], c[2], c[3]) end end end end end function render() if STAGE < #STAGES then if SNAP then gfx.blit_rgb(SNAP, 0, 0, W, H) end STAGES[STAGE + 1]() STAGE = STAGE + 1 SNAP = gfx.snapshot(0, 0, W, H) gfx.ready(false) return end gfx.blit_rgb(SNAP, 0, 0, W, H) local t = frame % CYCLE local u = -1 for _, g in ipairs(GOALS) do if t >= g and t < g + PARTY then u = t - g end end lights(u) digit(TX[1], score(HOME, t)) digit(TX[2], score(AWAY, t)) cheer(u) if CONFETTI and u >= 0 then confetti(u) end end