-
Notifications
You must be signed in to change notification settings - Fork 32
/
KethaneDetector.cs
189 lines (159 loc) · 6.56 KB
/
KethaneDetector.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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Kethane.PartModules
{
public class KethaneDetector : PartModule
{
public static bool ScanningSound
{
get { return Misc.Parse(SettingsManager.GetValue("ScanningSound"), true); }
set { SettingsManager.SetValue("ScanningSound", value); }
}
[KSPField(isPersistant = false)]
public float DetectingPeriod;
[KSPField(isPersistant = false)]
public float DetectingHeight;
[KSPField(isPersistant = false)]
public float PowerConsumption;
[KSPField(isPersistant = true)]
public bool IsDetecting;
public ConfigNode config;
private List<string> resources;
private double TimerEcho;
private float powerRatio;
private static AudioSource PingEmpty;
private static AudioSource PingDeposit;
[KSPEvent(guiActive = true, guiName = "Activate Detector", active = true, externalToEVAOnly = true, guiActiveUnfocused = true, unfocusedRange = 1.5f)]
public void EnableDetection()
{
IsDetecting = true;
}
[KSPEvent(guiActive = true, guiName = "Deactivate Detector", active = false, externalToEVAOnly = true, guiActiveUnfocused = true, unfocusedRange = 1.5f)]
public void DisableDetection()
{
IsDetecting = false;
}
[KSPAction("Activate Detector")]
public void EnableDetectionAction(KSPActionParam param)
{
EnableDetection();
}
[KSPAction("Deactivate Detector")]
public void DisableDetectionAction(KSPActionParam param)
{
DisableDetection();
}
[KSPAction("Toggle Detector")]
public void ToggleDetectionAction(KSPActionParam param)
{
IsDetecting = !IsDetecting;
}
[KSPEvent(guiActive = true, guiName = "Enable Scan Tone", active = true, externalToEVAOnly = true, guiActiveUnfocused = true, unfocusedRange = 1.5f)]
public void EnableSounds()
{
ScanningSound = true;
}
[KSPEvent(guiActive = true, guiName = "Disable Scan Tone", active = false, externalToEVAOnly = true, guiActiveUnfocused = true, unfocusedRange = 1.5f)]
public void DisableSounds()
{
ScanningSound = false;
}
[KSPField(isPersistant = false, guiActive = true, guiName = "Status")]
public string Status;
public override string GetInfo()
{
return String.Format("Maximum Altitude: {0:N0}m\nPower Consumption: {1:F2}/s\nScanning Period: {2:F2}s\nDetects: {3}", DetectingHeight, PowerConsumption, DetectingPeriod, String.Join(", ", resources.ToArray()));
}
public override void OnStart(PartModule.StartState state)
{
if (state == StartState.Editor) { return; }
this.part.force_activate();
PingEmpty = gameObject.AddComponent<AudioSource>();
PingEmpty.clip = GameDatabase.Instance.GetAudioClip("Kethane/Sounds/echo_empty");
PingEmpty.volume = 1;
PingEmpty.Stop();
PingDeposit = gameObject.AddComponent<AudioSource>();
PingDeposit.clip = GameDatabase.Instance.GetAudioClip("Kethane/Sounds/echo_deposit");
PingDeposit.volume = 1;
PingDeposit.Stop();
}
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 => n.GetValue("Name")).ToList();
if (resources.Count == 0)
{
resources = KethaneController.ResourceDefinitions.Select(r => r.Resource).ToList();
}
}
public override void OnUpdate()
{
Events["EnableDetection"].active = !IsDetecting;
Events["DisableDetection"].active = IsDetecting;
Events["EnableSounds"].active = !ScanningSound;
Events["DisableSounds"].active = ScanningSound;
if (Misc.GetTrueAltitude(vessel) <= this.DetectingHeight)
{
if (IsDetecting)
{
Status = powerRatio > 0 ? "Active" : "Insufficient Power";
}
else
{
Status = "Idle";
}
}
else
{
Status = "Out Of Range";
}
foreach (var animator in part.Modules.OfType<IDetectorAnimator>())
{
animator.IsDetecting = IsDetecting;
animator.PowerRatio = powerRatio;
}
}
public override void OnFixedUpdate()
{
double Altitude = Misc.GetTrueAltitude(vessel);
if (IsDetecting && this.vessel != null && this.vessel.gameObject.activeSelf && Altitude <= this.DetectingHeight)
{
var energyRequest = PowerConsumption * TimeWarp.fixedDeltaTime;
var energyDrawn = this.part.RequestResource("ElectricCharge", energyRequest);
this.powerRatio = energyDrawn / energyRequest;
TimerEcho += TimeWarp.deltaTime * this.powerRatio;
var TimerThreshold = this.DetectingPeriod * (1 + Altitude * 0.000002d);
if (TimerEcho >= TimerThreshold)
{
var detected = false;
var cell = MapOverlay.GetCellUnder(vessel.mainBody, vessel.transform.position);
if (resources.All(r => KethaneData.Current.Scans[r][vessel.mainBody.name][cell])) { return; }
foreach (var resource in resources)
{
KethaneData.Current.Scans[resource][vessel.mainBody.name][cell] = true;
if (KethaneData.Current.GetCellDeposit(resource, vessel.mainBody, cell) != null)
{
detected = true;
}
}
MapOverlay.Instance.RefreshCellColor(cell, vessel.mainBody);
TimerEcho = 0;
if (vessel == FlightGlobals.ActiveVessel && ScanningSound)
{
(detected ? PingDeposit : PingEmpty).Play();
}
}
}
else
{
this.powerRatio = 0;
}
}
}
}