This commit is contained in:
Azgaar 2021-04-10 14:33:59 +03:00
parent f8f2f52be9
commit 38c8a20c45
6 changed files with 37 additions and 38 deletions

View file

@ -287,25 +287,28 @@
// growth algorithm to assign cells to states like we did for cultures // growth algorithm to assign cells to states like we did for cultures
const expandStates = function() { const expandStates = function() {
TIME && console.time("expandStates"); TIME && console.time("expandStates");
const cells = pack.cells, states = pack.states, cultures = pack.cultures, burgs = pack.burgs; const {cells, states, cultures, burgs} = pack;
cells.state = new Uint16Array(cells.i.length); // cell state cells.state = new Uint16Array(cells.i.length);
const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p}); const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p});
const cost = []; const cost = [];
states.filter(s => s.i && !s.removed).forEach(function(s) { const neutral = cells.i.length / 5000 * 2500 * neutralInput.value * statesNeutral.value; // limit cost for state growth
cells.state[burgs[s.capital].cell] = s.i;
const b = cells.biome[cultures[s.culture].center]; // native biome states.filter(s => s.i && !s.removed).forEach(s => {
const capitalCell = burgs[s.capital].cell;
cells.state[capitalCell] = s.i;
const cultureCenter = cultures[s.culture].center;
const b = cells.biome[cultureCenter]; // state native biome
queue.queue({e:s.center, p:0, s:s.i, b}); queue.queue({e:s.center, p:0, s:s.i, b});
cost[s.center] = 1; cost[s.center] = 1;
}); });
const neutral = cells.i.length / 5000 * 2500 * neutralInput.value * statesNeutral.value; // limit cost for state growth
while (queue.length) { while (queue.length) {
const next = queue.dequeue(), n = next.e, p = next.p, s = next.s, b = next.b; const next = queue.dequeue();
const type = states[s].type; const {e, p, s, b} = next;
const culture = states[s].culture; const {type, culture} = states[s];
cells.c[n].forEach(function(e) { cells.c[e].forEach(e => {
if (cells.state[e] && e === states[cells.state[e]].center) return; // do not overwrite capital cells if (cells.state[e] && e === states[cells.state[e]].center) return; // do not overwrite capital cells
const cultureCost = culture === cells.culture[e] ? -9 : 100; const cultureCost = culture === cells.culture[e] ? -9 : 100;

View file

@ -43,6 +43,7 @@
const colors = getColors(count); const colors = getColors(count);
const emblemShape = document.getElementById("emblemShape").value; const emblemShape = document.getElementById("emblemShape").value;
const codes = [];
cultures.forEach(function(c, i) { cultures.forEach(function(c, i) {
const cell = c.center = placeCenter(c.sort ? c.sort : (i) => cells.s[i]); const cell = c.center = placeCenter(c.sort ? c.sort : (i) => cells.s[i]);
centers.add(cells.p[cell]); centers.add(cells.p[cell]);
@ -53,7 +54,8 @@
c.type = defineCultureType(cell); c.type = defineCultureType(cell);
c.expansionism = defineCultureExpansionism(c.type); c.expansionism = defineCultureExpansionism(c.type);
c.origin = 0; c.origin = 0;
c.code = getCode(c.name); c.code = abbreviate(c.name, codes);
codes.push(c.code);
cells.culture[cell] = i+1; cells.culture[cell] = i+1;
if (emblemShape === "random") c.shield = getRandomShield(); if (emblemShape === "random") c.shield = getRandomShield();
}); });
@ -122,20 +124,10 @@
TIME && console.timeEnd('generateCultures'); TIME && console.timeEnd('generateCultures');
} }
// assign a unique two-letters code (abbreviation)
function getCode(name) {
name = name.replace(/[()]/g, "");
const words = name.split(" "), letters = words.join("");
let code = words.length === 2 ? words[0][0]+words[1][0] : letters.slice(0,2);
for (let i=1; i < letters.length-1 && pack.cultures.some(c => c.code === code); i++) {
code = letters[0] + letters[i].toUpperCase();
}
return code;
}
const add = function(center) { const add = function(center) {
const defaultCultures = getDefault(); const defaultCultures = getDefault();
let culture, base, name; let culture, base, name;
if (pack.cultures.length < defaultCultures.length) { if (pack.cultures.length < defaultCultures.length) {
// add one of the default cultures // add one of the default cultures
culture = pack.cultures.length; culture = pack.cultures.length;
@ -147,7 +139,7 @@
name = Names.getCulture(culture, 5, 8, ""); name = Names.getCulture(culture, 5, 8, "");
base = pack.cultures[culture].base; base = pack.cultures[culture].base;
} }
const code = getCode(name); const code = abbreviate(name, pack.cultures.map(c => c.code));
const i = pack.cultures.length; const i = pack.cultures.length;
const color = d3.color(d3.scaleSequential(d3.interpolateRainbow)(Math.random())).hex(); const color = d3.color(d3.scaleSequential(d3.interpolateRainbow)(Math.random())).hex();

View file

@ -71,7 +71,7 @@
}); });
if (religionsInput.value == 0 || pack.cultures.length < 2) { if (religionsInput.value == 0 || pack.cultures.length < 2) {
religions.filter(r => r.i).forEach(r => r.code = getCode(r.name)); religions.filter(r => r.i).forEach(r => r.code = abbreviate(r.name));
return; return;
} }
@ -182,7 +182,7 @@
if (type === "Organized") [name, expansion] = getReligionName(form, deity, center) if (type === "Organized") [name, expansion] = getReligionName(form, deity, center)
else {name = getCultName(form, center); expansion = "global";} else {name = getCultName(form, center); expansion = "global";}
const formName = type === "Heresy" ? religions[r].form : form; const formName = type === "Heresy" ? religions[r].form : form;
const code = getCode(name); const code = abbreviate(name, religions.map(r => r.code));
religions.push({i, name, color, culture, type, form:formName, deity, expansion, expansionism:0, center, cells:0, area:0, rural:0, urban:0, origin:r, code}); religions.push({i, name, color, culture, type, form:formName, deity, expansion, expansionism:0, center, cells:0, area:0, rural:0, urban:0, origin:r, code});
cells.religion[center] = i; cells.religion[center] = i;
} }
@ -267,9 +267,9 @@
function checkCenters() { function checkCenters() {
const cells = pack.cells, religions = pack.religions; const cells = pack.cells, religions = pack.religions;
const codes = religions.map(r => r.code);
religions.filter(r => r.i).forEach(r => { religions.filter(r => r.i).forEach(r => {
// generate religion code (abbreviation) r.code = abbreviate(r.name, codes);
r.code = getCode(r.name);
// move religion center if it's not within religion area after expansion // move religion center if it's not within religion area after expansion
if (cells.religion[r.center] === r.i) return; // in area if (cells.religion[r.center] === r.i) return; // in area
@ -290,17 +290,6 @@
TIME && console.timeEnd('updateCulturesForReligions'); TIME && console.timeEnd('updateCulturesForReligions');
} }
// assign a unique two-letters code (abbreviation)
function getCode(rawName) {
const name = rawName.replace("Old ", ""); // remove Old prefix
const words = name.split(" "), letters = words.join("");
let code = words.length === 2 ? words[0][0]+words[1][0] : letters.slice(0,2);
for (let i=1; i < letters.length-1 && pack.religions.some(r => r.code === code); i++) {
code = letters[0] + letters[i].toUpperCase();
}
return code;
}
// get supreme deity name // get supreme deity name
const getDeityName = function(culture) { const getDeityName = function(culture) {
if (culture === undefined) {ERROR && console.error("Please define a culture"); return;} if (culture === undefined) {ERROR && console.error("Please define a culture"); return;}

View file

@ -216,6 +216,7 @@ function editCultures() {
const culture = +this.parentNode.dataset.id; const culture = +this.parentNode.dataset.id;
this.parentNode.dataset.name = this.value; this.parentNode.dataset.name = this.value;
pack.cultures[culture].name = this.value; pack.cultures[culture].name = this.value;
pack.cultures[culture].code = abbreviate(this.value, pack.cultures.map(c => c.code));
} }
function cultureChangeExpansionism() { function cultureChangeExpansionism() {

View file

@ -200,6 +200,7 @@ function editReligions() {
const religion = +this.parentNode.dataset.id; const religion = +this.parentNode.dataset.id;
this.parentNode.dataset.name = this.value; this.parentNode.dataset.name = this.value;
pack.religions[religion].name = this.value; pack.religions[religion].name = this.value;
pack.religions[religion].code = abbreviate(this.value, pack.religions.map(c => c.code));
} }
function religionChangeType() { function religionChangeType() {

View file

@ -447,6 +447,19 @@ function getAdjective(string) {
// get ordinal out of integer: 1 => 1st // get ordinal out of integer: 1 => 1st
const nth = n => n+(["st","nd","rd"][((n+90)%100-10)%10-1]||"th"); const nth = n => n+(["st","nd","rd"][((n+90)%100-10)%10-1]||"th");
// get two-letters code (abbreviation) from string
function abbreviate(name, restricted = []) {
const parsed = name.replace("Old ", "O ").replace(/[()]/g, ""); // remove Old prefix and parentheses
const words = parsed.split(" ");
const letters = words.join("");
let code = words.length === 2 ? words[0][0]+words[1][0] : letters.slice(0,2);
for (let i = 1; i < letters.length-1 && restricted.includes(code); i++) {
code = letters[0] + letters[i].toUpperCase();
}
return code;
}
// conjunct array: [A,B,C] => "A, B and C" // conjunct array: [A,B,C] => "A, B and C"
function list(array) { function list(array) {
if (!Intl.ListFormat) return array.join(", "); if (!Intl.ListFormat) return array.join(", ");