micromissiles-unity/Assets/Scripts/SimManager.cs

359 lines
12 KiB
C#
Raw Normal View History

2024-09-12 00:17:21 -07:00
using System.Collections;
using System.Collections.Generic;
2024-09-12 15:44:55 -07:00
using System.Linq;
2024-09-12 00:17:21 -07:00
using UnityEngine;
/// <summary>
/// Manages the simulation by handling missiles, targets, and their assignments.
/// Implements the Singleton pattern to ensure only one instance exists.
/// </summary>
2024-09-13 22:45:25 -07:00
public class SimManager : MonoBehaviour {
/// <summary>
/// Singleton instance of SimManager.
/// </summary>
2024-09-13 22:45:25 -07:00
public static SimManager Instance { get; private set; }
/// <summary>
/// Configuration settings for the simulation.
/// </summary>
2024-09-13 22:45:25 -07:00
[SerializeField]
public SimulationConfig simulationConfig;
2024-09-24 19:59:25 -07:00
private List<Interceptor> _activeInterceptors = new List<Interceptor>();
[SerializeField]
private List<ThreatData> _threatTable = new List<ThreatData>();
private Dictionary<Threat, ThreatData> _threatDataMap = new Dictionary<Threat, ThreatData>();
private List<Interceptor> _interceptorObjects = new List<Interceptor>();
private List<Threat> _threatObjects = new List<Threat>();
2024-09-13 22:45:25 -07:00
private float _elapsedSimulationTime = 0f;
private float endTime = 100f; // Set an appropriate end time
private bool simulationRunning = false;
private IAssignment _assignmentScheme;
public delegate void SimulationEventHandler();
public event SimulationEventHandler OnSimulationEnded;
public event SimulationEventHandler OnSimulationStarted;
/// <summary>
/// Gets the elapsed simulation time.
/// </summary>
/// <returns>The elapsed time in seconds.</returns>
2024-09-13 22:45:25 -07:00
public double GetElapsedSimulationTime() {
return _elapsedSimulationTime;
}
2024-09-24 19:59:25 -07:00
public List<Interceptor> GetActiveInterceptors() {
return _activeInterceptors;
}
2024-09-24 19:24:50 -07:00
public List<Threat> GetActiveThreats() {
return _threatTable.Where(threat => threat.Status != ThreatStatus.DESTROYED).Select(threat => threat.Threat).ToList();
}
public List<Agent> GetActiveAgents() {
2024-09-24 19:59:25 -07:00
return _activeInterceptors.ConvertAll(interceptor => interceptor as Agent)
.Concat(GetActiveThreats().ConvertAll(threat => threat as Agent))
.ToList();
}
2024-09-13 22:45:25 -07:00
void Awake() {
// Ensure only one instance of SimManager exists
if (Instance == null) {
Instance = this;
DontDestroyOnLoad(gameObject);
} else {
Destroy(gameObject);
}
simulationConfig = ConfigLoader.LoadSimulationConfig("1_salvo_1_hydra_7_drones.json");
Debug.Log(simulationConfig);
2024-09-13 22:45:25 -07:00
}
void Start() {
// Slow down time by simulationConfig.timeScale
if (Instance == this) {
StartSimulation();
2024-09-25 11:31:51 -07:00
ResumeSimulation();
2024-09-13 22:45:25 -07:00
}
2024-09-25 11:31:51 -07:00
2024-09-13 22:45:25 -07:00
}
public void SetTimeScale(float timeScale) {
Time.timeScale = timeScale;
Time.fixedDeltaTime = Time.timeScale * 0.02f;
Time.maximumDeltaTime = Time.timeScale * 0.15f;
}
public void StartSimulation() {
InitializeSimulation();
OnSimulationStarted?.Invoke();
}
public void PauseSimulation() {
SetTimeScale(0);
simulationRunning = false;
}
public void ResumeSimulation() {
SetTimeScale(simulationConfig.timeScale);
simulationRunning = true;
}
public bool IsSimulationRunning() {
return simulationRunning;
}
2024-09-13 22:45:25 -07:00
private void InitializeSimulation() {
2024-09-24 19:59:25 -07:00
List<Interceptor> missiles = new List<Interceptor>();
2024-09-13 22:45:25 -07:00
// Create missiles based on config
2024-09-24 19:59:25 -07:00
foreach (var swarmConfig in simulationConfig.interceptor_swarm_configs) {
2024-09-13 22:45:25 -07:00
for (int i = 0; i < swarmConfig.num_agents; i++) {
2024-09-24 19:59:25 -07:00
var interceptor = CreateInterceptor(swarmConfig.agent_config);
2024-09-13 22:45:25 -07:00
}
2024-09-12 15:44:55 -07:00
}
2024-09-12 00:17:21 -07:00
2024-09-24 19:24:50 -07:00
List<Threat> targets = new List<Threat>();
2024-09-13 22:45:25 -07:00
// Create targets based on config
2024-09-24 19:59:25 -07:00
foreach (var swarmConfig in simulationConfig.threat_swarm_configs) {
2024-09-13 22:45:25 -07:00
for (int i = 0; i < swarmConfig.num_agents; i++) {
2024-09-24 19:24:50 -07:00
var threat = CreateThreat(swarmConfig.agent_config);
2024-09-13 22:45:25 -07:00
}
2024-09-12 15:44:55 -07:00
}
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
_assignmentScheme = new ThreatAssignment();
2024-09-24 19:24:50 -07:00
// Invoke the simulation started event to let listeners
// know to invoke their own handler behavior
OnSimulationStarted?.Invoke();
2024-09-13 22:45:25 -07:00
}
2024-09-24 19:59:25 -07:00
public void AssignInterceptorsToThreats() {
AssignInterceptorsToThreats(_interceptorObjects);
2024-09-13 22:45:25 -07:00
}
public void RegisterInterceptorHit(Interceptor interceptor, Threat threat) {
2024-09-24 19:59:25 -07:00
if (interceptor is Interceptor missileComponent) {
_activeInterceptors.Remove(missileComponent);
}
}
public void RegisterInterceptorMiss(Interceptor interceptor, Threat threat) {
2024-09-24 19:59:25 -07:00
if (interceptor is Interceptor missileComponent) {
_activeInterceptors.Remove(missileComponent);
}
// Remove the interceptor from the threat's assigned interceptors
_threatDataMap[threat].RemoveInterceptor(interceptor);
}
public void RegisterThreatHit(Interceptor interceptor, Threat threat) {
ThreatData threatData = _threatDataMap[threat];
threatData.RemoveInterceptor(interceptor);
if (threatData != null) {
threatData.MarkDestroyed();
}
}
public void RegisterThreatMiss(Interceptor interceptor, Threat threat) {
Debug.Log($"RegisterThreatMiss: Interceptor {interceptor.name} missed threat {threat.name}");
ThreatData threatData = _threatDataMap[threat];
threatData.RemoveInterceptor(interceptor);
2024-09-13 22:45:25 -07:00
}
/// <summary>
/// Assigns the specified list of missiles to available targets based on the assignment scheme.
/// </summary>
/// <param name="missilesToAssign">The list of missiles to assign.</param>
2024-09-24 19:59:25 -07:00
public void AssignInterceptorsToThreats(List<Interceptor> missilesToAssign) {
2024-09-13 22:45:25 -07:00
// Perform the assignment
IEnumerable<IAssignment.AssignmentItem> assignments =
_assignmentScheme.Assign(missilesToAssign, _threatTable);
2024-09-13 22:45:25 -07:00
// Apply the assignments to the missiles
foreach (var assignment in assignments) {
assignment.Interceptor.AssignTarget(assignment.Threat);
_threatDataMap[assignment.Threat].AssignInterceptor(assignment.Interceptor);
Debug.Log($"Interceptor {assignment.Interceptor.name} assigned to threat {assignment.Threat.name}");
}
// Check if any interceptors were not assigned
List<Interceptor> unassignedInterceptors = missilesToAssign.Where(m => !m.HasAssignedTarget()).ToList();
if (unassignedInterceptors.Count > 0)
{
string unassignedIds = string.Join(", ", unassignedInterceptors.Select(m => m.name));
int totalInterceptors = missilesToAssign.Count;
int assignedInterceptors = totalInterceptors - unassignedInterceptors.Count;
Debug.LogWarning($"Warning: {unassignedInterceptors.Count} out of {totalInterceptors} interceptors were not assigned to any threat. " +
$"Unassigned interceptor IDs: {unassignedIds}. " +
$"Total interceptors: {totalInterceptors}, Assigned: {assignedInterceptors}, Unassigned: {unassignedInterceptors.Count}");
// Log information about the assignment scheme
Debug.Log($"Current Assignment Scheme: {_assignmentScheme.GetType().Name}");
2024-09-12 00:17:21 -07:00
}
2024-09-13 22:45:25 -07:00
}
/// <summary>
2024-09-24 19:59:25 -07:00
/// Creates a interceptor based on the provided configuration.
/// </summary>
2024-09-24 19:59:25 -07:00
/// <param name="config">Configuration settings for the interceptor.</param>
/// <returns>The created Interceptor instance, or null if creation failed.</returns>
public Interceptor CreateInterceptor(AgentConfig config) {
string prefabName = config.interceptor_type switch { InterceptorType.HYDRA_70 => "Hydra70",
InterceptorType.MICROMISSILE => "Micromissile",
2024-09-13 22:45:25 -07:00
_ => "Hydra70" };
GameObject interceptorObject = CreateAgent(config, prefabName);
if (interceptorObject == null)
2024-09-13 22:45:25 -07:00
return null;
2024-09-24 19:59:25 -07:00
// Interceptor-specific logic
2024-09-13 22:45:25 -07:00
switch (config.dynamic_config.sensor_config.type) {
case SensorType.IDEAL:
interceptorObject.AddComponent<IdealSensor>();
2024-09-13 22:45:25 -07:00
break;
default:
Debug.LogError($"Sensor type '{config.dynamic_config.sensor_config.type}' not found.");
break;
2024-09-12 00:17:21 -07:00
}
Interceptor interceptor = interceptorObject.GetComponent<Interceptor>();
_interceptorObjects.Add(interceptor);
_activeInterceptors.Add(interceptor);
// Subscribe events
interceptor.OnInterceptHit += RegisterInterceptorHit;
interceptor.OnInterceptMiss += RegisterInterceptorMiss;
2024-09-24 19:24:50 -07:00
// Assign a unique and simple ID
int missileId = _interceptorObjects.Count;
interceptorObject.name = $"{config.interceptor_type}_Interceptor_{missileId}";
return interceptorObject.GetComponent<Interceptor>();
2024-09-13 22:45:25 -07:00
}
/// <summary>
2024-09-24 19:24:50 -07:00
/// Creates a threat based on the provided configuration.
/// </summary>
2024-09-24 19:24:50 -07:00
/// <param name="config">Configuration settings for the threat.</param>
/// <returns>The created Threat instance, or null if creation failed.</returns>
private Threat CreateThreat(AgentConfig config) {
string prefabName = config.threat_type switch {
2024-09-24 19:59:25 -07:00
ThreatType.DRONE => "Drone", ThreatType.ANTISHIP_MISSILE => "AntishipMissile",
_ => throw new System.ArgumentException($"Unsupported threat type: {config.threat_type}")
2024-09-13 22:45:25 -07:00
};
2024-09-24 19:24:50 -07:00
GameObject threatObject = CreateAgent(config, prefabName);
if (threatObject == null)
2024-09-13 22:45:25 -07:00
return null;
Threat threat = threatObject.GetComponent<Threat>();
2024-09-24 19:24:50 -07:00
// Assign a unique and simple ID
int targetId = _threatTable.Count;
threatObject.name = $"{config.threat_type}_Target_{targetId}";
ThreatData threatData = new ThreatData(threat, threatObject.name);
_threatDataMap.Add(threat, threatData);
_threatTable.Add(threatData);
_threatObjects.Add(threat);
// Subscribe events
threat.OnInterceptHit += RegisterThreatHit;
threat.OnInterceptMiss += RegisterThreatMiss;
2024-09-24 19:24:50 -07:00
return threatObject.GetComponent<Threat>();
2024-09-13 22:45:25 -07:00
}
/// <summary>
2024-09-24 19:59:25 -07:00
/// Creates an agent (interceptor or threat) based on the provided configuration and prefab name.
/// </summary>
/// <param name="config">Configuration settings for the agent.</param>
/// <param name="prefabName">Name of the prefab to instantiate.</param>
/// <returns>The created GameObject instance, or null if creation failed.</returns>
2024-09-13 22:45:25 -07:00
public GameObject CreateAgent(AgentConfig config, string prefabName) {
GameObject prefab = Resources.Load<GameObject>($"Prefabs/{prefabName}");
if (prefab == null) {
Debug.LogError($"Prefab '{prefabName}' not found in Resources/Prefabs folder.");
return null;
2024-09-12 15:44:55 -07:00
}
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
Vector3 noiseOffset = Utilities.GenerateRandomNoise(config.standard_deviation.position);
Vector3 noisyPosition = config.initial_state.position + noiseOffset;
2024-09-13 22:45:25 -07:00
GameObject agentObject =
Instantiate(prefab, noisyPosition, Quaternion.Euler(config.initial_state.rotation));
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
Rigidbody agentRigidbody = agentObject.GetComponent<Rigidbody>();
Vector3 velocityNoise = Utilities.GenerateRandomNoise(config.standard_deviation.velocity);
Vector3 noisyVelocity = config.initial_state.velocity + velocityNoise;
2024-09-25 17:51:44 -07:00
agentRigidbody.linearVelocity = noisyVelocity;
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
agentObject.GetComponent<Agent>().SetAgentConfig(config);
return agentObject;
}
2024-09-12 15:44:55 -07:00
public void LoadNewConfig(string configFileName) {
simulationConfig = ConfigLoader.LoadSimulationConfig(configFileName);
if (simulationConfig != null) {
Debug.Log($"Loaded new configuration: {configFileName}");
RestartSimulation();
} else {
Debug.LogError($"Failed to load configuration: {configFileName}");
}
}
public void RestartSimulation() {
OnSimulationEnded?.Invoke();
Debug.Log("Simulation ended");
2024-09-13 22:45:25 -07:00
// Reset simulation time
_elapsedSimulationTime = 0f;
2024-09-25 11:31:51 -07:00
simulationRunning = IsSimulationRunning();
2024-09-13 22:45:25 -07:00
// Clear existing missiles and targets
foreach (var interceptor in _interceptorObjects) {
2024-09-24 19:59:25 -07:00
if (interceptor != null) {
Destroy(interceptor.gameObject);
2024-09-13 22:45:25 -07:00
}
2024-09-12 00:17:21 -07:00
}
foreach (var threat in _threatObjects) {
2024-09-24 19:24:50 -07:00
if (threat != null) {
Destroy(threat.gameObject);
2024-09-13 22:45:25 -07:00
}
}
_interceptorObjects.Clear();
_activeInterceptors.Clear();
_threatObjects.Clear();
_threatTable.Clear();
2024-09-13 22:45:25 -07:00
StartSimulation();
2024-09-13 22:45:25 -07:00
}
void Update() {
// Check if all missiles have terminated
2024-09-24 19:59:25 -07:00
bool allInterceptorsTerminated = true;
foreach (var interceptor in _interceptorObjects) {
2024-09-24 19:59:25 -07:00
if (interceptor != null && !interceptor.IsHit() && !interceptor.IsMiss()) {
allInterceptorsTerminated = false;
2024-09-13 22:45:25 -07:00
break;
}
}
// If all missiles have terminated, restart the simulation
2024-09-24 19:59:25 -07:00
if (allInterceptorsTerminated) {
2024-09-13 22:45:25 -07:00
RestartSimulation();
2024-09-12 00:17:21 -07:00
}
2024-09-13 22:45:25 -07:00
if (simulationRunning && _elapsedSimulationTime < endTime) {
_elapsedSimulationTime += Time.deltaTime;
} else if (_elapsedSimulationTime >= endTime) {
simulationRunning = false;
Debug.Log("Simulation completed.");
}
}
2024-09-12 00:17:21 -07:00
}