Difference between revisions of "OXP Scripted AI"

From Elite Wiki
(Scripted AI documentation)
 
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''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 <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)  
 
  {
 
  {
 
     // delta = duration of frame. Often irrelevant unless you're doing
 
     // delta = duration of frame. Often irrelevant unless you're doing
     // ultra-high-precision turns, or want a manouevre to last a particular
+
     // ultra-high-precision turns, or want a maneuvre to last a particular
 
     // length of time
 
     // length of time
 
   
 
   
Line 17: Line 15:
 
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.
 
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.
  
* <code>stickRoll</code>: number between -ship.maxRoll and [[Oolite_JavaScript_Reference:_Ship#maxRoll|ship.maxRoll]]
+
* <code>stickRoll</code>: roll rate in radians per second. A number between -ship.maxRoll and [[Oolite JavaScript Reference: Ship#maxRoll|ship.maxRoll]]
* <code>stickPitch</code>: the same for [[Oolite_JavaScript_Reference:_Ship#maxPitch|ship.maxPitch]]
+
* <code>stickRollFactor</code>: number between -1 and +1 to set a proportion of ship.maxRoll. Overrules <code>stickRoll</code> if both are present.
* <code>stickYaw</code>: the same for [[Oolite_JavaScript_Reference:_Ship#maxYaw|ship.maxYaw]]
+
* <code>stickPitch</code>: the same for [[Oolite JavaScript Reference: Ship#maxPitch|ship.maxPitch]]
* <code>desiredSpeed</code>: number 0 or greater. If this is above [[Oolite_JavaScript_Reference:_Ship#maxSpeed|ship.maxSpeed]], and the ship has usable injectors, it will use them.
+
* <code>stickPitchFactor</code>: as <code>stickRollFactor</code> but for pitch
 +
* <code>stickYaw</code>: the same for [[Oolite JavaScript Reference: Ship#maxYaw|ship.maxYaw]]
 +
* <code>stickYawFactor</code>: as <code>stickRollFactor</code> but for yaw
 +
* <code>desiredSpeed</code>: number 0 or greater. If this is above [[Oolite JavaScript Reference: Ship#maxSpeed|ship.maxSpeed]], and the ship has usable injectors, it will use them.
 +
* <code>desiredSpeedFactor</code>: number 0 or greater. 1 represents ship.maxSpeed, but higher speeds are possible with injectors. Overrules <code>desiredSpeed</code> if both are present.
 
* <code>chosenWeapon</code>: "FORWARD", "AFT", "PORT" or "STARBOARD". Ignored outside the attack version of the AI.
 
* <code>chosenWeapon</code>: "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.
 
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.
Line 39: Line 42:
  
 
There is no direct support for missiles, ECM, and other ship equipment, but you can call <code>ship.fireMissile()</code> or <code>ship.fireECM()</code> from within the <code>scriptedAI</code> function.
 
There is no direct support for missiles, ECM, and other ship equipment, but you can call <code>ship.fireMissile()</code> or <code>ship.fireECM()</code> from within the <code>scriptedAI</code> function.
 +
 +
--------------------------------------------------------------------------------
 +
[[Category:Oolite scripting]]

Latest revision as of 21:35, 3 March 2022

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 maneuvre 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: roll rate in radians per second. A number between -ship.maxRoll and ship.maxRoll
  • stickRollFactor: number between -1 and +1 to set a proportion of ship.maxRoll. Overrules stickRoll if both are present.
  • stickPitch: the same for ship.maxPitch
  • stickPitchFactor: as stickRollFactor but for pitch
  • stickYaw: the same for ship.maxYaw
  • stickYawFactor: as stickRollFactor but for yaw
  • desiredSpeed: number 0 or greater. If this is above ship.maxSpeed, and the ship has usable injectors, it will use them.
  • desiredSpeedFactor: number 0 or greater. 1 represents ship.maxSpeed, but higher speeds are possible with injectors. Overrules desiredSpeed if both are present.
  • 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.