This commit is contained in:
turtlebasket
2024-10-04 02:51:19 +00:00
parent be34e325d6
commit 7805acc392
387 changed files with 95157 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
// Integrated Air Defense System
public class IADS : MonoBehaviour {
public enum ThreatAssignmentStyle {
ONE_TIME,
CONTINUOUS
}
public static IADS Instance { get; private set; }
private IAssignment _assignmentScheme;
[SerializeField]
private List<ThreatData> _threatTable = new List<ThreatData>();
private Dictionary<Threat, ThreatData> _threatDataMap = new Dictionary<Threat, ThreatData>();
private List<Interceptor> _assignmentQueue = new List<Interceptor>();
private void Awake() {
if (Instance == null) {
Instance = this;
} else {
Destroy(gameObject);
}
}
private void Start() {
SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded;
SimManager.Instance.OnNewThreat += RegisterNewThreat;
SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor;
_assignmentScheme = new ThreatAssignment();
}
public void LateUpdate() {
if (_assignmentQueue.Count > 0) {
AssignInterceptorsToThreats(_assignmentQueue);
_assignmentQueue.Clear();
}
}
public void RequestThreatAssignment(List<Interceptor> interceptors) {
_assignmentQueue.AddRange(interceptors);
}
public void RequestThreatAssignment(Interceptor interceptor) {
_assignmentQueue.Add(interceptor);
}
/// <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>
public void AssignInterceptorsToThreats(List<Interceptor> missilesToAssign) {
// Perform the assignment
IEnumerable<IAssignment.AssignmentItem> assignments =
_assignmentScheme.Assign(missilesToAssign, _threatTable);
// 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}");
}
}
public void RegisterNewThreat(Threat threat) {
ThreatData threatData = new ThreatData(threat, threat.gameObject.name);
_threatTable.Add(threatData);
_threatDataMap.Add(threat, threatData);
// Subscribe to the threat's events
// TODO: If we do not want omniscient IADS, we
// need to model the IADS's sensors here.
threat.OnInterceptHit += RegisterThreatHit;
threat.OnInterceptMiss += RegisterThreatMiss;
}
public void RegisterNewInterceptor(Interceptor interceptor) {
// Placeholder
interceptor.OnInterceptMiss += RegisterInterceptorMiss;
interceptor.OnInterceptHit += RegisterInterceptorHit;
}
private void RegisterInterceptorHit(Interceptor interceptor, Threat threat) {
ThreatData threatData = _threatDataMap[threat];
if (threatData != null) {
threatData.RemoveInterceptor(interceptor);
MarkThreatDestroyed(threatData);
}
}
private void RegisterInterceptorMiss(Interceptor interceptor, Threat threat) {
// Remove the interceptor from the threat's assigned interceptors
_threatDataMap[threat].RemoveInterceptor(interceptor);
}
private void RegisterThreatHit(Interceptor interceptor, Threat threat) {
ThreatData threatData = _threatDataMap[threat];
if (threatData != null) {
threatData.RemoveInterceptor(interceptor);
MarkThreatDestroyed(threatData);
}
}
private void MarkThreatDestroyed(ThreatData threatData) {
if (threatData != null) {
threatData.MarkDestroyed();
}
}
private void RegisterThreatMiss(Interceptor interceptor, Threat threat) {
ThreatData threatData = _threatDataMap[threat];
threatData.RemoveInterceptor(interceptor);
}
private void RegisterSimulationEnded() {
_threatTable.Clear();
_threatDataMap.Clear();
_assignmentQueue.Clear();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a5899f1049cf3d64e8c06c1db772c879
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,48 @@
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public enum ThreatStatus {
UNASSIGNED,
ASSIGNED,
DESTROYED
}
[System.Serializable]
public class ThreatData
{
public Threat Threat;
[SerializeField]
private ThreatStatus _status;
public ThreatStatus Status { get { return _status; } }
public string ThreatID;
[SerializeField]
private List<Interceptor> _assignedInterceptors; // Changed from property to field
public void AssignInterceptor(Interceptor interceptor) {
if(Status == ThreatStatus.DESTROYED) {
Debug.LogError($"AssignInterceptor: Threat {ThreatID} is destroyed, cannot assign interceptor");
return;
}
_status = ThreatStatus.ASSIGNED;
_assignedInterceptors.Add(interceptor);
}
public void RemoveInterceptor(Interceptor interceptor) {
_assignedInterceptors.Remove(interceptor);
if(_assignedInterceptors.Count == 0) {
_status = ThreatStatus.UNASSIGNED;
}
}
public void MarkDestroyed() {
_status = ThreatStatus.DESTROYED;
}
// Constructor remains the same
public ThreatData(Threat threat, string threatID)
{
Threat = threat;
_status = ThreatStatus.UNASSIGNED;
ThreatID = threatID;
_assignedInterceptors = new List<Interceptor>(); // Initialize the list
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: adc0c5dbdb9dc7d498b50cf9a15c2db5

View File

@@ -0,0 +1,27 @@
using UnityEngine;
using System.Collections.Generic;
public class Vessel : MonoBehaviour {
[SerializeField]
private List<Interceptor> missileInventory = new List<Interceptor>();
public void AddInterceptor(Interceptor interceptor) {
if (interceptor != null) {
missileInventory.Add(interceptor);
}
}
public void RemoveInterceptor(Interceptor interceptor) {
missileInventory.Remove(interceptor);
}
public List<Interceptor> GetInterceptorInventory() {
return new List<Interceptor>(missileInventory);
}
public int GetInterceptorCount() {
return missileInventory.Count;
}
// Additional methods can be added here as needed
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 607b5f69ae2775c4ab0089839f65fd61
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: