Merge master (fire control 12) into codex/moddable-ships

This commit is contained in:
Meta Vens 2026-09-30 02:21:09 +02:00
commit 18e12d81ce
37 changed files with 4947 additions and 395 deletions

View file

@ -405,11 +405,8 @@ public class ActionGameManager : MonoBehaviour
// Cut 2 (docs/fire-control-cut.md): cycles the aim point among the current target's revealed
// subsystems -- decides nothing itself, only calls the one writer (TrySelectTargetItem), same
// predicate (FireControl.IsRevealed) the AI path uses. No generated typed accessor exists for this
// action yet (it was hand-authored into Aetheria.inputactions rather than regenerated through the
// Unity Input Actions editor, unavailable here), so it is looked up by name instead of through
// Input.Player.
Input.asset.FindAction("Player/Cycle Target Item").performed += context =>
// predicate (FireControl.IsRevealed) the AI path uses.
Input.Player.CycleTargetItem.performed += context =>
{
var target = CurrentEntity.Target.Value;
if (target == null) return;
@ -521,10 +518,29 @@ public class ActionGameManager : MonoBehaviour
var itemName = string.Join(" ", args);
var item = ItemManager.ItemData.GetAll<EquippableItemData>()
.FirstOrDefault(itemData => string.Equals(itemData.Name, itemName, StringComparison.InvariantCultureIgnoreCase));
if (item != null)
if (item == null)
{
_currentEntity.CargoBays.First().TryStore(ItemManager.CreateInstance(ItemManager.CreateLot(item, default, .95f)));
ConsoleController.Instance.AppendLogLine($"No equippable item named \"{itemName}\"");
return;
}
if (item is HullData hull)
{
if (hull.HullType != HullType.Ship)
{
ConsoleController.Instance.AppendLogLine($"Refused: {item.Name} is a {hull.HullType} hull, not a ship");
return;
}
if (DockedEntity == null)
{
ConsoleController.Instance.AppendLogLine($"Refused: dock to take delivery of a {item.Name}");
return;
}
CommissionShip(ItemManager.CreateInstance(ItemManager.CreateLot(item, default, .95f)) as EquippableItem);
ConsoleController.Instance.AppendLogLine($"{item.Name} moored at {DockedEntity.Name}");
return;
}
if (!_currentEntity.CargoBays.First().TryStore(ItemManager.CreateInstance(ItemManager.CreateLot(item, default, .95f))))
ConsoleController.Instance.AppendLogLine($"Refused: no cargo space for {item.Name}");
});
ConsoleController.AddCommand("trackmissile",
@ -535,7 +551,7 @@ public class ActionGameManager : MonoBehaviour
missileManager.OnFireGuided.Where(x => x.source == _currentEntity).Take(1).Subscribe(x =>
{
FollowCamera.Follow = x.missile.transform;
FollowCamera.LookAt = x.target;
FollowCamera.LookAt = x.target ? x.target : x.missile.transform;
x.missile.OnKill += () =>
{
FollowCamera.LookAt = ZoneRenderer.EntityInstances[CurrentEntity].LookAtPoint;
@ -877,6 +893,15 @@ public class ActionGameManager : MonoBehaviour
}
}
// A ship hull acquired while docked becomes a bare player ship moored at the docked entity. It takes no docking
// bay: mothballed ships do not need one. Buying from the trade menu and the give command both land here.
public Ship CommissionShip(EquippableItem hull)
{
var ship = new Ship(ItemManager, Zone, hull, ItemManager.GameplaySettings.DefaultEntitySettings) { IsPlayerShip = true };
ship.SetParent(DockedEntity);
return ship;
}
private void DoDock(Entity entity, EquippedDockingBay dockingBay)
{
TradeMenu.Inventory = entity.CargoBays.First();
@ -1282,6 +1307,13 @@ public class ActionGameManager : MonoBehaviour
}
}
private static string ResultLabel(ShotResult result) => result switch
{
ShotResult.Hit => "HIT",
ShotResult.Burst => "BURST",
_ => "MISS"
};
private void UpdateFireControlDebug(Entity target)
{
if (DebugInfoText == null) return;
@ -1310,7 +1342,7 @@ public class ActionGameManager : MonoBehaviour
var shot = CurrentEntity.Zone.PendingShots[i];
if (shot.Source != CurrentEntity || shot.Weapon != selectedItem) continue;
if (shot.Committed)
pendingLine = $"shot {shot.ShotId}: committed {(shot.Outcome.Hit ? "HIT" : "MISS")}";
pendingLine = $"shot {shot.ShotId}: committed {ResultLabel(shot.Outcome.Result)}";
else
{
var pDeviation = FireControl.DeviationProbability(shot, CurrentEntity.Zone.Time, out var deviation);
@ -1324,11 +1356,16 @@ public class ActionGameManager : MonoBehaviour
}
var lastLine = _debugLastShots.TryGetValue(selectedItem, out var lastShot)
? $"last {lastShot.ShotId}: {(lastShot.Hit ? "HIT" : "MISS")} cell {lastShot.Cell.x},{lastShot.Cell.y}"
? $"last {lastShot.ShotId}: {ResultLabel(lastShot.Result)} cell {lastShot.Cell.x},{lastShot.Cell.y}"
: "last: none";
var gates = target == null
? "target: none"
: $"gates vis {d.Visible} range {d.InRange} arc {d.InArc} lock {d.Locked}";
: $"gates designated {d.Designated} arc {d.InArc}";
// A fused weapon's forecast is its outcome, not a hit chance: the burst point or the refusal.
var outcomeLine = d.Outcome == FireOutcome.Burst ? $"burst at {d.BurstReach:F0}"
: d.Outcome == FireOutcome.Refused ? "refused: arming distance exceeds range"
: $"base {d.PBase:P1}";
DebugInfoText.text =
$"FIRE CONTROL - {selectedItem.Data.Name}\n" +
@ -1337,7 +1374,7 @@ public class ActionGameManager : MonoBehaviour
$"info {d.Info:F3}/{d.InfoDemandCeiling:F3} sensor {d.PSensor:F3}\n" +
$"accuracy {d.Accuracy:F3} spread {d.PSpread:F3} hull {d.POnHull:F3}\n" +
$"precision {d.Precision:F3} tracking {d.Tracking:F1}\n" +
$"base {d.PBase:P1}\n" +
$"{outcomeLine}\n" +
$"{pendingLine}\n" +
lastLine;
}

View file

@ -203,8 +203,11 @@ public class EntityInstance : MonoBehaviour
// Cut 3 (docs/fire-control-cut.md): carries the ShotId FireControl.Fire assigned so the
// effect manager (and whatever it spawns) can bind its presentation to Zone.ShotCommitted /
// ShotResolved instead of deciding or applying anything itself.
// The target handed to the effect is the shot's own, not the shooter's selection: a fused round
// whose selected target was invalid, or out of arc, flies at none (FireControl.Fire) and the
// effect must not chase one.
instantWeapon.OnFire += shotId =>
_instantWeaponManagers[data].Fire(instantWeapon, item, this, entity.Target.Value != null && ZoneRenderer.EntityInstances.ContainsKey(entity.Target.Value) ? ZoneRenderer.EntityInstances[entity.Target.Value] : null, shotId);
_instantWeaponManagers[data].Fire(instantWeapon, item, this, ShotTarget(shotId), shotId);
if (behavior is ChargedWeapon chargedWeapon)
{
@ -336,6 +339,10 @@ public class EntityInstance : MonoBehaviour
}
}
private EntityInstance ShotTarget(int shotId) =>
Entity.Zone.TryGetShot(shotId, out var shot) && shot.Target != null &&
ZoneRenderer.EntityInstances.TryGetValue(shot.Target, out var instance) ? instance : null;
private void OnSensorPingEnd()
{
Destroy(_currentPing.transform.gameObject);

View file

@ -35,6 +35,7 @@ public class GuidedProjectile : MonoBehaviour
private float _prevDist;
private bool _active;
private bool _alive;
private float _spawnTime;
private Vector3 _targetVelocity;
private Vector3 _previousTargetPosition;
@ -46,11 +47,74 @@ public class GuidedProjectile : MonoBehaviour
public Vector3 Velocity { get; set; }
public Entity SourceEntity { get; set; }
// How long after spawn the round keeps its binding to the simulation's verdict: the shot's own flight time (the
// manager sets it from the committed shot's ArrivalTime), so a round that faded out early still lives to show
// the detonation the simulation resolves at arrival. 0 (the default) kills the round when its fade ends.
public float BindingLifetime { get; set; }
public event Action OnKill;
// The simulation's verdict on this round, bound by GuidedProjectileManager to Zone.ShotCommitted and
// ShotResolved for its ShotId: this object flies and looks, and neither decides where the round bursts nor when.
// The one owner of the subscription's lifetime: a new binding disposes the one it replaces, Kill disposes it,
// and so does destruction, so a projectile torn down with its scene never leaves an observer on the zone's
// subjects to touch a destroyed transform.
public IDisposable Binding
{
get => _binding;
set
{
_binding?.Dispose();
_binding = value;
}
}
private IDisposable _binding;
void OnDestroy() => Binding = null;
// The commit named a burst point: fly to it, whatever this round was homing on (a target it would have
// passed, or a Range clamp it would have overshot). The point is a static target, so the old target's motion
// is dropped with it: the jump to the new point must not feed the guidance a target velocity.
public void BurstAt(Vector3 point)
{
Target = null;
TargetPosition = () => point;
_previousTargetPosition = point;
_targetVelocity = Vector3.zero;
}
// The resolution. A round that detonated (a hit, or a burst) bursts now, at the simulation's burst point when
// it has one; a round the simulation resolved without a detonation (a contact or delayed round that missed,
// one whose target is gone) just falls away, with no explosion to show.
public void Resolve(bool detonated, Vector3? point)
{
// A round that already faded out (overshot, out of flight) still shows the simulation's detonation: only a
// round that has been killed is done. The fade is stopped so it cannot Kill the round a second time.
if (!_alive) return;
if (!detonated)
{
if (_active) StartCoroutine(FadeOut());
return;
}
StopAllCoroutines();
// A hit carries no burst point: the effect plays where the round was homing (a round that faded out has stopped
// short of it), and is skipped when the round never knew where its target was.
Vector3? effectAt = point ?? (TargetPosition != null ? TargetPosition() : Target ? Target.position : (Vector3?) null);
if (point.HasValue) transform.position = point.Value;
if (HitEffect != null && effectAt.HasValue)
{
var ht = HitEffect.Instantiate<Transform>();
ht.position = effectAt.Value;
}
StartCoroutine(Kill());
}
void OnEnable()
{
_active = _alive = true;
_spawnTime = Time.time;
BindingLifetime = 0f; // a pooled round starts unbound; the manager binds it after enabling
_phase = Random.value * 100;
_prevDist = Single.MaxValue;
Particles.startColor = Color.white;
@ -84,14 +148,11 @@ public class GuidedProjectile : MonoBehaviour
var targetDist = diff.magnitude;
var sourceDist = length(StartPosition.xz - position.xz);
// Out of range, or flown past its target: the round falls away. Only the simulation's detonation
// (Resolve) shows an explosion; a round that merely ran out of flight has none.
if (sourceDist > Range || dot(diff.ToCultMath(), Velocity.ToCultMath()) < 0)
{
StartCoroutine(FadeOut());
if (HitEffect != null)
{
var ht = HitEffect.Instantiate<Transform>();
ht.position = t.position;
}
return;
}
_prevDist = targetDist;
@ -165,11 +226,16 @@ public class GuidedProjectile : MonoBehaviour
yield return null;
}
// The wait holds still: a round waiting on the verdict must not drift off its last position.
Velocity = Vector3.zero;
// Faded, but the simulation's verdict has not necessarily arrived: wait for it until the shot's flight is over.
while (Time.time - _spawnTime < BindingLifetime) yield return null;
StartCoroutine(Kill());
}
IEnumerator Kill()
{
Binding = null;
_active = false;
_alive = false;
Particles.Stop(true, ParticleSystemStopBehavior.StopEmitting);

View file

@ -9,6 +9,22 @@ using static CultMath.math;
public class GuidedProjectileManager : InstantWeaponEffectManager
{
// Presentation follows the committed shot (FireControl): the round retargets to the burst point the commit
// names and bursts when the shot resolves, rather than predicting either from its own homing.
private static void Bind(GuidedProjectile p, Zone zone, int shotId)
{
Vector3 At(ShotOutcome o) => new Vector3(o.BurstPoint.x, p.transform.position.y, o.BurstPoint.y);
var bindings = new CompositeDisposable();
zone.ShotCommitted.Where(o => o.ShotId == shotId && o.HasBurstPoint).Subscribe(o => p.BurstAt(At(o))).AddTo(bindings);
zone.ShotResolved.Where(o => o.ShotId == shotId)
.Subscribe(o => p.Resolve(o.Result == ShotResult.Hit || o.Result == ShotResult.Burst, o.HasBurstPoint ? At(o) : (Vector3?) null))
.AddTo(bindings);
// The binding outlives a fade-out until the shot's own arrival (the moment the simulation resolves it), plus a
// second of margin; a shot no longer pending has nothing left to resolve.
p.BindingLifetime = zone.TryGetShot(shotId, out var shot) ? max(0f, shot.ArrivalTime - zone.Time) + 1f : 0f;
p.Binding = bindings; // replaces, and so disposes, any binding the projectile already had
}
public Prototype ProjectilePrototype;
public Subject<(Entity source, Transform target, GuidedProjectile missile)> OnFireGuided = new Subject<(Entity source, Transform target, GuidedProjectile missile)>();
@ -17,12 +33,17 @@ public class GuidedProjectileManager : InstantWeaponEffectManager
{
if(weapon.Data is LauncherData launcher)
{
if (target == null) return;
// A round the simulation fired at no target (FireControl.Fire: a fused round with no valid targeting
// data, or out of arc) flies to the point it bursts at; one with no target and no burst is not a shot.
PendingShot shot = default;
if (target == null && !(source.Entity.Zone.TryGetShot(shotId, out shot) && shot.Fuse != null)) return;
var burst = shot.BurstPosition.ToUnity();
var p = ProjectilePrototype.Instantiate<GuidedProjectile>();
p.ShotId = shotId;
p.Source = source.transform;
p.SourceEntity = source.Entity;
p.Target = target.transform;
if (target != null) p.Target = target.transform;
else p.TargetPosition = () => burst;
p.Frequency = launcher.DodgeFrequency;
var hp = source.Entity.Hardpoints[item.Position.x, item.Position.y];
var barrel = source.GetBarrel(hp);
@ -34,7 +55,9 @@ public class GuidedProjectileManager : InstantWeaponEffectManager
p.Velocity = barrel.forward * weapon.Velocity;
p.Thrust = item.Evaluate(launcher.Thrust);
p.TopSpeed = item.Evaluate(launcher.MissileVelocity);
OnFireGuided.OnNext((source.Entity, target.transform, p));
Bind(p, source.Entity.Zone, shotId);
// A round with no target (a fused round fired at none) has no transform to hand a listener.
OnFireGuided.OnNext((source.Entity, target != null ? target.transform : null, p));
}
else if(weapon.Data is GuidedWeaponData guidance)
{
@ -54,6 +77,7 @@ public class GuidedProjectileManager : InstantWeaponEffectManager
p.Thrust = item.Evaluate(guidance.Thrust);
p.TopSpeed = item.Evaluate(guidance.MissileVelocity);
p.TargetPosition = () => (source.Entity.Position + length(source.LookAtPoint.position.ToCultMath() - source.Entity.Position) * source.Entity.LookDirection).ToUnity();
Bind(p, source.Entity.Zone, shotId);
}
else Debug.LogError($"Weapon {item.Data.Name} linked to {name} effect, but is not a Launcher!");
}