< Summary

Class:IADS
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/IADS/IADS.cs
Covered lines:0
Uncovered lines:87
Coverable lines:87
Total lines:143
Line coverage:0% (0 of 87)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:17
Method coverage:0% (0 of 17)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
IADS()0%2100%
Awake()0%6200%
Start()0%2100%
LateUpdate()0%20400%
RequestThreatAssignment(...)0%2100%
RequestThreatAssignment(...)0%2100%
AssignInterceptorsToThreats(...)0%12300%
RegisterNewThreat(...)0%2100%
RegisterNewInterceptor(...)0%2100%
RegisterInterceptorHit(...)0%6200%
RegisterInterceptorMiss(...)0%2100%
MarkThreatDestroyed(...)0%6200%
RegisterThreatHit(...)0%6200%
RegisterThreatMiss(...)0%6200%
RegisterSimulationEnded()0%2100%

File(s)

/github/workspace/Assets/Scripts/IADS/IADS.cs

#LineLine coverage
 1using UnityEngine;
 2using System;
 3using System.Collections;
 4using System.Collections.Generic;
 5using System.Linq;
 6
 7// Integrated Air Defense System
 8public class IADS : MonoBehaviour {
 9  public enum ThreatAssignmentStyle { ONE_TIME, CONTINUOUS }
 10
 011  public static IADS Instance { get; private set; }
 12  private IAssignment _assignmentScheme;
 13
 14  [SerializeField]
 015  private List<ThreatData> _threatTable = new List<ThreatData>();
 016  private Dictionary<Threat, ThreatData> _threatDataMap = new Dictionary<Threat, ThreatData>();
 17
 018  private List<Interceptor> _assignmentQueue = new List<Interceptor>();
 19
 020  private void Awake() {
 021    if (Instance == null) {
 022      Instance = this;
 023    } else {
 024      Destroy(gameObject);
 025    }
 026  }
 27
 028  private void Start() {
 029    SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded;
 030    SimManager.Instance.OnNewThreat += RegisterNewThreat;
 031    SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor;
 032    _assignmentScheme = new ThreatAssignment();
 033  }
 34
 035  public void LateUpdate() {
 036    if (_assignmentQueue.Count > 0) {
 037      int popCount = 100;
 38
 39      // Take up to popCount interceptors from the queue
 040      List<Interceptor> interceptorsToAssign = _assignmentQueue.Take(popCount).ToList();
 041      AssignInterceptorsToThreats(interceptorsToAssign);
 42
 43      // Remove the processed interceptors from the queue
 044      _assignmentQueue.RemoveRange(0, Math.Min(popCount, _assignmentQueue.Count));
 45
 46      // Check if any interceptors were not assigned
 047      List<Interceptor> assignedInterceptors =
 048          interceptorsToAssign.Where(m => m.HasAssignedTarget()).ToList();
 49
 050      if (assignedInterceptors.Count < interceptorsToAssign.Count) {
 51        // Back into the queue they go!
 052        _assignmentQueue.AddRange(interceptorsToAssign.Except(assignedInterceptors));
 053        Debug.Log($"Backing interceptors into the queue. Failed to assign.");
 054      }
 055    }
 056  }
 57
 058  public void RequestThreatAssignment(List<Interceptor> interceptors) {
 059    _assignmentQueue.AddRange(interceptors);
 060  }
 61
 062  public void RequestThreatAssignment(Interceptor interceptor) {
 063    _assignmentQueue.Add(interceptor);
 064  }
 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>
 070  public void AssignInterceptorsToThreats(List<Interceptor> missilesToAssign) {
 71    // Perform the assignment
 072    IEnumerable<IAssignment.AssignmentItem> assignments =
 73        _assignmentScheme.Assign(missilesToAssign, _threatTable);
 74
 75    // Apply the assignments to the missiles
 076    foreach (var assignment in assignments) {
 077      assignment.Interceptor.AssignTarget(assignment.Threat);
 078      _threatDataMap[assignment.Threat].AssignInterceptor(assignment.Interceptor);
 79      // Debug.Log(
 80      //     $"Interceptor {assignment.Interceptor.name} assigned to threat
 81      //     {assignment.Threat.name}");
 082    }
 083  }
 84
 085  public void RegisterNewThreat(Threat threat) {
 086    ThreatData threatData = new ThreatData(threat, threat.gameObject.name);
 087    _threatTable.Add(threatData);
 088    _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.
 093    threat.OnThreatHit += RegisterThreatHit;
 094    threat.OnThreatMiss += RegisterThreatMiss;
 095  }
 96
 097  public void RegisterNewInterceptor(Interceptor interceptor) {
 98    // Placeholder
 099    interceptor.OnInterceptMiss += RegisterInterceptorMiss;
 0100    interceptor.OnInterceptHit += RegisterInterceptorHit;
 0101  }
 102
 0103  private void RegisterInterceptorHit(Interceptor interceptor, Threat threat) {
 0104    ThreatData threatData = _threatDataMap[threat];
 0105    if (threatData != null) {
 0106      threatData.RemoveInterceptor(interceptor);
 0107      MarkThreatDestroyed(threatData);
 0108    }
 0109  }
 110
 0111  private void RegisterInterceptorMiss(Interceptor interceptor, Threat threat) {
 112    // Remove the interceptor from the threat's assigned interceptors
 0113    _threatDataMap[threat].RemoveInterceptor(interceptor);
 0114  }
 115
 0116  private void MarkThreatDestroyed(ThreatData threatData) {
 0117    if (threatData != null) {
 0118      threatData.MarkDestroyed();
 0119    }
 0120  }
 121
 0122  private void RegisterThreatHit(Threat threat) {
 0123    ThreatData threatData = _threatDataMap[threat];
 0124    if (threatData != null) {
 0125      MarkThreatDestroyed(threatData);
 0126    }
 0127  }
 128
 0129  private void RegisterThreatMiss(Threat threat) {
 130    // The threat missed (meaning it hit the floor, etc)
 0131    ThreatData threatData = _threatDataMap[threat];
 0132    if (threatData != null) {
 0133      MarkThreatDestroyed(threatData);
 0134    }
 135    // threatData.RemoveInterceptor(null);
 0136  }
 137
 0138  private void RegisterSimulationEnded() {
 0139    _threatTable.Clear();
 0140    _threatDataMap.Clear();
 0141    _assignmentQueue.Clear();
 0142  }
 143}