forked from FractalUK/KSPInterstellar
-
Notifications
You must be signed in to change notification settings - Fork 13
/
VistaEngineController.cs
161 lines (133 loc) · 5.54 KB
/
VistaEngineController.cs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace InterstellarPlugin
{
class VistaEngineController : FNResourceSuppliableModule {
[KSPField(isPersistant = true)]
bool IsEnabled;
[KSPField(isPersistant = false, guiActive = true, guiName = "Radiation Hazard To")]
public string radhazardstr;
[KSPField(isPersistant = true)]
bool rad_safety_features = true;
protected bool radhazard = false;
protected double minISP = 0;
protected double standard_megajoule_rate = 0;
protected double standard_deut_rate = 0;
protected double standard_lith_rate = 0;
[KSPEvent(guiActive = true, guiName = "Disable Radiation Safety", active = true)]
public void DeactivateRadSafety() {
rad_safety_features = false;
}
[KSPEvent(guiActive = true, guiName = "Activate Radiation Safety", active = false)]
public void ActivateRadSafety() {
rad_safety_features = true;
}
public override void OnStart(PartModule.StartState state) {
if (state == StartState.Editor) {return;}
ModuleEngines curEngineT = (ModuleEngines)this.part.Modules ["ModuleEngines"];
minISP = curEngineT.atmosphereCurve.Evaluate(0);
standard_deut_rate = curEngineT.propellants [1].ratio;
standard_lith_rate = curEngineT.propellants [2].ratio;
}
public override void OnUpdate() {
Events ["DeactivateRadSafety"].active = rad_safety_features;
Events ["ActivateRadSafety"].active = !rad_safety_features;
ModuleEngines curEngineT = (ModuleEngines)this.part.Modules ["ModuleEngines"];
if (curEngineT.isOperational && !IsEnabled) {
IsEnabled = true;
part.force_activate ();
}
List<Vessel> vessels = FlightGlobals.Vessels;
int kerbal_hazard_count = 0;
foreach (Vessel vess in vessels) {
float distance = (float)Vector3d.Distance (vessel.transform.position, vess.transform.position);
if (distance < 2000 && vess != this.vessel) {
kerbal_hazard_count += vess.GetCrewCount ();
}
}
if (kerbal_hazard_count > 0) {
radhazard = true;
if (kerbal_hazard_count > 1) {
radhazardstr = kerbal_hazard_count.ToString () + " Kerbals.";
} else {
radhazardstr = kerbal_hazard_count.ToString () + " Kerbal.";
}
Fields["radhazardstr"].guiActive = true;
} else {
Fields["radhazardstr"].guiActive = false;
radhazard = false;
radhazardstr = "None.";
}
}
public override void OnFixedUpdate() {
ModuleEngines curEngineT = (ModuleEngines)this.part.Modules ["ModuleEngines"];
float throttle = curEngineT.currentThrottle;
if (radhazard && throttle > 0 && rad_safety_features) {
curEngineT.Events ["Shutdown"].Invoke ();
curEngineT.currentThrottle = 0;
curEngineT.requestedThrottle = 0;
ScreenMessages.PostScreenMessage("Engines throttled down as they presently pose a radiation hazard!", 5.0f, ScreenMessageStyle.UPPER_CENTER);
foreach (FXGroup fx_group in part.fxGroups) {
fx_group.setActive (false);
}
}
System.Random rand = new System.Random (new System.DateTime().Millisecond);
List<Vessel> vessels = FlightGlobals.Vessels;
List<Vessel> vessels_to_remove = new List<Vessel> ();
List<ProtoCrewMember> crew_to_remove = new List<ProtoCrewMember> ();
double death_prob = 1.0 * TimeWarp.fixedDeltaTime;
if (radhazard && throttle > 0 && !rad_safety_features) {
foreach (Vessel vess in vessels) {
float distance = (float)Vector3d.Distance (vessel.transform.position, vess.transform.position);
if (distance < 2000 && vess != this.vessel && vess.GetCrewCount() > 0) {
float inv_sq_dist = distance / 50.0f;
float inv_sq_mult = 1.0f / inv_sq_dist / inv_sq_dist;
List<ProtoCrewMember> vessel_crew = vess.GetVesselCrew ();
foreach (ProtoCrewMember crew_member in vessel_crew) {
if (UnityEngine.Random.value >= (1.0 - death_prob*inv_sq_mult)) {
if(!vess.isEVA) {
ScreenMessages.PostScreenMessage(crew_member.name + " was killed by Neutron Radiation!", 5.0f, ScreenMessageStyle.UPPER_CENTER);
crew_to_remove.Add (crew_member);
}else{
ScreenMessages.PostScreenMessage(crew_member.name + " was killed by Neutron Radiation!", 5.0f, ScreenMessageStyle.UPPER_CENTER);
vessels_to_remove.Add (vess);
}
}
}
}
}
foreach (Vessel vess in vessels_to_remove) {
vess.rootPart.Die ();
}
foreach (ProtoCrewMember crew_member in crew_to_remove) {
Vessel vess = FlightGlobals.Vessels.Find (p => p.GetVesselCrew ().Contains (crew_member));
Part part = vess.Parts.Find(p => p.protoModuleCrew.Contains(crew_member));
part.RemoveCrewmember (crew_member);
crew_member.Die ();
}
}
if (throttle > 0) {
double power = consumeFNResource(2500.0 * TimeWarp.fixedDeltaTime, FNResourceManager.FNRESOURCE_MEGAJOULES);
curEngineT.propellants[1].ratio = (float)(standard_deut_rate / throttle / throttle);
curEngineT.propellants[2].ratio = (float)(standard_lith_rate / throttle / throttle);
FloatCurve newISP = new FloatCurve();
newISP.Add(0, (float)(minISP / throttle));
curEngineT.atmosphereCurve = newISP;
if (power >= 2500 * TimeWarp.fixedDeltaTime) {
curEngineT.maxThrust = 1100;
} else {
curEngineT.maxThrust = 0.0001f;
}
}
}
public override string getResourceManagerDisplayName() {
return "DT Vista Engine";
}
public override int getPowerPriority() {
return 1;
}
}
}