Skip to content

Commit

Permalink
fix: #1041
Browse files Browse the repository at this point in the history
  • Loading branch information
Azgaar committed Feb 19, 2024
1 parent c282ecb commit 1faaee4
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 37 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8045,7 +8045,7 @@
<script src="modules/burgs-and-states.js?v=1.92.00"></script>
<script src="modules/routes-generator.js"></script>
<script src="modules/religions-generator.js?v=1.93.08"></script>
<script src="modules/military-generator.js"></script>
<script src="modules/military-generator.js?v=1.96.00"></script>
<script src="modules/markers-generator.js?v=1.93.04"></script>
<script src="modules/coa-generator.js?v=1.91.05"></script>
<script src="modules/submap.js?v=1.96.00"></script>
Expand Down
11 changes: 11 additions & 0 deletions modules/dynamic/auto-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
}
}
31 changes: 15 additions & 16 deletions modules/dynamic/editors/states-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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
Expand Down
147 changes: 127 additions & 20 deletions modules/military-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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}
};

Expand All @@ -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 = [];
Expand Down Expand Up @@ -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) {
Expand All @@ -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
});
}
}

Expand Down Expand Up @@ -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
});
}
}

Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
};
})();

0 comments on commit 1faaee4

Please sign in to comment.