Skip to content
Verified contractExactMIT

ArgonautsPictureV6E

Address0x20add204fd6c4377fdffdddb8c146d88aba8298d
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,084
Creation transaction
0x8d84cab686…41160818ce

src/PictureV6E.sol

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

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

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

    function drops(uint256 tok, uint8[576] calldata own, uint256[576] calldata rgb, uint256 water, bool band)
        external
        pure
        returns (bytes memory)
    {
        unchecked {
            C memory c = _clock(tok, band);
            R memory r = _shape(own, rgb);
            (uint256[6] memory wt,) = _water(water);
            Ink memory k = Pen.open(30_000);
            uint256[] memory ds = new uint256[](576);
            uint256 nd;
            for (uint256 i = 0; i < r.nb; i++) {
                uint256 p = r.body[i];
                // forge-lint: disable-next-line(unsafe-typecast)
                if (p < 552 && !_fig(r, int256(p) + 24)) ds[nd++] = p;
            }
            if (nd < 3) {
                for (uint256 i = 0; i < r.ne; i++) {
                    ds[i] = r.edges[i];
                }
                nd = r.ne;
            }
            if (nd == 0) return "";
            int256 end = c.end;
            uint256 Z = c.Z;
            uint256 hi = wt[2];
            uint256 hi2 = wt[3];
            Ink memory cells = Pen.open(4_000);
            for (int256 j = (end + 14 * W / 10) / (W / 10); j * W / 10 < end + 7 * W; j++) {
                uint256 m = _mark();
                int256 w = j * W / 10 + PH - end;
                for (uint256 d = 0; d < 11; d++) {
                    uint256 sp = ds[_fi(_h2(d, Z + 42), nd)];
                    int256 a = w - (17 * W / 10 + _f(_h2(d, Z + 43), 28 * W / 10));
                    if (a < 0 || a > 13 * W / 10) continue;
                    // forge-lint: disable-next-line(unsafe-typecast)
                    int256 x = int256(sp % 24) * W;
                    // forge-lint: disable-next-line(unsafe-typecast)
                    int256 y = (int256(sp / 24) + 1) * W;
                    if (a < 4 * W / 10) {
                        _put(cells, r, x, y, hi2, a * 9 / 4, false);
                        if (a > W / 4) _put(cells, r, x, y + W, hi, (a - W / 4) * 4, false);
                    } else {
                        int256 f = a - 4 * W / 10;
                        int256 yy = y + W + 20 * f * f / W + 3 * f;
                        if (yy < 235 * W / 10) {
                            _dot(cells, r, x, yy, hi2, 95 * W / 100, false);
                            _put(cells, r, x, yy - W, hi, 4 * W / 10, false);
                        } else {
                            int256 fa = W / 2 - (a - W) * 5 / 3;
                            _put(cells, r, x - W, 23 * W, hi, fa, true);
                            _put(cells, r, x + W, 23 * W, hi, fa, true);
                        }
                    }
                }
                _frame(k, cells, c, j * W / 10, r);
                _free(m);
            }
            return k.close();
        }
    }
}

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.