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