| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using UnityEngine; |
| | | 5 | | |
| | | 6 | | // The Integrated Air Defense System (IADS) manages the air defense strategy. |
| | | 7 | | // It implements the singleton pattern to ensure that only one instance exists. |
| | | 8 | | public class IADS : MonoBehaviour { |
| | | 9 | | // Hierarchy parameters. |
| | | 10 | | private const float _hierarchyUpdatePeriod = 5f; |
| | | 11 | | private const float _coverageFactor = 1f; |
| | | 12 | | |
| | | 13 | | // The IADS only manages the launchers in the top level of the interceptor hierarchy. |
| | 1 | 14 | | private List<IHierarchical> _launchers = new List<IHierarchical>(); |
| | | 15 | | |
| | | 16 | | // Coroutine to perform the maintain the agent hierarchy. |
| | | 17 | | private Coroutine _hierarchyCoroutine; |
| | | 18 | | |
| | | 19 | | // List of threats waiting to be incorporated into the hierarchy. |
| | 1 | 20 | | private List<IHierarchical> _newThreats = new List<IHierarchical>(); |
| | | 21 | | |
| | 957 | 22 | | public static IADS Instance { get; private set; } |
| | | 23 | | |
| | 1087 | 24 | | public IReadOnlyList<IHierarchical> Launchers => _launchers.AsReadOnly(); |
| | | 25 | | |
| | 1 | 26 | | private void Awake() { |
| | 1 | 27 | | if (Instance != null && Instance != this) { |
| | 0 | 28 | | Destroy(gameObject); |
| | 1 | 29 | | } else { |
| | 1 | 30 | | Instance = this; |
| | 1 | 31 | | } |
| | 1 | 32 | | } |
| | | 33 | | |
| | 1 | 34 | | private void Start() { |
| | 1 | 35 | | SimManager.Instance.OnSimulationStarted += RegisterSimulationStarted; |
| | 1 | 36 | | SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded; |
| | 1 | 37 | | SimManager.Instance.OnNewLauncher += RegisterNewLauncher; |
| | 1 | 38 | | SimManager.Instance.OnNewThreat += RegisterNewThreat; |
| | 1 | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | private void OnDestroy() { |
| | 0 | 42 | | if (_hierarchyCoroutine != null) { |
| | 0 | 43 | | StopCoroutine(_hierarchyCoroutine); |
| | 0 | 44 | | _hierarchyCoroutine = null; |
| | 0 | 45 | | } |
| | 0 | 46 | | } |
| | | 47 | | |
| | 12 | 48 | | private void RegisterSimulationStarted() { |
| | 12 | 49 | | _hierarchyCoroutine = StartCoroutine(HierarchyManager(_hierarchyUpdatePeriod)); |
| | 12 | 50 | | } |
| | | 51 | | |
| | 11 | 52 | | private void RegisterSimulationEnded() { |
| | 22 | 53 | | if (_hierarchyCoroutine != null) { |
| | 11 | 54 | | StopCoroutine(_hierarchyCoroutine); |
| | 11 | 55 | | _hierarchyCoroutine = null; |
| | 11 | 56 | | } |
| | 11 | 57 | | _launchers.Clear(); |
| | 11 | 58 | | _newThreats.Clear(); |
| | 11 | 59 | | } |
| | | 60 | | |
| | 14 | 61 | | public void RegisterNewLauncher(IInterceptor interceptor) { |
| | 28 | 62 | | if (interceptor.HierarchicalAgent != null) { |
| | 14 | 63 | | interceptor.OnAssignSubInterceptor += AssignSubInterceptor; |
| | 14 | 64 | | interceptor.OnReassignTarget += ReassignTarget; |
| | 14 | 65 | | _launchers.Add(interceptor.HierarchicalAgent); |
| | 14 | 66 | | } |
| | 14 | 67 | | } |
| | | 68 | | |
| | 955 | 69 | | public void RegisterNewThreat(IThreat threat) { |
| | 1910 | 70 | | if (threat.HierarchicalAgent != null) { |
| | 955 | 71 | | _newThreats.Add(threat.HierarchicalAgent); |
| | 955 | 72 | | } |
| | 955 | 73 | | } |
| | | 74 | | |
| | 12 | 75 | | private IEnumerator HierarchyManager(float period) { |
| | 24 | 76 | | while (true) { |
| | 12 | 77 | | if (_newThreats.Count != 0) { |
| | 0 | 78 | | BuildHierarchy(); |
| | 0 | 79 | | } |
| | 12 | 80 | | yield return new WaitForSeconds(period); |
| | 0 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | private void BuildHierarchy() { |
| | | 85 | | // TODO(titan): The clustering algorithm should be aware of the capacity of the launcher. |
| | 0 | 86 | | var swarmClusterer = new KMeansClusterer(Mathf.RoundToInt(_launchers.Count / _coverageFactor)); |
| | 0 | 87 | | List<Cluster> swarms = swarmClusterer.Cluster(_newThreats); |
| | 0 | 88 | | _newThreats.Clear(); |
| | | 89 | | |
| | | 90 | | // Assign one swarm to each launcher. |
| | 0 | 91 | | var swarmToLauncherAssignment = |
| | | 92 | | new MinDistanceAssignment(Assignment.Assignment_EvenAssignment_Assign); |
| | 0 | 93 | | List<AssignmentItem> swarmToLauncherAssignments = |
| | | 94 | | swarmToLauncherAssignment.Assign(_launchers, swarms); |
| | 0 | 95 | | void AssignTarget(IHierarchical hierarchical, IHierarchical target) { |
| | 0 | 96 | | hierarchical.Target = target; |
| | 0 | 97 | | foreach (var subHierarchical in hierarchical.ActiveSubHierarchicals) { |
| | 0 | 98 | | AssignTarget(subHierarchical, target); |
| | 0 | 99 | | } |
| | 0 | 100 | | } |
| | 0 | 101 | | foreach (var assignment in swarmToLauncherAssignments) { |
| | | 102 | | // Assign the swarm as the target to the launcher. |
| | 0 | 103 | | assignment.First.Target = assignment.Second; |
| | | 104 | | // Assign the launcher as the target to all threats within the swarm. |
| | | 105 | | // TODO(titan): The threats would normally target the aircraft carrier within the strike |
| | | 106 | | // group. |
| | 0 | 107 | | AssignTarget(assignment.Second, assignment.First); |
| | 0 | 108 | | } |
| | 0 | 109 | | } |
| | | 110 | | |
| | 132 | 111 | | private void AssignSubInterceptor(IInterceptor subInterceptor) { |
| | 132 | 112 | | if (subInterceptor.CapacityRemaining <= 0) { |
| | 0 | 113 | | return; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | // Pass the sub-interceptor through all the launchers in order of increasing distance between |
| | | 117 | | // the sub-interceptor and the launcher's target. |
| | 132 | 118 | | var sortedLaunchers = |
| | 188 | 119 | | Launchers.Where(launcher => launcher.Target != null && !launcher.Target.IsTerminated) |
| | | 120 | | .OrderBy(launcher => |
| | 0 | 121 | | Vector3.Distance(subInterceptor.Position, launcher.Target.Position)); |
| | 396 | 122 | | foreach (var launcher in sortedLaunchers) { |
| | 0 | 123 | | if (launcher.AssignNewTarget(subInterceptor.HierarchicalAgent, |
| | 0 | 124 | | subInterceptor.CapacityRemaining)) { |
| | 0 | 125 | | break; |
| | | 126 | | } |
| | 0 | 127 | | } |
| | 132 | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | private void ReassignTarget(IHierarchical target) { |
| | | 131 | | // Assign the closest launcher with non-zero remaining capacity to pursue the target. |
| | 0 | 132 | | var closestLauncher = |
| | | 133 | | Launchers |
| | 0 | 134 | | .Select(launcher => new { |
| | | 135 | | Hierarchical = launcher, |
| | | 136 | | Interceptor = (launcher as HierarchicalAgent)?.Agent as IInterceptor, |
| | | 137 | | }) |
| | 0 | 138 | | .Where(launcher => launcher.Interceptor?.CapacityPlannedRemaining > 0) |
| | 0 | 139 | | .OrderBy(launcher => Vector3.Distance(target.Position, launcher.Hierarchical.Position)) |
| | | 140 | | .FirstOrDefault(); |
| | 0 | 141 | | if (closestLauncher == null) { |
| | 0 | 142 | | return; |
| | | 143 | | } |
| | 0 | 144 | | closestLauncher.Interceptor.ReassignTarget(target); |
| | 0 | 145 | | } |
| | | 146 | | } |