-
Notifications
You must be signed in to change notification settings - Fork 32
/
KethaneExtractor.cs
234 lines (194 loc) · 7.85 KB
/
KethaneExtractor.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.Linq;
using UnityEngine;
using System.Collections.Generic;
namespace Kethane.PartModules
{
public class KethaneExtractor : PartModule
{
public class Resource
{
public string Name { get; private set; }
public float Rate { get; private set; }
public Resource(ConfigNode node)
{
Name = node.GetValue("Name");
Rate = float.Parse(node.GetValue("Rate"));
}
}
private class DefaultExtractorAnimator : IExtractorAnimator
{
public ExtractorState CurrentState { get; private set; }
public void Deploy() { CurrentState = ExtractorState.Deployed; }
public void Retract() { CurrentState = ExtractorState.Retracted; }
public DefaultExtractorAnimator()
{
CurrentState = ExtractorState.Retracted;
}
}
private IExtractorAnimator animator;
private List<Resource> resources;
[KSPField(isPersistant = false)]
public float PowerConsumption;
[KSPField(isPersistant = false, guiActive = true, guiName = "Status")]
public string Status;
[KSPField(isPersistant = false)]
public string HeadTransform;
[KSPField(isPersistant = false)]
public string TailTransform;
[KSPField(isPersistant = false)]
public float HeadOffset;
[KSPField(isPersistant = false)]
public float TailOffset;
public ConfigNode config;
private Transform headTransform;
private Transform tailTransform;
private KethaneParticleEmitter[] emitters;
public override void OnStart(PartModule.StartState state)
{
this.part.force_activate();
animator = part.Modules.OfType<IExtractorAnimator>().SingleOrDefault();
if (animator == null)
{
animator = new DefaultExtractorAnimator();
}
else
{
Events["DeployDrill"].guiActiveEditor = true;
Events["RetractDrill"].guiActiveEditor = true;
}
headTransform = this.part.FindModelTransform(HeadTransform);
tailTransform = this.part.FindModelTransform(TailTransform);
if (state == StartState.Editor) { return; }
if (FlightGlobals.fetch == null) { return; }
emitters = part.Modules.OfType<KethaneParticleEmitter>().ToArray();
foreach (var emitter in emitters)
{
emitter.Setup();
emitter.EmitterTransform.parent = headTransform;
emitter.EmitterTransform.localRotation = Quaternion.identity;
}
}
public override void OnLoad(ConfigNode config)
{
if (this.config == null)
{
this.config = new ConfigNode();
config.CopyTo(this.config);
}
resources = this.config.GetNodes("Resource").Select(n => new Resource(n)).ToList();
}
[KSPEvent(guiActive = true, guiName = "Deploy Drill", active = true, externalToEVAOnly = true, guiActiveUnfocused = true, unfocusedRange = 1.5f)]
public void DeployDrill()
{
animator.Deploy();
}
[KSPEvent(guiActive = true, guiName = "Retract Drill", active = false, externalToEVAOnly = true, guiActiveUnfocused = true, unfocusedRange = 1.5f)]
public void RetractDrill()
{
animator.Retract();
}
[KSPAction("Deploy Drill")]
public void DeployDrillAction(KSPActionParam param)
{
DeployDrill();
}
[KSPAction("Retract Drill")]
public void RetractDrillAction(KSPActionParam param)
{
RetractDrill();
}
[KSPAction("Toggle Drill")]
public void ToggleDrillAction(KSPActionParam param)
{
if (animator.CurrentState == ExtractorState.Deployed || animator.CurrentState == ExtractorState.Deploying)
{
RetractDrill();
}
else if (animator.CurrentState == ExtractorState.Retracted || animator.CurrentState == ExtractorState.Retracting)
{
DeployDrill();
}
}
public override string GetInfo()
{
return String.Concat(resources.Select(r => String.Format("{0} Rate: {1:F2}L/s\n", r.Name, r.Rate)).ToArray()) + String.Format("Power Consumption: {0:F2}/s", PowerConsumption);
}
public void Update()
{
var retracted = (animator.CurrentState == ExtractorState.Retracted);
var deployed = (animator.CurrentState == ExtractorState.Deployed);
if (Events["DeployDrill"].active != retracted || Events["RetractDrill"].active != deployed)
{
Events["DeployDrill"].active = retracted;
Events["RetractDrill"].active = deployed;
foreach (var window in GameObject.FindObjectsOfType(typeof(UIPartActionWindow)).OfType<UIPartActionWindow>().Where(w => w.part == part))
{
window.displayDirty = true;
}
}
Status = animator.CurrentState.ToString();
if (!HighLogic.LoadedSceneIsFlight) { return; }
if (animator.CurrentState != ExtractorState.Retracted)
{
RaycastHit hitInfo;
var hit = raycastGround(out hitInfo);
foreach (var emitter in emitters.Where(e => e.Label != "gas"))
{
emitter.Emit = hit;
}
if (hit)
{
foreach (var emitter in emitters)
{
emitter.EmitterPosition = headTransform.InverseTransformPoint(hitInfo.point);
}
}
foreach (var emitter in emitters.Where(e => e.Label == "gas"))
{
if (animator.CurrentState == ExtractorState.Deployed)
{
emitter.Emit = hit && KethaneData.Current.GetDepositUnder("Kethane", this.vessel) != null;
}
else
{
emitter.Emit = false;
}
}
}
else
{
foreach (var emitter in emitters)
{
emitter.Emit = false;
}
}
}
public override void OnFixedUpdate()
{
if (animator.CurrentState != ExtractorState.Deployed) { return; }
if (!raycastGround()) { return; }
var energyRequest = this.PowerConsumption * TimeWarp.fixedDeltaTime;
var energyRatio = this.part.RequestResource("ElectricCharge", energyRequest) / energyRequest;
foreach (var resource in resources)
{
var deposit = KethaneData.Current.GetDepositUnder(resource.Name, this.vessel);
if (deposit == null) { continue; }
double amount = TimeWarp.fixedDeltaTime * resource.Rate * energyRatio;
amount = Math.Min(amount, deposit.Quantity);
deposit.Quantity += this.part.RequestResource(resource.Name, -amount);
}
}
private bool raycastGround()
{
RaycastHit hitInfo;
return raycastGround(out hitInfo);
}
private bool raycastGround(out RaycastHit hitInfo)
{
var mask = 1 << 15;
var direction = headTransform.position - tailTransform.position;
return Physics.Raycast(tailTransform.position - direction.normalized * TailOffset, direction, out hitInfo, direction.magnitude + HeadOffset + TailOffset, mask);
}
}
}