Added a test command and added npm stuff

This commit is contained in:
Bram van der Veen 2016-06-28 21:25:32 +02:00
parent c01f1ce27b
commit f4e1426efe
4 changed files with 85 additions and 44 deletions

7
LICENSE.md Normal file
View file

@ -0,0 +1,7 @@
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -10,56 +10,60 @@ var names = [];
var stars = []; var stars = [];
var planets = []; var planets = [];
function generate() { function generate(seed, amount) {
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 = { if (!amount) amount = 1000;
badwords: 0, if (!seed) seed = 1234;
duplicates: 0 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 = {
}, totalCount = 1000, i, position; badwords: 0,
duplicates: 0
}, totalCount = amount, i, position;
pseudoRandom = new PRNG('96aa48'); pseudoRandom = new PRNG(seed);
for (i = 0; i < totalCount; i++) { for (i = 0; i < totalCount; i++) {
var number_of_syllables = Math.floor(pseudoRandom.value() * 2 + 2), new_name; var number_of_syllables = Math.floor(pseudoRandom.value() * 2 + 2), new_name;
while (true) { while (true) {
new_name = utils.random_name(pseudoRandom, number_of_syllables); new_name = utils.random_name(pseudoRandom, number_of_syllables);
if (names.indexOf(new_name) >= 0) { if (names.indexOf(new_name) >= 0) {
rejects.duplicates++; rejects.duplicates++;
} else if (data.badwords.indexOf(new_name) >= 0 || data.badwords.indexContains(new_name) >= 0) { } else if (data.badwords.indexOf(new_name) >= 0 || data.badwords.indexContains(new_name) >= 0) {
rejects.badwords++ rejects.badwords++
} else { } else {
break; break;
} }
} }
names.push(new_name); names.push(new_name);
var r = pseudoRandom.realRange(min_radius, max_radius); var r = pseudoRandom.realRange(min_radius, max_radius);
var theta = spiral_b * Math.log(r / max_radius) + pseudoRandom.gaussrandom(scatter_theta); var theta = spiral_b * Math.log(r / max_radius) + pseudoRandom.gaussrandom(scatter_theta);
r += pseudoRandom.gaussrandom(scatter_radius); r += pseudoRandom.gaussrandom(scatter_radius);
// assign to a spiral arm // assign to a spiral arm
theta += pseudoRandom.range(0, spiral_arms - 1) * Math.PI * 2 / spiral_arms; theta += pseudoRandom.range(0, spiral_arms - 1) * Math.PI * 2 / spiral_arms;
position = { position = {
x: Math.cos(theta) * r * 100, x: Math.cos(theta) * r * 100,
y: Math.sin(theta) * r * 100, y: Math.sin(theta) * r * 100,
z: pseudoRandom.gaussrandom(thickness * 0.5) z: pseudoRandom.gaussrandom(thickness * 0.5)
}; };
stars.push(new Star(new_name,pseudoRandom.range(1, 100000),position)); stars.push(new Star(new_name,pseudoRandom.range(1, 100000),position));
} }
stars.sort(function(a, b) { stars.sort(function(a, b) {
return a.name > b.name ? 1 : (a.name < b.name ? -1 : 0); return a.name > b.name ? 1 : (a.name < b.name ? -1 : 0);
}); });
let temp = stars.pop() let temp = stars.pop()
Object.assign(temp, temp.description()); Object.assign(temp, temp.description());
for (star of stars) { for (star of stars) {
Object.assign(star, star.description()); Object.assign(star, star.description());
} }
fs.writeFileSync(__dirname + '/galaxy.json', JSON.stringify(stars, null, 2)) return stars;
// fs.writeFileSync(__dirname + '/galaxy.json', JSON.stringify(stars, null, 2))
// console.log('names rejected', rejects);
console.log('names rejected', rejects); // console.log('generate elapsed', (((new Date()).getTime() - start) * 0.001).toFixed(3) + "s");
console.log('generate elapsed', (((new Date()).getTime() - start) * 0.001).toFixed(3) + "s");
} }
generate(); module.exports = {
"generate": generate
}

27
package.json Normal file
View file

@ -0,0 +1,27 @@
{
"name": "galaxygen",
"version": "1.0.0",
"description": "A galaxy generator based on loewald.com/galaxy",
"main": "index.js",
"scripts": {
"test": "node index test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/96aa48/galaxygen.git"
},
"keywords": [
"galaxy",
"generator",
"loewald",
"lore",
"universe",
"prng"
],
"author": "Bram van der Veen",
"license": "MIT",
"bugs": {
"url": "https://github.com/96aa48/galaxygen/issues"
},
"homepage": "https://github.com/96aa48/galaxygen#readme"
}

3
test.js Normal file
View file

@ -0,0 +1,3 @@
const galaxy = require('./index');
console.log(galaxy.generate());