[explanation as of alpha 18]
The Ship AI can be roughly split into two parts:
Flight control. These are implemented in Ship-AI.cpp and are mostly members of Ship. These functions mostly implement a single timestep of a low level control routine, e.g., ship->AIMatchVel(v) sets the ship's thrusters to attempt to match the specified velocity (in the ship's parent frame), including counteracting external forces acting on the ship (gravity).
AI commands. In ShipAICmd.h/cpp. These implement high level AI routines: docking, flying to a particular body, orbiting a body, combat, or holding position
Some AI commands delegate behaviour to a sub-command in some circumstances. For example, if the fly-to AI is active and the ship is going to collide with a planet, fly-to will delegate to the fly-around AI (orbiting) to safely navigate around the planet before continuing towards the destination. If the fly-around AI is active but the target is too far away, then it delegates to the fly-to AI until it's close enough to enter an orbit.
The AI commands do their work in bool AICommand::TimeStepUpdate(), which is called from Ship::AITimeStep(). The AI command returns true from TimeStepUpdate to indicate that it has completed its task (e.g., AICmdKill returns true when it's lost or killed its target)
The combat AI does up to three things each time-step:
- Adjust its target heading by calculating the target's lead position, and if the target is close enough and target lead direction is close enough to the ship's current heading, then it fires the lasers.
- Apply side and/or vertical thrust (always full thrust) to evade fire from the target.
- Apply forward or reverse thrust to try to keep the target at a good combat distance.
These adjustments are not made every frame. The gap between each adjustment is picked randomly and affected by the two skill parameters: skillShoot and skillEvade. Heading adjustment is recalculated with a gap of under a second (unless skillShoot is really high), evasion adjustment is recalculated with a gap around 1 to 15 seconds (3 to 10, multiplied by skillEvade), range-closing adjustment is recalculated with a gap of around 0.25 to 10 seconds (1 to 5, multiplied by skillEvade).
These skill parameters also affect various other things. Smaller skill values give better skill.
- skillShoot affects:
- How often heading adjustments are made.
- How accurately the AI aims towards the calculated lead position (not how accurately the lead position is calculated)
- How close (actually, how big in the viewer) the target ship must be before the AI will fire the lasers.
- skillEvade affects:
- How often evasion thrust is changed.
- How smart to be when choosing which direction to evade in (there are 4 ways of picking evasion thrust, from which one is chosen based on skillEvade and a few rules to bias or eliminate in-applicable choices)
- How often range-closing thrust is changed.
- How close the desired target distance is from an 'ideal' value of 500 metres. Note that since this desired distance is only used to calculate a thrust level that is maintained (without feedback) till the next range-closing adjustment (at which point a new desired distance is picked), the skill adjustment here affects closing velocity more than average range to target.