# Lesson 3. Track a moving target

[All lessons](tutorials.md) · [Previous lesson](tutorial-engage.md) · [Next lesson](tutorial-sustain.md)

Relay Scout can now get into range. Add a lead for moving targets, use the cooldown to turn the turret, and hold the chassis still when taking a shot. Keep the [same Relay Scout loadout](tutorials.md#set-up-relay-scout).

## What changes

- Predict where the target will be after the current [tick](glossary.md#tick), then use `estimateIntercept()` to account for turret alignment and any flight time. The Pulse Laser beam has no travel time. Turret alignment and the target's movement still matter.
- Fall back to the scanned coordinates when the prediction can't produce an in-range shot. A new sighting may have no velocity yet.
- Use `aim()` between shots. Ask for `fire()` only when the mount is ready and nearly aligned. That keeps the pilot working with fresh aim points instead of waiting for a queued shot at old coordinates.

## Complete pilot

```php
<?php
declare(strict_types=1);

function collectPickup(array $scan): bool
{
    $pickup = $scan['pickups'][0] ?? null;
    if ($pickup === null) {
        return false;
    }
    $resources = $pilot->selfState()['resources'];
    $resource = $resources[$pickup['kind']] ?? null;
    if ($resource === null || $resource['current'] >= $resource['max']) {
        return false;
    }

    $x = $pickup['x'];
    $z = $pickup['z'];
    $trip = $pilot->estimateMove($x, $z);
    if ($trip === null || $trip['totalSeconds'] >= $pickup['secondsRemaining']) {
        return false;
    }

    return $pilot->moveTo($x, $z);
}

function attackPoint(array $enemy, array $battle): array
{
    $vx = $enemy['velocity']['x'] ?? 0.0;
    $vz = $enemy['velocity']['z'] ?? 0.0;
    $x = $enemy['x'] + $vx * $battle['tickDuration'];
    $z = $enemy['z'] + $vz * $battle['tickDuration'];
    $lead = $pilot->estimateIntercept(0, $x, $z, $vx, $vz);
    return ['x' => $lead['x'] ?? $enemy['x'], 'z' => $lead['z'] ?? $enemy['z']];
}

$scan = $pilot->scan();
$enemy = $scan['enemies'][0] ?? null;
$weapon = $pilot->mountState(0);
if ($weapon === null) {
    return;
}

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

$battle = $pilot->battleState();
$point = attackPoint($enemy, $battle);
$shot = $pilot->estimateShot(0, $point['x'], $point['z']);
if (!($shot['inRange'] ?? false)) {
    $point = ['x' => $enemy['x'], 'z' => $enemy['z']];
    $shot = $pilot->estimateShot(0, $point['x'], $point['z']);
}

$preferredRange = max(1.0, $weapon['effectiveRange'] - 3.0);
if ($enemy['distance'] > $preferredRange || !($shot['inRange'] ?? false)) {
    if ($shot['inRange'] ?? false) {
        $pilot->aim(0, $point['x'], $point['z']);
    } elseif ($weapon['pendingAim'] !== null) {
        $pilot->cancelAim(0);
    }
    $pilot->moveTo($enemy['x'], $enemy['z']);
    return;
}

if ($weapon['ready'] && $shot['aimSeconds'] <= $battle['tickDuration']) {
    $pilot->holdPosition();
    $pilot->fire(0, $point['x'], $point['z']);
} else {
    $pilot->aim(0, $point['x'], $point['z']);
}
```

## Check it in practice

Run **Solo combat**, seed **77**, against **Basic Bot**. In the decision trace, find an aim during cooldown and follow the next shot from launch to its reported outcome. Check that the opponent loses hull. Then try **Field Technician** with seed **101** and compare its movement and shield behavior. A launch and a reported hit answer different questions. For a checkpoint comparison, repeat the same test with [lesson 2](tutorial-engage.md).

## Try it yourself

Compare the scanned coordinates with the requested aim point on a tick where the enemy has a visible velocity. Check the first sighting too, when velocity is unavailable. If you test against a cloaking opponent, watch for the pilot canceling an old order when it loses the target and using fresh coordinates when the target reappears.

[Pilot API](pilot-api.md#estimate-before-committing) · [Combat strategy](combat-strategy.md#track-before-you-fire) · [Next lesson](tutorial-sustain.md)
