AI BEHAVIOR

AI behavior during combat is generally handled in-code, but a lot can also be customized.

AI BEHAVIOR FILE

Changes to the AI behavior are done through custom-made behavior (*.beh) files. The default files can be found in the Spg2\Behavior folder.
	OnNoticeEnemy:
	{
		Routine:
		{
			49	33
		}
		Routine:
		{
			8	33
		}
		Routine:
		{
			33	50
		}
		Routine:
		{
			35	50
		}
		Routine:
		{
			50	35
		}
		Routine:
		{
			33	65	50	57
		}
	}
	...
The file defines a series of events that can occur during the game, which tell a ship how to handle the situation. If an event is not present in the file, the ship handles such a situation with its default behavior pattern. Each behavior event is comprised of up to 6 routines, where each routine tells the ship exactly what to do and in which order. If more than 1 routine exists for a single event, the game chooses one routine randomly and executes it. Each routine is comprised of one or more AI chunks where each AI chunk represents a predefined action the ship takes (evasive maneuver, use equipment, flee to the nearest friendly station etc.).

AI BEHAVIOR EVENTS

Below is a list of all available events that each ship has at its disposal for custom modifications.
OnNoticeEnemy When a hostile object first enters the ship's sensor radius or decloaks
OnAttacked When the ship is attacked (NOT necessarily hit!)
OnHit When the ship is hit by weapons fire
OnCriticallyHit When the ship suffers a critical hit
OnHP75 When the ship's Hull drops to 75%
OnHP50 When the ship's Hull drops to 50%
OnHP25 When the ship's Hull drops to 25%
OnShieldDownOne When one of the ship's shields is depleted
OnShieldDownTwo When two of the ship's shields are depleted
OnShieldDownThree When three of the ship's shields are depleted
OnShieldDownAll When all of the ship's shields are depleted
OnSystemDisabled When one of the ship's primary systems gets disabled
OnWeaponsDisabled50 When half of the ship's weapon batteries get disabled
OnWeaponsDisabledAll When all of the ship's weapon batteries get disabled
OnEnemyCloak When a hostile ship within the AI ship's sensor radius cloaks
OnEnemyPTE When a hostile ship within the AI ship's sensor radius goes into Power to engines mode
OnBoarded When the AI ship gets boarded
OnGrappled When the AI ship gets grappled
OnEnemyFighterRelease When a hostile ship within the AI ship's sensor radius launches a fighter wing
OnOutnumbered When the ship is heavily outnumbered and outgunned. This event is fired off periodically.
OnAnomalyEntered When the ship enters the effective radius of an anomaly
OnFieldEntered When the ship enters an asteroid field, a junkyard or a nebula
OnPulse20 This event is fired off periodically, every 20 seconds.
OnPulse45 This event is fired off periodically, every 45 seconds.

AI ASSEMBLY

AI Assembly application can be found in the game root directory. It is the safest and the best way to modifiy existing behavior files or create completely new ones. Each AI chunk is described in the application so we won't address them here directly.

CombatAI DATABASE

If you're adding completely new behavior files, you MUST edit the CombatAI database. Otherwise the game won't even know the file exists, let alone use it.
	Id	Name	File	FactionId	ProfId
	0	DefaultSecurity	Spg2\Behavior\DefaultSecurity.beh	-1	0
Id Unique ID of the behavior. This is required for the database to function properly, but also for direct referencing when setting combat AI on fixed predefined ships.
Name Name of the behavior for easier recognition.
File Path to the actual behavior (*.beh) file.
FactionId Id of the faction that can use this behavior (->Base\Faction.wdt)
ProfId Id of the profession that can use this behavior (->Base\Profession.wdt

HOW THE GAME ASSIGNS THE BEHAVIOR AUTOMATICALLY

Assigning a behavior to a fixed ship, a ship placed directly into a sector is straightforward. You simply assign the behavior by its ID (->Base/CombatAI.wdt). But what about ships that are spawned by the game or via scripts? The CombatAI.wdt database is instrumental for this. More specifically the last two columns (FactionId & ProfId).

This is the general step-by-step procedure the game does:

  1. Get spawned ship faction and profession
  2. Search the behavior database for behaviors that correspond to the faction Id
  3. Search the filtered behavior results for behaviors that correspond to the profession Id
  4. Choose a behavior among the final results

Naturally, there can be situations where search results come out empty. In that case the game does several exceptions.

  1. If database search finds no results based on faction, it automatically skips to profession search and chooses based on that parameter.
  2. If database search manages to find results based on faction id, but no results are found based on profession id, then one of the first filtered results is chosen (based on faction id)
  3. If database search doesn't return ANY results based on any of the criteria, then a default behavior is assigned

LEAVING SOME CRITERIA UNDEFINED

Yep, this is possible. You can create your own default behaviors, based either on faction or profession. To do that, simply set the parameter to -1. Having both parameters (factionId & profId) set to -1, creates a default behavior that can be used for a ship of ANY profession and ANY faction.