Skip to content

Making avatars in code

The makers build avatars in code, with no player at the editor. There are three:

  • A seeded random avatar. Body, shape, colours, face, hair and an outfit with no clashes, from a seed, with a style, a colour palette and a franchise or tag filter. The same seed gives the same avatar, and a newer catalogue changes as few avatars as it can.
  • Dress like. Copy another avatar's outfit (and, if asked, its hair, face, colours and shape) onto this body. Items made for the other body become their variants for this one.
  • Outfit rules. Which items clash, which sets belong together, and a check (and a fix) for any manifest.

All of it comes from the offline catalogue: the slots, bodies, names, types and id tails of its items. There are no hand-written lists of items. It needs no ctx, no sources and no GPU.

A short example

From samples/c/makers.c, which is built with the samples:

#include "sona.h"

sona_catalogue *cat = NULL;
sona_maker *mk = NULL;
uint8_t a[SONA_MANIFEST_BYTES];
uint8_t b[SONA_MANIFEST_BYTES];
uint8_t c[SONA_MANIFEST_BYTES];
sona_random_options o;
sona_outfit_report rep;

sona_catalogue_load(NULL, "data/sona-catalogue.bin", &cat);
sona_maker_create(NULL, cat, &mk);

sona_random_defaults(&o);
o.style = SONA_STYLE_COSTUME;
o.body = 1;
/* a man in a costume, the same one for seed 42 every time */
sona_make_random(mk, 42, &o, a, NULL);

o.style = SONA_STYLE_ANY;
o.body = 2;
sona_make_random(mk, 7, &o, b, NULL);           /* a woman */
/* the woman in the man's outfit, as variants for her body */
sona_make_dress_like(mk, b, a, NULL, c, NULL);

sona_outfit_check(mk, c, &rep, NULL, 0);        /* rep.valid: no errors */

sona_maker_free(mk);
sona_catalogue_free(cat);
#include "sona.hpp"

sona::Catalogue cat("data/sona-catalogue.bin");
sona_maker *mk = nullptr;
uint8_t a[SONA_MANIFEST_BYTES];
uint8_t b[SONA_MANIFEST_BYTES];
uint8_t c[SONA_MANIFEST_BYTES];
sona_random_options o;
sona_outfit_report rep;

// sona.hpp has no maker: the C calls
sona_maker_create(nullptr, cat.raw(), &mk);

sona_random_defaults(&o);
o.style = SONA_STYLE_COSTUME;
o.body = 1;
// a man in a costume, the same one for seed 42 every time
sona_make_random(mk, 42, &o, a, nullptr);

o.style = SONA_STYLE_ANY;
o.body = 2;
sona_make_random(mk, 7, &o, b, nullptr);           // a woman
// the woman in the man's outfit, as variants for her body
sona_make_dress_like(mk, b, a, nullptr, c, nullptr);

sona_outfit_check(mk, c, &rep, nullptr, 0);        // rep.valid: no errors

// the catalogue frees itself
sona_maker_free(mk);
using var cat = SonaCatalogue.Load("data/sona-catalogue.bin");
using var mk = new SonaMaker(cat);

// a man in a costume, the same for seed 42 every time
var a = mk.Random(42,
    new SonaRandomOptions { Style = Native.SONA_STYLE_COSTUME, Body = 1 });
// a woman
var b = mk.Random(7, new SonaRandomOptions { Style = Native.SONA_STYLE_ANY,
    Body = 2 });
// the woman in the man's outfit, as variants for her body
var c = mk.DressLike(b.Manifest, a.Manifest);

var rep = mk.Check(c.Manifest);                  // rep.Valid: no errors
let bytes = std::fs::read("data/sona-catalogue.bin").unwrap_or_default();
let cat = Catalogue::from_bytes(&bytes)?;
let mk = Maker::new(&cat)?;

// a man in a costume
let a = mk.random(42, &RandomOptions { style: sona_sys::SONA_STYLE_COSTUME,
    body: 1, ..Default::default() })?;
// a woman
let b = mk.random(7, &RandomOptions { style: sona_sys::SONA_STYLE_ANY,
    body: 2, ..Default::default() })?;
// the woman in the man's outfit
let c = mk.dress_like(&b.manifest, &a.manifest, 0,
    sona_sys::SONA_MATCH_SIMILAR, false)?;

let rep = outfit_check(&c.manifest, Some(&mk))?;   // rep.valid: no errors
cat = sona.Catalogue("data/sona-catalogue.bin")
mk = sona.Maker(cat)

# a man in a costume, the same one for seed 42 every time
a, _ = mk.random(42, style="costume", body=1)
b, _ = mk.random(7, style="any", body=2)        # a woman
# the woman in the man's outfit, as variants for her body
c, _ = mk.dress_like(b, a)

rep = sona.outfit_check(c, mk)                  # its "valid": no errors

mk.close()
cat.close()

The maker

sona_result sona_maker_create(sona_ctx *ctx, const sona_catalogue *cat,
                              sona_maker **out);
void sona_maker_free(sona_maker *mk);

The maker reads the catalogue once and keeps what the calls need: per item its body fit, a 64-bit key from its id, the words of its name and how many names hold each word; the items the editor offers each body in each section; the items grouped by title (the id's last four bytes); and the matched sets. It never changes after create, so every call is safe from any number of threads at once. It reads the catalogue's strings: free the maker first. ctx may be NULL: then malloc is used and errors are codes only; with a ctx, its host allocator and sona_last_error.

It is made once from the catalogue and holds a record per item (136 bytes on 64-bit, set by the struct), plus the candidate lists and the title index.

Seeded random avatars

void sona_random_defaults(sona_random_options *o);
sona_result sona_make_random(const sona_maker *mk, uint32_t seed,
                             const sona_random_options *o,
                             uint8_t out[SONA_MANIFEST_BYTES],
                             sona_random_report *report);

A new avatar: the body, its default garments and a default face, then a shape and colours, then the slots in this order, each by chance: a costume, shirt, trousers, shoes, a hat, glasses, gloves, wristwear, earrings, a ring, a prop (with SONA_RANDOM_PROPS), the hair (when no hat covers it), eyes, eyebrows, mouth, nose, chin, ears, facial hair (men, a chance of 35%), eye shadow (women, a chance of 35%) and a facial feature (a chance of 12%). The chances are the ones sona-bench --outfits random uses (chances of 12%, 45% and 25% for a costume, a hat and glasses, and so on). An item is never picked when one already picked covers its slots, so nothing clashes; a required slot with no candidate keeps the body's default garment. o may be NULL (the defaults); report may be NULL.

sona_random_options field Meaning
size set by sona_random_defaults
body 0: the seed picks (or the base's body); 1 male; 2 female
style SONA_STYLE_ANY, _CASUAL, _SMART, _SPORTY, _COSTUME (below)
palette SONA_PALETTE_NATURAL, _VIVID, _PASTEL, _MONO, _FANTASY (below)
flags SONA_RANDOM_NO_COSTUME, _PROPS, _MATCHED_SET (the clothes of one matched set), _PLAIN (no accessories), and with a base _KEEP_FACE, _KEEP_HAIR, _KEEP_COLOURS, _KEEP_SHAPE
require, exclude SONA_ITEM_* flags every pick must have or lack, on top of offered and not hidden: require = SONA_ITEM_STOCK gives an avatar of built-in items only
skip_sections bit 1u << section per section to leave as the base has it
game the franchise filter: clothes of this game only (ASCII case folded)
text the tag filter: clothes whose name, game or type hold all these words
base a manifest to start from (with the keep flags and skip_sections); NULL: a new avatar
skin, hair, eyes + counts the host's own colours in place of the palette's
available, available_user return 0 for an item the host cannot draw (not in its packs, not licensed); it is never picked. Called on the calling thread, only for the winners

The filters (game, text, style) apply to the clothes, accessories and props; require, exclude and available to every pick. When a filter leaves a section empty, that section keeps the default (a required garment) or stays empty.

A content filter goes in through available: return sona_filter_allows(f, item), so the maker picks another allowed item instead of one the ctx would take off later. Run sona_filter_manifest_bytes on the result too, because the starter's own face and hair are not picks. The CLI and sona-mcp do both.

Styles filter clothes by the catalogue's own item types (sona_item_info.subcategory): casual (tees, hoodies, jeans, shorts, trainers, caps, ...), smart (shirts, polos, jackets, coats, suits, dresses, formal shoes, heels, brimmed hats, watches, ...), sporty (sports tees, shorts, leggings, trainers, caps, sweatbands, ...) and costume (costume pieces, helmets, costume hats, glasses and shoes, carryables), each with its own costume chance (chances of 4%, 30% and 85% for casual, smart (the suits and dresses) and costume; sporty has none).

Palettes give the skin, hair (eyebrows and facial hair follow it), eyes, lips, eye shadow and facial feature colours: natural (the CLI's skin, hair and eye tables), vivid (dyed hair, bright eyes), pastel, mono (greys) and fantasy (blue, green, gold, ... skin).

sona_random_report: the body, the seed, the matched set worn (or -1), per section the catalogue index picked (or -1), and per section how many items passed every filter.

The same seed, a newer catalogue

Every choice is a hash of (seed, purpose): the body, the shape, the colours and each slot's chance never read the catalogue. The item for a slot is the candidate with the highest hash of (seed, section, item id), rendezvous hashing, not an index into the list. So a catalogue update moves a pick only where the item picked before is gone or a new item scores higher for that seed; adding k items to a section of n moves about k/(n+k) of the picks there, and removing items changes only the avatars that wore them: every other avatar is byte for byte the same, and no body, shape or colour ever changes with the catalogue.

A new library version may change the tables (styles, palettes, chances) and so the avatars: the stability is for one library over catalogue updates.

Dress like

void sona_like_defaults(sona_like_options *o);
sona_result sona_make_dress_like(const sona_maker *mk,
                                 const uint8_t body[SONA_MANIFEST_BYTES],
                                 const uint8_t like[SONA_MANIFEST_BYTES],
                                 const sona_like_options *o,
                                 uint8_t out[SONA_MANIFEST_BYTES],
                                 sona_like_report *report);
uint32_t sona_make_variant(const sona_maker *mk, const sona_guid *item,
                           uint32_t body, uint32_t match, sona_guid *out);

out is body wearing like's parts; out may be body. The parts (SONA_LIKE_*): clothes (garments, costumes and accessories), props, hair, face (the face parts, chin, nose and ears), colours (all nine) and shape; the default is clothes and props. The body's own clothes come off first unless SONA_LIKE_LAYER, and a garment with no variant leaves its slot to the body's default, not to what it wore before. mk may be NULL: then only items both bodies wear, and custom items' pairs (sona_guid_pair), come across.

Each item goes through sona_make_variant, which answers with what this body wears for it:

Outcome When
SONA_RESOLVED_KEPT this body wears it as it is
SONA_RESOLVED_VARIANT the same item made for the other body: same title, the same name once gender words are out ("Pirate Boots - Men" and "- Women", "(Male)" and "(Female)"), or a custom item's pair
SONA_RESOLVED_SIMILAR the closest item of its title: a rare word in common (a team, a character; a word in at most 1 in 500 names, so not a colour), or the same item number (the id's second group)
SONA_RESOLVED_SUBSTITUTE with SONA_MATCH_ANY only: an item of its type from its title, else one of its type this body is offered, picked by a hash of the item's id (the same every time); never one that covers more slots
SONA_RESOLVED_COVERED resolved, then taken off by a later item of the copy
SONA_RESOLVED_DROPPED nothing fits within match

match is SONA_MATCH_SAME (variants only), SONA_MATCH_SIMILAR (the default) or SONA_MATCH_ANY. A candidate must cover a slot of the item's, at least half of a costume's slots, and the same face slot. available limits the candidates as in random.

sona_like_report: per item copied, its id, what the body wears for it and the outcome, and the counts. Over the full catalogue, 400 random avatars dressed like one of the other body (a third with SONA_MATCH_ANY): 340 kept, 675 variants, 203 similar, 171 substitutes, 331 dropped (the store items with no women's cut, under SIMILAR), 11 covered.

Outfit rules

uint32_t sona_outfit_clash(const sona_maker *mk, const sona_guid *a,
                           const sona_guid *b, uint32_t *slots);
uint32_t sona_outfit_conflicts(const sona_maker *mk,
                               const uint8_t manifest[SONA_MANIFEST_BYTES],
                               const sona_guid *item, sona_guid *out,
                               uint32_t cap);

Two items clash (SONA_CLASH_SLOT) when their slot masks share a bit: two hats, a full-body suit and a top, gloves and a ring (gloves cover the ring slot), a helmet and the hair. sona_manifest_wear takes the older one off. Two props share the prop slot and stack only with SONA_EDIT_STACK_PROPS (four at most). Face parts clash with the same face part. SONA_CLASH_BODY: no body wears both (one is made for men, the other for women). SONA_CLASH_SAME: the same id. Without a maker the bodies come from the ids alone. sona_outfit_conflicts lists what wearing an item would take off a manifest, as sona_manifest_wear with no flags would.

Matched sets

uint32_t sona_outfit_set_count(const sona_maker *mk);
sona_result sona_outfit_set_get(const sona_maker *mk, uint32_t set,
                                sona_outfit_set *out);
int32_t sona_outfit_set_of(const sona_maker *mk, const sona_guid *item);
uint32_t sona_outfit_set_items(const sona_maker *mk, uint32_t set,
                               uint32_t body, uint32_t *indexes,
                               uint32_t cap);

A set is built from the data: the offered, not stock, clothing items (costume to carryable) that share a title. A title with up to 16 such items is one set ("Fullmetal Alchemist", "Mars Rover Landing"); a larger one (a store) splits by the first word of the names that is not a gender or type word ("Kentucky" shirts, shoes and caps). A group is a set when one body wears two or more of its sections. sona_outfit_set gives the title, game, name (the shared word or the game), item count, the bodies (1 male, 2 female, 3 both), the sections and the slots; an item is in one set at most. The full catalogue has 1,674. SONA_RANDOM_MATCHED_SET dresses an avatar in one of them, and the check reports the set most worn items come from.

The check and the fix

sona_result sona_outfit_check(const sona_maker *mk,
                              const uint8_t manifest[SONA_MANIFEST_BYTES],
                              sona_outfit_report *report,
                              sona_outfit_issue *issues, uint32_t cap);
sona_result sona_outfit_fix(const sona_maker *mk,
                            uint8_t manifest[SONA_MANIFEST_BYTES],
                            sona_outfit_report *after);
const char *sona_outfit_issue_name(uint32_t kind);

Every issue of any manifest, errors first, up to cap into issues (report.issues counts them all). Each issue has its kind, severity, slots, the manifest field it is in (SONA_FIELD_*: worn[i] is i, face[i] 16 + i, chin 24, nose 25, ears 26, required[i] 28 + i, body 32, head 33, shape 34) and the items concerned.

Issue Severity Meaning
no_body error the body item is not a known body
clash error two worn items cover a slot in common
wrong_body error an item made for the other body
duplicate error one item worn twice
wrong_place error an item in a field its slots do not route to (a shirt in a face record, an eye in worn)
missing error nothing covers a slot every avatar needs: shirt, trousers, shoes, hair, mouth, eyes, eyebrows, chin, nose, ears, the head
bad_shape error height or weight is not a finite number
too_many_props error more than four props
shape_range warning height or weight outside -1 to 1 (it saturates when drawn)
stale_cache warning the last-worn cache holds an item for the other body
bad_mask warning a worn record's slot mask is not its item's
unknown warning not in the catalogue (with a maker)
props info more than one prop (a console keeps the newest)
not_offered info the editor does not list it: a hidden duplicate or not pickable
custom info a custom item (no console has it)

report.valid is 1 when there is no error; then the manifest's own IsValid holds too. Without a maker the catalogue's checks (unknown, not_offered, bodies by the catalogue) are left out. SONA_E_MANIFEST: the bytes do not parse.

sona_outfit_fix mends every error but no_body in place, with the editor's verbs: the worn items are worn again in order (a clash goes to the later item, as in the editor), duplicates go, items for the other body become their variants (SONA_MATCH_SIMILAR, else they come off), misplaced face parts and features go where they route, missing slots get the body's defaults (the cache's garment when it fits), a stale cache gets the body's own, the four newest props stay, a shape that is not a number becomes 0. after is the check after. SONA_E_ARGUMENT when the body is not known. Warnings and infos are left as they are.

The CLI and MCP

The CLI's avatar random now calls sona_make_random (new options --style, --palette, --game, --tag, --set, --props, --plain), and there are avatar dress-like, outfit check, outfit fix, outfit sets, outfit set and outfit clash (The sona command-line tool). The JSON of avatar random keeps its fields (random.picked with section, id and name; random.seed; random.body) and adds style, palette and set; the items a seed gives are not the ones the old CLI drew, since the picks are rendezvous hashes now.

sona-mcp has avatar_random, avatar_dress_like and outfit_check (with fix), only from items a source can draw (sona-mcp).

The C#, Python and Rust bindings have the whole section: SonaMaker (C#), sona.Maker, outfit_check, outfit_fix, outfit_clash (Python) and sona::Maker, outfit_check, outfit_fix, outfit_clash (Rust).

Speed

Over the full catalogue, a random avatar with its check and a dress like each take well under a millisecond. The filters walk a section's candidates once per pick (a few thousand for shirts); a text filter costs more (a word search per candidate). None of it runs in the frame: the crowd bench is not touched.

Limits

  • Colourways: the catalogue does not hold an item's colour table, so a random outfit wears each item's first colourway and dress like keeps a kept item's colours only.
  • Matched sets of a large title split by the first word of the names; a store whose names start with colours groups by colour ("Red ..."). Sets come from titles and names only; the catalogue has no set field.
  • Styles are a table over the catalogue's 70 item types; an item with no type is left out of every style but any.

This page as Markdown · All the docs as one file (llms-full.txt)