/
ConnectionInfo.cs
190 lines (170 loc) · 5.3 KB
/
ConnectionInfo.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
using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.Localization;
namespace KERBALISM
{
public enum LinkStatus // link state
{
direct_link,
indirect_link,
no_link,
blackout
};
public sealed class ConnectionInfo
{
public ConnectionInfo() { }
public ConnectionInfo(LinkStatus status, double rate, double strength, double internal_cost, double science_cost, string target_name)
{
this.linked = status == LinkStatus.direct_link || status == LinkStatus.indirect_link;
this.status = status;
this.rate = rate;
this.strength = strength;
this.internal_cost = internal_cost;
this.science_cost = science_cost;
this.target_name = target_name;
}
public ConnectionInfo(Vessel v)
{
// return no connection if there is no ec left
if (ResourceCache.Info(v, "ElectricCharge").amount <= double.Epsilon)
{
// hysteresis delay
if ((DB.Vessel(v).hyspos_signal >= 5.0))
{
DB.Vessel(v).hyspos_signal = 5.0;
DB.Vessel(v).hysneg_signal = 0.0;
return;
}
DB.Vessel(v).hyspos_signal += 0.1;
}
else
{
// hysteresis delay
DB.Vessel(v).hysneg_signal += 0.1;
if (!(DB.Vessel(v).hysneg_signal >= 5.0))
return;
DB.Vessel(v).hysneg_signal = 5.0;
DB.Vessel(v).hyspos_signal = 0.0;
}
List<ModuleDataTransmitter> transmitters;
// if vessel is loaded
if (v.loaded)
{
// find transmitters
transmitters = v.FindPartModulesImplementing<ModuleDataTransmitter>();
if (transmitters != null)
{
foreach (ModuleDataTransmitter t in transmitters)
{
if (t.antennaType == AntennaType.INTERNAL) // do not include internal data rate
internal_cost += t.DataResourceCost * t.DataRate;
else
{
// do we have an animation
ModuleDeployableAntenna animation = t.part.FindModuleImplementing<ModuleDeployableAntenna>();
if (animation != null)
{
// only include data rate if transmitter is extended
if (animation.deployState == ModuleDeployablePart.DeployState.EXTENDED)
{
rate += t.DataRate;
science_cost += t.DataResourceCost * t.DataRate;
}
}
// no animation
else
{
rate += t.DataRate;
science_cost += t.DataResourceCost * t.DataRate;
}
}
}
}
}
// if vessel is not loaded
else
{
// find proto transmitters
foreach (ProtoPartSnapshot p in v.protoVessel.protoPartSnapshots)
{
// get part prefab (required for module properties)
Part part_prefab = PartLoader.getPartInfoByName(p.partName).partPrefab;
transmitters = part_prefab.FindModulesImplementing<ModuleDataTransmitter>();
if (transmitters != null)
{
foreach (ModuleDataTransmitter t in transmitters)
{
if (t.antennaType == AntennaType.INTERNAL) // do not include internal data rate
internal_cost += t.DataResourceCost * t.DataRate;
else
{
// do we have an animation
ProtoPartModuleSnapshot m = p.FindModule("ModuleDeployableAntenna");
if (m != null)
{
// only include data rate if transmitter is extended
string deployState = Lib.Proto.GetString(m, "deployState");
if (deployState == "EXTENDED")
{
rate += t.DataRate;
science_cost += t.DataResourceCost * t.DataRate;
}
}
// no animation
else
{
rate += t.DataRate;
science_cost += t.DataResourceCost * t.DataRate;
}
}
}
}
}
}
// if CommNet is enabled
if (HighLogic.fetch.currentGame.Parameters.Difficulty.EnableCommNet)
{
// are we connected to DSN
if (v.connection != null)
{
if (v.connection.IsConnected)
{
linked = true;
status = v.connection.ControlPath.First.hopType == CommNet.HopType.Home ? LinkStatus.direct_link : LinkStatus.indirect_link;
strength = v.connection.SignalStrength;
rate = rate * strength;
target_name = Lib.Ellipsis(Localizer.Format(v.connection.ControlPath.First.end.displayName).Replace("Kerbin", "DSN"), 20);
return;
}
// is loss of connection due to plasma blackout
else if (Lib.ReflectionValue<bool>(v.connection, "inPlasma")) // calling InPlasma causes a StackOverflow :(
{
status = LinkStatus.blackout;
rate = 0.0;
internal_cost = 0.0;
science_cost = 0.0;
return;
}
}
// no connection
rate = 0.0;
internal_cost = 0.0;
science_cost = 0.0;
return;
}
// the simple stupid always connected signal system
linked = true;
status = LinkStatus.direct_link;
strength = 1; // 100 %
target_name = "DSN: KSC";
}
public bool linked = false; // true if there is a connection back to DSN
public LinkStatus status = LinkStatus.no_link; // the link status
public double rate = 0.0; // science data rate, internal transmitters can not transmit science data only telemetry data
public double internal_cost = 0.0; // control and telemetry ec cost
public double science_cost = 0.0; // science ec cost
public double strength = 0.0; // signal strength
public string target_name = ""; // receiving node name
}
} // KERBALISM