-
Notifications
You must be signed in to change notification settings - Fork 23
/
Ship.js
208 lines (139 loc) · 5.91 KB
/
Ship.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//@EliasHasle
/*
Notes:
For calculated values, I envision a lazy calculation pattern, where all properties involved in calculations are only accessed by specialized getters and setters, and calculated properties have some kind of needsUpdate flag or version number (that is set by any setters that will directly or indirectly affect the property). When, and only when, running the getter for the given property, the flag/version is checked, and if false (same version as in cache) the getter just returns the stored value. If true, the getter starts the calculation of the value, invoking other getters.
Suggested calculations to do:
- Inertia matrix (will need more detailed properties of parts).
*/
function Ship( specification ) {
JSONSpecObject.call( this, specification );
}
Ship.prototype = Object.create( JSONSpecObject.prototype );
Object.assign( Ship.prototype, {
constructor: Ship,
setFromSpecification: function ( specification ) {
this.attributes = specification.attributes || {};
this.structure = new Structure( specification.structure/*,this*/ );
//baseObjects and derivedObjects are arrays in the specification, but are objects (hashmaps) in the constructed ship object:
this.baseObjects = {};
for ( let i = 0; i < specification.baseObjects.length; i ++ ) {
let os = specification.baseObjects[ i ];
this.baseObjects[ os.id ] = new BaseObject( os );
}
this.derivedObjects = {};
for ( let i = 0; i < specification.derivedObjects.length; i ++ ) {
let os = specification.derivedObjects[ i ];
this.derivedObjects[ os.id ] = new DerivedObject( os, this.baseObjects );
}
this.designState = new ShipState( specification.designState );
return this;
},
getSpecification: function () {
let specification = {};
specification.attributes = this.attributes;
specification.structure = this.structure.getSpecification();
specification.baseObjects = Object.values( this.baseObjects ).map( o => o.getSpecification() );
specification.derivedObjects = Object.values( this.derivedObjects ).map( o => o.getSpecification() );
specification.designState = this.designState.getSpecification();
return specification;
},
//This should probably be separated in lightweight and deadweight
//Then this function should be replaced by a getDisplacement
getWeight: function ( shipState ) {
shipState = shipState || this.designState;
let components = [];
components.push(
this.structure.getWeight( this.designState )
);
//DEBUG
//console.log(components);
for ( let o of Object.values( this.derivedObjects ) ) {
components.push( o.getWeight( shipState ) );
}
var W = combineWeights( components );
//console.info("Calculated weight object: ", W);
return W;
},
calculateDraft: function ( shipState, epsilon = 0.001, rho = 1025 ) {
let w = this.getWeight( shipState );
let M = w.mass;
return this.structure.hull.calculateDraftAtMass( M, epsilon, rho );
},
//Separates between longitudinal and transverse GM
//To avoid confusion, no "default" GM or BM is specified in the output.
calculateStability: function ( shipState ) {
let w = this.getWeight( shipState );
let KG = w.cg.z;
let LCG = w.cg.x;
let T = this.structure.hull.calculateDraftAtMass( w.mass );
let { BMt, BMl, KB, LCB, LCF, LWL, BWL } = this.structure.hull.calculateAttributesAtDraft( T );
let GMt = KB + BMt - KG;
let GMl = KB + BMl - KG;
// avaiable just for small angles < 3°
// this calculation can be incorporated to Vessesl.js with no problem
let trim = ( LCB - LCG ) / GMl;
let draftfp = 0;
let draftap = 0;
let trimd = 0;
if ( trim < Math.tan( 3 * Math.PI / 180 ) ) {
draftfp = T - ( LWL - LCF ) * trim;
draftap = T + ( LCF ) * trim;
trimd = draftfp - draftap;
} else {
draftfp = null;
draftap = null;
trimd = null;
}
//change the trim for angles
trim = Math.atan( trim ) * 180 / Math.PI;
let heel = w.cg.y / GMt;
//change the hell for meters
heel *= BWL;
return { w, T, GMt, GMl, KB, BMt, BMl, KG, trim, draftfp, draftap, trimd, heel };
},
getFuelMass: function ( shipState ) {
shipState = shipState || this.designState;
let fuelMass = {};
fuelMass.totalMass = 0;
fuelMass.tankMass = {};
fuelMass.tankStates = {};
for ( let o of Object.values( this.derivedObjects ) ) {
if ( o.affiliations.group === "fuel tanks" ) {
fuelMass.tankStates[ o.id ] = shipState.getObjectState( o );
fuelMass.tankMass[ o.id ] = o.baseObject.weightInformation.contentDensity * o.baseObject.weightInformation.volumeCapacity * fuelMass.tankStates[ o.id ].fullness;
fuelMass.totalMass += fuelMass.tankMass[ o.id ];
}
}
return fuelMass;
},
subtractFuelMass: function ( mass, shipState ) {
shipState = shipState || this.designState;
var fuelMass = this.getFuelMass( shipState );
var totalFuel = fuelMass.totalMass;
var tankEntr = Object.entries( fuelMass.tankMass );
var fuelCap = 0;
for ( var tk = 0; tk < tankEntr.length; tk ++ ) {
var tkId = tankEntr[ tk ][ 0 ];
fuelCap += this.derivedObjects[ tkId ].baseObject.weightInformation.volumeCapacity * this.derivedObjects[ tkId ].baseObject.weightInformation.contentDensity;
}
// check if tanks have necessary fuel
if ( mass <= totalFuel ) { // if yes, subtract mass in the same proportion
for ( var tk = 0; tk < tankEntr.length; tk ++ ) {
var tkId = tankEntr[ tk ][ 0 ];
shipState.objectCache[ tkId ].state.fullness -= mass / fuelCap;
}
} else { // if not, make tanks empty
mass -= totalFuel;
for ( var tk = 0; tk < tankEntr.length; tk ++ ) {
var tkId = tankEntr[ tk ][ 0 ];
shipState.objectCache[ tkId ].state.fullness = 0;
}
console.error( "Vessel ran out of fuel before " + mass.toFixed( 2 ) + " tons were subtracted." );
}
// update related states. In the future, make this consistent with improved caching system
for ( var prop in shipState.objectCache ) {
shipState.objectCache[ prop ].thisStateVer ++;
}
shipState.version ++;
}
} );