info = { type = "image", title = "Pool break", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A pool break: balls scatter, one drops", api = 33, } local STEP_HZ = 30 local FSTEP = 0 local ltr = ({ sk = { ["Cloth colour"] = "Farba obrusu", ["Speed"] = "Rýchlosť", ["Rack rows"] = "Rady stojanov" }, cs = { ["Cloth colour"] = "Barva ubrusu", ["Speed"] = "Rychlost", ["Rack rows"] = "Řady stojanů" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "cloth", label = LT("Cloth colour"), type = "color", default = "#2352B4" }, { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "rows", label = LT("Rack rows"), type = "int", min = 2, max = 4, default = 3 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sqrt, cos, sin, atan = math.floor, math.sqrt, math.cos, math.sin, math.atan local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local PI = 3.14159265 local XL, XR, YT, YB = 5, 56, 9, 40 local ACC = 0.035 local CYC = 270 local T_HIT0 = 94 local V_CUE = 3.0 local TRAY_X, TRAY_Y, TRAY_END = 7, 54, 42 local POCK = { { 56, YT, 58, 6, 59, 7 }, { 56, YB, 58, 42, 59, 43 }, { 30.5, YT, 30, 6, 31, 7 }, { 30.5, YB, 30, 42, 31, 43 }, { XL, YT, 2, 6, 3, 7 }, { XL, YB, 2, 42, 3, 43 }, } local BALLS = { { 250, 206, 40 }, { 228, 40, 40 }, { 255, 134, 24 }, { 180, 72, 224 }, { 40, 180, 80 }, { 96, 204, 255 }, { 156, 36, 60 }, { 255, 140, 192 }, { 182, 112, 54 }, } local BLACK, CUEB = { 24, 24, 30 }, { 244, 244, 236 } local function fold(p, lo, hi) local w = hi - lo local q = (p - lo) % (2 * w) if q > w then q = 2 * w - q end return lo + q end local function rnd(n, k) return (hash32(n * 0x9E3779B1 + k * 0x85EBCA6B) & 0x7FFF) / 32767 end local function pot_col(n) return 1 + umod(hash32(n * 0x2545F491 + 0x1234), #BALLS) end local NB, B, CB, POT, T_HIT = 0, {}, {}, nil, 0 local ROWS, SPD, GEN_N = 3, 1, nil local function motion(b, L) b.v0 = sqrt(2 * ACC * L) b.T = b.v0 / ACC end local function aim(b, ang, L) local ux = floor(b.sx + cos(ang) * L + 0.5) local uy = floor(b.sy + sin(ang) * L + 0.5) local ex, ey = ux - b.sx, uy - b.sy local d = sqrt(ex * ex + ey * ey) if d < 0.5 then b.dx, b.dy, b.fx, b.fy = 0, 0, b.sx, b.sy motion(b, 0) return end b.dx, b.dy = ex / d, ey / d motion(b, d) b.fx, b.fy = fold(ux, XL, XR), fold(uy, YT, YB) end local function clear_spot(x, y, placed) for k = 1, #POCK do local p = POCK[k] local ex, ey = x - p[1], y - p[2] if ex * ex + ey * ey < 12 then return false end end for j = 1, #placed do local o = placed[j] local ex, ey = x - o.fx, y - o.fy if ex * ex + ey * ey < 14 then return false end end return true end local function gen(n) local RX = 40 + floor(rnd(n, 1) * 4.99) local RY = 22 + floor(rnd(n, 2) * 4.99) local CBX = 17 + floor(rnd(n, 3) * 4.99) NB = 0 for k = 0, ROWS - 1 do for j = 0, k do NB = NB + 1 local b = B[NB] or {} B[NB] = b b.sx, b.sy, b.row = RX + 3 * k, RY + 4 * j - 2 * k, k b.pot, b.black = false, (k == 2 and j == 1) end end T_HIT = T_HIT0 + (V_CUE - sqrt(V_CUE * V_CUE - 2 * ACC * (RX - 3 - CBX))) / ACC CB.x0, CB.sx, CB.sy, CB.t0 = CBX, RX - 3, RY, T_HIT local q = 1 + umod(hash32(n * 0x7FEB352D + 3), 4) local P = POCK[q] local tx, ty = q >= 3 and (2 * XR - P[1]) or P[1], P[2] local best, bd = 1, 1e9 for i = 1, NB do local b = B[i] local ex, ey = tx - b.sx, ty - b.sy if not b.black and ex * ex + ey * ey < bd then best, bd = i, ex * ex + ey * ey end end POT = B[best] POT.pot, POT.pq = true, q local pc = pot_col(n) POT.col = BALLS[pc] local ord = {} for c = 1, #BALLS do if c ~= pc then ord[#ord + 1] = c end end for c = #ord, 2, -1 do local j = 1 + umod(hash32(n * 0x632BE5AB + c), c) ord[c], ord[j] = ord[j], ord[c] end local m = 0 for i = 1, NB do local b = B[i] if b.black then b.col = BLACK elseif not b.pot then m = m + 1; b.col = BALLS[ord[m]] end end local placed = {} local ang, L = PI + (rnd(n, 4) - 0.5) * 2.2, 3 + rnd(n, 5) * 10 for try = 1, 6 do aim(CB, ang, L) if clear_spot(CB.fx, CB.fy, placed) then break end L = L * 0.7 end placed[1] = CB local Ix, Iy = RX - 2, RY for i = 1, NB do local b = B[i] b.t0 = T_HIT + b.row * 0.8 if b.pot then local ex, ey = tx - b.sx, ty - b.sy local d = sqrt(ex * ex + ey * ey) b.dx, b.dy = ex / d, ey / d motion(b, d * (1.2 + rnd(n, 6) * 0.25)) b.tc = b.t0 + (b.v0 - sqrt(b.v0 * b.v0 - 2 * ACC * d)) / ACC else local a = atan(b.sy - Iy, b.sx - Ix) * 1.7 + (rnd(n, 10 + i) - 0.5) * 0.6 local LL = 26 + rnd(n, 30 + i) * 56 + (i == 1 and 24 or 0) for try = 1, 7 do aim(b, a, LL) if clear_spot(b.fx, b.fy, placed) then break end LL = LL * 0.8 end placed[#placed + 1] = b end end end local function pos(b, t) local tau = t - b.t0 if tau <= 0 then return b.sx, b.sy, true end if tau >= b.T then return b.fx, b.fy, true end local s = b.v0 * tau - 0.5 * ACC * tau * tau return fold(b.sx + b.dx * s, XL, XR), fold(b.sy + b.dy * s, YT, YB), false end local function cue_pos(t) if t < T_HIT0 then return CB.x0, CB.sy, true end if t < T_HIT then local tau = t - T_HIT0 return CB.x0 + V_CUE * tau - 0.5 * ACC * tau * tau, CB.sy, false end return pos(CB, t) end local CW, EW, RW, FW = { 0, 1, 1, 0 }, { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } local function ball(x, y, c, rest, dot) local r, g, b = c[1], c[2], c[3] if rest then rect(x + 1, y, 1, 3, r, g, b) rect(x, y + 1, 3, 1, r, g, b) blend(x, y, r, g, b, 0.5); blend(x + 2, y, r, g, b, 0.5) blend(x, y + 2, r, g, b, 0.5); blend(x + 2, y + 2, r, g, b, 0.5) if dot then px(x + 1, y + 1, 240, 240, 230) end return end local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local gx, gy = 1 - fx, 1 - fy CW[1], CW[4], EW[1], EW[2], EW[3], EW[4] = gx, fx, gx, fx, gx, fx RW[1], RW[4], FW[1], FW[2], FW[3], FW[4] = gy, fy, gy, fy, gy, fy for j = 1, 4 do local rw, fw = RW[j], FW[j] for i = 1, 4 do local a = CW[i] * rw - 0.5 * EW[i] * fw if a > 0.97 then px(ix + i - 1, iy + j - 1, r, g, b) elseif a > 0.05 then blend(ix + i - 1, iy + j - 1, r, g, b, a) end end end end local function stick(t, cbx, cy) if t < 24 or t >= 132 then return end local g if t < 40 then local p = (t - 24) / 16; g = 3 + 27 * (1 - p) * (1 - p) elseif t < 80 then g = 3 + 7 * (0.5 - 0.5 * cos(PI * (t - 40) / 40)) elseif t < 90 then g = 10 elseif t < 94 then local p = (t - 90) / 4; g = 10 - 10 * p * p elseif t < 100 then g = -2 * (t - 94) / 6 else local p = (t - 100) / 32; g = -2 + 50 * p * p end local e = cbx - g local ie = floor(e) local f = e - ie if ie > 0 then blend_rect(0, cy + 1, ie, 1, 0, 0, 0, 0.3) end if ie - 20 > 0 then rect(0, cy - 1, ie - 20, 1, 84, 30, 22) end if ie - 13 > 0 then rect(0, cy, ie - 13, 1, 120, 44, 30) end rect(ie - 13, cy, 1, 1, 220, 220, 228) rect(ie - 12, cy, 10, 1, 234, 198, 134) px(ie - 2, cy, 246, 244, 236) px(ie - 1, cy, 70, 130, 230) if f > 0.05 then blend(ie, cy, 70, 130, 230, f) end end local function draw_pot(t) local b = POT if t < b.tc then local x, y, rest = pos(b, t) ball(x, y, b.col, rest, rest) return end local a, P, c = t - b.tc, POCK[b.pq], b.col if a < 3 then rect(P[5], P[6], 2, 2, c[1], c[2], c[3]) elseif a < 6 then px(P[5], P[6], c[1] * 6 // 10, c[2] * 6 // 10, c[3] * 6 // 10) end end local function draw_tray(n, t) local b0 = n - n % 6 for k = b0, n - 1 do ball(TRAY_X + 4 * (k - b0), TRAY_Y, BALLS[pot_col(k)], true, true) end local a = t - (POT.tc + 24) if a >= 0 then local sx = TRAY_X + 4 * (n - b0) local p = a / 30 if p >= 1 then ball(sx, TRAY_Y, POT.col, true, true) else ball(sx + (TRAY_END - sx) * (1 - p) * (1 - p), TRAY_Y, POT.col, false, false) end end end local BG = nil local FR, FG, FB = 35, 82, 180 local function build_bg() rect(0, 0, W, H, 62, 38, 70) for y = 1, H - 1, 4 do for x = (y // 4 % 2) * 3, W - 1, 6 do px(x, y, 90, 58, 98) end end rect(0, 4, 64, 44, 92, 48, 24) rect(1, 5, 62, 42, 150, 86, 42) rect(3, 7, 58, 38, 112, 62, 30) rect(4, 8, 56, 36, FR, FG, FB) local cr, cg, cb = FR * 68 // 100, FG * 68 // 100, FB * 68 // 100 rect(4, 8, 56, 1, cr, cg, cb); rect(4, 43, 56, 1, cr, cg, cb) rect(4, 8, 1, 36, cr, cg, cb); rect(59, 8, 1, 36, cr, cg, cb) for k = 1, 7 do if k ~= 4 then px(4 + 7 * k, 5, 240, 226, 186); px(4 + 7 * k, 46, 240, 226, 186) end end for k = 1, 3 do px(1, 8 + 9 * k, 240, 226, 186); px(62, 8 + 9 * k, 240, 226, 186) end for k = 1, #POCK do local x, y = POCK[k][3], POCK[k][4] rect(x + 1, y, 2, 4, 14, 10, 16) rect(x, y + 1, 4, 2, 14, 10, 16) end rect(3, 51, 46, 8, 132, 76, 38) rect(3, 51, 46, 1, 168, 102, 54) rect(5, 53, 42, 5, 70, 40, 22) rect(53, 52, 5, 5, 64, 124, 214) rect(54, 53, 3, 3, 38, 84, 170) px(53, 52, 120, 170, 240) BG = gfx.snapshot(0, 0, W, H) end function on_settings() local c = cfg.cloth or { r = 35, g = 82, b = 180 } FR, FG, FB = c.r, c.g, c.b SPD = 0.25 + 0.25 * (cfg.speed or 3) ROWS = cfg.rows or 3 BG, GEN_N = nil, nil end function render() FSTEP = frame * STEP_HZ // gfx.FPS if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local tt = FSTEP * SPD local n = floor(tt / CYC) local t = tt - n * CYC if n ~= GEN_N then gen(n); GEN_N = n end draw_tray(n, t) for i = 1, NB do if t >= 2 * i then local b = B[i] if b.pot then draw_pot(t) else local x, y, rest = pos(b, t) ball(x, y, b.col, rest, rest) end end end if t >= 2 * NB + 4 then local x, y, rest = cue_pos(t) ball(x, y, CUEB, rest, false) end stick(t, CB.x0, CB.sy + 1) end