1
Fork 0
mirror of https://github.com/thegeneralist01/Scene-Manager-DevRepo synced 2026-01-12 08:00:30 +01:00
Scene-Manager-DevRepo/SceneManager/Object Classes/Path.cs
Rich 8bc4de3528
Merge V2.1.1 release (#3)
* Mouse can now be used to fully navigate menus

* Added check for driver's current vehicle when releasing from Stop waypoint in case the ped is not in a vehicle.

* Lines are now only drawn between waypoint markers under the same conditions that waypoint markers are drawn

* Updated marker position to be player's mouse position

* Version update

* Added logic to update waypoint position during driving task if the waypoint position was changed before the driver arrived.

* Removed unused usings.  Refactored debug statements to use Game.LogTrivial instead of Logger.Log

* Removed class

* Removed Logger class

* Consolidated all custom enums to this class.  Added default waypoint settings from .ini

* Modified a hint message

* Fixed collector options not being enabled/disabled when Collector box is checked

* Refactored AITasking into CollectedVehicle.

* Updated reference to vehicle tasking based on AITasking refactor.  Fixed speed zone radius not updating correctly.

* Version update

* Refactored AITasking into CollectedVehicle

* Added check for CollectorRadius being more than SpeedZoneRadius.  Added debug messages when values are reset to default.

* Added hint message if player tries to edit waypoints while 3D waypoints are disabled

* Fixed a bug where a 3D waypoint marker would be drawn even if 3D waypoints were disabled

* Removed unnecessary property setting when a vehicle is being removed from a path.

* Added a check for if the driver loses their task and reassigns it.

* Fixed a bug where the 3D line between waypoints was still being drawn even though 3D waypoints were disabled in the settings menu.

* Updated version

* Added console command to show info about collected vehicles.

* Added ConsoleCommand class, removed AITasking class.

* Removed class after refactoring into CollectedVehicle

* Update README.md

* Update README.md

* Added ini setting for Advanced Barrier Options.  Added enum for TrafficLight state.  Added debug message for invalid barriers.

* Updated version

* Added MousePositionInWorld and RNUIMouseInputHandler classes

* Fixed a crash when a collected ped is arrested.

* Refactored to implement RNUIMouseInputHandler class.

* Removed unused method.

* Updated version

* Disabled deletion/creation of shadow barrier and re-enabled updating position based on mouse position.

* Added check for Driver's current vehicle in driving loop in case the Driver exited Vehicle at some point.  Adjusted some guard clause logic and log messages.

* Removed unused variable

* Renamed some methods to improve clarity

* Updated version.
2020-11-26 07:41:22 -07:00

228 lines
8.6 KiB
C#

using Rage;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace SceneManager
{
public class Path
{
internal int Number { get; set; }
internal bool IsEnabled { get; set; }
internal State State { get; set; }
internal List<Waypoint> Waypoints = new List<Waypoint>();
internal List<CollectedVehicle> CollectedVehicles = new List<CollectedVehicle>();
internal Path(int pathNum, State pathState)
{
Number = pathNum;
State = pathState;
DrawLinesBetweenWaypoints();
}
private void LowerWaypointBlipsOpacity()
{
foreach (Waypoint wp in Waypoints)
{
wp.Blip.Alpha = 0.5f;
if (wp.CollectorRadiusBlip)
{
wp.CollectorRadiusBlip.Alpha = 0.25f;
}
}
}
private void RestoreWaypointBlipsOpacity()
{
foreach (Waypoint wp in Waypoints)
{
if (wp.Blip)
{
wp.Blip.Alpha = 1.0f;
if (wp.CollectorRadiusBlip)
{
wp.CollectorRadiusBlip.Alpha = 0.5f;
}
}
}
}
internal void DisablePath()
{
IsEnabled = false;
foreach(Waypoint wp in Waypoints)
{
wp.RemoveSpeedZone();
}
if (SettingsMenu.mapBlips.Checked)
{
LowerWaypointBlipsOpacity();
}
}
internal void EnablePath()
{
IsEnabled = true;
foreach (Waypoint wp in Waypoints)
{
if (wp.IsCollector)
{
wp.AddSpeedZone();
}
}
if (SettingsMenu.mapBlips.Checked)
{
RestoreWaypointBlipsOpacity();
}
}
internal void DrawLinesBetweenWaypoints()
{
GameFiber.StartNew(() =>
{
while(true)
{
if (SettingsMenu.threeDWaypoints.Checked && (State == State.Finished && MenuManager.menuPool.IsAnyMenuOpen()) || (State == State.Creating && PathCreationMenu.pathCreationMenu.Visible))
{
for (int i = 0; i < Waypoints.Count; i++)
{
if (i != Waypoints.Count - 1)
{
if (Waypoints[i + 1].IsStopWaypoint)
{
Debug.DrawLine(Waypoints[i].Position, Waypoints[i + 1].Position, Color.Orange);
}
else
{
Debug.DrawLine(Waypoints[i].Position, Waypoints[i + 1].Position, Color.Green);
}
}
}
}
GameFiber.Yield();
}
});
}
internal void LoopForVehiclesToBeDismissed()
{
GameFiber.StartNew(() =>
{
while (PathMainMenu.paths.Contains(this))
{
//Logger.Log($"Dismissing unused vehicles for cleanup");
foreach (CollectedVehicle cv in CollectedVehicles.Where(cv => cv.Vehicle))
{
if (!cv.Vehicle.IsDriveable || cv.Vehicle.IsUpsideDown || !cv.Vehicle.HasDriver)
{
if (cv.Vehicle.HasDriver)
{
cv.Vehicle.Driver.Dismiss();
}
cv.Vehicle.Dismiss();
}
}
CollectedVehicles.RemoveAll(cv => !cv.Vehicle);
GameFiber.Sleep(60000);
}
});
}
internal void LoopWaypointCollection()
{
uint lastProcessTime = Game.GameTime; // Store the last time the full loop finished; this is a value in ms
int yieldAfterChecks = 50; // How many calculations to do before yielding
while (PathMainMenu.paths.Contains(this))
{
if (IsEnabled)
{
int checksDone = 0;
try
{
foreach (Waypoint waypoint in Waypoints)
{
if (waypoint != null & waypoint.IsCollector)
{
foreach (Vehicle v in World.GetAllVehicles())
{
if (VehicleIsNearWaypoint(v, waypoint) && VehicleIsValidForCollection(v))
{
CollectedVehicle newCollectedVehicle = AddVehicleToCollection(v);
GameFiber AssignTasksFiber = new GameFiber(() => newCollectedVehicle.AssignWaypointTasks(this, waypoint));
//GameFiber AssignTasksFiber = new GameFiber(() => AITasking.AssignWaypointTasks(newCollectedVehicle, this, waypoint));
AssignTasksFiber.Start();
}
checksDone++; // Increment the counter inside the vehicle loop
if (checksDone % yieldAfterChecks == 0)
{
GameFiber.Yield(); // Yield the game fiber after the specified number of vehicles have been checked
}
}
}
}
}
catch
{
//return;
}
}
GameFiber.Sleep((int)Math.Max(1, Game.GameTime - lastProcessTime)); // If the prior lines took more than a second to run, then you'll run again almost immediately, but if they ran fairly quickly, you can sleep the loop until the remainder of the time between checks has passed
lastProcessTime = Game.GameTime;
}
CollectedVehicle AddVehicleToCollection(Vehicle vehicle)
{
var collectedVehicle = new CollectedVehicle(vehicle, this);
CollectedVehicles.Add(collectedVehicle);
Game.LogTrivial($"Added {vehicle.Model.Name} to collection from path {Number} waypoint {1}.");
return collectedVehicle;
}
bool VehicleIsNearWaypoint(Vehicle v, Waypoint wp)
{
return v.FrontPosition.DistanceTo2D(wp.Position) <= wp.CollectorRadius && Math.Abs(wp.Position.Z - v.Position.Z) < 3;
}
bool VehicleIsValidForCollection(Vehicle v)
{
if (v && v != Game.LocalPlayer.Character.LastVehicle && (v.IsCar || v.IsBike || v.IsBicycle || v.IsQuadBike) && !v.IsSirenOn && v.IsEngineOn && v.IsOnAllWheels && v.Speed > 1 && !CollectedVehicles.Any(cv => cv?.Vehicle == v))
{
var vehicleCollectedOnAnotherPath = PathMainMenu.paths.Any(p => p.Number != Number && p.CollectedVehicles.Any(cv => cv.Vehicle == v));
if (vehicleCollectedOnAnotherPath)
{
return false;
}
if (v.HasDriver && v.Driver && !v.Driver.IsAlive)
{
return false;
}
if (!v.HasDriver)
{
v.CreateRandomDriver();
while (!v.HasDriver)
{
GameFiber.Yield();
}
if (v && v.Driver)
{
v.Driver.IsPersistent = true;
v.Driver.BlockPermanentEvents = true;
}
else
{
return false;
}
}
return true;
}
else
{
return false;
}
}
}
}
}