# Cookbook: recipes for game moments

Each recipe is a few lines for one game moment: make an avatar wave, cheer, look at the player, walk somewhere, hold
something, smile or high-five another. Each is in C, C++ (`sona.hpp`), C#, Rust and Python. They use the variables a
game already has: `ctx` (the context), `avatar` and `other` (two `READY` avatars), `player_head` (a point in metres).

Where a binding has no wrapper for a call, its recipe makes the C call through the binding's own raw layer (C++
`raw()`, C# `Native`). Unity, Godot and Unreal have the same features on their avatar components.

## Wave when a goal is scored

Crossfade to the wave once, then back to the idle when it ends.

```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);
```

```cpp
// once: loop 0, speed 1, from the start
sona_play_options once = sona::play_defaults();
once.loop = 0;
avatar.crossfade("GenericWave", 0.2f, &once);
avatar.queue("idle", 0.3f);
```

```csharp
// once, at speed 1, from the start
avatar.Crossfade("GenericWave", 0.2f, loop: false);
avatar.Queue("idle", 0.3f);
```

```rust
// once, at speed 1, from the start
let once = PlayOptions { looped: false, ..Default::default() };
avatar.crossfade("GenericWave", &once, 0.2)?;
avatar.queue("idle", &PlayOptions::default(), 0.3)?;
```

```python
# once, at speed 1, from the start
avatar.crossfade("GenericWave", 0.2, loop=False)
avatar.queue("idle", fade=0.3)
```

## Cheer from a line of text

The text-to-emote table picks stock clips and a face for a sentence ([guide](guides/emote.md)).

```c
sona_emote e = { sizeof e };
if (sona_emote_from_text("jump for joy", &e) == SONA_OK && e.count > 0)
  sona_avatar_emote(avatar, &e, 0.25f);
```

```cpp
sona_emote e;
if (sona::emote_from_text("jump for joy", e) == SONA_OK && e.count > 0)
  avatar.emote(e, 0.25f);
```

```csharp
var e = new Native.sona_emote { size = (uint)sizeof(Native.sona_emote) };
fixed (byte* text = System.Text.Encoding.UTF8.GetBytes("jump for joy\0"))
    if (Native.sona_emote_from_text(text, &e) == SonaResult.Ok && e.count > 0)
        Native.sona_avatar_emote(avatar.Handle, &e, 0.25f);   // the binding's raw call
```

```rust
let e = emote_from_text("jump for joy")?;
if e.count > 0 {
    avatar.emote(&e, 0.25)?;
}
```

```python
e = sona.emote_from_text("jump for joy")
if e["count"] > 0:
    avatar.emote(e, 0.25)
```

## Look at the player

The head turns and the eyes follow, inside the neck's limits. Call it each frame while the player moves.

```c
sona_ik_look look;
sona_ik_look_defaults(&look);
look.target[0] = player_head[0];
look.target[1] = player_head[1];
look.target[2] = player_head[2];
sona_avatar_ik_look(avatar, &look);
```

```cpp
sona_ik_look look;
sona_ik_look_defaults(&look);
look.target[0] = player_head[0];
look.target[1] = player_head[1];
look.target[2] = player_head[2];
sona_avatar_ik_look(avatar.raw(), &look);   // sona.hpp has no wrapper: the C call on the handle
```

```csharp
var look = SonaAvatar.IkLookDefaults();
look.target[0] = playerHead[0];
look.target[1] = playerHead[1];
look.target[2] = playerHead[2];
avatar.SetIkLook(look);
```

```rust
let mut look = ik_look_defaults();
look.target = player_head;
avatar.set_ik_look(Some(&look))?;
```

```python
avatar.ik_look(target=player_head)
```

## Walk to a spot

`sona_avatar_move` eases the placement over the time given; the stock walk plays while it moves ([clips](guides/clips.md)).

```c
sona_placement to = { { 2.0f, 0.0f, -3.0f }, 90.0f, 1.0f };   /* position, turn, scale */
sona_avatar_play_stock(avatar, SONA_STOCK_WALK_FORWARD, NULL, 0.25f);
sona_avatar_move(avatar, &to, 2.5f);
/* 2.5 s later, when it has arrived: */
sona_avatar_crossfade(avatar, "idle", NULL, 0.3f);
```

```cpp
const sona_placement to = { { 2.0f, 0.0f, -3.0f }, 90.0f, 1.0f };   // position, turn, scale
sona_avatar_play_stock(avatar.raw(), SONA_STOCK_WALK_FORWARD, nullptr, 0.25f);
avatar.move(to, 2.5f);
// 2.5 s later, when it has arrived:
avatar.crossfade("idle", 0.3f);
```

```csharp
avatar.PlayStock("walk_forward", 0.25f);
avatar.Move(new SonaPlacement(2, 0, -3, turnDeg: 90), 2.5f);   // position, turn, scale
// 2.5 s later, when it has arrived:
avatar.Crossfade("idle", 0.3f);
```

```rust
let walk = stock_clip_find("walk_forward").unwrap_or(0);
avatar.play_stock(walk, None, 0.25)?;
avatar.move_to(&Placement { position: [2.0, 0.0, -3.0], turn_deg: 90.0, scale: 1.0 }, 2.5);
// 2.5 s later, when it has arrived:
avatar.crossfade("idle", &PlayOptions::default(), 0.3)?;
```

```python
avatar.play_stock("walk_forward", fade=0.25)
avatar.move((2, 0, -3), turn_deg=90, over_seconds=2.5)
# 2.5 s later, when it has arrived:
avatar.crossfade("idle", 0.3)
```

The walk loops, so it never ends by itself; switch back when the move is done, not with a queue.

## Hold something where the hand is

Read a joint's world matrix each frame and put your object there; the matrix is row-vector, so the position is the
last row.

```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 */
}
```

```cpp
float hand[16];
int32_t joint = sona::joint_find("RightHand");
if (joint >= 0 && avatar.joint_world((uint32_t)joint, hand) == SONA_OK) {
  float x = hand[12], y = hand[13], z = hand[14];   // your object's position
  (void)x; (void)y; (void)z;
}
```

```csharp
var hand = new float[16];
if (avatar.TryGetJointWorld(SonaJoints.Find("RightHand"), hand))
{
    float x = hand[12], y = hand[13], z = hand[14];   // your object's position
}
```

```rust
if let Some(joint) = joint_find("RightHand") {
    let hand = avatar.joint_world(joint)?;
    let (x, y, z) = (hand[12], hand[13], hand[14]);   // your object's position
}
```

```python
hand = avatar.joint_world("RightHand")
x, y, z = hand[12:15]   # your object's position
```

For a real held item, the props API attaches a mesh to the hand and grips it ([props](guides/props.md)).

## A smile that holds

Set the mouth and leave the rest of the face to the clip (255 means "the clip's").

```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);
```

```cpp
const uint8_t face[5] = { 6, 255, 255, 255, 255 };   // mouth 6 is happy; brows and eyes from the clip
avatar.expression(face);
```

```csharp
avatar.SetExpression(6);   // mouth 6 is happy; brows and eyes from the clip
```

```rust
avatar.set_expression(Some([6, 255, 255, 255, 255]))?;   // mouth 6 is happy; brows and eyes from the clip
```

```python
avatar.expression([6, 255, 255, 255, 255])   # mouth 6 is happy; brows and eyes from the clip
```

## High five

Two avatars meet, their hands touch on the beat, and both go back to their idle ([guide](guides/interactions.md)).

```c
sona_interaction_desc d;
sona_interaction *it;
sona_interaction_defaults(&d, SONA_INTERACT_HIGH_FIVE);
d.a = avatar;
d.b = other;
if (sona_interaction_start(ctx, &d, &it) == SONA_OK) {
  /* each frame: sona_interaction_events(it, ...); when it ends, sona_interaction_remove(it) */
}
```

```cpp
sona_interaction_desc d;
sona_interaction *it = nullptr;
sona_interaction_defaults(&d, SONA_INTERACT_HIGH_FIVE);
d.a = avatar.raw();
d.b = other.raw();
if (sona_interaction_start(ctx.raw(), &d, &it) == SONA_OK) {
  // each frame: sona_interaction_events(it, ...); when it ends, sona_interaction_remove(it)
}
```

```csharp
var d = SonaContext.InteractionDefaults(Native.SONA_INTERACT_HIGH_FIVE);
d.a = avatar.Handle;
d.b = other.Handle;
var it = ctx.StartInteraction(d);
// each frame: it.Events(); when it ends, it.Dispose()
```

```rust
let mut d = interaction_defaults(sona_sys::SONA_INTERACT_HIGH_FIVE);
d.a = avatar.raw();
d.b = other.raw();
let mut it = ctx.start_interaction(&d)?;
// each frame: it.events(); dropping it removes it
```

```python
it = ctx.interaction("high_five", avatar, other)
# each frame: it.events(); when it ends, it.remove()
```

## A random extra

Seeded, so the same seed always gives the same person; here in everyday clothes, never a costume
([guide](guides/makers.md)).

```c
sona_catalogue *cat;
sona_maker *mk;
sona_random_options o;
uint8_t manifest[SONA_MANIFEST_BYTES];
sona_avatar *extra;
if (sona_catalogue_load(ctx, "sona-catalogue.bin", &cat) == SONA_OK && sona_maker_create(ctx, cat, &mk) == SONA_OK) {
  sona_random_defaults(&o);
  o.flags = SONA_RANDOM_NO_COSTUME;
  if (sona_make_random(mk, 42, &o, manifest, NULL) == SONA_OK) sona_avatar_add(ctx, manifest, NULL, &extra);
  sona_maker_free(mk);
}
```

```cpp
sona::Catalogue cat("sona-catalogue.bin", &ctx);
sona_maker *mk = nullptr;
if (sona_maker_create(ctx.raw(), cat.raw(), &mk) == SONA_OK) {
  sona_random_options o;
  sona_random_defaults(&o);
  o.flags = SONA_RANDOM_NO_COSTUME;
  uint8_t made[SONA_MANIFEST_BYTES];
  if (sona_make_random(mk, 42, &o, made, nullptr) == SONA_OK) {
    sona::Avatar extra = ctx.add(made);
  }
  sona_maker_free(mk);
}
```

```csharp
using var cat = SonaCatalogue.Load("sona-catalogue.bin", ctx);
using var mk = new SonaMaker(cat, ctx);
var made = mk.Random(42, new SonaRandomOptions { Flags = Native.SONA_RANDOM_NO_COSTUME });
var extra = ctx.Add(made.Manifest);
```

```rust
let bytes = std::fs::read("sona-catalogue.bin").unwrap_or_default();
let cat = Catalogue::from_bytes(&bytes)?;
let mk = Maker::new(&cat)?;
let made = mk.random(42, &RandomOptions { flags: sona_sys::SONA_RANDOM_NO_COSTUME, ..Default::default() })?;
let extra = ctx.add(&made.manifest, &AddOptions::default())?;
```

```python
cat = sona.Catalogue("sona-catalogue.bin", ctx)
mk = sona.Maker(cat, ctx)
made, info = mk.random(42, flags=1)   # 1: SONA_RANDOM_NO_COSTUME
extra = ctx.add(made)
mk.close()
```

## Remove an avatar when its player leaves

```c
sona_avatar_remove(avatar);
```

```cpp
avatar.remove();
```

```csharp
avatar.Remove();
```

```rust
let extra = ctx.add(manifest, &AddOptions::default())?;
extra.remove();   // or let it go out of scope: an avatar dropped is removed
```

```python
avatar.remove()
```

`sona_avatar_remove` frees what only that avatar used; do not use its handle again.
