Difference between revisions of "OXP Scripted AI"

From Elite Wiki
(Scripted AI documentation)
 
m (link to AI page)
Line 3: Line 3:
 
Scriptable flight AI provides an intermediate level of control between moving a ship with a frame callback, and using stock AI routines such as <code>performAttack</code>
 
Scriptable flight AI provides an intermediate level of control between moving a ship with a frame callback, and using stock AI routines such as <code>performAttack</code>
  
In the AI, use the command "<code>performScriptedAI</code>" or "<code>performScriptedAttackAI</code>" to put the ship into scriptable flight AI mode. It will then remain in this behaviour mode until you switch it to a different mode in the AI, or an error occurs in the script. While in this mode, the <code>[[Oolite_JavaScript_Reference:_ship_script_event_handlers#scriptedAI|scriptedAI]]</code> function in the ship script is called each frame to determine the behaviour.
+
In the [[OXP_howto_AI#Perform_Action|AI]], use the command "<code>performScriptedAI</code>" or "<code>performScriptedAttackAI</code>" to put the ship into scriptable flight AI mode. It will then remain in this behaviour mode until you switch it to a different mode in the AI, or an error occurs in the script. While in this mode, the <code>[[Oolite_JavaScript_Reference:_ship_script_event_handlers#scriptedAI|scriptedAI]]</code> function in the ship script is called each frame to determine the behaviour.
  
 
  this.scriptedAI = function(delta)  
 
  this.scriptedAI = function(delta)  

Revision as of 09:38, 18 August 2012

This page describes functionality planned for 1.77 which may be subject to significant changes

Scriptable flight AI provides an intermediate level of control between moving a ship with a frame callback, and using stock AI routines such as performAttack

In the AI, use the command "performScriptedAI" or "performScriptedAttackAI" to put the ship into scriptable flight AI mode. It will then remain in this behaviour mode until you switch it to a different mode in the AI, or an error occurs in the script. While in this mode, the scriptedAI function in the ship script is called each frame to determine the behaviour.

this.scriptedAI = function(delta) 
{
   // delta = duration of frame. Often irrelevant unless you're doing
   // ultra-high-precision turns, or want a manouevre to last a particular
   // length of time

   // calculations happen here
   return flightParameters;  
}

The function gets a virtual joystick to control the ship with, by returning an object with up to 5 properties. All properties are optional, though returning an object is not. The default is level flight, at zero speed, with the forward weapon selected. Numbers outside the allowed range will be corrected to the nearest allowed value.

  • stickRoll: number between -ship.maxRoll and ship.maxRoll
  • stickPitch: the same for ship.maxPitch
  • stickYaw: the same for ship.maxYaw
  • desiredSpeed: number 0 or greater. If this is above ship.maxSpeed, and the ship has usable injectors, it will use them.
  • chosenWeapon: "FORWARD", "AFT", "PORT" or "STARBOARD". Ignored outside the attack version of the AI.

The ship will then respond to the virtual joystick. If a large change is requested - say the ship is in level flight, and the virtual joystick is moved to maximum clockwise roll - then it will take some time for the ship to respond and accelerate to that roll rate: quite possibly more than one frame.

If the attack version of the AI is selected, and the ship's current target is in the sights of the selected weapon (and there is a weapon there, and it's ready to fire, etc.), the weapon will be fired.

For example:

this.scriptedAI = function(delta) 
{
  return { stickPitch: -0.2;
           desiredSpeed: this.ship.maxSpeed;
           chosenWeapon: "PORT"; };
}

will make the ship perform a fairly wide loop, and in attack mode, firing its port laser if the target crosses its sights. Might be a decent evasive attack if the target is the size of a small moon...

Since the function is called once per frame, it needs to be quick. Searching for entities and new targets, for instance, can take some time.

There is no direct support for missiles, ECM, and other ship equipment, but you can call ship.fireMissile() or ship.fireECM() from within the scriptedAI function.