# C API: Game features

Joints by name, pose overrides, clip crossfades and queues, a collision capsule, expressions, clip events and root motion. Like every avatar call they belong to the ctx's owner thread. Calls that return a `sona_result` give `SONA_E_ARGUMENT` for a NULL avatar or pointer. `sona_joint_world`, `sona_avatar_override`, `_queue`, `_capsule`, `_expression` and `_expression_counts` also give `SONA_E_DESTROYED` for an avatar that is being removed.

`sona_capsule` holds `a[3]`, `b[3]` and `radius`: 28 bytes of floats, world metres.

`sona_clip_event` holds `kind`, `mark`, `clip` and `time`, 16 bytes:

| Field | Meaning |
|---|---|
| `kind` | `SONA_CLIP_STARTED` (1), `SONA_CLIP_LOOPED` (2), `SONA_CLIP_ENDED` (3), `SONA_CLIP_FOOT_LEFT` (4), `SONA_CLIP_FOOT_RIGHT` (5), `SONA_CLIP_MARK` (6) |
| `mark` | The id given to `sona_avatar_mark`, for MARK events; else 0 |
| `clip` | The `sona_clip_get` index of the clip; -1 for a prop's clip or a raw clip id |
| `time` | Seconds into the clip |

## sona_joint_find

```c
int32_t sona_joint_find(const char *name);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `name` | `const char *` | A joint's name | required |

**Returns** `int32_t`.

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
sona_mask_preset(SONA_MASK_UPPER_BODY, m);
sona_mask_branch(m, sona_joint_find("Spine"), 0.5f);    /* half the wave in the spine ... */
sona_mask_branch(m, sona_joint_find("Chest"), 1.0f);    /* ... all of it from the chest up */
```

A joint's number from its name: a humanoid name (`Hips`, `Spine`, `Chest`, `Neck`, `Head`, `LeftUpperArm`, ... `PropLeft`, `PropRight`), `shape_NN` for a body-shape joint, or `joint_NN` for any joint by number. -1 when the name is unknown or NULL.

## sona_joint_name

```c
const char *sona_joint_name(uint32_t joint);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `joint` | `uint32_t` | A joint index, from `sona_joint_find` | - |

**Returns** `const char *`.

**Example**:

```c
const char * x_got = sona_joint_name(0);
printf("sona_joint_name: %s\n", x_got ? "a value" : "none");
```

The name of joints 0 to 70: the humanoid name, or `shape_NN`. NULL past the last joint. The string is static.

## sona_joint_world

```c
sona_result sona_joint_world(const sona_avatar *a, uint32_t joint, float out[16]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `joint` | `uint32_t` | A joint index, from `sona_joint_find` | - |
| `out` | `float [16]` | Receives a row-major 4x4 matrix | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Cookbook: recipes for game moments](../../cookbook.md)):

```c
float hand[16];
if (sona_joint_world(avatar, (uint32_t)sona_joint_find("RightHand"), hand) == SONA_OK) {
  float x = hand[12], y = hand[13], z = hand[14];
  (void)x; (void)y; (void)z;                       /* your object's position */
}
```

The joint's world matrix at the last `sona_update`: row-vector, metres, with the avatar's placement applied. `SONA_E_ARGUMENT` for a joint past 70. `SONA_E_NOT_READY` before the avatar is READY and posed. A sprite (see crowds) keeps the last pose computed before it became one.

## sona_avatar_override

```c
sona_result sona_avatar_override(sona_avatar *a, uint32_t joint, const float rotation[4], float weight,
                                 uint32_t mode);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `joint` | `uint32_t` | A joint index, from `sona_joint_find` | - |
| `rotation` | `const float [4]` | A unit quaternion (x, y, z, w); NULL clears the joint | required |
| `weight` | `float` | A weight from 0 to 1 | - |
| `mode` | `uint32_t` | `SONA_OVERRIDE_REPLACE` or `SONA_OVERRIDE_ADD` | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
float x_rotation[4];
memset(x_rotation, 0, sizeof x_rotation);
sona_result result = sona_avatar_override(avatar, 0, x_rotation, 0.5f, 0);
if (result != SONA_OK) printf("sona_avatar_override: %s\n", sona_result_name(result));
```

A rotation applied after the clip each frame, for look-at and aiming. `rotation` is a quaternion (x, y, z, w); it is normalised, and a zero or non-finite one is `SONA_E_ARGUMENT`. `mode` `SONA_OVERRIDE_REPLACE` (0) sets the joint's rotation from its rest orientation; `SONA_OVERRIDE_ADD` (1) rotates on top of the clip's. `weight` blends from the clip, 0 to 1 (above 1 counts as 1). A weight of 0 or a NULL `rotation` clears that joint. Joints the clip does not move are not affected while no clip plays. `SONA_E_ARGUMENT` for a joint past 70 or another mode.

## sona_avatar_clear_overrides

```c
void sona_avatar_clear_overrides(sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |

**Returns** Nothing.

**Example**:

```c
sona_avatar_clear_overrides(avatar);
```

Clears every override of the avatar. NULL is ignored.

## sona_avatar_crossfade

```c
sona_result sona_avatar_crossfade(sona_avatar *a, const char *clip, const sona_play_options *opts, float seconds);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `clip` | `const char *` | A clip key or id | required |
| `opts` | `const sona_play_options *` | The options, a [`sona_play_options`](../fields.md#sona_play_options) | required |
| `seconds` | `float` | Seconds to blend from the current pose; 0: a cut | `0`: a cut |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Getting started](../../getting-started.md)):

```c
sona_placement to = { { 1.0f, 0.0f, -2.0f }, 90.0f, 1.0f };
sona_avatar_move(avatar, &to, 0.5f);                  /* over half a second */
sona_avatar_crossfade(avatar, "GenericWave", NULL, 0.2f);
```

`sona_avatar_play`, blending from the current pose over `seconds`. 0, a negative or a non-finite value is a cut. It clears the queue. It returns what `sona_avatar_play` returns, so `SONA_E_ARGUMENT` for an item preview.

## sona_avatar_queue

```c
sona_result sona_avatar_queue(sona_avatar *a, const char *clip, const sona_play_options *opts, float fade_seconds);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `clip` | `const char *` | A clip key or id | required |
| `opts` | `const sona_play_options *` | The options, a [`sona_play_options`](../fields.md#sona_play_options) | `NULL`: the defaults |
| `fade_seconds` | `float` | Seconds to blend over | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_LIMIT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Cookbook: recipes for game moments](../../cookbook.md)):

```c
/* once: loop 0, speed 1, from the start */
sona_play_options once = { 0, 1.0f, 0.0f };
sona_avatar_crossfade(avatar, "GenericWave", &once, 0.2f);
sona_avatar_queue(avatar, "idle", NULL, 0.3f);
```

Plays `clip` when the current one ends, blending over `fade_seconds` (0: a cut). A looping clip never ends, so queue after a non-looping one. NULL `opts` takes the defaults. Up to `SONA_QUEUE_MAX` clips wait; `SONA_E_LIMIT` past that. `SONA_E_ARGUMENT` for a NULL or over-long clip name, or an item preview. `sona_avatar_play` and `sona_avatar_crossfade` clear the queue.

## sona_avatar_queued

```c
uint32_t sona_avatar_queued(const sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_avatar_queued(avatar);
printf("sona_avatar_queued: %lld\n", (long long)x_got);
```

How many clips are waiting in the queue. 0 for NULL.

## sona_avatar_capsule

```c
sona_result sona_avatar_capsule(const sona_avatar *a, sona_capsule *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `out` | `sona_capsule *` | Receives the [`sona_capsule`](../fields.md#sona_capsule) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_capsule x_capsule;
memset(&x_capsule, 0, sizeof x_capsule);
sona_result result = sona_avatar_capsule(avatar, &x_capsule);
if (result != SONA_OK) printf("sona_avatar_capsule: %s\n", sona_result_name(result));
```

A vertical capsule around the posed body, in world space, for collisions: the segment `a`..`b` and its `radius`, scaled with the avatar. `SONA_E_NOT_READY` before the avatar is READY and posed.

## sona_avatar_expression

```c
sona_result sona_avatar_expression(sona_avatar *a, const uint8_t values[5]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `values` | `const uint8_t [5]` | The shape of the mouth, both eyebrows and both eyes; 255 leaves one as it is | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Cookbook: recipes for game moments](../../cookbook.md)):

```c
uint8_t face[5] = { 6, 255, 255, 255, 255 };     /* mouth 6 is happy; brows and eyes from the clip */
sona_avatar_expression(avatar, face);
```

Sets the face. `values` are mouth, left eyebrow, right eyebrow, left eye, right eye, each a shape index (0 is the face's neutral shape) or 255 to leave that part as the clip has it. NULL hands the whole face back to the clip. The face texture is baked again when the shapes change; each combination is baked once and cached. `sona_pose_data.expression` reports what is shown.

Mouth shapes 0 to 6 are moods: neutral, sad, angry, confused, laughing, shocked, happy. 7 to 13 are speech shapes for lip sync: `SONA_MOUTH_O` (7), `SONA_MOUTH_AI`, `SONA_MOUTH_EE`, `SONA_MOUTH_FV`, `SONA_MOUTH_W`, `SONA_MOUTH_L`, `SONA_MOUTH_DTH` (13). `SONA_MOUTH_NEUTRAL` is 0. A sprite does not show expressions.

## sona_avatar_expression_counts

```c
sona_result sona_avatar_expression_counts(const sona_avatar *a, uint32_t counts[3]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `counts` | `uint32_t [3]` | Receives how many mouth, eyebrow and eye shapes the avatar has | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
uint32_t x_counts[3];
memset(x_counts, 0, sizeof x_counts);
sona_result result = sona_avatar_expression_counts(avatar, x_counts);
if (result != SONA_OK) printf("sona_avatar_expression_counts: %s\n", sona_result_name(result));
```

How many shapes this face has: `counts[0]` mouth, `counts[1]` eyebrows, `counts[2]` eyes. 0 for a part the face lacks. `SONA_E_NOT_READY` before READY.

## sona_avatar_clip_events

```c
uint32_t sona_avatar_clip_events(sona_avatar *a, sona_clip_event *out, uint32_t max);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `out` | `sona_clip_event *` | Receives the [`sona_clip_event`](../fields.md#sona_clip_event) | required |
| `max` | `uint32_t` | How many entries `out` holds | - |

**Returns** `uint32_t`.

**Example**:

```c
sona_clip_event x_clip_event;
memset(&x_clip_event, 0, sizeof x_clip_event);
uint32_t x_got = sona_avatar_clip_events(avatar, &x_clip_event, 1);
printf("sona_avatar_clip_events: %lld\n", (long long)x_got);
```

Moves up to `max` events into `out`, oldest first, and returns how many. They are what happened on the avatar's clip since the last call. 0 for NULL arguments or `max` 0. Footfalls come from the evaluated pose, so an avatar that is not posed (culled, a sprite, or not READY) has none. Up to `SONA_CLIP_EVENT_MAX` are held; past that the oldest go.

## sona_avatar_mark

```c
sona_result sona_avatar_mark(sona_avatar *a, float seconds, uint32_t id);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `seconds` | `float` | The clip time the mark sits at, seconds | - |
| `id` | `uint32_t` | An id of your own, given back with the event | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_LIMIT` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_result result = sona_avatar_mark(avatar, 0.5f, 0);
if (result != SONA_OK) printf("sona_avatar_mark: %s\n", sona_result_name(result));
```

A MARK event with this `id` each time the current clip passes `seconds`. Marks go when another clip starts. `SONA_E_ARGUMENT` for a negative or NaN time; `SONA_E_LIMIT` past `SONA_MARK_MAX` marks.

## sona_avatar_clear_marks

```c
void sona_avatar_clear_marks(sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |

**Returns** Nothing.

**Example**:

```c
sona_avatar_clear_marks(avatar);
```

Removes the avatar's marks. NULL is ignored.

## sona_avatar_root_motion

```c
sona_result sona_avatar_root_motion(sona_avatar *a, uint32_t mode);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `mode` | `uint32_t` | `SONA_ROOT_MOTION_OFF`, `_EXTRACT` or `_APPLY` | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Animation layers, masks, blend spaces and state machines](../../guides/layers.md)):

```c
float upper[SONA_JOINT_COUNT];
sona_layer_options lo;

sona_avatar_play(a, "00400000-0064-0003-c1c8-f109a19cb2e0", NULL);   /* the stock walk */
sona_avatar_root_motion(a, SONA_ROOT_MOTION_EXTRACT);                /* walk in place, the game moves the avatar */

sona_mask_preset(SONA_MASK_UPPER_BODY, upper);
sona_layer_defaults(&lo);
lo.mask = upper;
sona_avatar_layer_play(a, 1, "GenericWave", &lo);                  /* layer 1: the wave, on Spine and above */
```

What happens to the clip's sideways travel of the hips. `SONA_ROOT_MOTION_OFF` (0) leaves it in the pose. `SONA_ROOT_MOTION_EXTRACT` (1) holds the hips where the clip starts and collects the travel for `sona_avatar_take_root_motion`. `SONA_ROOT_MOTION_APPLY` (2) moves the avatar's placement by it each update. Setting a mode drops what was collected. `SONA_E_ARGUMENT` for another mode.

## sona_avatar_take_root_motion

```c
sona_result sona_avatar_take_root_motion(sona_avatar *a, float delta[3]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `delta` | `float [3]` | Receives the root motion since the last call, world space | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
float x_delta[3];
memset(x_delta, 0, sizeof x_delta);
sona_result result = sona_avatar_take_root_motion(avatar, x_delta);
if (result != SONA_OK) printf("sona_avatar_take_root_motion: %s\n", sona_result_name(result));
```

The travel collected since the last call, in world metres (the avatar's turn and scale applied), and clears it.
