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

-Converted ControlledVehicles from List to Dictionary

-Removed ControlledVehicle from collection via dict key at end of AssignTasks
This commit is contained in:
Rich Dunne 2020-06-08 15:20:29 -06:00
parent 826e45f40b
commit 071ac92ba0
3 changed files with 53 additions and 48 deletions

View file

@ -5,6 +5,7 @@ namespace SceneManager
public class ControlledVehicle
{
public Vehicle Vehicle;
public string LicensePlate;
public int Path;
public int TotalWaypoints;
public int CurrentWaypoint;
@ -13,9 +14,10 @@ namespace SceneManager
public bool StoppedAtWaypoint;
public bool Redirected;
public ControlledVehicle(Vehicle vehicle, int path, int totalWaypoints, int currentWaypoint, bool tasksAssigned, bool dismissNow, bool redirected)
public ControlledVehicle(Vehicle vehicle, string licensePlate, int path, int totalWaypoints, int currentWaypoint, bool tasksAssigned, bool dismissNow, bool redirected)
{
Vehicle = vehicle;
LicensePlate = licensePlate;
Path = path;
TotalWaypoints = totalWaypoints;
CurrentWaypoint = currentWaypoint;

View file

@ -173,20 +173,21 @@ namespace SceneManager
// Before deleting a path, we need to dismiss any vehicles controlled by that path and remove the vehicles from ControlledVehicles
//Game.LogTrivial($"Deleting path {index+1}");
Game.LogTrivial($"Deleting path {path.WaypointData[0].Path}");
var matchingVehicle = TrafficPathing.ControlledVehicles.Where(cv => cv.Path == path.WaypointData[0].Path).ToList();
var matchingVehicle = TrafficPathing.ControlledVehicles.Where(cv => cv.Value.Path == path.WaypointData[0].Path).ToList();
Game.LogTrivial($"Running foreach loop");
foreach (ControlledVehicle cv in matchingVehicle)
foreach (KeyValuePair<string, ControlledVehicle> cv in matchingVehicle)
{
if (cv.Vehicle.Exists() && cv.Vehicle.IsValid() && cv.Vehicle.Driver.Exists() && cv.Vehicle.Driver.IsValid())
if (cv.Value.Vehicle.Exists() && cv.Value.Vehicle.IsValid() && cv.Value.Vehicle.Driver.Exists() && cv.Value.Vehicle.Driver.IsValid())
{
cv.DismissNow = true;
cv.Vehicle.Driver.Tasks.Clear();
cv.Vehicle.Driver.Dismiss();
cv.Value.DismissNow = true;
cv.Value.Vehicle.Driver.Tasks.Clear();
cv.Value.Vehicle.Driver.Dismiss();
TrafficPathing.ControlledVehicles.Remove(cv.Value.LicensePlate);
//Game.LogTrivial($"{cv.vehicle.Model.Name} cleared from path {cv.path}");
}
}
Game.LogTrivial($"Remove all vehicles in the path");
TrafficPathing.ControlledVehicles.RemoveAll(cv => cv.Path == path.WaypointData[0].Path);
//TrafficPathing.ControlledVehicles.RemoveAll(cv => cv.Path == path.WaypointData[0].Path);
// Remove the speed zone so cars don't continue to be affected after the path is deleted
foreach (WaypointData wd in path.WaypointData)
@ -272,39 +273,39 @@ namespace SceneManager
if (v.Exists() && v.IsValid() && v.HasDriver && v.Driver.IsAlive)
{
// Check if there's a matching vehicle in ControlledVehicles. If so, check if it has tasks and proceed, else add it to the collection and assign tasks
var matchingVehicle = TrafficPathing.ControlledVehicles.Where(cv => cv.Vehicle == v).ToList();
if (matchingVehicle.ElementAtOrDefault(0) != null && matchingVehicle[0].TasksAssigned)
var matchingVehicle = TrafficPathing.ControlledVehicles.Where(cv => cv.Value.Vehicle == v).ToList();
if (matchingVehicle.ElementAtOrDefault(0).Value != null && matchingVehicle[0].Value.TasksAssigned)
{
Game.LogTrivial($"[Direct Driver] {v.Model.Name} already in collection with tasks. Clearing tasks.");
v.Driver.Tasks.Clear();
matchingVehicle[0].Path = paths[directDriver.Index].WaypointData[0].Path;
matchingVehicle[0].TotalWaypoints = paths[directDriver.Index].WaypointData.Count;
matchingVehicle[0].CurrentWaypoint = 1;
matchingVehicle[0].DismissNow = true;
matchingVehicle[0].StoppedAtWaypoint = false;
matchingVehicle[0].Redirected = true;
GameFiber DirectTaskFiber = new GameFiber(() => TrafficPathing.DirectTask(matchingVehicle[0], paths[directDriver.Index].WaypointData));
matchingVehicle[0].Value.Path = paths[directDriver.Index].WaypointData[0].Path;
matchingVehicle[0].Value.TotalWaypoints = paths[directDriver.Index].WaypointData.Count;
matchingVehicle[0].Value.CurrentWaypoint = 1;
matchingVehicle[0].Value.DismissNow = true;
matchingVehicle[0].Value.StoppedAtWaypoint = false;
matchingVehicle[0].Value.Redirected = true;
GameFiber DirectTaskFiber = new GameFiber(() => TrafficPathing.DirectTask(matchingVehicle[0].Value, paths[directDriver.Index].WaypointData));
DirectTaskFiber.Start();
}
else if(matchingVehicle.ElementAtOrDefault(0) != null && !matchingVehicle[0].TasksAssigned)
else if(matchingVehicle.ElementAtOrDefault(0).Value != null && !matchingVehicle[0].Value.TasksAssigned)
{
Game.LogTrivial($"[Direct Driver] {v.Model.Name} already in collection, but with no tasks.");
v.Driver.Tasks.Clear();
matchingVehicle[0].Path = paths[directDriver.Index].WaypointData[0].Path;
matchingVehicle[0].TotalWaypoints = paths[directDriver.Index].WaypointData.Count;
matchingVehicle[0].CurrentWaypoint = 1;
matchingVehicle[0].DismissNow = true;
matchingVehicle[0].StoppedAtWaypoint = false;
matchingVehicle[0].Redirected = true;
GameFiber DirectTaskFiber = new GameFiber(() => TrafficPathing.DirectTask(matchingVehicle[0], paths[directDriver.Index].WaypointData));
matchingVehicle[0].Value.Path = paths[directDriver.Index].WaypointData[0].Path;
matchingVehicle[0].Value.TotalWaypoints = paths[directDriver.Index].WaypointData.Count;
matchingVehicle[0].Value.CurrentWaypoint = 1;
matchingVehicle[0].Value.DismissNow = true;
matchingVehicle[0].Value.StoppedAtWaypoint = false;
matchingVehicle[0].Value.Redirected = true;
GameFiber DirectTaskFiber = new GameFiber(() => TrafficPathing.DirectTask(matchingVehicle[0].Value, paths[directDriver.Index].WaypointData));
DirectTaskFiber.Start();
}
else
{
TrafficPathing.ControlledVehicles.Add(new ControlledVehicle(v, paths[directDriver.Index].WaypointData[0].Path, paths[directDriver.Index].WaypointData.Count, 1, false, false, true));
TrafficPathing.ControlledVehicles.Add(v.LicensePlate, new ControlledVehicle(v, v.LicensePlate, paths[directDriver.Index].WaypointData[0].Path, paths[directDriver.Index].WaypointData.Count, 1, false, false, true));
Game.LogTrivial($"[Direct Driver] {v.Model.Name} not in collection, adding to collection for path {paths[directDriver.Index].WaypointData[0].Path} with {paths[directDriver.Index].WaypointData.Count} waypoints");
GameFiber DirectTaskFiber = new GameFiber(() => TrafficPathing.DirectTask(TrafficPathing.ControlledVehicles.Last(), paths[directDriver.Index].WaypointData));
GameFiber DirectTaskFiber = new GameFiber(() => TrafficPathing.DirectTask(TrafficPathing.ControlledVehicles[v.LicensePlate], paths[directDriver.Index].WaypointData));
DirectTaskFiber.Start();
}
Game.LogTrivial($"Directed driver of {v.Model.Name} to path {paths[directDriver.Index].WaypointData[0].Path}.");
@ -329,33 +330,33 @@ namespace SceneManager
{
if (v.Exists() && v.IsValid() && v.HasDriver && v.Driver.IsAlive)
{
var matchingVehicle = TrafficPathing.ControlledVehicles.Where(cv => cv.Vehicle == v).ToList();
if (matchingVehicle.ElementAtOrDefault(0) != null && matchingVehicle[0].CurrentWaypoint < matchingVehicle[0].TotalWaypoints && !matchingVehicle[0].StoppedAtWaypoint)
var matchingVehicle = TrafficPathing.ControlledVehicles.Where(cv => cv.Value.Vehicle == v).ToList();
if (matchingVehicle.ElementAtOrDefault(0).Value != null && matchingVehicle[0].Value.CurrentWaypoint < matchingVehicle[0].Value.TotalWaypoints && !matchingVehicle[0].Value.StoppedAtWaypoint)
{
matchingVehicle[0].DismissNow = true;
matchingVehicle[0].Value.DismissNow = true;
v.Driver.Tasks.Clear();
v.Driver.Dismiss();
Game.LogTrivial($"Dismissed driver of {v.Model.Name} from the path");
}
else if (matchingVehicle.ElementAtOrDefault(0) != null && matchingVehicle[0].CurrentWaypoint < matchingVehicle[0].TotalWaypoints)
else if (matchingVehicle.ElementAtOrDefault(0).Value != null && matchingVehicle[0].Value.CurrentWaypoint < matchingVehicle[0].Value.TotalWaypoints)
{
matchingVehicle[0].StoppedAtWaypoint = false;
Game.LogTrivial($"Dismissed driver of {v.Model.Name} from waypoint {matchingVehicle[0].CurrentWaypoint}");
matchingVehicle[0].Value.StoppedAtWaypoint = false;
Game.LogTrivial($"Dismissed driver of {v.Model.Name} from waypoint {matchingVehicle[0].Value.CurrentWaypoint}");
}
else if (matchingVehicle.ElementAtOrDefault(0) != null && matchingVehicle[0].CurrentWaypoint == matchingVehicle[0].TotalWaypoints)
else if (matchingVehicle.ElementAtOrDefault(0).Value != null && matchingVehicle[0].Value.CurrentWaypoint == matchingVehicle[0].Value.TotalWaypoints)
{
matchingVehicle[0].StoppedAtWaypoint = false;
matchingVehicle[0].DismissNow = true;
matchingVehicle[0].Value.StoppedAtWaypoint = false;
matchingVehicle[0].Value.DismissNow = true;
v.Driver.Tasks.Clear();
v.Driver.Dismiss();
Game.LogTrivial($"Dismissed driver of {v.Model.Name} from final waypoint and ultimately the path");
}
else if (matchingVehicle.ElementAtOrDefault(0) != null)
else if (matchingVehicle.ElementAtOrDefault(0).Value != null)
{
matchingVehicle[0].DismissNow = true;
matchingVehicle[0].Value.DismissNow = true;
v.Driver.Tasks.Clear();
v.Driver.Dismiss();
Game.LogTrivial($"Dismissed driver of {v.Model.Name} from path {matchingVehicle[0].Path}");
Game.LogTrivial($"Dismissed driver of {v.Model.Name} from path {matchingVehicle[0].Value.Path}");
}
else
{

View file

@ -7,7 +7,8 @@ namespace SceneManager
{
public static class TrafficPathing
{
public static List<ControlledVehicle> ControlledVehicles = new List<ControlledVehicle> { };
public static Dictionary<string, ControlledVehicle> ControlledVehicles = new Dictionary<string, ControlledVehicle>();
//public static List<ControlledVehicle> ControlledVehicles = new List<ControlledVehicle> { };
public static void InitialWaypointVehicleCollector(List<WaypointData> waypointData)
{
@ -34,6 +35,7 @@ namespace SceneManager
while (waypointData.ElementAtOrDefault(0) != null)
{
Game.DisplaySubtitle($"Vehicles in collection: {ControlledVehicles.Count()}");
// Getting vehicles within 3f of waypoint
try
{
@ -43,26 +45,26 @@ namespace SceneManager
if(VehicleAndDriverValid(v) && v != Game.LocalPlayer.Character.CurrentVehicle && v.HasDriver && v.Driver.IsAlive && (v.IsCar || v.IsBike || v.IsBicycle || v.IsQuadBike))
{
// Check if there's an object in the list with a matching vehicle
var matchingVehicle = ControlledVehicles.Where(cv => cv.Vehicle == v).ToList();
var matchingVehicle = ControlledVehicles.Where(cv => cv.Value.Vehicle == v).ToList();
// If there's a match, then check if the first match has tasksAssigned. If not, AssignTasks
if (matchingVehicle.ElementAtOrDefault(0) != null && !matchingVehicle[0].TasksAssigned && !matchingVehicle[0].DismissNow)
if (matchingVehicle.ElementAtOrDefault(0).Value != null && !matchingVehicle[0].Value.TasksAssigned && !matchingVehicle[0].Value.DismissNow)
{
Game.LogTrivial($"[InitialWaypointVehicleCollector] {v.Model.Name} already in collection, but with no tasks. Assigning tasks.");
matchingVehicle[0].TasksAssigned = true;
GameFiber AssignTasksFiber = new GameFiber(() => AssignTasks(matchingVehicle[0], waypointData));
matchingVehicle[0].Value.TasksAssigned = true;
GameFiber AssignTasksFiber = new GameFiber(() => AssignTasks(matchingVehicle[0].Value, waypointData));
AssignTasksFiber.Start();
}
// Else if object doesn't exist, add to collection and AssignTasks
else if (matchingVehicle.ElementAtOrDefault(0) != null && matchingVehicle[0].TasksAssigned)
else if (matchingVehicle.ElementAtOrDefault(0).Value != null && matchingVehicle[0].Value.TasksAssigned)
{
//Game.LogTrivial($"Vehicle already in collection with tasks. Do nothing.");
}
else
{
ControlledVehicles.Add(new ControlledVehicle(v, waypointData[0].Path, waypointData.Count, 1, true, false, false));
ControlledVehicles.Add(v.LicensePlate, new ControlledVehicle(v, v.LicensePlate, waypointData[0].Path, waypointData.Count, 1, true, false, false));
Game.LogTrivial($"Added {v.Model.Name} to collection from initial waypoint at path {waypointData[0].Path} with {waypointData.Count} waypoints");
GameFiber AssignTasksFiber = new GameFiber(() => AssignTasks(ControlledVehicles.Last(), waypointData));
GameFiber AssignTasksFiber = new GameFiber(() => AssignTasks(ControlledVehicles[v.LicensePlate], waypointData));
AssignTasksFiber.Start();
}
}
@ -121,6 +123,7 @@ namespace SceneManager
{
GameFiber.Yield();
}
ControlledVehicles.Remove(cv.LicensePlate);
Game.LogTrivial($"AssignTasks exit");
}
@ -199,7 +202,6 @@ namespace SceneManager
cv.Vehicle.Driver.Tasks.Clear();
//cv.Vehicle.IsPersistent = false;
cv.Vehicle.Driver.BlockPermanentEvents = false;
ControlledVehicles.Remove(cv);
}
else if(!VehicleAndDriverValid(cv))
{