Reworked star.js for a better dataset

This commit is contained in:
Bram van der Veen 2016-06-28 22:51:12 +02:00
parent 95e5118e14
commit 8b9517d991

78
star.js
View file

@ -4,51 +4,51 @@ const utils = require('./utils');
const data = require('./data.json'); const data = require('./data.json');
function Star(name, seed, position) { function Star(name, seed, position) {
this.name = name; var pseudoRandom = new PRNG(this.seed),
this.seed = seed; spectralClass = pseudoRandom.pick(["O", "B", "A", "F", "G", "K", "M"], [0.0001, 0.2, 1, 3, 8, 12, 20]),
this.position = position; spectralIndex = pseudoRandom.range(0, 9),
return this; stellarTemplate = data.starTypes[spectralClass];
}
Star.prototype.detail = function() { this.name = name;
var pseudoRandom = new PRNG(this.seed) this.seed = seed;
, detail = {} this.position = position;
, spectralClass = pseudoRandom.pick(["O", "B", "A", "F", "G", "K", "M"], [0.0001, 0.2, 1, 3, 8, 12, 20]) this.spectralType = spectralClass + spectralIndex;
, spectralIndex = pseudoRandom.range(0, 9) this.luminosity = stellarTemplate.luminosity * (4 / (spectralIndex + 2)) * 50;
, stellarTemplate = data.starTypes[spectralClass]; this.radius = Math.sqrt(this.luminosity);
detail.spectralType = spectralClass + spectralIndex; this.numberOfPlanets = pseudoRandom.range(stellarTemplate.planets[0], stellarTemplate.planets[1]);
detail.luminosity = stellarTemplate.luminosity * (4 / (spectralIndex + 2)) * 50; this.planetSeed = pseudoRandom.range(0, 1000000);
detail.radius = Math.sqrt(detail.luminosity); this.color = stellarTemplate.color;
detail.numberOfPlanets = pseudoRandom.range(stellarTemplate.planets[0], stellarTemplate.planets[1]);
detail.planetSeed = pseudoRandom.range(0, 1000000); return this;
detail.template = stellarTemplate;
return detail;
} }
Star.prototype.planets = function() { Star.prototype.planets = function() {
var detail = this.detail() var planets = [],
, planets = [] pseudoRandom = new PRNG(this.planetSeed),
, pseudoRandom = new PRNG(detail.planetSeed) radius_min = 0.4 * pseudoRandom.realRange(0.5, 2),
, radius_min = 0.4 * pseudoRandom.realRange(0.5, 2) radius_max = 50 * pseudoRandom.realRange(0.5, 2),
, radius_max = 50 * pseudoRandom.realRange(0.5, 2) total_weight = (Math.pow(this.numberOfPlanets, 2) + this.numberOfPlanets) * 0.5,
, total_weight = (Math.pow(detail.numberOfPlanets, 2) + detail.numberOfPlanets) * 0.5 r = radius_min;
, r = radius_min;
for (var i = 0; i < detail.numberOfPlanets; i++) { for (var i = 0; i < this.numberOfPlanets; i++) {
r += i / total_weight * pseudoRandom.realRange(0.5, 1) * (radius_max - radius_min); r += i / total_weight * pseudoRandom.realRange(0.5, 1) * (radius_max - radius_min);
planets.push(new Planet(utils.kappatalize(this.name) + "-" + utils.romanNumeral(i + 1),pseudoRandom.range(0, 100000),r,detail.luminosity / Math.pow(r, 2))); planets.push(new Planet(utils.kappatalize(this.name) + "-" + utils.romanNumeral(i + 1), pseudoRandom.range(0, 100000), r, this.luminosity / Math.pow(r, 2)));
} }
return planets;
return planets;
} }
Star.prototype.description = function() { Star.prototype.description = function() {
let planets = []; let planets = [];
for (planet of this.planets()) { for (planet of this.planets()) {
planet.description = planet.description(); planet.description = planet.description();
planets.push(planet); planets.push(planet);
} }
return { return {
"detail": this.detail(), "planets": planets
"planets": planets }
}
} }
module.exports = Star; module.exports = Star;