Skip to content
Verified contractExactMIT

ArgonautsPictureV6D

Address0xd0316df09a7ecb490c548ec7b05be8effe140429
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.24

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,328,083
Creation transaction
0xb399e79ba7…b2724d84a2

src/PictureV6D.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {Ink, Pen, Fixed} from "./Strokes.sol";
import {ArgonautsPictureV6Kit} from "./PictureV6Kit.sol";

/// @title ArgonautsPictureV6D
contract ArgonautsPictureV6D is ArgonautsPictureV6Kit {
    using Pen for Ink;

    struct P {
        uint256 vz;
        uint256 kind;
        uint256 fish;
        int256 t0;
        int256 len;
    }

    struct V {
        C c;
        R r;
        uint256 water;
        uint256[] kinds;
        uint256 nk;
        uint256[] fishes;
        uint256 nf;
        uint256 jcol;
        int256 cx;
        int256 cy;
        uint256[] ring;
        int256 stop;
        P[2] plan;
        uint256 np;
        uint256[128][12] ink;
        int256[42] jglow;
        uint256[42] jc;
        uint256 njc;
        int256[25] lglow;
    }

    function frames(uint256 tok, uint8[576] calldata own, uint256[576] calldata rgb, uint256 water, bool band)
        external
        pure
        returns (bytes memory)
    {
        unchecked {
            V memory v;
            v.c = _clock(tok, band);
            uint256 rr = _h2(v.c.Z, 901);
            v.np = _lt(rr, 4, 100) ? 2 : _lt(rr, 20, 100) ? 1 : 0;
            if (v.np == 0) return "";
            v.r = _shape(own, rgb);
            v.water = water;
            _cast(v, tok);
            uint256 cnt = v.r.nb == 0 ? 1 : v.r.nb;
            // forge-lint: disable-next-line(unsafe-typecast)
            v.cx = int256(v.r.sx) * W / int256(cnt);
            // forge-lint: disable-next-line(unsafe-typecast)
            v.cy = int256(v.r.sy) * W / int256(cnt);
            _ring(v, cnt);
            _plan(v);
            _glows(v);
            Ink memory o = Pen.open(260_000);
            Ink memory cells = Pen.open(8_000);
            int256 end = v.c.end;
            for (int256 j = 65; j * W / 10 < end; j++) {
                uint256 m = _mark();
                int256 u = j * W / 10 + PH;
                int256 hu = _sm(u * 10 / 15) * (W - _sm((u - end - 25 * W / 10) * 10 / 15)) / W;
                for (uint256 i = 0; i < v.np; i++) {
                    P memory p = v.plan[i];
                    int256 t = u - p.t0;
                    if (t < 0 || t > p.len) continue;
                    int256 a = 78 * W / 100 * hu / W * _cl((v.stop + 2 * W - u) / 2) / W;
                    if (p.kind == 4 || p.kind == 5 || p.kind == 7) a = a * 85 / 78 * 92 / 100;
                    if (p.kind == 6 || p.kind == 7) _fishVisit(v, cells, p, t, a);
                    else if (p.kind == 0) _crab(v, cells, p.vz, t, a);
                    else if (p.kind == 1) _star(v, cells, p.vz, t, a);
                    else if (p.kind == 2) _nudi(v, cells, p.vz, t, a);
                    else if (p.kind == 3) _horse(v, cells, p.vz, t, a);
                    else if (p.kind == 4) _jelly(v, cells, p.vz, t, a);
                    else _lure(v, cells, p.vz, t, a);
                }
                _frame(o, cells, v.c, j * W / 10, v.r);
                _free(m);
            }
            return o.close();
        }
    }

    function _order(uint256 tok, uint256 n, uint256 salt) internal pure returns (uint256[] memory o) {
        unchecked {
            o = new uint256[](n);
            uint256[] memory h = new uint256[](n);
            for (uint256 i = 0; i < n; i++) {
                o[i] = i;
                h[i] = _h2(tok, i + salt);
            }
            for (uint256 i = 1; i < n; i++) {
                uint256 j = i;
                while (j > 0 && h[o[j - 1]] > h[o[j]]) {
                    (o[j - 1], o[j]) = (o[j], o[j - 1]);
                    j--;
                }
            }
        }
    }

    function _cast(V memory v, uint256 tok) internal pure {
        unchecked {
            v.kinds = _order(tok, 6, 600);
            v.nk = 3 + _fi(_h2(tok, 610), 2);
            v.fishes = _order(tok, 5, 700);
            v.nf = 2 + _fi(_h2(tok, 611), 2);
            uint256[4] memory jc = [uint256(0xaaf0ff), 0xffb4e6, 0xc8aaff, 0xa0ffd2];
            v.jcol = jc[_fi(_h2(tok, 616), 4)];
        }
    }

    function _before(int256[2] memory a, int256[2] memory b) internal pure returns (bool) {
        unchecked {
            uint256 ga = _grp(a);
            uint256 gb = _grp(b);
            if (ga != gb) return ga < gb;
            if (ga == 1 || ga == 3) return false;
            return a[0] * b[1] - a[1] * b[0] > 0;
        }
    }

    function _grp(int256[2] memory a) internal pure returns (uint256) {
        unchecked {
            if (a[1] < 0) return 0;
            if (a[1] == 0 && a[0] >= 0) return 1;
            if (a[1] > 0) return 2;
            return 3;
        }
    }

    function _ring(V memory v, uint256 cnt) internal pure {
        unchecked {
            uint256 n = v.r.ne;
            v.ring = new uint256[](n);
            int256[2][] memory e = new int256[2][](n);
            for (uint256 i = 0; i < n; i++) {
                uint256 p = v.r.edges[i];
                // forge-lint: disable-next-line(unsafe-typecast)
                int256[2] memory d =
                    [int256(p % 24) * int256(cnt) - int256(v.r.sx), int256(p / 24) * int256(cnt) - int256(v.r.sy)];
                uint256 j = i;
                while (j > 0 && _before(d, e[j - 1])) {
                    e[j] = e[j - 1];
                    v.ring[j] = v.ring[j - 1];
                    j--;
                }
                e[j] = d;
                v.ring[j] = p;
            }
        }
    }

    function _plan(V memory v) internal pure {
        unchecked {
            int256 start = 65 * W / 10;
            v.stop = v.c.end - W / 2;
            int256 span = v.stop - start;
            for (uint256 k = 0; k < v.np; k++) {
                uint256 vz = v.c.Z * 13 + k * 977;
                uint256 kind;
                if (v.water == 1) {
                    uint256 q = _fi(_h2(k, vz), 5);
                    kind = q == 0 || q == 3 ? 4 : q == 1 ? 5 : q == 2 ? 7 : 2;
                } else if (_lt(_h2(k, vz + 1), 45, 100)) {
                    kind = 6;
                } else {
                    kind = v.kinds[_fi(_h2(k, vz + 2), v.nk)];
                }
                P memory p = v.plan[k];
                p.vz = vz;
                p.kind = kind;
                p.t0 = start + _f(_h2(k, vz + 3), span - 10 * W > W / 2 ? span - 10 * W : W / 2);
                p.len = kind == 6 ? 9 * W : kind == 4 ? 11 * W : 10 * W;
                p.fish = kind == 6 ? v.fishes[_fi(_h2(k, vz + 4), v.nf)] : 5;
            }
        }
    }

    function _glows(V memory v) internal pure {
        unchecked {
            for (int256 gy = -2; gy <= 3; gy++) {
                for (int256 gx = -3; gx <= 3; gx++) {
                    int256 d2 = gx * gx * W + gy * gy * 144 * W / 100;
                    // forge-lint: disable-next-line(unsafe-typecast)
                    v.jglow[uint256((gy + 2) * 7 + gx + 3)] = W - _sqrtW(d2) * 10 / 32;
                    // forge-lint: disable-next-line(unsafe-typecast)
                    if (d2 <= 1024 * W / 100 && d2 >= 196 * W / 100) v.jc[v.njc++] = uint256((gy + 2) * 7 + gx + 3);
                }
            }
            for (int256 gy = -2; gy <= 2; gy++) {
                for (int256 gx = -2; gx <= 2; gx++) {
                    // forge-lint: disable-next-line(unsafe-typecast)
                    v.lglow[uint256((gy + 2) * 5 + gx + 2)] = W - _sqrtW((gx * gx + gy * gy) * W) * 10 / 24;
                }
            }
        }
    }

    function _ink(V memory v, uint256 set, bytes1 ch) internal pure returns (uint256 c) {
        unchecked {
            uint256 i = uint8(ch) & 127;
            c = v.ink[set][i];
            if (c != 0) return c & 0xFFFFFF;
            c = _pack(set >= 6 ? _fishInk(set - 6, ch) : _spriteInk(set, ch, v.jcol));
            v.ink[set][i] = c | (1 << 24);
        }
    }

    function _ease(int256 x) internal pure returns (int256) {
        unchecked {
            return x * x / W * (3 * W - 2 * x) / W;
        }
    }

    function _murk(uint256 c, int256 f) internal pure returns (int256[3] memory) {
        unchecked {
            return _lerp(_cw(c), _cw(0x0e283a), f * W / 100);
        }
    }

    function _fishRows(uint256 f) internal pure returns (bytes memory rows, uint256 w) {
        unchecked {
            if (f == 0) return ("..dd..t.bdb..tbbebt.lll.", 6);
            if (f == 1) return ("..dd..t.bbd..tbbebt.bbb.", 6);
            if (f == 2) return ("..w...t.kwk..tkwkyt.kwk.", 6);
            if (f == 3) return ("t.b...bbebt.b..", 5);
            if (f == 4) return ("t......twkkwt.....", 6);
            return (".g..gssg.g..", 4);
        }
    }

    function _fishInk(uint256 f, bytes1 ch) internal pure returns (int256[3] memory) {
        unchecked {
            if (f == 5) return _cw(ch == "g" ? 0x141e32 : 0xa0ffe6);
            uint256 c;
            if (f == 0) {
                c = ch == "t" ? 0xecd660 : ch == "d" ? 0x181c32 : ch == "b" ? 0x2c62e4 : ch == "l" ? 0x508cf4 : 0x0c0c14;
            } else if (f == 1) {
                c = ch == "t" ? 0xd6dc78 : ch == "d" ? 0x2846be : ch == "b" ? 0x2eacb2 : 0x0c1016;
            } else if (f == 2) {
                c = ch == "w" ? 0xf4f2e8 : ch == "y" ? 0xece278 : 0x18181e;
            } else if (f == 3) {
                c = ch == "t" ? 0x1e46b4 : ch == "b" ? 0x3296ff : 0x0a0e1e;
            } else {
                c = ch == "t" ? 0x4682e6 : ch == "w" ? 0xd6e6ff : 0x181c30;
            }
            return _murk(c, 36);
        }
    }

    function _spriteInk(uint256 set, bytes1 ch, uint256 jcol) internal pure returns (int256[3] memory) {
        unchecked {
            if (set == 0) return _murk(ch == "c" ? 0xec7896 : ch == "b" ? 0xc44668 : 0x963250, 34);
            if (set == 1) return _murk(ch == "p" ? 0x9654aa : ch == "P" ? 0xba7ccc : 0xe2c8e8, 34);
            if (set == 2) return _murk(ch == "v" ? 0xaa5adc : ch == "V" ? 0xe6c8ff : 0xfafaff, 30);
            if (set == 3) return _murk(ch == "h" ? 0xd6b4ec : 0x28143c, 34);
            return _cw(jcol);
        }
    }

    function _sprite(
        V memory v,
        Ink memory k,
        bytes memory rows,
        uint256 w,
        uint256 set,
        int256 x,
        int256 y,
        bool flip,
        int256 a,
        bool behind
    ) internal pure {
        unchecked {
            int256 X0 = _roundI(x);
            int256 Y0 = _roundI(y);
            uint256 h = rows.length / w;
            for (uint256 r = 0; r < h; r++) {
                for (uint256 c = 0; c < w; c++) {
                    bytes1 ch = rows[r * w + c];
                    if (ch == ".") continue;
                    // forge-lint: disable-next-line(unsafe-typecast)
                    int256 X = flip ? X0 + int256(w - 1 - c) : X0 + int256(c);
                    // forge-lint: disable-next-line(unsafe-typecast)
                    _put(k, v.r, X * W, (Y0 + int256(r)) * W, _ink(v, set, ch), a, behind);
                }
            }
        }
    }

    function _side(uint256 vz) internal pure returns (int256) {
        unchecked {
            return _lt(_h2(1, vz), 1, 2) ? int256(-1) : int256(1);
        }
    }

    struct F {
        int256 side;
        int256 sx;
        int256 sy;
        int256 x;
        int256 y;
        int256 dir;
        bool behind;
        int256 T1;
        int256 T2;
        int256 T3;
    }

    function _fishVisit(V memory v, Ink memory k, P memory p, int256 t, int256 a) internal pure {
        unchecked {
            uint256 n = v.r.nr != 0 ? v.r.nr : v.r.ne;
            if (n == 0) return;
            uint256 sp = v.r.nr != 0 ? v.r.reefs[_fi(_h2(2, p.vz), n)] : v.r.edges[_fi(_h2(2, p.vz), n)];
            (bytes memory rows, uint256 w) = _fishRows(p.fish);
            F memory f;
            f.side = _side(p.vz);
            // forge-lint: disable-next-line(unsafe-typecast)
            f.sx = int256(sp % 24) * W;
            // forge-lint: disable-next-line(unsafe-typecast)
            f.sy = int256(sp / 24) * W;
            f.T1 = 16 * W / 10 + _f(_h2(4, p.vz), W);
            f.T2 = f.T1 + 15 * W / 10 + _f(_h2(5, p.vz), 2 * W);
            f.T3 = f.T2 + 2 * W;
            if (t < f.T1) {
                // forge-lint: disable-next-line(unsafe-typecast)
                int256 hx = f.sx + f.side * (int256(w) - 1) * W;
                int256 x0 = f.side > 0 ? 29 * W : -6 * W;
                int256 y0 = f.sy - 4 * W + _f(_h2(3, p.vz), 8 * W);
                int256 e = _ease(t * W / f.T1);
                f.x = x0 + (hx - x0) * e / W;
                f.y = y0 + (f.sy - y0) * e / W + Fixed.sin(6 * e) * 6 / 10;
                f.dir = -f.side;
            } else if (t < f.T2) {
                int256 dart = Fixed.sin((t - f.T1) * 9) * 10 > 3 * W ? int256(1) : int256(0);
                // forge-lint: disable-next-line(unsafe-typecast)
                f.x = f.sx + f.side * (int256(w) - 1) * W + f.side * (1 - dart) * 9 * W / 10;
                f.y = f.sy;
                f.dir = -f.side;
                int256 tm = t % (W / 2);
                // forge-lint: disable-next-line(unsafe-typecast)
                if (dart == 1 && _h2(uint256(_floorI(t * 4)), p.vz) * 4 > 3 * 4294967296) {
                    _dot(k, v.r, f.sx + f.side * (W + tm * 4), f.sy + tm * 3, 0xffc8dc, a * 8 / 10, true);
                }
            } else if (t < f.T3) {
                int256 an = (t - f.T2) / 2 * 6283 / 1000 * f.side;
                f.x = v.cx - Fixed.cos(an) * 9 * f.side;
                f.y = f.sy + Fixed.sin(an) * 3;
                f.dir = Fixed.sin(an) * f.side > 0 ? int256(1) : int256(-1);
                f.behind = Fixed.cos(an) * f.side * 10 > 2 * W;
            } else {
                int256 e = _ease(_cl((t - f.T3) * 10 / 18));
                f.x = v.cx - 9 * f.side * W - f.side * 22 * e;
                f.y = f.sy - 3 * e;
                f.dir = -f.side;
            }
            _drawFish(v, k, p.fish, rows, w, f, t, a);
        }
    }

    function _drawFish(
        V memory v,
        Ink memory k,
        uint256 fish,
        bytes memory rows,
        uint256 w,
        F memory f,
        int256 t,
        int256 a
    ) internal pure {
        unchecked {
            uint256 hh = rows.length / w;
            bool fold = _floorI(t * 5) % 2 == 1;
            int256 X0 = _roundI(f.x);
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 Y0 = _roundI(f.y) - int256(hh / 2);
            for (uint256 r = 0; r < hh; r++) {
                for (uint256 c = 0; c < w; c++) {
                    bytes1 ch = rows[r * w + c];
                    if (ch == ".") continue;
                    // forge-lint: disable-next-line(unsafe-typecast)
                    int256 cc = fold && c == 0 && ch == "t" ? int256(1) : int256(c);
                    // forge-lint: disable-next-line(unsafe-typecast)
                    int256 X = f.dir > 0 ? X0 + cc - (int256(w) - 1) : X0 - cc + (int256(w) - 1);
                    // forge-lint: disable-next-line(unsafe-typecast)
                    _put(k, v.r, X * W, (Y0 + int256(r)) * W, _ink(v, 6 + fish, ch), a, f.behind);
                }
            }
        }
    }

    function _onTop(V memory v, int256 x) internal pure returns (int256) {
        unchecked {
            int256 xi = _roundI(x);
            if (xi < 0) xi = 0;
            if (xi > 23) xi = 23;
            // forge-lint: disable-next-line(unsafe-typecast)
            uint256 tp = v.r.top[uint256(xi)];
            // forge-lint: disable-next-line(unsafe-typecast)
            return (int256(tp < 24 ? tp : 23) - 3) * W;
        }
    }

    function _crab(V memory v, Ink memory k, uint256 vz, int256 t, int256 a) internal pure {
        unchecked {
            uint256[] memory xs = new uint256[](24);
            uint256 n;
            for (uint256 x = 1; x < 22; x++) {
                uint256 tp = v.r.top[x];
                // forge-lint: disable-next-line(unsafe-typecast)
                if (tp < 24 && tp >= v.r.y0 && int256(tp) * W < v.cy) xs[n++] = x;
            }
            if (n == 0) return;
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 x0 = int256(xs[_fi(_h2(1, vz), n)]);
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 x1 = int256(xs[_fi(_h2(2, vz), n)]);
            int256 dx = x1 > x0 ? x1 - x0 : x0 - x1;
            int256 T1 = 16 * W / 10;
            int256 T2 = T1 + dx * 45 * W / 100 + W / 10;
            int256 T3 = T2 + 15 * W / 10 + _f(_h2(3, vz), 25 * W / 10);
            int256 side = x1 * W > v.cx ? int256(1) : int256(-1);
            int256 xx;
            int256 y;
            int256 fr = _floorI(t * 100 / 22) % 2;
            if (t < T1) {
                xx = x0 * W - 15 * W / 10;
                y = -3 * W + (_onTop(v, x0 * W) + 3 * W) * _ease(t * W / T1) / W;
                fr = _floorI(t * 100 / 12) % 2;
            } else if (t < T2) {
                int256 q = (t - T1) * W / (T2 - T1);
                xx = x0 * W - 15 * W / 10 + (x1 - x0) * W * _floorI(q * dx + HALF) / (dx > 1 ? dx : int256(1));
                y = _onTop(v, xx + 15 * W / 10);
            } else if (t < T3) {
                xx = x1 * W - 15 * W / 10;
                y = _onTop(v, x1 * W);
                fr = _floorI(t * 10 / 6) % 2;
            } else {
                int256 w = t - T3;
                int256 slide = w * 3 < 8 * W ? w * 3 : 8 * W;
                xx = x1 * W - 15 * W / 10 + side * slide;
                y = _onTop(v, xx + 15 * W / 10) + (w > W ? (w - W) * (w - W) / W * 10 : int256(0));
                if (y > 24 * W) return;
            }
            _sprite(v, k, fr == 1 ? bytes("c..c.bb..ll.") : bytes("c..c.bb.l..l"), 4, 0, xx, y, false, a, false);
        }
    }

    function _star(V memory v, Ink memory k, uint256 vz, int256 t, int256 a) internal pure {
        unchecked {
            uint256[] memory cols = new uint256[](24);
            uint256 n;
            for (uint256 x = 2; x < 21; x++) {
                if (v.r.top[x] < 24) cols[n++] = x;
            }
            if (n == 0) return;
            uint256 sc = cols[_fi(_h2(1, vz), n)];
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 land = int256(v.r.top[sc] + _fi(_h2(2, vz), 4)) * W;
            int256 T1 = 22 * W / 10;
            int256 T2 = T1 + 3 * W + _f(_h2(3, vz), 3 * W);
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 xs = (int256(sc) - 2) * W;
            int256 y;
            int256 fr = _floorI(t * 10 / 17) % 2;
            if (t < T1) {
                y = -5 * W + (land + 5 * W) * _ease(t * W / T1) / W;
                xs += Fixed.sin(2 * t) * 8 / 10;
            } else if (t < T2) {
                y = land + (t - T1) * 45 / 100;
            } else {
                int256 w = t - T2;
                y = land + (T2 - T1) * 45 / 100 + w * w / W * 9;
                if (y > 24 * W) return;
            }
            _sprite(
                v,
                k,
                fr == 1 ? bytes("..p...pPp.pPwPp.p.p..p.p.") : bytes("..p...pPp.pPwPp.p.p.p...p"),
                5,
                1,
                xs,
                y,
                false,
                a,
                false
            );
        }
    }

    function _nudi(V memory v, Ink memory k, uint256 vz, int256 t, int256 a) internal pure {
        unchecked {
            uint256 L = v.ring.length;
            if (L < 6) return;
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 s0 = int256(_fi(_h2(1, vz), L));
            int256 dir = _lt(_h2(2, vz), 1, 2) ? int256(1) : int256(-1);
            int256 steps = _floorI(t * 100 / 55);
            int256 fade = _cl(t * 10 / 6) * _cl((7 * W - t) * 10 / 8) / W;
            if (fade <= 0) return;
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 il = int256(L);
            // forge-lint: disable-next-line(unsafe-typecast)
            uint256 p = v.ring[uint256(((s0 + dir * steps) % il + il) % il)];
            // forge-lint: disable-next-line(unsafe-typecast)
            uint256 p2 = v.ring[uint256(((s0 + dir * (steps + 1)) % il + il) % il)];
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 x = (int256(p % 24) - 1) * W;
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 y = (int256(p / 24) - 1) * W;
            _sprite(v, k, "w.w.vvvV", 4, 2, x, y, p2 % 24 < p % 24, a * fade / W, false);
        }
    }

    function _horse(V memory v, Ink memory k, uint256 vz, int256 t, int256 a) internal pure {
        unchecked {
            int256 side = _side(vz);
            uint256[] memory mids = new uint256[](v.r.ne);
            uint256 n;
            for (uint256 i = 0; i < v.r.ne; i++) {
                uint256 p = v.r.edges[i];
                // forge-lint: disable-next-line(unsafe-typecast)
                int256 y = int256(p / 24) * W;
                // forge-lint: disable-next-line(unsafe-typecast)
                if (y > v.cy - 3 * W && y < v.cy + 4 * W && (int256(p % 24) * W - v.cx) * side > 0) mids[n++] = p;
            }
            if (n == 0) return;
            uint256 e = mids[_fi(_h2(2, vz), n)];
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 ex = int256(e % 24) * W + side * W;
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 ey = (int256(e / 24) - 3) * W;
            int256 T1 = 2 * W;
            int256 T2 = T1 + 3 * W + _f(_h2(3, vz), 2 * W);
            int256 x;
            int256 yy;
            if (t < T1) {
                int256 q = _ease(t * W / T1);
                int256 from = side > 0 ? 26 * W : -4 * W;
                x = from + (ex - from) * q / W;
                yy = ey - 4 * W + 4 * q + Fixed.sin(3 * t) / 2;
            } else if (t < T2) {
                x = ex + Fixed.sin(16 * t / 10) / 2;
                yy = ey + Fixed.sin(11 * t / 10) * 3 / 10;
            } else {
                int256 q = _ease(_cl((t - T2) * 10 / 25));
                x = ex + side * q * 10;
                yy = ey - 14 * q;
            }
            _sprite(v, k, ".hhhhe.h..hh..h", 3, 3, x, yy, side < 0, a, false);
        }
    }

    function _jellyAt(uint256 vz, int256 t) internal pure returns (int256 x, int256 y) {
        unchecked {
            bool up = _lt(_h2(1, vz), 1, 2);
            int256 x0 = 2 * W + _f(_h2(2, vz), 20 * W);
            int256 sp = 26 * W / 10 + _f(_h2(3, vz), 16 * W / 10);
            y = up ? 26 * W - t * sp / W : -4 * W + t * sp / W;
            // forge-lint: disable-next-line(unsafe-typecast)
            x = x0 + Fixed.sin(7 * t / 10 + int256(vz % 1000) * W) * 22 / 10;
        }
    }

    function _jelly(V memory v, Ink memory k, uint256 vz, int256 t, int256 a) internal pure {
        unchecked {
            (int256 x, int256 y) = _jellyAt(vz, t);
            bool front = _lt(_h2(4, vz), 1, 2);
            if (y < -6 * W || y > 28 * W) return;
            bytes memory rows = _floorI(t * 22 / 10) % 2 == 1 ? bytes(".g.ggg") : bytes("gggg.g");
            _sprite(v, k, rows, 3, 4, x - W, y, false, a * 85 / 100, !front);
            uint256 col = v.jcol;
            int256[6] memory sn;
            for (int256 i = 0; i < 6; i++) {
                // forge-lint: disable-next-line(unsafe-typecast)
                sn[uint256(i)] = Fixed.sin(3 * t + i * W) * 6 / 10;
            }
            for (int256 j = 0; j < 3; j++) {
                for (int256 s2 = 0; s2 < 4; s2++) {
                    _put(
                        k,
                        v.r,
                        // forge-lint: disable-next-line(unsafe-typecast)
                        x - W + j * W + sn[uint256(s2 + j)],
                        y + (2 + s2) * W,
                        col,
                        a * (5 - s2) / 10,
                        !front
                    );
                }
            }
            for (uint256 i = 0; i < v.njc; i++) {
                uint256 c = v.jc[i];
                // forge-lint: disable-next-line(unsafe-typecast)
                _put(
                    k,
                    v.r,
                    x + (int256(c % 7) - 3) * W,
                    y + (int256(c / 7) - 2) * W,
                    col,
                    a * 12 / 100 * v.jglow[c] / W,
                    true
                );
            }
        }
    }

    function _lure(V memory v, Ink memory k, uint256 vz, int256 t, int256 a) internal pure {
        unchecked {
            int256 side = _side(vz);
            int256 lx = v.cx + side * (8 * W + _f(_h2(2, vz), 3 * W));
            int256 ly = 5 * W + _f(_h2(3, vz), 12 * W) + Fixed.sin(13 * t / 10) * 12 / 10;
            int256 fade = _cl(t * 10 / 12) * _cl((9 * W - t) * 10 / 12) / W;
            if (fade <= 0) return;
            int256 af = a * fade / W;
            for (int256 gy = -2; gy <= 2; gy++) {
                for (int256 gx = -2; gx <= 2; gx++) {
                    int256 d2 = (gx * gx + gy * gy) * W;
                    if (d2 > 576 * W / 100) continue;
                    _put(
                        k,
                        v.r,
                        lx + gx * W,
                        ly + gy * W,
                        0xa0ffe6,
                        // forge-lint: disable-next-line(unsafe-typecast)
                        d2 < 36 * W / 100 ? af : af * 18 / 100 * v.lglow[uint256((gy + 2) * 5 + gx + 2)] / W,
                        true
                    );
                }
            }
            _put(k, v.r, lx + side * W, ly + W, 0x32505a, af * 6 / 10, true);
            _put(k, v.r, lx + 2 * side * W, ly + 2 * W, 0x32505a, af / 2, true);
            int256 mo = t > 4 * W && t < 54 * W / 10
                ? _sm((t - 4 * W) * 10 / 3) * (W - _sm((t - 51 * W / 10) * 10 / 3)) / W
                : int256(0);
            if (mo <= 0) return;
            for (int256 j = 0; j < 6; j++) {
                int256 xk = lx + side * (3 * W + j * 6 * W / 10);
                _put(k, v.r, xk, ly + (3 + j % 2) * W, 0xe6f0ec, af * mo / W * 8 / 10, true);
                _put(k, v.r, xk, ly + (6 - j % 2) * W, 0xe6f0ec, af * mo / W * 7 / 10, true);
            }
        }
    }
}

src/PictureV6Kit.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {Ink, Pen, Fixed} from "./Strokes.sol";

/// @title ArgonautsPictureV6Kit
abstract contract ArgonautsPictureV6Kit {
    using Pen for Ink;

    int256 internal constant W = 1e18;
    int256 internal constant HALF = 5e17;
    int256 internal constant LOOP = 200e18;
    int256 internal constant PH = 4e16;
    uint256 internal constant M32 = 0xFFFFFFFF;

    struct R {
        uint8[576] own;
        uint256[576] rgb;
        uint256[] body;
        uint256 nb;
        bool[576] edge;
        uint256[] edges;
        uint256 ne;
        bool[576] reef;
        uint256[] reefs;
        uint256 nr;
        uint256[24] top;
        int256[576] d;
        uint256 x0;
        uint256 x1;
        uint256 y0;
        uint256 y1;
        uint256 sx;
        uint256 sy;
        uint256 run;
    }

    struct C {
        uint256 tok;
        uint256 Z;
        int256 hold;
        int256 end;
        int256 sea0;
        int256 s0;
    }

    function _mark() internal pure returns (uint256 m) {
        assembly ("memory-safe") {
            m := mload(0x40)
        }
    }

    function _free(uint256 m) internal pure {
        assembly {
            mstore(0x40, m)
        }
    }

    function _h2(uint256 a, uint256 b) internal pure returns (uint256 h) {
        unchecked {
            h = ((a * 0x9E3779B1) & M32) ^ (((b + 0x632BE5AB) * 0x85EBCA77) & M32);
            h ^= h >> 16;
            h = (h * 0x7FEB352D) & M32;
            h ^= h >> 15;
        }
    }

    function _f(uint256 h, int256 x) internal pure returns (int256) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            return int256((h * uint256(x)) >> 32);
        }
    }

    function _fi(uint256 h, uint256 n) internal pure returns (uint256) {
        unchecked {
            return (h * n) >> 32;
        }
    }

    function _lt(uint256 h, uint256 num, uint256 den) internal pure returns (bool) {
        unchecked {
            return h * den < num * 4294967296;
        }
    }

    function _mod(int256 a, int256 m) internal pure returns (int256 r) {
        unchecked {
            r = a % m;
            if (r < 0) r += m;
        }
    }

    function _cl(int256 v) internal pure returns (int256) {
        unchecked {
            return v < 0 ? int256(0) : v > W ? W : v;
        }
    }

    function _sm(int256 v) internal pure returns (int256) {
        v = _cl(v);
        unchecked {
            return v * v / W * (3 * W - 2 * v) / W;
        }
    }

    function _floorI(int256 x) internal pure returns (int256 q) {
        assembly {
            q := sdiv(x, W)
            if and(slt(x, 0), iszero(iszero(smod(x, W)))) { q := sub(q, 1) }
        }
    }

    function _roundI(int256 x) internal pure returns (int256 q) {
        assembly {
            x := add(x, HALF)
            q := sdiv(x, W)
            if and(slt(x, 0), iszero(iszero(smod(x, W)))) { q := sub(q, 1) }
        }
    }

    function _ceilI(int256 x) internal pure returns (int256) {
        unchecked {
            return -_floorI(-x);
        }
    }

    function _sqrtW(int256 v) internal pure returns (int256) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            uint256 n = uint256(v) * uint256(W);
            if (n < 2) return int256(n);
            uint256 t = n;
            uint256 x = 2;
            if (t >= 1 << 128) {
                t >>= 128;
                x <<= 64;
            }
            if (t >= 1 << 64) {
                t >>= 64;
                x <<= 32;
            }
            if (t >= 1 << 32) {
                t >>= 32;
                x <<= 16;
            }
            if (t >= 1 << 16) {
                t >>= 16;
                x <<= 8;
            }
            if (t >= 1 << 8) {
                t >>= 8;
                x <<= 4;
            }
            if (t >= 1 << 4) {
                t >>= 4;
                x <<= 2;
            }
            if (t >= 1 << 2) x <<= 1;
            uint256 y = (x + n / x) / 2;
            while (y < x) {
                x = y;
                y = (x + n / x) / 2;
            }
            // forge-lint: disable-next-line(unsafe-typecast)
            return int256(x);
        }
    }

    function _dec(Ink memory k, int256 v, uint256 places) internal pure {
        assembly ("memory-safe") {
            function wnum(at, n) -> e {
                let l := 1
                for { let t := n } gt(t, 9) {} {
                    t := div(t, 10)
                    l := add(l, 1)
                }
                e := add(at, l)
                let q := e
                for {} 1 {} {
                    q := sub(q, 1)
                    mstore8(q, add(48, mod(n, 10)))
                    n := div(n, 10)
                    if iszero(n) { break }
                }
            }
            let at := mload(k)
            if slt(v, 0) {
                mstore8(at, 0x2d)
                at := add(at, 1)
                v := sub(0, v)
            }
            let p := exp(10, places)
            let q := div(add(mul(v, p), HALF), W)
            let whole := div(q, p)
            let fr := mod(q, p)
            if or(iszero(fr), iszero(iszero(whole))) { at := wnum(at, whole) }
            if fr {
                mstore8(at, 0x2e)
                at := add(at, 1)
                let d := div(p, 10)
                for {} fr {} {
                    mstore8(at, add(48, div(fr, d)))
                    at := add(at, 1)
                    fr := mod(fr, d)
                    d := div(d, 10)
                }
            }
            mstore(k, at)
        }
    }

    function _pct(Ink memory k, int256 u) internal pure {
        unchecked {
            _dec(k, u * 100 / 200, 4);
            k.ch(0x25);
        }
    }

    function _hex(Ink memory k, int256[3] memory c) internal pure {
        unchecked {
            uint256 rgb;
            for (uint256 i = 0; i < 3; i++) {
                int256 q = _roundI(c[i]);
                if (q < 0) q = 0;
                if (q > 255) q = 255;
                // forge-lint: disable-next-line(unsafe-typecast)
                rgb = (rgb << 8) | uint256(q);
            }
            k.hex6(rgb);
        }
    }

    function _pack(int256[3] memory c) internal pure returns (uint256 rgb) {
        unchecked {
            for (uint256 i = 0; i < 3; i++) {
                int256 q = _roundI(c[i]);
                if (q < 0) q = 0;
                if (q > 255) q = 255;
                // forge-lint: disable-next-line(unsafe-typecast)
                rgb = (rgb << 8) | uint256(q);
            }
        }
    }

    function _px(Ink memory k, uint256 p) internal pure {
        unchecked {
            k.ch(0x4d);
            k.num(p % 24);
            k.ch(0x20);
            k.num(p / 24);
            k.w("h1v1h-1z", 8);
        }
    }

    function _lerp(int256[3] memory a, int256[3] memory b, int256 f) internal pure returns (int256[3] memory o) {
        unchecked {
            for (uint256 i = 0; i < 3; i++) {
                o[i] = a[i] + (b[i] - a[i]) * f / W;
            }
        }
    }

    function _cw(uint256 v) internal pure returns (int256[3] memory o) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            o = [int256(v >> 16) * W, int256((v >> 8) & 255) * W, int256(v & 255) * W];
        }
    }

    function _min255(int256 v) internal pure returns (int256) {
        unchecked {
            return v > 255 * W ? 255 * W : v;
        }
    }

    function _bright(int256[3] memory c) internal pure returns (int256[3] memory) {
        unchecked {
            return
                [_min255(c[0] * 14 / 10 + 30 * W), _min255(c[1] * 14 / 10 + 30 * W), _min255(c[2] * 14 / 10 + 40 * W)];
        }
    }

    function _water(uint256 w) internal pure returns (uint256[6] memory c, int256 chop) {
        unchecked {
            if (w == 1) return ([uint256(0x080c1a), 0x000106, 0x28466e, 0x78a0d2, 0x78aadc, 0x96dcff], 35);
            if (w == 2) return ([uint256(0x460e20), 0x240612, 0xf09c84, 0xffdece, 0xffdece, 0xffd6d2], 45);
            if (w == 3) return ([uint256(0x2c3e46), 0x0a121a, 0x96aab4, 0xe2ecf0, 0xd2e2ec, 0xc8e2f0], 120);
            return ([uint256(0x103a52), 0x030e20, 0x78c8e4, 0xdcf8ff, 0xc8f0ff, 0xbeeeff], 45);
        }
    }

    function _clock(uint256 tok, bool band) internal pure returns (C memory c) {
        unchecked {
            c.tok = tok;
            c.Z = tok * 7919;
            c.hold = 40 * W + _f(_h2(tok, 77), 20 * W);
            c.end = c.hold + W;
            c.sea0 = band ? 4 * W : int256(0);
            c.s0 = _f(_h2(tok, 1201), 180 * W) + c.sea0;
        }
    }

    function _at(C memory c, int256 u) internal pure returns (int256) {
        unchecked {
            return _mod(-(c.s0 + u), LOOP);
        }
    }

    function _fig(R memory r, int256 p) internal pure returns (bool) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            return p >= 0 && p < 576 && r.own[uint256(p)] != 0;
        }
    }

    function _shape(uint8[576] calldata own, uint256[576] calldata rgb) internal pure returns (R memory r) {
        unchecked {
            r.body = new uint256[](576);
            r.edges = new uint256[](576);
            r.reefs = new uint256[](576);
            r.x0 = 24;
            r.y0 = 24;
            for (uint256 p = 0; p < 576; p++) {
                r.own[p] = own[p];
                r.rgb[p] = rgb[p];
                if (own[p] != 1) continue;
                uint256 x = p % 24;
                uint256 y = p / 24;
                r.body[r.nb++] = p;
                r.sx += x;
                r.sy += y;
                if (x < r.x0) r.x0 = x;
                if (x > r.x1) r.x1 = x;
                if (y < r.y0) r.y0 = y;
                if (y > r.y1) r.y1 = y;
            }
            for (uint256 i = 0; i < r.nb; i++) {
                uint256 p = r.body[i];
                // forge-lint: disable-next-line(unsafe-typecast)
                int256 q = int256(p);
                if (!_fig(r, q - 1) || !_fig(r, q + 1) || !_fig(r, q - 24) || (p < 552 && !_fig(r, q + 24))) {
                    r.edge[p] = true;
                    r.edges[r.ne++] = p;
                }
                uint256 v = rgb[p];
                uint256 cr = v >> 16;
                uint256 cg = (v >> 8) & 255;
                uint256 cb = v & 255;
                uint256 mx = cr > cg ? (cr > cb ? cr : cb) : (cg > cb ? cg : cb);
                uint256 mn = cr < cg ? (cr < cb ? cr : cb) : (cg < cb ? cg : cb);
                uint256 d = mx - mn;
                if (d < 40) continue;
                bool tan = mx == cr && cg >= cb && 14 * d <= 60 * (cg - cb) && 60 * (cg - cb) <= 52 * d;
                if (!tan) {
                    r.reef[p] = true;
                    r.reefs[r.nr++] = p;
                }
            }
            for (uint256 x = 0; x < 24; x++) {
                r.top[x] = 24;
                for (uint256 y = 0; y < 24; y++) {
                    uint256 p = y * 24 + x;
                    if (own[p] != 0 && own[p] != 4) {
                        r.top[x] = y;
                        break;
                    }
                }
            }
            _depth(r);
        }
    }

    function _depth(R memory r) internal pure {
        unchecked {
            uint256[] memory q = new uint256[](576);
            uint256 n;
            for (uint256 p = 0; p < 576; p++) {
                r.d[p] = -1;
            }
            for (uint256 i = 0; i < r.nb; i++) {
                uint256 p = r.body[i];
                // forge-lint: disable-next-line(unsafe-typecast)
                if (!_fig(r, int256(p) - 24) && p / 24 <= r.y0 + 2) {
                    r.d[p] = 0;
                    q[n++] = p;
                }
            }
            if (n == 0 && r.nb != 0) {
                r.d[r.body[0]] = 0;
                q[n++] = r.body[0];
            }
            int256[8] memory ks = [int256(-1), 1, -24, 24, -25, -23, 23, 25];
            for (uint256 i = 0; i < n; i++) {
                uint256 p0 = q[i];
                for (uint256 j = 0; j < 8; j++) {
                    // forge-lint: disable-next-line(unsafe-typecast)
                    int256 m = int256(p0) + ks[j];
                    if (m < 0 || m > 575) continue;
                    // forge-lint: disable-next-line(unsafe-typecast)
                    uint256 um = uint256(m);
                    if (r.own[um] != 1 || r.d[um] != -1) continue;
                    if ((um % 24 > p0 % 24 ? um % 24 - p0 % 24 : p0 % 24 - um % 24) > 1) continue;
                    r.d[um] = r.d[p0] + 1;
                    q[n++] = um;
                }
            }
            for (uint256 i = 0; i < r.nb; i++) {
                if (r.d[r.body[i]] == -1) r.d[r.body[i]] = 20;
            }
        }
    }

    function _put(Ink memory k, R memory r, int256 x, int256 y, uint256 c, int256 a, bool behind) internal pure {
        x = _roundI(x);
        y = _roundI(y);
        if (x < 0 || y < 0 || x > 23 || y > 23 || a <= 0) return;
        uint256 q;
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            q = uint256(y * 24 + x);
        }
        uint8 o = r.own[q];
        if (o == 4 || (behind && o != 0)) return;
        if (a > W) a = W;
        if (r.run != c | (1 << 24)) {
            if (r.run != 0) k.w("</g>", 4);
            k.w('<g fill="', 9);
            k.hex6(c);
            k.w('">', 2);
            r.run = c | (1 << 24);
        }
        assembly ("memory-safe") {
            function wnum(at, n) -> e {
                switch lt(n, 10)
                case 1 {
                    mstore8(at, add(48, n))
                    e := add(at, 1)
                }
                default {
                    mstore8(at, add(48, div(n, 10)))
                    mstore8(add(at, 1), add(48, mod(n, 10)))
                    e := add(at, 2)
                }
            }
            let at := mload(k)
            mstore(at, '<path opacity="')
            at := add(at, 15)
            let v := div(add(mul(a, 1000), HALF), W)
            let fr := mod(v, 1000)
            switch fr
            case 0 {
                mstore8(at, add(48, div(v, 1000)))
                at := add(at, 1)
            }
            default {
                if iszero(lt(v, 1000)) {
                    mstore8(at, 49)
                    at := add(at, 1)
                }
                mstore8(at, 0x2e)
                at := add(at, 1)
                let d := 100
                for {} fr {} {
                    mstore8(at, add(48, div(fr, d)))
                    at := add(at, 1)
                    fr := mod(fr, d)
                    d := div(d, 10)
                }
            }
            mstore(at, '" d="M')
            at := wnum(add(at, 6), x)
            mstore8(at, 0x20)
            at := wnum(add(at, 1), y)
            mstore(at, 'h1v1h-1"/>')
            mstore(k, add(at, 10))
        }
    }

    function _dot(Ink memory k, R memory r, int256 x, int256 y, uint256 c, int256 a, bool behind) internal pure {
        unchecked {
            int256 x0 = _floorI(x);
            int256 y0 = _floorI(y);
            int256 fx = x - x0 * W;
            int256 fy = y - y0 * W;
            _put(k, r, x0 * W, y0 * W, c, a * (W - fx) / W * (W - fy) / W * 115 / 100, behind);
            _put(k, r, (x0 + 1) * W, y0 * W, c, a * fx / W * (W - fy) / W * 115 / 100, behind);
            _put(k, r, x0 * W, (y0 + 1) * W, c, a * (W - fx) / W * fy / W * 115 / 100, behind);
            _put(k, r, (x0 + 1) * W, (y0 + 1) * W, c, a * fx / W * fy / W * 115 / 100, behind);
        }
    }

    function _frame(Ink memory o, Ink memory cells, C memory c, int256 u, R memory r) internal pure {
        unchecked {
            if (cells.at == cells.from) return;
            if (r.run != 0) {
                cells.w("</g>", 4);
                r.run = 0;
            }
            o.w('<g class="vw" style="--t:-', 26);
            _dec(o, _at(c, u), 1);
            o.w('s">', 3);
            o.put(cells.close());
            o.w("</g>", 4);
            cells.at = cells.from;
        }
    }
}

src/Strokes.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

struct Ink {
    uint256 at;
    uint256 from;
    uint256 cap;
}

library Pen {
    error TooLong();

    function open(uint256 cap) internal pure returns (Ink memory k) {
        bytes memory b = new bytes(cap + 32);
        uint256 at;
        assembly ("memory-safe") {
            at := add(b, 32)
        }
        k.at = at;
        k.from = at;
        k.cap = cap;
    }

    function put(Ink memory k, bytes memory s) internal pure {
        assembly ("memory-safe") {
            let n := mload(s)
            let at := mload(k)
            mcopy(at, add(s, 32), n)
            mstore(k, add(at, n))
        }
    }

    function w(Ink memory k, bytes32 s, uint256 n) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            mstore(at, s)
            mstore(k, add(at, n))
        }
    }

    function ch(Ink memory k, uint256 c) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            mstore8(at, c)
            mstore(k, add(at, 1))
        }
    }

    function num(Ink memory k, uint256 v) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            switch lt(v, 10)
            case 1 {
                mstore8(at, add(48, v))
                mstore(k, add(at, 1))
            }
            default {
                switch lt(v, 100)
                case 1 {
                    mstore8(at, add(48, div(v, 10)))
                    mstore8(add(at, 1), add(48, mod(v, 10)))
                    mstore(k, add(at, 2))
                }
                default {
                    let n := 1
                    for { let t := v } gt(t, 9) {} {
                        t := div(t, 10)
                        n := add(n, 1)
                    }
                    let p := add(at, n)
                    mstore(k, p)
                    for {} 1 {} {
                        p := sub(p, 1)
                        mstore8(p, add(48, mod(v, 10)))
                        v := div(v, 10)
                        if iszero(v) { break }
                    }
                }
            }
        }
    }

    function sint(Ink memory k, int256 v) internal pure {
        if (v < 0) {
            ch(k, 0x2d);
            // forge-lint: disable-next-line(unsafe-typecast)
            num(k, uint256(-v));
        } else {
            // forge-lint: disable-next-line(unsafe-typecast)
            num(k, uint256(v));
        }
    }

    function hex6(Ink memory k, uint256 rgb) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            mstore8(at, 0x23)
            let n := and(shr(20, rgb), 15)
            mstore8(add(at, 1), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(16, rgb), 15)
            mstore8(add(at, 2), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(12, rgb), 15)
            mstore8(add(at, 3), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(8, rgb), 15)
            mstore8(add(at, 4), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(4, rgb), 15)
            mstore8(add(at, 5), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(rgb, 15)
            mstore8(add(at, 6), add(add(n, 48), mul(39, gt(n, 9))))
            mstore(k, add(at, 7))
        }
    }

    function op(Ink memory k, uint256 v) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            switch iszero(v)
            case 1 {
                mstore8(at, 48)
                at := add(at, 1)
            }
            default {
                switch lt(v, 1000)
                case 0 {
                    mstore8(at, 49)
                    at := add(at, 1)
                }
                default {
                    mstore8(at, 46)
                    mstore8(add(at, 1), add(48, div(v, 100)))
                    at := add(at, 2)
                    let r := mod(v, 100)
                    if r {
                        mstore8(at, add(48, div(r, 10)))
                        at := add(at, 1)
                        r := mod(r, 10)
                        if r {
                            mstore8(at, add(48, r))
                            at := add(at, 1)
                        }
                    }
                }
            }
            mstore(k, at)
        }
    }

    function secs(Ink memory k, uint256 steps) internal pure {
        num(k, steps / 10);
        uint256 r = steps % 10;
        if (r != 0) {
            ch(k, 0x2e);
            ch(k, 48 + r);
        }
        ch(k, 0x73);
    }

    function close(Ink memory k) internal pure returns (bytes memory b) {
        uint256 used = k.at - k.from;
        if (used > k.cap) revert TooLong();
        uint256 from = k.from;
        assembly ("memory-safe") {
            b := sub(from, 32)
            mstore(b, used)
        }
    }
}

library Fixed {
    int256 internal constant WAD = 1e18;
    int256 internal constant HALF = 5e17;
    int256 internal constant PI = 3141592653589793238;
    int256 internal constant TWO_PI = 6283185307179586476;
    int256 internal constant HALF_PI = 1570796326794896619;

    function sin(int256 x) internal pure returns (int256) {
        unchecked {
            x = x % TWO_PI;
            if (x < 0) x += TWO_PI;
            bool neg;
            if (x >= PI) {
                x -= PI;
                neg = true;
            }
            if (x > HALF_PI) x = PI - x;
            int256 x2 = x * x / WAD;
            int256 term = x;
            int256 sum = x;
            term = term * x2 / WAD / 6;
            sum -= term;
            term = term * x2 / WAD / 20;
            sum += term;
            term = term * x2 / WAD / 42;
            sum -= term;
            term = term * x2 / WAD / 72;
            sum += term;
            term = term * x2 / WAD / 110;
            sum -= term;
            term = term * x2 / WAD / 156;
            sum += term;
            return neg ? -sum : sum;
        }
    }

    function cos(int256 x) internal pure returns (int256) {
        return sin(x + HALF_PI);
    }

    function round(int256 x) internal pure returns (int256 q) {
        unchecked {
            int256 v = x + HALF;
            q = v / WAD;
            if (v < 0 && v % WAD != 0) q -= 1;
        }
    }

    function op(int256 x) internal pure returns (uint256 k) {
        unchecked {
            if (x <= 0) return 0;
            // forge-lint: disable-next-line(unsafe-typecast)
            k = uint256((x * 1000 + HALF) / WAD);
            if (k > 1000) k = 1000;
        }
    }
}

struct Card {
    uint256 tok;
    uint8[7] t;
    uint256[576] px;
    uint256[128] x;
    uint256[] eyes;
    uint256[] lens;
    uint256[] red;
    uint256[] blue;
    uint256[] screen;
    uint256[] body;
    uint256[] head;
    uint256[] halo;
    uint256[] led;
    uint256[] smoke;
    uint256[] petals;
    uint256[] polyps;
    uint256[] glints;
    uint256[] crown;
    uint256 smokeRGB;
    bool dragon;
    int256 tempo;
    int256 tb0;
    int256 pv;
}

struct Wisps {
    uint256 nw;
    uint256[] full;
    uint256[] half;
    int256[] dx;
    uint256[] dy;
    uint256 any1;
    uint256 any2;
}

struct Light {
    uint256[] mask;
    uint256 count;
    uint256 cycle;
    bool own;
    uint256 loop;
    uint256 colour;
    int256 amount;
    int256 rise;
    int256 fall;
    uint256 mulC;
    uint256 mulS;
    uint256 add;
    uint256 mod;
}

/// @title Strokes
abstract contract Strokes {
    using Pen for Ink;

    int256 internal constant WAD = 1e18;

    uint256 internal constant RGB = 0xFFFFFF;
    uint256 internal constant SMK = 1 << 27;
    uint256 internal constant RED = 1 << 28;
    uint256 internal constant BLUE = 1 << 29;

    uint256 internal constant GROUND = 0;
    uint256 internal constant BODY = 1;
    uint256 internal constant CLOAK = 2;
    uint256 internal constant EYES = 4;
    uint256 internal constant MOUTH = 5;
    uint256 internal constant HEAD = 6;
    uint256 internal constant SMOKE = 7;

    function _mark() internal pure returns (uint256 m) {
        assembly ("memory-safe") {
            m := mload(0x40)
        }
    }

    function _free(uint256 m) internal pure {
        assembly {
            mstore(0x40, m)
        }
    }

    function _own(uint256 v) internal pure returns (uint256) {
        return (v >> 24) & 7;
    }

    function _chroma(uint256 c) internal pure returns (uint256) {
        unchecked {
            uint256 r = (c >> 16) & 255;
            uint256 g = (c >> 8) & 255;
            uint256 b = c & 255;
            uint256 mx = r > g ? r : g;
            if (b > mx) mx = b;
            uint256 mn = r < g ? r : g;
            if (b < mn) mn = b;
            return mx - mn;
        }
    }

    function _top(uint256 c) internal pure returns (uint256 mx) {
        uint256 r = (c >> 16) & 255;
        uint256 g = (c >> 8) & 255;
        uint256 b = c & 255;
        mx = r > g ? r : g;
        if (b > mx) mx = b;
    }

    function _lum(uint256 c) internal pure returns (uint256) {
        unchecked {
            return 299 * ((c >> 16) & 255) + 587 * ((c >> 8) & 255) + 114 * (c & 255);
        }
    }

    function _trim(uint256[] memory a, uint256 n) internal pure {
        assembly ("memory-safe") {
            mstore(a, n)
        }
    }

    function _sw(Card memory c, uint256 i) internal pure returns (int256) {
        // forge-lint: disable-next-line(unsafe-typecast)
        return int256((c.x[i] * 1e18) >> 32);
    }

    function _steps(Card memory c, uint256 p10) internal pure returns (uint256 n) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            n = uint256((int256(p10) * WAD * 2 + c.tempo) / (2 * c.tempo));
            if (n < 2) n = 2;
        }
    }

    function _from(Card memory c, uint256 p10) internal pure returns (int256) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            return (c.tb0 * 10 / int256(p10)) % WAD;
        }
    }

    function _at(int256 from, uint256 i, uint256 n) internal pure returns (int256) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            return (from + WAD * int256(i) / int256(n)) % WAD;
        }
    }

    function _ch(uint256 v, uint256 a, uint256 b) internal pure returns (uint256 o) {
        unchecked {
            o = (v * a + b * 100 + 50) / 100;
            if (o > 255) o = 255;
        }
    }

    function _scaled(uint256 rgb, uint256 a, uint256 b) internal pure returns (uint256) {
        return (_ch(rgb >> 16, a, b) << 16) | (_ch((rgb >> 8) & 255, a, b) << 8) | _ch(rgb & 255, a, b);
    }

    function _flat(uint256 n, uint256 colour) internal pure returns (uint256[] memory out) {
        unchecked {
            out = new uint256[](n);
            for (uint256 i = 0; i < n; i++) {
                out[i] = colour;
            }
        }
    }

    function _tint(Card memory c, uint256[] memory pxs, uint256 a, uint256 b)
        internal
        pure
        returns (uint256[] memory out)
    {
        unchecked {
            out = new uint256[](pxs.length);
            for (uint256 i = 0; i < pxs.length; i++) {
                out[i] = _scaled(c.px[pxs[i]] & RGB, a, b);
            }
        }
    }

    function _any(uint256[] memory v) internal pure returns (bool) {
        unchecked {
            for (uint256 i = 0; i < v.length; i++) {
                if (v[i] != 0) return true;
            }
            return false;
        }
    }

    function _anim(
        Ink memory k,
        bytes32 attr,
        uint256 attrLen,
        uint256[] memory v,
        uint256 steps,
        uint256 shift,
        bool raw
    ) internal pure {
        unchecked {
            k.w('<animate attributeName="', 24);
            k.w(attr, attrLen);
            k.w('" values="', 10);
            _vals(k, v, raw);
            k.w('" dur="', 7);
            k.secs(steps);
            k.ch(0x22);
            if (shift != 0) {
                k.w(' begin="-', 9);
                k.secs(shift);
                k.ch(0x22);
            }
            k.w(' calcMode="discrete" repeatCount', 32);
            k.w('="indefinite"/>', 15);
        }
    }

    function _vals(Ink memory k, uint256[] memory v, bool raw) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            let n := mload(v)
            let p := add(v, 32)
            for { let i := 0 } lt(i, n) { i := add(i, 1) } {
                if i {
                    mstore8(at, 0x3b)
                    at := add(at, 1)
                }
                let x := mload(add(p, shl(5, i)))
                switch raw
                case 0 {
                    switch iszero(x)
                    case 1 {
                        mstore8(at, 48)
                        at := add(at, 1)
                    }
                    default {
                        switch lt(x, 1000)
                        case 0 {
                            mstore8(at, 49)
                            at := add(at, 1)
                        }
                        default {
                            mstore8(at, 46)
                            mstore8(add(at, 1), add(48, div(x, 100)))
                            at := add(at, 2)
                            let r := mod(x, 100)
                            if r {
                                mstore8(at, add(48, div(r, 10)))
                                at := add(at, 1)
                                r := mod(r, 10)
                                if r {
                                    mstore8(at, add(48, r))
                                    at := add(at, 1)
                                }
                            }
                        }
                    }
                }
                default {
                    if gt(x, 99) {
                        mstore8(at, add(48, div(x, 100)))
                        at := add(at, 1)
                        x := mod(x, 100)
                        mstore8(at, add(48, div(x, 10)))
                        at := add(at, 1)
                        x := mod(x, 10)
                    }
                    if gt(x, 9) {
                        mstore8(at, add(48, div(x, 10)))
                        at := add(at, 1)
                        x := mod(x, 10)
                    }
                    mstore8(at, add(48, x))
                    at := add(at, 1)
                }
            }
            mstore(k, at)
        }
    }

    function _run(Ink memory k, uint256 x, uint256 y, uint256 n) internal pure {
        assembly ("memory-safe") {
            function d(at, v) -> to {
                to := at
                if gt(v, 9) {
                    mstore8(to, add(48, div(v, 10)))
                    to := add(to, 1)
                }
                mstore8(to, add(48, mod(v, 10)))
                to := add(to, 1)
            }
            let at := mload(k)
            mstore8(at, 0x4d)
            at := d(add(at, 1), x)
            mstore8(at, 0x20)
            at := d(add(at, 1), y)
            mstore8(at, 0x68)
            at := d(add(at, 1), n)
            mstore(at, "v1h-")
            at := d(add(at, 4), n)
            mstore8(at, 0x7a)
            mstore(k, add(at, 1))
        }
    }

    function _px(uint256[576] memory px, uint256 p) internal pure returns (uint256 v) {
        assembly ("memory-safe") {
            v := mload(add(px, shl(5, p)))
        }
    }

    function _paths(Ink memory k, uint256[] memory pxs, uint256[] memory cols) internal pure {
        unchecked {
            uint256 len = pxs.length;
            uint256[] memory runs = new uint256[](len);
            uint256 nr;
            uint256 i;
            while (i < len) {
                uint256 p = pxs[i];
                uint256 col = cols[i];
                uint256 n = 1;
                while (i + n < len && pxs[i + n] == p + n && (p + n) / 24 == p / 24 && cols[i + n] == col) n++;
                runs[nr++] = p | (n << 16) | (col << 32);
                i += n;
            }
            for (i = 0; i < nr; i++) {
                uint256 r = runs[i];
                if (r >> 255 != 0) continue;
                uint256 col = (r >> 32) & RGB;
                k.w('<path fill="', 12);
                k.hex6(col);
                k.w('" d="', 5);
                for (uint256 j = i; j < nr; j++) {
                    uint256 q = runs[j];
                    if (q >> 255 != 0 || ((q >> 32) & RGB) != col) continue;
                    _run(k, (q & 0xFFFF) % 24, (q & 0xFFFF) / 24, (q >> 16) & 0xFFFF);
                    runs[j] = q | (1 << 255);
                }
                k.w('"/>', 3);
            }
        }
    }

    function _shape(Ink memory k, uint256[] memory pxs, uint256 parity) internal pure {
        unchecked {
            uint256 len = pxs.length;
            uint256 i;
            while (i < len) {
                uint256 p = pxs[i];
                if (parity < 2 && p % 2 != parity) {
                    i++;
                    continue;
                }
                uint256 n = 1;
                if (parity == 2) {
                    while (i + n < len && pxs[i + n] == p + n && (p + n) / 24 == p / 24) n++;
                }
                _run(k, p % 24, p / 24, n);
                i += n;
            }
        }
    }

    function _fade(Ink memory k, uint256[] memory pxs, uint256[] memory cols, uint256[] memory v, uint256 steps)
        internal
        pure
    {
        if (pxs.length == 0 || !_any(v)) return;
        k.w('<g opacity="0">', 15);
        _anim(k, "opacity", 7, v, steps, 0, false);
        _paths(k, pxs, cols);
        k.w("</g>", 4);
    }

    function _lights(Ink memory k, Card memory c, Light memory o) internal pure {
        unchecked {
            uint256 nc = o.own ? _steps(c, o.cycle) : o.cycle;
            uint256[] memory xs = new uint256[](o.loop);
            uint256[] memory ys = new uint256[](o.loop);
            uint256[] memory env = new uint256[](nc);
            for (uint256 s = 0; s < o.count; s++) {
                // forge-lint: disable-next-line(unsafe-typecast)
                int256 c0 = (o.own ? c.tb0 * 10 / int256(o.cycle) : int256(0)) + WAD * int256(s) / int256(o.count);
                // forge-lint: disable-next-line(unsafe-typecast)
                uint256 first = uint256(c0 / WAD);
                int256 fr = c0 % WAD;
                // forge-lint: disable-next-line(unsafe-typecast)
                uint256 sh = uint256(fr * int256(nc) / WAD);
                for (uint256 j = 0; j < o.loop; j++) {
                    uint256 p = o.mask[(c.x[((first + j) * o.mulC + s * o.mulS + o.add) % o.mod] * o.mask.length) >> 32];
                    xs[j] = p % 24;
                    ys[j] = p / 24;
                }
                for (uint256 m = 0; m < nc; m++) {
                    int256 f = _at(fr, m, nc);
                    int256 e = f < o.rise ? f * WAD / o.rise : WAD - (f - o.rise) * WAD / o.fall;
                    env[m] = Fixed.op(e * o.amount / 1000);
                }
                k.w('<rect width="1" height="1" fill=', 32);
                k.w('"', 1);
                k.hex6(o.colour);
                k.w('" opacity="0">', 14);
                _anim(k, "x", 1, xs, nc * o.loop, sh, true);
                _anim(k, "y", 1, ys, nc * o.loop, sh, true);
                _anim(k, "opacity", 7, env, nc, 0, false);
                k.w("</rect>", 7);
            }
        }
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "forge-std/=lib/forge-std/src/",
    "ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.