-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHPMGSystem.cs
More file actions
141 lines (122 loc) · 4.24 KB
/
HPMGSystem.cs
File metadata and controls
141 lines (122 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
IMyRadioAntenna missileAntenna;
IMyCameraBlock cameraBlock;
IMySensorBlock sensorBlock;
List<IMyTerminalBlock> launchThrusters = new List<IMyTerminalBlock>();
List<IMyWarhead> warheads = new List<IMyWarhead>();
IMyRemoteControl remoteControl;
Vector3D targetPosition;
bool isLaunched = false;
public Program()
{
missileAntenna = GridTerminalSystem.GetBlockWithName("Missile Antenna") as IMyRadioAntenna;
cameraBlock = GridTerminalSystem.GetBlockWithName("Camera Block Name") as IMyCameraBlock;
sensorBlock = GridTerminalSystem.GetBlockWithName("Sensor Block Name") as IMySensorBlock;
remoteControl = GridTerminalSystem.GetBlockWithName("PM- Guidance System") as IMyRemoteControl;
GridTerminalSystem.GetBlockGroupWithName("PM- Launch Thrusters").GetBlocks(launchThrusters);
GridTerminalSystem.GetBlockGroupWithName("PM- Power System").GetBlocksOfType(warheads);
// Enable raycasting for the camera
cameraBlock.EnableRaycast = true;
// Configure the sensor
sensorBlock.LeftExtend = 1;
sensorBlock.RightExtend = 1;
sensorBlock.TopExtend = 1;
sensorBlock.BottomExtend = 1;
sensorBlock.FrontExtend = 1;
sensorBlock.BackExtend = 1;
sensorBlock.DetectLargeShips = true;
sensorBlock.DetectSmallShips = true;
// Run the script every 10 update ticks (about 1 second)
Runtime.UpdateFrequency = UpdateFrequency.Update20;
}
public void Main(string argument, UpdateType updateSource)
{
string targetName = missileAntenna.CustomData;
if (string.IsNullOrEmpty(targetName))
{
UpdateAntennaStatus("waiting for hit list");
return;
}
bool targetIdentified = false;
IMyBroadcastListener listener = IGC.RegisterBroadcastListener(targetName);
while (listener.HasPendingMessage)
{
MyIGCMessage message = listener.AcceptMessage();
targetPosition = (Vector3D)message.Data;
targetIdentified = true;
if (Vector3D.Distance(remoteControl.GetPosition(), targetPosition) <= 5000)
{
UpdateAntennaStatus("target locked, launching");
ArmWarheads();
if (!isLaunched)
{
ActivateLaunchThrusters();
isLaunched = true;
}
break;
}
}
if (targetIdentified && !isLaunched)
{
UpdateAntennaStatus("target identified, watching");
}
else if (!targetIdentified)
{
UpdateAntennaStatus("scanning for targets");
}
// Control missile after launch
if (isLaunched)
{
// Increase update frequency for more frequent guidance updates
Runtime.UpdateFrequency = UpdateFrequency.Update1;
// Continuously update target position from latest broadcast
IMyBroadcastListener flightListener = IGC.RegisterBroadcastListener(missileAntenna.CustomData);
if (flightListener.HasPendingMessage)
{
MyIGCMessage flightMessage = flightListener.AcceptMessage();
targetPosition = (Vector3D)flightMessage.Data;
}
// Update missile flight path
remoteControl.SetAutoPilotEnabled(false);
remoteControl.ClearWaypoints();
remoteControl.AddWaypoint(targetPosition, "Target");
remoteControl.SetAutoPilotEnabled(true);
// Check for impact
if (sensorBlock.IsActive && sensorBlock.LastDetectedEntity != null)
{
DetonateWarheads();
}
// Update the estimated countdown until impact
UpdateAntennaStatus("In flight - " + EstimateTimeToImpact() + "s to impact");
}
}
void UpdateAntennaStatus(string status)
{
missileAntenna.CustomName = "Missile Antenna [" + status + "]";
}
string EstimateTimeToImpact()
{
double distanceToTarget = Vector3D.Distance(remoteControl.GetPosition(), targetPosition);
double speed = remoteControl.GetShipSpeed();
return (speed > 0) ? (distanceToTarget / speed).ToString("F2") : "N/A";
}
void ActivateLaunchThrusters()
{
foreach (var thruster in launchThrusters)
{
(thruster as IMyThrust).ThrustOverridePercentage = 100;
}
}
void ArmWarheads()
{
foreach (var warhead in warheads)
{
warhead.IsArmed = true;
}
}
void DetonateWarheads()
{
foreach (var warhead in warheads)
{
warhead.Detonate();
}
}