info = { type = "image", title = "Handshake", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Two gloved hands meet, shake and part again", api = 33, } local tr = ({ sk = { ["Left sleeve"] = "Ľavý rukáv", ["Right sleeve"] = "Pravý rukáv", ["Shakes"] = "Trasenie" }, cs = { ["Left sleeve"] = "Levý rukáv", ["Right sleeve"] = "Pravý rukáv", ["Shakes"] = "Třesení" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "left", label = T("Left sleeve"), type = "color", default = "#3C82F0" }, { key = "right", label = T("Right sleeve"), type = "color", default = "#F26A2C" }, { key = "shakes", label = T("Shakes"), type = "int", min = 2, max = 6, default = 4 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, abs = math.floor, math.sin, math.cos, math.abs local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local BGC, D1, D2 = { 52, 28, 70 }, { 84, 46, 104 }, { 118, 68, 136 } local KOL = { 28, 20, 36 } local GLOVE = { ".......KKK............", "......KWWWK...........", "......KWWWK...........", "......KWWWK...........", "KKKKKKKWWWKKKKKKKKKK..", "KWWKWWWWWWWWWWWWWWWWK.", "KWWKWWWWWWWWWWWWWWWWK.", "KWWKWWWWWWWWKKKKKKKKK.", "KWWKWWWWWWWWWWWWWWWWWK", "KWWKWWWWWWWWWWWWWWWWWK", "KWWKWWWWWWWWKKKKKKKKK.", "KWWKWWWWWWWWWWWWWWWWK.", "KWWKWWWWWWWWWWWWWWWWK.", "KWWKWWWWWWWWKKKKKKK...", "KWWKWWWWWWWWWWWWWWK...", "KWWKGGGGGGGGGWWWWWK...", "KKKKKKKKKKKKKKKKKK....", } local GW, GH = 22, 17 local GLOVE_L, GLOVE_R local GPAL = { 0, 0, 0, 28, 20, 36, 255, 255, 255, 204, 206, 234 } local MAP = { ["."] = "\0", K = "\1", W = "\2", G = "\3" } local function build_gloves() local l, r = {}, {} for i = 1, GH do l[i] = GLOVE[i]:gsub("[.KWG]", MAP) r[i] = GLOVE[i]:reverse():gsub("[.KWG]", MAP) end GLOVE_L, GLOVE_R = table.concat(l), table.concat(r) end local BG = nil local function disc(cx, cy, r, c) for y = cy - r, cy + r - 1 do local dy = y + 0.5 - cy local q = r * r - dy * dy if q > 0 then local hw = math.sqrt(q) local xa, xb = math.ceil(cx - hw - 0.5), floor(cx + hw - 0.5) if xb >= xa then rect(xa, y, xb - xa + 1, 1, c[1], c[2], c[3]) end end end end local function build_bg() rect(0, 0, W, H, BGC[1], BGC[2], BGC[3]) disc(32, 33, 28, D1) disc(32, 33, 19, D2) local STAR = { 176, 120, 190 } for i = 1, 12 do local h = hash32(i * 0x9E3779B1) local x, y = math.umod(h, W), math.umod(h >> 8, 8) + (i % 2 == 0 and 0 or 54) px(x, y, STAR[1], STAR[2], STAR[3]) end BG = gfx.snapshot(0, 0, W, H) end local SL, SLD = { 60, 130, 240 }, { 40, 88, 172 } local SR, SRD = { 242, 106, 44 }, { 176, 70, 24 } local YS = 41 local function sleeve(xw, yw, left, c, cd) local len = left and xw or (W - xw) if len <= 0 then return end local x = 0 while x < len do local w = len - x < 3 and len - x or 3 local ym = floor(YS + (yw - YS) * (x + w * 0.5) / len + 0.5) local xx = left and x or (W - x - w) rect(xx, ym - 6, w, 13, KOL[1], KOL[2], KOL[3]) rect(xx, ym - 5, w, 11, c[1], c[2], c[3]) rect(xx, ym + 2, w, 3, cd[1], cd[2], cd[3]) x = x + w end end local DX, DY = {}, {} for i = 0, 7 do DX[i], DY[i] = cos(i * PI / 4), sin(i * PI / 4) end local function sparks(cx, cy, k, seed) local r = 7 + 2 * k for i = 0, 7 do local h = hash32(seed * 0x9E3779B1 + i * 0x85EBCA6B) & 0x7FFF if h % 3 ~= 0 then local a = i * PI / 4 + (h % 100) / 100 * 0.5 local ca, sa = cos(a), sin(a) local x0, y0 = floor(cx + ca * r + 0.5), floor(cy + sa * r * 0.8 + 0.5) local x1, y1 = floor(cx + ca * (r + 2.2) + 0.5), floor(cy + sa * (r + 2.2) * 0.8 + 0.5) local x2, y2 = floor(cx + ca * (r + 4.4) + 0.5), floor(cy + sa * (r + 4.4) * 0.8 + 0.5) if k < 5 then px(x0, y0, 255, 238, 120); px(x1, y1, 255, 238, 120); px(x2, y2, 255, 255, 255) else px(x0, y0, 255, 170, 60); px(x1, y1, 255, 170, 60) end end end end local function smooth(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end function render() if not BG then build_bg(); build_gloves() end gfx.blit_rgb(BG, 0, 0, W, H) local n = cfg.shakes or 4 local t_apart, t_app, t_sq, t_shk = 1.5, 0.7, 0.2, 0.45 local t_hold, t_ret, t_idle = 0.4, 0.7, 0.6 local shake_len = n * t_shk local cyc = t_apart + t_app + t_sq + shake_len + t_hold + t_ret + t_idle local t = (frame / FPS) % cyc local round = floor(frame / FPS / cyc) local p, dy, vy, burst = 0, 0, 0, -1 local t1 = t_apart local t2 = t1 + t_app local t3 = t2 + t_sq local t4 = t3 + shake_len local t5 = t4 + t_hold local t6 = t5 + t_ret if t < t1 then p = 0 elseif t < t2 then p = smooth((t - t1) / t_app) elseif t < t3 then p = 1 elseif t < t4 then p = 1 local s = t - t3 local ph = s / t_shk * 2 * PI local env = 1 - 0.25 * s / shake_len dy = floor(4.6 * env * sin(ph) + 0.5) vy = cos(ph) local half = t_shk / 2 local ks = (s + half * 0.5) % half if ks < 8 / FPS then burst = floor(ks * FPS); end elseif t < t5 then p = 1 elseif t < t6 then p = 1 - smooth((t - t5) / t_ret) else p = 0 end local bob = 0 if p < 0.01 then bob = floor(1.2 * sin(frame / FPS * 3) + 0.5) end local lx = 5 + floor(12 * p + 0.5) local rx = 37 - floor(12 * p + 0.5) local yc = 33 + dy + bob local y0 = yc - 10 local cl, cr = cfg.left or { r = 60, g = 130, b = 240 }, cfg.right or { r = 242, g = 106, b = 44 } SL[1], SL[2], SL[3] = cl.r, cl.g, cl.b SLD[1], SLD[2], SLD[3] = cl.r * 66 // 100, cl.g * 66 // 100, cl.b * 66 // 100 SR[1], SR[2], SR[3] = cr.r, cr.g, cr.b SRD[1], SRD[2], SRD[3] = cr.r * 66 // 100, cr.g * 66 // 100, cr.b * 66 // 100 sleeve(lx, yc, true, SL, SLD) sleeve(rx + GW, yc, false, SR, SRD) gfx.blit_indexed(GLOVE_L, GPAL, lx, y0, GW, GH, GW, GH, true) gfx.blit_indexed(GLOVE_R, GPAL, rx, y0, GW, GH, GW, GH, true) if t >= t3 and t < t4 and abs(vy) > 0.35 then for k = 0, 2 do local x = 25 + k * 7 + (k == 1 and 1 or 0) local ya = vy > 0 and (y0 + GH + 1 + (k % 2)) or (y0 - 3 - (k % 2)) rect(x, ya, 1, 2, 255, 255, 255) end end if burst >= 0 then sparks(31, yc - 2, burst, round * 31 + floor((t - t3) / (t_shk / 2))) end end