| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics.Contracts; |
| | | 4 | | using System.Linq; |
| | | 5 | | using UnityEngine; |
| | | 6 | | |
| | | 7 | | // The threat assignment class assigns interceptors to the targets based |
| | | 8 | | // on the threat level of the targets. |
| | | 9 | | public class ThreatAssignment : IAssignment { |
| | | 10 | | // Assign a target to each interceptor that has not been assigned a target yet. |
| | | 11 | | [Pure] |
| | | 12 | | public IEnumerable<IAssignment.AssignmentItem> Assign(in IReadOnlyList<Interceptor> interceptors, |
| | 0 | 13 | | in IReadOnlyList<Threat> threats) { |
| | 0 | 14 | | List<IAssignment.AssignmentItem> assignments = new List<IAssignment.AssignmentItem>(); |
| | | 15 | | |
| | 0 | 16 | | List<Interceptor> assignableInterceptors = IAssignment.GetAssignableInterceptors(interceptors); |
| | 0 | 17 | | if (assignableInterceptors.Count == 0) { |
| | 0 | 18 | | Debug.LogWarning("No assignable interceptors found."); |
| | 0 | 19 | | return assignments; |
| | | 20 | | } |
| | | 21 | | |
| | 0 | 22 | | List<Threat> activeThreats = IAssignment.GetActiveThreats(threats); |
| | 0 | 23 | | if (activeThreats.Count == 0) { |
| | 0 | 24 | | Debug.LogWarning("No active threats found."); |
| | 0 | 25 | | return assignments; |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | Vector3 positionToDefend = Vector3.zero; |
| | 0 | 29 | | List<ThreatInfo> threatInfos = CalculateThreatLevels(activeThreats, positionToDefend); |
| | | 30 | | |
| | | 31 | | // Sort the threats first by whether an interceptor is assigned to them already and then by |
| | | 32 | | // their threat level in descending order. |
| | 0 | 33 | | threatInfos = threatInfos.OrderBy(threat => threat.Threat.AssignedInterceptors.Count) |
| | 0 | 34 | | .ThenByDescending(threat => threat.ThreatLevel) |
| | | 35 | | .ToList(); |
| | | 36 | | |
| | 0 | 37 | | var assignableInterceptorsEnumerator = assignableInterceptors.GetEnumerator(); |
| | 0 | 38 | | int threatIndex = 0; |
| | 0 | 39 | | while (assignableInterceptorsEnumerator.MoveNext()) { |
| | 0 | 40 | | assignments.Add(new IAssignment.AssignmentItem(assignableInterceptorsEnumerator.Current, |
| | | 41 | | threatInfos[threatIndex].Threat)); |
| | 0 | 42 | | threatIndex = (threatIndex + 1) % threatInfos.Count; |
| | 0 | 43 | | } |
| | 0 | 44 | | return assignments; |
| | 0 | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | private List<ThreatInfo> CalculateThreatLevels(List<Threat> threats, Vector3 defensePosition) { |
| | 0 | 48 | | List<ThreatInfo> threatInfos = new List<ThreatInfo>(); |
| | | 49 | | |
| | 0 | 50 | | foreach (var threat in threats) { |
| | 0 | 51 | | float distanceToMean = Vector3.Distance(threat.transform.position, defensePosition); |
| | 0 | 52 | | float velocityMagnitude = threat.GetVelocity().magnitude; |
| | | 53 | | |
| | | 54 | | // Calculate the threat level based on proximity and velocity. |
| | 0 | 55 | | float threatLevel = (1 / distanceToMean) * velocityMagnitude; |
| | | 56 | | |
| | 0 | 57 | | threatInfos.Add(new ThreatInfo(threat, threatLevel)); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | // Sort threats in descending order. |
| | 0 | 61 | | return threatInfos.OrderByDescending(threat => threat.ThreatLevel).ToList(); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | private class ThreatInfo { |
| | 0 | 65 | | public Threat Threat { get; } |
| | 0 | 66 | | public float ThreatLevel { get; } |
| | | 67 | | |
| | 0 | 68 | | public ThreatInfo(Threat threat, float threatLevel) { |
| | 0 | 69 | | Threat = threat; |
| | 0 | 70 | | ThreatLevel = threatLevel; |
| | 0 | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |