-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEngineIgnitorExternal.cs
More file actions
200 lines (172 loc) · 5.31 KB
/
EngineIgnitorExternal.cs
File metadata and controls
200 lines (172 loc) · 5.31 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EngineIgnitor
{
public class ModuleExternalIgnitor : PartModule
{
public static List<ModuleExternalIgnitor> s_ExternalIgnitors = new List<ModuleExternalIgnitor>();
// We can ignite as many times as we want by default.
// -1: Infinite. 0: Unavailable. 1~...: As is.
[KSPField(isPersistant = false)]
public int ignitionsAvailable = -1;
// Remain ignitionsRemained.
[KSPField(isPersistant = true)]
public int ignitionsRemained = -1;
[KSPField(isPersistant = false, guiActive = true, guiName = "Ignitions")]
private string ignitionsAvailableString = "Infinite";
// Whether we use our own resources or the vessel's.
[KSPField(isPersistant = false)]
public bool provideRequiredResources = false;
[KSPField(isPersistant = false)]
public string ignitorType = "universal";
[KSPField(isPersistant = false)]
public float igniteRange = 1.5f;
private StartState m_startState = StartState.None;
public List<IgnitorResource> ignitorResources;
public Part attachedPart = null;
private const int CLEAR_ATTACHED_PART_DELAY = 10;
private int clearAttachedPartDelay = CLEAR_ATTACHED_PART_DELAY;
public override void OnStart(StartState state)
{
m_startState = state;
if (state != StartState.None && state != StartState.Editor)
{
if(s_ExternalIgnitors.Contains(this) == false)
s_ExternalIgnitors.Add(this);
}
if (state == StartState.Editor)
{
ignitionsRemained = ignitionsAvailable;
}
}
public override void OnAwake()
{
base.OnAwake();
if (ignitorResources == null)
ignitorResources = new List<IgnitorResource>();
}
public override void OnUpdate()
{
if (m_startState != StartState.None && m_startState != StartState.Editor)
{
if (this.part.parent != null)
{
attachedPart = this.part.parent;
clearAttachedPartDelay = CLEAR_ATTACHED_PART_DELAY;
}
else
{
if (clearAttachedPartDelay > 0)
clearAttachedPartDelay--;
if (clearAttachedPartDelay == 0)
{
attachedPart = null;
}
}
if (ignitionsRemained != -1)
ignitionsAvailableString = ignitorType + " - " + ignitionsRemained.ToString() + "/" + ignitionsAvailable.ToString();
else
ignitionsAvailableString = ignitorType + " - " + "Infinite";
if (this.vessel == null)
{
s_ExternalIgnitors.Remove(this);
}
else
{
bool enoughIgnitorResources = true;
if (provideRequiredResources == true && ignitorResources.Count > 0)
{
foreach (IgnitorResource resource in ignitorResources)
{
resource.currentAmount = Convert.ToSingle(part.Resources[resource.name].amount);
if (resource.currentAmount < resource.amount)
{
enoughIgnitorResources = false;
break;
}
}
}
if ((ignitionsRemained == -1 || ignitionsRemained > 0) && enoughIgnitorResources)
{
if (s_ExternalIgnitors.Contains(this) == false)
s_ExternalIgnitors.Add(this);
}
else
{
s_ExternalIgnitors.Remove(this);
}
}
}
}
public void ConsumeResource()
{
foreach (IgnitorResource resource in ignitorResources)
{
part.Resources[resource.name].amount = Math.Max(0.0f, part.Resources[resource.name].amount - resource.amount);
}
}
public override void OnSave(ConfigNode node)
{
foreach (IgnitorResource ignitorResource in ignitorResources)
{
ignitorResource.Save(node.AddNode("IGNITOR_RESOURCE"));
}
base.OnSave(node);
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
if (ignitorResources != null)
ignitorResources = new List<IgnitorResource>();
if (part.partInfo != null)
{
ConfigNode origNode = null;
//Debug.Log(part.partInfo.name);
foreach (UrlDir.UrlConfig config in GameDatabase.Instance.GetConfigs("PART"))
{
//Debug.Log(config.name.Replace("_", "."));
if (config.name.Replace("_", ".") == part.partInfo.name)
{
foreach (ConfigNode configNode in config.config.GetNodes("MODULE"))
{
//Debug.Log(configNode.GetValue("name"));
if (configNode.GetValue("name") == moduleName)
{
origNode = configNode;
break;
}
}
break;
}
}
if (origNode != null)
{
//Debug.Log("Original module config node found.");
foreach (ConfigNode subNode in origNode.GetNodes("IGNITOR_RESOURCE"))
{
//Debug.Log("IgnitorResource node found.");
if (subNode.HasValue("name") == false || subNode.HasValue("amount") == false)
{
//Debug.Log("Ignitor Resource must have \'name\' and \'amount\'.");
continue;
}
IgnitorResource newIgnitorResource = new IgnitorResource();
newIgnitorResource.Load(subNode);
//Debug.Log("IgnitorResource added: " + newIgnitorResource.name + " " + newIgnitorResource.amount.ToString("F2"));
ignitorResources.Add(newIgnitorResource);
}
}
}
//Debug.Log("Total ignitor resources: " + ignitorResources.Count.ToString());
}
public override string GetInfo()
{
if (ignitionsAvailable != -1)
return "Can ignite for " + ignitionsAvailable.ToString() + " time(s).\n" + "Ignitor type: " + ignitorType + "\n";
else
return "Can ignite for infinite times.\n" + "Ignitor type: " + ignitorType + "\n";
}
}
}