From a889d24b380cf1e6c34c963e2e24fbffbd005c96 Mon Sep 17 00:00:00 2001
From: Claude
The following states will lose their state status and each become a single province of ${emblem(rullingState.i)}${rullingState.name}: ${mergedList}.
+Their burgs, regiments and lands (along with any existing internal provinces, which are collapsed into the new province) will be assigned to ${emblem(rullingState.i)}${rullingState.name}.
+Are you sure you want to merge states? This action cannot be reverted.
` + : /* html */ ` +The following states will be removed: ${mergedList}.
+Removed states data (burgs, provinces, regiments) will be assigned to ${emblem(rullingState.i)}${rullingState.name}.
+Are you sure you want to merge states? This action cannot be reverted.
`; + confirmationDialog({ title: "Merge states", - // prettier-ignore - message: /* html */ ` -The following states will be removed: ${statesToMerge.map(stateId => `${emblem(stateId)}${pack.states[stateId].name}`).join(", ")}.
-Removed states data (burgs, provinces, regiments) will be assigned to ${emblem(rullingState.i)}${rullingState.name}.
-Are you sure you want to merge states? This action cannot be reverted.
`, + message, confirm: "Merge", onConfirm: () => { - mergeStates(statesToMerge, rulingStateId); + mergeStates(statesToMerge, rulingStateId, mergeToProvinces); $(this).dialog("close"); } }); @@ -1449,10 +1465,46 @@ function openStateMergeDialog() { } }); - function mergeStates(statesToMerge, rulingStateId) { + function mergeStates(statesToMerge, rulingStateId, mergeToProvinces = false) { const rulingState = pack.states[rulingStateId]; const rulingStateArmy = ensureEl("army" + rulingStateId); + // demote a merged state into a single province of the ruling state, collapsing + // any of its existing internal provinces into that one new province + function demoteStateToProvince(stateId) { + const state = pack.states[stateId]; + const newProvinceId = pack.provinces.length; + + // collapse the state's existing internal provinces + pack.provinces.forEach(province => { + if (!province.i || province.removed || province.state !== stateId) return; + const coaId = "provinceCOA" + province.i; + if (ensureEl(coaId)) ensureEl(coaId).remove(); + emblems.select(`#provinceEmblems > use[data-i='${province.i}']`).remove(); + pack.provinces[province.i] = {i: province.i, removed: true}; + }); + + // assign all of the former state's land cells to the new province + pack.cells.state.forEach((s, i) => { + if (s === stateId) pack.cells.province[i] = newProvinceId; + }); + + const burg = state.capital || 0; + const center = burg ? pack.burgs[burg].cell : state.center; + const name = state.name; + const formName = "Province"; + const fullName = name + " " + formName; + const color = state.color; + const coa = state.coa; // reuse the former state's emblem + const pole = state.pole || pack.cells.p[center]; + + pack.provinces.push({i: newProvinceId, state: rulingStateId, center, burg, name, formName, fullName, color, coa, pole}); + rulingState.provinces = rulingState.provinces || []; + rulingState.provinces.push(newProvinceId); + + COArenderer.add("province", newProvinceId, coa, pole[0], pole[1]); + } + // remove states to be merged statesToMerge.forEach(stateId => { const state = pack.states[stateId]; @@ -1487,6 +1539,10 @@ function openStateMergeDialog() { }); armies.select("g#army" + stateId).remove(); + + // optionally turn the merged state into a province of the ruling state + // (reads cells.state before the cell reassignment below) + if (mergeToProvinces) demoteStateToProvince(stateId); }); // reassing burgs @@ -1514,6 +1570,7 @@ function openStateMergeDialog() { debug.selectAll(".highlight").remove(); States.getPoles(); + if (mergeToProvinces) Provinces.getPoles(); layerIsOn("toggleStates") ? drawStates() : toggleStates(); layerIsOn("toggleBorders") ? drawBorders() : toggleBorders(); layerIsOn("toggleProvinces") && drawProvinces();