| | | 1 | | using UnityEngine; |
| | | 2 | | |
| | | 3 | | // The maximum speed assignment assigns hierarchical agents to hierarchical agents by maximizing the |
| | | 4 | | // overall intercept speed. The assignment cost is defined as the agent's fractional speed loss |
| | | 5 | | // during the maneuver. |
| | | 6 | | public class MaxSpeedAssignment : CostBasedAssignment { |
| | | 7 | | // Minimum fractional speed to prevent division by zero. |
| | | 8 | | private const float _minFractionalSpeed = 1e-6f; |
| | | 9 | | |
| | | 10 | | public MaxSpeedAssignment(AssignDelegate assignFunction) |
| | 0 | 11 | | : base(CalculateSpeedLoss, assignFunction) {} |
| | | 12 | | |
| | 0 | 13 | | private static float CalculateSpeedLoss(IHierarchical hierarchical, IHierarchical target) { |
| | 0 | 14 | | if (hierarchical is not HierarchicalAgent hierarchicalAgent) { |
| | 0 | 15 | | return 0; |
| | | 16 | | } |
| | | 17 | | |
| | 0 | 18 | | IAgent agent = hierarchicalAgent.Agent; |
| | 0 | 19 | | float fractionalSpeed = FractionalSpeed.Calculate(agent, target.Position); |
| | 0 | 20 | | return agent.Speed / Mathf.Max(fractionalSpeed, _minFractionalSpeed); |
| | 0 | 21 | | } |
| | | 22 | | } |