/
SimPart.cs
183 lines (152 loc) · 5.88 KB
/
SimPart.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
#nullable enable
using System;
using System.Collections.Generic;
using System.Text;
using MechJebLib.Utils;
namespace MechJebLib.Simulations
{
public class SimPart
{
private static readonly ObjectPool<SimPart> _pool = new ObjectPool<SimPart>(New, Clear);
public readonly List<SimPartModule> Modules = new List<SimPartModule>();
public readonly List<SimPart> CrossFeedPartSet = new List<SimPart>();
public readonly List<SimPart> Links = new List<SimPart>();
public readonly Dictionary<int, SimResource> Resources = new Dictionary<int, SimResource>();
private readonly Dictionary<int, double> _resourceDrains = new Dictionary<int, double>();
public int DecoupledInStage;
public bool StagingOn;
public int InverseStage;
public SimVessel Vessel;
public string Name;
public bool ActivatesEvenIfDisconnected;
public bool IsThrottleLocked;
public int ResourcePriority;
public double ResourceRequestRemainingThreshold;
public bool IsEnabled;
public double Mass;
public double DryMass;
public double CrewMass;
public double ModulesStagedMass;
public double ModulesUnstagedMass;
public double DisabledResourcesMass;
public double EngineResiduals;
public bool IsLaunchClamp;
public bool IsEngine;
public bool IsSepratron => IsEngine && IsThrottleLocked && ActivatesEvenIfDisconnected && InverseStage == DecoupledInStage;
private SimPart()
{
// Always set in Borrow()
Vessel = null!;
Name = null!;
}
public void UpdateMass()
{
if (IsLaunchClamp)
{
Mass = 0;
return;
}
Mass = DryMass + CrewMass + DisabledResourcesMass;
Mass += Vessel.CurrentStage <= InverseStage ? ModulesStagedMass : ModulesUnstagedMass;
//ModulesCurrentMass = Mass;
foreach (SimResource resource in Resources.Values)
Mass += resource.Amount * resource.Density;
}
public void Dispose()
{
foreach (SimPartModule m in Modules)
m.Dispose();
_pool.Release(this);
}
public static SimPart Borrow(SimVessel vessel, string name)
{
SimPart part = _pool.Borrow();
part.Vessel = vessel;
part.Name = name;
return part;
}
private static SimPart New()
{
return new SimPart();
}
private static void Clear(SimPart p)
{
p.Modules.Clear();
p.Links.Clear();
p.CrossFeedPartSet.Clear();
p.Resources.Clear();
p._resourceDrains.Clear();
p.Vessel = null!;
p.IsLaunchClamp = false;
p.IsEngine = false;
p.IsThrottleLocked = false;
}
public bool TryGetResource(int resourceId, out SimResource resource)
{
return Resources.TryGetValue(resourceId, out resource);
}
public void ApplyResourceDrains(double dt)
{
foreach (int id in _resourceDrains.Keys)
Resources[id] = Resources[id].Drain(dt * _resourceDrains[id]);
}
public void UpdateResourceResidual(double residual, int resourceId)
{
if (!Resources.TryGetValue(resourceId, out SimResource resource))
return;
resource.Residual = Math.Max(resource.Residual, residual);
Resources[resourceId] = resource;
}
public double ResidualThreshold(int resourceId)
{
return Resources[resourceId].ResidualThreshold + ResourceRequestRemainingThreshold;
}
public void ClearResourceDrains()
{
_resourceDrains.Clear();
}
public void AddResourceDrain(int resourceId, double resourceConsumption)
{
if (_resourceDrains.TryGetValue(resourceId, out double resourceDrain))
_resourceDrains[resourceId] = resourceDrain + resourceConsumption;
else
_resourceDrains.Add(resourceId, resourceConsumption);
}
public double ResourceMaxTime()
{
double maxTime = double.MaxValue;
foreach (SimResource resource in Resources.Values)
{
if (resource.Free)
continue;
if (resource.Amount <= ResourceRequestRemainingThreshold)
continue;
if (!_resourceDrains.TryGetValue(resource.Id, out double resourceDrain))
continue;
double dt = (resource.Amount - resource.ResidualThreshold) / resourceDrain;
maxTime = Math.Min(maxTime, dt);
}
return maxTime;
}
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine($"{Name}: ");
sb.Append(" Neighbors:");
for (int i = 0; i < Links.Count; i++)
sb.Append($" {Links[i].Name}");
sb.AppendLine();
//sb.Append(" CrossFeedPartSet:");
//for (int i = 0; i < CrossFeedPartSet.Count; i++)
// sb.Append($" {CrossFeedPartSet[i].Name}");
//sb.AppendLine();
sb.Append(" Resources:");
foreach (SimResource resource in Resources.Values)
sb.Append($" {resource.Id}={resource.Amount}*{resource.Density}");
sb.AppendLine();
sb.Append($" DecoupledInStage: {DecoupledInStage} InverseStage: {InverseStage}");
sb.AppendLine();
return sb.ToString();
}
}
}