| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using UnityEngine; |
| | | 5 | | |
| | | 6 | | // The simulation manager handles the creation of all agents. |
| | | 7 | | // It implements the singleton pattern to ensure that only one instance exists. |
| | | 8 | | public class SimManager : MonoBehaviour { |
| | | 9 | | // Simulation events. |
| | | 10 | | public delegate void SimulationEventHandler(); |
| | | 11 | | public event SimulationEventHandler OnSimulationStarted; |
| | | 12 | | public event SimulationEventHandler OnSimulationEnded; |
| | | 13 | | |
| | | 14 | | // Interceptor events. |
| | | 15 | | public delegate void NewInterceptorEventHandler(IInterceptor interceptor); |
| | | 16 | | public event NewInterceptorEventHandler OnNewInterceptor; |
| | | 17 | | public delegate void NewAssetEventHandler(IInterceptor asset); |
| | | 18 | | public event NewAssetEventHandler OnNewAsset; |
| | | 19 | | public delegate void NewLauncherEventHandler(IInterceptor launcher); |
| | | 20 | | public event NewLauncherEventHandler OnNewLauncher; |
| | | 21 | | |
| | | 22 | | // Threat events. |
| | | 23 | | public delegate void NewThreatEventHandler(IThreat threat); |
| | | 24 | | public event NewThreatEventHandler OnNewThreat; |
| | | 25 | | |
| | | 26 | | // Default simulation configuration file. |
| | | 27 | | private const string _defaultSimulationConfigFile = "7_quadcopters.pbtxt"; |
| | | 28 | | |
| | | 29 | | // Default simulator configuration file. |
| | | 30 | | private const string _defaultSimulatorConfigFile = "simulator.pbtxt"; |
| | | 31 | | |
| | | 32 | | // Map from the agent type to the prefab class name. |
| | | 33 | | // The prefab class must exist in the Resources/Prefabs directory. |
| | 0 | 34 | | private static readonly Dictionary<Configs.AgentType, string> _agentTypePrefabMap = new() { |
| | | 35 | | { Configs.AgentType.Vessel, "Vessel" }, |
| | | 36 | | { Configs.AgentType.ShoreBattery, "ShoreBattery" }, |
| | | 37 | | { Configs.AgentType.CarrierInterceptor, "CarrierInterceptor" }, |
| | | 38 | | { Configs.AgentType.MissileInterceptor, "MissileInterceptor" }, |
| | | 39 | | { Configs.AgentType.FixedWingThreat, "FixedWingThreat" }, |
| | | 40 | | { Configs.AgentType.RotaryWingThreat, "RotaryWingThreat" }, |
| | | 41 | | }; |
| | | 42 | | |
| | | 43 | | // Asset color. |
| | 0 | 44 | | private static readonly Color _assetColor = new Color(0.75f, 0.4f, 0f); |
| | | 45 | | |
| | 0 | 46 | | public static SimManager Instance { get; private set; } |
| | | 47 | | |
| | | 48 | | // Simulation configuration. |
| | 0 | 49 | | public Configs.SimulationConfig SimulationConfig { get; set; } |
| | | 50 | | |
| | | 51 | | // Simulator configuration. |
| | 0 | 52 | | public Configs.SimulatorConfig SimulatorConfig { get; set; } |
| | | 53 | | |
| | | 54 | | // Simulation time. |
| | 0 | 55 | | public float ElapsedTime { get; private set; } = 0f; |
| | 0 | 56 | | public bool IsPaused { get; private set; } = false; |
| | | 57 | | |
| | | 58 | | // If true, the simulation is currently running. |
| | 0 | 59 | | public bool IsRunning { get; private set; } = false; |
| | | 60 | | |
| | | 61 | | // If true, automatically restart the simulation. |
| | 0 | 62 | | public bool AutoRestartOnEnd { get; set; } = true; |
| | | 63 | | |
| | 0 | 64 | | public string Timestamp { get; private set; } = ""; |
| | | 65 | | |
| | | 66 | | // Lists of all agents in the simulation. |
| | 0 | 67 | | private List<IAgent> _interceptors = new List<IAgent>(); |
| | 0 | 68 | | private List<IAgent> _threats = new List<IAgent>(); |
| | 0 | 69 | | private List<IAgent> _dummyAgents = new List<IAgent>(); |
| | | 70 | | |
| | 0 | 71 | | public IReadOnlyList<IAgent> Interceptors => _interceptors.AsReadOnly(); |
| | 0 | 72 | | public IReadOnlyList<IAgent> Threats => _threats.AsReadOnly(); |
| | 0 | 73 | | public IReadOnlyList<IAgent> Agents => Interceptors.Concat(Threats).ToList().AsReadOnly(); |
| | | 74 | | |
| | | 75 | | // Interceptor and threat costs. |
| | 0 | 76 | | public float CostLaunchedInterceptors { get; private set; } = 0f; |
| | 0 | 77 | | public float CostDestroyedThreats { get; private set; } = 0f; |
| | | 78 | | |
| | | 79 | | // Track the number of interceptors and threats spawned and terminated. |
| | 0 | 80 | | private int _numInterceptorsSpawned = 0; |
| | 0 | 81 | | private int _numThreatsSpawned = 0; |
| | 0 | 82 | | private int _numThreatsTerminated = 0; |
| | | 83 | | |
| | 0 | 84 | | public void StartSimulation() { |
| | 0 | 85 | | IsRunning = true; |
| | 0 | 86 | | OnSimulationStarted?.Invoke(); |
| | 0 | 87 | | Debug.Log("Simulation started."); |
| | 0 | 88 | | UIManager.Instance?.LogActionMessage("[SIM] Simulation started."); |
| | | 89 | | |
| | 0 | 90 | | InitializeAssets(); |
| | 0 | 91 | | InitializeLaunchers(); |
| | 0 | 92 | | InitializeThreats(); |
| | 0 | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | public void EndSimulation() { |
| | 0 | 96 | | IsRunning = false; |
| | 0 | 97 | | OnSimulationEnded?.Invoke(); |
| | 0 | 98 | | Debug.Log("Simulation ended."); |
| | 0 | 99 | | UIManager.Instance?.LogActionMessage("[SIM] Simulation ended."); |
| | | 100 | | |
| | | 101 | | // Clear existing interceptors and threats. |
| | 0 | 102 | | foreach (var interceptor in _interceptors) { |
| | 0 | 103 | | if (interceptor as MonoBehaviour != null) { |
| | 0 | 104 | | Destroy(interceptor.gameObject); |
| | 0 | 105 | | } |
| | 0 | 106 | | } |
| | 0 | 107 | | foreach (var threat in _threats) { |
| | 0 | 108 | | if (threat as MonoBehaviour != null) { |
| | 0 | 109 | | Destroy(threat.gameObject); |
| | 0 | 110 | | } |
| | 0 | 111 | | } |
| | 0 | 112 | | foreach (var dummyAgent in _dummyAgents) { |
| | 0 | 113 | | if (dummyAgent as MonoBehaviour != null) { |
| | 0 | 114 | | Destroy(dummyAgent.gameObject); |
| | 0 | 115 | | } |
| | 0 | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | _interceptors.Clear(); |
| | 0 | 119 | | _threats.Clear(); |
| | 0 | 120 | | _dummyAgents.Clear(); |
| | 0 | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | public void PostSimulation() { |
| | 0 | 124 | | if (AutoRestartOnEnd) { |
| | 0 | 125 | | ResetAndStartSimulation(); |
| | 0 | 126 | | } |
| | 0 | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | public void PauseSimulation() { |
| | 0 | 130 | | IsPaused = true; |
| | 0 | 131 | | SetGameSpeed(); |
| | 0 | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | public void ResumeSimulation() { |
| | 0 | 135 | | IsPaused = false; |
| | 0 | 136 | | SetGameSpeed(); |
| | 0 | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | public void QuitSimulation() { |
| | 0 | 140 | | Application.Quit(); |
| | 0 | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | public void ResetAndStartSimulation() { |
| | 0 | 144 | | ElapsedTime = 0f; |
| | 0 | 145 | | CostLaunchedInterceptors = 0f; |
| | 0 | 146 | | CostDestroyedThreats = 0f; |
| | | 147 | | |
| | 0 | 148 | | _numInterceptorsSpawned = 0; |
| | 0 | 149 | | _numThreatsSpawned = 0; |
| | 0 | 150 | | _numThreatsTerminated = 0; |
| | | 151 | | |
| | 0 | 152 | | StartSimulation(); |
| | 0 | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | public void LoadNewSimulationConfig(string simulationConfigFile) { |
| | 0 | 156 | | if (IsRunning) { |
| | 0 | 157 | | EndSimulation(); |
| | 0 | 158 | | } |
| | 0 | 159 | | LoadSimConfigs(simulationConfigFile); |
| | 0 | 160 | | SetGameSpeed(); |
| | | 161 | | |
| | 0 | 162 | | if (SimulationConfig != null) { |
| | 0 | 163 | | Debug.Log($"Loaded new simulation configuration: {simulationConfigFile}."); |
| | 0 | 164 | | ResetAndStartSimulation(); |
| | 0 | 165 | | } else { |
| | 0 | 166 | | Debug.LogError($"Failed to load simulation configuration: {simulationConfigFile}."); |
| | 0 | 167 | | } |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | // Create an interceptor based on the provided configuration. |
| | | 171 | | public IInterceptor CreateInterceptor(Configs.AgentConfig config, Simulation.State initialState, |
| | 0 | 172 | | bool ignoreMetrics = false) { |
| | 0 | 173 | | if (config == null) { |
| | 0 | 174 | | return null; |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | // Load the static configuration. |
| | 0 | 178 | | Configs.StaticConfig staticConfig = ConfigLoader.LoadStaticConfig(config.ConfigFile); |
| | 0 | 179 | | if (staticConfig == null) { |
| | 0 | 180 | | return null; |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | GameObject interceptorObject = null; |
| | 0 | 184 | | if (_agentTypePrefabMap.TryGetValue(staticConfig.AgentType, out var prefab)) { |
| | 0 | 185 | | interceptorObject = CreateAgent(config, initialState, prefab); |
| | 0 | 186 | | } |
| | 0 | 187 | | if (interceptorObject == null) { |
| | 0 | 188 | | return null; |
| | | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | IInterceptor interceptor = interceptorObject.GetComponent<IInterceptor>(); |
| | 0 | 192 | | interceptor.HierarchicalAgent = new HierarchicalAgent(interceptor); |
| | 0 | 193 | | interceptor.StaticConfig = staticConfig; |
| | 0 | 194 | | interceptor.OnTerminated += RegisterInterceptorTerminated; |
| | 0 | 195 | | _interceptors.Add(interceptor); |
| | 0 | 196 | | ++_numInterceptorsSpawned; |
| | | 197 | | |
| | | 198 | | // Assign a unique and simple ID. |
| | 0 | 199 | | interceptorObject.name = $"{staticConfig.Name}_Interceptor_{_numInterceptorsSpawned}"; |
| | | 200 | | |
| | 0 | 201 | | if (!ignoreMetrics) { |
| | | 202 | | // Add the interceptor's unit cost to the total cost. |
| | 0 | 203 | | CostLaunchedInterceptors += staticConfig.Cost; |
| | 0 | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | OnNewInterceptor?.Invoke(interceptor); |
| | 0 | 207 | | return interceptor; |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | // Create a threat based on the provided configuration. |
| | | 211 | | // Returns the created threat instance, or null if creation failed. |
| | 0 | 212 | | public IThreat CreateThreat(Configs.AgentConfig config) { |
| | 0 | 213 | | if (config == null) { |
| | 0 | 214 | | return null; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | // Load the static configuration. |
| | 0 | 218 | | Configs.StaticConfig staticConfig = ConfigLoader.LoadStaticConfig(config.ConfigFile); |
| | 0 | 219 | | if (staticConfig == null) { |
| | 0 | 220 | | return null; |
| | | 221 | | } |
| | | 222 | | |
| | 0 | 223 | | GameObject threatObject = null; |
| | 0 | 224 | | if (_agentTypePrefabMap.TryGetValue(staticConfig.AgentType, out var prefab)) { |
| | 0 | 225 | | threatObject = CreateRandomAgent(config, prefab); |
| | 0 | 226 | | } |
| | 0 | 227 | | if (threatObject == null) { |
| | 0 | 228 | | return null; |
| | | 229 | | } |
| | | 230 | | |
| | 0 | 231 | | IThreat threat = threatObject.GetComponent<IThreat>(); |
| | 0 | 232 | | threat.HierarchicalAgent = new HierarchicalAgent(threat); |
| | 0 | 233 | | threat.StaticConfig = staticConfig; |
| | 0 | 234 | | threat.OnDestroyed += RegisterThreatDestroyed; |
| | 0 | 235 | | threat.OnTerminated += RegisterThreatTerminated; |
| | 0 | 236 | | _threats.Add(threat); |
| | 0 | 237 | | ++_numThreatsSpawned; |
| | | 238 | | |
| | | 239 | | // Assign a unique name. |
| | 0 | 240 | | threatObject.name = $"{staticConfig.Name}_Threat_{_numThreatsSpawned}"; |
| | | 241 | | |
| | 0 | 242 | | OnNewThreat?.Invoke(threat); |
| | 0 | 243 | | return threat; |
| | 0 | 244 | | } |
| | | 245 | | |
| | 0 | 246 | | public IAgent CreateDummyAgent(in Vector3 position, in Vector3 velocity) { |
| | 0 | 247 | | GameObject dummyAgentPrefab = Resources.Load<GameObject>($"Prefabs/DummyAgent"); |
| | 0 | 248 | | GameObject dummyAgentObject = Instantiate(dummyAgentPrefab, position, Quaternion.identity); |
| | 0 | 249 | | var dummyAgent = dummyAgentObject.GetComponent<IAgent>(); |
| | 0 | 250 | | dummyAgent.Velocity = velocity; |
| | 0 | 251 | | _dummyAgents.Add(dummyAgent); |
| | 0 | 252 | | dummyAgent.OnTerminated += (agent) => _dummyAgents.Remove(agent); |
| | 0 | 253 | | return dummyAgent; |
| | 0 | 254 | | } |
| | | 255 | | |
| | 0 | 256 | | public void DestroyDummyAgent(IAgent dummyAgent) { |
| | 0 | 257 | | dummyAgent.Terminate(); |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | // Create an agent based on the provided configuration and prefab name. |
| | | 261 | | private GameObject CreateAgent(Configs.AgentConfig config, Simulation.State initialState, |
| | 0 | 262 | | string prefabName) { |
| | 0 | 263 | | GameObject prefab = Resources.Load<GameObject>($"Prefabs/{prefabName}"); |
| | 0 | 264 | | if (prefab == null) { |
| | 0 | 265 | | Debug.LogError($"Prefab {prefabName} not found in Resources/Prefabs directory."); |
| | 0 | 266 | | return null; |
| | | 267 | | } |
| | | 268 | | |
| | 0 | 269 | | GameObject agentObject = Instantiate(prefab, Coordinates3.FromProto(initialState.Position), |
| | | 270 | | prefab.transform.rotation); |
| | 0 | 271 | | IAgent agent = agentObject.GetComponent<IAgent>(); |
| | 0 | 272 | | agent.AgentConfig = config; |
| | 0 | 273 | | Vector3 velocity = Coordinates3.FromProto(initialState.Velocity); |
| | 0 | 274 | | agent.Velocity = velocity; |
| | | 275 | | |
| | | 276 | | // Set the rotation to face the initial velocity. |
| | 0 | 277 | | if (velocity.sqrMagnitude > Mathf.Epsilon) { |
| | 0 | 278 | | Quaternion targetRotation = Quaternion.LookRotation(velocity, Vector3.up); |
| | 0 | 279 | | agentObject.transform.rotation = targetRotation; |
| | 0 | 280 | | } |
| | 0 | 281 | | return agentObject; |
| | 0 | 282 | | } |
| | | 283 | | |
| | | 284 | | // Create a random agent based on the provided configuration and prefab name. |
| | 0 | 285 | | private GameObject CreateRandomAgent(Configs.AgentConfig config, string prefabName) { |
| | | 286 | | // Randomize the position and the velocity. |
| | 0 | 287 | | Vector3 positionNoise = Utilities.GenerateRandomNoise(config.StandardDeviation.Position); |
| | 0 | 288 | | Vector3 velocityNoise = Utilities.GenerateRandomNoise(config.StandardDeviation.Velocity); |
| | 0 | 289 | | var initialState = new Simulation.State() { |
| | | 290 | | Position = Coordinates3.ToProto(Coordinates3.FromProto(config.InitialState.Position) + |
| | | 291 | | positionNoise), |
| | | 292 | | Velocity = Coordinates3.ToProto(Coordinates3.FromProto(config.InitialState.Velocity) + |
| | | 293 | | velocityNoise), |
| | | 294 | | }; |
| | 0 | 295 | | return CreateAgent(config, initialState, prefabName); |
| | 0 | 296 | | } |
| | | 297 | | |
| | 0 | 298 | | private void Awake() { |
| | 0 | 299 | | if (Instance != null && Instance != this) { |
| | 0 | 300 | | Destroy(gameObject); |
| | 0 | 301 | | return; |
| | | 302 | | } |
| | 0 | 303 | | Instance = this; |
| | 0 | 304 | | DontDestroyOnLoad(gameObject); |
| | | 305 | | |
| | 0 | 306 | | if (!RunWorker.IsWorkerMode) { |
| | 0 | 307 | | LoadSimConfigs(_defaultSimulationConfigFile); |
| | 0 | 308 | | } |
| | 0 | 309 | | Timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss"); |
| | 0 | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | private System.Collections.IEnumerator Start() { |
| | 0 | 313 | | IsPaused = false; |
| | | 314 | | // Wait one frame so every manager has finished Start() and subscribed to simulation events. |
| | 0 | 315 | | yield return null; |
| | | 316 | | |
| | 0 | 317 | | if (!RunWorker.IsWorkerMode) { |
| | 0 | 318 | | StartSimulation(); |
| | 0 | 319 | | ResumeSimulation(); |
| | 0 | 320 | | } |
| | 0 | 321 | | } |
| | | 322 | | |
| | 0 | 323 | | private void FixedUpdate() { |
| | 0 | 324 | | if (IsRunning && !IsPaused && ElapsedTime < SimulationConfig.EndTime) { |
| | 0 | 325 | | ElapsedTime += Time.fixedDeltaTime; |
| | 0 | 326 | | } |
| | 0 | 327 | | } |
| | | 328 | | |
| | 0 | 329 | | private void LateUpdate() { |
| | 0 | 330 | | if (ShouldEndSimulation()) { |
| | 0 | 331 | | EndSimulation(); |
| | 0 | 332 | | PostSimulation(); |
| | 0 | 333 | | } |
| | 0 | 334 | | } |
| | | 335 | | |
| | 0 | 336 | | private void InitializeAssets() { |
| | 0 | 337 | | foreach (var assetConfig in SimulationConfig.AssetConfigs) { |
| | 0 | 338 | | IInterceptor asset = |
| | | 339 | | CreateInterceptor(assetConfig, assetConfig.InitialState, ignoreMetrics: true); |
| | 0 | 340 | | if (asset != null) { |
| | | 341 | | // Change the color of the asset to be orange. |
| | 0 | 342 | | Renderer[] renderers = asset.gameObject.GetComponentsInChildren<Renderer>(); |
| | 0 | 343 | | foreach (var renderer in renderers) { |
| | 0 | 344 | | var propertyBlock = new MaterialPropertyBlock(); |
| | 0 | 345 | | propertyBlock.SetColor("_BaseColor", _assetColor); |
| | 0 | 346 | | propertyBlock.SetColor("_Color", _assetColor); |
| | 0 | 347 | | renderer.SetPropertyBlock(propertyBlock); |
| | 0 | 348 | | } |
| | 0 | 349 | | OnNewAsset?.Invoke(asset); |
| | 0 | 350 | | } |
| | 0 | 351 | | } |
| | 0 | 352 | | } |
| | | 353 | | |
| | 0 | 354 | | private void InitializeLaunchers() { |
| | 0 | 355 | | foreach (var swarmConfig in SimulationConfig.InterceptorSwarmConfigs) { |
| | 0 | 356 | | IInterceptor launcher = CreateInterceptor( |
| | | 357 | | swarmConfig.AgentConfig, swarmConfig.AgentConfig.InitialState, ignoreMetrics: true); |
| | 0 | 358 | | if (launcher != null) { |
| | 0 | 359 | | OnNewLauncher?.Invoke(launcher); |
| | | 360 | | // All launchers are assets. |
| | 0 | 361 | | OnNewAsset?.Invoke(launcher); |
| | 0 | 362 | | } |
| | 0 | 363 | | } |
| | 0 | 364 | | } |
| | | 365 | | |
| | 0 | 366 | | private void InitializeThreats() { |
| | 0 | 367 | | foreach (var swarmConfig in SimulationConfig.ThreatSwarmConfigs) { |
| | 0 | 368 | | for (int i = 0; i < swarmConfig.NumAgents; ++i) { |
| | 0 | 369 | | CreateThreat(swarmConfig.AgentConfig); |
| | 0 | 370 | | } |
| | 0 | 371 | | } |
| | 0 | 372 | | } |
| | | 373 | | |
| | 0 | 374 | | private void LoadSimConfigs(string simulationConfigFile) { |
| | 0 | 375 | | SimulatorConfig = ConfigLoader.LoadSimulatorConfig(_defaultSimulatorConfigFile); |
| | | 376 | | // If a worker run is provided, enable telemetry logging and event logging. |
| | 0 | 377 | | if (RunWorker.IsWorkerMode) { |
| | 0 | 378 | | SimulatorConfig.EnableTelemetryLogging = true; |
| | 0 | 379 | | SimulatorConfig.EnableEventLogging = true; |
| | 0 | 380 | | } |
| | 0 | 381 | | SimulationConfig = ConfigLoader.LoadSimulationConfig(simulationConfigFile); |
| | 0 | 382 | | } |
| | | 383 | | |
| | 0 | 384 | | private void SetGameSpeed() { |
| | 0 | 385 | | if (IsPaused) { |
| | 0 | 386 | | Time.fixedDeltaTime = 0; |
| | 0 | 387 | | SetTimeScale(timeScale: 0); |
| | 0 | 388 | | } else { |
| | 0 | 389 | | Time.fixedDeltaTime = 1.0f / SimulatorConfig.PhysicsUpdateRate; |
| | 0 | 390 | | SetTimeScale(SimulationConfig.TimeScale); |
| | 0 | 391 | | } |
| | 0 | 392 | | } |
| | | 393 | | |
| | 0 | 394 | | private void SetTimeScale(float timeScale) { |
| | 0 | 395 | | Time.timeScale = timeScale; |
| | | 396 | | // Time.fixedDeltaTime is derived from the simulator configuration. |
| | 0 | 397 | | Time.maximumDeltaTime = Time.fixedDeltaTime * 3; |
| | 0 | 398 | | } |
| | | 399 | | |
| | 0 | 400 | | private void RegisterInterceptorTerminated(IAgent interceptor) { |
| | 0 | 401 | | _interceptors.Remove(interceptor); |
| | 0 | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | private void RegisterThreatDestroyed(IThreat threat) { |
| | 0 | 405 | | CostDestroyedThreats += threat.StaticConfig.Cost; |
| | 0 | 406 | | } |
| | | 407 | | |
| | 0 | 408 | | private void RegisterThreatTerminated(IAgent threat) { |
| | 0 | 409 | | _threats.Remove(threat); |
| | 0 | 410 | | ++_numThreatsTerminated; |
| | 0 | 411 | | } |
| | | 412 | | |
| | 0 | 413 | | private bool ShouldEndSimulation() { |
| | 0 | 414 | | if (IsRunning && ElapsedTime >= SimulationConfig.EndTime) { |
| | 0 | 415 | | return true; |
| | | 416 | | } |
| | | 417 | | // A worker run can end early once all spawned threats have been terminated. |
| | 0 | 418 | | if (RunWorker.IsWorkerMode && _numThreatsSpawned > 0 && |
| | 0 | 419 | | _numThreatsTerminated >= _numThreatsSpawned) { |
| | 0 | 420 | | return true; |
| | | 421 | | } |
| | 0 | 422 | | return false; |
| | 0 | 423 | | } |
| | | 424 | | } |