Logic Bots
Chapters

Lesson 2. Close and fire

All lessons · Previous lesson · Next lesson

Start with the bot from Lesson 1. Keep the Mediumweight Relay · Energy (medium-energy), with a Pulse Laser in slot 0, a Shield Generator in slot 1, and a Nanobot Repair System in slot 2. Builder numbers those mounts 1, 2, and 3.

This lesson keeps the pickup helper and adds an engagement decision. With a visible enemy, the bot approaches until it is 3 arena units inside the Pulse Laser’s effective range. It stops issuing movement commands at that distance and requests a shot when the Pulse Laser is ready. With no visible enemy, it looks for a useful pickup.

Replace the pilot

Open PHP source, replace the previous lesson’s script with this full version, and select Compile.

<?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);
}

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

$weapon = $pilot->mountState(0);
if ($weapon === null) {
    return;
}

$x = $enemy['x'];
$z = $enemy['z'];
$engagementDistance = max(1.0, $weapon['effectiveRange'] - 3.0);
if ($enemy['distance'] > $engagementDistance) {
    $pilot->moveTo($x, $z);
    return;
}

if ($weapon['ready']) {
    $pilot->fire(0, $x, $z);
}

Reproduce the first shot

  1. Open Practice. Choose Solo combat, set seed 77, and choose Basic Bot as First opponent. Select Test run.
  2. Open the decision trace. The initial enemy distance is about 29.76 units. The first tick queues movement. Watch that gap shrink before the first shot request.
  3. Look near 1.15 simulated seconds for the Pulse Laser discharge. On the following tick, the trace reports a shotResolved event with outcome: hit. Follow the opponent’s hull bar to see the effect.

This fixture puts the opening decision in a one-on-one match. A request from fire(0, $x, $z) gives the turret a point to aim at. Commands shows the request and Outcomes reports the result. A launch and a confirmed hit are separate events.

Try one range change

Change the 3.0 margin in $engagementDistance to 5.0 and repeat the same fixture. Compare the distance at the first shot, time to first hit, and hull remaining. Save the version you want to keep after comparing the runs.

The 24-unit threshold is this pilot’s decision. The Pulse Laser’s maximum range is 47 units and its base effective range is 27. Keep the movement and firing conditions aligned if you change the policy. If you tune them separately, leave no gap where the bot waits for a shot it refuses to take.

The target coordinates are from the current scan. A moving opponent can leave that point before the turret aligns. In the next lesson, you’ll keep the turret tracking and use firing estimates to deal with that delay.

See Combat strategy, Testing and iteration, and the Glossary.