From e72346e447936b0f47e698c50b2ba0113d0a5222 Mon Sep 17 00:00:00 2001 From: Adam Guthrie Date: Sat, 28 Nov 2026 15:46:00 -0800 Subject: [PATCH 1/2] Initial migration form previous code --- FivePRS.Client/Callouts/CalloutBase.cs | 18 +++++- FivePRS.Client/Tasks/FootPursuitPace.cs | 61 +++++++++++++++++++ FivePRS.Police/Callouts/TrafficStopCallout.cs | 6 ++ .../Callouts/VehiclePursuitCallout.cs | 6 ++ FivePRS.Police/TrafficStops/TrafficStop.cs | 32 ++++++++++ .../TrafficStops/TrafficStopManager.cs | 2 + 6 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 FivePRS.Client/Tasks/FootPursuitPace.cs diff --git a/FivePRS.Client/Callouts/CalloutBase.cs b/FivePRS.Client/Callouts/CalloutBase.cs index 6cc9379..cc17580 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,20 @@ protected async Task FootPursuitAsync(Ped suspect, float catchDistM, float escap var dist = Vector3.Distance(Game.PlayerPed.Position, suspect.Position); - if (dist <= catchDistM) + var exhausted = pace.Update(); + if (dist <= catchDistM || 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..28da54e --- /dev/null +++ b/FivePRS.Client/Tasks/FootPursuitPace.cs @@ -0,0 +1,61 @@ +using System; +using CitizenFX.Core; +using CitizenFX.Core.Native; + +namespace FivePRS.Client.Tasks +{ + public sealed class FootPursuitPace : IDisposable + { + private const int SprintDurationMs = 8_000; + private const int FirstSurrenderCheckMs = 25_000; + private const int SurrenderCheckIntervalMs = 10_000; + private const double SurrenderChance = 0.5; + private const float InitialMoveBlendRatio = 2.6f; + private const float TiredMoveBlendRatio = 1.5f; + 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 float _paceVariation; + private int _nextSurrenderCheckMs = FirstSurrenderCheckMs; + private bool _slowed; + private bool _disposed; + + public FootPursuitPace(Ped suspect) + { + _suspect = suspect; + _startedAt = API.GetGameTimer(); + // Two rolls make paces near the middle more common than either extreme. + _paceVariation = (float)(Random.NextDouble() - Random.NextDouble()) * MaxPaceVariation; + API.SetPedMaxMoveBlendRatio(_suspect.Handle, InitialMoveBlendRatio + _paceVariation); + } + + public bool Update() + { + var elapsed = unchecked(API.GetGameTimer() - _startedAt); + if (!_slowed && elapsed >= SprintDurationMs) + { + API.SetPedMaxMoveBlendRatio(_suspect.Handle, TiredMoveBlendRatio + _paceVariation); + _slowed = true; + } + + if (elapsed < _nextSurrenderCheckMs) return false; + + // Stay on the original schedule without rolling repeatedly after a delayed update. + var overdueMs = elapsed - _nextSurrenderCheckMs; + _nextSurrenderCheckMs = elapsed + SurrenderCheckIntervalMs - overdueMs % SurrenderCheckIntervalMs; + return Random.NextDouble() < SurrenderChance; + } + + public void Dispose() + { + if (_disposed) return; + _disposed = true; + if (_suspect.Exists()) + API.SetPedMaxMoveBlendRatio(_suspect.Handle, FullMoveBlendRatio); + } + } +} \ No newline at end of file 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..35c407e 100644 --- a/FivePRS.Police/TrafficStops/TrafficStopManager.cs +++ b/FivePRS.Police/TrafficStops/TrafficStopManager.cs @@ -227,6 +227,8 @@ private async Task UpdateStopAsync(TrafficStop stop) if (distance > (stop.IsVehicleStop && stop.DriverInVehicle ? VehicleEscapeRange : FootEscapeRange)) await EndAsync(false, "~r~The suspect got away."); + else + await stop.UpdateFootFlightAsync(); break; case StopPhase.Stopped: From cb9f447d13f0d78da9e1283c785ef321be6066d1 Mon Sep 17 00:00:00 2001 From: Adam Guthrie Date: Sun, 29 Nov 2026 09:39:21 -0800 Subject: [PATCH 2/2] tweak running parameters added second wind chance --- FivePRS.Client/Callouts/CalloutBase.cs | 5 +- FivePRS.Client/Tasks/FootPursuitPace.cs | 82 ++++++++++++++----- .../TrafficStops/TrafficStopManager.cs | 8 +- 3 files changed, 70 insertions(+), 25 deletions(-) diff --git a/FivePRS.Client/Callouts/CalloutBase.cs b/FivePRS.Client/Callouts/CalloutBase.cs index cc17580..9507002 100644 --- a/FivePRS.Client/Callouts/CalloutBase.cs +++ b/FivePRS.Client/Callouts/CalloutBase.cs @@ -200,8 +200,9 @@ protected async Task FootPursuitAsync(Ped suspect, float catchDistM, float escap var dist = Vector3.Distance(Game.PlayerPed.Position, suspect.Position); - var exhausted = pace.Update(); - if (dist <= catchDistM || exhausted) + var cornered = dist <= catchDistM; + var exhausted = !cornered && pace.Update(); + if (cornered || exhausted) { pace.Dispose(); await TaskManager.AssignTaskAsync(suspect, PedTaskType.PutHandsUp); diff --git a/FivePRS.Client/Tasks/FootPursuitPace.cs b/FivePRS.Client/Tasks/FootPursuitPace.cs index 28da54e..822053c 100644 --- a/FivePRS.Client/Tasks/FootPursuitPace.cs +++ b/FivePRS.Client/Tasks/FootPursuitPace.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using CitizenFX.Core; using CitizenFX.Core.Native; @@ -6,48 +7,89 @@ namespace FivePRS.Client.Tasks { public sealed class FootPursuitPace : IDisposable { - private const int SprintDurationMs = 8_000; - private const int FirstSurrenderCheckMs = 25_000; + private const int SprintDurationMs = 12_000; + private const int SlowdownDurationMs = 4_000; private const int SurrenderCheckIntervalMs = 10_000; - private const double SurrenderChance = 0.5; + 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 TiredMoveBlendRatio = 1.5f; + 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 = FirstSurrenderCheckMs; - private bool _slowed; + 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(); - // Two rolls make paces near the middle more common than either extreme. + _motivation = Random.Next(1, 11); _paceVariation = (float)(Random.NextDouble() - Random.NextDouble()) * MaxPaceVariation; - API.SetPedMaxMoveBlendRatio(_suspect.Handle, InitialMoveBlendRatio + _paceVariation); + _wasInSecondWindRange = Vector3.Distance(Game.PlayerPed.Position, suspect.Position) <= SecondWindRangeMeters; + _ = ApplyPaceEachFrameAsync(); } - public bool Update() + private async Task ApplyPaceEachFrameAsync() { - var elapsed = unchecked(API.GetGameTimer() - _startedAt); - if (!_slowed && elapsed >= SprintDurationMs) + var sprintCap = InitialMoveBlendRatio + _paceVariation; + while (!_disposed && _suspect.Exists() && !_suspect.IsDead) { - API.SetPedMaxMoveBlendRatio(_suspect.Handle, TiredMoveBlendRatio + _paceVariation); - _slowed = true; + 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; - if (elapsed < _nextSurrenderCheckMs) return false; + _secondWindRolled = true; + if (Random.Next(100) >= SecondWindChancePercent) return; - // Stay on the original schedule without rolling repeatedly after a delayed update. - var overdueMs = elapsed - _nextSurrenderCheckMs; - _nextSurrenderCheckMs = elapsed + SurrenderCheckIntervalMs - overdueMs % SurrenderCheckIntervalMs; - return Random.NextDouble() < SurrenderChance; + var durationSeconds = Random.Next(MinSecondWindSeconds, MaxSecondWindSeconds + 1); + _secondWindEndsAtMs = elapsed + durationSeconds * 1_000; } public void Dispose() @@ -58,4 +100,4 @@ public void Dispose() API.SetPedMaxMoveBlendRatio(_suspect.Handle, FullMoveBlendRatio); } } -} \ No newline at end of file +} diff --git a/FivePRS.Police/TrafficStops/TrafficStopManager.cs b/FivePRS.Police/TrafficStops/TrafficStopManager.cs index 35c407e..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,13 +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.");