info = { type = "image", title = "Pool lanes", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Swimmers crawl lengths of a sunlit pool", api = 33, } local tr = ({ sk = { ["Swimmers"] = "Plavci", ["Water"] = "Voda", ["Lagoon"] = "Lagúna", ["Deep blue"] = "Hlboká modrá", ["Emerald"] = "Smaragd" }, cs = { ["Swimmers"] = "Plavci", ["Water"] = "Voda", ["Lagoon"] = "Laguna", ["Deep blue"] = "Hluboká modrá", ["Emerald"] = "Smaragd" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Swimmers"), type = "int", min = 1, max = 3, default = 2 }, { key = "water", label = T("Water"), type = "int", default = "lagoon", opts = { keys = { "lagoon", "deep", "emerald" }, T("Lagoon"), T("Deep blue"), T("Emerald") } }, } local rect, blend_rect, blend_mask = gfx.rect, gfx.blend_rect, gfx.blend_mask local floor, sin, abs, sqrt = math.floor, math.sin, math.abs, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local WATER = { lagoon = { 64, 210, 220, 36, 166, 206, 44, 184, 200, 20, 110, 150 }, deep = { 52, 150, 232, 26, 98, 190, 36, 128, 214, 14, 52, 132 }, emerald = { 70, 214, 170, 38, 164, 140, 50, 186, 148, 20, 100, 90 }, } local WC = WATER.lagoon local LANE_Y = { 14, 32, 50 } local BG = nil local MASKS, NM, ROWS = {}, 6, 32 local stage = 0 local function build_bg() local a = WC for y = 6, 57 do local t = (y - 6) / 51 local r, g, bl = floor(a[1] + (a[4] - a[1]) * t), floor(a[2] + (a[5] - a[2]) * t), floor(a[3] + (a[6] - a[3]) * t) rect(4, y, 56, 1, r, g, bl) if (y - 6) % 8 == 7 then rect(4, y, 56, 1, floor(r * 0.9), floor(g * 0.93), floor(bl * 0.96)) end end for k = 0, 6 do local x = 4 + k * 8 + 7 for y = 6, 57, 4 do local t = (y + 1.5 - 6) / 51 rect(x, y, 1, 4, floor((a[1] + (a[4] - a[1]) * t) * 0.9), floor((a[2] + (a[5] - a[2]) * t) * 0.93), floor((a[3] + (a[6] - a[3]) * t) * 0.96)) end end for i = 1, 3 do local yc = LANE_Y[i] rect(10, yc, 44, 2, a[10], a[11], a[12]) rect(8, yc - 3, 2, 8, a[10], a[11], a[12]) rect(54, yc - 3, 2, 8, a[10], a[11], a[12]) end rect(0, 0, W, 4, 226, 222, 210); rect(0, 60, W, 4, 226, 222, 210) rect(0, 4, 4, 56, 226, 222, 210); rect(60, 4, 4, 56, 226, 222, 210) for k = 0, 7 do rect(k * 8, 0, 1, 4, 206, 202, 190); rect(k * 8, 60, 1, 4, 206, 202, 190) rect(0, k * 8 + 3, 4, 1, 206, 202, 190); rect(60, k * 8 + 3, 4, 1, 206, 202, 190) end rect(3, 4, 1, 56, 176, 170, 158); rect(60, 4, 1, 56, 176, 170, 158) rect(4, 3, 56, 1, 176, 170, 158); rect(4, 60, 56, 1, 176, 170, 158) for i = 0, 3 do local y = 4 + 18 * i for x = 4, 59, 3 do if ((x - 4) // 3) % 2 == 0 then rect(x, y, 3, 2, 228, 56, 60) else rect(x, y, 3, 2, 244, 244, 244) end end end BG = gfx.snapshot(0, 0, W, H) end local function build_masks() local m = stage // 3 + 1 if m > NM then return end local part = stage % 3 local ph = (m - 1) * TAU / NM local rows = MASKS[-m] or {} MASKS[-m] = rows local y0, y1 = part * 11, part == 2 and ROWS - 1 or part * 11 + 10 for cy = y0, y1 do local v = cy * TAU / ROWS local row = {} for cx = 0, ROWS - 1 do local u = cx * TAU / ROWS local ra = 1 - abs(sin(3 * u + 1.6 * sin(2 * v + ph))) local rb = 1 - abs(sin(3 * v + 1.6 * sin(2 * u - ph))) local q = ra > rb and ra or rb q = q * q; q = q * q; q = q * q row[cx + 1] = floor(q * 255) end rows[cy + 1] = string.char(table.unpack(row, 1, ROWS)) end if part == 2 then MASKS[m] = table.concat(rows); MASKS[-m] = nil end stage = stage + 1 end local CAPS = { { 226, 52, 60 }, { 250, 214, 64 }, { 64, 190, 84 } } local SUITS = { { 40, 60, 150 }, { 230, 90, 40 }, { 240, 240, 244 } } local SKIN, SKIN_D = { 238, 196, 156 }, { 206, 160, 130 } local SW = { { lane = 2, v = 7.5, off = 0 }, { lane = 3, v = 5.2, off = 4.0 }, { lane = 1, v = 9.5, off = 9.0 }, } local XW, X0 = 58, 20 local TG, TF = 0.9, 0.6 local function bar(x0, x1, y, h, r, g, b, al) local i0, i1 = floor(x0), floor(x1) if i0 == i1 then blend_rect(i0, y, 1, h, r, g, b, (x1 - x0) * al); return end blend_rect(i0, y, 1, h, r, g, b, (1 - (x0 - i0)) * al) if i1 - i0 > 1 then if al >= 1 then rect(i0 + 1, y, i1 - i0 - 1, h, r, g, b) else blend_rect(i0 + 1, y, i1 - i0 - 1, h, r, g, b, al) end end if x1 > i1 then blend_rect(i1, y, 1, h, r, g, b, (x1 - i1) * al) end end local function seg(x, d, s0, s1, y, h, c, al) if d > 0 then bar(x - s1, x - s0, y, h, c[1], c[2], c[3], al) else bar(x + s0, x + s1, y, h, c[1], c[2], c[3], al) end end local WHITE = { 255, 255, 255 } local function draw_swimmer(i, t) local s = SW[i] local yc = LANE_Y[s.lane] local v = s.v local v1 = 2.2 * v local lg = (v1 + v) / 2 * TG local ts = (XW - X0 - lg) / v local leg = TG + ts + TF local tau = (t + s.off) % (2 * leg) local mir = tau >= leg if mir then tau = tau - leg end local xl, hl, under, flip = 0, 1, false, nil if tau < TG then xl = X0 + v1 * tau - (v1 - v) * tau * tau / (2 * TG) under = true elseif tau < TG + ts then xl = X0 + lg + v * (tau - TG) else xl, under, flip = XW, true, tau - TG - ts if flip > TF / 2 then xl, hl = 64 - X0, -1 end end local x = mir and 64 - xl or xl local hd = mir and -hl or hl local al = under and 0.42 or 1 local cap, suit = CAPS[i], SUITS[i] local kick = floor(sin(TAU * t * 3.4 + i) + 1.5) - 1 local ph = (t * 0.95 + i * 0.37) % 1 if not under then for k = 1, 4 do local wx = hd > 0 and x - 14 - 4 * k or x + 14 + 4 * k local lat = floor(1.5 + 0.9 * k) local wa = 0.5 - 0.1 * k blend_rect(floor(wx), yc - lat, 2, 1, 255, 255, 255, wa) blend_rect(floor(wx), yc + 1 + lat, 2, 1, 255, 255, 255, wa) end seg(x, hd, -2, -0.2, yc - 1, 3, WHITE, 0.55) for side = 1, 2 do local p = (ph + (side - 1) * 0.5) % 1 local sgn = side == 1 and -1 or 1 local base = sgn < 0 and yc - 3 or yc + 2 if p < 0.5 then local k = p / 0.5 local sc = 7 - 14 * k seg(x, hd, sc - 2, sc + 2.5, base + sgn * floor(1.2 * sin(3.14159 * k) + 0.5), 1, SKIN, 1) else local sc = -5 + 20 * (p - 0.5) seg(x, hd, sc - 2, sc + 2.5, base - sgn, 1, SKIN_D, 0.7) end end end seg(x, hd, 11, 14, yc - 1 + kick, 1, SKIN_D, al) seg(x, hd, 11, 14, yc + 1 - kick, 1, SKIN_D, al) seg(x, hd, 3, 11, yc - 2, 4, suit, al) seg(x, hd, 0, 3.2, yc - 1, 3, cap, al) if not under then seg(x, hd, 14, 18, yc - 1 + kick, 1, WHITE, 0.35) end if flip then local k = flip / TF local r = 2 + floor(6 * k) local wx = mir and 4 or 60 - r local a2 = 0.5 * (1 - k) blend_rect(wx, yc - r, r, 1, 255, 255, 255, a2) blend_rect(wx, yc + r, r, 1, 255, 255, 255, a2) blend_rect(wx, yc - 1, r, 3, 255, 255, 255, a2 * 0.7) end end local ORDER = {} function on_settings() WC = WATER[cfg.water or "lagoon"] or WATER.lagoon BG = nil ORDER = {} local n = cfg.n or 2 for i = 1, n do ORDER[#ORDER + 1] = i end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) if stage < NM * 3 then build_masks() end local t = frame / FPS if MASKS[NM] then local f = t * 0.5 local m = floor(f) local q = f - m blend_mask(MASKS[m % NM + 1], 220, 255, 255, 0, 0, W, H, ROWS, ROWS, 0.30 * (1 - q)) blend_mask(MASKS[(m + 1) % NM + 1], 220, 255, 255, 0, 0, W, H, ROWS, ROWS, 0.30 * q) end for k = 1, #ORDER do draw_swimmer(ORDER[k], t) end end