This repository has been archived on 2024-02-25. You can view files and clone it, but cannot push or open issues or pull requests.
galaxygen/index.js
Bram van der Veen 9c8274d227 First commit
2016-06-28 16:14:00 +02:00

92 lines
3 KiB
JavaScript

const fs = require('fs');
const Planet = require('./planet');
const PRNG = require('./prng');
const Star = require('./star');
const MersenneTwister = require('./twister');
const utils = require('./utils');
const data = require('./data.json');
var names = [];
var stars = [];
var planets = [];
function generate() {
var spiral_arms = 2, spiral_angle_degrees = 360, min_radius = 0.05, max_radius = 0.9, thickness = 0.1, scatter_theta = Math.PI / spiral_arms * 0.2, scatter_radius = min_radius * 0.4, spiral_b = spiral_angle_degrees / Math.PI * min_radius / max_radius, start = (new Date()).getTime(), names = [], rejects = {
badwords: 0,
duplicates: 0
}, totalCount = 1000, i, position;
pseudoRandom = new PRNG('96aa48');
for (i = 0; i < totalCount; i++) {
var number_of_syllables = Math.floor(pseudoRandom.value() * 2 + 2), new_name;
while (true) {
new_name = utils.random_name(pseudoRandom, number_of_syllables);
if (names.indexOf(new_name) >= 0) {
rejects.duplicates++;
} else if (data.badwords.indexOf(new_name) >= 0 || data.badwords.indexContains(new_name) >= 0) {
rejects.badwords++
} else {
break;
}
}
names.push(new_name);
var r = pseudoRandom.realRange(min_radius, max_radius);
var theta = spiral_b * Math.log(r / max_radius) + pseudoRandom.gaussrandom(scatter_theta);
r += pseudoRandom.gaussrandom(scatter_radius);
// assign to a spiral arm
theta += pseudoRandom.range(0, spiral_arms - 1) * Math.PI * 2 / spiral_arms;
position = {
x: Math.cos(theta) * r * 100,
y: Math.sin(theta) * r * 100,
z: pseudoRandom.gaussrandom(thickness * 0.5)
};
stars.push(new Star(new_name,pseudoRandom.range(1, 100000),position));
}
stars.sort(function(a, b) {
return a.name > b.name ? 1 : (a.name < b.name ? -1 : 0);
});
let temp = stars.pop()
Object.assign(temp, temp.description());
for (star of stars) {
Object.assign(star, star.description());
}
fs.writeFileSync(__dirname + '/galaxy.json', JSON.stringify(stars, null, 2))
console.log('names rejected', rejects);
console.log('generate elapsed', (((new Date()).getTime() - start) * 0.001).toFixed(3) + "s");
}
Array.prototype.indexContains = function(word) {
for (var idx = 0; idx < this.length; idx++) {
var test = this[idx];
if (test.indexOf(word) >= 0 || word.indexOf(test) >= 0) {
return idx;
}
}
return -1;
}
Math.clamp = function(a, min, max) {
return a < min ? min : (a > max ? max : a);
};
Array.prototype.insertAt = function(where, what) {
if (where < 0) {
this.splice(0, 0, what);
} else {
var tail = this.splice(where);
this.push(what)
for (var i = 0; i < tail.length; i++) {
this.push(tail[i]);
}
}
}
generate();