ability to lock culture in a easy-defined set

fixes uncaught error in commit dada419 from PR #910
This commit is contained in:
Canis Artorus 2023-02-23 00:06:50 -07:00
parent e4a7a6ecf2
commit 2ea53d2688
2 changed files with 50 additions and 44 deletions

View file

@ -118,69 +118,75 @@ window.Cultures = (function () {
function selectCultures(c) {
let def = getDefault(c);
if (c === def.length) return def;
if (def.every(d => d.odd === 1)) return def.splice(0, c);
const count = Math.min(c, def.length);
const cultures = [];
pack.cultures?.forEach(function (culture) {
if (culture.lock) cultures.push(culture);
});
if (cultures.length && pack.religions?.length > 1) {
const religions = pack.religions;
const religMap = [];
religions.forEach(r => religMap.push(r.type === "Folk" ? 0 : r.i)); // remove folk religions in general
if (cultures.length) {
for (let j = 0; j < cultures.length; j++) {
// locked cultures bring their folk religions along to the new id
const newId = j + 1;
religMap[cultures[j].i] = newId;
};
if (pack.religions?.length > 1) {
const religions = pack.religions;
const religMap = [];
religions.forEach(r => religMap.push(r.type === "Folk" ? 0 : r.i)); // remove folk religions in general
religions.forEach(r => {
if (r.i === 0) return;
// queue for movement to newId, or removal
if (religMap[r.i]) r.i = religMap[r.i];
else if (r.locked) {
// locked folk religion getting a new culture
if (religMap.find(r.i) > 0) { // something already there, shift away from conflict
const nextId = religMap.find(0);
if (nextId > 0) {
religMap[r.i] = nextId;
r.i = nextId;
}
else { // none available...
religMap[r.i] = religMap.length;
r.i = religMap.length;
religMap.push(r.i);
for (let j = 0; j < cultures.length; j++) {
// locked cultures bring their folk religions along to the new id
const newId = j + 1;
religMap[cultures[j].i] = newId;
};
religions.forEach(r => {
if (r.i === 0) return;
// queue for movement to newId, or removal
if (religMap[r.i]) r.i = religMap[r.i];
else if (r.locked) {
// locked folk religion getting a new culture
if (religMap.find(r.i) > 0) { // something already there, shift away from conflict
const nextId = religMap.find(0);
if (nextId > 0) {
religMap[r.i] = nextId;
r.i = nextId;
}
else { // none available...
religMap[r.i] = religMap.length;
r.i = religMap.length;
religMap.push(r.i);
}
}
}
else r.removed = true; // the unlocked folk religions that were left behind by unlocked cultures
// update origin heirarchy to the new ids
r.origins = r.origins.map(i => religMap[i]).filter(i => i);
});
for (const i of cells.i) {
cells.religion[i] = religMap[cells.religion[i]];
}
else r.removed = true; // the unlocked folk religions that were left behind by unlocked cultures
// update origin heirarchy to the new ids
r.origins = r.origins.map(i => religMap[i]).filter(i => i);
});
for (const i of cells.i) {
cells.religion[i] = religMap[cells.religion[i]];
}
} else {
if (c === def.length) return def;
if (def.every(d => d.odd === 1)) return def.splice(0, c);
}
pack.religions?.forEach(r => {
if (r.type === "Folk" && !r.locked)
cell.religion = cells.religion.map(ri => ri === r.i ? 0 : ri);
cells.religion = cells.religion.map(ri => ri === r.i ? 0 : ri);
});
for (let culture, rnd, i = 0; cultures.length < count && i < 200; i++) {
let culture, rnd;
let i = 0;
do {
do {
rnd = rand(def.length - 1);
culture = def[rnd];
} while (!P(culture.odd));
i++;
} while (i < 200 && !P(culture.odd));
cultures.push(culture);
def.splice(rnd, 1);
}
} while (cultures.length < c && def.length > 0);
return cultures;
}

View file

@ -814,7 +814,7 @@ window.Religions = (function () {
const faith = religions.find(r => r.i === i);
if (faith) {
if (faith.type === "Folk" && !faith.locked)
tReligions.push({...faith, removed: true}));
tReligions.push({...faith, removed: true});
else tReligions.push(faith);
}
else tReligions.push({
@ -861,7 +861,7 @@ window.Religions = (function () {
if (r.origins?.length < 1) r.origins = [0];
if (r.type === "Folk"){
if (r.type === "Folk" && cultures[r.i]) {
r.center = cultures[r.i].center;
}
else {