info = { type = "image", title = "Bowling lane", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A bowling ball rolls down the lane for a strike", api = 44, } local tr = ({ sk = { ["Ball colour"] = "Farba gule" }, cs = { ["Ball colour"] = "Barva koule" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "ball", label = T("Ball colour"), type = "color", default = "#2E62E6" }, } local floor, ceil, sqrt, sin, cos, exp, abs = math.floor, math.ceil, math.sqrt, math.sin, math.cos, math.exp, math.abs local hash32, schar, unpack, concat = math.hash32, string.char, table.unpack, table.concat local blit, blend_rect = gfx.blit_rgb, gfx.blend_rect local W, H = gfx.W, gfx.H local YH, M, KH = 11.2, 51.8, 28 local C, TROLL, OFF = 190, 66, 16 local YPIT = 25 local BOARD = {} for i = 0, 38 do BOARD[i] = (hash32(i * 7919 + 13) & 255) / 255 end local function bg_px(x, y) local fx, fy = x + 0.5, y + 0.5 if fy < 13 then if fy > 8 and fy < 10 and (x % 9) < 2 then return 255, 244, 200 end return 16, 20, 44 elseif fy < 21 then local s = sin(fx * 0.33 + fy * 0.55) + sin(fx * 0.17 - fy * 0.4) if fy < 14 then return 70, 86, 170 end if s > 1.2 then return 244, 146, 40 end if s > 0.85 then return 70, 112, 214 end return 32, 54, 140 elseif fy < YPIT - 0.4 then return 22, 16, 20 end local zp = M / (fy - YH) local u = (fx - 32) * zp / KH local au = abs(u) if au < 1 then local bi = floor((u + 1) * 19.5) local sheen = exp(-u * u / 0.5) * (0.55 + 0.45 * sin(zp * 3.1 + 0.7)) local k = (0.78 + 0.2 * sheen + 0.06 * (BOARD[bi] - 0.5)) * (1 - 0.1 * (zp - 1) / 2.9) if zp < 2.4 and ((u + 1) * 19.5 - bi) < 0.14 then k = k * 0.9 end if au > 0.88 then k = k * (1 - (au - 0.88) * 1.2) end if zp > 1.95 and zp < 2.35 then local hw = 0.13 * (2.35 - zp) / 0.4 for a = -2, 2 do if abs(u - a * 0.33) < hw then k = k * 0.64 end end end if zp > 1.02 and zp < 1.07 then k = k * 0.6 end return 226 * k, 170 * k, 102 * k elseif au < 1.3 then local t = (au - 1) / 0.3 local k = 0.3 + 0.7 * (2 * t - 1) ^ 2 return 30 + 60 * k, 32 + 62 * k, 44 + 68 * k end local s = (zp * 2.6) % 1 local k = 1 - 0.18 * (zp - 1) / 2.9 if s < 0.14 then return 92 * k, 122 * k, 218 * k end return 34 * k, 44 * k, 96 * k end local ROWS, BG, BUILT = {}, nil, 0 local function build_chunk() local y0 = BUILT local part = {} for y = y0, y0 + 7 do local row, n = {}, 0 for x = 0, W - 1 do local r, g, b = bg_px(x, y) row[n + 1], row[n + 2], row[n + 3] = floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) n = n + 3 end part[#part + 1] = schar(unpack(row, 1, n)) end local s = concat(part) ROWS[#ROWS + 1] = s blit(s, 0, y0, W, 8) BUILT = y0 + 8 if BUILT >= H then BG = concat(ROWS) end end local PIN = { ".wg.", ".wg.", ".rr.", "wwwg", "wwwg", ".wwg" } local PCOL = { w = { 240, 240, 246 }, g = { 168, 172, 196 }, r = { 214, 40, 48 } } local KEY = { 1, 254, 1 } local POSES = {} local function pose(p) local s = POSES[p] if s then return s end local a = p * 0.7854 local ca, sa = cos(a), sin(a) local rows = {} for j = 0, 7 do local row, n = {}, 0 for i = 0, 7 do local ox, oy = i + 0.5 - 4, j + 0.5 - 4 local sx, sy = floor(ox * ca + oy * sa + 2), floor(-ox * sa + oy * ca + 3) local c = KEY if sx >= 0 and sx < 4 and sy >= 0 and sy < 6 then local ch = PIN[sy + 1]:sub(sx + 1, sx + 1) if ch ~= "." then c = PCOL[ch] end end row[n + 1], row[n + 2], row[n + 3] = c[1], c[2], c[3] n = n + 3 end rows[j + 1] = schar(unpack(row, 1, n)) end s = concat(rows) POSES[p] = s return s end local PXC, PYB, PROW = {}, {}, {} do local i = 0 for r = 0, 3 do local zr = 3.3 + 0.15 * r for k = 0, r do i = i + 1 PXC[i], PYB[i], PROW[i] = 32 + (k - r / 2) * 16.1 / zr, YH + M / zr, r end end end local FALL, D, VX, V0, OM, LOW = {}, {}, {}, {}, {}, {} local CA, CB, CUR = -0.5, 0.06, -1 local GRAV = 0.06 local function setup(n) local function h(a) return (hash32(n * 131 + a * 0x9E3779B1) & 0x7FFF) / 32768 end CB, CA = 0.02 + 0.1 * h(1), -0.62 + 0.3 * h(2) local bxs = 32 + CB * KH / 3.3 for i = 1, 10 do FALL[i] = true end if h(3) < 0.4 then FALL[7 + floor(h(5) * 4)] = false if h(4) < 0.5 then FALL[4 + floor(h(6) * 3)] = false end end for i = 1, 10 do D[i] = 2 * PROW[i] + floor(abs(PXC[i] - bxs) / 3.5) + floor(h(10 + i) * 3) VX[i] = (PXC[i] + (h(30 + i) - 0.5) * 4 > bxs and 1 or -1) * (0.12 + 0.4 * h(20 + i)) V0[i] = 0.7 + 0.5 * h(40 + i) OM[i] = (h(50 + i) < 0.5 and 1 or -1) * (0.08 + 0.1 * h(60 + i)) LOW[i] = 130 + 3 * PROW[i] + floor(h(70 + i) * 8) end end local function pin_state(i, t) local xc, yb = PXC[i], PYB[i] if not FALL[i] or t < TROLL + D[i] then return 0, xc, yb - 3, 0 end if t >= LOW[i] then local s = (t - LOW[i]) / 14 if s >= 1 then return 0, xc, yb - 3, 0 end return 2, xc, yb - 3 - 22 * (1 - s) * (1 - s), 0 end local tau = t - TROLL - D[i] local cy = yb - 3 - (V0[i] * tau - 0.5 * GRAV * tau * tau) local desc = tau > V0[i] / GRAV if desc and cy - 3.5 > YPIT then return 3 end return 1, xc + VX[i] * tau, cy, floor(tau * OM[i] * 1.5) % 8, desc and cy + 4 > YPIT end local function draw_pin(cx, cy, p) blit(pose(p), floor(cx + 0.5) - 4, floor(cy + 0.5) - 4, 8, 8, KEY[1], KEY[2], KEY[3]) end local LX, LY, LZ = -0.38, -0.55, 0.74 local HX, HY, HZ = -0.2, -0.29, 0.93 local HOLES = { { -0.14, -0.3, 0.94 }, { 0.14, -0.32, 0.93 }, { 0.0, -0.58, 0.81 } } local function draw_ball(cx, cy, r, phi, col) local n = ceil(2 * r) + 3 local x0, y0 = floor(cx - n / 2 + 0.5), floor(cy - n / 2 + 0.5) local cp, sp = cos(phi), sin(phi) local rows = {} for j = 0, n - 1 do local row, m = {}, 0 for i = 0, n - 1 do local dx, dy = (x0 + i + 0.5 - cx) / r, (y0 + j + 0.5 - cy) / r local d2 = dx * dx + dy * dy local R, G, B = KEY[1], KEY[2], KEY[3] if d2 <= 1 + 0.5 / r then local nz = d2 < 1 and sqrt(1 - d2) or 0 local ty, tz = dy * cp - nz * sp, dy * sp + nz * cp local w = sin(7 * ty + 2.2 * sin(4 * dx + 1) + 1.5 * tz) local k = 0.62 + 0.3 * (0.5 + 0.5 * w) local cr, cg, cb = col.r * k, col.g * k, col.b * k if w > 0.85 then cr, cg, cb = cr + 70, cg + 70, cb + 70 end for q = 1, 3 do local hh = HOLES[q] local hx, hy, hz = dx - hh[1], ty - hh[2], tz - hh[3] if hx * hx + hy * hy + hz * hz < 0.02 then cr, cg, cb = 8, 8, 12 end end local diff = dx * LX + dy * LY + nz * LZ if diff < 0 then diff = 0 end local sd = dx * HX + dy * HY + nz * HZ local sp8 = 0 if sd > 0 then sd = sd * sd; sd = sd * sd; sd = sd * sd; sp8 = sd * sd * sd * 230 end local lit = 0.16 + 0.84 * diff if d2 > 1 then lit = lit * 0.4 end R, G, B = cr * lit + sp8, cg * lit + sp8, cb * lit + sp8 R, G, B = floor(R > 255 and 255 or R), floor(G > 255 and 255 or G), floor(B > 255 and 255 or B) if R == KEY[1] and G == KEY[2] and B == KEY[3] then G = 253 end end row[m + 1], row[m + 2], row[m + 3] = R, G, B m = m + 3 end rows[j + 1] = schar(unpack(row, 1, m)) end blit(concat(rows), x0, y0, n, n, KEY[1], KEY[2], KEY[3]) end function render() if not BG then build_chunk(); if not BG then gfx.ready(false) end return end blit(BG, 0, 0, W, H) local fr = frame + OFF local n, t = fr // C, fr % C if n ~= CUR then setup(n); CUR = n end local function ball() local col = cfg.ball or { r = 46, g = 98, b = 230 } local zp, ub if t < TROLL then local s = t / TROLL zp, ub = 1.1 + 2.2 * s, CA + (CB - CA) * s ^ 1.6 else zp, ub = 3.3 + (t - TROLL) * 0.06, CB end if zp > 4 then return end local r = floor(7.5 / zp * 2 + 0.5) / 2 local cx, yc = 32 + ub * KH / zp, YH + M / zp local cy = yc - r * 0.98 blend_rect(cx - r * 0.8, yc - 0.5, r * 1.6, 1.5, 10, 6, 4, 0.4) blend_rect(cx - r * 0.5, yc + 1, r, ceil(r * 0.5), col.r, col.g, col.b, 0.2) if t >= TROLL then gfx.clip(0, 0, W, YPIT) end draw_ball(cx, cy, r, 9 * (t / TROLL), col) if t >= TROLL then gfx.clip() end end if t >= TROLL then ball() end for i = 10, 1, -1 do local m, cx, cy = pin_state(i, t) if m == 0 then draw_pin(cx, cy, 0) end end for i = 10, 1, -1 do local m, cx, cy, p, low = pin_state(i, t) if m == 1 then if low then gfx.clip(0, 0, W, YPIT) end draw_pin(cx, cy, p) if low then gfx.clip() end elseif m == 2 then gfx.clip(0, 21, W, H - 21) draw_pin(cx, cy, 0) gfx.clip() end end if t < TROLL then ball() end end