Merge consumable-ai r3 (cut consumable-ai; verdicts s2, s3)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-16 20:36:18 +02:00
commit e5d35352ee
4 changed files with 555 additions and 6 deletions

View file

@ -10,21 +10,41 @@ using float2x2 = CultMath.float2x2;
public class CombatState : BaseState
{
private const int DPS_SAMPLE_COUNT = 32;
// The Strength of one Track paint in this ship's own IncomingPaints: a fire-control emitter tracking this ship
// that hard (rulings lock-warning-strength, sensor-stat-set item 4, launcher-lock-solution-quality).
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 bool _optimumStale = true;
private readonly List<(int index, float dps)> _availableGroups = new List<(int index, float dps)>();
private readonly List<LockWeapon> _availableLockingWeapons = new List<LockWeapon>();
public CombatState(Agent agent) : base(agent)
{
SampleDps();
foreach (var weapon in _agent.Ship.Weapons)
{
weapon.Item.Online.Subscribe(b => SampleDps());
}
foreach (var item in _agent.Ship.Equipment) Watch(item);
_agent.Ship.Equipment.ObserveAdd().Subscribe(add => Watch(add.Value));
_agent.Ship.Equipment.ObserveRemove().Subscribe(_ => _optimumStale = true);
}
// The optimum is a function of the loadout's evaluated stats, so it is re-derived at the next Update (agents update
// before the entities, so by then the weapons that came online this tick have evaluated) whenever a weapon is
// added, removed or changes online state. Sampling on the event itself would read the stats before they exist: a
// ship the zone just spawned would keep an optimum of 0.
private void Watch(EquippedItem item)
{
if (item.Behaviors.OfType<Weapon>().Any()) item.Online.Subscribe(_ => _optimumStale = true);
}
public override void Update(float delta)
{
if (_optimumStale)
{
SampleDps();
_optimumStale = false;
}
var target = _agent.Ship.Target.Value.Entity;
if (target == null) return;
@ -78,6 +98,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)
@ -132,6 +154,27 @@ public class CombatState : BaseState
// Fire charged guns!
}
// The AI plays the player's rule on the player's information: vent when one Track paint on this ship passes half,
// otherwise overdrive to close a distant target ahead. It reads only this ship's own IncomingPaints. Both go
// through TryActivateConsumable, and a ship carrying neither does nothing.
private void UseConsumables(float3 toTarget, float targetDistance)
{
var ship = _agent.Ship;
if (ship.IncomingPaints.Any(p => p.Mode == PaintMode.Track && p.Strength > 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);
}
// One of a kind at a time: a Stackable design would otherwise be drained once per tick.
private void Use(Predicate<BehaviorData> kind)
{
if (_agent.Ship.HasActiveConsumable(kind)) return;
var consumable = _agent.Ship.FirstCarriedConsumable(kind);
if (consumable != null) _agent.Ship.TryActivateConsumable(consumable);
}
private void SampleDps()
{
var minRange = Single.MaxValue;

View file

@ -1350,8 +1350,20 @@ 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 HasActiveConsumable(Predicate<BehaviorData> kind) =>
_activeConsumables.Any(e => e.Data.Behaviors.Any(b => kind(b)));
public bool TryActivateConsumable(ConsumableItemData data)
{
// Death and docking Remove and Deactivate the ship while its agent keeps ticking.
if (!_active) return false;
if (!CanActivateConsumable(data)) return false;
var key = ItemManager.ItemData.RefOf<ItemData>(data).Key;