1
Fork 0
mirror of https://github.com/thegeneralist01/Scene-Manager-DevRepo synced 2026-01-11 23:50:29 +01:00

Fixed AI driving around vehicles stopped at Stop waypoints.

This commit is contained in:
Rich Dunne 2020-09-03 17:52:28 -06:00
parent 046bfbd599
commit 268bcc4ca6
2 changed files with 20 additions and 53 deletions

View file

@ -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<Waypoint> 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.");

View file

@ -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<string, CollectedVehicle> collectedVehicles = new Dictionary<string, CollectedVehicle>();
public static List<CollectedVehicle> collectedVehicles = new List<CollectedVehicle>();
public static void StartCollectingAtWaypoint(List<Path> 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<Path> 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<int>(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)