diff --git a/FivePRS.Client/Callouts/CalloutBase.cs b/FivePRS.Client/Callouts/CalloutBase.cs index 6cc9379..9507002 100644 --- a/FivePRS.Client/Callouts/CalloutBase.cs +++ b/FivePRS.Client/Callouts/CalloutBase.cs @@ -174,6 +174,7 @@ protected async Task FootPursuitAsync(Ped suspect, float catchDistM, float escap API.SetPedFleeAttributes(suspect.Handle, 0, false); API.SetPedCombatAttributes(suspect.Handle, 17, true); await TaskManager.AssignTaskAsync(suspect, PedTaskType.FleeFromPlayer); + using var pace = new FootPursuitPace(suspect); ClientBrain.ShowNotification("~r~Suspect fleeing on foot~w~ | Pursue and arrest!"); @@ -181,8 +182,17 @@ protected async Task FootPursuitAsync(Ped suspect, float catchDistM, float escap { await Timing.WaitAsync(PollMs, ct); + if (ArrestManager.IsInCustody(suspect)) + { + pace.Dispose(); + ClientBrain.ShowNotification("~g~Suspect in custody~w~ | Callout complete."); + CalloutCompleted(); + return; + } + if (!suspect.Exists() || suspect.IsDead) { + pace.Dispose(); ClientBrain.ShowNotification("~g~Suspect down~w~ | Callout complete."); CalloutCompleted(); return; @@ -190,16 +200,21 @@ protected async Task FootPursuitAsync(Ped suspect, float catchDistM, float escap var dist = Vector3.Distance(Game.PlayerPed.Position, suspect.Position); - if (dist <= catchDistM) + var cornered = dist <= catchDistM; + var exhausted = !cornered && pace.Update(); + if (cornered || exhausted) { + pace.Dispose(); await TaskManager.AssignTaskAsync(suspect, PedTaskType.PutHandsUp); - ClientBrain.ShowNotification("~g~Suspect cornered~w~ | Move in and arrest them."); + var status = exhausted ? "Suspect exhausted" : "Suspect cornered"; + ClientBrain.ShowNotification($"~g~{status}~w~ | Move in and arrest them."); await WaitForArrestAsync(suspect, ct); return; } if (dist > escapeDistM) { + pace.Dispose(); ClientBrain.ShowNotification("~r~Suspect escaped~w~ | Callout failed."); CalloutFailed(); return; diff --git a/FivePRS.Client/Tasks/FootPursuitPace.cs b/FivePRS.Client/Tasks/FootPursuitPace.cs new file mode 100644 index 0000000..822053c --- /dev/null +++ b/FivePRS.Client/Tasks/FootPursuitPace.cs @@ -0,0 +1,103 @@ +using System; +using System.Threading.Tasks; +using CitizenFX.Core; +using CitizenFX.Core.Native; + +namespace FivePRS.Client.Tasks +{ + public sealed class FootPursuitPace : IDisposable + { + private const int SprintDurationMs = 12_000; + private const int SlowdownDurationMs = 4_000; + private const int SurrenderCheckIntervalMs = 10_000; + private const int SurrenderChanceIncreasePercent = 10; + private const int MaxSurrenderChancePercent = 35; + private const float SurrenderCheckRangeMeters = 15f; + private const int SecondWindChancePercent = 10; + private const float SecondWindRangeMeters = 15f; + private const int MinSecondWindSeconds = 5; + private const int MaxSecondWindSeconds = 8; + private const float InitialMoveBlendRatio = 2.6f; + private const float RunMoveBlendRatio = 1.6f; + private const float SecondWindMoveBlendRatio = 2.3f; + private const float FullMoveBlendRatio = 3f; + private const float MaxPaceVariation = 0.4f; + private static readonly Random Random = new(); + + private readonly Ped _suspect; + private readonly int _startedAt; + private readonly int _motivation; + private readonly float _paceVariation; + private int _nextSurrenderCheckMs = SurrenderCheckIntervalMs * 2; + private int _secondWindEndsAtMs; + private bool _wasInSecondWindRange; + private bool _secondWindRolled; + private bool _disposed; + + public FootPursuitPace(Ped suspect) + { + _suspect = suspect; + _startedAt = API.GetGameTimer(); + _motivation = Random.Next(1, 11); + _paceVariation = (float)(Random.NextDouble() - Random.NextDouble()) * MaxPaceVariation; + _wasInSecondWindRange = Vector3.Distance(Game.PlayerPed.Position, suspect.Position) <= SecondWindRangeMeters; + _ = ApplyPaceEachFrameAsync(); + } + + private async Task ApplyPaceEachFrameAsync() + { + var sprintCap = InitialMoveBlendRatio + _paceVariation; + while (!_disposed && _suspect.Exists() && !_suspect.IsDead) + { + var elapsed = unchecked(API.GetGameTimer() - _startedAt); + var remaining = Math.Max(0f, Math.Min(1f, + (SprintDurationMs + SlowdownDurationMs - elapsed) / (float)SlowdownDurationMs)); + var secondWindActive = elapsed < _secondWindEndsAtMs; + var maxMoveBlendRatio = secondWindActive + ? SecondWindMoveBlendRatio + : RunMoveBlendRatio + (sprintCap - RunMoveBlendRatio) * remaining; + API.SetPedMaxMoveBlendRatio(_suspect.Handle, maxMoveBlendRatio); + await BaseScript.Delay(0); + } + } + + public bool Update() + { + var elapsed = unchecked(API.GetGameTimer() - _startedAt); + var distance = Vector3.Distance(Game.PlayerPed.Position, _suspect.Position); + CheckSecondWind(elapsed, distance); + if (elapsed < _secondWindEndsAtMs || elapsed < _nextSurrenderCheckMs || + distance > SurrenderCheckRangeMeters) return false; + + _nextSurrenderCheckMs = elapsed + SurrenderCheckIntervalMs; + var completedIntervals = (elapsed - (SurrenderCheckIntervalMs * 2)) / SurrenderCheckIntervalMs; + var surrenderChancePercent = Math.Min(MaxSurrenderChancePercent, + SurrenderChanceIncreasePercent + (11 - _motivation) + completedIntervals * SurrenderChanceIncreasePercent); + return Random.Next(1, 101) <= surrenderChancePercent; + } + + private void CheckSecondWind(int elapsed, float distance) + { + if (_secondWindRolled) return; + + var inRange = distance <= SecondWindRangeMeters; + var enteredRange = inRange && !_wasInSecondWindRange; + _wasInSecondWindRange = inRange; + if (elapsed < SprintDurationMs + SlowdownDurationMs || !enteredRange) return; + + _secondWindRolled = true; + if (Random.Next(100) >= SecondWindChancePercent) return; + + var durationSeconds = Random.Next(MinSecondWindSeconds, MaxSecondWindSeconds + 1); + _secondWindEndsAtMs = elapsed + durationSeconds * 1_000; + } + + public void Dispose() + { + if (_disposed) return; + _disposed = true; + if (_suspect.Exists()) + API.SetPedMaxMoveBlendRatio(_suspect.Handle, FullMoveBlendRatio); + } + } +} diff --git a/FivePRS.Police/Callouts/TrafficStopCallout.cs b/FivePRS.Police/Callouts/TrafficStopCallout.cs index 485bf2d..d36cae1 100644 --- a/FivePRS.Police/Callouts/TrafficStopCallout.cs +++ b/FivePRS.Police/Callouts/TrafficStopCallout.cs @@ -344,6 +344,12 @@ private async Task RunPursuitLoopAsync(CancellationToken ct) return; } + if (!_driver.IsInVehicle(_suspectVehicle)) + { + await RunFootPursuitAsync(ct); + return; + } + var distToPlayer = Vector3.Distance( Game.PlayerPed.Position, _suspectVehicle.Position); diff --git a/FivePRS.Police/Callouts/VehiclePursuitCallout.cs b/FivePRS.Police/Callouts/VehiclePursuitCallout.cs index d83de2b..37f3bfe 100644 --- a/FivePRS.Police/Callouts/VehiclePursuitCallout.cs +++ b/FivePRS.Police/Callouts/VehiclePursuitCallout.cs @@ -221,6 +221,12 @@ private async Task RunPursuitLoopAsync(CancellationToken ct) return; } + if (!_driver.IsInVehicle(_car)) + { + await RunFootPursuitAsync(ct); + return; + } + var distToPlayer = Vector3.Distance(Game.PlayerPed.Position, _car.Position); if (distToPlayer > EscapeDistM) diff --git a/FivePRS.Police/TrafficStops/TrafficStop.cs b/FivePRS.Police/TrafficStops/TrafficStop.cs index 1c500a7..94a34a0 100644 --- a/FivePRS.Police/TrafficStops/TrafficStop.cs +++ b/FivePRS.Police/TrafficStops/TrafficStop.cs @@ -4,6 +4,7 @@ using CitizenFX.Core.Native; using FivePRS.Client; using FivePRS.Client.Arrest; +using FivePRS.Client.Tasks; using FivePRS.Core.Civilian; using FivePRS.Core.Police; @@ -32,6 +33,8 @@ public sealed class TrafficStop private const float MaxPullOverTravel = 50f; private readonly Blip _blip; + private FootPursuitPace? _footPursuitPace; + private bool _surrenderedFromExhaustion; public TrafficStop(Ped driver, Vehicle? vehicle, int seed) { @@ -140,6 +143,9 @@ private bool IsAhead(Vector3 target) public void Hold() { + _footPursuitPace?.Dispose(); + _footPursuitPace = null; + _surrenderedFromExhaustion = false; Phase = StopPhase.Stopped; if (Vehicle is not null && DriverInVehicle) @@ -168,9 +174,30 @@ public void Flee() API.TaskVehicleDriveWander(Driver.Handle, Vehicle.Handle, 35f, RecklessDrivingStyle); return; } + BeginFootFlight(); + } + private void BeginFootFlight() + { API.SetPedFleeAttributes(Driver.Handle, 0, false); API.TaskSmartFleePed(Driver.Handle, Game.PlayerPed.Handle, 300f, -1, false, false); + _footPursuitPace = new FootPursuitPace(Driver); + } + + public async Task UpdateFootFlightAsync() + { + if (DriverInVehicle || _surrenderedFromExhaustion) return; + + if (_footPursuitPace is null) + BeginFootFlight(); + + if (_footPursuitPace?.Update() != true) return; + + _footPursuitPace.Dispose(); + _footPursuitPace = null; + _surrenderedFromExhaustion = true; + await TaskManager.AssignTaskAsync(Driver, PedTaskType.PutHandsUp); + ClientBrain.ShowNotification("~g~Suspect exhausted~w~ | Move in and arrest them."); } public void StepOut() @@ -185,6 +212,9 @@ public void GetBackIn() public async Task EndAsync(bool release) { + _footPursuitPace?.Dispose(); + _footPursuitPace = null; + if (_blip.Exists()) _blip.Delete(); if (Vehicle is not null && Vehicle.Exists()) API.SetVehicleIndicatorLights(Vehicle.Handle, RightIndicator, false); @@ -195,6 +225,8 @@ public async Task EndAsync(bool release) if (release && !Driver.IsDead) await ReleaseAsync(); + else if (_surrenderedFromExhaustion && !Driver.IsDead) + TaskManager.ClearTasks(Driver); if (Driver.Exists()) { diff --git a/FivePRS.Police/TrafficStops/TrafficStopManager.cs b/FivePRS.Police/TrafficStops/TrafficStopManager.cs index e29c486..130c3ab 100644 --- a/FivePRS.Police/TrafficStops/TrafficStopManager.cs +++ b/FivePRS.Police/TrafficStops/TrafficStopManager.cs @@ -199,8 +199,6 @@ private async Task UpdateStopAsync(TrafficStop stop) return; } - var distance = Vector3.Distance(Game.PlayerPed.Position, stop.Anchor.Position); - switch (stop.Phase) { case StopPhase.PullingOver: @@ -225,11 +223,17 @@ private async Task UpdateStopAsync(TrafficStop stop) return; } - if (distance > (stop.IsVehicleStop && stop.DriverInVehicle ? VehicleEscapeRange : FootEscapeRange)) + var inVehicle = stop.DriverInVehicle; + var pursuitPosition = inVehicle ? stop.Anchor.Position : stop.Driver.Position; + var pursuitDistance = Vector3.Distance(Game.PlayerPed.Position, pursuitPosition); + if (pursuitDistance > (inVehicle ? VehicleEscapeRange : FootEscapeRange)) await EndAsync(false, "~r~The suspect got away."); + else + await stop.UpdateFootFlightAsync(); break; case StopPhase.Stopped: + var distance = Vector3.Distance(Game.PlayerPed.Position, stop.Anchor.Position); if (distance > AbandonRange) { await EndAsync(true, $"You left the stop, so {Noun(stop)} was released.");