forked from AniruddhKSP/KSP-Beamed-Power-Standalone-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReceivedPower.cs
180 lines (166 loc) · 8.13 KB
/
ReceivedPower.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
using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.Localization;
namespace UniversalResourceTransfer
{
public class ReceivedPower
{
PlanetOcclusion occlusion = new PlanetOcclusion();
VesselFinder vesselFinder = new VesselFinder();
RelativisticEffects relativistic = new RelativisticEffects();
List<Vessel> VesselList = new List<Vessel>();
List<double> excessList = new List<double>();
List<double> constantList = new List<double>();
List<string> targetList = new List<string>();
public List<string> wavelengthList = new List<string>();
List<CelestialBody> planetList = new List<CelestialBody>();
int frames = 20;
//localization
string occludedby = Localizer.Format("#LOC_BeamedPower_status_Occludedby") + " ";
string operational = Localizer.Format("#LOC_BeamedPower_status_Operational");
string vesselNone = Localizer.Format("#LOC_BeamedPower_Vessel_None");
string warpEngaged = Localizer.Format("#LOC_BeamedPower_WarpDriveEngaged");
private double FractionalFlux(Vector3d source_pos, Vector3d dest_pos, Part part)
{
Vector3d resultant = (source_pos - dest_pos).normalized;
double Angle = Vector3d.Angle(resultant, -part.transform.up);
double flux = Math.Cos(Math.PI * Angle / 180d);
return flux;
}
public void Directional(Part thisPart, int counter, bool Listening, float percentagePower, double recvSize, double recvEfficiency,
bool useSpotArea, bool useFacingVector, string state, out string status, out string VesselName, out double receivedpower, out int count)
{
frames += 1;
if (frames == 40)
{
vesselFinder.SourceData(thisPart.vessel.GetDisplayName(), out VesselList, out excessList, out constantList, out targetList, out wavelengthList);
frames = 0;
}
if (counter >= VesselList.Count)
{
counter = 0;
}
if (VesselList.Count > 0)
{
if (Listening & targetList[counter] == thisPart.vessel.GetDisplayName())
{
Vector3d dest = thisPart.vessel.GetWorldPos3D();
double excess2 = excessList[counter]; double constant2 = constantList[counter];
VesselName = VesselList[counter].GetDisplayName();
Vector3d source = VesselList[counter].GetWorldPos3D();
double distance = Vector3d.Distance(source, dest);
double spotsize = (useSpotArea) ? Math.Pow((constant2 * distance / 2), 2) * 3.14 : constant2 * distance;
occlusion.IsOccluded(source, dest, wavelengthList[counter], out CelestialBody body, out bool occluded);
// adding EC that has been received
if (recvSize < spotsize)
{
receivedpower = Math.Round(((recvSize / spotsize) * recvEfficiency * excess2 * (percentagePower / 100)), 1);
}
else
{
receivedpower = Math.Round(((recvEfficiency * excess2) * (percentagePower / 100)), 1);
}
if (occluded)
{
receivedpower = 0;
state = occludedby + body.GetDisplayName().TrimEnd('N', '^');
}
else
{
state = operational;
}
receivedpower *= relativistic.RedOrBlueShift(VesselList[counter], thisPart.vessel, state, out state);
if (useFacingVector)
{
receivedpower *= FractionalFlux(source, dest, thisPart);
}
if (relativistic.WarpDriveEngaged(thisPart))
{
receivedpower = 0;
state = warpEngaged;
}
}
else
{
receivedpower = 0;
VesselName = vesselNone;
}
}
else
{
receivedpower = 0;
VesselName = vesselNone;
}
status = state; count = counter;
}
public void Spherical(Part thisPart, bool Listening, float percentagePower, double recvSize,
double recvEfficiency, bool useSpotArea, bool useFacingVector, string state, out string status, out double received_power)
{
frames += 1;
if (frames == 30)
{
vesselFinder.SourceData(thisPart.vessel.GetDisplayName(), out VesselList, out excessList, out constantList, out targetList, out wavelengthList);
frames = 0;
}
received_power = 0;
if (VesselList.Count > 0)
{
if (Listening)
{
Vector3d dest = thisPart.vessel.GetWorldPos3D();
planetList.Clear();
// adds up all the received power values from all vessels in CorrectVesselList
for (int n = 0; n < VesselList.Count; n++)
{
if (targetList[n] == thisPart.vessel.GetDisplayName())
{
double excess2 = excessList[n]; double constant2 = constantList[n];
Vector3d source = VesselList[n].GetWorldPos3D();
double distance = Vector3d.Distance(source, dest);
double spotsize = (useSpotArea) ? Math.Pow(constant2 * distance / 2, 2) * 3.14 : constant2 * distance;
occlusion.IsOccluded(source, dest, wavelengthList[n], out CelestialBody celestial, out bool occluded);
// adding EC that has been received
if (recvSize < spotsize)
{
if (occluded == false)
{
received_power += ((recvSize / spotsize) * recvEfficiency * excess2 * (percentagePower / 100))
* relativistic.RedOrBlueShift(VesselList[n], thisPart.vessel, state, out state)
* ((useFacingVector) ? FractionalFlux(source, dest, thisPart) : 1);
}
}
else
{
if (occluded == false)
{
received_power += (recvEfficiency * excess2 * (percentagePower / 100))
* relativistic.RedOrBlueShift(VesselList[n], thisPart.vessel, state, out state)
* ((useFacingVector) ? FractionalFlux(source, dest, thisPart) : 1);
}
}
if (occluded)
{
planetList.Add(celestial);
}
}
}
if (planetList.Count > 0)
{
state = occludedby + planetList[planetList.Count - 1].GetDisplayName().TrimEnd('N', '^');
}
else
{
state = operational;
}
if (relativistic.WarpDriveEngaged(thisPart) & state != warpEngaged)
{
received_power = 0d;
state = warpEngaged;
}
}
}
status = state;
}
}
}