Space AI Documentation

Table of Contents

Function Reference

Movement

Number thrust( [ power ] )

Optionally updates the power setting of the player ship's main engines to a value between 0 and 1, then returns the power setting. A thrust value of 0 corresponds to 0 acceleration, and a thrust value of 1 corresponds to 0.5 units/frame^2 of acceleration in the direction the player's ship is pointing.

Parameters
Returns: <Number>

The current power setting of the player ship's main engines.

Example:
thrust(1); // Sets the thrust power to full var thrustPower = thrust(); // Gets the current thrust power
Number turn( [ power ] )

Optionally updates the power setting of the player ship's turning engines to a value between 1 (counterclockwise) and -1 (clockwise), then returns the power setting. A turn value of 1 corresponds to 0.005 radians/frame^2 of angular acceleration and a turn value of -1 corresponds to -0.005 radians/frame^2 of angular acceleration.

Parameters
Returns: <Number>

The power setting of the player ship's turning engines.

Example:
turn(1); // Sets the turn power to fully counterclockwise var turnPower = turn(); // Gets the current turn power

Diagnostics

Vector accel()

Gets the x, y, and angular acceleration of the player ship. The x and y acceleration are computed from the angular position of the ship and the power setting of its main engines, and are given in pixels/frame^2. The angular acceleration is computed from the power setting of the ship's turning engines, and is given in radians/frame^2.

Returns: <Vector>

An object with numeric 'x', 'y', and 'angular' properties.

Example:
var a = accel(); console.log('accel.x: ' + a.x); console.log('accel.y: ' + a.y); console.log('accel.angular: ' + a.angular);
Rectangle bounds()

Gets the player ship's current bounding box.

Returns <Rectangle>

An object with numeric 'x', 'y', 'width', and 'height' properties.

Example:
var b = bounds(); console.log('bounds.x: ' + b.x); console.log('bounds.y: ' + b.y); console.log('bounds.width: ' + b.width); console.log('bounds.height: ' + b.height);
Number health()

Gets the player's current health.

Returns: <Number>
Example:
var currentHealth = health();
Vector pos()

Gets the x, y, and angular position of the player ship. The x and y position are given in pixels. The angular position, or rotation, is given in radians.

Returns: <Vector>

An object with numeric 'x', 'y', and 'angular' properties.

Example:
var p = pos(); console.log('pos.x: ' + p.x); console.log('pos.y: ' + p.y); console.log('pos.angular: ' + p.angular):
Vector vel()

Gets the x, y, and angular velocity of the player ship. The x and y velocity are given in pixels/frame. The angular velocity is given in radians/frame.

Returns: <Vector>

Extra info on the return value.

Example:
var v = vel(); console.log('vel.x: ' + v.x); console.log('vel.y: ' + v.y); console.log('vel.angular: ' + v.angular);

Radar

Array radar( [ filter ] )

Gets an array of game objects that the player ship can pick up on its sensors, optionally filtered by the given filter.

Parameters
Returns: <Array>

The array of GameObjects.

Example:
var asteroids = radar('asteroid'); var reachTarget = radar({ type: 'target', objective: 'reach' })[0];

See the radar usage example for more.

Weapons

void fire( x, y )

Fires the currently equipped weapon in the direction (x, y). If called more than once, the latest direction will be used. If x and y are both zero, the weapon will not be fired.

Parameters
Example:
fire(1, 0); // Fire in the (x = 1, y = 0) direction, along the positive x axis

See the weapons usage example for more.

void equip( [ weapon ] )

If weapon is of type String, the weapon with that name is equipped. If weapon is of type Weapon, the weapon with a name of weapon.name is equipped. If no matching weapon is found or weapon is not set, the current weapon is unequipped.

Parameters
Example:
equip('rocket');

See the weapons usage example for more.

Weapon equipped()

Returns the currently equipped weapon object, or null if no weapon is currently equipped.

Returns: <Weapon>

The currently equipped weapon.

Example:
var ammoRemaining = equipped().ammo;

See the weapons usage example for more.

Array weapons()

Gets an array of weapons available to the player ship.

Returns: <Array>

The array of available Weapon objects.

Example:
var allWeapons = weapons(); // Gets the array of available weapons to iterate over

See the weapons usage example for more.

Storage

Object load( key, [ defaultValue ] )

Loads a value with the given key from persistent storage.

Parameters
Returns: <Object>

The value stored at the given key, or the defaultValue.

Example:
var prevPos = load('previous position', pos());

See the persistent variables example for more.

void store( key, value )

Stores a value at the given key in persistent storage.

Parameters
Example:
store('previous position', pos());

See the persistent variables example for more.

Logging

void console.error( message )

Prints the given message to the console as an error. Pops out the console window if it is currently hidden.

Parameters
Example:
console.error('Unexpected condition!'); // Logs a string to the console as an error console.error(pos()); // Logs an object to the console as an error
void console.log( message )

Prints the given message to the console.

Parameters
Example:
console.log('Some text here!'); // Logs a string to the console as a plain message console.log(pos()); // Logs an object to the console as a plain message
void console.warn( message )

Prints the given message to the console as a warning.

Parameters
Example:
console.warn('Watch out for those asteroids!'); // Logs a string to the console as a warning console.warn(pos()); // Logs an object to the console as a warning