Consumable AI: agents vent when locked and overdrive to close, with tests

CombatState activates a vapour dump when an enemy lock on the ship passes .5,
else a throttle lock when the target is past twice the optimum range and
ahead within 15 degrees. Consumables are picked by behaviour kind through
Entity.FirstCarriedConsumable and activated through TryActivateConsumable.
Work in progress: tests not yet run (this commit may not pass them).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-10 06:16:38 +02:00
commit f270529f9b
3 changed files with 387 additions and 0 deletions

View file

@ -10,6 +10,11 @@ using float2x2 = CultMath.float2x2;
public class CombatState : BaseState
{
private const int DPS_SAMPLE_COUNT = 32;
// A lock past this fraction is one the target feels (ruling sensor-stat-set: a painted target always learns it).
private const float LockAlarm = .5f;
// Overdrive only to close a target farther than this many optimum ranges, while heading within this many degrees of it.
private const float OverdriveRangeFactor = 2f;
private const float OverdriveArcDegrees = 15f;
private float _optimumRange;
private readonly List<(int index, float dps)> _availableGroups = new List<(int index, float dps)>();
private readonly List<LockWeapon> _availableLockingWeapons = new List<LockWeapon>();
@ -76,6 +81,8 @@ public class CombatState : BaseState
foreach(var w in _availableLockingWeapons) w.Activate();
UseConsumables(toTarget, targetDistance);
var selectedGroup = -1;
var maxDps = Single.MinValue;
foreach (var group in _availableGroups)
@ -131,6 +138,26 @@ public class CombatState : BaseState
// Fire charged guns!
}
// The AI plays the player's rule on the player's information: vent when an enemy's lock on this ship passes half,
// otherwise overdrive to close a distant target ahead. Both go through TryActivateConsumable, and a ship carrying
// neither does nothing.
private void UseConsumables(float3 toTarget, float targetDistance)
{
var ship = _agent.Ship;
if (ship.VisibleEnemies.Any(e => e.Target.Value.Entity == ship &&
e.Weapons.OfType<LockWeapon>().Any(w => w.Lock > LockAlarm)))
Use(b => b is VapourDumpData);
else if (targetDistance > OverdriveRangeFactor * _optimumRange &&
dot(normalize(ship.Direction), normalize(toTarget.xz)) > cos(radians(OverdriveArcDegrees)))
Use(b => b is ThrottleLockData);
}
private void Use(Predicate<BehaviorData> kind)
{
var consumable = _agent.Ship.FirstCarriedConsumable(kind);
if (consumable != null) _agent.Ship.TryActivateConsumable(consumable);
}
private void SampleDps()
{
var minRange = Single.MaxValue;

View file

@ -885,6 +885,13 @@ public abstract class Entity
return data.Stackable || FindActiveConsumable(data) == null;
}
// The first consumable design in cargo with a behaviour of the asked kind, or null. Callers ask by kind, never
// by name: a design's name is a label and its behaviours are what it does.
public ConsumableItemData FirstCarriedConsumable(Predicate<BehaviorData> kind) =>
CargoBays.SelectMany(bay => bay.Cargo.Keys).OfType<ConsumableItem>()
.Select(item => ItemManager.GetData(item) as ConsumableItemData)
.FirstOrDefault(data => data != null && data.Behaviors.Any(b => kind(b)));
public bool TryActivateConsumable(ConsumableItemData data)
{
if (!CanActivateConsumable(data)) return false;