using Content.Server.NPC.Components; using Content.Shared.CombatMode; using Content.Shared.Interaction; using Robust.Shared.Map; using Robust.Shared.Physics.Components; namespace Content.Server.NPC.Systems; public sealed partial class NPCCombatSystem { [Dependency] private readonly SharedCombatModeSystem _combat = default!; [Dependency] private readonly RotateToFaceSystem _rotate = default!; // TODO: Don't predict for hitscan private const float ShootSpeed = 20f; /// /// Cooldown on raycasting to check LOS. /// public const float UnoccludedCooldown = 0.2f; private void InitializeRanged() { SubscribeLocalEvent(OnRangedStartup); SubscribeLocalEvent(OnRangedShutdown); } private void OnRangedStartup(EntityUid uid, NPCRangedCombatComponent component, ComponentStartup args) { if (TryComp(uid, out var combat)) { _combat.SetInCombatMode(uid, true, combat); } else { component.Status = CombatStatus.Unspecified; } } private void OnRangedShutdown(EntityUid uid, NPCRangedCombatComponent component, ComponentShutdown args) { if (TryComp(uid, out var combat)) { _combat.SetInCombatMode(uid, false, combat); } } private void UpdateRanged(float frameTime) { var bodyQuery = GetEntityQuery(); var xformQuery = GetEntityQuery(); var combatQuery = GetEntityQuery(); var query = EntityQueryEnumerator(); while (query.MoveNext(out var uid, out var comp, out var xform)) { if (comp.Status == CombatStatus.Unspecified) continue; if (!xformQuery.TryGetComponent(comp.Target, out var targetXform) || !bodyQuery.TryGetComponent(comp.Target, out var targetBody)) { comp.Status = CombatStatus.TargetUnreachable; comp.ShootAccumulator = 0f; continue; } if (targetXform.MapID != xform.MapID) { comp.Status = CombatStatus.TargetUnreachable; comp.ShootAccumulator = 0f; continue; } if (combatQuery.TryGetComponent(uid, out var combatMode)) { _combat.SetInCombatMode(uid, true, combatMode); } if (!_gun.TryGetGun(uid, out var gunUid, out var gun)) { comp.Status = CombatStatus.NoWeapon; comp.ShootAccumulator = 0f; continue; } comp.LOSAccumulator -= frameTime; var (worldPos, worldRot) = _transform.GetWorldPositionRotation(xform, xformQuery); var (targetPos, targetRot) = _transform.GetWorldPositionRotation(targetXform, xformQuery); // We'll work out the projected spot of the target and shoot there instead of where they are. var distance = (targetPos - worldPos).Length; var oldInLos = comp.TargetInLOS; // TODO: Should be doing these raycasts in parallel // Ideally we'd have 2 steps, 1. to go over the normal details for shooting and then 2. to handle beep / rotate / shoot if (comp.LOSAccumulator < 0f) { comp.LOSAccumulator += UnoccludedCooldown; comp.TargetInLOS = _interaction.InRangeUnobstructed(uid, comp.Target, distance + 0.1f); } if (!comp.TargetInLOS) { comp.ShootAccumulator = 0f; comp.Status = CombatStatus.TargetUnreachable; continue; } if (!oldInLos && comp.SoundTargetInLOS != null) { _audio.PlayPvs(comp.SoundTargetInLOS, uid); } comp.ShootAccumulator += frameTime; if (comp.ShootAccumulator < comp.ShootDelay) { continue; } var mapVelocity = targetBody.LinearVelocity; var targetSpot = targetPos + mapVelocity * distance / ShootSpeed; // If we have a max rotation speed then do that. var goalRotation = (targetSpot - worldPos).ToWorldAngle(); var rotationSpeed = comp.RotationSpeed; if (!_rotate.TryRotateTo(uid, goalRotation, frameTime, comp.AccuracyThreshold, rotationSpeed?.Theta ?? double.MaxValue, xform)) { continue; } // TODO: LOS // TODO: Ammo checks // TODO: Burst fire // TODO: Cycling // Max rotation speed // TODO: Check if we can face if (!Enabled || !_gun.CanShoot(gun)) continue; EntityCoordinates targetCordinates; if (_mapManager.TryFindGridAt(xform.MapID, targetPos, out var gridUid, out var mapGrid)) { targetCordinates = new EntityCoordinates(gridUid, mapGrid.WorldToLocal(targetSpot)); } else { targetCordinates = new EntityCoordinates(xform.MapUid!.Value, targetSpot); } _gun.AttemptShoot(uid, gunUid, gun, targetCordinates); } } }