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

View file

@ -310,15 +310,6 @@ function recreateStates() {
function regenerateProvinces() {
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);
drawBorders();
if (layerIsOn("toggleProvinces")) drawProvinces();

View file

@ -28,7 +28,7 @@ const version = "1.89.00"; // generator version, update each time
<ul>
<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>Data Charts screen</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>New heightmap template: Fractious</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>
<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>