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