Files
KSP-MGA-Planner/dist/main/solvers/sequence.js
Krafpy 8da935b047 2D physics fix and error handling consistency
- Reimplemented 2D orbital mechanics functions in physics-2d.ts
and encaspulated inside a `Physics2D` namespace.
- Deleted physics.ts and moved the required functions into physics-3d.ts
Further cleaning and reimplemntation will be done on the physics-3d.ts
content.
- Forced consistency on error handling : every raised error throws
an `Error` object.
- Commented sections relative to 2D physics and sequence generation.
2021-12-19 17:19:30 +01:00

46 lines
1.5 KiB
JavaScript

export class FlybySequence {
constructor(system, ids) {
this.ids = ids;
this.bodies = [];
for (const id of ids) {
this.bodies.push(system.bodyFromId(id));
}
this.length = this.bodies.length;
const getSubstr = (i) => this.bodies[i].name.substring(0, 2);
let str = getSubstr(0);
for (let i = 1; i < this.length; i++) {
str += "-" + getSubstr(i);
}
this.seqString = str;
}
static fromString(str, system) {
str = str.trim();
const initials = str.split('-');
const ids = [];
let attractor = 0;
for (let i = 0; i < initials.length; i++) {
let valid = false;
for (const body of system.orbiting) {
if (body.name.substring(0, 2) == initials[i]) {
if (i == 0) {
attractor = body.attractor.id;
}
else if (body.attractor.id != attractor) {
throw new Error("All bodies of the sequence must orbit around the same body.");
}
ids.push(body.id);
valid = true;
break;
}
}
if (!valid) {
throw new Error("Invalid custom sequence input.");
}
}
if (ids.length <= 1) {
throw new Error("The sequence must contain at least two bodies.");
}
return new FlybySequence(system, ids);
}
}