refactor(#902): regenerate cultures

This commit is contained in:
Azgaar 2023-01-01 19:01:27 +03:00
parent 5f1c2adee0
commit 788ce5f1db
3 changed files with 36 additions and 56 deletions

View file

@ -6,13 +6,8 @@ window.Cultures = (function () {
const generate = function () { const generate = function () {
TIME && console.time("generateCultures"); TIME && console.time("generateCultures");
cells = pack.cells; cells = pack.cells;
const prevCultures = {};
if (cells.culture) { const cultureIds = new Uint16Array(cells.i.length); // cell cultures
cells.culture.forEach(function (cultureId, index) {
prevCultures[index] = cultureId;
})
}
cells.culture = new Uint16Array(cells.i.length); // cell cultures
let count = Math.min(+culturesInput.value, +culturesSet.selectedOptions[0].dataset.max); let count = Math.min(+culturesInput.value, +culturesSet.selectedOptions[0].dataset.max);
const populated = cells.i.filter(i => cells.s[i]); // populated cells const populated = cells.i.filter(i => cells.s[i]); // populated cells
@ -21,9 +16,12 @@ window.Cultures = (function () {
if (!count) { if (!count) {
WARN && console.warn(`There are no populated cells. Cannot generate cultures`); WARN && console.warn(`There are no populated cells. Cannot generate cultures`);
pack.cultures = [{name: "Wildlands", i: 0, base: 1, shield: "round"}]; pack.cultures = [{name: "Wildlands", i: 0, base: 1, shield: "round"}];
cells.culture = cultureIds;
alertMessage.innerHTML = /* html */ `The climate is harsh and people cannot live in this world.<br /> alertMessage.innerHTML = /* html */ `The climate is harsh and people cannot live in this world.<br />
No cultures, states and burgs will be created.<br /> No cultures, states and burgs will be created.<br />
Please consider changing climate settings in the World Configurator`; Please consider changing climate settings in the World Configurator`;
$("#alert").dialog({ $("#alert").dialog({
resizable: false, resizable: false,
title: "Extreme climate warning", title: "Extreme climate warning",
@ -57,32 +55,25 @@ window.Cultures = (function () {
const emblemShape = document.getElementById("emblemShape").value; const emblemShape = document.getElementById("emblemShape").value;
const codes = []; const codes = [];
let unoccupied = [...populated];
cultures.forEach(function (c, i) { cultures.forEach(function (c, i) {
const newId = i + 1;
if (c.lock) { if (c.lock) {
centers.add(c.center);
cells.culture[c.center] = i + 1;
codes.push(c.code); codes.push(c.code);
centers.add(c.center);
const cultureCells = []; for (const i of cells.i) {
Object.entries(prevCultures).forEach(function ([index, cultureId]) { if (cells.culture[i] === c.i) cultureIds[i] = newId;
if (cultureId === c.i) {
cells.culture[index] = i + 1;
cultureCells.push(parseInt(index));
} }
});
unoccupied = unoccupied.filter(function (cell) { c.i = newId;
return !cultureCells.includes(cell);
});
c.i = i + 1;
return; return;
} }
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]);
c.i = i + 1; c.i = newId;
delete c.odd; delete c.odd;
delete c.sort; delete c.sort;
c.color = colors[i]; c.color = colors[i];
@ -91,24 +82,27 @@ window.Cultures = (function () {
c.origins = [0]; c.origins = [0];
c.code = abbreviate(c.name, codes); c.code = abbreviate(c.name, codes);
codes.push(c.code); codes.push(c.code);
cells.culture[cell] = i + 1; cultureIds[cell] = newId;
if (emblemShape === "random") c.shield = getRandomShield(); if (emblemShape === "random") c.shield = getRandomShield();
}); });
cells.culture = cultureIds;
function placeCenter(v) { function placeCenter(v) {
let c, let spacing = (graphWidth + graphHeight) / 2 / count;
spacing = (graphWidth + graphHeight) / 2 / count; const MAX_ATTEMPTS = 100;
// Only use cells where there are no culture already on that cell, which may happen if a locked culture
// was restored. We can be sure that locked cultures will always be first in the list. const sorted = [...populated].sort((a, b) => v(b) - v(a));
const sorted = [...unoccupied].sort((a, b) => v(b) - v(a)), const max = Math.floor(sorted.length / 2);
max = Math.floor(sorted.length / 2);
do { let cellId = 0;
c = sorted[biased(0, max, 5)]; for (let i = 0; i < MAX_ATTEMPTS; i++) {
cellId = sorted[biased(0, max, 5)];
spacing *= 0.9; spacing *= 0.9;
} while ( if (!cultureIds[cellId] && !centers.find(cells.p[cellId][0], cells.p[cellId][1], spacing)) break;
centers.find(cells.p[c][0], cells.p[c][1], spacing) !== undefined }
);
return c; return cellId;
} }
// the first culture with id 0 is for wildlands // the first culture with id 0 is for wildlands
@ -130,11 +124,9 @@ window.Cultures = (function () {
const count = Math.min(c, def.length); const count = Math.min(c, def.length);
const cultures = []; const cultures = [];
if (pack.cultures) { pack.cultures?.forEach(function (culture) {
pack.cultures.forEach(function (culture) {
if (culture.lock) cultures.push(culture); if (culture.lock) cultures.push(culture);
}); });
}
for (let culture, rnd, i = 0; cultures.length < count && i < 200; i++) { for (let culture, rnd, i = 0; cultures.length < count && i < 200; i++) {
do { do {
@ -531,7 +523,7 @@ window.Cultures = (function () {
p = next.p, p = next.p,
c = next.c; c = next.c;
const type = pack.cultures[c].type; const type = pack.cultures[c].type;
cells.c[n].forEach(function (e) { cells.c[n].forEach(e => {
if (pack.cultures[cells.culture[e]]?.lock) return; if (pack.cultures[cells.culture[e]]?.lock) return;
const biome = cells.biome[e]; const biome = cells.biome[e];

View file

@ -310,15 +310,6 @@ function recreateStates() {
function regenerateProvinces() { function regenerateProvinces() {
unfog(); unfog();
// TODO: lockedProvincesIds
for (const i of pack.cells.i) {
const provinceId = pack.cells.province[i];
const lockedProvinceIndex = lockedProvincesIds.indexOf(provinceId) + 1;
// lockedProvinceIndex is an index of locked province or 0 if state is not locked
pack.cells.province[i] = lockedProvinceIndex;
}
BurgsAndStates.generateProvinces(true, true); BurgsAndStates.generateProvinces(true, true);
drawBorders(); drawBorders();
if (layerIsOn("toggleProvinces")) drawProvinces(); if (layerIsOn("toggleProvinces")) drawProvinces();

View file

@ -28,7 +28,7 @@ const version = "1.89.00"; // generator version, update each time
<ul> <ul>
<strong>Latest changes:</strong> <strong>Latest changes:</strong>
<li>Can now lock states, provinces, cultures, and religions from being regenerated</li> <li>Lock states, provinces, cultures, and religions from regeneration</li>
<li>Heightmap brushes: linear edit option</li> <li>Heightmap brushes: linear edit option</li>
<li>Data Charts screen</li> <li>Data Charts screen</li>
<li>Сultures and religions can have multiple parents in hierarchy tree</li> <li>Сultures and religions can have multiple parents in hierarchy tree</li>
@ -36,9 +36,6 @@ const version = "1.89.00"; // generator version, update each time
<li>Dialogs optimization for mobile</li> <li>Dialogs optimization for mobile</li>
<li>New heightmap template: Fractious</li> <li>New heightmap template: Fractious</li>
<li>Template Editor: mask and invert tools</li> <li>Template Editor: mask and invert tools</li>
<li>Ability to install the App</li>
<li>14 new default fonts</li>
<li>Caching for faster startup</li>
</ul> </ul>
<p>Join our <a href="${discord}" target="_blank">Discord server</a> and <a href="${reddit}" target="_blank">Reddit community</a> to ask questions, share maps, discuss the Generator and Worlbuilding, report bugs and propose new features.</p> <p>Join our <a href="${discord}" target="_blank">Discord server</a> and <a href="${reddit}" target="_blank">Reddit community</a> to ask questions, share maps, discuss the Generator and Worlbuilding, report bugs and propose new features.</p>