Verified contractExactMIT
StillDerive
Address0xca5ce783125acdd36d02f1fa5044e46a5992663e
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.28
- Same code
Only this contract
Details
- Compiler
- solc 0.8.28+commit.7893614a
- 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,358,695
- Creation transaction
- 0x27df038c53…9aa17cb6b7
2 files
- src/StillDerive.solmain1.24 KB
- src/DeriveBody.sol34.35 KB
src/StillDerive.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import {DeriveBody} from "./DeriveBody.sol";
/// Module 1 of the CARNAGE still: the body's recipe, derived from the token number and its
/// face anchor (stillsol/DeriveBody.sol, unchanged). Reads its tables from the store's chunks.
contract StillDerive is DeriveBody {
function recipe(address[] calldata shared, address[] calldata dtab, uint256 token, bytes calldata anchor)
external view returns (bytes memory rc, uint256 face, int256[3][16] memory colq)
{
T memory t;
(rc, t, colq) = _recipeC(_join(shared), _join(dtab), token, anchor);
face = t.face;
}
function _join(address[] memory chunks) internal view returns (bytes memory b) {
uint256 total;
for (uint256 i; i < chunks.length; i++) total += chunks[i].code.length - 1;
b = new bytes(total);
assembly ("memory-safe") {
let d := add(b, 32)
for { let i := 0 } lt(i, mload(chunks)) { i := add(i, 1) } {
let a := mload(add(add(chunks, 32), shl(5, i)))
let n := sub(extcodesize(a), 1)
extcodecopy(a, d, 1, n)
d := add(d, n)
}
}
}
}
src/DeriveBody.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
/// @title CARNAGE fighter derivation, 27 October 2026
/// @notice A fighter's recipe from its token number and the series seed, in whole
/// numbers - exactly derive_int.py. Writes the part recipe bytes the body
/// builder reads (pack.py's parts section), then the face trailer:
/// u8 host, u8 faceIdx, i32 mid, yFace, zBase (the stored anchor), 16 x rgb.
abstract contract DeriveBody {
uint256 constant M32 = 0xffffffff;
int256 constant ONE = 1 << 24;
// ------------------------------------------------------------------ byte readers
function _u8(bytes memory b, uint256 o) internal pure returns (uint256 v) { assembly ("memory-safe") { v := byte(0, mload(add(add(b, 32), o))) } }
function _u16(bytes memory b, uint256 o) internal pure returns (uint256 v) { assembly ("memory-safe") { v := shr(240, mload(add(add(b, 32), o))) } }
function _u32(bytes memory b, uint256 o) internal pure returns (uint256 v) { assembly ("memory-safe") { v := shr(224, mload(add(add(b, 32), o))) } }
function _u64(bytes memory b, uint256 o) internal pure returns (uint256 v) { assembly ("memory-safe") { v := shr(192, mload(add(add(b, 32), o))) } }
function _i16(bytes memory b, uint256 o) internal pure returns (int256 v) { assembly ("memory-safe") { v := signextend(1, shr(240, mload(add(add(b, 32), o)))) } }
function _i32(bytes memory b, uint256 o) internal pure returns (int256 v) { assembly ("memory-safe") { v := signextend(3, shr(224, mload(add(add(b, 32), o)))) } }
function _i64(bytes memory b, uint256 o) internal pure returns (int256 v) { assembly ("memory-safe") { v := signextend(7, shr(192, mload(add(add(b, 32), o)))) } }
// ------------------------------------------------------------------ fixed point
function _mul(int256 a, int256 b) internal pure returns (int256 v) { assembly { v := sar(24, mul(a, b)) } }
function _fdiv(int256 a, int256 b) internal pure returns (int256 q) {
unchecked { q = a / b; if (a < 0 && q * b != a) q -= 1; }
}
function _sinTab(bytes memory sh, uint256 sineOff, int256 t) internal pure returns (int256 v) {
unchecked {
uint256 tt = uint256(t) & 0xffffff;
uint256 quad = tt >> 22; uint256 r = tt & 0x3fffff;
if (quad & 1 == 1) r = 0x400000 - r;
uint256 x = r * 4096; uint256 i = x >> 22; uint256 f = x & 0x3fffff;
if (i < 4096) {
int256 s0 = _i32(sh, sineOff + 4 * i); int256 s1 = _i32(sh, sineOff + 4 * i + 4);
v = s0 + ((s1 - s0) * int256(f) >> 22);
} else v = _i32(sh, sineOff + 4 * 4096);
v >>= 6;
if (quad >= 2) v = -v;
}
}
function _cosTab(bytes memory sh, uint256 sineOff, int256 t) internal pure returns (int256) { unchecked { return _sinTab(sh, sineOff, t + 0x400000); } }
// log2 of k / 2**fb, k > 0, Q24
function _log2k(uint256 k, uint256 fb) internal pure returns (int256) {
unchecked {
uint256 n = 0; for (uint256 t = k; t > 1; t >>= 1) n++;
int256 ip = int256(n) - int256(fb);
uint256 m = n <= 62 ? k << (62 - n) : k >> (n - 62);
uint256 f = 0;
for (uint256 i = 0; i < 24; i++) {
m = (m * m) >> 62;
if (m >= (2 << 62)) { m >>= 1; f |= 1 << (23 - i); }
}
return ip * ONE + int256(f);
}
}
function _lnk(uint256 k, uint256 fb) internal pure returns (int256) { return _mul(_log2k(k, fb), 11629080); } // LN2 q
function _exp(int256 y) internal pure returns (int256) {
unchecked {
int256 z = _mul(y, 24204406); // LOG2E q
int256 n = z >> 24; int256 f = z - (n << 24);
uint256 x = (uint256(f) * 195103586505167) >> 24; // LN2 in Q48
uint256 term = 1 << 48; uint256 s = term;
for (uint256 i = 1; i < 13; i++) { term = (term * x) >> 48; term /= i; s += term; }
int256 v = int256(s >> 24);
return n >= 0 ? v << uint256(n) : v >> uint256(-n);
}
}
function _isqrt(uint256 x) internal pure returns (uint256 r) {
if (x == 0) return 0;
unchecked {
r = 1 << ((_bits(x) + 1) / 2);
uint256 y = (r + x / r) / 2;
while (y < r) { r = y; y = (r + x / r) / 2; }
}
}
function _bits(uint256 x) internal pure returns (uint256 n) { while (x > 0) { n++; x >>= 1; } }
function _powq(int256 x, int256 a) internal pure returns (int256) { return x > 0 ? _exp(_mul(a, _lnk(uint256(x), 24))) : int256(0); }
// ------------------------------------------------------------------ the random stream
struct R { uint256 a; }
function _seedR(uint256 a) internal pure returns (R memory r) { r.a = a & M32; }
function _next(R memory r) internal pure returns (uint256 k) {
unchecked {
uint256 a = (r.a + 0x6D2B79F5) & M32; r.a = a;
uint256 t = ((a ^ (a >> 15)) * (1 | a)) & M32;
t = ((t + (((t ^ (t >> 7)) * (61 | t)) & M32)) & M32) ^ t;
k = (t ^ (t >> 14)) & M32;
}
}
function _pick(uint256 k, uint256 n) internal pure returns (uint256) { return (k * n) >> 32; }
function _kq(uint256 k) internal pure returns (int256) { return int256(k >> 8); }
// ------------------------------------------------------------------ the tables
struct T {
uint256 nCls; uint256 P; uint256 nFace; uint256 nNames;
uint256 series; uint256 nSeries;
uint256 cls; uint256 clsSz;
uint256[5] list; // offsets of BODIES SLABS SOLIDS SOLIDS_PT ARCS (each: u8 n, then u16s)
uint256 colours; uint256 genome; uint256 limbs; uint256 humanOrder; uint256 groups; uint256 masks;
uint256 names; uint256 human; uint256 bone; uint256 boneTail; uint256 solids; uint256 face;
// from the shared geometry tables
uint256 baser; uint256 sine; uint256[] src; uint256 RINGS; uint256 SECT; uint256 rings; uint256 sides;
}
function _tables(bytes memory sh, bytes memory dt) internal pure returns (T memory t) {
unchecked {
t.nCls = _u8(dt, 0); t.P = _u8(dt, 1); t.nFace = _u8(dt, 2); t.nNames = _u8(dt, 3); t.nSeries = _u8(dt, 4);
t.series = 5; uint256 o = 5 + t.nSeries;
t.cls = o; t.clsSz = 8 + 8 * (t.P - 1) + 60 + 6 + 1 + 40 + 48; o += t.nCls * t.clsSz;
for (uint256 i = 0; i < 5; i++) { t.list[i] = o; o += 1 + 2 * _u8(dt, o); }
t.colours = o; o += 2 + 3 * _u16(dt, o);
t.genome = o; o += 36;
t.limbs = o;
o += 1 + 8; uint256 hm = _u8(dt, o); o += 1 + 8 * (hm > 1 ? hm - 1 : 0);
o += 1 + 8; uint256 bm = _u8(dt, o); o += 1 + 8 * (bm > 1 ? bm - 1 : 0);
o += 2;
t.humanOrder = o; { uint256 n = _u8(dt, o); o += 1; for (uint256 i = 0; i < n; i++) o += 2 + _u8(dt, o + 1); }
t.groups = o; { uint256 n = _u8(dt, o); o += 1; for (uint256 i = 0; i < n; i++) o += 1 + _u8(dt, o); }
t.masks = o; o += 4;
t.names = o; o += 15 * 9;
t.human = o; o += t.nNames * 15 * 22;
t.bone = o; o += 15 * 7;
t.boneTail = o; o += 2 + 12;
t.solids = o; o += 4 + _u16(dt, o + 2) * 4 * t.nCls;
t.face = o;
// the geometry tables: where BASER, the sine table and each source begin
uint256 nP = _u16(sh, 0);
t.sides = _u8(sh, 2); t.rings = _u8(sh, 3); t.RINGS = _u8(sh, 4); t.SECT = _u8(sh, 5);
uint256 nSrc = _u16(sh, 6); uint256 nC = _u8(sh, 8);
uint256 p = 9 + nP * 52;
t.baser = p; p += 4 * nP;
t.sine = p; p += 4 * 4097;
t.src = new uint256[](nSrc);
for (uint256 i = 0; i < nSrc; i++) {
t.src[i] = p; uint256 kind = _u8(sh, p);
if (kind == 0) { uint256 mask = _u16(sh, p + 1); uint256 c = 0; while (mask > 0) { c += mask & 1; mask >>= 1; } p += 3 + 4 * c * t.RINGS * t.SECT; }
else if (kind == 1) p += 153;
else if (kind == 2) p += 149;
else p += 29 + 4 * nC * (t.rings + 2);
}
}
}
// ------------------------------------------------------------------ the fighter
struct F {
uint256 ci; uint256 c; uint256 seed; uint256 palIdx;
uint256 kT; uint256 kB; uint256 kA;
int256 thickVar; int256 thickness; int256 armBoost; int256 limbF;
uint256[] pool; uint256 faceIdx;
uint256[15] g0; int256[15] w0; uint256[15] g1; int256[15] w1; uint256[15] nmix;
int256[15] thick;
uint256[15] human; // 0 none, else name index + 1
uint256[15] bone; // 1 if a skeleton part
int256[3][15] col;
int256[6] W;
int256 bulk; bool bulkSet;
}
function _tokenKey(bytes memory dt, T memory t, uint256 id) internal pure returns (uint256 k) {
unchecked {
k = (2166136261 ^ (((id + 1) * 2654435761) & M32)) & M32;
for (uint256 i = 0; i < t.nSeries; i++) { k = (k ^ _u8(dt, t.series + i)) & M32; k = (k * 16777619) & M32; }
k = (k ^ (((id + 1) * 40503) & M32)) & M32;
k = ((k ^ (k >> 15)) * 2246822519) & M32;
k = (k ^ (k >> 13)) & M32;
}
}
function _thr(bytes memory dt, uint256 o) internal pure returns (uint256) { return _u64(dt, o); }
function _skullSlot(uint256 tok, uint256 K) internal pure returns (bool) {
unchecked {
if (K == 0) return false;
uint256 b = (tok * K) / 1000;
uint256 lo = (b * 1000 + K - 1) / K; uint256 hi = ((b + 1) * 1000 + K - 1) / K - 1;
uint256 h = (((b + 1) * 2654435761) & M32) ^ 0x534b554c;
h = ((h ^ (h >> 15)) * 2246822519) & M32;
h = (h ^ (h >> 13)) & M32;
return tok == lo + (h % (hi - lo + 1));
}
}
function _derive(bytes memory sh, bytes memory dt, T memory t, uint256 token) internal pure returns (F memory f) {
unchecked {
R memory ir = _seedR(_tokenKey(dt, t, token)); _next(ir); _next(ir);
uint256 u = _next(ir); uint256 ci = 0;
while (u >= _u64(dt, t.cls + ci * t.clsSz)) ci++;
f.ci = ci; uint256 c = t.cls + ci * t.clsSz; f.c = c;
uint256 cq = c + 8 * t.P; // the class's Q24 block
f.seed = (_next(ir) * 99999) >> 32;
f.palIdx = _pick(_next(ir), _u16(dt, t.colours) / 5 > 0 ? _u16(dt, t.colours) / 5 : 1);
f.kT = _next(ir); f.thickVar = _i32(dt, cq) + _mul(_i32(dt, cq + 4), _kq(f.kT));
f.kB = _next(ir); f.thickness = _i32(dt, cq + 8) + _mul(_i32(dt, cq + 12), _kq(f.kB));
f.kA = _next(ir); f.armBoost = _i32(dt, cq + 16) + _mul(_i32(dt, cq + 20), _kq(f.kA));
// the source pool
{
R memory sr = _seedR((f.seed * 2246822519 + 3266489917) & M32); _next(sr); _next(sr);
uint256 us = _next(sr); uint256 nEx = 0;
for (uint256 i = 0; i < t.P - 1; i++) if (us >= _u64(dt, c + 8 + 8 * i)) nEx++;
f.pool = new uint256[](t.P);
uint256 j = 0;
for (uint256 i = 0; i < t.P - nEx; i++) f.pool[j++] = _listAt(dt, t.list[0], _next(sr));
uint256 cf = cq + 60 + 6; // flags
for (uint256 i = 0; i < nEx; i++) {
uint256 lst;
if (_next(sr) < _u64(dt, cf + 1)) {
if (_next(sr) < _u64(dt, cf + 9)) lst = t.list[1];
else lst = (_u8(dt, cf) & 1 == 1 && _next(sr) < _u64(dt, cf + 17)) ? t.list[3] : t.list[2];
} else lst = t.list[4];
f.pool[j++] = _listAt(dt, lst, _next(sr));
}
}
{
R memory lr = _seedR(((f.seed * 2654435761) & M32) ^ 0x9E3779B1); _next(lr); _next(lr);
int256 uLimb = _kq(_next(lr));
int256 lf = _mul(_i32(dt, cq + 24), ONE + _mul(uLimb - ONE / 2, _i32(dt, cq + 28)));
f.limbF = lf > 838861 ? lf : int256(838861); // q(0.05)
}
// the face design: the fraction of a weighted sum in Q48
{
uint256 c48 = cq + 60 + 6 + 1 + 40;
int256 th48 = _i64(dt, c48) + ((_i64(dt, c48 + 8) * int256(f.kB)) >> 32);
int256 tv48 = _i64(dt, c48 + 16) + ((_i64(dt, c48 + 24) * int256(f.kT)) >> 32);
int256 ab48 = _i64(dt, c48 + 32) + ((_i64(dt, c48 + 40) * int256(f.kA)) >> 32);
int256 s = ((th48 * 38609922555400680) >> 48) + int256((f.kB * 25166677667699752) >> 32)
+ int256((f.kA * 17262860321664532) >> 32) + ((tv48 * 12162533743667446) >> 48)
+ int256((f.kT * 8193736572047196) >> 32) + ((ab48 * 4804777852450898) >> 48);
f.faceIdx = ((uint256(s) & ((1 << 48) - 1)) * t.nFace) >> 48;
}
_genome(dt, t, f);
_thickness(sh, dt, t, f);
_limbs(dt, t, f, token);
_colours(dt, t, f);
}
}
function _listAt(bytes memory dt, uint256 lo, uint256 k) internal pure returns (uint256) {
return _u16(dt, lo + 1 + 2 * _pick(k, _u8(dt, lo)));
}
function _groupOf(bytes memory dt, T memory t, uint256 part) internal pure returns (uint256 g, bool found) {
unchecked {
uint256 o = t.groups; uint256 n = _u8(dt, o); o += 1;
for (uint256 i = 0; i < n; i++) {
uint256 np = _u8(dt, o);
for (uint256 j = 0; j < np; j++) if (_u8(dt, o + 1 + j) == part) { g = i; found = true; }
o += 1 + np;
}
}
}
function _genome(bytes memory dt, T memory t, F memory f) internal pure {
unchecked {
R memory r = _seedR((f.seed * 2654435761) & M32);
uint256 P = f.pool.length;
uint256 whole = f.pool[_pick(_next(r), P)];
uint256 nG = _u8(dt, t.groups);
uint256[] memory gp = new uint256[](nG);
uint256 MIX = _u64(dt, t.genome); uint256 MIX35 = _u64(dt, t.genome + 8);
for (uint256 g = 0; g < nG; g++) gp[g] = _next(r) < MIX ? f.pool[_pick(_next(r), P)] : whole;
int256 blend = _i32(dt, t.genome + 24);
for (uint256 part = 0; part < 15; part++) {
(uint256 g, bool found) = _groupOf(dt, t, part);
uint256 primary = found ? gp[g] : whole;
if (_next(r) < MIX35) primary = f.pool[_pick(_next(r), P)];
uint256 other = f.pool[_pick(_next(r), P)];
int256 w = _mul(blend, ONE / 4 + (_kq(_next(r)) >> 1));
f.g0[part] = primary;
if (other == primary) { f.nmix[part] = 1; f.w0[part] = ONE; }
else { f.nmix[part] = 2; f.w0[part] = ONE - w; f.g1[part] = other; f.w1[part] = w; }
}
}
}
function _thickness(bytes memory sh, bytes memory dt, T memory t, F memory f) internal pure {
unchecked {
R memory r2 = _seedR((f.seed * 40503 + 17) & M32); _next(r2);
uint256 PS = _u64(dt, t.genome + 16); int256 q055 = _i32(dt, t.genome + 32);
uint256 o = t.groups; uint256 n = _u8(dt, o); o += 1;
for (uint256 i = 0; i < 15; i++) f.thick[i] = ONE;
for (uint256 g = 0; g < n; g++) {
if (_next(r2) < PS) _next(r2);
_next(r2);
uint256 k1 = _next(r2); if (k1 < 5) k1 = 5;
int256 u2 = _kq(_next(r2));
int256 z = _mul(int256(_isqrt(uint256(-2 * _lnk(k1, 32)) << 24)), _cosTab(sh, t.sine, u2));
int256 kk = _exp(_mul(_mul(z, f.thickVar), q055));
uint256 np = _u8(dt, o);
for (uint256 j = 0; j < np; j++) f.thick[_u8(dt, o + 1 + j)] = kk;
o += 1 + np;
}
}
}
function _limbs(bytes memory dt, T memory t, F memory f, uint256 token) internal pure {
unchecked {
uint256 L = t.limbs;
uint256 hOn = _u8(dt, L); uint256 hRate = _u64(dt, L + 1); uint256 hMax = _u8(dt, L + 9);
uint256 hTh = L + 10; uint256 o = hTh + 8 * (hMax > 1 ? hMax - 1 : 0);
uint256 bOn = _u8(dt, o); uint256 bRate = _u64(dt, o + 1); uint256 bMax = _u8(dt, o + 9);
uint256 bTh = o + 10; o = bTh + 8 * (bMax > 1 ? bMax - 1 : 0);
uint256 K = _u16(dt, o);
bool skull = _skullSlot(token, K);
uint256 nHO = _u8(dt, t.humanOrder);
uint256[] memory hoOff = new uint256[](nHO);
{ uint256 p = t.humanOrder + 1; for (uint256 i = 0; i < nHO; i++) { hoOff[i] = p; p += 2 + _u8(dt, p + 1); } }
// human limbs
R memory rh = _seedR(((f.seed * 2246822519) & M32) ^ 0x48554d41); _next(rh); _next(rh);
if (hOn == 1 && _next(rh) < hRate) {
uint256[] memory pool = new uint256[](nHO); uint256 np = 0;
for (uint256 i = 0; i < nHO; i++) if (_u8(dt, hoOff[i]) == 0 || !(bOn == 1 && skull)) pool[np++] = i;
uint256 left = _limbCount(dt, _next(rh), hTh, hMax);
while (left > 0 && np > 0) {
uint256 j = _pick(_next(rh), np); uint256 g = pool[j];
for (uint256 m = j; m + 1 < np; m++) pool[m] = pool[m + 1];
np--;
uint256 who = _pick(_next(rh), t.nNames);
uint256 cnt = _u8(dt, hoOff[g] + 1);
for (uint256 m = 0; m < cnt; m++) {
if (left == 0) break;
f.human[_u8(dt, hoOff[g] + 2 + m)] = who + 1; left--;
}
}
}
// skeleton limbs
if (bOn == 1) {
R memory rb = _seedR(((f.seed * 2654435761) & M32) ^ 0x534b454c); _next(rb); _next(rb);
if (skull) f.bone[4] = 1; // upperneck
if (_next(rb) < bRate) {
uint256[] memory pool = new uint256[](nHO); uint256 np = 0;
for (uint256 i = 0; i < nHO; i++) if (_u8(dt, hoOff[i]) == 0) pool[np++] = i;
uint256 left = _limbCount(dt, _next(rb), bTh, bMax);
if (skull) left--;
while (left > 0 && np > 0) {
uint256 j = _pick(_next(rb), np); uint256 g = pool[j];
for (uint256 m = j; m + 1 < np; m++) pool[m] = pool[m + 1];
np--;
uint256 cnt = _u8(dt, hoOff[g] + 1);
for (uint256 m = 0; m < cnt; m++) {
if (left == 0) break;
uint256 p = _u8(dt, hoOff[g] + 2 + m);
if (f.human[p] != 0) continue;
f.bone[p] = 1; left--;
}
}
}
}
}
}
function _limbCount(bytes memory dt, uint256 k, uint256 th, uint256 mx) internal pure returns (uint256 n) {
if (mx <= 1) return mx;
n = 1;
for (uint256 i = 0; i + 1 < mx; i++) if (k >= _u64(dt, th + 8 * i)) n++;
}
function _colours(bytes memory dt, T memory t, F memory f) internal pure {
unchecked {
uint256 nSet = _u16(dt, t.colours);
uint256[3][5] memory cols;
for (uint256 i = 0; i < 5; i++) {
uint256 e = t.colours + 2 + 3 * ((f.palIdx * 5 + i) % nSet);
cols[i] = [_u8(dt, e), _u8(dt, e + 1), _u8(dt, e + 2)];
}
R memory r2 = _seedR((f.seed * 40503 + 17) & M32);
uint256 anchor = _pick(_next(r2), 5);
uint256 PS = _u64(dt, t.genome + 16); int256 sp018 = _i32(dt, t.genome + 28);
bool[15] memory set_;
uint256 o = t.groups; uint256 n = _u8(dt, o); o += 1;
for (uint256 g = 0; g < n; g++) {
uint256 pk = _next(r2) < PS ? _pick(_next(r2), 5) : anchor;
int256 j = ONE + _mul(_kq(_next(r2)) - ONE / 2, sp018);
int256[3] memory cc;
for (uint256 ch = 0; ch < 3; ch++) { int256 v = int256(cols[pk][ch]) * j; cc[ch] = v < 255 * ONE ? v : 255 * ONE; }
uint256 np = _u8(dt, o);
for (uint256 m = 0; m < np; m++) { uint256 p = _u8(dt, o + 1 + m); f.col[p] = cc; set_[p] = true; }
o += 1 + np;
}
for (uint256 p = 0; p < 15; p++) if (!set_[p]) for (uint256 ch = 0; ch < 3; ch++) f.col[p][ch] = int256(cols[0][ch]) * ONE;
}
}
// ------------------------------------------------------------------ part 2: the recipe bytes
function _style(bytes memory dt, F memory f, uint256 cq) internal pure {
unchecked {
uint256 kpow = _u8(dt, cq + 60 + 5);
for (uint256 i = 0; i < 6; i++) f.W[i] = ONE;
if (kpow == 0) return;
R memory r = _seedR(((f.seed * 1597334677) & M32) ^ 0x7feb352d); _next(r); _next(r);
int256[6] memory w; int256 mx = 0;
for (uint256 i = 0; i < 6; i++) w[i] = _kq(_next(r));
for (uint256 i = 0; i < 6; i++) {
int256 v = ONE;
for (uint256 m = 0; m < kpow; m++) v = _mul(v, w[i]);
w[i] = v; if (v > mx) mx = v;
}
for (uint256 i = 0; i < 6; i++) f.W[i] = mx > 0 ? (w[i] * ONE) / mx : ONE;
}
}
function _scale(bytes memory dt, T memory t, F memory f, uint256 part) internal pure returns (int256 v) {
uint256 arm = _u16(dt, t.masks); uint256 dist = _u16(dt, t.masks + 2);
v = _mul(f.thickness, f.thick[part]);
if ((arm >> part) & 1 == 1) v = _mul(v, f.armBoost);
if ((dist >> part) & 1 == 1) v = _mul(v, f.limbF);
}
/// radiusAt(part, 0.5, 0) - for the skeleton's bulk
function _radiusMid(bytes memory sh, bytes memory dt, T memory t, F memory f, uint256 part) internal pure returns (int256 r) {
unchecked {
for (uint256 m = 0; m < f.nmix[part]; m++) {
uint256 s = m == 0 ? f.g0[part] : f.g1[part]; int256 w = m == 0 ? f.w0[part] : f.w1[part];
uint256 p = t.src[s]; uint256 kind = _u8(sh, p); int256 v;
if (kind == 0) {
uint256 mask = _u16(sh, p + 1);
if ((mask >> part) & 1 == 0) v = _i32(sh, t.baser + 4 * part);
else {
uint256 below = mask & ((1 << part) - 1); uint256 cnt = 0; while (below > 0) { cnt += below & 1; below >>= 1; }
v = _i32(sh, p + 3 + 4 * (cnt * t.RINGS * t.SECT + 2 * t.SECT));
}
} else if (kind == 1 || kind == 2) {
uint256 row = p + 1 + 4 * 14; // row 2 of 5 x 7
v = _i32(sh, row) + _i32(sh, row + 4) + _i32(sh, row + 12) + _i32(sh, row + 20);
if (v < 67109) v = 67109;
} else {
uint256 x = p + 1;
int256 xs = _i32(sh, x) + _i32(sh, x + 4) + _i32(sh, x + 12) + _i32(sh, x + 20);
if (xs < 1006633) xs = 1006633;
uint256 first = _u16(dt, t.solids);
v = _mul(_mul(xs, _i32(dt, t.solids + 4 + 4 * ((s - first) * t.nCls + f.ci))), _i32(sh, t.baser + 4 * part));
}
r += _mul(w, v);
}
}
}
function _bulk(bytes memory sh, bytes memory dt, T memory t, F memory f) internal pure returns (int256) {
if (f.bulkSet) return f.bulk;
unchecked {
int256 s = 0; int256 n = 0;
for (uint256 p = 0; p < 15; p++) {
int256 own = _i32(dt, t.bone + 7 * p + 1);
if (_u8(dt, t.bone + 7 * p) != 255 && own > 0) {
s += (_mul(_radiusMid(sh, dt, t, f, p), _scale(dt, t, f, p)) * ONE) / own; n++;
}
}
f.bulk = n > 0 ? _powq(s / n, _i32(dt, t.boneTail + 2)) : ONE;
f.bulkSet = true;
return f.bulk;
}
}
function _recipe(bytes memory sh, bytes memory dt, uint256 token, bytes memory anchor) internal pure returns (bytes memory out, T memory t) {
int256[3][16] memory col_;
(out, t, col_) = _recipeC(sh, dt, token, anchor);
}
/// the recipe, plus the 16 colours at full precision (Q24 on 0..255; [15] the face's)
function _recipeC(bytes memory sh, bytes memory dt, uint256 token, bytes memory anchor) internal pure returns (bytes memory out, T memory t, int256[3][16] memory colq) {
t = _tables(sh, dt);
F memory f = _derive(sh, dt, t, token);
uint256 cq = f.c + 8 * t.P;
_style(dt, f, cq);
out = new bytes(15 * 120 + 80);
uint256 o = 0;
out[o++] = bytes1(uint8(f.ci));
for (uint256 part = 0; part < 15; part++) o = _part(sh, dt, t, f, part, cq, out, o);
// the face trailer: host, design, the stored anchor, then the colours
uint256 host = uint8(anchor[0]);
out[o++] = bytes1(uint8(host)); out[o++] = bytes1(uint8(f.faceIdx));
for (uint256 i = 1; i < 13; i++) out[o++] = anchor[i];
for (uint256 p = 0; p < 15; p++) for (uint256 ch = 0; ch < 3; ch++) out[o++] = bytes1(uint8(uint256((f.col[p][ch] + ONE / 2) >> 24)));
// the face colour: the group colour furthest from the host's
{
int256[3] memory h = f.col[host]; int256 bd = -1; uint256 best = 0;
uint256 g = t.groups; uint256 n = _u8(dt, g); g += 1;
for (uint256 i = 0; i < n; i++) {
uint256 np = _u8(dt, g);
for (uint256 m = 0; m < np; m++) {
uint256 p = _u8(dt, g + 1 + m); int256[3] memory cc = f.col[p];
int256 dd = (cc[0] - h[0]) * (cc[0] - h[0]) + (cc[1] - h[1]) * (cc[1] - h[1]) + (cc[2] - h[2]) * (cc[2] - h[2]);
if (dd > bd) { bd = dd; best = p; }
}
g += 1 + np;
}
for (uint256 ch = 0; ch < 3; ch++) out[o++] = bytes1(uint8(uint256((f.col[best][ch] + ONE / 2) >> 24)));
for (uint256 p = 0; p < 15; p++) colq[p] = f.col[p];
colq[15] = f.col[best];
}
assembly ("memory-safe") { mstore(out, o) }
}
function _w8(bytes memory b, uint256 o, uint256 v) internal pure returns (uint256) { b[o] = bytes1(uint8(v)); return o + 1; }
function _w16(bytes memory b, uint256 o, uint256 v) internal pure returns (uint256) { b[o] = bytes1(uint8(v >> 8)); b[o + 1] = bytes1(uint8(v)); return o + 2; }
function _w32(bytes memory b, uint256 o, int256 v) internal pure returns (uint256) {
uint256 u = uint256(v) & M32;
b[o] = bytes1(uint8(u >> 24)); b[o + 1] = bytes1(uint8(u >> 16)); b[o + 2] = bytes1(uint8(u >> 8)); b[o + 3] = bytes1(uint8(u));
return o + 4;
}
function _part(bytes memory sh, bytes memory dt, T memory t, F memory f, uint256 part, uint256 cq, bytes memory out, uint256 o) internal pure returns (uint256) {
unchecked {
int256 L = _i32(sh, 9 + part * 52 + 48);
int256 ov = ((_u16(dt, t.masks) >> part) & 1 == 1) ? 2348810 + _mul(f.armBoost - ONE, 2684355) : int256(2348810); // q(0.14), q(0.16)
// a human limb
if (f.human[part] != 0) {
uint256 h = t.human + ((f.human[part] - 1) * 15 + part) * 22;
if (_u8(dt, h) == 1) {
o = _w8(out, o, 2); o = _w8(out, o, f.human[part] - 1); o = _w8(out, o, _u8(dt, h + 1));
for (uint256 i = 0; i < 5; i++) o = _w32(out, o, _i32(dt, h + 2 + 4 * i));
return o;
}
}
// a skeleton limb
if (f.bone[part] == 1) {
uint256 bo = t.bone + 7 * part; uint256 pi = _u8(dt, bo);
if (pi != 255) {
int256 r0 = _mul(_mul(_i32(dt, bo + 1), _bulk(sh, dt, t, f)), _i32(dt, t.boneTail + 6));
if (r0 > 0) {
bool head = pi == _u8(dt, t.boneTail);
int256 kk = head ? _i32(dt, t.boneTail + 10) : ONE;
o = _w8(out, o, 3); o = _w8(out, o, pi);
o = _w32(out, o, _mul(r0, kk)); o = _w32(out, o, L); o = _w32(out, o, kk);
o = _w16(out, o, uint256(int256(_i16(dt, bo + 5))) & 0xffff);
o = _w8(out, o, (head && _u8(dt, t.boneTail + 1) == 1) ? 1 : 0);
return o;
}
}
}
// the dominant source decides how the part is built
uint256 best = f.g0[part];
if (f.nmix[part] == 2 && f.w1[part] > f.w0[part]) best = f.g1[part];
uint256 kind = _u8(sh, t.src[best]);
int256 sc = _scale(dt, t, f, part);
if (kind != 0) {
o = _w8(out, o, kind == 1 ? 4 : (kind == 2 ? 5 : 6));
o = _w16(out, o, best); o = _w32(out, o, sc); o = _w32(out, o, ov);
if (kind == 3) o = _w32(out, o, _i32(sh, t.baser + 4 * part));
return o;
}
return _tube(sh, dt, t, f, part, cq, out, o, L, ov, sc);
}
}
function _tube(bytes memory sh, bytes memory dt, T memory t, F memory f, uint256 part, uint256 cq, bytes memory out, uint256 o, int256 L, int256 ov, int256 sc) internal pure returns (uint256) {
unchecked {
int256[16] memory s; // 0 e 1 g 2 amp 3 k 4 ph 5 ea 6 tw 7 bx 8 by 9 rip 10 rn 11 rp 12 girth 13 spk 14 spkOn 15 -
uint256 kSi; uint256 kRi;
{
uint256 nm = t.names + 9 * part;
uint256 key = _u32(dt, nm + 4); // the stem
uint256 flags = _u8(dt, cq + 60 + 6);
if (_u8(dt, nm + 8) == 1 && flags & 2 == 2) {
R memory pr = _seedR(((f.seed * 2654435761) & M32) ^ key ^ 0x1b873593); _next(pr);
if (_next(pr) < _u64(dt, cq + 60 + 7 + 24)) key = _u32(dt, nm);
}
R memory r = _seedR(((f.seed * 2246822519) & M32) ^ key ^ 0x27d4eb2f); _next(r); _next(r);
R memory rs = _seedR(((f.seed * 2135587861) & M32) ^ key ^ 0x165667b1); _next(rs); _next(rs);
s[0] = _mul(_mul(_kq(_next(r)), _i32(dt, cq + 32)), f.W[1]);
s[1] = _kq(_next(r));
s[2] = _mul(_mul(_kq(_next(r)), _i32(dt, cq + 36)), f.W[0]);
s[3] = int256(2 + _pick(_next(r), _u8(dt, cq + 60)));
s[4] = _kq(_next(r));
s[5] = _kq(_next(r)) >> 1;
s[6] = _mul(_mul(2 * _kq(_next(r)) - ONE, _i32(dt, cq + 40)), f.W[2]);
s[7] = _mul(_mul(2 * _kq(_next(r)) - ONE, _i32(dt, cq + 44)), f.W[3]);
s[8] = _mul(_mul(2 * _kq(_next(r)) - ONE, _i32(dt, cq + 44)), f.W[3]);
s[9] = _mul(_mul(_kq(_next(r)), _i32(dt, cq + 48)), f.W[4]);
s[10] = int256(1 + _pick(_next(r), _u8(dt, cq + 61)));
s[11] = _kq(_next(r));
int256 tg = _mul(_mul(2 * s[1] - ONE, _i32(dt, cq + 52)), f.W[5]);
s[12] = tg >= 0 ? ONE + 3 * tg : (ONE * ONE) / (ONE - 3 * tg);
s[13] = _mul(_kq(_next(r)), _i32(dt, cq + 56));
s[14] = _next(rs) < _u64(dt, cq + 60 + 7 + 32) ? int256(1) : int256(0);
kSi = _next(rs); kRi = _next(rs);
}
o = _w8(out, o, 1); o = _w32(out, o, ov);
o = _w8(out, o, f.nmix[part]);
o = _w16(out, o, f.g0[part]); o = _w32(out, o, f.w0[part]);
if (f.nmix[part] == 2) { o = _w16(out, o, f.g1[part]); o = _w32(out, o, f.w1[part]); }
o = _w32(out, o, _mul(sc, s[12]));
o = _w32(out, o, s[2] > 0 ? _mul(s[2], L) : int256(0));
o = _w8(out, o, uint256(s[3])); o = _w32(out, o, s[4]);
o = _w32(out, o, s[9] > 0 ? _mul(s[9], L) : int256(0));
o = _w8(out, o, uint256(s[10]));
o = _w32(out, o, s[11]); o = _w32(out, o, s[6]); o = _w32(out, o, _mul(s[7], L)); o = _w32(out, o, _mul(s[8], L));
o = _w8(out, o, s[0] > 0 ? 1 : 0);
o = _w32(out, o, _mul(s[0], L)); o = _w32(out, o, _mul(_mul(s[0], 9227469), L)); // q(0.55)
o = _w32(out, o, _cosTab(sh, t.sine, s[5])); o = _w32(out, o, _sinTab(sh, t.sine, s[5]));
if (s[13] > 0 && s[14] == 1) {
o = _w32(out, o, _mul(s[13], L));
uint256 R_ = t.rings + 1; uint256 sides = t.sides;
uint256 dA = _spkDiv(sides, _u8(dt, cq + 63));
uint256 sN = _u8(dt, cq + 64);
uint256 dB = sN <= 1 ? R_ + 1 : (R_ / (sN - 1) > 0 ? R_ / (sN - 1) : 1);
uint256 sp = _u8(dt, cq + 62); if (sp > 6) sp = 6; if (sp < 1) sp = 1;
o = _w8(out, o, dA); o = _w8(out, o, dB); o = _w8(out, o, _pick(kSi, dA)); o = _w8(out, o, _pick(kRi, dB));
int256 wA = ONE + _fdiv((int256(dA) * ONE / 2 - ONE) * int256(6 - sp), 5); if (wA < 8388608) wA = 8388608;
int256 wB = ONE + _fdiv((int256(dB) * ONE / 2 - ONE) * int256(6 - sp), 5); if (wB < 8388608) wB = 8388608;
o = _w32(out, o, wA); o = _w32(out, o, wB);
} else o = _w32(out, o, 0);
return o;
}
}
function _spkDiv(uint256 n, uint256 want) internal pure returns (uint256 best) {
if (n < 1) n = 1; if (want < 1) want = 1;
best = n; uint256 bd = type(uint256).max;
for (uint256 d = 2; d <= n; d++) {
if (n % d != 0) continue;
uint256 q = n / d; uint256 e = q > want ? q - want : want - q; // |n/d - want|, exact on the divisors
if (e < bd) { bd = e; best = d; }
}
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.