| | 1 | | using UnityEngine; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | |
|
| | 7 | | // Integrated Air Defense System |
| | 8 | | public class IADS : MonoBehaviour { |
| | 9 | | public enum ThreatAssignmentStyle { ONE_TIME, CONTINUOUS } |
| | 10 | |
|
| 0 | 11 | | public static IADS Instance { get; private set; } |
| | 12 | | private IAssignment _assignmentScheme; |
| | 13 | |
|
| | 14 | | [SerializeField] |
| 0 | 15 | | private List<ThreatData> _threatTable = new List<ThreatData>(); |
| 0 | 16 | | private Dictionary<Threat, ThreatData> _threatDataMap = new Dictionary<Threat, ThreatData>(); |
| | 17 | |
|
| 0 | 18 | | private List<Interceptor> _assignmentQueue = new List<Interceptor>(); |
| | 19 | |
|
| 0 | 20 | | private void Awake() { |
| 0 | 21 | | if (Instance == null) { |
| 0 | 22 | | Instance = this; |
| 0 | 23 | | } else { |
| 0 | 24 | | Destroy(gameObject); |
| 0 | 25 | | } |
| 0 | 26 | | } |
| | 27 | |
|
| 0 | 28 | | private void Start() { |
| 0 | 29 | | SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded; |
| 0 | 30 | | SimManager.Instance.OnNewThreat += RegisterNewThreat; |
| 0 | 31 | | SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor; |
| 0 | 32 | | _assignmentScheme = new ThreatAssignment(); |
| 0 | 33 | | } |
| | 34 | |
|
| 0 | 35 | | public void LateUpdate() { |
| 0 | 36 | | if (_assignmentQueue.Count > 0) { |
| 0 | 37 | | int popCount = 100; |
| | 38 | |
|
| | 39 | | // Take up to popCount interceptors from the queue |
| 0 | 40 | | List<Interceptor> interceptorsToAssign = _assignmentQueue.Take(popCount).ToList(); |
| 0 | 41 | | AssignInterceptorsToThreats(interceptorsToAssign); |
| | 42 | |
|
| | 43 | | // Remove the processed interceptors from the queue |
| 0 | 44 | | _assignmentQueue.RemoveRange(0, Math.Min(popCount, _assignmentQueue.Count)); |
| | 45 | |
|
| | 46 | | // Check if any interceptors were not assigned |
| 0 | 47 | | List<Interceptor> assignedInterceptors = |
| 0 | 48 | | interceptorsToAssign.Where(m => m.HasAssignedTarget()).ToList(); |
| | 49 | |
|
| 0 | 50 | | if (assignedInterceptors.Count < interceptorsToAssign.Count) { |
| | 51 | | // Back into the queue they go! |
| 0 | 52 | | _assignmentQueue.AddRange(interceptorsToAssign.Except(assignedInterceptors)); |
| 0 | 53 | | Debug.Log($"Backing interceptors into the queue. Failed to assign."); |
| 0 | 54 | | } |
| 0 | 55 | | } |
| 0 | 56 | | } |
| | 57 | |
|
| 0 | 58 | | public void RequestThreatAssignment(List<Interceptor> interceptors) { |
| 0 | 59 | | _assignmentQueue.AddRange(interceptors); |
| 0 | 60 | | } |
| | 61 | |
|
| 0 | 62 | | public void RequestThreatAssignment(Interceptor interceptor) { |
| 0 | 63 | | _assignmentQueue.Add(interceptor); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Assigns the specified list of missiles to available targets based on the assignment scheme. |
| | 68 | | /// </summary> |
| | 69 | | /// <param name="missilesToAssign">The list of missiles to assign.</param> |
| 0 | 70 | | public void AssignInterceptorsToThreats(List<Interceptor> missilesToAssign) { |
| | 71 | | // Perform the assignment |
| 0 | 72 | | IEnumerable<IAssignment.AssignmentItem> assignments = |
| | 73 | | _assignmentScheme.Assign(missilesToAssign, _threatTable); |
| | 74 | |
|
| | 75 | | // Apply the assignments to the missiles |
| 0 | 76 | | foreach (var assignment in assignments) { |
| 0 | 77 | | assignment.Interceptor.AssignTarget(assignment.Threat); |
| 0 | 78 | | _threatDataMap[assignment.Threat].AssignInterceptor(assignment.Interceptor); |
| | 79 | | // Debug.Log( |
| | 80 | | // $"Interceptor {assignment.Interceptor.name} assigned to threat |
| | 81 | | // {assignment.Threat.name}"); |
| 0 | 82 | | } |
| 0 | 83 | | } |
| | 84 | |
|
| 0 | 85 | | public void RegisterNewThreat(Threat threat) { |
| 0 | 86 | | ThreatData threatData = new ThreatData(threat, threat.gameObject.name); |
| 0 | 87 | | _threatTable.Add(threatData); |
| 0 | 88 | | _threatDataMap.Add(threat, threatData); |
| | 89 | |
|
| | 90 | | // Subscribe to the threat's events |
| | 91 | | // TODO: If we do not want omniscient IADS, we |
| | 92 | | // need to model the IADS's sensors here. |
| 0 | 93 | | threat.OnThreatHit += RegisterThreatHit; |
| 0 | 94 | | threat.OnThreatMiss += RegisterThreatMiss; |
| 0 | 95 | | } |
| | 96 | |
|
| 0 | 97 | | public void RegisterNewInterceptor(Interceptor interceptor) { |
| | 98 | | // Placeholder |
| 0 | 99 | | interceptor.OnInterceptMiss += RegisterInterceptorMiss; |
| 0 | 100 | | interceptor.OnInterceptHit += RegisterInterceptorHit; |
| 0 | 101 | | } |
| | 102 | |
|
| 0 | 103 | | private void RegisterInterceptorHit(Interceptor interceptor, Threat threat) { |
| 0 | 104 | | ThreatData threatData = _threatDataMap[threat]; |
| 0 | 105 | | if (threatData != null) { |
| 0 | 106 | | threatData.RemoveInterceptor(interceptor); |
| 0 | 107 | | MarkThreatDestroyed(threatData); |
| 0 | 108 | | } |
| 0 | 109 | | } |
| | 110 | |
|
| 0 | 111 | | private void RegisterInterceptorMiss(Interceptor interceptor, Threat threat) { |
| | 112 | | // Remove the interceptor from the threat's assigned interceptors |
| 0 | 113 | | _threatDataMap[threat].RemoveInterceptor(interceptor); |
| 0 | 114 | | } |
| | 115 | |
|
| 0 | 116 | | private void MarkThreatDestroyed(ThreatData threatData) { |
| 0 | 117 | | if (threatData != null) { |
| 0 | 118 | | threatData.MarkDestroyed(); |
| 0 | 119 | | } |
| 0 | 120 | | } |
| | 121 | |
|
| 0 | 122 | | private void RegisterThreatHit(Threat threat) { |
| 0 | 123 | | ThreatData threatData = _threatDataMap[threat]; |
| 0 | 124 | | if (threatData != null) { |
| 0 | 125 | | MarkThreatDestroyed(threatData); |
| 0 | 126 | | } |
| 0 | 127 | | } |
| | 128 | |
|
| 0 | 129 | | private void RegisterThreatMiss(Threat threat) { |
| | 130 | | // The threat missed (meaning it hit the floor, etc) |
| 0 | 131 | | ThreatData threatData = _threatDataMap[threat]; |
| 0 | 132 | | if (threatData != null) { |
| 0 | 133 | | MarkThreatDestroyed(threatData); |
| 0 | 134 | | } |
| | 135 | | // threatData.RemoveInterceptor(null); |
| 0 | 136 | | } |
| | 137 | |
|
| 0 | 138 | | private void RegisterSimulationEnded() { |
| 0 | 139 | | _threatTable.Clear(); |
| 0 | 140 | | _threatDataMap.Clear(); |
| 0 | 141 | | _assignmentQueue.Clear(); |
| 0 | 142 | | } |
| | 143 | | } |