This commit is contained in:
Canis Artorus 2023-02-21 19:05:11 -07:00
parent 81a19337a9
commit c830bf31ba
3 changed files with 139 additions and 125 deletions

View file

@ -127,24 +127,28 @@ window.Cultures = (function () {
pack.cultures?.forEach(function (culture) { pack.cultures?.forEach(function (culture) {
if (culture.lock) cultures.push(culture); if (culture.lock) cultures.push(culture);
}); });
if (pack.religions?.length) { if (pack.religions?.length) {
const religions = pack.religions;
const religMap = []; const religMap = [];
pack.religions.forEach(r => religMap.push(r.type === "Folk" ? 0 : r.i)); // remove folk religions in general religions.forEach(r => religMap.push(r.type === "Folk" ? 0 : r.i)); // remove folk religions in general
for (const j = 0; j < cultures.length; j++) { // locked cultures save their folk religions, at the new id for (const j = 0; j < cultures.length; j++) { // locked cultures save their folk religions, at the new id
const k = j+1; const newId = j + 1;
religMap[cultures[j].i] = k; religMap[cultures[j].i] = newId;
}); };
for (const i of cells.i) { for (const i of cells.i) {
cells.religion[i] = religMap[cells.religion[i]]; cells.religion[i] = religMap[cells.religion[i]];
} }
for (const i = 0; i < religMap.length; i++) { for (const i = 0; i < religMap.length; i++) {
// update origin heirarchy to the new ids // update origin heirarchy to the new ids
pack.religions[i].origins = pack.religions[i].origins.map(i => religMap[i]).filter(i => i); religions[i].origins = religions[i].origins.map(i => religMap[i]).filter(i => i);
if (religMap[i] !== i) { if (religMap[i] !== i) {
if(religMap[i] !== 0) if (religMap[i]) religions[religMap[i]] = religions[i];
pack.religions[religMap[i]] = pack.religions[i]; // unlocked folk religions for unlocked cultures are removed pending regeneration
// folk religions for unlocked cultures are removed pending regeneration if (!religions[i].locked) religions[i].removed = true;
pack.religions[i].removed = true;
} }
} }
} }

View file

@ -190,7 +190,7 @@ function religionsEditorAddLines() {
<div data-tip="Religion area" class="religionArea hide" style="width: 5em">${si(area) + unit}</div> <div data-tip="Religion area" class="religionArea hide" style="width: 5em">${si(area) + unit}</div>
<span data-tip="${populationTip}" class="icon-male hide"></span> <span data-tip="${populationTip}" class="icon-male hide"></span>
<div data-tip="${populationTip}" class="religionPopulation hide pointer">${si(population)}</div> <div data-tip="${populationTip}" class="religionPopulation hide pointer">${si(population)}</div>
<select data-tip="Potential religion extent" class="religionLimit placeholder hide" style="width: 5em"></select> <select data-tip="Potential religion extent" class="religionExtent placeholder hide" style="width: 5em"></select>
<span class="icon-resize-full placeholder hide"></span> <span class="icon-resize-full placeholder hide"></span>
<input class="religionExpan placeholder hide" type="number" /> <input class="religionExpan placeholder hide" type="number" />
</div>`; </div>`;
@ -236,7 +236,7 @@ function religionsEditorAddLines() {
culture culture
</span>` </span>`
: :
`<select data-tip="Potential religion extent" class="religionLimit hide" style="width: 5em"> `<select data-tip="Potential religion extent" class="religionExtent hide" style="width: 5em">
${getExtentOptions(r.expansion)} ${getExtentOptions(r.expansion)}
</select>` </select>`
} }
@ -283,7 +283,7 @@ function religionsEditorAddLines() {
$body.querySelectorAll("div > input.religionDeity").forEach(el => el.on("input", religionChangeDeity)); $body.querySelectorAll("div > input.religionDeity").forEach(el => el.on("input", religionChangeDeity));
$body.querySelectorAll("div > span.icon-arrows-cw").forEach(el => el.on("click", regenerateDeity)); $body.querySelectorAll("div > span.icon-arrows-cw").forEach(el => el.on("click", regenerateDeity));
$body.querySelectorAll("div > div.religionPopulation").forEach(el => el.on("click", changePopulation)); $body.querySelectorAll("div > div.religionPopulation").forEach(el => el.on("click", changePopulation));
$body.querySelectorAll("div > select.religionLimit").forEach(el => el.on("change", religionChangeLimit)); $body.querySelectorAll("div > select.religionExtent").forEach(el => el.on("change", religionChangeExtent));
$body.querySelectorAll("div > input.religionExpan").forEach(el => el.on("change", religionChangeExpansionism)); $body.querySelectorAll("div > input.religionExpan").forEach(el => el.on("change", religionChangeExpansionism));
$body.querySelectorAll("div > span.icon-trash-empty").forEach(el => el.on("click", religionRemovePrompt)); $body.querySelectorAll("div > span.icon-trash-empty").forEach(el => el.on("click", religionRemovePrompt));
$body.querySelectorAll("div > span.icon-lock").forEach($el => $el.on("click", updateLockStatus)); $body.querySelectorAll("div > span.icon-lock").forEach($el => $el.on("click", updateLockStatus));
@ -481,7 +481,7 @@ function changePopulation() {
} }
} }
function religionChangeLimit() { function religionChangeExtent() {
const religion = +this.parentNode.dataset.id; const religion = +this.parentNode.dataset.id;
this.parentNode.dataset.expansion = this.value; this.parentNode.dataset.expansion = this.value;
pack.religions[religion].expansion = this.value; pack.religions[religion].expansion = this.value;
@ -839,10 +839,7 @@ function updateLockStatus() {
function recalculateReligions(must) { function recalculateReligions(must) {
if (!must && !religionsAutoChange.checked) return; if (!must && !religionsAutoChange.checked) return;
Religions.resetUnlockedReligions(); Religions.recalculate();
Religions.expandReligions();
Religions.expandHeresies();
Religions.checkReligionCenters();
drawReligions(); drawReligions();
refreshReligionsEditor(); refreshReligionsEditor();

View file

@ -578,7 +578,7 @@ window.Religions = (function () {
expandHeresies(); expandHeresies();
checkReligionCenters(); checkCenters();
TIME && console.timeEnd("generateReligions"); TIME && console.timeEnd("generateReligions");
}; };
@ -588,9 +588,9 @@ window.Religions = (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 religionIds = pack.cells.religion;
const cells = pack.cells; const {cells, religions} = pack;
pack.religions 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;
@ -603,12 +603,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 = pack.religions[r].expansion; const expansion = 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 (pack.religions[religionIds[nextCell]]?.lock) return; if (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;
@ -618,7 +618,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) / pack.religions[r].expansionism; (cultureCost + stateCost + biomeCost + populationCost + heightCost + waterCost) / religions[r].expansionism;
if (totalCost > neutral) return; if (totalCost > neutral) return;
if (!cost[nextCell] || totalCost < cost[nextCell]) { if (!cost[nextCell] || totalCost < cost[nextCell]) {
@ -635,9 +635,9 @@ window.Religions = (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 religionIds = pack.cells.religion;
const cells = pack.cells; const {cells, religions} = pack;
pack.religions 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
@ -652,13 +652,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 (pack.religions[religionIds[nextCell]]?.lock) return; if (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(pack.religions[r].expansionism, 0.1); p + (religionCost + biomeCost + heightCost + waterCost) / Math.max(religions[r].expansionism, 0.1);
if (totalCost > neutral) return; if (totalCost > neutral) return;
@ -671,16 +671,32 @@ window.Religions = (function () {
} }
} }
function checkReligionCenters() { function checkCenters() {
const cells = pack.cells;
pack.religions.forEach(r => { pack.religions.forEach(r => {
if (!r.i) return; if (!r.i) return;
// 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 (r.type==="Folk" || pack.cells.religion[r.center] === r.i) return; // in area, or non-expanding if (r.type==="Folk" || cells.religion[r.center] === r.i) return; // in area, or non-expanding
const firstCell = pack.cells.i.find(i => pack.cells.religion[i] === r.i); const firstCell = cells.i.find(i => 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
}); });
} }
function recalculate() {
const {religion, culture} = pack.cells;
// start with the culutres map, but also keep locked religions
for(const i of pack.cells.i){
if( !pack.religions[religion[i]]?.lock ){
religion[i] = culture[i];
}
}
expandReligions();
expandHeresies();
checkCenters();
}
const add = function (center) { const add = function (center) {
const {cells, religions} = pack; const {cells, religions} = pack;
const religionId = cells.religion[center]; const religionId = cells.religion[center];
@ -717,7 +733,7 @@ window.Religions = (function () {
form: formName, form: formName,
deity, deity,
expansion, expansion,
expansionism: 0.01, expansionism: 0,
center, center,
cells: 0, cells: 0,
area: 0, area: 0,
@ -759,10 +775,12 @@ window.Religions = (function () {
}; };
if(c.i < religions.length){ if(c.i < religions.length){
// move an existing organized religion to the end of the array
const rCargo = religions[c.i]; const rCargo = religions[c.i];
if(!rCargo.removed) { if(!rCargo.removed) {
const newId = religions.length; const newId = religions.length;
rCargo.i = newId; rCargo.i = newId;
for(const i of cells.i) { for(const i of cells.i) {
if(cells.religion[i] = c.i) { if(cells.religion[i] = c.i) {
cells.religion[i] = newId; cells.religion[i] = newId;
@ -776,8 +794,10 @@ window.Religions = (function () {
} }
} }
}); });
religions.push(rCargo); religions.push(rCargo);
} }
religions[c.i] = newFolk; religions[c.i] = newFolk;
} else { } else {
religions.push(newFolk); religions.push(newFolk);
@ -788,11 +808,14 @@ window.Religions = (function () {
TIME && console.time("updateCulturesForReligions"); TIME && console.time("updateCulturesForReligions");
pack.religions.forEach(r => { pack.religions.forEach(r => {
if(r.i === 0) return; if(r.i === 0) return;
if(!r.origins.length) r.origins = [0]; if(!r.origins.length) r.origins = [0];
if(r.type === "Folk"){ if(r.type === "Folk"){
if(r.removed) addFolk(pack.cultures[r.i].center); // regnerate folk religions for the regenerated cultures if(r.removed) addFolk(pack.cultures[r.i].center); // regnerate folk religions for the regenerated cultures
else r.center = pack.cultures[r.i].center; else r.center = pack.cultures[r.i].center;
} }
// set new namebase culture
else r.culture = pack.cells.culture[r.center]; else r.culture = pack.cells.culture[r.center];
}); });
TIME && console.timeEnd("updateCulturesForReligions"); TIME && console.timeEnd("updateCulturesForReligions");
@ -874,15 +897,5 @@ window.Religions = (function () {
return type() + " of the " + generateMeaning(); return type() + " of the " + generateMeaning();
} }
const resetUnlockedReligions = function() { return {generate, add, addFolk, getDeityName, updateCultures, recalculate};
const {religion, culture} = pack.cells;
for(const i of pack.cells.i){
if( !pack.religions[religion[i]]?.lock ){
religion[i] = culture[i];
}
}
};
return {generate, expandReligions, expandHeresies, checkReligionCenters, add, addFolk, getDeityName, updateCultures, resetUnlockedReligions};
})(); })();