# Pilot API

[Wiki home](index.md) · [PHP syntax](php-syntax.md) · [PHP reference](php-reference.md)

Call these methods on `$pilot`. The [live PHP reference](php-reference.md) lists every allowed method and its signature. This chapter explains how to combine observations, commands, forecasts, and feedback in a pilot.

## Coordinates and tick order

Positions and distances use arena units on the X/Z plane. Positive X is east and positive Z is south. Heading zero faces positive Z. Use `atan2($dx, $dz)` for the absolute heading toward an offset. Headings and bearings use radians. Speeds use units per second. The engine runs 20 ticks per second. Read `battleState()['tickDuration']` when converting between time and ticks. The first mount in Builder is numbered 1, which is slot `0` in code. Participant IDs, team numbers, and team seats describe the current match. Seats start at 1.

Each tick begins with resource recovery, equipment upkeep, cooldowns, status updates, and knockback. Pilots then observe a consistent decision snapshot and queue their commands. The engine resolves team coordination, equipment toggles, movement and turning, then aim and fire. Projectiles, pulses, pickups, and any sudden-death damage follow. Repeating an observation after calling `toggle()` or `moveTo()` returns the same observed state for that tick. Budget shared energy and ammo locally when ordering more than one mount.

## Read the arena

| Method | What to use it for |
| --- | --- |
| `scan()` | Access `self`, distance-ordered `allies` and `enemies`, `pickups`, and visible `threats`. Check `velocity` for `null` on a new or reacquired contact. Cloaked enemies and destroyed bots are omitted. Living cloaked allies remain visible to their team. |
| `selfState()` | Read the caller's exact hull, shield, energy, and ammo as `resources` entries with `current` and `max`. Read current position, heading, drive speed, stun, cloak, and knockback here. |
| `mountState($slot)` | Read a fitted component's `mode`, `targeting`, `ready`, resource costs, and any supported range, radius, or pending aim. An empty slot returns `null`. Targeting is `point` or `self` for a firing mount. Use `enabled` and `active` separately for toggles. A configured toggle can be temporarily suppressed. |
| `battleState()` | Read clocks, normal or sudden-death phase, `arenaBounds`, `movementBounds`, and `pickupRules`. Aim coordinates can reach the wall. The movement bounds have a two-unit inset. |
| `teamState()` | Read the own-team roster with stable seat numbers, living status, broad resource bands, and whether a teammate can move or has a ready weapon. Match IDs to `scan()['allies']` for living teammate positions. |

A scan can omit the very target you wanted to track. Treat contacts as observations rather than persistent target locks. Read pickups from `scan()['pickups']`. The list contains zero or one pickup. Its `id`, `kind`, `amount`, and `secondsRemaining` help you decide whether the resource is worth the detour.

## Send commands and verify their results

| Method | How the command behaves |
| --- | --- |
| `moveTo($x, $z, $reverse)` | Request one tick of turning or driving toward a point. Repeat on later ticks to continue the route. Reverse turns the chassis to face away from the goal so it can drive backward. |
| `turnTo($heading)`, `holdPosition()` | Rotate the chassis in place or clear the current tick's drive and turn requests. Holding still leaves knockback in effect. |
| `destination()`, `queuedActions()` | Inspect this tick's queued intentions. They describe what the pilot requested. |
| `aim($slot, $x, $z)`, `fire($slot, $x, $z)`, `cancelAim($slot)` | Aim at a fixed point, request a shot, or cancel the mount's pending order. Use `fire($slot)` with a self-centered effect. Each mount can accept one new aim, fire, or cancel command per tick. |
| `toggle($slot, $on)` | Request enabling or disabling a toggle component. Use the current `enabled` value to avoid submitting redundant changes. |

The Boolean from a command reports whether the pilot queued it. `actionResults()` reports results and rejections from the previous tick. Movement can report `turning`, `driving`, `arrived`, or `blockedByBot`. Fire can report `aiming`, `fired`, `outOfRange`, `cooldown`, `insufficientEnergy`, or `insufficientAmmo` as applicable. Guard optional entries with `??`. A turret can aim during cooldown and can keep turning toward a queued fire point after the tick that requested it. A stale point can still produce a shot, so update or cancel a pending order when the target disappears. Energy and ammo are spent at launch.

Read `recentEvents()` for the newly observed outcomes this tick. Match `shotLaunched` to `shotResolved` using `shotId`. A resolved `outcome` of `hit` indicates a visible impact reported for that shot. Actual damage, stun, and push are different effects. A launch, a hit, and hull damage answer different questions. Read the practice trace and opponent hull to confirm the effect you intended. Repeated reads of `recentEvents()` during one tick return the same list.

## Estimate before committing

| Method | What it estimates |
| --- | --- |
| `weaponRange($slot)` | The directional weapon's maximum range or the centered effect's radius. For a point weapon, `mountState($slot)['effectiveRange']` describes the range before extra spread. Targeting and jammer effects can alter it. |
| `estimateMove($x, $z, $reverse)` | Turn, drive, and total time with the current drive capacity and a clear route. Compare both directions and recheck after a status change. |
| `estimateShot($slot, $x, $z)` | `inRange`, `aimSeconds`, and `travelSeconds` for an aimed mount from its current pose. A ready turret can still require time to align. |
| `estimateIntercept($slot, $x, $z, $vx, $vz)` | An aim point assuming the target continues at its observed velocity. Recheck the result with `estimateShot()` and recalculate when the target changes course. |
| `estimateThreats()`, `estimateThreatsAt($x, $z)` | Predicted impact times for currently visible effects at the current or proposed location. Combine the latter with a movement estimate to see whether you can arrive in time. |
| `estimateFriendlyFire($slot, $x, $z)` | Whether friendly fire is enabled and a list of visible allies at risk. Omit coordinates for a self-centered effect. |
| `estimateReadiness($slot)` | The estimated ready tick, time, and blocker for applicable firing and toggle mounts. Long waits and unknown recovery can yield null timing. |

Guard nullable forecasts. They describe the present pose and known effects, and can change when a bot moves, a projectile spreads, or a new obstacle appears. See [Combat strategy](combat-strategy.md) and [Equipment](equipment.md) for choosing a useful range and planning resource costs.

### Example. Close and fire a beam

Equip a Beam Laser in the first mount. This pilot approaches its nearest visible opponent until it is a few units inside effective range, then aims or fires from its current position. It cancels pending aim when the target disappears. Add avoidance and threat handling before entering a team match.

```php
<?php
$scan = $pilot->scan();
$enemy = $scan['enemies'][0] ?? null;
$weapon = $pilot->mountState(0);
if ($weapon === null || $weapon['targeting'] !== 'point') {
    return;
}

$pending = $weapon['pendingAim'] ?? null;
if ($enemy === null) {
    if ($pending !== null) {
        $pilot->cancelAim(0);
    }
    return;
}

$x = $enemy['x'];
$z = $enemy['z'];
$shot = $pilot->estimateShot(0, $x, $z);
$usefulRange = max(1.0, ($weapon['effectiveRange'] ?? $weapon['range']) - 3.0);
if ($enemy['distance'] > $usefulRange || !($shot['inRange'] ?? false)) {
    if ($pending !== null) {
        $pilot->cancelAim(0);
    }
    $pilot->moveTo($x, $z);
    return;
}

if ($weapon['ready']) {
    $pilot->fire(0, $x, $z);
} elseif (($pending['action'] ?? null) !== 'fire') {
    $pilot->aim(0, $x, $z);
}
```

## Coordinate and retain small amounts of state

- Use `signal()` and `signals()` for simple teammate pings. Use `sendTeamMessage()` and `teamMessages()` for structured messages with a match-local ID and a lifetime. Delivery occurs on the next tick and reads are non-consuming.
- Use `claimTeamObjective()`, `releaseTeamObjective()`, and `teamClaims()` to coordinate pickup routes. Claims are advisory and leave the arena's collection rules in effect.
- Use `publishTeamData()`, `clearTeamData()`, and `teamData()` for short-lived shared values. `memory()`, `recall()`, and `remember()` are private to the calling bot.
- Use `randomInt()` for deterministic variation, `workRemaining()` for the current interpreter budget, and `debug()` to add small observations to the authorized practice trace.

See [Team strategy](team-strategy.md) for communication patterns, [Testing](testing.md) to verify commands and hit evidence, and [Examples](examples.md) for complete pilots.
