using System.Linq; using Content.Client.Gameplay; using Content.Shared.CombatMode; using Content.Shared.Effects; using Content.Shared.Hands.Components; using Content.Shared.Mobs.Components; using Content.Shared.StatusEffect; using Content.Shared.Weapons.Melee; using Content.Shared.Weapons.Melee.Events; using Content.Shared.Weapons.Ranged.Components; using Robust.Client.GameObjects; using Robust.Client.Graphics; using Robust.Client.Input; using Robust.Client.Player; using Robust.Client.State; using Robust.Shared.Input; using Robust.Shared.Map; using Robust.Shared.Players; using Robust.Shared.Prototypes; using Robust.Shared.Timing; namespace Content.Client.Weapons.Melee; public sealed partial class MeleeWeaponSystem : SharedMeleeWeaponSystem { [Dependency] private readonly IEyeManager _eyeManager = default!; [Dependency] private readonly IGameTiming _timing = default!; [Dependency] private readonly IInputManager _inputManager = default!; [Dependency] private readonly IPlayerManager _player = default!; [Dependency] private readonly IStateManager _stateManager = default!; [Dependency] private readonly AnimationPlayerSystem _animation = default!; [Dependency] private readonly InputSystem _inputSystem = default!; private EntityQuery _xformQuery; private const string MeleeLungeKey = "melee-lunge"; public override void Initialize() { base.Initialize(); _xformQuery = GetEntityQuery(); SubscribeNetworkEvent(OnMeleeLunge); UpdatesOutsidePrediction = true; } public override void Update(float frameTime) { base.Update(frameTime); if (!Timing.IsFirstTimePredicted) return; var entityNull = _player.LocalPlayer?.ControlledEntity; if (entityNull == null) return; var entity = entityNull.Value; if (!TryGetWeapon(entity, out var weaponUid, out var weapon)) return; if (!CombatMode.IsInCombatMode(entity) || !Blocker.CanAttack(entity)) { weapon.Attacking = false; return; } var useDown = _inputSystem.CmdStates.GetState(EngineKeyFunctions.Use); var altDown = _inputSystem.CmdStates.GetState(EngineKeyFunctions.UseSecondary); if (useDown != BoundKeyState.Down && altDown != BoundKeyState.Down) { if (weapon.Attacking) { RaisePredictiveEvent(new StopAttackEvent(weaponUid)); } } if (weapon.Attacking || weapon.NextAttack > Timing.CurTime) { return; } // TODO using targeted actions while combat mode is enabled should NOT trigger attacks. // TODO: Need to make alt-fire melee its own component I guess? // Melee and guns share a lot in the middle but share virtually nothing at the start and end so // it's kinda tricky. // I think as long as we make secondaries their own component it's probably fine // as long as guncomp has an alt-use key then it shouldn't be too much of a PITA to deal with. if (TryComp(weaponUid, out var gun) && gun.UseKey) { return; } var mousePos = _eyeManager.ScreenToMap(_inputManager.MouseScreenPosition); if (mousePos.MapId == MapId.Nullspace) { return; } EntityCoordinates coordinates; if (MapManager.TryFindGridAt(mousePos, out var gridUid, out _)) { coordinates = EntityCoordinates.FromMap(gridUid, mousePos, TransformSystem, EntityManager); } else { coordinates = EntityCoordinates.FromMap(MapManager.GetMapEntityId(mousePos.MapId), mousePos, TransformSystem, EntityManager); } // Heavy attack. if (altDown == BoundKeyState.Down) { // If it's an unarmed attack then do a disarm if (weapon.AltDisarm && weaponUid == entity) { EntityUid? target = null; if (_stateManager.CurrentState is GameplayStateBase screen) { target = screen.GetClickedEntity(mousePos); } EntityManager.RaisePredictiveEvent(new DisarmAttackEvent(target, coordinates)); return; } ClientHeavyAttack(entity, coordinates, weaponUid, weapon); return; } // Light attack if (useDown == BoundKeyState.Down) { var attackerPos = Transform(entity).MapPosition; if (mousePos.MapId != attackerPos.MapId || (attackerPos.Position - mousePos.Position).Length() > weapon.Range) { return; } EntityUid? target = null; if (_stateManager.CurrentState is GameplayStateBase screen) { target = screen.GetClickedEntity(mousePos); } RaisePredictiveEvent(new LightAttackEvent(target, weaponUid, coordinates)); } } protected override bool InRange(EntityUid user, EntityUid target, float range, ICommonSession? session) { var xform = Transform(target); var targetCoordinates = xform.Coordinates; var targetLocalAngle = xform.LocalRotation; return Interaction.InRangeUnobstructed(user, target, targetCoordinates, targetLocalAngle, range); } protected override void DoDamageEffect(List targets, EntityUid? user, TransformComponent targetXform) { // Server never sends the event to us for predictiveeevent. if (_timing.IsFirstTimePredicted) RaiseLocalEvent(new ColorFlashEffectEvent(Color.Red, targets)); } protected override bool DoDisarm(EntityUid user, DisarmAttackEvent ev, EntityUid meleeUid, MeleeWeaponComponent component, ICommonSession? session) { if (!base.DoDisarm(user, ev, meleeUid, component, session)) return false; if (!TryComp(user, out var combatMode) || combatMode.CanDisarm != true) { return false; } // They need to either have hands... if (!HasComp(ev.Target!.Value)) { // or just be able to be shoved over. if (TryComp(ev.Target!.Value, out var status) && status.AllowedEffects.Contains("KnockedDown")) return true; if (Timing.IsFirstTimePredicted && HasComp(ev.Target.Value)) PopupSystem.PopupEntity(Loc.GetString("disarm-action-disarmable", ("targetName", ev.Target.Value)), ev.Target.Value); return false; } return true; } /// /// Raises a heavy attack event with the relevant attacked entities. /// This is to avoid lag effecting the client's perspective too much. /// private void ClientHeavyAttack(EntityUid user, EntityCoordinates coordinates, EntityUid meleeUid, MeleeWeaponComponent component) { // Only run on first prediction to avoid the potential raycast entities changing. if (!_xformQuery.TryGetComponent(user, out var userXform) || !Timing.IsFirstTimePredicted) { return; } var targetMap = coordinates.ToMap(EntityManager, TransformSystem); if (targetMap.MapId != userXform.MapID) return; var userPos = TransformSystem.GetWorldPosition(userXform); var direction = targetMap.Position - userPos; var distance = MathF.Min(component.Range, direction.Length()); // This should really be improved. GetEntitiesInArc uses pos instead of bounding boxes. // Server will validate it with InRangeUnobstructed. var entities = ArcRayCast(userPos, direction.ToWorldAngle(), component.Angle, distance, userXform.MapID, user).ToList(); RaisePredictiveEvent(new HeavyAttackEvent(meleeUid, entities.GetRange(0, Math.Min(MaxTargets, entities.Count)), coordinates)); } private void OnMeleeLunge(MeleeLungeEvent ev) { // Entity might not have been sent by PVS. if (Exists(ev.Entity)) DoLunge(ev.Entity, ev.Angle, ev.LocalPos, ev.Animation); } }