forked from Kerbas-ad-astra/WildBlueTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeatManager.cs
More file actions
184 lines (155 loc) · 7.61 KB
/
HeatManager.cs
File metadata and controls
184 lines (155 loc) · 7.61 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using KSP.IO;
/*
Source code copyrighgt 2015, by Michael Billard (Angel-125)
License: CC BY-NC-SA 4.0
License URL: https://creativecommons.org/licenses/by-nc-sa/4.0/
If you want to use this code, give me a shout on the KSP forums! :)
Wild Blue Industries is trademarked by Michael Billard and may be used for non-commercial purposes. All other rights reserved.
Note that Wild Blue Industries is a ficticious entity
created for entertainment purposes. It is in no way meant to represent a real entity.
Any similarity to a real entity is purely coincidental.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace WildBlueIndustries
{
public class PartTargetTemp
{
public Part part;
public double targetTemp;
}
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class HeatManager : MonoBehaviour
{
private const float kRoomTemperature = 293;
public List<ModuleRadiator> radiators = new List<ModuleRadiator>();
public List<PartTargetTemp> partTargetTemps = new List<PartTargetTemp>();
protected Vessel activeVessel = null;
protected int vesselPartCount = -1;
//By default, the target temperature is 49% of the part's maximum temperature.
//This is just under the game's temperature warning gauge.
//This value can be configured in the Cooldown config file.
protected float maxTempPercent = 0.49f;
public void Start()
{
string value;
ConfigNode[] nodes = GameDatabase.Instance.GetConfigNodes("COOLDOWN");
if (nodes != null)
{
value = nodes[0].GetValue("maxTempPercent");
if (string.IsNullOrEmpty(value) == false)
maxTempPercent = float.Parse(value);
}
}
public void FixedUpdate()
{
//The manager is only usable during flight.
if (HighLogic.LoadedSceneIsFlight == false)
return;
try
{
//Get active vessel
if (FlightGlobals.ActiveVessel != activeVessel)
{
activeVessel = FlightGlobals.ActiveVessel;
if (activeVessel == null)
return;
//If the part count has changed, such as when the vessel breaks up, docks, or undocks, then
//find all the radiators and non-radiator parts and their target temperatures.
FindPartTemperatures();
}
//Now, manage the heat
ManageHeat();
//Since ModuleRadiator derives from ModuleDeployableSolarPanel, we don't get a fixed update to override.
//So we'll lend a hand here...
foreach (ModuleRadiator radiator in radiators)
radiator.UpdateState();
}
catch (Exception ex)
{
Debug.Log("Heat Manager got an exception: " + ex);
}
}
public void FindPartTemperatures()
{
if (activeVessel.Parts.Count != vesselPartCount)
{
vesselPartCount = activeVessel.Parts.Count;
radiators.Clear();
partTargetTemps.Clear();
ModuleRadiator radiator;
ModuleTargetTemp moduleTargetTemp;
foreach (Part part in activeVessel.Parts)
{
//If the part has a radiator then add it to the radiator's list.
radiator = part.FindModuleImplementing<ModuleRadiator>();
if (radiator != null)
{
radiators.Add(radiator);
radiator.radiatorDestroyedDelegate = RadiatorDestroyed;
continue;
}
//It isn't a radiator, if it's physicsless then keep going
if (part.PhysicsSignificance == 1)
continue;
//Create a new PartTargetTemp
PartTargetTemp partTargetTemp = new PartTargetTemp();
partTargetTemps.Add(partTargetTemp);
partTargetTemp.part = part;
//If the part has a ModuleTargetTemp then use its temperature
moduleTargetTemp = part.FindModuleImplementing<ModuleTargetTemp>();
if (moduleTargetTemp != null)
partTargetTemp.targetTemp = moduleTargetTemp.targetTemperature;
//If the part is crewed then the target temperature is room temperature.
else if (part.CrewCapacity > 0)
partTargetTemp.targetTemp = kRoomTemperature;
//By default, the part will be kept at a percentage of its maximum temperature.
//This can be configured in the Cooldown config file
else
partTargetTemp.targetTemp = part.maxTemp * maxTempPercent;
}
}
}
public void ManageHeat()
{
//Amount of thermal energy in the system, defined as thermal mass * temperature
double partThermalEnergy = 0;
//Thermal energy at the target temperature.
//Some parts may run hot, others want to stay cool.
//Either way we want to make sure that the part doesn't get too cold.
double partThermalAtTargetTemp = 0;
//Total amount of thermal energy that may be transferred to the radiators.
double partThermalTransfer = 0f;
//Amount of thermal energy to transfer per active radiator
double thermalTransferPerRadiator = 0f;
foreach (PartTargetTemp partTargetTemp in partTargetTemps)
{
//Calculate the thermal energy transfer (thermal energy = kJ/K * K = kJ)
partThermalEnergy = partTargetTemp.part.thermalMass * partTargetTemp.part.temperature;
partThermalAtTargetTemp = partTargetTemp.part.thermalMass * partTargetTemp.targetTemp;
partThermalTransfer = partThermalEnergy - partThermalAtTargetTemp;
thermalTransferPerRadiator = partThermalTransfer / radiators.Count;
if (thermalTransferPerRadiator < 0.001f)
continue;
//Now, distribute the heat to all active radiators.
partThermalTransfer = 0f; //We'll use this to know how much heat was actually transferred.
foreach (ModuleRadiator radiator in radiators)
partThermalTransfer += radiator.TransferHeat(thermalTransferPerRadiator);
//Transfer the heat out of the part
//Practice conservation of heat!
if (partThermalTransfer > 0.001f)
partTargetTemp.part.AddThermalFlux(-partThermalTransfer);
}
}
public void RadiatorDestroyed(ModuleRadiator doomed)
{
//Remove the doomed radiator
if (radiators.Contains(doomed))
radiators.Remove(doomed);
}
}
}