| | 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, |
| 2 | 14 | | in IReadOnlyList<ThreatData> targets) { |
| 2 | 15 | | List<IAssignment.AssignmentItem> assignments = new List<IAssignment.AssignmentItem>(); |
| | 16 | |
|
| 2 | 17 | | List<Interceptor> assignableInterceptors = IAssignment.GetAssignableInterceptors(interceptors); |
| 2 | 18 | | if (assignableInterceptors.Count == 0) { |
| 0 | 19 | | Debug.LogWarning("No assignable interceptors found"); |
| 0 | 20 | | return assignments; |
| | 21 | | } |
| | 22 | |
|
| 2 | 23 | | List<ThreatData> activeThreats = IAssignment.GetActiveThreats(targets); |
| 2 | 24 | | if (activeThreats.Count == 0) { |
| 0 | 25 | | Debug.LogWarning("No active threats found"); |
| 0 | 26 | | return assignments; |
| | 27 | | } |
| | 28 | |
|
| 2 | 29 | | Vector3 positionToDefend = Vector3.zero; |
| 2 | 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 |
| 7 | 34 | | threatInfos = threatInfos.OrderBy(t => t.ThreatData.assignedInterceptorCount) |
| 5 | 35 | | .ThenByDescending(t => t.ThreatLevel) |
| | 36 | | .ToList(); |
| | 37 | |
|
| 2 | 38 | | var assignableInterceptorsEnumerator = assignableInterceptors.GetEnumerator(); |
| 2 | 39 | | if (assignableInterceptorsEnumerator.MoveNext()) // Move to the first element |
| 2 | 40 | | { |
| 20 | 41 | | foreach (ThreatInfo threatInfo in threatInfos) { |
| 5 | 42 | | assignments.Add(new IAssignment.AssignmentItem(assignableInterceptorsEnumerator.Current, |
| | 43 | | threatInfo.ThreatData.Threat)); |
| 6 | 44 | | if (!assignableInterceptorsEnumerator.MoveNext()) { |
| 1 | 45 | | break; |
| | 46 | | } |
| 4 | 47 | | } |
| 2 | 48 | | } |
| 2 | 49 | | return assignments; |
| 2 | 50 | | } |
| | 51 | |
|
| | 52 | | private List<ThreatInfo> CalculateThreatLevels(List<ThreatData> threatTable, |
| 2 | 53 | | Vector3 defensePosition) { |
| 2 | 54 | | List<ThreatInfo> threatInfos = new List<ThreatInfo>(); |
| | 55 | |
|
| 21 | 56 | | foreach (ThreatData threatData in threatTable) { |
| 5 | 57 | | Threat threat = threatData.Threat; |
| 5 | 58 | | float distanceToMean = Vector3.Distance(threat.transform.position, defensePosition); |
| 5 | 59 | | float velocityMagnitude = threat.GetVelocity().magnitude; |
| | 60 | |
|
| | 61 | | // Calculate threat level based on proximity and velocity |
| 5 | 62 | | float threatLevel = (1 / distanceToMean) * velocityMagnitude; |
| | 63 | |
|
| 5 | 64 | | threatInfos.Add(new ThreatInfo(threatData, threatLevel)); |
| 5 | 65 | | } |
| | 66 | |
|
| | 67 | | // Sort threats in descending order |
| 7 | 68 | | return threatInfos.OrderByDescending(t => t.ThreatLevel).ToList(); |
| 2 | 69 | | } |
| | 70 | |
|
| | 71 | | private class ThreatInfo { |
| 10 | 72 | | public ThreatData ThreatData { get; } |
| 10 | 73 | | public float ThreatLevel { get; } |
| | 74 | |
|
| 10 | 75 | | public ThreatInfo(ThreatData threatData, float threatLevel) { |
| 5 | 76 | | ThreatData = threatData; |
| 5 | 77 | | ThreatLevel = threatLevel; |
| 5 | 78 | | } |
| | 79 | | } |
| | 80 | | } |