/
AscentBuilder.cs
156 lines (127 loc) · 6.17 KB
/
AscentBuilder.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
/*
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/
using System.Collections.Generic;
using MechJebLib.Primitives;
using MechJebLib.Utils;
using static MechJebLib.Utils.Statics;
#nullable enable
namespace MechJebLib.PVG
{
public partial class Ascent
{
public class AscentBuilder
{
public readonly List<Phase> _phases = new List<Phase>();
public V3 _r0 { get; private set; }
public V3 _v0 { get; private set; }
public V3 _u0 { get; private set; }
public double _t0 { get; private set; }
public double _mu { get; private set; }
public double _rbody { get; private set; }
public double _apR { get; private set; }
public double _peR { get; private set; }
public double _attR { get; private set; }
public double _incT { get; private set; }
public double _lanT { get; private set; }
public double _fpaT { get; private set; }
public double _hT { get; private set; }
public bool _attachAltFlag { get; private set; }
public bool _lanflag { get; private set; }
public bool _fixedBurnTime { get; private set; }
public int _optimizedPhase { get; private set; }
public int _optimizedCoastPhase { get; private set; } = -1;
public Solution? _solution { get; private set; }
public AscentBuilder AddStageUsingFinalMass(double m0, double mf, double isp, double bt, int kspStage, int mjPhase,
bool optimizeTime = false,
bool unguided = false)
{
Print(
$"[MechJebLib.AscentBuilder] AddStageUsingFinalMass({m0}, {mf}, {isp}, {bt}, {kspStage}, {(optimizeTime ? "true" : "false")}, {(unguided ? "true" : "false")})");
_phases.Add(Phase.NewStageUsingFinalMass(m0, mf, isp, bt, kspStage, mjPhase, optimizeTime, unguided));
if (optimizeTime)
_optimizedPhase = _phases.Count - 1;
return this;
}
public AscentBuilder AddStageUsingThrust(double m0, double thrust, double isp, double bt, int kspStage, int mjPhase,
bool optimizeTime = false,
bool unguided = false)
{
Print(
$"[MechJebLib.AscentBuilder] AddStageUsingThrust({m0}, {thrust}, {isp}, {bt}, {kspStage}, {(optimizeTime ? "true" : "false")}, {(unguided ? "true" : "false")})");
_phases.Add(Phase.NewStageUsingThrust(m0, thrust, isp, bt, kspStage, mjPhase, optimizeTime, unguided));
if (optimizeTime)
_optimizedPhase = _phases.Count - 1;
return this;
}
public AscentBuilder AddFixedCoast(double m0, double ct, int kspStage, int mjPhase)
{
Print($"[MechJebLib.AscentBuilder] AddFixedCoast({m0}, {ct}, {kspStage})");
_phases.Add(Phase.NewFixedCoast(m0, ct, kspStage, mjPhase));
return this;
}
public AscentBuilder AddOptimizedCoast(double m0, double mint, double maxt, int kspStage, int mjPhase)
{
Print($"[MechJebLib.AscentBuilder] AddOptimizedCoast({m0}, {mint}, {maxt}, {kspStage})");
_phases.Add(Phase.NewOptimizedCoast(m0, mint, maxt, kspStage, mjPhase));
_optimizedCoastPhase = _phases.Count - 1;
return this;
}
public AscentBuilder Initial(V3 r0, V3 v0, V3 u0, double t0, double mu, double rbody)
{
Check.NonZeroFinite(r0);
Check.NonZeroFinite(v0);
Check.Finite(t0);
Check.PositiveFinite(mu);
Check.PositiveFinite(rbody);
Print($"[MechJebLib.AscentBuilder] Initial({r0}, {v0}, {u0}, {t0}, {mu}, {rbody})");
_r0 = r0;
_v0 = v0;
_u0 = u0.normalized;
_t0 = t0;
_mu = mu;
_rbody = rbody;
return this;
}
public Ascent Build()
{
var ascent = new Ascent(this);
return ascent;
}
// This magical interface needs to be busted up
public AscentBuilder SetTarget(double peR, double apR, double attR, double inclination, double lan, double fpa, bool attachAltFlag,
bool lanflag)
{
Print(
$"[MechJebLib.AscentBuilder] SetTarget({peR}, {apR}, {attR}, {inclination}, {lan}, {fpa}, {(attachAltFlag ? "true" : "false")}, {(lanflag ? "true" : "false")})");
_peR = peR;
_apR = apR;
_attR = attR;
_incT = inclination;
_lanT = lan;
_fpaT = fpa;
_attachAltFlag = attachAltFlag;
_lanflag = lanflag;
return this;
}
public AscentBuilder OldSolution(Solution solution)
{
Print("[MechJebLib.AscentBuilder] OldSolution(<solution>)");
_solution = solution;
return this;
}
public AscentBuilder FixedBurnTime(bool fixedBurnTime)
{
Print($"[MechJebLib.AscentBuilder] FixedBurnTime({(fixedBurnTime ? "true" : "false")})");
_fixedBurnTime = fixedBurnTime;
return this;
}
public AscentBuilder TerminalConditions(double hT)
{
_hT = hT;
return this;
}
}
}
}