info = { type = "image", title = "Ball box", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "Balls fill a glass box, then pour out", api = 33, } local tr = ({ sk = { ["Colour"] = "Farba", ["Teal"] = "Tyrkysová", ["Coral"] = "Koralová", ["Violet"] = "Fialová", ["Gold"] = "Zlatá", ["Rainbow"] = "Dúha", ["Balls"] = "Loptičky", ["Speed"] = "Rýchlosť" }, cs = { ["Colour"] = "Barva", ["Teal"] = "Tyrkysová", ["Coral"] = "Korálová", ["Violet"] = "Fialová", ["Gold"] = "Zlatá", ["Rainbow"] = "Duha", ["Balls"] = "Kuličky", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tone", label = T("Colour"), type = "int", default = "teal", opts = { keys = { "teal", "coral", "violet", "gold", "rainbow" }, T("Teal"), T("Coral"), T("Violet"), T("Gold"), T("Rainbow") } }, { key = "balls", label = T("Balls"), type = "int", min = 6, max = 16, default = 14 }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, blend_rect = gfx.rect, gfx.blend_rect local floor, ceil, sqrt, min, max = math.floor, math.ceil, math.sqrt, math.min, math.max local hash32 = math.hash32 local W, H = gfx.W, gfx.H local T0 = 0 local XL, XR, TOP, FLOOR = 16, 48, 28, 50 local G, EF, EW, EB = 330, 0.42, 0.5, 0.45 local TONES = { teal = { { 36, 184, 170 }, { 24, 150, 160 }, { 70, 210, 190 }, { 28, 120, 140 }, { 120, 224, 206 } }, coral = { { 240, 100, 80 }, { 220, 70, 70 }, { 255, 140, 100 }, { 190, 60, 70 }, { 255, 170, 130 } }, violet = { { 150, 100, 230 }, { 120, 80, 200 }, { 180, 130, 240 }, { 96, 70, 170 }, { 206, 170, 250 } }, gold = { { 240, 184, 48 }, { 220, 150, 30 }, { 255, 212, 90 }, { 190, 120, 30 }, { 255, 230, 140 } }, rainbow = { { 235, 60, 60 }, { 240, 170, 40 }, { 80, 200, 90 }, { 60, 140, 235 }, { 190, 90, 220 } }, } local BGE = { 12, 26, 44 } local RC = { 2.7, 3.3, 4.0 } local SZ = { 8, 9, 10 } local PAL = {} local NB, GAP = 14, 11 local BG, INIT = nil, false local function shade(c, sb) local r, g, b = c[1], c[2], c[3] if sb <= 3 then local k = 0.32 + 0.68 * sb / 3 return r * k, g * k, b * k end local k = (sb - 3) / 3 if sb == 7 then k = 1.5 end local m = min(k, 1) local lr, lg, lb = r + (255 - r) * 0.55, g + (255 - g) * 0.55, b + (255 - b) * 0.55 local hr, hg, hb = r + (lr - r) * m, g + (lg - g) * m, b + (lb - b) * m if sb == 7 then return hr + (255 - hr) * 0.7, hg + (255 - hg) * 0.7, hb + (255 - hb) * 0.7 end return hr, hg, hb end function on_settings() local set = TONES[cfg.tone] or TONES.teal for t = 1, 5 do local p = { 0, 0, 0 } for idx = 1, 32 do local sb, cq = (idx - 1) // 4, (idx - 1) % 4 + 1 local r, g, b = shade(set[t], sb) local a = cq / 4 p[#p + 1] = floor(BGE[1] + (r - BGE[1]) * a + 0.5) p[#p + 1] = floor(BGE[2] + (g - BGE[2]) * a + 0.5) p[#p + 1] = floor(BGE[3] + (b - BGE[3]) * a + 0.5) end PAL[t] = p end NB = cfg.balls or 14 GAP = floor(30 * (0.62 - 0.08 * (cfg.speed or 3))) INIT = false end local SPR = {} local LX, LY, LZ = -0.45, -0.55, 0.70 local function sprite(cl, qx, qy) local S, r = SZ[cl], RC[cl] local cx, cy = S / 2 + (qx + 0.5) / 4 - 0.125, S / 2 + (qy + 0.5) / 4 - 0.125 local cells = {} for j = 0, S - 1 do for i = 0, S - 1 do local dx, dy = i + 0.5 - cx, j + 0.5 - cy local d = sqrt(dx * dx + dy * dy) local cq = floor(min(1, max(0, r - d + 0.5)) * 4 + 0.5) local v = 0 if cq > 0 then local nx, ny = dx / r, dy / r local rr = min(1, nx * nx + ny * ny) local lam = 0.15 + 0.85 * max(0, nx * LX + ny * LY + sqrt(1 - rr) * LZ) local sb = min(6, floor(lam * 6.4)) local hx, hy = dx + 0.4 * r, dy + 0.45 * r if hx * hx + hy * hy < (0.22 * r) ^ 2 then sb = 7 end v = sb * 4 + cq end cells[#cells + 1] = v end end return string.char(table.unpack(cells, 1, S * S)) end local built = 0 local function get_sprite(cl, qx, qy) local key = ((cl - 1) * 4 + qy) * 4 + qx + 1 local s = SPR[key] if s then return s end if built >= 3 then return SPR[((cl - 1) * 4) * 4 + 1] end built = built + 1 s = sprite(cl, qx, qy) SPR[key] = s return s end local X, Y, VX, VY, R, CL, TN, ON, SL = {}, {}, {}, {}, {}, {}, {}, {}, {} local sf, simf, cyc, PRE = 0, 0, 0, 0 local FOPEN, FCLOSE, FEND = 0, 0, 0 local function rnd(i, k) return (hash32(cyc * 7919 + i * 104729 + k * 15485863) & 0x7FFF) / 0x7FFF end local function reset(first) for i = 1, NB do local cl = (i * 7) % 3 + 1 R[i], CL[i] = RC[cl], cl TN[i] = floor(rnd(i, 1) * 5) + 1 ON[i], SL[i], VX[i], VY[i] = 0, 0, 0, 0 end PRE = first and floor(NB * 0.6) or 0 for i = 1, PRE do local row, col = (i - 1) // 5, (i - 1) % 5 X[i] = XL + 3.5 + col * 6.8 + (row % 2) * 3.4 Y[i] = FLOOR - 3.5 - row * 6.2 ON[i], SL[i] = 1, 0 end sf = PRE * GAP FOPEN = NB * GAP + 75 FCLOSE = FOPEN + 120 FEND = FCLOSE + 45 end local function slide(sf_) local function ease(p) p = min(1, max(0, p)); return p * p * (3 - 2 * p) end return 16 * (ease((sf_ - FOPEN) / 36) - ease((sf_ - FCLOSE) / 30)) end local function pairs_pass() for i = 1, NB - 1 do if ON[i] == 1 then local xi, yi, ri = X[i], Y[i], R[i] for j = i + 1, NB do if ON[j] == 1 then local dx, dy = X[j] - xi, Y[j] - yi local rs = ri + R[j] if dx < rs and dx > -rs and dy < rs and dy > -rs then local d2 = dx * dx + dy * dy if d2 < rs * rs and d2 > 1e-6 then local d = sqrt(d2) local nx, ny = dx / d, dy / d local ov = rs - d local mi, mj = ri * ri, R[j] * R[j] local vn = (VX[j] - VX[i]) * nx + (VY[j] - VY[i]) * ny if vn < -45 then SL[i], SL[j] = 0, 0 end local sli, slj = SL[i] > 25, SL[j] > 25 if not (sli and slj) then local wi, wj = mj / (mi + mj), mi / (mi + mj) if sli then wi, wj = 0, 1 elseif slj then wi, wj = 1, 0 end X[i] = X[i] - nx * ov * wi; Y[i] = Y[i] - ny * ov * wi X[j] = X[j] + nx * ov * wj; Y[j] = Y[j] + ny * ov * wj if vn < 0 then local imi, imj = sli and 0 or 1 / mi, slj and 0 or 1 / mj local eb = vn < -30 and EB or 0 local jm = -(1 + eb) * vn / (imi + imj) VX[i] = VX[i] - jm * nx * imi; VY[i] = VY[i] - jm * ny * imi VX[j] = VX[j] + jm * nx * imj; VY[j] = VY[j] + jm * ny * imj end xi, yi = X[i], Y[i] end end end end end end end end local function substep(dt, s) for i = 1, NB do if ON[i] == 1 then if SL[i] <= 25 then VY[i] = VY[i] + G * dt if VY[i] > 200 then VY[i] = 200 end X[i] = X[i] + VX[i] * dt Y[i] = Y[i] + VY[i] * dt end end end pairs_pass() pairs_pass() local lend, rstart = 32 - s, 32 + s for i = 1, NB do if ON[i] == 1 then local x, y, r = X[i], Y[i], R[i] if y < FLOOR + 2 then if x - r < XL then X[i] = XL + r; if VX[i] < 0 then VX[i] = -VX[i] * EW end end if x + r > XR then X[i] = XR - r; if VX[i] > 0 then VX[i] = -VX[i] * EW end end x = X[i] if y + r > FLOOR and y < FLOOR + r + 3 and ((x >= XL and x <= lend) or (x >= rstart and x <= XR)) then Y[i] = FLOOR - r if VY[i] > 0 then VY[i] = -VY[i] * EF if -VY[i] < 18 then VY[i] = 0 end end VX[i] = VX[i] * 0.985 end end local sp = VX[i] * VX[i] + VY[i] * VY[i] if sp < 64 and y < FLOOR + 1 and sf < FOPEN then SL[i] = SL[i] + 1 else SL[i] = 0 end if SL[i] > 25 then VX[i], VY[i] = 0, 0 end end end end local function step() sf = sf + 1 for i = PRE + 1, NB do if ON[i] == 0 and sf >= (i - 1) * GAP then ON[i] = 1 X[i] = 24 + rnd(i, 2) * 16 Y[i] = -R[i] - 1 VX[i], VY[i] = (rnd(i, 3) - 0.5) * 24, 30 end end if sf == FOPEN then for i = 1, NB do SL[i] = 0 end end local s = slide(sf) substep(1 / 60, s) substep(1 / 60, s) for i = 1, NB do if ON[i] == 1 and Y[i] > H + 8 then ON[i] = 2 end end if sf >= FEND then cyc = cyc + 1; reset(false) end end local function build_bg() for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, floor(8 + 6 * t + 0.5), floor(16 + 14 * t + 0.5), floor(32 + 20 * t + 0.5)) end blend_rect(XL, TOP, XR - XL, FLOOR - TOP, 120, 170, 200, 0.06) rect(XL - 2, TOP, 2, H - TOP, 70, 110, 134) rect(XR, TOP, 2, H - TOP, 70, 110, 134) rect(XL - 2, TOP, 1, H - TOP, 120, 164, 186) rect(XR + 1, TOP, 1, H - TOP, 38, 64, 84) rect(XL - 4, TOP - 1, 5, 2, 150, 190, 208) rect(XR - 1, TOP - 1, 5, 2, 150, 190, 208) rect(XL - 6, 52, 6, 2, 46, 70, 90) rect(XR, 52, 6, 2, 46, 70, 90) rect(XL - 8, 62, 10, 2, 46, 70, 90) rect(XR - 2, 62, 10, 2, 46, 70, 90) BG = gfx.snapshot(0, 0, W, H) end function render() if not BG then build_bg() end if not INIT then INIT = true cyc = 0 reset(true) simf = frame for cl = 1, 3 do SPR[((cl - 1) * 4) * 4 + 1] = sprite(cl, 0, 0) end end local target = frame + T0 if simf > target or target - simf > 150 then reset(true); simf = target end local n = 0 while simf < target and n < 6 do step(); simf = simf + 1; n = n + 1 end built = 0 gfx.blit_rgb(BG, 0, 0, W, H) local s = slide(sf) if s < 16 then local lw = 32 - s - XL if lw > 0 then rect(XL, FLOOR, lw, 2, 96, 116, 132) rect(XL, FLOOR, lw, 1, 190, 208, 220) end local rx = 32 + s if XR - rx > 0 then rect(rx, FLOOR, XR - rx, 2, 96, 116, 132) rect(rx, FLOOR, XR - rx, 1, 190, 208, 220) end end for i = 1, NB do if ON[i] == 1 then local cl = CL[i] local S = SZ[cl] local ox, oy = X[i] - S / 2, Y[i] - S / 2 local ix, iy = floor(ox), floor(oy) if iy < H and iy + S > 0 then local qx, qy = min(3, floor((ox - ix) * 4)), min(3, floor((oy - iy) * 4)) gfx.blit_indexed(get_sprite(cl, qx, qy), PAL[TN[i]], ix, iy, S, S, S, S, true) end end end blend_rect(XL + 2, TOP + 1, 2, 18, 255, 255, 255, 0.10) blend_rect(XL + 5, TOP + 1, 1, 10, 255, 255, 255, 0.07) blend_rect(XR - 4, TOP + 4, 1, 14, 255, 255, 255, 0.06) end