info = { type = "image", title = "Pinball table", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Pinball ball bounces off bumpers and flippers", api = 33, } local tr = ({ sk = { ["Table"] = "Stôl", ["Navy"] = "Námornícka", ["Teal"] = "Tyrkysová", ["Plum"] = "Slivková", ["Green"] = "Zelená", ["Speed"] = "Rýchlosť" }, cs = { ["Table"] = "Stůl", ["Navy"] = "Námořnická", ["Teal"] = "Tyrkysová", ["Plum"] = "Švestková", ["Green"] = "Zelená", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tbl", label = T("Table"), type = "int", default = 0, opts = { T("Navy"), T("Teal"), T("Plum"), T("Green") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 3, default = 2 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, ceil, sqrt, sin, cos = math.floor, math.ceil, math.sqrt, math.sin, math.cos local hash32 = math.hash32 local W, H = gfx.W, gfx.H local TABLES = { { { 24, 44, 124 }, { 46, 76, 176 } }, { { 14, 86, 104 }, { 30, 134, 152 } }, { { 74, 32, 112 }, { 114, 60, 160 } }, { { 20, 92, 56 }, { 38, 136, 84 } }, } local WALL, WALL_HI, WALL_LO = { 132, 142, 176 }, { 204, 212, 236 }, { 82, 90, 124 } local DRAIN, LANE = { 12, 14, 34 }, { 16, 22, 70 } local ORANGE, ORANGE_HI = { 255, 140, 32 }, { 255, 206, 120 } local AZURE, AZURE_HI = { 52, 198, 238 }, { 214, 250, 255 } local FLASH = { 255, 240, 200 } local MIR = 58 local BUMP = { { 19, 19, 4 }, { 39, 19, 4 }, { 29, 31, 4 } } local BR = 1.5 local SEG = { { 3, 3, 55, 3, 0, 0.6 }, { 55, 3, 61, 3, 0, 0.6 }, { 3, 3, 3, 64, 0, 0.6 }, { 3, 12, 12, 3, 0, 0.7 }, { 51, 3, 61, 13, 0, 0.95 }, { 61, 0, 61, 64, 0, 0.5 }, { 56, 17, 56, 64, 1, 0.5 }, { 57, 62.5, 61, 62.5, 0, 0.2 }, { 3, 38, 15, 50, 0, 0.8 }, { 55, 38, 43, 50, 0, 0.8 }, { 15, 50, 15, 64, 0, 0.5 }, { 43, 50, 43, 64, 0, 0.5 }, } local PIV, FLEN, FT = { 16, 52 }, 10, 1.25 local ANG = { 0.5236, 0, -0.4363 } local function seg_dist(x, y, ax, ay, bx, by) local dx, dy = bx - ax, by - ay local u = ((x - ax) * dx + (y - ay) * dy) / (dx * dx + dy * dy) if u < 0 then u = 0 elseif u > 1 then u = 1 end local qx, qy = ax + u * dx, ay + u * dy return sqrt((x - qx) ^ 2 + (y - qy) ^ 2), qx, qy end local FSEG, FRUN = { {}, {} }, { {}, {} } for pose = 0, 2 do local tx, ty = PIV[1] + FLEN * cos(ANG[pose + 1]), PIV[2] + FLEN * sin(ANG[pose + 1]) FSEG[1][pose] = { PIV[1], PIV[2], tx, ty } FSEG[2][pose] = { MIR - PIV[1], PIV[2], MIR - tx, ty } local runs, mr = {}, {} for y = floor(math.min(PIV[2], ty)) - 2, ceil(math.max(PIV[2], ty)) + 2 do local x0, x1 for x = PIV[1] - 3, ceil(tx) + 2 do if seg_dist(x + 0.5, y + 0.5, PIV[1], PIV[2], tx, ty) <= FT - 0.05 then x0 = x0 or x; x1 = x end end if x0 then runs[#runs + 1] = { x0, y, x1 - x0 + 1 } mr[#mr + 1] = { MIR - x0 - (x1 - x0 + 1), y, x1 - x0 + 1 } end end FRUN[1][pose], FRUN[2][pose] = runs, mr end local function disc(cx, cy, r, c) for y = floor(cy - r), ceil(cy + r) do local dy = y + 0.5 - cy local q = r * r - dy * dy if q > 0 then local hw = sqrt(q) local x0, x1 = ceil(cx - hw - 0.5), floor(cx + hw - 0.5) if x1 >= x0 then rect(x0, y, x1 - x0 + 1, 1, c[1], c[2], c[3]) end end end end local BG, BG_KEY = nil, nil local LAMPS = { { 17, 8 }, { 23, 8 }, { 29, 8 }, { 35, 8 }, { 41, 8 } } local ARROWS = { { 8, 33 }, { 47, 33 } } local function build_bg(P) local F, RING = P[1], P[2] rect(0, 0, W, H, WALL[1], WALL[2], WALL[3]) rect(3, 3, 58, 14, F[1], F[2], F[3]) rect(3, 17, 52, 47, F[1], F[2], F[3]) rect(57, 17, 4, 47, LANE[1], LANE[2], LANE[3]) for y = 3, 11 do rect(3, y, 12 - y, 1, WALL[1], WALL[2], WALL[3]) end for y = 3, 12 do rect(y + 48, y, 13 - y, 1, WALL[1], WALL[2], WALL[3]) end rect(55, 17, 2, 47, WALL[1], WALL[2], WALL[3]) for y = 38, 63 do local w = y < 51 and y - 37 or 13 rect(3, y, w, 1, WALL[1], WALL[2], WALL[3]) rect(MIR - 3 - w, y, w, 1, WALL[1], WALL[2], WALL[3]) end rect(16, 58, 26, 6, DRAIN[1], DRAIN[2], DRAIN[3]) rect(2, 3, 1, 61, WALL_HI[1], WALL_HI[2], WALL_HI[3]) rect(3, 2, 58, 1, WALL_HI[1], WALL_HI[2], WALL_HI[3]) rect(55, 17, 1, 46, WALL_HI[1], WALL_HI[2], WALL_HI[3]) rect(61, 13, 1, 51, WALL_LO[1], WALL_LO[2], WALL_LO[3]) rect(57, 62, 4, 2, WALL_LO[1], WALL_LO[2], WALL_LO[3]) for _, b in ipairs(BUMP) do disc(b[1], b[2], 6.6, RING) disc(b[1], b[2], 5.6, F) disc(b[1], b[2], b[3] + 0.4, ORANGE) disc(b[1], b[2], b[3] - 1.1, AZURE) px(b[1] - 1, b[2] - 1, AZURE_HI[1], AZURE_HI[2], AZURE_HI[3]) rect(b[1] - 3, b[2] - 4, 2, 1, ORANGE_HI[1], ORANGE_HI[2], ORANGE_HI[3]) end for _, l in ipairs(LAMPS) do rect(l[1], l[2], 2, 2, RING[1], RING[2], RING[3]) end for _, a in ipairs(ARROWS) do rect(a[1], a[2], 3, 1, 120, 70, 30); rect(a[1] + 1, a[2] + 1, 1, 1, 120, 70, 30) end rect(28, 45, 2, 6, RING[1], RING[2], RING[3]) rect(26, 47, 6, 2, RING[1], RING[2], RING[3]) BG = gfx.snapshot(0, 0, W, H) end local G, SUB, VMAX = 0.05, 3, 3.4 local bx, by, vx, vy = 59, 57, 0, 0 local mode, timer, nball, age, still, simf = 1, 6, 0, 0, 0, 0 local fpose, fseq, frise, fsave = { 0, 0 }, { 0, 0 }, { false, false }, { true, true } local flash, lamp, forced = { 0, 0, 0 }, 0, false local TRAIL = { { 59, 57 }, { 59, 57 } } local FIRE = { 1, 2, 2, 2, 2, 2, 2, 1, 1, 0 } local function rnd(n) return (hash32(n * 0x9E3779B1) & 0x7FFF) / 32768 end local function reset_sim() bx, by, vx, vy = 59, 57, 0, 0 mode, timer, nball, age, still, simf = 1, 6, 0, 0, 0, 0 fpose[1], fpose[2], fseq[1], fseq[2] = 0, 0, 0, 0 flash[1], flash[2], flash[3], lamp, forced = 0, 0, 0, 0, false fsave[1], fsave[2] = rnd(1) < 0.85, rnd(2) < 0.85 end local function hit(ax, ay, cx, cy, rr, e) local d, qx, qy = seg_dist(bx, by, ax, ay, cx, cy) if d >= rr then return end local nx, ny = bx - qx, by - qy if d < 1e-4 then nx, ny, d = 0, -1, 1 end nx, ny = nx / d, ny / d bx, by = bx + nx * (rr - d), by + ny * (rr - d) local vn = vx * nx + vy * ny if vn < 0 then vx, vy = vx - (1 + e) * vn * nx, vy - (1 + e) * vn * ny end return nx, ny end local function sub_step(dt) vy = vy + G * dt bx, by = bx + vx * dt, by + vy * dt for i = 1, #SEG do local s = SEG[i] hit(s[1], s[2], s[3], s[4], BR + s[5], s[6]) end for i = 1, 3 do local b = BUMP[i] local dx, dy = bx - b[1], by - b[2] local d = sqrt(dx * dx + dy * dy) if d < b[3] + BR then local nx, ny = dx / d, dy / d bx, by = b[1] + nx * (b[3] + BR), b[2] + ny * (b[3] + BR) local vn = vx * nx + vy * ny if vn < 0 then vx, vy = vx - 2 * vn * nx, vy - 2 * vn * ny end vn = vx * nx + vy * ny if vn < 1.9 then vx, vy = vx + (1.9 - vn) * nx, vy + (1.9 - vn) * ny end flash[i], lamp = 7, 14 end end for s = 1, 2 do local f = FSEG[s][fpose[s]] local nx, ny = hit(f[1], f[2], f[3], f[4], BR + FT, 0.35) if nx and frise[s] then local vn = vx * nx + vy * ny if vn < 3 then vx, vy = vx + (3 - vn) * nx, vy + (3 - vn) * ny end end end local sp = sqrt(vx * vx + vy * vy) if sp > VMAX then vx, vy = vx * VMAX / sp, vy * VMAX / sp end end local function sim_frame() local spd = ({ 0.8, 1.0, 1.3 })[cfg.speed or 2] or 1 for s = 1, 2 do frise[s] = false if fseq[s] > 0 then local np = FIRE[fseq[s]] frise[s] = np > fpose[s] fpose[s] = np fseq[s] = fseq[s] < #FIRE and fseq[s] + 1 or 0 end end lamp = lamp > 0 and lamp - 1 or 0 for i = 1, 3 do if flash[i] > 0 then flash[i] = flash[i] - 1 end end TRAIL[2], TRAIL[1] = TRAIL[1], { bx, by } if mode == 1 then timer = timer - 1 if timer <= 0 then mode, timer = 2, 0 end elseif mode == 2 then timer = timer + 1 by = 57 + 3 * (timer < 16 and timer / 16 or 1) if timer >= 19 then mode, age, still, forced = 3, 0, 0, false vx, vy = 0, -(2.7 + 0.45 * rnd(nball + 11)) fsave[1], fsave[2] = rnd(nball * 2 + 3) < 0.82, rnd(nball * 2 + 4) < 0.82 end else age = age + 1 if age > 1100 then forced = true end for s = 1, 2 do if fseq[s] == 0 and fsave[s] and not forced and vy > -0.3 then local f = FSEG[s][0] if seg_dist(bx, by, f[1], f[2], f[3], f[4]) < 5.2 then fseq[s] = 1 end end end local dt = spd / SUB for _ = 1, SUB do sub_step(dt) end if vx * vx + vy * vy < 0.004 then still = still + 1 else still = 0 end if still > 40 then vx, vy, still = 1.1 * (rnd(age) < 0.5 and -1 or 1), -1, 0 end if bx > 57.5 and by > 57 and age > 12 then mode, timer = 2, 0; bx, by, vx, vy = 59, 57, 0, 0 elseif by > 67 then nball = nball + 1 mode, timer, bx, by, vx, vy = 1, 34 + floor(rnd(nball) * 24), 59, 57, 0, 0 TRAIL[1], TRAIL[2] = { 59, 57 }, { 59, 57 } end end end local function draw_ball(x, y, a) for j = floor(y - 2), floor(y + 2) do for i = floor(x - 2), floor(x + 2) do local c = BR + 0.5 - sqrt((i + 0.5 - x) ^ 2 + (j + 0.5 - y) ^ 2) if c > 0 then blend(i, j, 232, 238, 252, (c > 1 and 1 or c) * a) end end end end function render() local P = TABLES[(cfg.tbl or 0) + 1] or TABLES[1] if BG_KEY ~= P then BG_KEY = P; build_bg(P) end if frame < simf then reset_sim() end if frame - simf > 90 then simf = frame - 90 end while simf < frame do sim_frame(); simf = simf + 1 end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame local lit = (t // 7) % 5 + 1 for i = 1, 5 do local l = LAMPS[i] if lamp > 0 and lamp % 4 < 2 then rect(l[1], l[2], 2, 2, AZURE_HI[1], AZURE_HI[2], AZURE_HI[3]) elseif i == lit then rect(l[1], l[2], 2, 2, AZURE[1], AZURE[2], AZURE[3]) elseif i == (lit + 3) % 5 + 1 then blend_rect(l[1], l[2], 2, 2, AZURE[1], AZURE[2], AZURE[3], 0.4) end end for _, a in ipairs(ARROWS) do local on = lamp > 0 or (t // 10) % 2 == 0 local c = on and ORANGE or { 120, 70, 30 } rect(a[1], a[2], 3, 1, c[1], c[2], c[3]); rect(a[1] + 1, a[2] + 1, 1, 1, c[1], c[2], c[3]) end for i = 1, 3 do if flash[i] > 0 then local b = BUMP[i] disc(b[1], b[2], b[3] + 0.4, flash[i] > 3 and FLASH or ORANGE_HI) disc(b[1], b[2], b[3] - 1.1, AZURE_HI) end end local tip = mode == 2 and by + 1.5 or 58.5 tip = floor(tip + 0.5) rect(57, tip, 4, 1, 190, 196, 214) for y = tip + 1, 61, 2 do rect(58, y, 2, 1, 150, 156, 176) end for s = 1, 2 do local runs = FRUN[s][fpose[s]] for i = 1, #runs do local r = runs[i] rect(r[1], r[2], r[3], 1, 238, 232, 214) end local px0 = s == 1 and PIV[1] - 1 or MIR - PIV[1] rect(px0, PIV[2] - 1, 2, 2, ORANGE[1], ORANGE[2], ORANGE[3]) end local sp = vx * vx + vy * vy if mode == 3 and sp > 2 then draw_ball(TRAIL[2][1], TRAIL[2][2], 0.18) draw_ball(TRAIL[1][1], TRAIL[1][2], 0.35) end draw_ball(bx, by, 1) end