Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions FivePRS.Client/Callouts/CalloutBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,32 +174,47 @@ 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!");

while (true)
{
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;
}

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;
Expand Down
103 changes: 103 additions & 0 deletions FivePRS.Client/Tasks/FootPursuitPace.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
6 changes: 6 additions & 0 deletions FivePRS.Police/Callouts/TrafficStopCallout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions FivePRS.Police/Callouts/VehiclePursuitCallout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions FivePRS.Police/TrafficStops/TrafficStop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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);

Expand All @@ -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())
{
Expand Down
10 changes: 7 additions & 3 deletions FivePRS.Police/TrafficStops/TrafficStopManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.");
Expand Down
Loading