restructure religion generator for extensibility

This commit is contained in:
Canis Artorus 2023-02-01 18:00:51 -07:00
parent cc998e7773
commit 672e82cc08
2 changed files with 54 additions and 25 deletions

View file

@ -773,3 +773,16 @@ function updateLockStatus() {
classList.toggle("icon-lock-open"); classList.toggle("icon-lock-open");
classList.toggle("icon-lock"); classList.toggle("icon-lock");
} }
function recalculateReligions(must) {
if (!must && !religionsAutoChange.checked) return;
Religions.resetUnlockedReligions();
Religions.expandReligions();
Religions.expandHeresies();
Religions.checkReligionCentres();
drawReligions();
refreshReligionsEditor();
drawReligionCenters();
}

View file

@ -438,6 +438,8 @@ window.Religions = (function () {
} }
} }
cells.religion = religionIds;
// generate organized religions // generate organized religions
for (let i = 0; religions.length < count && i < 1000; i++) { for (let i = 0; religions.length < count && i < 1000; i++) {
let center = sorted[biased(0, sorted.length - 1, 5)]; // religion center let center = sorted[biased(0, sorted.length - 1, 5)]; // religion center
@ -520,6 +522,8 @@ window.Religions = (function () {
religionsTree.add([x, y]); religionsTree.add([x, y]);
} }
pack.religions = religions;
expandReligions(); expandReligions();
// generate heresies // generate heresies
@ -540,8 +544,8 @@ window.Religions = (function () {
const name = getCultName("Heresy", center); const name = getCultName("Heresy", center);
const expansionism = gauss(1.2, 0.5, 0, 5); const expansionism = gauss(1.2, 0.5, 0, 5);
const color = getMixedColor(r.color, 0.4, 0.2); // "url(#hatch6)"; const color = getMixedColor(r.color, 0.4, 0.2); // "url(#hatch6)";
religions.push({ pack.religions.push({
i: religions.length, i: pack.religions.length,
name, name,
color, color,
culture, culture,
@ -559,19 +563,20 @@ window.Religions = (function () {
expandHeresies(); expandHeresies();
checkCenters(); checkReligionCenters();
cells.religion = religionIds;
pack.religions = religions;
TIME && console.timeEnd("generateReligions"); TIME && console.timeEnd("generateReligions");
};
// growth algorithm to assign cells to religions // growth algorithm to assign cells to religions
function expandReligions() { const expandReligions = function() {
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 = [];
const religionIds = pack.cells.religion;
const cells = pack.cells;
religions
pack.religions
.filter(r => !r.lock && (r.type === "Organized" || r.type === "Cult")) .filter(r => !r.lock && (r.type === "Organized" || r.type === "Cult"))
.forEach(r => { .forEach(r => {
religionIds[r.center] = r.i; religionIds[r.center] = r.i;
@ -584,12 +589,12 @@ window.Religions = (function () {
while (queue.length) { while (queue.length) {
const {e, p, r, c, s} = queue.dequeue(); const {e, p, r, c, s} = queue.dequeue();
const expansion = religions[r].expansion; const expansion = pack.religions[r].expansion;
cells.c[e].forEach(nextCell => { cells.c[e].forEach(nextCell => {
if (expansion === "culture" && c !== cells.culture[nextCell]) return; if (expansion === "culture" && c !== cells.culture[nextCell]) return;
if (expansion === "state" && s !== cells.state[nextCell]) return; if (expansion === "state" && s !== cells.state[nextCell]) return;
if (religions[religionIds[nextCell]]?.lock) return; if (pack.religions[religionIds[nextCell]]?.lock) return;
const cultureCost = c !== cells.culture[nextCell] ? 10 : 0; const cultureCost = c !== cells.culture[nextCell] ? 10 : 0;
const stateCost = s !== cells.state[nextCell] ? 10 : 0; const stateCost = s !== cells.state[nextCell] ? 10 : 0;
@ -599,7 +604,7 @@ window.Religions = (function () {
const waterCost = cells.h[nextCell] < 20 ? (cells.road[nextCell] ? 50 : 1000) : 0; const waterCost = cells.h[nextCell] < 20 ? (cells.road[nextCell] ? 50 : 1000) : 0;
const totalCost = const totalCost =
p + p +
(cultureCost + stateCost + biomeCost + populationCost + heightCost + waterCost) / religions[r].expansionism; (cultureCost + stateCost + biomeCost + populationCost + heightCost + waterCost) / pack.religions[r].expansionism;
if (totalCost > neutral) return; if (totalCost > neutral) return;
if (!cost[nextCell] || totalCost < cost[nextCell]) { if (!cost[nextCell] || totalCost < cost[nextCell]) {
@ -609,14 +614,16 @@ window.Religions = (function () {
} }
}); });
} }
} };
// growth algorithm to assign cells to heresies // growth algorithm to assign cells to heresies
function expandHeresies() { const expandHeresies = function() {
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 = [];
const religionIds = pack.cells.religion;
const cells = pack.cells;
religions pack.religions
.filter(r => !r.lock && r.type === "Heresy") .filter(r => !r.lock && r.type === "Heresy")
.forEach(r => { .forEach(r => {
const b = religionIds[r.center]; // "base" religion id const b = religionIds[r.center]; // "base" religion id
@ -631,13 +638,13 @@ window.Religions = (function () {
const {e, p, r, b} = queue.dequeue(); const {e, p, r, b} = queue.dequeue();
cells.c[e].forEach(nextCell => { cells.c[e].forEach(nextCell => {
if (religions[religionIds[nextCell]]?.lock) return; if (pack.religions[religionIds[nextCell]]?.lock) return;
const religionCost = religionIds[nextCell] === b ? 0 : 2000; const religionCost = religionIds[nextCell] === b ? 0 : 2000;
const biomeCost = cells.road[nextCell] ? 0 : biomesData.cost[cells.biome[nextCell]]; const biomeCost = cells.road[nextCell] ? 0 : biomesData.cost[cells.biome[nextCell]];
const heightCost = Math.max(cells.h[nextCell], 20) - 20; const heightCost = Math.max(cells.h[nextCell], 20) - 20;
const waterCost = cells.h[nextCell] < 20 ? (cells.road[nextCell] ? 50 : 1000) : 0; const waterCost = cells.h[nextCell] < 20 ? (cells.road[nextCell] ? 50 : 1000) : 0;
const totalCost = const totalCost =
p + (religionCost + biomeCost + heightCost + waterCost) / Math.max(religions[r].expansionism, 0.1); p + (religionCost + biomeCost + heightCost + waterCost) / Math.max(pack.religions[r].expansionism, 0.1);
if (totalCost > neutral) return; if (totalCost > neutral) return;
@ -648,20 +655,19 @@ window.Religions = (function () {
} }
}); });
} }
} };
function checkCenters() { const checkReligionCenters = function() {
const codes = religions.map(r => r.code); const codes = pack.religions.map(r => r.code);
religions.forEach(r => { pack.religions.forEach(r => {
if (!r.i) return; if (!r.i) return;
r.code = abbreviate(r.name, codes); r.code = abbreviate(r.name, codes);
// 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 (religionIds[r.center] === r.i) return; // in area if (pack.cells.religion[r.center] === r.i) return; // in area
const firstCell = cells.i.find(i => religionIds[i] === r.i); const firstCell = pack.cells.i.find(i => pack.cells.religion[i] === r.i);
if (firstCell) r.center = firstCell; // move center, othervise it's an extinct religion if (firstCell) r.center = firstCell; // move center, othervise it's an extinct religion
}); });
}
}; };
const add = function (center) { const add = function (center) {
@ -799,5 +805,15 @@ window.Religions = (function () {
return type() + " of the " + generateMeaning(); return type() + " of the " + generateMeaning();
} }
return {generate, add, getDeityName, updateCultures}; const resetUnlockedReligions = function() {
const religion = pack.cells.religion;
const culture = pack.cells.culture;
for(const i of pack.cells.i){
if( culture[i] && !pack.religions[religion[i]]?.lock ){
religion[i] = culture[i];
}
}
};
return {generate, expandReligions, expandHeresies, checkReligionCentres, add, getDeityName, updateCultures, resetUnlockedReligions};
})(); })();