info = { type = "image", title = "Coin spin", category = "btc", author = "BRRRO", version = "1.0", license = "MIT", desc = "A gold coin spins and floats over its shadow", api = 33, } local tr = ({ sk = { ["Spin speed"] = "Rýchlosť otáčania", ["Background"] = "Pozadie", ["Hover"] = "Vznášanie" }, cs = { ["Spin speed"] = "Rychlost otáčení", ["Background"] = "Pozadí", ["Hover"] = "Vznášení" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Spin speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "bg", label = T("Background"), type = "color", default = "#5A7A8C" }, { key = "hover", label = T("Hover"), type = "bool", default = true }, } local rect, blend_rect, blit_idx = gfx.rect, gfx.blend_rect, gfx.blit_indexed local floor, sin, cos, sqrt, abs, min, max = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.min, math.max local W, FPS = gfx.W, gfx.FPS local R, D = 24, 48 local TH = 4 local G = { { 70, 42, 10 }, { 118, 76, 18 }, { 164, 112, 28 }, { 204, 152, 44 }, { 232, 184, 74 }, { 250, 212, 118 }, { 255, 238, 176 } } local PALF, PALS = { 0, 0, 0 }, { 0, 0, 0 } for i = 1, 7 do for c = 1, 3 do PALF[#PALF + 1], PALS[#PALS + 1] = G[i][c], G[i][c] end end local GLYPH = { face = { s = 3, "..#.#...", "######..", ".##..##.", ".##..##.", ".#####..", ".##...##", ".##...##", "#######.", "..#.#..." }, back = { s = 3, "###..#.", "..#.##.", "###..#.", "#....#.", "###.###" }, } for _, g in pairs(GLYPH) do g.w, g.h = #g[1] * g.s, #g * g.s g.x0, g.y0 = (D - g.w) // 2, (D - g.h) // 2 end local function bit(g, x, y) local gx, gy = x - g.x0, y - g.y0 if gx < 0 or gy < 0 or gx >= g.w or gy >= g.h then return false end return g[gy // g.s + 1]:byte(gx // g.s + 1) == 35 end local function cell(x, y, g) local dx, dy = x + 0.5 - R, y + 0.5 - R local d = sqrt(dx * dx + dy * dy) if d > R then return 0 end local lit = -(dx * 0.55 + dy * 0.83) / R local v if d > R - 1 then v = 2.4 + 2 * lit elseif d > R - 4.5 then v = 4.6 + 2.6 * lit elseif d > R - 5.6 then return 2 elseif bit(g, x, y) then return 2 elseif bit(g, x - 1, y - 1) then return 7 else v = 4.3 + 1.8 * lit + (1 - d / (R - 5.6)) * 0.8 end return max(1, min(7, floor(v))) end local SIDE = {} do local r = {} for y = 0, D - 1 do local v = 4 - 3 * (y + 0.5 - R) / R r[y + 1] = max(1, min(7, floor(v + 0.5) - (y % 2 == 0 and 1 or 0))) end SIDE = string.char(table.unpack(r, 1, D)) end local FACE, BACK, FR, BR = nil, nil, {}, {} local STEP, ROWS_PER = 0, 6 local function build() local row = {} for y = STEP * ROWS_PER, STEP * ROWS_PER + ROWS_PER - 1 do for x = 0, D - 1 do row[x + 1] = cell(x, y, GLYPH.face) end FR[y + 1] = string.char(table.unpack(row, 1, D)) for x = 0, D - 1 do row[x + 1] = cell(x, y, GLYPH.back) end BR[y + 1] = string.char(table.unpack(row, 1, D)) end STEP = STEP + 1 if STEP * ROWS_PER >= D then FACE, BACK = table.concat(FR), table.concat(BR) end end local function tint(pal, k) for i = 1, 7 do for c = 1, 3 do pal[i * 3 + c] = min(255, floor(G[i][c] * k)) end end end local function chord(xx, xc, hw) if hw < 0.5 then return R end local u = (xx - xc) / hw return floor(R * sqrt(max(0, 1 - u * u)) + 0.5) end local OMEGA = { 6.2832 / 7, 6.2832 / 5, 6.2832 / 3.6, 6.2832 / 2.6, 6.2832 / 1.9 } function render() local t = frame / FPS local bg = cfg.bg gfx.clear(bg.r, bg.g, bg.b) if not FACE then build() return end local a = (t - 2) * OMEGA[cfg.speed or 3] local ca, sa = cos(a), sin(a) local yc = 30 + (cfg.hover and 2.5 * sin(t * 1.85) or 0) local yi = floor(yc + 0.5) local lift = (yc - 27.5) / 5 local sw = (13 + 2.5 * lift) * (0.3 + 0.7 * abs(ca)) local al = 0.3 + 0.08 * lift for k = -1, 1 do local hw = floor(sw * (k == 0 and 1 or 0.65) + 0.5) blend_rect(32 - hw, 59 + k, 2 * hw, 1, 0, 0, 0, al * (k == 0 and 1 or 0.55)) end local lit = (ca >= 0 and 1 or -1) * (0.87 * ca - 0.5 * sa) tint(PALF, (ca >= 0 and 1 or 0.78) * (0.55 + 0.55 * max(0, min(1, lit)))) tint(PALS, 0.55 + 0.4 * abs(sa)) local w = floor(D * abs(ca) + 0.5) local shift = -(ca >= 0 and 1 or -1) * TH * sa local sh = floor(abs(shift) + 0.5) local x0 = floor(32 - (w + sh) / 2 + 0.5) local fx = x0 + (shift < 0 and sh or 0) local bx = shift < 0 and x0 or fx + w local xcb = fx + w / 2 + (shift < 0 and -sh or sh) if sh > 0 and w >= 1 then blit_idx(ca >= 0 and FACE or BACK, PALS, fx + (shift < 0 and -sh or sh), yi - R, w, D, D, D, true) end local lo, hi = min(xcb, fx + w / 2), max(xcb, fx + w / 2) for i = 0, sh - 1 do local xx = bx + i + 0.5 local hh = chord(xx, max(lo, min(hi, xx)), w / 2) if hh > 0 then blit_idx(SIDE, PALS, bx + i, yi - hh, 1, 2 * hh, 1, D) end end if w >= 3 then blit_idx(ca >= 0 and FACE or BACK, PALF, fx, yi - R, w, D, D, D, true) local p = (t % 3.4) / 0.8 if p < 1 and w >= 12 then local xs = fx + floor(p * w) for k = -1, 1 do local hh = chord(xs + k + 0.5, fx + w / 2, w / 2) if hh > 0 then blend_rect(xs + k, yi - hh, 1, 2 * hh, 255, 250, 230, k == 0 and 0.3 or 0.12) end end end end end