From 268bcc4ca6fb135b6dc1070e2e21ff8c8dcf77b8 Mon Sep 17 00:00:00 2001 From: Rich Dunne Date: Thu, 3 Sep 2020 17:52:28 -0600 Subject: [PATCH] Fixed AI driving around vehicles stopped at Stop waypoints. --- SceneManager/AITasking.cs | 18 ++++++++++- SceneManager/VehicleCollector.cs | 55 ++------------------------------ 2 files changed, 20 insertions(+), 53 deletions(-) diff --git a/SceneManager/AITasking.cs b/SceneManager/AITasking.cs index e69c6ff..a06db1c 100644 --- a/SceneManager/AITasking.cs +++ b/SceneManager/AITasking.cs @@ -1,4 +1,5 @@ using Rage; +using System; using System.Collections.Generic; using System.Linq; @@ -32,6 +33,15 @@ namespace SceneManager private static void DriveVehicleToNextWaypoint(CollectedVehicle collectedVehicle, List waypoints, Waypoint currentWaypoint) { + if (!collectedVehicle.Vehicle) + { + return; + } + if (!collectedVehicle.Driver) + { + return; + } + var vehicle = collectedVehicle.Vehicle; var driver = vehicle.Driver; @@ -72,8 +82,13 @@ namespace SceneManager private static void StopVehicleAtWaypoint(CollectedVehicle collectedVehicle) { Game.LogTrivial($"{collectedVehicle.Vehicle.Model.Name} stopping at waypoint."); - collectedVehicle.Vehicle.Driver.Tasks.PerformDrivingManeuver(VehicleManeuver.GoForwardStraightBraking); + collectedVehicle.Driver.Tasks.DriveToPosition(collectedVehicle.Vehicle.FrontPosition, 10f, VehicleDrivingFlags.StopAtDestination); collectedVehicle.SetStoppedAtWaypoint(true); + + while (collectedVehicle.Vehicle && collectedVehicle.StoppedAtWaypoint) + { + GameFiber.Yield(); + } } public static void DismissDriver(CollectedVehicle cv) @@ -103,6 +118,7 @@ namespace SceneManager cv.Vehicle.Driver.IsPersistent = false; cv.Vehicle.Dismiss(); + cv.Vehicle.IsSirenOn = false; cv.Vehicle.IsPersistent = false; Game.LogTrivial($"{cv.Vehicle.Model.Name} dismissed successfully."); diff --git a/SceneManager/VehicleCollector.cs b/SceneManager/VehicleCollector.cs index c93d584..aaa239c 100644 --- a/SceneManager/VehicleCollector.cs +++ b/SceneManager/VehicleCollector.cs @@ -10,34 +10,11 @@ namespace SceneManager { // Driving styles https://gtaforums.com/topic/822314-guide-driving-styles/ // also https://vespura.com/fivem/drivingstyle/ - [Flags] - private enum EDrivingFlags - { - None = 0, - StopForVehicles = 1, - StopForPeds = 2, - AvoidEmptyVehicles = 8, - AvoidPeds = 16, - AvoidObjects = 32, - StopForTrafficLights = 128, - UseBlinkers = 256, - AllowWrongWay = 512, - TakeShortestPath = 262144, - IgnoreRoads = 4194304, - IgnorePathfinding = 16777216, - AvoidHighways = 536870912, - Normal = StopForVehicles | StopForPeds | AvoidEmptyVehicles | StopForTrafficLights | UseBlinkers | AllowWrongWay | IgnoreRoads, - TotalControl = AllowWrongWay | AvoidObjects | AvoidPeds | TakeShortestPath | StopForTrafficLights | IgnorePathfinding | StopForVehicles - } - //public static Dictionary collectedVehicles = new Dictionary(); public static List collectedVehicles = new List(); public static void StartCollectingAtWaypoint(List paths, Path path, Waypoint waypoint) { - GameFiber AssignStopFlagForVehiclesFiber = new GameFiber(() => AssignStopForVehiclesFlag(paths, path, waypoint)); - AssignStopFlagForVehiclesFiber.Start(); - while (paths.Contains(path) && path.Waypoints.Contains(waypoint)) { if (path.IsEnabled && waypoint.IsCollector) @@ -57,7 +34,7 @@ namespace SceneManager break; } - Game.LogTrivial($"Vehicle: {vehicle.Model.Name}, Distance to waypoint: {vehicle.DistanceTo2D(waypoint.Position)}"); + Game.LogTrivial($"Vehicle: {vehicle.Model.Name}, Waypoint collector radius: {waypoint.CollectorRadius}, Distance to waypoint: {vehicle.DistanceTo2D(waypoint.Position)}"); var collectedVehicle = collectedVehicles.Where(cv => cv.Vehicle == vehicle) as CollectedVehicle; // If the vehicle is not in the collection yet @@ -82,35 +59,9 @@ namespace SceneManager } } - private static Vehicle[] GetNearbyVehiclesForCollection(Vector3 collectorPosition, float collectorRadius) + private static Vehicle[] GetNearbyVehiclesForCollection(Vector3 collectorWaypointPosition, float collectorRadius) { - return (from v in World.GetAllVehicles() where v.DistanceTo2D(collectorPosition) <= collectorRadius && v.IsValidForCollection() select v).ToArray(); - } - - private static Vehicle[] GetNearbyVehiclesForStopFlag(Vector3 collectorPosition) - { - return (from v in World.GetAllVehicles() where v.DistanceTo2D(collectorPosition) <= 50f select v).ToArray(); - } - - private static void AssignStopForVehiclesFlag(List paths, Path path, Waypoint waypoint) - { - while (paths.Contains(path) && path.Waypoints.Contains(waypoint)) - { - if (path.IsEnabled) - { - foreach (Vehicle v in GetNearbyVehiclesForStopFlag(waypoint.Position).Where(v => v && v.HasDriver && v.Driver)) - { - SetDriveTaskDrivingFlags(v.Driver, EDrivingFlags.StopForVehicles); - } - } - GameFiber.Sleep(500); - } - } - - private static void SetDriveTaskDrivingFlags(this Ped ped, EDrivingFlags flags) - { - ulong SetDriveTaskDrivingFlagsHash = 0xDACE1BE37D88AF67; - Rage.Native.NativeFunction.CallByHash(SetDriveTaskDrivingFlagsHash, ped, (int)flags); + return (from v in World.GetAllVehicles() where v.DistanceTo2D(collectorWaypointPosition) <= collectorRadius - 0.5f && v.IsValidForCollection() select v).ToArray(); } private static CollectedVehicle AddVehicleToCollection(Path path, Waypoint waypoint, Vehicle v)