| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.IO; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | // Integrated Air Defense System. |
| | 9 | | public class IADS : MonoBehaviour { |
| 2 | 10 | | public static IADS Instance { get; private set; } |
| | 11 | |
|
| | 12 | | private const float LaunchInterceptorsPeriod = 0.4f; |
| | 13 | | private const float CheckForEscapingThreatsPeriod = 5.0f; |
| | 14 | | private const float ClusterThreatsPeriod = 2.0f; |
| | 15 | |
|
| | 16 | | // TODO(titan): Choose the CSV file based on the interceptor type. |
| 1 | 17 | | private ILaunchAnglePlanner _launchAnglePlanner = |
| | 18 | | new LaunchAngleCsvInterpolator(Path.Combine("Planning", "hydra70_launch_angle.csv")); |
| 1 | 19 | | private IAssignment _assignmentScheme = new MaxSpeedAssignment(); |
| | 20 | | private Coroutine _launchInterceptorsCoroutine; |
| | 21 | |
|
| | 22 | | [SerializeField] |
| 1 | 23 | | private List<TrackFileData> _trackFiles = new List<TrackFileData>(); |
| 1 | 24 | | private Dictionary<Agent, TrackFileData> _trackFileMap = new Dictionary<Agent, TrackFileData>(); |
| | 25 | |
|
| 1 | 26 | | private List<Interceptor> _assignmentQueue = new List<Interceptor>(); |
| 1 | 27 | | private List<Cluster> _threatClusters = new List<Cluster>(); |
| 1 | 28 | | private Dictionary<Cluster, ThreatClusterData> _threatClusterMap = |
| | 29 | | new Dictionary<Cluster, ThreatClusterData>(); |
| 1 | 30 | | private Dictionary<Interceptor, Cluster> _interceptorClusterMap = |
| | 31 | | new Dictionary<Interceptor, Cluster>(); |
| 1 | 32 | | private HashSet<Interceptor> _assignableInterceptors = new HashSet<Interceptor>(); |
| | 33 | |
|
| 1 | 34 | | private HashSet<Threat> _threatsToCluster = new HashSet<Threat>(); |
| | 35 | | private Coroutine _checkForEscapingThreatsCoroutine; |
| | 36 | | private Coroutine _clusterThreatsCoroutine; |
| | 37 | |
|
| 1 | 38 | | private int _trackFileIdTicker = 0; |
| | 39 | |
|
| 1 | 40 | | private void Awake() { |
| 2 | 41 | | if (Instance == null) { |
| 1 | 42 | | Instance = this; |
| 1 | 43 | | } else { |
| 0 | 44 | | Destroy(gameObject); |
| 0 | 45 | | } |
| 1 | 46 | | } |
| | 47 | |
|
| 0 | 48 | | private void OnDestroy() { |
| 0 | 49 | | if (_launchInterceptorsCoroutine != null) { |
| 0 | 50 | | StopCoroutine(_launchInterceptorsCoroutine); |
| 0 | 51 | | } |
| 0 | 52 | | if (_checkForEscapingThreatsCoroutine != null) { |
| 0 | 53 | | StopCoroutine(_checkForEscapingThreatsCoroutine); |
| 0 | 54 | | } |
| 0 | 55 | | if (_clusterThreatsCoroutine != null) { |
| 0 | 56 | | StopCoroutine(_clusterThreatsCoroutine); |
| 0 | 57 | | } |
| 0 | 58 | | } |
| | 59 | |
|
| 1 | 60 | | public void Start() { |
| 1 | 61 | | SimManager.Instance.OnSimulationStarted += RegisterSimulationStarted; |
| 1 | 62 | | SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded; |
| 1 | 63 | | SimManager.Instance.OnNewThreat += RegisterNewThreat; |
| 1 | 64 | | SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor; |
| 1 | 65 | | } |
| | 66 | |
|
| 27 | 67 | | public void LateUpdate() { |
| | 68 | | // Detach interceptors assigned to fully-destroyed clusters. |
| 27 | 69 | | CleanupDestroyedClusters(); |
| | 70 | |
|
| | 71 | | // Update the cluster centroids. |
| 1491 | 72 | | foreach (var cluster in _threatClusters) { |
| 470 | 73 | | cluster.Recenter(); |
| 470 | 74 | | _threatClusterMap[cluster].UpdateCentroid(); |
| 470 | 75 | | } |
| | 76 | |
|
| | 77 | | // Assign any interceptors that are no longer assigned to any threat. |
| 27 | 78 | | AssignInterceptorToThreat( |
| 0 | 79 | | _assignableInterceptors.Where(interceptor => !interceptor.IsTerminated()).ToList()); |
| 27 | 80 | | } |
| | 81 | |
|
| 10 | 82 | | private void RegisterSimulationStarted() { |
| 10 | 83 | | _launchInterceptorsCoroutine = |
| | 84 | | StartCoroutine(LaunchInterceptorsManager(LaunchInterceptorsPeriod)); |
| 10 | 85 | | _checkForEscapingThreatsCoroutine = |
| | 86 | | StartCoroutine(CheckForEscapingThreatsManager(CheckForEscapingThreatsPeriod)); |
| 10 | 87 | | _clusterThreatsCoroutine = StartCoroutine(ClusterThreatsManager(ClusterThreatsPeriod)); |
| 10 | 88 | | } |
| | 89 | |
|
| 10 | 90 | | private IEnumerator LaunchInterceptorsManager(float period) { |
| 20 | 91 | | while (true) { |
| | 92 | | // Check whether an interceptor should be launched at a cluster and launch it. |
| 10 | 93 | | CheckAndLaunchInterceptors(); |
| 10 | 94 | | yield return new WaitForSeconds(period); |
| 0 | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| 10 | 98 | | private void CheckAndLaunchInterceptors() { |
| 30 | 99 | | foreach (var cluster in _threatClusters) { |
| | 100 | | // Check whether an interceptor has already been assigned to the cluster. |
| 0 | 101 | | if (_threatClusterMap[cluster].Status != ThreatClusterStatus.UNASSIGNED) { |
| 0 | 102 | | continue; |
| | 103 | | } |
| | 104 | |
|
| | 105 | | // Check whether all threats in the cluster have terminated. |
| 0 | 106 | | bool allTerminated = cluster.Threats.All(threat => threat.IsTerminated()); |
| 0 | 107 | | if (allTerminated) { |
| 0 | 108 | | continue; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | // Create a predictor to track the cluster's centroid. |
| 0 | 112 | | IPredictor predictor = new LinearExtrapolator(_threatClusterMap[cluster].Centroid); |
| | 113 | |
|
| | 114 | | // Create a launch planner. |
| 0 | 115 | | ILaunchPlanner planner = new IterativeLaunchPlanner(_launchAnglePlanner, predictor); |
| 0 | 116 | | LaunchPlan plan = planner.Plan(); |
| | 117 | |
|
| | 118 | | // Check whether an interceptor should be launched. |
| 0 | 119 | | if (plan.ShouldLaunch) { |
| 0 | 120 | | Debug.Log( |
| | 121 | | $"Launching a carrier interceptor at an elevation of {plan.LaunchAngle} degrees to position {plan.InterceptP |
| 0 | 122 | | UIManager.Instance.LogActionMessage( |
| | 123 | | $"[IADS] Launching a carrier interceptor at an elevation of {plan.LaunchAngle} degrees to position {plan.Int |
| | 124 | |
|
| | 125 | | // Create a new interceptor. |
| 0 | 126 | | Configs.AgentConfig config = |
| | 127 | | SimManager.Instance.SimulationConfig.InterceptorSwarmConfigs.Count > 0 |
| | 128 | | ? SimManager.Instance.SimulationConfig.InterceptorSwarmConfigs[0].AgentConfig |
| | 129 | | : null; |
| 0 | 130 | | Simulation.State initialState = new Simulation.State(); |
| | 131 | |
|
| | 132 | | // Set the initial position, which defaults to the origin. |
| 0 | 133 | | initialState.Position = Coordinates3.ToProto(Vector3.zero); |
| | 134 | |
|
| | 135 | | // Set the initial velocity to point along the launch vector. |
| 0 | 136 | | initialState.Velocity = Coordinates3.ToProto(plan.GetNormalizedLaunchVector() * 1e-3f); |
| 0 | 137 | | Interceptor interceptor = SimManager.Instance.CreateInterceptor(config, initialState); |
| | 138 | |
|
| | 139 | | // Assign the interceptor to the cluster. |
| 0 | 140 | | _interceptorClusterMap[interceptor] = cluster; |
| 0 | 141 | | interceptor.AssignTarget(_threatClusterMap[cluster].Centroid); |
| 0 | 142 | | _threatClusterMap[cluster].AssignInterceptor(interceptor); |
| | 143 | |
|
| | 144 | | // Create an interceptor swarm. |
| 0 | 145 | | SimManager.Instance.AddInterceptorSwarm(new List<Agent> { interceptor as Agent }); |
| 0 | 146 | | } |
| 0 | 147 | | } |
| 10 | 148 | | } |
| | 149 | |
|
| 0 | 150 | | public bool ShouldLaunchSubmunitions(Interceptor carrier) { |
| 0 | 151 | | if (!HasClusterAssignment(carrier)) { |
| 0 | 152 | | return false; |
| | 153 | | } |
| | 154 | | // The carrier interceptor will spawn submunitions when any target is greater than 30 degrees |
| | 155 | | // away from the carrier interceptor's current velocity or when any threat is within 500 meters |
| | 156 | | // of the interceptor. |
| | 157 | | const float SubmunitionSpawnMaxAngularDeviation = 30.0f; |
| | 158 | | const float SubmunitionSpawnMinDistanceToThreat = 500.0f; |
| | 159 | | const float SubmunitionSpawnMaxDistanceToThreat = 2000.0f; |
| | 160 | | // TODO(titan): The prediction time should be a function of the submunition characteristic, such |
| | 161 | | // as the boost time. |
| | 162 | | const float SubmunitionSpawnPredictionTime = 0.6f; |
| | 163 | |
|
| 0 | 164 | | Cluster cluster = _interceptorClusterMap[carrier]; |
| 0 | 165 | | List<Threat> threats = cluster.Threats.ToList(); |
| 0 | 166 | | Vector3 carrierPosition = carrier.GetPosition(); |
| 0 | 167 | | Vector3 carrierVelocity = carrier.GetVelocity(); |
| 0 | 168 | | foreach (var threat in threats) { |
| 0 | 169 | | IPredictor predictor = new LinearExtrapolator(threat); |
| 0 | 170 | | PredictorState predictedState = predictor.Predict(SubmunitionSpawnPredictionTime); |
| 0 | 171 | | Vector3 positionToPredictedThreat = predictedState.Position - carrierPosition; |
| 0 | 172 | | float predictedDistanceToThreat = positionToPredictedThreat.magnitude; |
| | 173 | |
|
| | 174 | | // Check whether the distance to the threat is less than the minimum distance. |
| 0 | 175 | | if (predictedDistanceToThreat < SubmunitionSpawnMinDistanceToThreat) { |
| 0 | 176 | | return true; |
| | 177 | | } |
| | 178 | |
|
| | 179 | | // Check whether the angular deviation exceeds the maximum angular deviation. |
| 0 | 180 | | float distanceDeviation = |
| | 181 | | (Vector3.ProjectOnPlane(positionToPredictedThreat, carrierVelocity)).magnitude; |
| 0 | 182 | | float angularDeviation = |
| | 183 | | Mathf.Asin(distanceDeviation / predictedDistanceToThreat) * Mathf.Rad2Deg; |
| 0 | 184 | | if (angularDeviation > SubmunitionSpawnMaxAngularDeviation && |
| 0 | 185 | | predictedDistanceToThreat < SubmunitionSpawnMaxDistanceToThreat) { |
| 0 | 186 | | return true; |
| | 187 | | } |
| 0 | 188 | | } |
| 0 | 189 | | return false; |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | // True if an interceptor still has a valid cluster assignment. |
| 0 | 193 | | private bool HasClusterAssignment(Interceptor interceptor) { |
| 0 | 194 | | return _interceptorClusterMap.ContainsKey(interceptor); |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | // Releases interceptors from clusters that only contain destroyed threats. |
| 27 | 198 | | private void CleanupDestroyedClusters() { |
| 1491 | 199 | | foreach (Cluster cluster in _threatClusters) { |
| 940 | 200 | | if (!cluster.IsFullyTerminated()) { |
| 470 | 201 | | continue; |
| | 202 | | } |
| | 203 | |
|
| 0 | 204 | | IReadOnlyList<Interceptor> assigned = _threatClusterMap[cluster].AssignedInterceptors; |
| 0 | 205 | | if (assigned.Count == 0) { |
| 0 | 206 | | continue; |
| | 207 | | } |
| | 208 | |
|
| 0 | 209 | | foreach (Interceptor interceptor in assigned.ToList()) { |
| 0 | 210 | | ReleaseInterceptorFromCluster(cluster, interceptor); |
| 0 | 211 | | } |
| 0 | 212 | | } |
| 27 | 213 | | } |
| | 214 | |
|
| | 215 | | // Detach a single interceptor from a cluster and decide its next action. |
| | 216 | | // All interceptors are queued for reassignment in the hierarchical architecture, |
| | 217 | | // including carrier interceptors. |
| 0 | 218 | | private void ReleaseInterceptorFromCluster(Cluster cluster, Interceptor interceptor) { |
| 0 | 219 | | _threatClusterMap[cluster].RemoveInterceptor(interceptor); |
| 0 | 220 | | _interceptorClusterMap.Remove(interceptor); |
| | 221 | | // TODO(titan): During the hierarchical architecture overhaul/refactor, |
| | 222 | | // we need to properly handle the assignment of carrier interceptors at this point. |
| | 223 | | // Since they have been unassigned from their cluster, they will need to be queued for |
| | 224 | | // a new cluster assignment. Right now, they will go for re-assignment but fail |
| | 225 | | // since they return false for IsAssignable(). |
| 0 | 226 | | RequestAssignInterceptorToThreat(interceptor); |
| 0 | 227 | | } |
| | 228 | |
|
| 0 | 229 | | public void AssignSubmunitionsToThreats(Interceptor carrier, List<Interceptor> interceptors) { |
| | 230 | | // Assign threats to the submunitions. |
| 0 | 231 | | Cluster cluster = _interceptorClusterMap[carrier]; |
| 0 | 232 | | List<Threat> threats = cluster.Threats.ToList(); |
| 0 | 233 | | IEnumerable<IAssignment.AssignmentItem> assignments = |
| | 234 | | _assignmentScheme.Assign(interceptors, threats); |
| | 235 | |
|
| | 236 | | // Mark the cluster as delegated to submunitions. |
| 0 | 237 | | _threatClusterMap[cluster].RemoveInterceptor(carrier, delegated: true); |
| | 238 | |
|
| | 239 | | // Apply the assignments to the submunitions. |
| 0 | 240 | | foreach (var assignment in assignments) { |
| 0 | 241 | | assignment.Interceptor.AssignTarget(assignment.Threat); |
| 0 | 242 | | } |
| | 243 | |
|
| | 244 | | // Check whether any submunitions were not assigned to a threat. |
| 0 | 245 | | foreach (var interceptor in interceptors) { |
| 0 | 246 | | if (!interceptor.HasAssignedTarget()) { |
| 0 | 247 | | RequestAssignInterceptorToThreat(interceptor); |
| 0 | 248 | | } |
| 0 | 249 | | } |
| 0 | 250 | | } |
| | 251 | |
|
| 0 | 252 | | public void RequestAssignInterceptorToThreat(Interceptor interceptor) { |
| 0 | 253 | | interceptor.UnassignTarget(); |
| 0 | 254 | | _assignableInterceptors.Add(interceptor); |
| 0 | 255 | | } |
| | 256 | |
|
| 27 | 257 | | private void AssignInterceptorToThreat(in IReadOnlyList<Interceptor> interceptors) { |
| 54 | 258 | | if (interceptors.Count == 0) { |
| 27 | 259 | | return; |
| | 260 | | } |
| | 261 | |
|
| | 262 | | // The threat originally assigned to the interceptor has been terminated, so assign another |
| | 263 | | // threat to the interceptor. |
| | 264 | |
|
| | 265 | | // This pulls from all available track files, not from our previously assigned cluster. |
| 0 | 266 | | List<Threat> threats = _trackFiles.Where(trackFile => trackFile.Agent is Threat) |
| 0 | 267 | | .Select(trackFile => trackFile.Agent as Threat) |
| | 268 | | .ToList(); |
| 0 | 269 | | if (threats.Count == 0) { |
| 0 | 270 | | return; |
| | 271 | | } |
| | 272 | |
|
| 0 | 273 | | IEnumerable<IAssignment.AssignmentItem> assignments = |
| | 274 | | _assignmentScheme.Assign(interceptors, threats); |
| | 275 | |
|
| | 276 | | // Apply the assignments to the submunitions. |
| 0 | 277 | | foreach (var assignment in assignments) { |
| 0 | 278 | | assignment.Interceptor.AssignTarget(assignment.Threat); |
| 0 | 279 | | _assignableInterceptors.Remove(assignment.Interceptor); |
| 0 | 280 | | } |
| 27 | 281 | | } |
| | 282 | |
|
| 755 | 283 | | public void RequestClusterThreat(Threat threat) { |
| 755 | 284 | | _threatsToCluster.Add(threat); |
| 755 | 285 | | } |
| | 286 | |
|
| 10 | 287 | | private IEnumerator CheckForEscapingThreatsManager(float period) { |
| 20 | 288 | | while (true) { |
| 10 | 289 | | yield return new WaitForSeconds(period); |
| 0 | 290 | | CheckForEscapingThreats(); |
| 0 | 291 | | } |
| | 292 | | } |
| | 293 | |
|
| 0 | 294 | | private void CheckForEscapingThreats() { |
| 0 | 295 | | List<Threat> threats = _trackFiles |
| 0 | 296 | | .Where(trackFile => trackFile.Status == TrackStatus.ASSIGNED && |
| | 297 | | trackFile.Agent is Threat) |
| 0 | 298 | | .Select(trackFile => trackFile.Agent as Threat) |
| | 299 | | .ToList(); |
| 0 | 300 | | if (threats.Count == 0) { |
| 0 | 301 | | return; |
| | 302 | | } |
| | 303 | |
|
| | 304 | | // Check whether the threats are escaping the pursuing interceptors. |
| 0 | 305 | | foreach (var threat in threats) { |
| 0 | 306 | | bool isEscaping = threat.AssignedInterceptors.All(interceptor => { |
| 0 | 307 | | Vector3 interceptorPosition = interceptor.GetPosition(); |
| 0 | 308 | | Vector3 threatPosition = threat.GetPosition(); |
| | 309 | |
|
| 0 | 310 | | float threatTimeToHit = (float)(threatPosition.magnitude / threat.GetSpeed()); |
| 0 | 311 | | float interceptorTimeToHit = |
| | 312 | | (float)((threatPosition - interceptorPosition).magnitude / interceptor.GetSpeed()); |
| 0 | 313 | | return interceptorPosition.magnitude > threatPosition.magnitude || |
| | 314 | | threatTimeToHit < interceptorTimeToHit; |
| 0 | 315 | | }); |
| 0 | 316 | | if (isEscaping) { |
| 0 | 317 | | RequestClusterThreat(threat); |
| 0 | 318 | | } |
| 0 | 319 | | } |
| 0 | 320 | | } |
| | 321 | |
|
| 10 | 322 | | private IEnumerator ClusterThreatsManager(float period) { |
| 20 | 323 | | while (true) { |
| 10 | 324 | | ClusterThreats(); |
| 10 | 325 | | yield return new WaitForSeconds(period); |
| 0 | 326 | | } |
| | 327 | | } |
| | 328 | |
|
| 10 | 329 | | private void ClusterThreats() { |
| | 330 | | // Maximum number of threats per cluster. |
| | 331 | | const int MaxSize = 7; |
| | 332 | | // Maximum cluster radius in meters. |
| | 333 | | const float MaxRadius = 500; |
| | 334 | |
|
| | 335 | | // Filter the threats. |
| 10 | 336 | | List<Threat> threats = |
| | 337 | | _threatsToCluster |
| 755 | 338 | | .Where(threat => !threat.IsTerminated() && threat.AssignedInterceptors.Count == 0) |
| | 339 | | .ToList(); |
| 10 | 340 | | if (threats.Count == 0) { |
| 0 | 341 | | return; |
| | 342 | | } |
| | 343 | |
|
| | 344 | | // Cluster threats. |
| 10 | 345 | | IClusterer clusterer = new AgglomerativeClusterer(new List<Agent>(threats), MaxSize, MaxRadius); |
| 10 | 346 | | clusterer.Cluster(); |
| 10 | 347 | | var clusters = clusterer.Clusters; |
| 10 | 348 | | Debug.Log($"Clustered {threats.Count} threats into {clusters.Count} clusters."); |
| 10 | 349 | | UIManager.Instance.LogActionMessage( |
| | 350 | | $"[IADS] Clustered {threats.Count} threats into {clusters.Count} clusters."); |
| | 351 | |
|
| 10 | 352 | | _threatClusters = clusters.ToList(); |
| 561 | 353 | | foreach (var cluster in clusters) { |
| 177 | 354 | | _threatClusterMap.Add(cluster, new ThreatClusterData(cluster)); |
| 177 | 355 | | } |
| | 356 | |
|
| 10 | 357 | | _threatsToCluster.Clear(); |
| 10 | 358 | | } |
| | 359 | |
|
| 755 | 360 | | public void RegisterNewThreat(Threat threat) { |
| 755 | 361 | | string trackID = $"T{1000 + ++_trackFileIdTicker}"; |
| 755 | 362 | | ThreatData trackFile = new ThreatData(threat, trackID); |
| 755 | 363 | | _trackFiles.Add(trackFile); |
| 755 | 364 | | _trackFileMap.Add(threat, trackFile); |
| 755 | 365 | | RequestClusterThreat(threat); |
| | 366 | |
|
| 755 | 367 | | threat.OnThreatHit += RegisterThreatHit; |
| 755 | 368 | | threat.OnThreatMiss += RegisterThreatMiss; |
| 755 | 369 | | } |
| | 370 | |
|
| 0 | 371 | | public void RegisterNewInterceptor(Interceptor interceptor) { |
| 0 | 372 | | string trackID = $"I{2000 + ++_trackFileIdTicker}"; |
| 0 | 373 | | InterceptorData trackFile = new InterceptorData(interceptor, trackID); |
| 0 | 374 | | _trackFiles.Add(trackFile); |
| 0 | 375 | | _trackFileMap.Add(interceptor, trackFile); |
| | 376 | |
|
| 0 | 377 | | interceptor.OnInterceptMiss += RegisterInterceptorMiss; |
| 0 | 378 | | interceptor.OnInterceptHit += RegisterInterceptorHit; |
| 0 | 379 | | } |
| | 380 | |
|
| 0 | 381 | | private void RegisterInterceptorHit(Interceptor interceptor, Threat threat) { |
| 0 | 382 | | var threatTrack = _trackFileMap[threat] as ThreatData; |
| 0 | 383 | | var interceptorTrack = _trackFileMap[interceptor] as InterceptorData; |
| | 384 | |
|
| 0 | 385 | | if (threatTrack != null) { |
| 0 | 386 | | threatTrack.RemoveInterceptor(interceptor); |
| 0 | 387 | | threatTrack.MarkDestroyed(); |
| 0 | 388 | | } |
| | 389 | |
|
| 0 | 390 | | if (interceptorTrack != null) { |
| 0 | 391 | | interceptorTrack.RemoveThreat(threat); |
| 0 | 392 | | interceptorTrack.MarkDestroyed(); |
| 0 | 393 | | } |
| | 394 | |
|
| | 395 | | // Assign the interceptors to other threats. |
| 0 | 396 | | foreach (var assignedInterceptor in threat.AssignedInterceptors.ToList()) { |
| 0 | 397 | | if (assignedInterceptor is not CarrierInterceptor) { |
| 0 | 398 | | RequestAssignInterceptorToThreat(assignedInterceptor as Interceptor); |
| 0 | 399 | | } |
| 0 | 400 | | } |
| 0 | 401 | | } |
| | 402 | |
|
| 0 | 403 | | private void RegisterInterceptorMiss(Interceptor interceptor, Threat threat) { |
| | 404 | | // Assign the interceptor to another threat. |
| 0 | 405 | | RequestAssignInterceptorToThreat(interceptor); |
| | 406 | |
|
| 0 | 407 | | var threatTrack = _trackFileMap[threat] as ThreatData; |
| 0 | 408 | | var interceptorTrack = _trackFileMap[interceptor] as InterceptorData; |
| | 409 | |
|
| 0 | 410 | | if (threatTrack != null) { |
| 0 | 411 | | threatTrack.RemoveInterceptor(interceptor); |
| | 412 | |
|
| | 413 | | // Check if the threat is being targeted by at least one interceptor. |
| 0 | 414 | | if (threatTrack.AssignedInterceptorCount == 0) { |
| 0 | 415 | | RequestClusterThreat(threat); |
| 0 | 416 | | } |
| 0 | 417 | | } |
| | 418 | |
|
| 0 | 419 | | if (interceptorTrack != null) { |
| 0 | 420 | | interceptorTrack.RemoveThreat(threat); |
| 0 | 421 | | interceptorTrack.MarkDestroyed(); |
| 0 | 422 | | } |
| 0 | 423 | | } |
| | 424 | |
|
| 0 | 425 | | private void RegisterThreatHit(Threat threat) { |
| 0 | 426 | | var threatTrack = _trackFileMap[threat] as ThreatData; |
| 0 | 427 | | if (threatTrack != null) { |
| 0 | 428 | | threatTrack.MarkDestroyed(); |
| 0 | 429 | | } |
| | 430 | |
|
| | 431 | | // Re-assign the assigned interceptors to other threats. |
| 0 | 432 | | foreach (var interceptor in threat.AssignedInterceptors.ToList()) { |
| 0 | 433 | | RequestAssignInterceptorToThreat(interceptor as Interceptor); |
| 0 | 434 | | } |
| 0 | 435 | | } |
| | 436 | |
|
| 0 | 437 | | private void RegisterThreatMiss(Threat threat) { |
| | 438 | | // The threat missed (e.g., it hit the floor). |
| | 439 | | // Re-assign the assigned interceptors to other threats. |
| 0 | 440 | | foreach (var interceptor in threat.AssignedInterceptors.ToList()) { |
| 0 | 441 | | RequestAssignInterceptorToThreat(interceptor as Interceptor); |
| 0 | 442 | | } |
| | 443 | |
|
| 0 | 444 | | var threatTrack = _trackFileMap[threat] as ThreatData; |
| 0 | 445 | | if (threatTrack != null) { |
| 0 | 446 | | threatTrack.MarkDestroyed(); |
| 0 | 447 | | } |
| 0 | 448 | | } |
| | 449 | |
|
| 0 | 450 | | public List<ThreatData> GetThreatTracks() => _trackFiles.OfType<ThreatData>().ToList(); |
| | 451 | |
|
| | 452 | | public List<InterceptorData> GetInterceptorTracks() => |
| 0 | 453 | | _trackFiles.OfType<InterceptorData>().ToList(); |
| | 454 | |
|
| 9 | 455 | | private void RegisterSimulationEnded() { |
| 9 | 456 | | _trackFiles.Clear(); |
| 9 | 457 | | _trackFileMap.Clear(); |
| 9 | 458 | | _assignmentQueue.Clear(); |
| 9 | 459 | | _threatClusters.Clear(); |
| 9 | 460 | | _threatClusterMap.Clear(); |
| 9 | 461 | | _interceptorClusterMap.Clear(); |
| 9 | 462 | | _assignableInterceptors.Clear(); |
| 9 | 463 | | _threatsToCluster.Clear(); |
| 9 | 464 | | _trackFileIdTicker = 0; |
| 9 | 465 | | } |
| | 466 | | } |