fix states regen count

This commit is contained in:
Azgaar 2021-09-01 00:46:57 +03:00
parent 46838a1702
commit e42bc58bd1

View file

@ -136,21 +136,11 @@ function recalculatePopulation() {
function regenerateStates() { function regenerateStates() {
const localSeed = Math.floor(Math.random() * 1e9); // new random seed const localSeed = Math.floor(Math.random() * 1e9); // new random seed
Math.random = aleaPRNG(localSeed); Math.random = aleaPRNG(localSeed);
const burgs = pack.burgs.filter(b => b.i && !b.removed);
if (!burgs.length) {
tip("No burgs to generate states. Please create burgs first", false, "error");
return;
}
if (burgs.length < +regionsInput.value) {
tip(`Not enough burgs to generate ${regionsInput.value} states. Will generate only ${burgs.length} states`, false, "warn");
}
// burg local ids sorted by a bit randomized population: const statesCount = +regionsInput.value;
const sorted = burgs const burgs = pack.burgs.filter(b => b.i && !b.removed);
.map((b, i) => [i, b.population * Math.random()]) if (!burgs.length) return tip("There are no any burgs to generate states. Please create burgs first", false, "error");
.sort((a, b) => b[1] - a[1]) if (burgs.length < statesCount) tip(`Not enough burgs to generate ${statesCount} states. Will generate only ${burgs.length} states`, false, "warn");
.map(b => b[0]);
const capitalsTree = d3.quadtree();
// turn all old capitals into towns // turn all old capitals into towns
burgs burgs
@ -167,8 +157,7 @@ function regenerateStates() {
unfog(); unfog();
// if desired states number is 0 if (!statesCount) {
if (regionsInput.value == 0) {
tip(`Cannot generate zero states. Please check the <i>States Number</i> option`, false, "warn"); tip(`Cannot generate zero states. Please check the <i>States Number</i> option`, false, "warn");
pack.states = pack.states.slice(0, 1); // remove all except of neutrals pack.states = pack.states.slice(0, 1); // remove all except of neutrals
pack.states[0].diplomacy = []; // clear diplomacy pack.states[0].diplomacy = []; // clear diplomacy
@ -184,26 +173,34 @@ function regenerateStates() {
return; return;
} }
const neutral = pack.states[0].name; // burg local ids sorted by a bit randomized population:
const count = Math.min(+regionsInput.value, burgs.length); const sortedBurgs = burgs
.map((b, i) => [b, b.population * Math.random()])
.sort((a, b) => b[1] - a[1])
.map(b => b[0]);
const capitalsTree = d3.quadtree();
const neutral = pack.states[0].name; // neutrals name
const count = Math.min(statesCount, burgs.length) + 1; // +1 for neutral
let spacing = (graphWidth + graphHeight) / 2 / count; // min distance between capitals let spacing = (graphWidth + graphHeight) / 2 / count; // min distance between capitals
pack.states = d3.range(count).map(i => { pack.states = d3.range(count).map(i => {
if (!i) return {i, name: neutral}; if (!i) return {i, name: neutral};
let capital = null, let capital = null;
x = 0, for (const burg of sortedBurgs) {
y = 0; const {x, y} = burg;
for (const i of sorted) { if (capitalsTree.find(x, y, spacing) === undefined) {
capital = burgs[i]; burg.capital = 1;
(x = capital.x), (y = capital.y); capital = burg;
if (capitalsTree.find(x, y, spacing) === undefined) break; capitalsTree.add([x, y]);
moveBurgToGroup(burg.i, "cities");
break;
}
spacing = Math.max(spacing - 1, 1); spacing = Math.max(spacing - 1, 1);
} }
capitalsTree.add([x, y]);
capital.capital = 1;
moveBurgToGroup(capital.i, "cities");
const culture = capital.culture; const culture = capital.culture;
const basename = capital.name.length < 9 && capital.cell % 5 === 0 ? capital.name : Names.getCulture(culture, 3, 6, "", 0); const basename = capital.name.length < 9 && capital.cell % 5 === 0 ? capital.name : Names.getCulture(culture, 3, 6, "", 0);
const name = Names.getState(basename, culture); const name = Names.getState(basename, culture);