Difference between revisions of "OXP Scripted AI"
m (added category) |
m (Page moved. No need for _ in links.) |
||
Line 1: | Line 1: | ||
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 [[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>[[ |
+ | 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 |
+ | // ultra-high-precision turns, or want a maneuvre to last a particular |
// length of time |
// length of time |
||
Line 15: | 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 [[ |
+ | * <code>stickRoll</code>: number between -ship.maxRoll and [[Oolite JavaScript Reference: Ship#maxRoll|ship.maxRoll]] |
* <code>stickRollFactor</code>: number between -1 and +1 to set a proportion of ship.maxRoll. Overrules <code>stickRoll</code> if both are present. |
* <code>stickRollFactor</code>: number between -1 and +1 to set a proportion of ship.maxRoll. Overrules <code>stickRoll</code> if both are present. |
||
− | * <code>stickPitch</code>: the same for [[ |
+ | * <code>stickPitch</code>: the same for [[Oolite JavaScript Reference: Ship#maxPitch|ship.maxPitch]] |
* <code>stickPitchFactor</code>: as <code>stickRollFactor</code> but for pitch |
* <code>stickPitchFactor</code>: as <code>stickRollFactor</code> but for pitch |
||
− | * <code>stickYaw</code>: the same for [[ |
+ | * <code>stickYaw</code>: the same for [[Oolite JavaScript Reference: Ship#maxYaw|ship.maxYaw]] |
* <code>stickYawFactor</code>: as <code>stickRollFactor</code> but for yaw |
* <code>stickYawFactor</code>: as <code>stickRollFactor</code> but for yaw |
||
− | * <code>desiredSpeed</code>: number 0 or greater. If this is above [[ |
+ | * <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>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. |
Latest revision as of 21:30, 16 January 2016
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
: number between -ship.maxRoll and ship.maxRollstickRollFactor
: number between -1 and +1 to set a proportion of ship.maxRoll. OverrulesstickRoll
if both are present.stickPitch
: the same for ship.maxPitchstickPitchFactor
: asstickRollFactor
but for pitchstickYaw
: the same for ship.maxYawstickYawFactor
: asstickRollFactor
but for yawdesiredSpeed
: 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. OverrulesdesiredSpeed
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.