# C API: Hair and cloth sway

Long hair, skirts, dresses, robes, coat tails and earrings swing as the avatar moves, under the ctx's gravity and
wind, off the body's capsules. Off by default, per avatar. What sways is read from each worn mesh; those vertices are
re-skinned in a shared copy through palette joints the mesh never reads, and the copy draws with its own palette,
listed after the props in `sona_pose`. Each struct starts with `size = sizeof(struct)` and is refused
(`SONA_E_ARGUMENT`) when `size` is smaller; the `_defaults` functions set it. Avatar calls belong to the owner
thread. The guide, with what counts as hanging, the solver and the cost, is
[Hair and cloth sway](../../guides/sway.md).

| Struct | Bytes | Fields |
|---|---|---|
| `sona_sway` | 60 | `size`, `parts`, `quality`, `stiffness`, `damping`, `wind_scale`, `min_pixels`, `reserved[8]` |
| `sona_sway_world` | 68 | `size`, `gravity[3]`, `wind[3]`, `gust`, `gust_seconds`, `reserved[8]` |
| `sona_sway_info` | 68 | `size`, `parts`, `meshes`, `chains`, `nodes`, `vertices`, `steps`, `resting`, `max_offset`, `reserved[8]` |

Parts: `SONA_SWAY_HAIR` (1), `SONA_SWAY_CLOTH` (2), `SONA_SWAY_ACCESSORIES` (4, earrings), `SONA_SWAY_HEADWEAR` (8,
costume and hat pieces below the head), `SONA_SWAY_DEFAULT` (7), `SONA_SWAY_ALL` (15). Quality: `SONA_SWAY_LOW` (0,
30 steps a second), `SONA_SWAY_MEDIUM` (1, 60), `SONA_SWAY_HIGH` (2, 120).

## sona_sway_defaults

```c
void sona_sway_defaults(sona_sway *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_sway *` | The [`sona_sway`](../fields.md#sona_sway) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Hair and cloth sway](../../guides/sway.md)):

```c
sona_sway o;
sona_sway_defaults(&o);                  /* hair, cloth and earrings; medium quality */
sona_avatar_sway(avatar, &o);            /* before or after READY */

sona_sway_world w;
sona_sway_world_defaults(&w);            /* gravity 0 -9.81 0, no wind */
w.wind[0] = 4;                           /* a 4 m/s breeze along +X, gusting */
sona_sway_world_set(ctx, &w);

sona_update(ctx, dt, &view, NULL);       /* the chains step inside the poses of this update */
```

Parts `SONA_SWAY_DEFAULT`, medium quality, stiffness and damping 0.5, wind scale 1, 48 minimum pixels.

## sona_avatar_sway

```c
sona_result sona_avatar_sway(sona_avatar *a, const sona_sway *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_sway *` | A [`sona_sway`](../fields.md#sona_sway) | required |

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

**Example** (also in [Hair and cloth sway](../../guides/sway.md)):

```c
sona_sway o;
sona_sway_defaults(&o);                  /* hair, cloth and earrings; medium quality */
sona_avatar_sway(avatar, &o);            /* before or after READY */

sona_sway_world w;
sona_sway_world_defaults(&w);            /* gravity 0 -9.81 0, no wind */
w.wind[0] = 4;                           /* a 4 m/s breeze along +X, gusting */
sona_sway_world_set(ctx, &w);

sona_update(ctx, dt, &view, NULL);       /* the chains step inside the poses of this update */
```

Turns sway on with these options, before or after READY (it applies to every later build too). A change of parts
re-skins at once and bumps `build_version`. NULL turns it off: the items' own meshes again. `SONA_E_ARGUMENT` for a
small `size`, no part or an unknown one, a quality past `SONA_SWAY_HIGH`, stiffness or damping outside 0..1, a wind
scale outside 0..100 or negative minimum pixels; `SONA_E_DESTROYED` for an avatar being removed.

## sona_avatar_sway_reset

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

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

**Returns** Nothing.

**Example**:

```c
sona_avatar_sway_reset(avatar);
```

Back to the modelled shape, at rest, at the next pose. NULL is ignored.

## sona_sway_world_defaults

```c
void sona_sway_world_defaults(sona_sway_world *w);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `w` | `sona_sway_world *` | The [`sona_sway_world`](../fields.md#sona_sway_world) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Hair and cloth sway](../../guides/sway.md)):

```c
sona_sway o;
sona_sway_defaults(&o);                  /* hair, cloth and earrings; medium quality */
sona_avatar_sway(avatar, &o);            /* before or after READY */

sona_sway_world w;
sona_sway_world_defaults(&w);            /* gravity 0 -9.81 0, no wind */
w.wind[0] = 4;                           /* a 4 m/s breeze along +X, gusting */
sona_sway_world_set(ctx, &w);

sona_update(ctx, dt, &view, NULL);       /* the chains step inside the poses of this update */
```

Gravity 0 -9.81 0, no wind, gust 0.35 with a period of 1.5 s.

## sona_sway_world_set

```c
sona_result sona_sway_world_set(sona_ctx *ctx, const sona_sway_world *w);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `w` | `const sona_sway_world *` | A [`sona_sway_world`](../fields.md#sona_sway_world) | required |

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

**Example** (also in [Hair and cloth sway](../../guides/sway.md)):

```c
sona_sway o;
sona_sway_defaults(&o);                  /* hair, cloth and earrings; medium quality */
sona_avatar_sway(avatar, &o);            /* before or after READY */

sona_sway_world w;
sona_sway_world_defaults(&w);            /* gravity 0 -9.81 0, no wind */
w.wind[0] = 4;                           /* a 4 m/s breeze along +X, gusting */
sona_sway_world_set(ctx, &w);

sona_update(ctx, dt, &view, NULL);       /* the chains step inside the poses of this update */
```

The gravity and wind every swaying avatar of the ctx feels from its next pose; resting avatars are posed again. NULL:
the defaults. `SONA_E_ARGUMENT` for a small `size`, a gust outside 0..1, gust seconds outside 0.05..600, gravity over
1000 or wind over 100 on an axis, or a value that is not finite.

## sona_avatar_sway_info

```c
sona_result sona_avatar_sway_info(sona_avatar *a, sona_sway_info *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `out` | `sona_sway_info *` | Receives the [`sona_sway_info`](../fields.md#sona_sway_info) | 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_sway_info x_sway_info;
memset(&x_sway_info, 0, sizeof x_sway_info);
x_sway_info.size = sizeof x_sway_info;
sona_result result = sona_avatar_sway_info(avatar, &x_sway_info);
if (result != SONA_OK) printf("sona_avatar_sway_info: %s\n", sona_result_name(result));
```

What sways (`parts`, `meshes`, `chains`, `nodes`, `vertices`) and what the last pose did (`steps`, `resting`,
`max_offset` in metres). With sway off: `parts` as found in what it wears, the rest 0. `SONA_E_NOT_READY` before
READY.

## sona_avatar_sway_mesh_parts

```c
uint32_t sona_avatar_sway_mesh_parts(const sona_avatar *a, uint32_t mesh);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `mesh` | `uint32_t` | A mesh index of the pose data | - |

**Returns** `uint32_t`.

**Example**:

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

The `SONA_SWAY_*` kinds one of `sona_pose`'s meshes sways with; 0 for one that does not, a bad index, or an avatar
with sway off or not READY.
