info = { type = "image", title = "Ping pong hall", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Two players rally across a blue table", api = 33, } local tr = ({ sk = { ["Shirts"] = "Dresy", ["Classic"] = "Klasické", ["Sunny"] = "Slnečné", ["Forest"] = "Lesné", ["Rally speed"] = "Rýchlosť výmeny", ["Slow"] = "Pomaly", ["Normal"] = "Bežne", ["Fast"] = "Rýchlo", ["Score"] = "Skóre" }, cs = { ["Shirts"] = "Dresy", ["Classic"] = "Klasické", ["Sunny"] = "Slunečné", ["Forest"] = "Lesní", ["Rally speed"] = "Rychlost výměny", ["Slow"] = "Pomalu", ["Normal"] = "Běžně", ["Fast"] = "Rychle", ["Score"] = "Skóre" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "shirts", label = T("Shirts"), type = "int", default = "classic", opts = { keys = { "classic", "sunny", "forest" }, T("Classic"), T("Sunny"), T("Forest") } }, { key = "speed", label = T("Rally speed"), type = "int", default = 1, opts = { T("Slow"), T("Normal"), T("Fast") } }, { key = "score", label = T("Score"), type = "bool", default = true }, } local rect, blend_rect = gfx.rect, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sqrt, abs, min, max = math.floor, math.sqrt, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local FY, TY = 57, 45 local BOUNCE = TY - 2 local FLOOR_BALL = 58 local TT = 0.67 local SPEEDS = { 34, 45, 60 } local SHIRTS = { classic = { { 196, 56, 64 }, { 52, 160, 152 } }, sunny = { { 232, 188, 48 }, { 132, 84, 176 } }, forest = { { 72, 150, 84 }, { 224, 112, 48 } }, } local SKIN, SHORTS, SHOE, EYE = { 232, 184, 140 }, { 44, 44, 70 }, { 236, 232, 224 }, { 30, 24, 24 } local HAIRS = { { 60, 44, 36 }, { 214, 170, 72 } } local BLADE, WOOD = { 170, 40, 36 }, { 120, 80, 50 } local BALL = { 252, 122, 28 } local C1, C2, D1, D2 local function dark(c) return { floor(c[1] * 0.8), floor(c[2] * 0.8), floor(c[3] * 0.8) } end C1, C2 = SHIRTS.classic[1], SHIRTS.classic[2] D1, D2 = dark(C1), dark(C2) local function T3(c) return c[1], c[2], c[3] end local BG local function build_bg() for y = 0, 47 do local t = y / 47 rect(0, y, W, 1, floor(196 - 20 * t), floor(166 - 24 * t), floor(130 - 24 * t)) end rect(0, 38, W, 10, 142, 106, 78) rect(0, 38, W, 1, 112, 80, 58) for _, wx in ipairs({ 4, 48 }) do rect(wx - 1, 5, 14, 14, 128, 98, 72) rect(wx, 6, 12, 12, 228, 216, 174) rect(wx + 5, 6, 2, 12, 128, 98, 72) rect(wx, 11, 12, 1, 128, 98, 72) end for y = 48, H - 1 do local t = (y - 48) / 15 rect(0, y, W, 1, floor(178 - 34 * t), floor(132 - 30 * t), floor(86 - 24 * t)) if (y - 48) % 4 == 0 then blend_rect(0, y, W, 1, 70, 44, 24, 0.3) end end for y = 48, H - 1 do local off = (y // 4) * 13 for x = off % 17, W - 1, 17 do blend_rect(x, y, 1, 1, 70, 44, 24, 0.22) end end blend_rect(12, 57, 40, 4, 60, 38, 22, 0.3) rect(0, 47, W, 1, 96, 70, 50) rect(16, 51, 2, 6, 70, 70, 84) rect(45, 51, 2, 6, 70, 70, 84) rect(13, 45, 38, 1, 196, 214, 240) rect(13, 46, 38, 2, 50, 90, 190) rect(13, 48, 38, 3, 30, 58, 150) rect(13, 51, 38, 1, 20, 36, 100) rect(31, 46, 2, 2, 190, 208, 238) BG = snapshot(0, 0, W, H) end local R, VX = nil, 45 local function shot(k) local s = R.sp[k] if s then return s end local h = hash32(R.seed + k * 0x9E3779B1) local hs = (R.s0 + k) % 2 local d = hs == 0 and 1 or -1 local cx, ay = 16 + umod(h, 4), 40 + umod(h >> 4, 3) s = { hs = hs, d = d, cx = cx, ax = hs == 0 and cx or 62 - cx, ay = ay } R.sp[k] = s if k < R.n then local h2 = hash32(R.seed + (k + 1) * 0x9E3779B1) local xb0 = 38 + umod(h >> 8, 6) local xb = hs == 0 and xb0 or 62 - xb0 local bx = hs == 0 and 62 - (16 + umod(h2, 4)) or 16 + umod(h2, 4) local by = 40 + umod(h2 >> 4, 3) local hA = 6 + umod(h >> 12, 3) local T1, T2 = abs(xb - s.ax) / VX, abs(bx - xb) / VX local g = 2 * (sqrt(hA) + sqrt(hA + BOUNCE - ay)) ^ 2 / (T1 * T1) s.kind, s.xb, s.T1, s.g = "n", xb, T1, g s.vy0, s.vy1 = -sqrt(2 * g * hA), (by - BOUNCE) / T2 - 0.5 * g * T2 s.dur = T1 + T2 elseif R.ending == "net" then local nx = hs == 0 and 29 or 33 local Tn, g = abs(nx - s.ax) / VX, 166 s.kind, s.nx, s.T1, s.g = "net", nx, Tn, g s.vy0 = (42 - ay) / Tn - 0.5 * g * Tn s.dur = Tn + 1.1 else local xl0 = 42 + umod(h >> 8, 3) local xl = hs == 0 and xl0 or 62 - xl0 local T1 = abs(xl - s.ax) / VX local g = 2 * (sqrt(11) + sqrt(11 + FLOOR_BALL - ay)) ^ 2 / (T1 * T1) s.kind, s.xl, s.T1, s.g, s.vy0 = "out", xl, T1, g, -sqrt(2 * g * 11) s.vyl = s.vy0 + g * T1 s.dur = T1 + 2.2 end return s end local function new_rally(prev) R = { sp = {}, ts = {}, ct = { {}, {} } } R.seed = prev and hash32(prev.seed + 0x7F4A7C15) or 0x1234567 R.t0 = prev and prev.t_end or 0 R.s0, R.n = umod(R.seed >> 3, 2), 2 + umod(R.seed >> 6, 4) R.ending = umod(R.seed >> 9, 2) == 0 and "net" or "out" local t = R.t0 + TT for k = 0, R.n do local s = shot(k) R.ts[k] = t local c = R.ct[s.hs + 1] c[#c + 1] = { t = t, x0 = s.cx - 13 } t = t + s.dur end R.t_end = t + 0.6 local last, tn = R.sp[R.n], R.ts[R.n] local a, b = 0, 0 if prev then a, b = prev.sc2[1], prev.sc2[2] end if a >= 11 or b >= 11 then a, b = 0, 0 end R.sc1 = { a, b } R.sc2 = { a + last.hs, b + 1 - last.hs } R.start = prev and prev.fin or { R.ct[1][1].x0, R.ct[2][1].x0 } R.fin = { R.ct[1][#R.ct[1]].x0, R.ct[2][#R.ct[2]].x0 } if R.ending == "net" then R.t_pt = tn + last.T1 else R.t_pt = tn + last.T1 + 1.5 R.pick_p, R.pick_a, R.pick_b = 1 - last.hs, tn + last.T1 + 1.0, tn + last.T1 + 1.9 end end function on_settings() local s = SHIRTS[cfg.shirts] or SHIRTS.classic C1, C2 = s[1], s[2] D1, D2 = dark(C1), dark(C2) VX = SPEEDS[(cfg.speed or 1) + 1] or 45 R = nil end local function ball(t) if t < R.t0 + TT then local s, u = shot(0), t - R.t0 local g = 64 / (TT * TT) return s.ax, s.ay - (g * TT / 2 * u - 0.5 * g * u * u) end for k = 0, R.n do local s, u = R.sp[k], t - R.ts[k] if u >= 0 and u < s.dur then local d, g = s.d, s.g if s.kind == "n" then if u < s.T1 then return s.ax + d * VX * u, s.ay + s.vy0 * u + 0.5 * g * u * u end u = u - s.T1 return s.xb + d * VX * u, BOUNCE + s.vy1 * u + 0.5 * g * u * u elseif s.kind == "net" then if u < s.T1 then return s.ax + d * VX * u, s.ay + s.vy0 * u + 0.5 * g * u * u end u = u - s.T1 if u > 0.8 and floor(u * 10) % 2 == 1 then return nil end return s.nx - d * min(3, 8 * u), min(BOUNCE, 42 + 0.5 * g * u * u) end if u < s.T1 then return s.ax + d * VX * u, s.ay + s.vy0 * u + 0.5 * g * u * u end u = u - s.T1 if u >= 1.5 then return nil end local vf, e = 0.3 * VX, 0.5 local ur = min(u, 1.4) local x = s.xl + d * (vf * ur - 0.5 * vf / 1.4 * ur * ur) local t1 = 2 * e * s.vyl / g local rise = 0 if u < t1 then rise = e * s.vyl * u - 0.5 * g * u * u elseif u < t1 * (1 + e) then local u2 = u - t1 rise = e * e * s.vyl * u2 - 0.5 * g * u2 * u2 end return x, FLOOR_BALL - rise end end end local function pos0(p, t) local ct, cur, tp = R.ct[p + 1], R.start[p + 1], R.t0 for i = 1, #ct do local c = ct[i] if t < c.t then local steps = floor((t - tp - 0.25) * FPS) local dx = c.x0 - cur if steps <= 0 or dx == 0 then return cur end return cur + (dx > 0 and 1 or -1) * min(abs(dx), steps) end cur, tp = c.x0, c.t end return cur end local function pose(p, t) if R.pick_p == p and t >= R.pick_a and t < R.pick_b then return "crouch" end local ct = R.ct[p + 1] for i = 1, #ct do local dt = t - ct[i].t if dt >= -0.26 and dt < 0 then return "back" end if dt >= 0 and dt < 0.2 then return "hit" end end return "ready" end local function player(p, x0, ps) local jer = p == 0 and C1 or C2 local hair = HAIRS[p + 1] local y0 = FY - 23 local dy = ps == "crouch" and 3 or (ps == "back" and 1 or 0) local function R_(x, y, w, h, c) rect(p == 1 and 63 - (x0 + x + w - 1) or x0 + x, y0 + y, w, h, T3(c)) end blend_rect(p == 1 and 63 - (x0 + 6) or x0 - 1, FY + 1, 8, 1, 60, 38, 22, 0.35) R_(0, 0 + dy, 5, 2, hair); R_(0, 2 + dy, 1, 3, hair) R_(1, 2 + dy, 4, 4, SKIN); R_(4, 3 + dy, 1, 1, EYE); R_(2, 6 + dy, 2, 1, SKIN) R_(0, 7 + dy, 6, 9, jer); R_(0, 11 + dy, 6, 1, p == 0 and D1 or D2) R_(0, 16 + dy, 6, 4, SHORTS) if dy < 3 then R_(1, 20 + dy, 2, 3 - dy, SKIN); R_(3, 20 + dy, 2, 3 - dy, SKIN) end R_(0, 23, 3, 1, SHOE); R_(3, 23, 3, 1, SHOE) if ps == "hit" then R_(5, 8, 5, 2, SKIN); R_(10, 8, 1, 1, WOOD); R_(11, 6, 2, 5, BLADE) elseif ps == "back" then R_(-1, 9, 2, 2, SKIN); R_(-2, 8, 1, 1, WOOD); R_(-3, 4, 3, 4, BLADE) elseif ps == "crouch" then R_(5, 11, 2, 12, SKIN) else R_(5, 9, 4, 2, SKIN); R_(9, 10, 1, 1, WOOD); R_(10, 8, 2, 4, BLADE) end end local FC = { font = "3x5", w = 22, align = "center" } local TOP, BOT function render() if not BG then build_bg() end local t = frame / FPS if not R then new_rally(nil) end while t >= R.t_end do new_rally(R) end blit_rgb(BG, 0, 0, W, H) for p = 0, 1 do player(p, pos0(p, t), pose(p, t)) end local s = R.sp[R.n] local bul = 0 if R.ending == "net" then local u = t - R.ts[R.n] - s.T1 if u >= 0 and u < 0.3 then bul = s.d end end rect(31, 40, 2, 1, 236, 236, 230) rect(31, 44, 2, 1, 150, 150, 162) rect(31 + bul, 41, 1, 3, 214, 214, 220) rect(32 + bul, 41, 1, 3, 150, 150, 162) local bx, by = ball(t) if bx then local ix, iy = floor(bx + 0.5), floor(by + 0.5) if ix > 13 and ix < 49 and iy < BOUNCE then blend_rect(ix, TY + 1, 2, 1, 16, 32, 96, 0.5) end rect(ix, iy, 2, 2, T3(BALL)) end if cfg.score then if not TOP then TOP, BOT = gfx.text_ink("0-0", FC) end local sc = t >= R.t_pt and R.sc2 or R.sc1 rect(21, 2, 22, 9, 66, 46, 34) rect(21, 2, 22, 1, 116, 84, 58) gfx.text(21, 2 + (9 - (BOT - TOP + 1)) // 2 - TOP, string.format("%d-%d", sc[1], sc[2]), 255, 214, 130, FC) end end