diff --git a/modules/cultures-generator.js b/modules/cultures-generator.js index 6925bfdc..f29a4d53 100644 --- a/modules/cultures-generator.js +++ b/modules/cultures-generator.js @@ -138,21 +138,40 @@ window.Cultures = (function () { const newId = j + 1; religMap[cultures[j].i] = newId; }; - - for (const i of cells.i) { - cells.religion[i] = religMap[cells.religion[i]]; - } - + 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 r.removed = !r.locked; + 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]]; + } } + pack.religions?.forEach(r => { + if (r.type === "Folk" && !r.locked) + cell.religion = cells.religion.map(ri => ri === r.i ? 0 : ri); + }); for (let culture, rnd, i = 0; cultures.length < count && i < 200; i++) { do { diff --git a/modules/religions-generator.js b/modules/religions-generator.js index 35c8247d..7ba513be 100644 --- a/modules/religions-generator.js +++ b/modules/religions-generator.js @@ -784,44 +784,88 @@ window.Religions = (function () { } religions[c.i] = newFolk; - } else { - religions.push(newFolk); - } + } + else religions.push(newFolk); }; function updateCultures() { TIME && console.time("updateCulturesForReligions"); - const religions = []; - for (let i=0; i < pack.religions.length; i++) { - const faith = pack.religions.find(r => r.i === i); - if (faith) { - if (faith.type === "Folk" && faith.removed && !pack.cultures[i].removed) { - const codes = religions.map(r => r.code); - religions.push(createFolk(c, codes)); + const {religions, cultures, cells} = pack; + + const tReligions = []; + const spareCovenants = []; + const codes = religions.filter(r => !r.removed).map(r => r.code); + + tReligions.push(religions[0]); + for (let i = 1; i < cultures.length; i++) { + const faith = religions.find(r => r.i === i); + if (faith && !faith.removed) { + if (faith.type === "Folk") { + tReligions.push(faith); + continue; } - else religions.push(faith); + else spareCovenants.push(faith); } - else religions.push({ - i, - name: "removed index", - origins: [null], - removed: true - }); + const newFolk = createFolk(cultures[i], codes); + tReligions.push(newFolk); + codes.push(newFolk.code); + } + for (let i = cultures.length; i < religions.length; i++) { + const faith = religions.find(r => r.i === i); + if (faith) { + if (faith.type === "Folk" && !faith.locked) + tReligions.push({...faith, removed: true})); + else tReligions.push(faith); + } + else tReligions.push({ + i, + name: "filler index", + origins: [null], + removed: true + }); } - pack.religions = religions; + const updateMap = []; + for (let k = 0; k < spareCovenants.length; k++) { + const sc = spareCovenants[k]; + const newId = tReligions.length; + + for (const i of cells.i) { + if (cells.religion[i] = sc.i) { + cells.religion[i] = newId; + } + } + + updateMap.push({oldId: sc.i, newId}); + tReligions.forEach(r => { + if (r.i === 0) return; + for (let i = 0; i < r.origins.length; i++) { + if (r.origins[i] === sc.i) { + r.origins[i] === newId; + return; + } + } + }); + // update origins from other spareCovenants + for (let i = 0; i < sc.origins.length; i++) { + const changeRule = updateMap.find(u => u.oldId === sc.origins[i]) + if (changeRule) sc.origins[i] = changeRule.newId; + } + tReligions.push({...sc, i: newId}); + } + + pack.religions = tReligions; pack.religions.forEach(r => { - if(r.i === 0) return; + if (r.i === 0) return; - if(r.origins?.length < 1) r.origins = [0]; + if (r.origins?.length < 1) r.origins = [0]; - if(r.type === "Folk"){ - r.center = pack.cultures[r.i].center; + if (r.type === "Folk"){ + r.center = cultures[r.i].center; } else { - r.culture = pack.cells.culture[r.center]; - if (r.i < pack.cultures.length) addFolk(pack.cultures[r.i].center); + r.culture = cells.culture[r.center]; } }); TIME && console.timeEnd("updateCulturesForReligions");