private void SetCurrentTask(AIBackupUnitTask task) { currentTask = task; currentTask.Finished += OnCurrentTaskFinished; currentTask.Start(); TaskStarted?.Invoke(currentTask); }
private void OnCurrentTaskFinished(AIBackupUnitTask task, bool aborted) { if (currentTask != null) { currentTask.Finished -= OnCurrentTaskFinished; TaskFinished?.Invoke(currentTask); currentTask = null; } }
public static void SendFirefightersUnit(Vector3 position, FirefighterBackupTask task) { RotatedVector3 spawn = Util.GetSpawnLocationAroundPlayer(true); FirefightersBackupUnit[] currentUnits = BackupUnit.GetAllBackupUnitsOfType <FirefightersBackupUnit>(); FirefightersBackupUnit unit = (currentUnits == null || currentUnits.Length == 0) ? null : currentUnits.Where(u => !u.IsDismissedOrDeleted && !u.IsResponding).OrderBy(u => Vector3.DistanceSquared(u.Vehicle.Position, position)).FirstOrDefault(); if (unit == null) { unit = new FirefightersBackupUnit(spawn.Position, spawn.Heading); } else { unit.AI.AbortAllTasks(); } unit.AI.DriveToPosition(position, true, 15f, 20.0f, VehicleDrivingFlags.Emergency); unit.IsResponding = true; switch (task) { case FirefighterBackupTask.ExtinguishFireInArea: unit.AI.ExtinguishFireInArea(position, 125.0f, false); break; } AIBackupUnitTask unitTask = unit.AI.DriveToPosition(FireStationsManager.Instance.Buildings.OrderBy(s => Vector3.DistanceSquared(s.Entrance, spawn.Position)).First().Entrance, false, 8f, 50.0f, VehicleDrivingFlags.Normal, false); unitTask.Started += (t) => { unit.IsResponding = false; }; unitTask.Finished += (t, aborted) => { if (!aborted) { if (Vector3.DistanceSquared(Game.LocalPlayer.Character.Position, unit.Vehicle) > 70.0f * 70.0f) { unit.Delete(); } else { unit.Dismiss(); } } }; }
// if considerTaskPriority is true and tasks priority is greater than current task, current task is aborted and the queue is cleared protected AIBackupUnitTask GiveTask <TTask>(bool considerTaskPriority = true, params object[] args) where TTask : AIBackupUnitTask { AIBackupUnitTask t = (AIBackupUnitTask)Activator.CreateInstance(typeof(TTask), BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, args, null); Game.LogTrivial($"[{this.GetType().Name}.GiveTask] ({typeof(TTask).Name}, ConsiderTaskPriority:{considerTaskPriority})"); if (!IsDoingAnyTask) { Game.LogTrivial($"[{this.GetType().Name}.GiveTask] No task running, setting as current task..."); SetCurrentTask(t); } else { if (considerTaskPriority) { if (t.Priority > CurrentTaskPriority) { Game.LogTrivial($"[{this.GetType().Name}.GiveTask] Priority greater than current task, aborting current task, clearing queue and setting as current task..."); tasksQueue.Clear(); currentTask.Abort(); SetCurrentTask(t); } else { Game.LogTrivial($"[{this.GetType().Name}.GiveTask] Priority less than current task, enqueuing task..."); tasksQueue.Enqueue(t); } } else { Game.LogTrivial($"[{this.GetType().Name}.GiveTask] Enqueuing task..."); tasksQueue.Enqueue(t); } } return(t); }