| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using UnityEngine; |
| | | 5 | | |
| | | 6 | | // Base implementation of an interceptor. |
| | | 7 | | public abstract class InterceptorBase : AgentBase, IInterceptor { |
| | | 8 | | public event InterceptHitMissEventHandler OnHit; |
| | | 9 | | public event InterceptHitMissEventHandler OnMiss; |
| | | 10 | | public event InterceptorAssignEventHandler OnAssignSubInterceptor; |
| | | 11 | | public event TargetReassignEventHandler OnReassignTarget; |
| | | 12 | | |
| | | 13 | | // Default proportional navigation controller gain. |
| | | 14 | | private const float _proportionalNavigationGain = 5f; |
| | | 15 | | |
| | | 16 | | // Time to accumulate unassigned targets before launching additional sub-interceptors. |
| | | 17 | | private const float _unassignedTargetsLaunchPeriod = 2.5f; |
| | | 18 | | |
| | 146 | 19 | | public IEscapeDetector EscapeDetector { get; set; } |
| | | 20 | | |
| | | 21 | | // Maximum number of threats that this interceptor can target. |
| | | 22 | | [SerializeField] |
| | | 23 | | private int _capacity; |
| | | 24 | | |
| | | 25 | | // Capacity of each sub-interceptor. |
| | | 26 | | [SerializeField] |
| | | 27 | | private int _capacityPerSubInterceptor; |
| | | 28 | | |
| | | 29 | | // Number of sub-interceptors. |
| | | 30 | | [SerializeField] |
| | | 31 | | private int _numSubInterceptors; |
| | | 32 | | |
| | | 33 | | // Number of sub-interceptors remaining that can be planned to launch. |
| | | 34 | | [SerializeField] |
| | | 35 | | private int _numSubInterceptorsPlannedRemaining; |
| | | 36 | | |
| | | 37 | | // Number of sub-interceptors remaining. |
| | | 38 | | [SerializeField] |
| | | 39 | | private int _numSubInterceptorsRemaining; |
| | | 40 | | |
| | | 41 | | public int Capacity { |
| | 0 | 42 | | get => _capacity; |
| | | 43 | | protected |
| | 42 | 44 | | set { _capacity = value; } |
| | | 45 | | } |
| | | 46 | | public int CapacityPerSubInterceptor { |
| | 132 | 47 | | get => _capacityPerSubInterceptor; |
| | | 48 | | protected |
| | 42 | 49 | | set { _capacityPerSubInterceptor = value; } |
| | | 50 | | } |
| | 0 | 51 | | public virtual int CapacityPlannedRemaining => CapacityPerSubInterceptor * |
| | | 52 | | NumSubInterceptorsPlannedRemaining; |
| | 132 | 53 | | public virtual int CapacityRemaining => CapacityPerSubInterceptor * NumSubInterceptorsRemaining; |
| | | 54 | | public int NumSubInterceptors { |
| | 160 | 55 | | get => _numSubInterceptors; |
| | | 56 | | protected |
| | 42 | 57 | | set { _numSubInterceptors = value; } |
| | | 58 | | } |
| | | 59 | | public int NumSubInterceptorsPlannedRemaining { |
| | 0 | 60 | | get => _numSubInterceptorsPlannedRemaining; |
| | | 61 | | protected |
| | 438 | 62 | | set { _numSubInterceptorsPlannedRemaining = value; } |
| | | 63 | | } |
| | | 64 | | public int NumSubInterceptorsRemaining { |
| | 306 | 65 | | get => _numSubInterceptorsRemaining; |
| | | 66 | | protected |
| | 84 | 67 | | set { _numSubInterceptorsRemaining = value; } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // Set of unassigned targets for which an additional sub-interceptor should be launched. |
| | 16 | 71 | | private HashSet<IHierarchical> _unassignedTargets = new HashSet<IHierarchical>(); |
| | | 72 | | |
| | | 73 | | // Coroutine for handling unassigned targets. |
| | | 74 | | private Coroutine _unassignedTargetsCoroutine; |
| | | 75 | | |
| | 0 | 76 | | public void AssignSubInterceptor(IInterceptor subInterceptor) { |
| | 0 | 77 | | if (subInterceptor.CapacityRemaining <= 0) { |
| | 0 | 78 | | return; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | // Assign a new target to the sub-interceptor within the parent interceptor's assigned targets. |
| | 0 | 82 | | if (!HierarchicalAgent.AssignNewTarget(subInterceptor.HierarchicalAgent, |
| | 0 | 83 | | subInterceptor.CapacityRemaining)) { |
| | | 84 | | // Propagate the sub-interceptor target assignment to the parent interceptor above. |
| | 0 | 85 | | OnAssignSubInterceptor?.Invoke(subInterceptor); |
| | 0 | 86 | | } |
| | 0 | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | public void ReassignTarget(IHierarchical target) { |
| | | 90 | | // If a target needs to be re-assigned, the interceptor should in the following order: |
| | | 91 | | // 1. Queue up the unassigned targets in preparation of launching an additional |
| | | 92 | | // sub-interceptor. |
| | | 93 | | // 2. If no existing sub-interceptor has been assigned to pursue the queued target(s), launch |
| | | 94 | | // another sub-interceptor(s) to pursue the target(s). |
| | | 95 | | // 3. Propagate the target re-assignment to the parent interceptor above. |
| | 0 | 96 | | if (CapacityPlannedRemaining <= 0) { |
| | 0 | 97 | | OnReassignTarget?.Invoke(target); |
| | 0 | 98 | | return; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | _unassignedTargets.Add(target); |
| | 0 | 102 | | } |
| | | 103 | | |
| | 14 | 104 | | protected override void Start() { |
| | 14 | 105 | | base.Start(); |
| | 14 | 106 | | _unassignedTargetsCoroutine = |
| | | 107 | | StartCoroutine(UnassignedTargetsManager(_unassignedTargetsLaunchPeriod)); |
| | 14 | 108 | | OnMiss += RegisterMiss; |
| | 14 | 109 | | } |
| | | 110 | | |
| | 132 | 111 | | protected override void FixedUpdate() { |
| | 132 | 112 | | base.FixedUpdate(); |
| | | 113 | | |
| | | 114 | | // Check whether the interceptor has a target. If not, request a new target from the parent |
| | | 115 | | // interceptor. |
| | 264 | 116 | | if (HierarchicalAgent.Target == null || HierarchicalAgent.Target.IsTerminated) { |
| | 132 | 117 | | OnAssignSubInterceptor?.Invoke(this); |
| | 132 | 118 | | } |
| | | 119 | | |
| | | 120 | | // Check whether any targets are escaping from the interceptor. |
| | 132 | 121 | | if (EscapeDetector != null && HierarchicalAgent.Target != null && |
| | 0 | 122 | | !HierarchicalAgent.Target.IsTerminated) { |
| | 0 | 123 | | List<IHierarchical> targetHierarchicals = |
| | | 124 | | HierarchicalAgent.Target.LeafHierarchicals(activeOnly: true, withTargetOnly: false); |
| | 0 | 125 | | List<IHierarchical> escapingTargets = |
| | | 126 | | targetHierarchicals.Where(EscapeDetector.IsEscaping).ToList(); |
| | 0 | 127 | | foreach (var target in escapingTargets) { |
| | 0 | 128 | | OnReassignTarget?.Invoke(target); |
| | 0 | 129 | | } |
| | 0 | 130 | | if (escapingTargets.Count == targetHierarchicals.Count) { |
| | 0 | 131 | | OnAssignSubInterceptor?.Invoke(this); |
| | 0 | 132 | | } |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | // Update the planned number of sub-interceptors remaining. |
| | | 136 | | // TODO(titan): Update the planned number of sub-interceptors remaining when the number of leaf |
| | | 137 | | // hierarchical objects changes, such as when a new target is added. |
| | 132 | 138 | | List<IHierarchical> leafHierarchicals = |
| | | 139 | | HierarchicalAgent.LeafHierarchicals(activeOnly: false, withTargetOnly: false); |
| | 132 | 140 | | NumSubInterceptorsPlannedRemaining = |
| | | 141 | | Mathf.Min(NumSubInterceptorsRemaining, NumSubInterceptors - leafHierarchicals.Count); |
| | | 142 | | |
| | | 143 | | // Navigate towards the target. |
| | 132 | 144 | | AccelerationInput = Controller?.Plan() ?? Vector3.zero; |
| | 132 | 145 | | Acceleration = Movement?.Act(AccelerationInput) ?? Vector3.zero; |
| | 132 | 146 | | _rigidbody.AddForce(Acceleration, ForceMode.Acceleration); |
| | 132 | 147 | | } |
| | | 148 | | |
| | 13 | 149 | | protected override void OnDestroy() { |
| | 13 | 150 | | base.OnDestroy(); |
| | | 151 | | |
| | 26 | 152 | | if (_unassignedTargetsCoroutine != null) { |
| | 13 | 153 | | StopCoroutine(_unassignedTargetsCoroutine); |
| | 13 | 154 | | _unassignedTargetsCoroutine = null; |
| | 13 | 155 | | } |
| | 13 | 156 | | } |
| | | 157 | | |
| | 14 | 158 | | protected override void UpdateAgentConfig() { |
| | 14 | 159 | | base.UpdateAgentConfig(); |
| | | 160 | | |
| | | 161 | | // Calculate the capacity. |
| | 70 | 162 | | int NumAgents(Configs.AgentConfig config) { |
| | 98 | 163 | | if (config == null || config.SubAgentConfig == null) { |
| | 28 | 164 | | return 1; |
| | | 165 | | } |
| | 42 | 166 | | return (int)config.SubAgentConfig.NumSubAgents * NumAgents(config.SubAgentConfig.AgentConfig); |
| | 70 | 167 | | } |
| | 14 | 168 | | Capacity = NumAgents(AgentConfig); |
| | 14 | 169 | | CapacityPerSubInterceptor = NumAgents(AgentConfig.SubAgentConfig?.AgentConfig); |
| | 14 | 170 | | NumSubInterceptors = (int)(AgentConfig.SubAgentConfig?.NumSubAgents ?? 0); |
| | 14 | 171 | | NumSubInterceptorsPlannedRemaining = NumSubInterceptors; |
| | 14 | 172 | | NumSubInterceptorsRemaining = NumSubInterceptors; |
| | | 173 | | |
| | | 174 | | // Set the controller. |
| | 14 | 175 | | switch (AgentConfig.DynamicConfig?.FlightConfig?.ControllerType) { |
| | 0 | 176 | | case Configs.ControllerType.ProportionalNavigation: { |
| | 0 | 177 | | Controller = new PnController(this, _proportionalNavigationGain); |
| | 0 | 178 | | break; |
| | | 179 | | } |
| | 0 | 180 | | case Configs.ControllerType.AugmentedProportionalNavigation: { |
| | 0 | 181 | | Controller = new ApnController(this, _proportionalNavigationGain); |
| | 0 | 182 | | break; |
| | | 183 | | } |
| | 14 | 184 | | default: { |
| | 14 | 185 | | Debug.LogWarning( |
| | | 186 | | $"Controller type {AgentConfig.DynamicConfig?.FlightConfig?.ControllerType} not found."); |
| | 14 | 187 | | Controller = null; |
| | 14 | 188 | | break; |
| | | 189 | | } |
| | | 190 | | } |
| | 14 | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | protected override void OnDrawGizmos() { |
| | | 194 | | const float axisLength = 10f; |
| | | 195 | | |
| | 0 | 196 | | base.OnDrawGizmos(); |
| | | 197 | | |
| | 0 | 198 | | if (Application.isPlaying) { |
| | | 199 | | // Target. |
| | 0 | 200 | | if (HierarchicalAgent.Target != null && !HierarchicalAgent.Target.IsTerminated) { |
| | 0 | 201 | | Gizmos.color = new Color(1, 1, 1, 0.15f); |
| | 0 | 202 | | Gizmos.DrawLine(Position, HierarchicalAgent.Target.Position); |
| | 0 | 203 | | } |
| | | 204 | | |
| | | 205 | | // Up direction. |
| | 0 | 206 | | Gizmos.color = Color.yellow; |
| | 0 | 207 | | Gizmos.DrawRay(Position, Up * axisLength); |
| | | 208 | | |
| | | 209 | | // Forward direction. |
| | 0 | 210 | | Gizmos.color = Color.blue; |
| | 0 | 211 | | Gizmos.DrawRay(Position, Forward * axisLength); |
| | | 212 | | |
| | | 213 | | // Right direction. |
| | 0 | 214 | | Gizmos.color = Color.red; |
| | 0 | 215 | | Gizmos.DrawRay(Position, Right * axisLength); |
| | 0 | 216 | | } |
| | 0 | 217 | | } |
| | | 218 | | |
| | | 219 | | // If the interceptor collides with the ground or another agent, it will be terminated. It is |
| | | 220 | | // possible for an interceptor to collide with another interceptor or with a non-target threat. |
| | | 221 | | // The interceptor records a hit only if it collides with a threat and destroys it with the |
| | | 222 | | // threat's kill probability. |
| | 994 | 223 | | private void OnTriggerEnter(Collider other) { |
| | 994 | 224 | | if (CheckFloorCollision(other)) { |
| | 0 | 225 | | OnMiss?.Invoke(this); |
| | 0 | 226 | | Terminate(); |
| | 0 | 227 | | } |
| | | 228 | | |
| | 994 | 229 | | IAgent otherAgent = other.gameObject.GetComponentInParent<IAgent>(); |
| | 1988 | 230 | | if (ShouldIgnoreCollision(otherAgent)) { |
| | 994 | 231 | | return; |
| | | 232 | | } |
| | | 233 | | // Check if the collision is with a threat. |
| | 0 | 234 | | if (otherAgent is IThreat threat) { |
| | | 235 | | // Check the kill probability. |
| | 0 | 236 | | float killProbability = threat.StaticConfig.HitConfig?.KillProbability ?? 1; |
| | 0 | 237 | | bool isHit = Random.value <= killProbability; |
| | 0 | 238 | | if (isHit) { |
| | 0 | 239 | | threat.HandleIntercept(); |
| | 0 | 240 | | OnHit?.Invoke(this); |
| | 0 | 241 | | Terminate(); |
| | 0 | 242 | | } else { |
| | 0 | 243 | | OnMiss?.Invoke(this); |
| | 0 | 244 | | } |
| | 0 | 245 | | } |
| | 994 | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | private void RegisterMiss(IInterceptor interceptor) { |
| | | 249 | | // Request the parent interceptor to re-assign the target to another interceptor if there are no |
| | | 250 | | // other pursuers. |
| | 0 | 251 | | IHierarchical target = interceptor.HierarchicalAgent.Target; |
| | 0 | 252 | | if (target == null || target.IsTerminated) { |
| | 0 | 253 | | return; |
| | | 254 | | } |
| | 0 | 255 | | List<IHierarchical> targetHierarchicals = |
| | | 256 | | target.LeafHierarchicals(activeOnly: true, withTargetOnly: false); |
| | 0 | 257 | | foreach (var targetHierarchical in targetHierarchicals) { |
| | 0 | 258 | | OnReassignTarget?.Invoke(targetHierarchical); |
| | 0 | 259 | | } |
| | | 260 | | |
| | | 261 | | // Request a new target from the parent interceptor. |
| | 0 | 262 | | OnAssignSubInterceptor?.Invoke(interceptor); |
| | 0 | 263 | | } |
| | | 264 | | |
| | 14 | 265 | | private IEnumerator UnassignedTargetsManager(float period) { |
| | 28 | 266 | | while (true) { |
| | 64 | 267 | | yield return new WaitUntil(() => _unassignedTargets.Count > 0); |
| | 0 | 268 | | yield return new WaitForSeconds(period); |
| | | 269 | | |
| | 0 | 270 | | IEnumerable<IHierarchical> unassignedTargets = _unassignedTargets.ToList(); |
| | 0 | 271 | | _unassignedTargets.Clear(); |
| | | 272 | | |
| | | 273 | | // Check whether the unassigned targets are still unassigned or are escaping the assigned |
| | | 274 | | // pursuers. |
| | 0 | 275 | | var filteredTargets = |
| | | 276 | | unassignedTargets |
| | 0 | 277 | | .Where(target => !target.IsTerminated && target.ActivePursuers.All(pursuer => { |
| | 0 | 278 | | var pursuerAgent = pursuer as HierarchicalAgent; |
| | 0 | 279 | | var interceptor = pursuerAgent?.Agent as IInterceptor; |
| | 0 | 280 | | return interceptor == null || interceptor.CapacityRemaining == 0 || |
| | | 281 | | (interceptor.EscapeDetector?.IsEscaping(target) ?? true); |
| | 0 | 282 | | })) |
| | | 283 | | .ToList(); |
| | 0 | 284 | | if (filteredTargets.Count > CapacityPlannedRemaining) { |
| | | 285 | | // If there are more unassigned targets than the capacity remaining, propagate the target |
| | | 286 | | // re-assignment to the parent interceptor for the excess targets. |
| | 0 | 287 | | var orderedTargets = |
| | 0 | 288 | | filteredTargets.OrderBy(target => Vector3.Distance(Position, target.Position)); |
| | 0 | 289 | | var excessTargets = orderedTargets.Skip(CapacityPlannedRemaining); |
| | 0 | 290 | | foreach (var target in excessTargets) { |
| | 0 | 291 | | OnReassignTarget?.Invoke(target); |
| | 0 | 292 | | } |
| | 0 | 293 | | unassignedTargets = orderedTargets.Take(CapacityPlannedRemaining); |
| | 0 | 294 | | } else { |
| | 0 | 295 | | unassignedTargets = filteredTargets; |
| | 0 | 296 | | } |
| | 0 | 297 | | if (!unassignedTargets.Any()) { |
| | 0 | 298 | | continue; |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | // Create a new hierarchical object with the cluster of unassigned targets as the target. |
| | 0 | 302 | | var newTargetSubHierarchical = new HierarchicalBase(); |
| | 0 | 303 | | int numUnassignedTargets = 0; |
| | 0 | 304 | | foreach (var target in unassignedTargets) { |
| | 0 | 305 | | newTargetSubHierarchical.AddSubHierarchical(target); |
| | 0 | 306 | | ++numUnassignedTargets; |
| | 0 | 307 | | } |
| | 0 | 308 | | var newSubHierarchical = new HierarchicalBase { Target = newTargetSubHierarchical }; |
| | 0 | 309 | | HierarchicalAgent.AddSubHierarchical(newSubHierarchical); |
| | 0 | 310 | | Debug.Log($"Reclustered {numUnassignedTargets} target(s) into a new cluster for {this}."); |
| | 0 | 311 | | UIManager.Instance.LogActionMessage( |
| | | 312 | | $"[IADS] Reclustered {numUnassignedTargets} target(s) into a new cluster for {this}."); |
| | | 313 | | |
| | | 314 | | // Recursively cluster the newly assigned targets. |
| | 0 | 315 | | newSubHierarchical.RecursiveCluster(maxClusterSize: CapacityPerSubInterceptor); |
| | 0 | 316 | | } |
| | | 317 | | } |
| | | 318 | | } |