DOM ENGINEV0.27.0FIZLET
Start

AIM V0.27.0 ALPHA

@fzlt/core/aim

Where bots and turrets point and when they may fire. Bound once to body arrays (a @fzlt/core/actor's or a steer's x, y, heading a, velocity vx, vy) and, for line of sight, a @fzlt/core/grid board; then everything takes rows: - lead(i, j, speed): where i must shoot a shot of speed px/s to meet j, moving at (vx, vy): the intercept of a straight shot and a straight mover, the earliest one ahead; - cone(i, j, half, range): j inside i's heading ± half radians and within range; - sees(i, j): no mask cell strictly between them on the board (Bresenham, symmetric), or the game's own clear(i, j); - pick(i, list, n, half, range): the nearest foe of i among list[0..n) in its cone and in sight (an actor table's list and n as they are). Results land in out[0..3] ([x, y, t, d]: aim point, seconds to impact or the off-heading angle, and distance), so no call returns a double: doubles returned or passed across a call V8 doesn't inline are boxed. Distances are @fzlt/core/steer's exact hypot (Math.hypot's bits, no heap number). DOM-free, stateless between calls, zero allocation.

@fzlt/core/aim · GZIP691 B0.7 KB of a 1.0 KB budget

Measured on this build: minified and gzipped with everything it imports, the way npm run size checks the budget in CI.

STATUS · what the flags meanALPHA

In the package to try; no promise yet. New (E11): no game aims on it yet; DOM Battlefield is first in line (src/ACTORS.md).

SINCE
v0.28.0
SHIPPED IN
no released game or page yet
MODULES · hosted on this site
import { aim } from "https://fizlet.com/dist/aim.js";
NPM · @fzlt/core
npm install @fzlt/core
import { aim } from "@fzlt/core/aim";
IN SHORT
const s = aim({ x: A.x, y: A.y, a: A.a, vx: A.vx, vy: A.vy, board,
  foe: (i, j) => A.alive[j] === 1 && A.side[j] !== A.side[i] });
const j = s.pick(i, A.list, A.n, 0.6, 460); // nearest foe in the cone, in sight
if (j >= 0 && s.lead(i, j, 400)) fire(i, s.out[0], s.out[1]); // where it will be

API

Every export with its signature, parameters, return type, doc comment and status flag, generated at build time from the published type declarations of v0.27.0 by the TypeScript compiler. For AI agents: the whole API as plain text.

AimOptions interface ALPHA

Options for aim(): the body arrays, an optional board and sight mask, and who counts as a foe.

export interface AimOptions {
    x: Float64Array;
    y: Float64Array;
    /** Headings in radians (for `cone`); without them every cone is a circle. */
    a?: Float64Array;
    /** Velocities in px/s (for `lead`); without them targets stand still. */
    vx?: Float64Array;
    vy?: Float64Array;
    /** Line of sight on this board (world px; its `cell` size); without one everything is in sight. */
    board?: Board;
    /** Cell flags that block sight (default 1: grid's BLOCKED; BLOCKED | COVER lets cover block). */
    mask?: number;
    /** Your own line of sight, used instead of `board` (a game whose tiles have their own rule). */
    clear?(i: number, j: number): boolean;
    /** May i shoot at j? (default: any j but i). With actors: `(i, j) => A.side[j] !== A.side[i]`. */
    foe?(i: number, j: number): boolean;
}

aim function ALPHA

Aiming bound to body arrays: lead, cone, sees and pick, results in out, zero allocation.

aim(o: AimOptions): Aim
Returns
Aim
Declaration
export declare function aim(o: AimOptions): {
    out: Float64Array<ArrayBuffer>;
    /** out = [aim x, aim y, seconds to impact, distance to the aim point]. False when the shot can
     *  never catch j (out is then j where it stands, t = -1). */
    lead(i: number, j: number, speed: number): boolean;
    /** j within `range` px and inside i's heading ± `half` rad. out = [j's x, y, angle off heading
     *  (signed, -π..π: turn by it), distance]. */
    cone(i: number, j: number, half: number, range: number): boolean;
    sees: (i: number, j: number) => boolean;
    /** The nearest foe of i among rows list[0..n) inside its cone and range and in sight; -1 if none.
     *  Skip dead rows with `foe` (e.g. `A.alive[j] && …`). Sight is checked last, and only for a row
     *  closer than the best so far, so a long list costs a sight check per closer candidate, not per row. out is the pick's. */
    pick(i: number, list: Int32Array, n: number, half: number, range: number): number;
};

Aim type ALPHA

The aimer aim() returns.

export type Aim = ReturnType<typeof aim>;