diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..02f8973 --- /dev/null +++ b/LICENSE.md @@ -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. diff --git a/index.js b/index.js index fe646ae..ab0c358 100644 --- a/index.js +++ b/index.js @@ -10,56 +10,60 @@ 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; +function generate(seed, amount) { + if (!amount) amount = 1000; + if (!seed) seed = 1234; + 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 = amount, i, position; - pseudoRandom = new PRNG('96aa48'); + pseudoRandom = new PRNG(seed); - 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); - }); + 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() + let temp = stars.pop() - Object.assign(temp, temp.description()); + Object.assign(temp, temp.description()); - for (star of stars) { - Object.assign(star, star.description()); - } + for (star of stars) { + 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('generate elapsed', (((new Date()).getTime() - start) * 0.001).toFixed(3) + "s"); + // console.log('names rejected', rejects); + // console.log('generate elapsed', (((new Date()).getTime() - start) * 0.001).toFixed(3) + "s"); } -generate(); +module.exports = { + "generate": generate +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f9e9e6f --- /dev/null +++ b/package.json @@ -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" +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..829b60f --- /dev/null +++ b/test.js @@ -0,0 +1,3 @@ +const galaxy = require('./index'); + +console.log(galaxy.generate());