info = { type = "image", title = "Newton's cradle", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Chrome balls click back and forth in a cradle", api = 44, } local tr = ({ sk = { ["Background"] = "Pozadie", ["Dark"] = "Tmavé", ["Light"] = "Svetlé" }, cs = { ["Background"] = "Pozadí", ["Dark"] = "Tmavé", ["Light"] = "Světlé" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bg", label = T("Background"), type = "int", default = "dark", opts = { keys = { "dark", "light" }, T("Dark"), T("Light") } }, } local rect, blend_rect, blit = gfx.rect, gfx.blend_rect, gfx.blit_indexed local floor, sin, cos, sqrt, abs, max, min = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.max, math.min local W, H = gfx.W, gfx.H local PI = 3.14159 local L, PIV_Y, R = 38, 6, 4.0 local XS = { 16, 24, 32, 40, 48 } local PLATE_Y = 51 local DEG = PI / 180 local R0, LF, HOLD, Q = 60, 24, 8, 18 local DAMP, AMIN = 0.86, 0.8 * DEG local RUNS = { { n = 1, a0 = 17 * DEG }, { n = 2, a0 = 16 * DEG } } local CYC = 0 for _, r in ipairs(RUNS) do local k = 0 while r.a0 * DAMP ^ (k + 1) >= AMIN do k = k + 1 end r.k = k + 1 r.len = R0 + LF + HOLD + Q + r.k * 2 * Q CYC = CYC + r.len end local DARK = true local CACHE = {} local PAL = { 0, 0, 0 } for i = 1, 64 do local g = (i - 1) * 4 + 2 PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = g * 0.95, g, min(255, g * 1.06 + 4) end local BGNOM = 24 local BG = nil function on_settings() DARK = (cfg.bg or "dark") ~= "light" BGNOM = DARK and 24 or 196 CACHE, BG = {}, nil end local function shade(nx, ny, nz) local rx, ry = 2 * nz * nx, 2 * nz * ny local g if DARK then if ry > 0.12 then g = 95 + 100 * ry ^ 0.8 elseif ry > -0.12 then g = 205 - 60 * abs(ry) / 0.12 * 0.5 else g = 70 + 56 * (1 + ry) end else if ry > 0.12 then g = 214 + 36 * ry elseif ry > -0.12 then g = 255 else g = 70 + 70 * (1 + ry) end end local dx, dy = nx + 0.38, ny - 0.45 local hl = max(0, 1 - sqrt(dx * dx + dy * dy) / 0.3) g = g + 170 * hl * hl local edge = min(1, nz * 1.7) g = g * ((DARK and 0.5 or 0.45) + (DARK and 0.5 or 0.55) * edge) if abs(nx) > 0.62 then g = g * 0.72 end return min(255, max(0, g)) end local function sprite(fx, fy) local key = fx * 4 + fy local s = CACHE[key] if s then return s end local cx, cy = 5 + fx / 4, 5 + fy / 4 local rows, row = {}, {} for j = 0, 9 do for i = 0, 9 do local dx, dy = i + 0.5 - cx, j + 0.5 - cy local d = sqrt(dx * dx + dy * dy) local cov = min(1, max(0, R - d + 0.5)) local cell = 0 if cov > 0.15 then local nx, ny = dx / R, -dy / R local q = nx * nx + ny * ny if q > 0.97 then local k = sqrt(0.97 / q); nx, ny, q = nx * k, ny * k, 0.97 end local g = shade(nx, ny, sqrt(1 - q)) g = g * cov + BGNOM * (1 - cov) cell = min(64, floor(g / 4) + 1) end row[i + 1] = cell end rows[j + 1] = string.char(table.unpack(row, 1, 10)) end s = table.concat(rows) CACHE[key] = s return s end local function build_bg() if DARK then rect(0, 0, W, H, 24, 26, 32) else for y = 0, PLATE_Y - 1 do local t = y / PLATE_Y rect(0, y, W, 1, 216 - 40 * t, 220 - 40 * t, 228 - 38 * t) end end rect(0, PLATE_Y, W, 5, 44, 46, 56) rect(0, PLATE_Y, W, 1, 104, 110, 124) rect(0, PLATE_Y + 5, W, H - PLATE_Y - 5, 22, 24, 30) rect(0, PLATE_Y + 5, W, 1, 62, 66, 78) local S = DARK and { 214, 222, 232 } or { 150, 156, 168 } local M = DARK and { 140, 148, 160 } or { 100, 106, 118 } local D = DARK and { 66, 72, 84 } or { 64, 68, 78 } rect(0, 2, W, 1, S[1], S[2], S[3]); rect(0, 3, W, 2, M[1], M[2], M[3]); rect(0, 5, W, 1, D[1], D[2], D[3]) rect(0, 2, 1, PLATE_Y - 2, S[1], S[2], S[3]); rect(W - 1, 2, 1, PLATE_Y - 2, D[1], D[2], D[3]) for k = 1, #XS do rect(XS[k] - 1, PIV_Y - 1, 2, 2, D[1], D[2], D[3]) end BG = gfx.snapshot(0, 0, W, H) end local ANG = { 0, 0, 0, 0, 0 } local function smooth(s) return s * s * (3 - 2 * s) end local function angles(f) for i = 1, 5 do ANG[i] = 0 end local t = f % CYC local run = RUNS[1] if t >= run.len then t = t - run.len; run = RUNS[2] end local n, a0 = run.n, run.a0 t = t - R0 if t < 0 then return end local left, right = n, 6 - n local th if t < LF then th = -a0 * smooth(t / LF) else t = t - LF if t < HOLD then th = -a0 else t = t - HOLD if t < Q then th = -a0 * cos(PI / 2 * t / Q) else t = t - Q local k = t // (2 * Q) + 1 if k > run.k then return end local tau = t - (k - 1) * 2 * Q local a = a0 * DAMP ^ k * sin(PI / 2 * tau / Q) if k % 2 == 1 then for i = right, 5 do ANG[i] = a end else for i = 1, left do ANG[i] = -a end end return end end end for i = 1, left do ANG[i] = th end end local STR = { 176, 182, 194 } local function string_line(xp, bx, by) local sr, sg, sb, al = STR[1], STR[2], STR[3], 0.45 if not DARK then sr, sg, sb = 90, 94, 106 end if abs(bx - xp) < 0.05 then blend_rect(xp - 1, PIV_Y, 1, by - PIV_Y, sr, sg, sb, al * 0.5) blend_rect(xp, PIV_Y, 1, by - PIV_Y, sr, sg, sb, al * 0.5) return end local y = PIV_Y local ylast = by while y < ylast do local h = min(4, ylast - y) local x = xp + (bx - xp) * ((y + h / 2 - PIV_Y) / (by - PIV_Y)) local p = floor(x - 0.5) local w = p + 1 - (x - 0.5) blend_rect(p, floor(y), 1, floor(h + 0.99), sr, sg, sb, al * w) blend_rect(p + 1, floor(y), 1, floor(h + 0.99), sr, sg, sb, al * (1 - w)) y = y + h end end function render() if not BG then if not cfg then gfx.ready(false) return end build_bg() gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) angles(frame + 100) local ref = DARK and 0.22 or 0.28 for i = 1, 5 do local th = ANG[i] local bx, by = XS[i] + L * sin(th), PIV_Y + L * cos(th) string_line(XS[i], bx, by) end for i = 1, 5 do local th = ANG[i] local bx, by = XS[i] + L * sin(th), PIV_Y + L * cos(th) local ix, iy = floor(bx), floor(by) local fx, fy = floor((bx - ix) * 4), floor((by - iy) * 4) local lx = floor(bx + 0.5) if DARK then blend_rect(lx - 3, PLATE_Y + 1, 6, 1, 150, 158, 172, ref * 0.8) blend_rect(lx - 2, PLATE_Y + 2, 4, 1, 150, 158, 172, ref * 0.5) else blend_rect(lx - 3, PLATE_Y + 1, 6, 1, 10, 12, 18, 0.5) blend_rect(lx - 2, PLATE_Y + 2, 4, 1, 10, 12, 18, 0.35) end blit(sprite(fx, fy), PAL, ix - 5, iy - 5, 10, 10, 10, 10, true) end end