info = { type = "image", title = "Domino roll", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A row of dominoes topples one by one", api = 33, } local tr = ({ sk = { ["Dominoes"] = "Domina", ["Speed"] = "Rýchlosť", ["Ivory"] = "Slonovina", ["Black"] = "Čierne", ["Red"] = "Červené", ["Slow"] = "Pomaly", ["Normal"] = "Bežne", ["Fast"] = "Rýchlo" }, cs = { ["Dominoes"] = "Domina", ["Speed"] = "Rychlost", ["Ivory"] = "Slonovina", ["Black"] = "Černá", ["Red"] = "Červená", ["Slow"] = "Pomalu", ["Normal"] = "Běžně", ["Fast"] = "Rychle" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "color", label = T("Dominoes"), type = "int", default = "ivory", opts = { keys = { "ivory", "black", "red" }, T("Ivory"), T("Black"), T("Red") } }, { key = "speed", label = T("Speed"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, T("Slow"), T("Normal"), T("Fast") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sin, cos, abs, max, min, exp, sqrt = math.floor, math.ceil, math.sin, math.cos, math.abs, math.max, math.min, math.exp, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PHI, EL = 0.698, 0.489 local SPHI, CPHI, SEL, CEL = sin(PHI), cos(PHI), sin(EL), cos(EL) local DUX, DUY = CPHI, -SPHI * SEL local DVX, DVY = -SPHI, -CPHI * SEL local PT, PW, PH = 3.6, 10, 18 local PITCH = 10 local STEPX, STEPY = PITCH * DUX, PITCH * DUY local ANG = 6 local NPOSE = 15 local AF = math.acos(PT / PITCH) * 180 / 3.14159 local HIT = math.asin((PITCH - PT) / PH) * 180 / 3.14159 local N = 14 local LDU, LDV, LDZ = -0.589, -0.158, 0.8 local AMB = 0.38 local COLORS = { ivory = { base = { 236, 228, 208 }, edge = { 100, 90, 72 }, pip = { 38, 34, 40 } }, black = { base = { 70, 72, 84 }, edge = { 128, 134, 156 }, pip = { 232, 232, 226 } }, red = { base = { 198, 40, 46 }, edge = { 100, 16, 20 }, pip = { 244, 232, 204 } }, } local C = COLORS.ivory local TBL = { 22, 24, 32 } local FACES = { { n = { -1, 0, 0 }, p = { { 0, 0, 0 }, { 0, PW, 0 }, { 0, PW, PH }, { 0, 0, PH } } }, { n = { 1, 0, 0 }, p = { { PT, 0, 0 }, { PT, PW, 0 }, { PT, PW, PH }, { PT, 0, PH } } }, { n = { 0, -1, 0 }, p = { { 0, 0, 0 }, { PT, 0, 0 }, { PT, 0, PH }, { 0, 0, PH } } }, { n = { 0, 1, 0 }, p = { { 0, PW, 0 }, { PT, PW, 0 }, { PT, PW, PH }, { 0, PW, PH } } }, { n = { 0, 0, 1 }, p = { { 0, 0, PH }, { PT, 0, PH }, { PT, PW, PH }, { 0, PW, PH } } }, { n = { 0, 0, -1 }, p = { { 0, 0, 0 }, { PT, 0, 0 }, { PT, PW, 0 }, { 0, PW, 0 } } }, } local BODY, REFL, FACE = {}, {}, {} local NEXT = 0 local function clamp255(v) v = floor(v + 0.5); return v < 0 and 0 or (v > 255 and 255 or v) end local function fillpoly(pts, ox, oy, colfn) local ymin, ymax = 1e9, -1e9 for i = 1, 4 do local y = pts[i][2] if y < ymin then ymin = y end if y > ymax then ymax = y end end for y = ceil(ymin - 0.5), ceil(ymax - 0.5) - 1 do local yc = y + 0.5 local xl, xr = 1e9, -1e9 for i = 1, 4 do local a, b = pts[i], pts[i % 4 + 1] local y1, y2 = a[2], b[2] if (y1 <= yc and y2 > yc) or (y2 <= yc and y1 > yc) then local x = a[1] + (yc - y1) * (b[1] - a[1]) / (y2 - y1) if x < xl then xl = x end if x > xr then xr = x end end end local ia, ib = ceil(xl - 0.5), ceil(xr - 0.5) - 1 if ib >= ia then local r, g, b = colfn((yc - ymin) / (ymax - ymin + 0.001), yc) rect(ia + ox, y + oy, ib - ia + 1, 1, r, g, b) end end end local function line(x1, y1, x2, y2, ox, oy, r, g, b) local n = ceil(max(abs(x2 - x1), abs(y2 - y1))) if n < 1 then n = 1 end for k = 0, n do px(floor(x1 + (x2 - x1) * k / n + 0.5) + ox, floor(y1 + (y2 - y1) * k / n + 0.5) + oy, r, g, b) end end local function build_pose(p, pass) local a = p * ANG * 3.14159 / 180 local c, s = cos(a), sin(a) local hx, hy = PT * DUX, PT * DUY do local flip = pass == 2 local function V(u, v, z) local ru = u - PT local u2 = PT + ru * c + z * s local z2 = -ru * s + z * c if flip then z2 = -z2 end return u2 * DUX + v * DVX - hx, u2 * DUY + v * DVY - z2 * CEL - hy end local quads = {} local minx, miny, maxx, maxy = 1e9, 1e9, -1e9, -1e9 for f = 1, 6 do local F = FACES[f] local nu = F.n[1] * c + F.n[3] * s local nz = -F.n[1] * s + F.n[3] * c if flip then nz = -nz end local nv = F.n[2] local pts = {} for i = 1, 4 do local q = F.p[i] local x, y = V(q[1], q[2], q[3]) pts[i] = { x, y } if x < minx then minx = x end if x > maxx then maxx = x end if y < miny then miny = y end if y > maxy then maxy = y end end if -nu * SPHI * CEL - nv * CPHI * CEL + nz * SEL > 0.02 then local d = nu * LDU + nv * LDV + nz * LDZ quads[#quads + 1] = { pts = pts, shade = AMB + (1 - AMB) * (d > 0 and d or 0), top = f == 5 } end end local ox, oy = -floor(minx) + 1, -floor(miny) + 1 local cw, ch = ceil(maxx) - floor(minx) + 3, ceil(maxy) - floor(miny) + 3 rect(0, 0, cw, ch, 255, 0, 255) local B = C.base for k = 1, #quads do local qd = quads[k] local sh = qd.shade fillpoly(qd.pts, ox, oy, function(g, yc) local f = sh * (qd.top and 1.0 or (1 - 0.30 * g)) if flip then local depth = yc / (PH * CEL + 3) if depth > 1 then depth = 1 elseif depth < 0 then depth = 0 end local kk = 0.14 * (1 - 0.9 * depth) return clamp255(TBL[1] + (B[1] * f - TBL[1]) * kk), clamp255(TBL[2] + (B[2] * f - TBL[2]) * kk), clamp255(TBL[3] + (B[3] * f - TBL[3]) * kk) end return clamp255(B[1] * f), clamp255(B[2] * f), clamp255(B[3] * f) end) end if not flip then local E = C.edge for k = 1, #quads do local qd = quads[k] local m = 0.6 + 0.4 * qd.shade local er, eg, eb = clamp255(E[1] * m), clamp255(E[2] * m), clamp255(E[3] * m) for i = 1, 4 do local q1, q2 = qd.pts[i], qd.pts[i % 4 + 1] line(q1[1], q1[2], q2[1], q2[2], ox, oy, er, eg, eb) end end local x1, y1 = V(0, PW * 0.12, PH * 0.5) local x2, y2 = V(0, PW * 0.88, PH * 0.5) line(x1, y1, x2, y2, ox, oy, E[1], E[2], E[3]) local o1, o2 = V(0, 0, 0) local a1, a2 = V(0, PW, 0) local b1, b2 = V(0, 0, PH) FACE[p] = { o1, o2, a1 - o1, a2 - o2, b1 - o1, b2 - o2 } end local t = { gfx.snapshot(0, 0, cw, ch), cw, ch, -ox, -oy } if flip then REFL[p] = t else BODY[p] = t end end end local BG = nil local BOKEH = { { 6, 9, 4, 255, 196, 130 }, { 27, 16, 5, 160, 190, 255 }, { 45, 7, 3, 255, 170, 110 }, { 60, 21, 4, 200, 220, 255 }, { 78, 12, 5, 255, 210, 150 }, { 90, 24, 3, 150, 180, 255 }, } local function build_bg() for y = 0, H - 1 do local v = 8 + 24 * exp(-((y - 40) / 13) ^ 2) rect(0, y, W, 1, floor(v * 0.9), floor(v * 0.96), floor(v * 1.3)) end BG = gfx.snapshot(0, 0, W, H) end local TSTEP, FALL, T0 = 0.2, 0.46, 1.3 local CYCLE, TEND = 8, 4 function on_settings() C = COLORS[cfg.color or "ivory"] or COLORS.ivory TSTEP = ({ slow = 0.30, normal = 0.20, fast = 0.13 })[cfg.speed or "normal"] or 0.2 FALL = TSTEP / math.sqrt(HIT / AF) TEND = T0 + (N - 1) * TSTEP + FALL CYCLE = TEND + 1.2 + 0.9 + 0.3 BODY, REFL, FACE, BG, NEXT = {}, {}, {}, nil, 0 end local function smooth(u) u = u < 0 and 0 or (u > 1 and 1 or u); return u * u * (3 - 2 * u) end local PIPS = { { { 0.5, 0.5 } }, { { 0.27, 0.8 }, { 0.73, 0.2 } }, { { 0.27, 0.8 }, { 0.5, 0.5 }, { 0.73, 0.2 } }, { { 0.27, 0.8 }, { 0.73, 0.8 }, { 0.27, 0.2 }, { 0.73, 0.2 } }, { { 0.27, 0.8 }, { 0.73, 0.8 }, { 0.5, 0.5 }, { 0.27, 0.2 }, { 0.73, 0.2 } }, { { 0.27, 0.8 }, { 0.73, 0.8 }, { 0.27, 0.5 }, { 0.73, 0.5 }, { 0.27, 0.2 }, { 0.73, 0.2 } }, } local function pips(f, X, Y, n, zoff, r, g, b) local set = PIPS[n] if not set then return end for k = 1, #set do local vf, zf = set[k][1], zoff + set[k][2] * 0.45 + 0.025 px(floor(X + f[1] + vf * f[3] + zf * f[5] + 0.5), floor(Y + f[2] + vf * f[4] + zf * f[6] + 0.5), r, g, b) end end function render() if not BG then build_bg() elseif not REFL[NPOSE - 1] then build_pose(NEXT // 2, NEXT % 2 + 1) NEXT = NEXT + 1 end gfx.blit_rgb(BG, 0, 0, W, H) local t = (frame / FPS) % CYCLE local u = (t - (T0 - 0.4)) / (TEND - T0 + 0.8) local cam = -0.4 + (N - 1 - 1.8 + 0.4) * smooth(u) local camx = cam * STEPX for i = 1, #BOKEH do local d = BOKEH[i] local x = (d[1] - camx * 0.2) % 96 - 16 local r = d[3] for dy = -r, r do local hw = floor(sqrt(r * r + 0.5 - dy * dy)) blend_rect(floor(x) - hw, d[2] + dy, 2 * hw + 1, 1, d[4], d[5], d[6], 0.17) end end if not REFL[NPOSE - 1] then return end local x0, y0 = 20, 50 local i0, i1 = max(0, floor(cam) - 4), min(N - 1, ceil(cam) + 9) local PA = {} for i = i0, i1 do local tau = t - (T0 + i * TSTEP) local af = i == N - 1 and 86 or AF local al = 0 if tau > 0 then local s = tau / FALL al = s >= 1 and af or af * s * s end PA[i] = floor(al / ANG + 0.5) end for i = i1, i0, -1 do local R = REFL[PA[i]] local X, Y = x0 + (i - cam) * STEPX, y0 + (i - cam) * STEPY gfx.blit_rgb(R[1], floor(X + 0.5) + R[4], floor(Y + 0.5) + R[5], R[2], R[3], 255, 0, 255) end local pr, pg, pb = C.pip[1], C.pip[2], C.pip[3] for i = i1, i0, -1 do local p = PA[i] local Bd = BODY[p] local X, Y = x0 + (i - cam) * STEPX, y0 + (i - cam) * STEPY local ix, iy = floor(X + 0.5), floor(Y + 0.5) gfx.blit_rgb(Bd[1], ix + Bd[4], iy + Bd[5], Bd[2], Bd[3], 255, 0, 255) if p <= 10 then local f = FACE[p] pips(f, ix, iy, (i * 3 + 1) % 7, 0.5, pr, pg, pb) pips(f, ix, iy, (i * 5 + 2) % 7, 0.0, pr, pg, pb) end end local fa = 0 if t < 0.9 then fa = 1 - t / 0.9 elseif t > TEND + 1.2 then fa = min(1, (t - TEND - 1.2) / 0.9) end if fa > 0 then blend_rect(0, 0, W, H, 0, 0, 0, fa) end end