# Shared systems

A capability describes behavior. A provider implements that behavior. The world
policy selects one provider for each capability.

```ts
import { defineSystem } from '@threetopia/world';

export default defineSystem({
  provides: ['water.surface@2'],
  requires: ['environment.time@1'],
  entry: 'water.js',
  parameters: {
    'water.surface@2': {
      resource: { level: { type: 'number', required: true } },
      scope: { waveEnergy: { type: 'number', min: 0, max: 1 } }
    }
  }
});
```

This is a contract example. It does not select an ocean algorithm.

## Water surface and currents

| Capability | Required sample fields |
| --- | --- |
| `water.surface@2` | `height`, `normal`, `sampleTime`, `age` |
| `water.current@1` | `velocity`, `sampleTime`, `age` |

Currents are a separate capability. `velocity` is `[x, y, z]` in world axes,
in metres per second. Times and sample age use the shared simulation clock,
in seconds. A missing sample is not a zero vector.

A package that needs surface data and currents declares both requirements:

```ts
requires: ['water.surface@2', 'water.current@1']
```

World policy selects each provider. A surface provider does not imply a current
provider. Compilation fails at the package requirement if no current provider
is selected. The runtime also rejects undeclared capability access, even if a
provider exists. Do not replace missing current data with `[0, 0, 0]`.

Tidewater supplies surface data. It does not supply a current capability.
This preserves its original boat and wave behavior. Delayed GPU samples still
need timing and boundary evidence before acceptance.

`water.surface@1` keeps its old height, normal, flow, and time contract.
Existing revisions and adapters keep that meaning. Use new package versions
to adopt version 2. Do not silently migrate a published world.

## Provider lifecycle

A reviewed adapter receives the host renderer, camera, scene, clock, abort signal,
and capability services. It returns `capabilities`, `update`, and `dispose`.

```ts
export function createWater(host) {
  const time = host.services.get('environment.time@1');
  const water = createOriginalWater({ renderer: host.renderer, time });
  host.scene.add(water.object);
  return {
    capabilities: { 'water.surface@2': water.surface },
    update: (delta, now) => water.update(delta, now),
    dispose() {
      host.scene.remove(water.object);
      water.dispose();
    }
  };
}
```

`createOriginalWater` above represents the creator's implementation. Keep its
math and appearance. Do not replace it with the reference demo's water.

Use metres and world coordinates. Water queries must describe their sample time
and age. Physics must use the same surface as rendering. Test delayed GPU results.

Dispose only owned resources. Remove input listeners. Cancel pending loads.
Prove independent instances and repeated removal before acceptance.

## threejsworlds source

Fifteen private packages are imported under `packages/systems`: camera, engine,
environment, fog, gfx, particles, post, road, scatter, shadows, sky, terrain,
vegetation, water, and world. Each has `SOURCE.json` and a README.

Their original package names remain intact. These source packages are not
published registry providers. The imported `world` package contains runtime
contracts; `@threetopia/world` contains the authoring API.

The reference client uses the sky and camera code. Their Threetopia adaptations
remove implicit sky input and add resource and listener disposal. The remaining
systems need explicit capability adapters and parity evidence before activation.

## Settings and previews

Expose only supported parameters. A resource owns shared defaults. A spatial
scope changes approved parameters. It cannot swap a renderer or water algorithm.
Overlapping scopes need an explicit precedence rule.

A provider change must compile and review the whole affected world. Add a
regression test for each resolved conflict, including the region boundary.

Declare asset bounds for the full motion range. The compiler applies scale and rotation before it checks region ownership. Model bounds are checked against actual GLB vertices. Undeclared settings fail validation.
