2016-06-28 16:13:28 +02:00
|
|
|
const PRNG = require('./prng');
|
|
|
|
const Planet = require('./planet');
|
|
|
|
const utils = require('./utils');
|
|
|
|
const data = require('./data.json');
|
|
|
|
|
|
|
|
function Star(name, seed, position) {
|
2016-09-18 15:44:47 +02:00
|
|
|
var pseudoRandom = new PRNG(seed),
|
|
|
|
spectralClass = pseudoRandom.pick(["black hole", "O", "B", "A", "F", "G", "K", "M"], [.5, 0.0001, 0.2, 1, 3, 8, 12, 20]),
|
2016-06-28 22:51:12 +02:00
|
|
|
spectralIndex = pseudoRandom.range(0, 9),
|
|
|
|
stellarTemplate = data.starTypes[spectralClass];
|
|
|
|
|
|
|
|
this.name = name;
|
|
|
|
this.seed = seed;
|
|
|
|
this.position = position;
|
2016-09-18 15:44:47 +02:00
|
|
|
if (spectralClass != 'black hole')
|
|
|
|
this.spectralType = spectralClass + spectralIndex;
|
|
|
|
else
|
|
|
|
this.spectralType = spectralClass;
|
2016-06-28 22:51:12 +02:00
|
|
|
this.luminosity = stellarTemplate.luminosity * (4 / (spectralIndex + 2)) * 50;
|
|
|
|
this.radius = Math.sqrt(this.luminosity);
|
|
|
|
this.numberOfPlanets = pseudoRandom.range(stellarTemplate.planets[0], stellarTemplate.planets[1]);
|
2016-09-18 15:44:47 +02:00
|
|
|
this.numberOfStations = pseudoRandom.range(0, this.numberOfPlanets / 20);
|
2016-06-28 22:51:12 +02:00
|
|
|
this.planetSeed = pseudoRandom.range(0, 1000000);
|
|
|
|
this.color = stellarTemplate.color;
|
2016-06-28 23:01:39 +02:00
|
|
|
this.planets = this.generatePlanets();
|
2016-06-28 22:51:12 +02:00
|
|
|
|
|
|
|
return this;
|
2016-06-28 16:13:28 +02:00
|
|
|
}
|
2016-06-28 22:51:12 +02:00
|
|
|
|
2016-08-11 13:35:36 +02:00
|
|
|
Star.prototype.generatePlanets = function () {
|
2016-06-28 22:51:12 +02:00
|
|
|
var planets = [],
|
|
|
|
pseudoRandom = new PRNG(this.planetSeed),
|
|
|
|
radius_min = 0.4 * pseudoRandom.realRange(0.5, 2),
|
|
|
|
radius_max = 50 * pseudoRandom.realRange(0.5, 2),
|
|
|
|
total_weight = (Math.pow(this.numberOfPlanets, 2) + this.numberOfPlanets) * 0.5,
|
2016-08-11 13:35:36 +02:00
|
|
|
r = radius_min
|
2016-06-28 22:51:12 +02:00
|
|
|
|
|
|
|
for (var i = 0; i < this.numberOfPlanets; i++) {
|
|
|
|
r += i / total_weight * pseudoRandom.realRange(0.5, 1) * (radius_max - radius_min);
|
2016-08-11 13:35:36 +02:00
|
|
|
planets.push(new Planet(utils.kappatalize(this.name) + "-" + utils.romanNumeral(i + 1), pseudoRandom.range(0, 1000000), r, this.luminosity / Math.pow(r, 2)));
|
2016-06-28 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 15:44:47 +02:00
|
|
|
this.divideStations(planets, this.seed);
|
2016-08-11 13:35:36 +02:00
|
|
|
|
2016-06-28 22:51:12 +02:00
|
|
|
return planets;
|
2016-06-28 16:13:28 +02:00
|
|
|
}
|
2016-06-28 22:51:12 +02:00
|
|
|
|
2016-09-18 15:44:47 +02:00
|
|
|
Star.prototype.divideStations = function (planets, seed) {
|
2016-08-11 13:35:36 +02:00
|
|
|
var stations = this.numberOfStations || 0;
|
|
|
|
var i = planets.length - 1;
|
2016-09-18 15:44:47 +02:00
|
|
|
var pseudoRandom = new PRNG(seed);
|
2016-08-11 13:35:36 +02:00
|
|
|
|
|
|
|
for (i = 0; i < stations; i++) {
|
|
|
|
var arr = []
|
|
|
|
for (planet of planets) {
|
|
|
|
arr.push(100 / planets.length);
|
|
|
|
}
|
|
|
|
var planet = pseudoRandom.pick(planets, arr);
|
|
|
|
planet.station = planet.generateStation(pseudoRandom);
|
|
|
|
planet.station.seed = planet.seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-28 16:13:28 +02:00
|
|
|
module.exports = Star;
|