From 1faaee48e02a0c317b6adbed3531adec0903474b Mon Sep 17 00:00:00 2001 From: Azgaar Date: Mon, 19 Feb 2024 18:58:59 +0100 Subject: [PATCH] fix: #1041 --- index.html | 2 +- modules/dynamic/auto-update.js | 11 ++ modules/dynamic/editors/states-editor.js | 31 +++-- modules/military-generator.js | 147 ++++++++++++++++++++--- 4 files changed, 154 insertions(+), 37 deletions(-) diff --git a/index.html b/index.html index 04e1c9eef..fe6223617 100644 --- a/index.html +++ b/index.html @@ -8045,7 +8045,7 @@ - + diff --git a/modules/dynamic/auto-update.js b/modules/dynamic/auto-update.js index 1dd5bfead..51c5ee4c5 100644 --- a/modules/dynamic/auto-update.js +++ b/modules/dynamic/auto-update.js @@ -815,5 +815,16 @@ export function resolveVersionConflicts(version) { fitScaleBar(scaleBar, svgWidth, svgHeight); if (!layerIsOn("toggleScaleBar")) scaleBar.style("display", "none"); + + // v1.96.00 changed coloring approach for regiments + armies.selectAll(":scope > g").each(function () { + const fill = this.getAttribute("fill"); + if (!fill) return; + const darkerColor = d3.color(fill).darker().hex(); + this.setAttribute("color", darkerColor); + this.querySelectorAll("g > rect:nth-child(2)").forEach(rect => { + rect.setAttribute("fill", "currentColor"); + }); + }); } } diff --git a/modules/dynamic/editors/states-editor.js b/modules/dynamic/editors/states-editor.js index 24a63bb61..2adcbbc82 100644 --- a/modules/dynamic/editors/states-editor.js +++ b/modules/dynamic/editors/states-editor.js @@ -1394,6 +1394,7 @@ function openStateMergeDialog() { function mergeStates(statesToMerge, rulingStateId) { const rulingState = pack.states[rulingStateId]; + const rulingStateArmy = byId("army" + rulingStateId); // remove states to be merged statesToMerge.forEach(stateId => { @@ -1410,27 +1411,25 @@ function openStateMergeDialog() { emblems.select(`#stateEmblems > use[data-i='${stateId}']`).remove(); // add merged state regiments to the ruling state - state.military.forEach(m => { - const oldId = `regiment${stateId}-${m.i}`; - - const newRegiment = {...m, i: rulingState.military.length}; - rulingState.military.push(newRegiment); - - const newId = `regiment${rulingStateId}-${newRegiment.i}`; + state.military.forEach(regiment => { + const oldId = `regiment${stateId}-${regiment.i}`; + const newIndex = rulingState.military.length; + rulingState.military.push({...regiment, i: newIndex}); + const newId = `regiment${rulingStateId}-${newIndex}`; const note = notes.find(n => n.id === oldId); if (note) note.id = newId; - const rulingStateArmy = armies.select("g#army" + rulingStateId); - armies - .select("g#army" + stateId) - .selectAll("g") - .each(function () { - this.setAttribute("id", newId); - rulingStateArmy.node().appendChild(this); - }); - armies.select("g#army" + stateId).remove(); + const element = byId(oldId); + if (element) { + element.id = newId; + element.dataset.state = rulingStateId; + element.dataset.i = newIndex; + rulingStateArmy.appendChild(element); + } }); + + armies.select("g#army" + stateId).remove(); }); // reassing burgs diff --git a/modules/military-generator.js b/modules/military-generator.js index 648f56377..be56658f5 100644 --- a/modules/military-generator.js +++ b/modules/military-generator.js @@ -10,7 +10,18 @@ window.Military = (function () { const expn = d3.sum(valid.map(s => s.expansionism)); // total expansion const area = d3.sum(valid.map(s => s.area)); // total area - const rate = {x: 0, Ally: -0.2, Friendly: -0.1, Neutral: 0, Suspicion: 0.1, Enemy: 1, Unknown: 0, Rival: 0.5, Vassal: 0.5, Suzerain: -0.5}; + const rate = { + x: 0, + Ally: -0.2, + Friendly: -0.1, + Neutral: 0, + Suspicion: 0.1, + Enemy: 1, + Unknown: 0, + Rival: 0.5, + Vassal: 0.5, + Suzerain: -0.5 + }; const stateModifier = { melee: {Nomadic: 0.5, Highland: 1.2, Lake: 1, Naval: 0.7, Hunting: 1.2, River: 1.1}, @@ -24,14 +35,59 @@ window.Military = (function () { }; const cellTypeModifier = { - nomadic: {melee: 0.2, ranged: 0.5, mounted: 3, machinery: 0.4, naval: 0.3, armored: 1.6, aviation: 1, magical: 0.5}, - wetland: {melee: 0.8, ranged: 2, mounted: 0.3, machinery: 1.2, naval: 1.0, armored: 0.2, aviation: 0.5, magical: 0.5}, - highland: {melee: 1.2, ranged: 1.6, mounted: 0.3, machinery: 3, naval: 1.0, armored: 0.8, aviation: 0.3, magical: 2} + nomadic: { + melee: 0.2, + ranged: 0.5, + mounted: 3, + machinery: 0.4, + naval: 0.3, + armored: 1.6, + aviation: 1, + magical: 0.5 + }, + wetland: { + melee: 0.8, + ranged: 2, + mounted: 0.3, + machinery: 1.2, + naval: 1.0, + armored: 0.2, + aviation: 0.5, + magical: 0.5 + }, + highland: { + melee: 1.2, + ranged: 1.6, + mounted: 0.3, + machinery: 3, + naval: 1.0, + armored: 0.8, + aviation: 0.3, + magical: 2 + } }; const burgTypeModifier = { - nomadic: {melee: 0.3, ranged: 0.8, mounted: 3, machinery: 0.4, naval: 1.0, armored: 1.6, aviation: 1, magical: 0.5}, - wetland: {melee: 1, ranged: 1.6, mounted: 0.2, machinery: 1.2, naval: 1.0, armored: 0.2, aviation: 0.5, magical: 0.5}, + nomadic: { + melee: 0.3, + ranged: 0.8, + mounted: 3, + machinery: 0.4, + naval: 1.0, + armored: 1.6, + aviation: 1, + magical: 0.5 + }, + wetland: { + melee: 1, + ranged: 1.6, + mounted: 0.2, + machinery: 1.2, + naval: 1.0, + armored: 0.2, + aviation: 0.5, + magical: 0.5 + }, highland: {melee: 1.2, ranged: 2, mounted: 0.3, machinery: 3, naval: 1.0, armored: 0.8, aviation: 0.3, magical: 2} }; @@ -40,8 +96,16 @@ window.Military = (function () { const d = s.diplomacy; const expansionRate = minmax(s.expansionism / expn / (s.area / area), 0.25, 4); // how much state expansionism is realized - const diplomacyRate = d.some(d => d === "Enemy") ? 1 : d.some(d => d === "Rival") ? 0.8 : d.some(d => d === "Suspicion") ? 0.5 : 0.1; // peacefulness - const neighborsRateRaw = s.neighbors.map(n => (n ? pack.states[n].diplomacy[s.i] : "Suspicion")).reduce((s, r) => (s += rate[r]), 0.5); + const diplomacyRate = d.some(d => d === "Enemy") + ? 1 + : d.some(d => d === "Rival") + ? 0.8 + : d.some(d => d === "Suspicion") + ? 0.5 + : 0.1; // peacefulness + const neighborsRateRaw = s.neighbors + .map(n => (n ? pack.states[n].diplomacy[s.i] : "Suspicion")) + .reduce((s, r) => (s += rate[r]), 0.5); const neighborsRate = minmax(neighborsRateRaw, 0.3, 3); // neighbors rate s.alert = minmax(rn(expansionRate * diplomacyRate * neighborsRate, 2), 0.1, 5); // alert rate (area modifier) s.temp.platoons = []; @@ -86,8 +150,10 @@ window.Military = (function () { let modifier = cells.pop[i] / 100; // basic rural army in percentages if (culture !== stateObj.culture) modifier = stateObj.form === "Union" ? modifier / 1.2 : modifier / 2; // non-dominant culture - if (religion !== cells.religion[stateObj.center]) modifier = stateObj.form === "Theocracy" ? modifier / 2.2 : modifier / 1.4; // non-dominant religion - if (cells.f[i] !== cells.f[stateObj.center]) modifier = stateObj.type === "Naval" ? modifier / 1.2 : modifier / 1.8; // different landmass + if (religion !== cells.religion[stateObj.center]) + modifier = stateObj.form === "Theocracy" ? modifier / 2.2 : modifier / 1.4; // non-dominant religion + if (cells.f[i] !== cells.f[stateObj.center]) + modifier = stateObj.type === "Naval" ? modifier / 1.2 : modifier / 1.8; // different landmass const type = getType(i); for (const unit of options.military) { @@ -111,7 +177,17 @@ window.Military = (function () { n = 1; } - stateObj.temp.platoons.push({cell: i, a: total, t: total, x, y, u: unit.name, n, s: unit.separate, type: unit.type}); + stateObj.temp.platoons.push({ + cell: i, + a: total, + t: total, + x, + y, + u: unit.name, + n, + s: unit.separate, + type: unit.type + }); } } @@ -153,7 +229,17 @@ window.Military = (function () { n = 1; } - stateObj.temp.platoons.push({cell: b.cell, a: total, t: total, x, y, u: unit.name, n, s: unit.separate, type: unit.type}); + stateObj.temp.platoons.push({ + cell: b.cell, + a: total, + t: total, + x, + y, + u: unit.name, + n, + s: unit.separate, + type: unit.type + }); } } @@ -261,7 +347,8 @@ window.Military = (function () { const army = armies .append("g") .attr("id", "army" + s) - .attr("fill", baseColor); + .attr("fill", baseColor) + .attr("color", darkerColor); const g = army .selectAll("g") @@ -282,7 +369,7 @@ window.Military = (function () { .attr("y", d => d.y) .text(d => getTotal(d)); g.append("rect") - .attr("fill", darkerColor) + .attr("fill", "currentColor") .attr("x", d => x(d) - h) .attr("y", d => y(d)) .attr("width", h) @@ -304,12 +391,13 @@ window.Military = (function () { let army = armies.select("g#army" + s); if (!army.size()) { const baseColor = pack.states[s].color[0] === "#" ? pack.states[s].color : "#999"; + const darkerColor = d3.color(army.attr("fill")).darker().hex(); army = armies .append("g") .attr("id", "army" + s) - .attr("fill", baseColor); + .attr("fill", baseColor) + .attr("color", darkerColor); } - const darkerColor = d3.color(army.attr("fill")).darker().hex(); const g = army .append("g") @@ -320,7 +408,7 @@ window.Military = (function () { g.append("rect").attr("x", x1).attr("y", y1).attr("width", w).attr("height", h); g.append("text").attr("x", reg.x).attr("y", reg.y).text(getTotal(reg)); g.append("rect") - .attr("fill", darkerColor) + .attr("fill", "currentColor") .attr("x", x1 - h) .attr("y", y1) .attr("width", h) @@ -379,7 +467,13 @@ window.Military = (function () { // get default regiment emblem const getEmblem = function (r) { if (!r.n && !Object.values(r.u).length) return "🔰"; // "Newbie" regiment without troops - if (!r.n && pack.states[r.state].form === "Monarchy" && pack.cells.burg[r.cell] && pack.burgs[pack.cells.burg[r.cell]].capital) return "👑"; // "Royal" regiment based in capital + if ( + !r.n && + pack.states[r.state].form === "Monarchy" && + pack.cells.burg[r.cell] && + pack.burgs[pack.cells.burg[r.cell]].capital + ) + return "👑"; // "Royal" regiment based in capital const mainUnit = Object.entries(r.u).sort((a, b) => b[1] - a[1])[0][0]; // unit with more troops in regiment const unit = options.military.find(u => u.name === mainUnit); return unit.icon; @@ -400,7 +494,9 @@ window.Military = (function () { .map(t => `— ${t}: ${r.u[t]}`) .join("\r\n") : null; - const troops = composition ? `\r\n\r\nRegiment composition in ${options.year} ${options.eraShort}:\r\n${composition}.` : ""; + const troops = composition + ? `\r\n\r\nRegiment composition in ${options.year} ${options.eraShort}:\r\n${composition}.` + : ""; const campaign = s.campaigns ? ra(s.campaigns) : null; const year = campaign ? rand(campaign.start, campaign.end) : gauss(options.year - 100, 150, 1, options.year - 6); @@ -409,5 +505,16 @@ window.Military = (function () { notes.push({id: `regiment${s.i}-${r.i}`, name: `${r.icon} ${r.name}`, legend}); }; - return {generate, redraw, getDefaultOptions, getName, generateNote, drawRegiments, drawRegiment, moveRegiment, getTotal, getEmblem}; + return { + generate, + redraw, + getDefaultOptions, + getName, + generateNote, + drawRegiments, + drawRegiment, + moveRegiment, + getTotal, + getEmblem + }; })();