# Abilities, attributes and effects

> Give the player and any object attributes such as health, stamina and hunger, effects that change them, abilities with costs and cooldowns, gameplay tags, and smart objects NPCs can use.

Source: https://feather-engine.com/docs/gameplay/abilities/

The ability system is a data-driven way to build stats and powers. **Attributes** are numbers an
owner has, such as health, stamina, hunger or warmth. **Effects** change them: once, for a time, or
until removed, and can tick on a period. **Abilities** spend attributes, cool down and run a
wind-up, active and recovery timeline. **Gameplay tags** such as `status.burning` tie it together:
effects grant them, and effects and abilities can require them or be blocked by them.

Everything is project data, edited in the **Abilities** tab or with the `ability.*` commands, and
it works for any owner: the player, any object, or any name a script picks. Scripts drive it during
Play, and it runs the same in the editor, in exported games and in headless simulations.

## What's included

- **Attributes** with a base value, a minimum and maximum, an optional cap read from another
  attribute (health up to `max_health`), regeneration or decay per second, and thresholds that
  hold an effect while the value is low.
- **Effects** that are instant, timed or infinite, optionally periodic, with Add, Multiply and
  Override modifiers, tags they grant, tags they need or are blocked by, tags they cleanse, and
  stacking rules.
- **Abilities** with attribute costs, a cooldown, wind-up, active and recovery phases, tags they
  need, are blocked by, hold while running or are cancelled by, and effects on the owner and on a
  target.
- **Hierarchical tags:** having `status.burning` also counts as having `status`.
- A **survival preset** to start from, and **smart objects**: places with an activity NPCs can use.
- Saves keep every owner's attributes, effects, tags and cooldowns.

## Set up abilities

1. Open **Window → World & terrain** and select the **Abilities** tab.
2. Click **Add survival preset** to start from a working set, or build your own.
3. Pick **Attributes**, **Effects** or **Abilities** at the top. The count of each is shown next to
   its name.
4. Type an ID such as `mana` in the `new_id` field and click **+ Add**. IDs are lowercase letters,
   digits and `_`, starting with a letter.
5. Select the item in the list and edit it on the right. Click **Apply** to keep the changes,
   **Revert** to drop them, or **Remove** to delete the item.
6. Use the item from a script (see [Extend it](#extend-it)) and press **Play**.

A new effect starts as **Timed** for 5 s, and a new ability starts with an **Active** time of
0.5 s. Changes made during Play apply at once.

### Attributes

| Setting | Field | Range | Default |
| --- | --- | --- | --- |
| Label | `label` | Text | Empty |
| Base | `base` | Any number | 100 |
| Min, Max | `min`, `max` | Min no higher than Max | 0, 100 |
| Capped by | `max_attribute` | Another attribute, or none | None |
| Regen | `regen` | Change per second; negative decays | 0 |
| Regen delay | `regen_delay` | Seconds after a drop, 0 or more | 0 |
| Thresholds | `thresholds` | `[{below, effect}]` | None |

An attribute's **current value** is its base with the modifiers of active, non-periodic effects
applied, kept between the minimum and the cap. The cap is the maximum, or the lower of the maximum
and the **Capped by** attribute's value.

Regeneration adds to the base every second once **Regen delay** seconds have passed since the base
last dropped. Decay (a negative **Regen**) never waits. Hunger and thirst work this way in the
survival preset.

A threshold holds its effect while the attribute's value is below the level, and removes it when
the value rises to the level again. The survival preset uses this to apply `starving` while hunger
is below 15.

### Effects

| Setting | Field | Range | Default |
| --- | --- | --- | --- |
| Duration | `duration` | `Instant`, `Timed`, `Infinite` | Instant |
| Seconds | `seconds` | Above 0 for Timed | 0 |
| Period | `period` | 0, or 0.05 s and more | 0 |
| Stacking | `stacking` | `Refresh`, `Stack`, `Ignore` | Refresh |
| Max stacks | `max_stacks` | 1–99, for Stack | 0 |
| Modifiers | `modifiers` | `[{attribute, op, value}]`; `op` is `Add`, `Multiply` or `Override` | None |
| Grants tags | `grant_tags` | Tags | None |
| Requires | `require_tags` | Tags | None |
| Blocked by | `block_tags` | Tags | None |
| Removes effects with | `remove_tags` | Tags | None |

How the modifiers apply depends on the duration and period:

- **Instant** effects change the base once: damage, healing, eating.
- **Timed** and **Infinite** effects with a **Period** of 0 hold their modifiers on the current
  value while they are active. Adds are summed, the result is multiplied, and an Override
  replaces it.
- With a **Period** above 0, they change the base every period instead, like damage over time.

An effect applies only when the owner has every **Requires** tag and none of the **Blocked by**
tags. Applying it first removes the owner's active effects that grant any of its **Removes effects
with** tags: in the preset, `soaked` puts out `burning`, and its `status.wet` tag then blocks
`burning` from coming back.

Applying an effect that is already active depends on **Stacking**. **Refresh** restarts its time,
**Stack** adds a stack (modifiers scale with the stack count) up to **Max stacks** and restarts its
time, and **Ignore** does nothing.

### Abilities

| Setting | Field | Range | Default |
| --- | --- | --- | --- |
| Cooldown | `cooldown` | 0 s or more | 0 s |
| Windup | `windup` | 0 s or more | 0 s |
| Active | `active` | 0 s or more | 0 s |
| Recovery | `recovery` | 0 s or more | 0 s |
| Costs | `costs` | `[{attribute, amount}]`, amounts 0 or more | None |
| Requires | `require_tags` | Tags | None |
| Blocked by | `block_tags` | Tags | None |
| Tags while running | `active_tags` | Tags | None |
| Cancelled by | `cancel_tags` | Tags | None |
| Effects on self | `self_effects` | Effect IDs | None |
| Effects on target | `target_effects` | Effect IDs | None |

Starting an ability pays its costs and starts its cooldown. It then runs **Windup**, **Active** and
**Recovery** in turn. When the active phase starts, its effects apply to the owner and, when a
target was given, to the target. An owner runs one ability at a time, recovery included. If the
owner gains a **Cancelled by** tag while the ability runs, it stops.

An ability can't start while another one runs (`busy`), while it cools down, without a required
tag, with a blocking tag or without enough of a cost attribute.

### Gameplay tags

Tags are dotted lowercase words, such as `status.burning`. Each part uses lowercase letters, digits
and `_`. A parent tag is matched by any of its children: an owner with `status.burning` has
`status`, but not `statuses`. Owners get tags from their active effects, their running ability and
scripts. The tag list on the `tags` field is for suggestions only; any valid tag works.

### The survival preset

**Add survival preset** (`ability.preset` `survival`) merges these into the project, replacing
items with the same IDs:

| Kind | Items |
| --- | --- |
| Attributes | `max_health` 100; `health` capped by `max_health`, regenerates 1/s after 5 s; `stamina` regenerates 15/s; `hunger` and `thirst` decay 0.2/s and 0.3/s; `warmth` |
| Effects | `starving` and `parched` (held below 15 hunger or thirst, −1 and −1.5 health every 2 s); `eat` (+35 hunger) and `drink` (+40 thirst); `burning` (4 s, −3 health every 0.5 s, blocked by `status.wet`); `soaked` (6 s, cleanses `status.burning`); `sprint_boost` |
| Abilities | `dash`: costs 25 stamina, 2 s cooldown, 0.1 s wind-up, 0.3 s active, 0.2 s recovery, blocked and cancelled by `status.stunned` |

## Example: a dash on Q

You'll give the player the preset's dash on **Q**, refusing it when the player is tired or it is
cooling down. Start from a copy of the **Ocean Voyage** example.

1. Open **Window → World & terrain → Abilities** and click **Add survival preset**.
2. Select **Abilities**, then `dash`, to see its 25 stamina cost and 2 s cooldown.
3. Open **Window → Game logic**, select **World scripts**, click **New**, name it `Dash` and click
   **Create**. Paste the script below and press **Cmd/Ctrl+Enter**.

```rust
// Q dashes, if the player has the stamina and the dash has cooled down.
fn start() {
    on("ability", |d| {
        if d.ability == "dash" && d.phase == "end" {
            set("dashes", get("dashes", 0) + 1);
        }
    });
}

fn update(dt) {
    if action_pressed("ability") {
        let why = ability_blocked("player", "dash");
        if why == "" {
            activate_ability("player", "dash");
        } else {
            print("No dash: " + why);
        }
    }
}
```

4. Press **Play** and press **Q**. Press it again at once and the Console shows
   `No dash: cooldown …s`.

Do the same from the command line. Save the script as `dash.rhai` in the workspace folder:

```bash
cp -R examples/ocean-voyage /tmp/demo
cd /tmp/demo   # put dash.rhai here

feather-cli --workspace /tmp/demo --project project.feather.json command ability.preset \
  --json '{"name":"survival"}' --save project.feather.json

jq -n --rawfile src dash.rhai '{name:"Dash",source:$src}' | \
  feather-cli --workspace /tmp/demo --project project.feather.json command world_script.set \
  --json - --save project.feather.json
```

**Check it worked.** Press Q at once and again one second later, in a two-second headless run:

```bash
feather-cli --workspace /tmp/demo --project project.feather.json command game.simulate --json '{
  "frames": 120,
  "inputs": [
    {"frame": 10, "key": "Q", "pressed": true}, {"frame": 12, "key": "Q", "pressed": false},
    {"frame": 60, "key": "Q", "pressed": true}, {"frame": 62, "key": "Q", "pressed": false}
  ]}'
```

In `data.report`, `world.state.dashes` is `1`, `logs` contains `No dash: cooldown 1.2s`, and
`abilities.player` shows `stamina` back at 100 with a little of the `dash` cooldown left.

## Smart objects

A smart object is a place that offers an activity: a bench to sit on, a forge to work at, a
campfire to warm up by. NPCs with a [behaviour tree](/docs/gameplay/ai-and-navigation/) use it with
the **Use Smart Object** action (`use_smart_object`), whose `tag` is the activity.

1. Select the object and click **Add Component → Smart object** in the Details panel.
2. Set the **Activity**, such as `sit`, and how long a use takes.
3. Optionally, name a **Clip** the user plays there and an **Effect**: an effect ID from the
   Abilities tab that the user gets when a use completes.
4. Click **+ Slot** for each place a user can stand, then set each slot's offset.
5. In an NPC's behaviour tree, add a **Use Smart Object** action with **tag** set to the activity.

| Setting | Field | Range | Default |
| --- | --- | --- | --- |
| Activity | `activity` | 1–48 characters | `use` |
| Duration | `duration` | 0–600 s | 5 s |
| Cooldown | `cooldown` | Seconds a slot rests after a use, 0–3600 | 0 s |
| Clip | `clip` | Up to 96 characters | None |
| Effect | `effect` | An effect ID, up to 48 characters | None |
| Slots | `slots` | Up to 16 offsets in the object's local box space, within ±100 | One slot at the object |

The action claims the nearest free slot within its `distance` (default 30 m), walks there at its
`speed` (default 2.5 m/s), plays the clip for the duration (or the action's `seconds`), applies the
effect to the NPC, frees the slot and succeeds. It fails when no slot is free or the slot can't be
reached. Several NPCs can share an object with several slots. A claim lapses half a second after
the action stops running, so an interrupted branch never keeps a slot.

## Extend it

Every function takes an owner: `"player"`, an object (an ID or a handle from `obj`), or any name
you choose.

| Function | Description |
| --- | --- |
| `attr(owner, attribute)` | The current value |
| `set_attr(owner, attribute, value)`, `add_attr(owner, attribute, delta)` | Change the base; return the new value |
| `apply_effect(owner, effect[, source])` | `true` when applied; `false` when tags block it or Ignore stacking refuses it |
| `remove_effect(owner, effect)`, `has_effect(owner, effect)` | Remove or check an active effect |
| `add_gameplay_tag(owner, tag)`, `remove_gameplay_tag(owner, tag)` | Add or remove a tag directly; added tags are counted |
| `has_gameplay_tag(owner, tag)`, `gameplay_tags(owner)` | Check one tag, or list them all |
| `activate_ability(owner, ability[, target])` | `true` when the ability started |
| `ability_blocked(owner, ability)` | `""` when it can start, or why not: `busy`, `cooldown 1.2s`, `needs tag …`, `blocked by tag …`, `not enough stamina` |
| `cancel_ability(owner)`, `cooldown_left(owner, ability)` | Stop the running ability, or read the seconds of cooldown left |
| `ability_state(owner)` | `#{ability, phase, left, target}` for the running ability, or `()` |
| `smart_objects(activity)` | IDs of the objects offering an activity |
| `smart_object_free(id)` | How many of an object's slots are free now |

`has_tag(id, tag)` is a different function: it checks an object's scene tags, not gameplay tags.

| Event | Data |
| --- | --- |
| `ability` | `owner`, `ability`, `phase` (`start`, `active`, `recovery`, `end`, `cancelled`), `target` |
| `effect` | `owner`, `effect`, `phase` (`applied`, `removed`, `expired`), `target` (who applied it) |
| `attribute` | `owner`, `attribute`, `phase` `depleted` (the base reached the minimum) |
| `smart_object` | `id` (the NPC), `object`, `activity` |

```rust
// A world script: eating restores hunger, and the player is warned when health runs out.
fn on_use_item(id) {
    if id == "bread" {
        apply_effect("player", "eat");
    }
}

fn start() {
    on("attribute", |d| {
        if d.owner == "player" && d.attribute == "health" {
            toast("You collapse.");
        }
    });
}
```

## Limits

- Up to 256 attributes, 256 effects and 256 abilities, and 1,024 listed tags.
- IDs are up to 48 characters; tags are up to 64.
- Numbers must be finite, within ±1,000,000,000.
- `ability.remove` is refused while another item still refers to the one you remove.
- A smart object has up to 16 slots.

## Automate it

```bash
# Start from the survival preset
feather-cli command ability.preset --json '{"name":"survival"}'

# Add a mana attribute that refills 5 per second
feather-cli command ability.upsert \
  --json '{"kind":"attribute","value":{"id":"mana","base":50,"max":50,"regen":5}}'

# A poison that ticks 2 damage every second for 6 s, stacking up to 3 times
feather-cli command ability.upsert \
  --json '{"kind":"effect","value":{"id":"poison","duration":"Timed","seconds":6,"period":1,"modifiers":[{"attribute":"health","op":"Add","value":-2}],"grant_tags":["status.poisoned"],"stacking":"Stack","max_stacks":3}}'

# Make object 36 a bench two NPCs can sit on; each gets "rested" when they finish
feather-cli command ability.upsert \
  --json '{"kind":"effect","value":{"id":"rested","duration":"Timed","seconds":30,"grant_tags":["mood.rested"]}}'
feather-cli command scene.update \
  --json '{"id":36,"patch":{"smart_object":{"activity":"sit","duration":3,"effect":"rested","slots":[[-0.25,0,0],[0.25,0,0]]}}}'
```

`ability.get` returns the attributes, effects, abilities and tags, or one item with `kind` and `id`.
`ability.set` patches the whole set at once, including `start_values`, the values every owner
starts with. `ability.upsert` with `rename_from` renames an item and updates the references to it.
`game.simulate` reports each owner's attributes, tags, effects, running ability and cooldowns in
`abilities`, and smart object uses in `smart_objects`. See [Simulation](/docs/agents/simulation/).
