info = { type = "image", title = "Chess game", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Scholar's mate on a tilted chessboard", api = 44, } local tr = ({ sk = { ["Pieces"] = "Figúrky", ["Classic"] = "Klasické", ["Wooden"] = "Drevené", ["Speed"] = "Rýchlosť" }, cs = { ["Pieces"] = "Figurky", ["Classic"] = "Klasické", ["Wooden"] = "Dřevěné", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "set", label = T("Pieces"), type = "int", default = "classic", opts = { keys = { "classic", "wood" }, T("Classic"), T("Wooden") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 3, default = 2 }, } local rect, blend_rect, blit_rgb, blit = gfx.rect, gfx.blend_rect, gfx.blit_rgb, gfx.blit_indexed local floor, ceil, sin, sqrt, abs = math.floor, math.ceil, math.sin, math.sqrt, math.abs local W, H, FPS = gfx.W, gfx.H, gfx.FPS local function Yf(i) local s = i / 8; return 9 + 52.5 * (s + 0.35 * s * s) / 1.35 end local function HW(y) return 15 + (y - 9) * (15 / 52.5) end local function px_of(f, r) local y = Yf(r + 0.5) return 32 + HW(y) * (f + 0.5 - 4) / 4, y + 1 end local SETS = { classic = { light = { 236, 232, 208 }, dark = { 104, 140, 92 }, wh = { 250, 248, 240 }, wl = { 120, 116, 112 }, wo = { 60, 54, 48 }, bh = { 104, 106, 124 }, bl = { 22, 22, 28 }, bo = { 4, 4, 8 } }, wood = { light = { 232, 190, 130 }, dark = { 140, 90, 52 }, wh = { 252, 226, 168 }, wl = { 150, 112, 70 }, wo = { 70, 40, 20 }, bh = { 134, 84, 52 }, bl = { 46, 24, 14 }, bo = { 10, 4, 2 } }, } local S = SETS.classic local PALW, PALB = nil, nil local BG, BGSTAGE = nil, 0 local SPEEDV = { 1.2, 1.8, 2.6 } local function ramp(lo, hi, out) local p = { 0, 0, 0 } for i = 0, 5 do for j = 1, 3 do p[#p + 1] = floor(lo[j] + (hi[j] - lo[j]) * i / 5 + 0.5) end end for j = 1, 3 do p[#p + 1] = out[j] end return p end local INIT = {} local BACK = { "R", "N", "B", "Q", "K", "B", "N", "R" } for f = 0, 7 do INIT[#INIT + 1] = { BACK[f + 1], true, f, 7 } INIT[#INIT + 1] = { "P", true, f, 6 } INIT[#INIT + 1] = { "P", false, f, 1 } INIT[#INIT + 1] = { BACK[f + 1], false, f, 0 } end local function at(f, r) for i, p in ipairs(INIT) do if p[3] == f and p[4] == r then return i end end end local MOVES = { { at(4, 6), 4, 6, 4, 4 }, { at(4, 1), 4, 1, 4, 3 }, { at(5, 7), 5, 7, 2, 4 }, { at(1, 0), 1, 0, 2, 2 }, { at(3, 7), 3, 7, 7, 3 }, { at(6, 0), 6, 0, 5, 2 }, { at(3, 7), 7, 3, 5, 1, at(5, 1) }, } local KING = at(4, 0) local ST, DU, CYC = {}, {}, 0 function on_settings() S = SETS[cfg.set] or SETS.classic PALW, PALB = ramp(S.wl, S.wh, S.wo), ramp(S.bl, S.bh, S.bo) BG, BGSTAGE = nil, 0 local v = SPEEDV[cfg.speed or 2] local t = 24 for k, m in ipairs(MOVES) do local x0, y0 = px_of(m[2], m[3]) local x1, y1 = px_of(m[4], m[5]) ST[k] = t DU[k] = math.max(10, floor(sqrt((x1 - x0) ^ 2 + (y1 - y0) ^ 2) / v)) t = t + DU[k] + 18 end CYC = t + 50 + 40 + 18 end local function board_rows(ya, yb) for y = ya, yb do local hw = HW(y < 9 and 9 or y) if y < 9 or y > 61 then rect(32 - floor(hw) - 3, y, 2 * floor(hw) + 6, 1, 96, 64, 40) else local yy = (y + 0.5 - 9) / 52.5 * 1.35 local r = floor((sqrt(1 + 1.4 * yy) - 1) / 0.7 * 8) rect(32 - floor(hw) - 3, y, 2 * floor(hw) + 6, 1, 96, 64, 40) rect(floor(32 - hw + 0.5), y, floor(2 * hw + 0.5), 1, S.light[1], S.light[2], S.light[3]) for c = 0, 7 do if (c + r) % 2 == 1 then local x0, x1 = floor(32 + hw * (c - 4) / 4 + 0.5), floor(32 + hw * (c - 3) / 4 + 0.5) rect(x0, y, x1 - x0, 1, S.dark[1], S.dark[2], S.dark[3]) end end end end end local PRIM = { P = { { "e", 0, 1, 3, 1.1 }, { "e", 0, 3.8, 1.9, 2.6 }, { "e", 0, 5.8, 2.3, 0.8 }, { "e", 0, 7.4, 1.9, 1.9 } }, R = { { "e", 0, 1, 3.2, 1.2 }, { "r", 0, 4.2, 2.1, 3 }, { "r", 0, 8, 2.8, 1.4 } }, B = { { "e", 0, 1, 3.2, 1.2 }, { "e", 0, 4.4, 2.2, 3.4 }, { "e", 0, 7.2, 2.6, 0.8 }, { "e", 0, 9.4, 1.8, 2.6 }, { "e", 0, 11.8, 0.9, 0.9 } }, N = { { "e", 0, 1, 3.2, 1.2 }, { "e", 0.2, 4, 2.4, 3.2 }, { "e", -0.3, 7, 1.7, 2.6 }, { "e", 1.1, 7.2, 0.8, 2.2 }, { "e", -1.2, 9, 2.3, 1.5 }, { "e", -3, 8.2, 1.5, 1 }, { "e", 0.3, 10.7, 0.8, 1.1 } }, Q = { { "e", 0, 1, 3.2, 1.2 }, { "e", 0, 4.6, 2.4, 3.7 }, { "e", 0, 8, 3, 0.8 }, { "e", 0, 10.2, 2.4, 1.6 }, { "e", 0, 12.6, 0.9, 0.9 } }, K = { { "e", 0, 1, 3.2, 1.2 }, { "e", 0, 4.8, 2.5, 3.9 }, { "e", 0, 8.4, 3, 0.8 }, { "e", 0, 10.4, 2.1, 1.7 }, { "r", 0, 13.2, 0.5, 1.5 }, { "r", 0, 13.5, 1.4, 0.5 } }, } local GRID, GW, GH = {}, {}, {} local queue, qn = {}, 1 local function sc_of(k) return 0.108 * 2 * HW(Yf(k + 0.5)) / 8 end local function bake(ty, k) local sc = sc_of(k) local gw, gh = 2 * ceil(3.7 * sc) + 3, ceil(15.4 * sc) + 3 local cxp = gw // 2 + 0.5 local g = {} for i = 1, gw * gh do g[i] = 0 end local pad = 0.35 / sc for _, p in ipairs(PRIM[ty]) do local rx, ry = p[4] + pad, p[5] + pad for y = 0, gh - 1 do local ly = (gh - 2 - (y + 0.5)) / sc local ny = (ly - p[3]) / ry if ny > -1 and ny < 1 then for x = 0, gw - 1 do local nx = ((x + 0.5 - cxp) / sc - p[2]) / rx local a = p[1] == "e" and 1 - ny * ny or 1 if p[1] == "e" and nx * nx > a then a = -1 end if a > 0 and nx > -1 and nx < 1 then local nz = p[1] == "e" and sqrt(a - nx * nx) or sqrt(1 - nx * nx) local d = -0.65 * nx + (p[1] == "e" and 0.3 * ny or 0) + 0.7 * nz if d < 0 then d = 0 elseif d > 1 then d = 1 end g[y * gw + x + 1] = 1 + floor(d * 5 + 0.5) end end end end end local o = {} for y = 0, gh - 1 do for x = 0, gw - 1 do local i = y * gw + x + 1 if g[i] == 0 and ((x > 0 and g[i - 1] > 0) or (x < gw - 1 and g[i + 1] > 0) or (y > 0 and g[i - gw] > 0) or (y < gh - 1 and g[i + gw] > 0)) then o[i] = 7 end end end for i, v in pairs(o) do g[i] = v end local rows = {} for y = 0, gh - 1 do rows[y + 1] = string.char(table.unpack(g, y * gw + 1, y * gw + gw)) end local key = ty .. k GRID[key], GW[key], GH[key] = table.concat(rows), gw, gh end for _, ty in ipairs({ "R", "N", "B", "Q", "K" }) do for _, k in ipairs({ 0, 7 }) do queue[#queue + 1] = { ty, k } end end queue[#queue + 1] = { "P", 1 }; queue[#queue + 1] = { "P", 6 } local PF, PR = {}, {} local BN, BK = {}, {} for r = 1, 8 do BK[r] = {} end local boot = 0 local mover, moverOff = nil, 0 local function draw_piece(i, f, r, dx, lift) local p = INIT[i] local k = floor(r + 0.5) if k < 0 then k = 0 elseif k > 7 then k = 7 end local key = p[1] .. k if not GRID[key] then bake(p[1], k) end local x, y = px_of(f, r) x = x + dx if x < -8 or x > 72 then return end local gw, gh = GW[key], GH[key] local hs = floor(3 * sc_of(k) + 0.5) blend_rect(floor(x + 0.5) - hs, floor(y + 0.5), 2 * hs + 1, 1, 0, 0, 0, 0.35) blit(GRID[key], p[2] and PALW or PALB, floor(x + 0.5) - gw // 2, floor(y + 0.5) - (gh - 3) - lift, gw, gh, gw, gh, true) end function render() if not PALW then on_settings() end boot = boot + 1 for _ = 1, 3 do local j = queue[qn] if j then bake(j[1], j[2]); qn = qn + 1 end end if BGSTAGE == 0 then rect(0, 0, W, H, 44, 34, 32) board_rows(6, 34); BGSTAGE = 1 elseif BGSTAGE == 1 then board_rows(35, 63) BG = gfx.snapshot(0, 0, W, H); BGSTAGE = 2 end if boot < 8 or not BG then gfx.ready(false) return end local fr = (frame + 150) % CYC blit_rgb(BG, 0, 0, W, H) local last = #MOVES local mate0 = ST[last] + DU[last] if fr >= mate0 and fr < mate0 + 50 then local a = 0.5 * abs(sin((fr - mate0) * 0.2)) local kf, kr = INIT[KING][3], INIT[KING][4] for y = floor(Yf(kr)) + 1, floor(Yf(kr + 1)) do local hw = HW(y) blend_rect(floor(32 + hw * (kf - 4) / 4 + 0.5), y, floor(hw / 4 + 0.5), 1, 255, 40, 40, a) end end for i = 1, 32 do PF[i], PR[i] = INIT[i][3], INIT[i][4] end local mi, mf, mr, mlift, capi, capdx = nil, 0, 0, 0, nil, 0 for k = 1, last do local m = MOVES[k] if fr >= ST[k] then local u = (fr - ST[k]) / DU[k] if u > 1 then u = 1 end PF[m[1]], PR[m[1]] = m[2] + (m[4] - m[2]) * u, m[3] + (m[5] - m[3]) * u if u < 1 then mi, mf, mr = m[1], PF[m[1]], PR[m[1]] mlift = INIT[m[1]][1] == "N" and floor(6 * sin(3.1416 * u) + 0.5) or floor(2 * sin(3.1416 * u) + 0.5) end if m[6] and u >= 1 then capi, capdx = m[6], (fr - ST[k] - DU[k]) * 2 end end end for r = 1, 8 do BN[r] = 0 end for i = 1, 32 do if i ~= mi and i ~= capi then local r = floor(PR[i] + 0.5) + 1 if r < 1 then r = 1 elseif r > 8 then r = 8 end BN[r] = BN[r] + 1; BK[r][BN[r]] = i end end for r = 1, 8 do for n = 1, BN[r] do local i = BK[r][n]; draw_piece(i, PF[i], PR[i], 0, 0) end end if capi then draw_piece(capi, PF[capi], PR[capi], capdx, 0) end if mi then draw_piece(mi, mf, mr, 0, mlift) end local a = 0 if fr < 36 then a = 1 - fr / 36 elseif fr > CYC - 18 then a = (fr - (CYC - 18)) / 18 end if boot < 20 then a = a > 1 - (boot - 8) / 12 and a or 1 - (boot - 8) / 12 end if a > 0.02 then blend_rect(0, 0, W, H, 0, 0, 0, a) end end