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) { function selectCultures(c) {
let def = getDefault(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 = []; const cultures = [];
pack.cultures?.forEach(function (culture) { pack.cultures?.forEach(function (culture) {
if (culture.lock) cultures.push(culture); if (culture.lock) cultures.push(culture);
}); });
if (cultures.length && pack.religions?.length > 1) { if (cultures.length) {
const religions = pack.religions;
const religMap = [];
religions.forEach(r => religMap.push(r.type === "Folk" ? 0 : r.i)); // remove folk religions in general
for (let j = 0; j < cultures.length; j++) { if (pack.religions?.length > 1) {
// locked cultures bring their folk religions along to the new id const religions = pack.religions;
const newId = j + 1; const religMap = [];
religMap[cultures[j].i] = newId; religions.forEach(r => religMap.push(r.type === "Folk" ? 0 : r.i)); // remove folk religions in general
};
religions.forEach(r => { for (let j = 0; j < cultures.length; j++) {
if (r.i === 0) return; // locked cultures bring their folk religions along to the new id
// queue for movement to newId, or removal const newId = j + 1;
if (religMap[r.i]) r.i = religMap[r.i]; religMap[cultures[j].i] = newId;
else if (r.locked) { };
// locked folk religion getting a new culture
if (religMap.find(r.i) > 0) { // something already there, shift away from conflict religions.forEach(r => {
const nextId = religMap.find(0); if (r.i === 0) return;
if (nextId > 0) { // queue for movement to newId, or removal
religMap[r.i] = nextId; if (religMap[r.i]) r.i = religMap[r.i];
r.i = nextId; else if (r.locked) {
} // locked folk religion getting a new culture
else { // none available... if (religMap.find(r.i) > 0) { // something already there, shift away from conflict
religMap[r.i] = religMap.length; const nextId = religMap.find(0);
r.i = religMap.length; if (nextId > 0) {
religMap.push(r.i); 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 => { pack.religions?.forEach(r => {
if (r.type === "Folk" && !r.locked) 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 { do {
rnd = rand(def.length - 1); rnd = rand(def.length - 1);
culture = def[rnd]; culture = def[rnd];
} while (!P(culture.odd)); i++;
} while (i < 200 && !P(culture.odd));
cultures.push(culture); cultures.push(culture);
def.splice(rnd, 1); def.splice(rnd, 1);
} } while (cultures.length < c && def.length > 0);
return cultures; return cultures;
} }

View file

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