< Summary

Class:MassReleaseStrategyBase
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Release/MassReleaseStrategyBase.cs
Covered lines:0
Uncovered lines:46
Coverable lines:46
Total lines:86
Line coverage:0% (0 of 46)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:4
Method coverage:0% (0 of 4)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MassReleaseStrategyBase(...)0%2100%
Release(...)0%2401500%

File(s)

/github/workspace/Assets/Scripts/Release/MassReleaseStrategyBase.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using UnityEngine;
 4
 5// Base implementation of a mass release strategy.
 6//
 7// The mass release strategy checks whether all sub-interceptors should be released simultaneously
 8// to target the target hierarchical object. The released sub-interceptors are then assigned to the
 9// sub-hierarchical objects of the target hierarchical object.
 10public abstract class MassReleaseStrategyBase : ReleaseStrategyBase {
 11  private const float _epsilon = 1e-12f;
 12  private const float _fanOutAngleDegrees = 60f;
 13
 014  public IAssignment Assignment { get; init; }
 15
 016  public MassReleaseStrategyBase(IAgent agent, IAssignment assignment) : base(agent) {
 017    Assignment = assignment;
 018  }
 19
 020  protected override List<IAgent> Release(IEnumerable<IHierarchical> hierarchicals) {
 021    if (Agent is not CarrierBase carrier || carrier.NumSubInterceptorsRemaining <= 0) {
 022      return new List<IAgent>();
 23    }
 24
 025    Dictionary<IHierarchical, List<IHierarchical>> targetToHierarchicalMap =
 026        hierarchicals.Where(hierarchical => hierarchical.Target != null)
 027            .GroupBy(hierarchical => hierarchical.Target)
 028            .ToDictionary(group => group.Key, group => group.ToList());
 029    List<IHierarchical> targets = targetToHierarchicalMap.Keys.ToList();
 030    if (targets.Count == 0) {
 031      return new List<IAgent>();
 32    }
 033    LaunchPlan launchPlan = PlanRelease(targets);
 034    if (!launchPlan.ShouldLaunch) {
 035      return new List<IAgent>();
 36    }
 37
 38    // Release all sub-interceptors.
 039    Configs.SubAgentConfig subAgentConfig = carrier.AgentConfig.SubAgentConfig;
 040    Vector3 position = carrier.Position;
 041    Simulation.CartesianCoordinates positionCoordinates = Coordinates3.ToProto(position);
 42
 043    Vector3 velocity = carrier.Velocity;
 044    Vector3 perpendicularDirection = Vector3.Cross(velocity, Vector3.up);
 045    if (perpendicularDirection.sqrMagnitude < _epsilon) {
 046      perpendicularDirection = Vector3.Cross(velocity, Vector3.forward);
 047    }
 48
 049    var releasedAgents = new List<IAgent>();
 050    for (int i = 0; i < subAgentConfig.NumSubAgents; ++i) {
 51      // Fan the submunitions radially outwards from the carrier's velocity vector.
 052      Vector3 lateralDirection =
 53          Quaternion.AngleAxis(i * 360 / subAgentConfig.NumSubAgents, velocity) *
 54          perpendicularDirection;
 055      Simulation.CartesianCoordinates velocityCoordinates =
 56          Coordinates3.ToProto(Vector3.RotateTowards(
 57              velocity, lateralDirection, maxRadiansDelta: _fanOutAngleDegrees * Mathf.Deg2Rad,
 58              maxMagnitudeDelta: Mathf.Cos(_fanOutAngleDegrees * Mathf.Deg2Rad)));
 059      Simulation.State initialState = new Simulation.State() {
 60        Position = positionCoordinates,
 61        Velocity = velocityCoordinates,
 62      };
 063      IAgent subInterceptor =
 64          SimManager.Instance.CreateInterceptor(subAgentConfig.AgentConfig, initialState);
 065      if (subInterceptor is IInterceptor) {
 066        releasedAgents.Add(subInterceptor);
 067      }
 068    }
 69
 70    // Assign the released sub-interceptors to the targets.
 071    var releasedAgentHierarchicals =
 072        releasedAgents.Select(agent => agent.HierarchicalAgent).ToList();
 073    List<AssignmentItem> assignments = Assignment.Assign(releasedAgentHierarchicals, targets);
 074    foreach (var assignment in assignments) {
 075      assignment.First.Target = assignment.Second;
 076      foreach (var hierarchical in targetToHierarchicalMap[assignment.Second]) {
 077        hierarchical.AddLaunchedHierarchical(assignment.First);
 078      }
 079    }
 80
 081    return releasedAgents;
 082  }
 83
 84  // Plan the release for the given targets.
 85  protected abstract LaunchPlan PlanRelease(IEnumerable<IHierarchical> targets);
 86}