| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Diagnostics.Contracts; |
| | | 3 | | using System.Linq; |
| | | 4 | | using UnityEngine; |
| | | 5 | | |
| | | 6 | | // The round-robin assignment class assigns interceptors to the targets in a |
| | | 7 | | // round-robin order using the new paradigm. |
| | | 8 | | public class RoundRobinAssignment : IAssignment { |
| | | 9 | | // Previous target index that was assigned. |
| | 0 | 10 | | private int prevTargetIndex = -1; |
| | | 11 | | |
| | | 12 | | // Assign a target to each interceptor that has not been assigned a target yet. |
| | | 13 | | [Pure] |
| | | 14 | | public IEnumerable<IAssignment.AssignmentItem> Assign(in IReadOnlyList<Interceptor> interceptors, |
| | 0 | 15 | | in IReadOnlyList<Threat> threats) { |
| | 0 | 16 | | List<IAssignment.AssignmentItem> assignments = new List<IAssignment.AssignmentItem>(); |
| | | 17 | | |
| | | 18 | | // Get the list of interceptors that are available for assignment. |
| | 0 | 19 | | List<Interceptor> assignableInterceptors = IAssignment.GetAssignableInterceptors(interceptors); |
| | 0 | 20 | | if (assignableInterceptors.Count == 0) { |
| | 0 | 21 | | return assignments; |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | // Get the list of active threats that need to be addressed. |
| | 0 | 25 | | List<Threat> activeThreats = IAssignment.GetActiveThreats(threats); |
| | 0 | 26 | | if (activeThreats.Count == 0) { |
| | 0 | 27 | | Debug.LogWarning("No active threats found."); |
| | 0 | 28 | | return assignments; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | // Perform round-robin assignment. |
| | 0 | 32 | | foreach (Interceptor interceptor in assignableInterceptors) { |
| | | 33 | | // Determine the next target index in a round-robin fashion. |
| | 0 | 34 | | int nextTargetIndex = (prevTargetIndex + 1) % activeThreats.Count; |
| | 0 | 35 | | Threat threat = activeThreats[nextTargetIndex]; |
| | | 36 | | |
| | | 37 | | // Assign the interceptor to the selected threat. |
| | 0 | 38 | | assignments.Add(new IAssignment.AssignmentItem(interceptor, threat)); |
| | | 39 | | |
| | | 40 | | // Update the previous target index. |
| | 0 | 41 | | prevTargetIndex = nextTargetIndex; |
| | 0 | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | return assignments; |
| | 0 | 45 | | } |
| | | 46 | | } |