-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartFailure.cs
234 lines (211 loc) · 5.92 KB
/
PartFailure.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using KSP.IO;
/******************************************************************************
* Copyright (c) 2013, Justin Bengtson
*
* Part Failure for Kerbal Space Program
*
* This code is licensed under Creative Commons CC BY-NC-SA 3.0
* Attribution-NonCommercial-ShareAlike 3.0 Unported
*
* License text available here:
* http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
******************************************************************************/
namespace RegexKSP {
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class PartDamage : MonoBehaviour {
private bool configLoaded = false;
public PluginConfiguration config;
private double checkInterval = 10.0;
private double checkThreshold = 0.9; // chance per interval seconds damage happens
private int randomTries = 5;
private double pollTime = 0.0;
private System.Random random = new System.Random();
public void Awake() {
loadConfig();
}
public void OnDisable() {
saveConfig();
}
public void OnDestroy() {
}
public void FixedUpdate() {
if(canRun) {
double timeNow = Planetarium.GetUniversalTime();
double timeDiff = timeNow - pollTime;
if(timeDiff > checkInterval) {
pollTime = timeNow;
if(random.NextDouble() < checkThreshold) {
doDamage();
}
}
}
}
public static void markGUIDirty(Part p) {
foreach(UIPartActionWindow window in FindObjectsOfType(typeof(UIPartActionWindow))) {
if(window.part == p) {
window.displayDirty = true;
}
}
}
/*
private void doDamage() {
List<Part> parts = FlightGlobals.ActiveVessel.parts;
int numParts = parts.Count;
Debug.Log("Number of parts: " + numParts);
// try to find a part randomly.
bool found = false;
for(int k = 0; k < randomTries; k++) {
Part p = parts[random.Next(0, numParts - 1)];
if(found = canDamage(p)) {
break;
}
}
// if we haven't found one randomly, grab the first one we can fuck up.
if(!found) {
foreach(Part p in parts) {
if(canDamage(p)) {
break;
}
}
}
}
*/
public bool doDamage(string typeModule) {
}
private List<Part> findEligibleParts(string typeModule, List<Part> pCol) {
List<Part> eligible = new List<Part>();
foreach(Part p in pCol) {
bool has = false;
foreach(PartModule pm in p.Modules) {
if(pm.ClassName == typeModule) {
has = true;
}
}
if(has) { eligible.Add(p); }
}
return eligible;
}
private List<Part> findEligibleResourceParts(List<Part> pCol) {
List<Part> eligible = new List<Part>();
foreach(Part p in pCol) {
if(p.Resources.Count > 0) {
eligible.Add(p);
}
}
return eligible;
}
private bool canDamage(Part p) {
if(hasModule(p, "ModuleGimbal")) {
// engine gimbal damage
p.AddModule("DamageTestModule");
return true;
}
if(hasModule(p, "ModuleEngines")) {
// engine damage
p.AddModule("DamageTestModule");
return true;
}
if(hasModule(p, "ModuleParachute")) {
// parachute damage
p.AddModule("DamageTestModule");
return true;
}
if(hasModule(p, "ModuleDecouple") || hasModule(p, "ModuleAnchoredDecoupler")) {
// decoupler damage
p.AddModule("DamageTestModule");
return true;
}
if(hasModule(p, "ModuleDockingNode")) {
// docking port damage
p.AddModule("DamageTestModule");
return true;
}
if(p.Resources.Count > 0) {
// fuel tank damage
Debug.Log("Found a fuel tank");
addPartModule(p, "DamageTestModule");
return true;
}
if(hasModule(p, "ModuleResourceIntake")) {
// intake damage
p.AddModule("DamageTestModule");
return true;
}
if(hasModule(p, "ModuleWheel")) {
// wheel damage
p.AddModule("DamageTestModule");
return true;
}
if(hasModule(p, "ModuleEnviroSensor")) {
// sensor damage
p.AddModule("DamageTestModule");
return true;
}
if(hasModule(p, "ModuleDeployableSolarPanel")) {
// solar panel damage
p.AddModule("DamageTestModule");
return true;
}
if(hasModule(p, "ModuleGenerator")) {
// generator damage
p.AddModule("DamageTestModule");
return true;
}
return false;
}
private void addPartModule(Part p, string module) {
try {
ConfigNode node = new ConfigNode("MODULE");
node.AddValue("name", module);
p.AddModule(node);
} catch(Exception e) {
Debug.Log("PartDamage: Failed to add PartModule" + module + "--> " + e.Message + "\n" + e.StackTrace);
}
}
/*
private bool hasModule(Part p, string className) {
foreach(PartModule pm in p.Modules) {
if(pm.ClassName == className) {
return true;
}
}
return false;
}
*/
private bool canRun {
get {
return FlightGlobals.fetch != null && FlightGlobals.ActiveVessel != null;
}
}
private void loadConfig() {
Debug.Log("Loading PartDamage settings.");
if(!configLoaded) {
config = KSP.IO.PluginConfiguration.CreateForType<PartDamage>(null);
config.load();
configLoaded = true;
try {
checkInterval = config.GetValue<double>("checkInterval", 10.0);
checkThreshold = config.GetValue<double>("checkThreshold", 0.9);
randomTries = config.GetValue<int>("randomTries", 5);
} catch(ArgumentException) {
// do nothing here, the defaults are already set
}
}
}
private void saveConfig() {
Debug.Log("Saving PartDamage settings.");
if(!configLoaded) {
config = KSP.IO.PluginConfiguration.CreateForType<PartDamage>(null);
}
config["checkInterval"] = checkInterval;
config["checkThreshold"] = checkThreshold;
config["randomTries"] = randomTries;
config.save();
}
}
}