Skip to content

Instantly share code, notes, and snippets.

@colgraff
Last active November 18, 2022 02:55
Show Gist options
  • Save colgraff/5e850b2a6333610c2921cb6b7f1d091d to your computer and use it in GitHub Desktop.
Save colgraff/5e850b2a6333610c2921cb6b7f1d091d to your computer and use it in GitHub Desktop.
Star Wars D20 Planet Name Randomizer
const syllables = ["ad","al","alm","am","an","ant","arb","as","bak","ban","bar","bel","bes","bil","bin","bon","bor","bril","cer","com","cor","cron","dag","dan","dar","dor","dur","en","end","er","fed","fir","gal","gam","gan","gess","gli","glo","goh","gul","hon","is","jum","kal","kam","kar","kes","kom","kur","lan","les","lid","lor","us","mel","mer","mim","myr","nab","nal","nat","nik","nog","nub","om","on","ond","phin","pin","rax","rer","rif","rin","ris","rish","ron","rur","rut","ryl","sav","sel","sen","sem","sul","sum","tal","tan","tel","tis","top","tro","tros","um","val","var","ver","wat","yin","zel","zoc"];
const suffixes = ["a","aan","ar","as","ea","eer","i","ia","in","ine","ior","ir","is","oo","os","oth","u","uk","yi","yn"];
const nameLength = [1,2,2,3,3,4];
const spacing = [[1,,,],[,1,,],[1,1,,],[,,1,],[1,,1,],[,1,1,],[1,1,1,]];
Math.randomInt = function(max, min = 0) { return Math.floor(Math.random()*(max - min + 1)) + min; };
String.prototype.capitalize = function() { return this.charAt(0).toUpperCase() + this.slice(1); };
Array.prototype.random = function() { return this[Math.randomInt(this.length - 1)]; };
Array.prototype.joinCapitalize = function(separator = "") { return this.join(separator).capitalize(); };
Array.prototype.interleave = function(other) {
return this.reduce((combined, element, position) => combined.concat(element, other[position]), []);
};
Number.prototype.toRoman = function() {
if (this < 1 || this > 10) { return ""; }
return ["","I","II","III","IV","V","VI","VII","VIII","IX","X"][this];
};
function punctuate(components) {
if (components.length < 2 || components.length > 4) { return components; }
const masks = spacing.slice(0, Math.pow(2, components.length - 1) - 1);
const punctuation = masks.random().map((value) => value?["'", "-"].random():"");
return components.interleave(punctuation);
}
function generatePlanetName() {
let components = Array.from({length:nameLength.random()}, () => syllables.random());
const special = Math.randomInt(20, 1);
if (special <= 7) { return components.joinCapitalize(); }
if (special <= 12) { return components.concat([suffixes.random()]).joinCapitalize(); }
if (special <= 15) { return punctuate(components).joinCapitalize(); }
if (special <= 19) { return components.joinCapitalize() + ' ' + (Math.randomInt(10, 1)).toRoman(); }
return "Ord " + components.joinCapitalize();
}
ChatMessage.create({content: generatePlanetName()});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment