This commit is contained in:
Azgaar 2020-03-28 23:48:56 +03:00
parent c8f758ab3c
commit d1c795494d
12 changed files with 215 additions and 117 deletions

View file

@ -10,11 +10,20 @@
console.time("generateMilitaryForces");
cells = pack.cells, p = cells.p, states = pack.states;
const valid = states.filter(s => s.i && !s.removed); // valid states
if (!options.military) options.military = getDefaultOptions();
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:-.2, Friendly:-.1, Neutral:0, Suspicion:.1, Enemy:1, Unknown:0, Rival:.5, Vassal:.5, Suzerain:-.5};
const stateModifier = {
"melee": {"Nomadic":.5, "Highland":1.2, "Lake":1, "Naval":.7, "Hunting":1.2, "River":1.1},
"ranged": {"Nomadic":.9, "Highland":1.3, "Lake":1, "Naval":.8, "Hunting":2, "River":.8},
"mounted": {"Nomadic":2.3, "Highland":.6, "Lake":.7, "Naval":.3, "Hunting":.7, "River":.8},
"machinery":{"Nomadic":.8, "Highland":1.4, "Lake":1.1, "Naval":1.4, "Hunting":.4, "River":1.1},
"naval": {"Nomadic":.5, "Highland":.5, "Lake":1.2, "Naval":1.8, "Hunting":.7, "River":1.2}
};
valid.forEach(s => {
const temp = s.temp = {}, d = s.diplomacy;
const expansionRate = Math.min(Math.max((s.expansionism / expn) / (s.area / area), .25), 4); // how much state expansionism is realized
@ -25,19 +34,10 @@
// apply overall state modifiers for unit types based on state features
for (const unit of options.military) {
let modifier = 1;
if (unit.type === "mounted") {
if (s.type === "Naval") modifier /= 1.4;
if (s.form === "Horde") modifier *= 2;
} else if (unit.type === "ranged") {
if (s.type === "Hunting") modifier *= 1.4;
} else if (unit.type === "naval") {
if (s.type === "Naval") modifier *= 2; else
if (s.type === "River") modifier *= 1.2; else
if (s.type === "Nomadic") modifier /= 1.4;
if (s.form === "Republic") modifier *= 1.2;
}
if (!stateModifier[unit.type]) continue;
let modifier = stateModifier[unit.type][s.type] || 1;
if (unit.type === "mounted" && s.form === "Horde") modifier *= 2; else
if (unit.type === "naval" && s.form === "Republic") modifier *= 1.2;
temp[unit.name] = modifier * s.alert;
}
@ -76,9 +76,10 @@
if (u.type === "ranged") army *= 1.4; else
if (u.type === "mounted") army /= 3;
}
if (highland) { // highlands special rules
if (u.type === "ranged") army *= 2; else
if (u.type === "melee") army *= 1.2; else
if (u.type === "ranged") army *= 1.6; else
if (u.type === "mounted") army /= 3;
}
@ -93,41 +94,41 @@
for (const b of pack.burgs) {
if (!b.i || b.removed || !b.state || !b.population) continue;
const s = states[b.state]; // burg state
let m = b.population * urbanization.value / 100; // basic urban army in percentages
if (b.capital) m *= 1.2; // capital has household troops
if (b.culture !== s.culture) m = s.form === "Union" ? m / 1.2 : m / 2; // non-dominant culture
if (cells.religion[b.cell] !== cells.religion[s.center]) m = s.form === "Theocracy" ? m / 2.2 : m / 1.4; // non-dominant religion
if (cells.f[b.cell] !== cells.f[s.center]) m = s.type === "Naval" ? m / 1.2 : m / 1.8; // different landmass
const biome = cells.biome[b.cell]; // burg biome
const nomadic = [1, 2, 3, 4].includes(biome);
const wetland = [7, 8, 9, 12].includes(biome);
const highland = cells.h[b.cell] >= 70;
for (const u of options.military) {
const perc = +u.urban;
if (isNaN(perc) || perc <= 0) continue;
let army = m * perc; // basic army for rural cell
if (u.type === "naval") {
if (!b.port) continue; // only ports have naval units
army *= normalizeNaval(pack.features[b.port].ports);
}
if (nomadic) { // "nomadic" biomes special rules
if (u.type === "melee") army /= 3; else
if (u.type === "machinery") army /= 2; else
if (u.type === "mounted") army *= 3;
}
if (wetland) { // "wet" biomes special rules
if (u.type === "melee") army *= 1.2; else
if (u.type === "ranged") army *= 1.4; else
if (u.type === "machinery") army *= 1.2; else
if (u.type === "mounted") army /= 4;
}
if (highland) { // highlands special rules
if (u.type === "ranged") army *= 2; else
if (u.type === "naval") army /= 3; else
@ -196,8 +197,18 @@
console.timeEnd("generateMilitaryForces");
}
function drawRegiments(regiments, s) {
const size = +armies.attr("data-size");
const getDefaultOptions = function() {
return [
{name:"infantry", rural:.25, urban:.2, crew:1, type:"melee", separate:0},
{name:"archers", rural:.12, urban:.2, crew:1, type:"ranged", separate:0},
{name:"cavalry", rural:.12, urban:.03, crew:3, type:"mounted", separate:0},
{name:"artillery", rural:0, urban:.03, crew:8, type:"machinery", separate:0},
{name:"fleet", rural:0, urban:.015, crew:100, type:"naval", separate:1}
];
}
const drawRegiments = function(regiments, s) {
const size = +armies.attr("box-size");
const w = d => d.n ? size * 4 : size * 6;
const h = size * 2;
const x = d => rn(d.x - w(d) / 2, 2);
@ -216,7 +227,7 @@
}
const drawRegiment = function(reg, s, x = reg.x, y = reg.y) {
const size = +armies.attr("data-size");
const size = +armies.attr("box-size");
const w = reg.n ? size * 4 : size * 6;
const h = size * 2;
const x1 = rn(x - w / 2, 2);
@ -236,7 +247,7 @@
const getTotal = reg => reg.a > (reg.n ? 999 : 99999) ? si(reg.a) : reg.a;
const getName = function(r, regiments) {
const proper = r.n ? null :
const proper = r.n ? null :
cells.province[r.cell] ? pack.provinces[cells.province[r.cell]].name :
cells.burg[r.cell] ? pack.burgs[cells.burg[r.cell]].name : null
const number = nth(regiments.filter(reg => reg.n === r.n && reg.i < r.i).length+1);
@ -271,6 +282,6 @@
notes.push({id:`regiment${s.i}-${r.i}`, name:`${r.icon} ${r.name}`, legend});
}
return {generate, getName, generateNote, drawRegiment, getTotal, getEmblem};
return {generate, getDefaultOptions, getName, generateNote, drawRegiments, drawRegiment, getTotal, getEmblem};
})));

View file

@ -74,7 +74,7 @@
// directly connect first port with the farthest one on the same island to remove gap
void function() {
if (pack.features[f].type === "lake") return;
if (!pack.features[f] || pack.features[f].type === "lake") return;
const portsOnIsland = ports.filter(b => cells.f[b.cell] === cells.f[first]);
if (portsOnIsland.length < 4) return;
const opposite = ports[d3.scan(portsOnIsland, (a, b) => ((b.y - ports[0].y) ** 2 + (b.x - ports[0].x) ** 2) - ((a.y - ports[0].y) ** 2 + (a.x - ports[0].x) ** 2))].cell;

View file

@ -231,7 +231,7 @@ function getMapData() {
barSize.value, barLabel.value, barBackOpacity.value, barBackColor.value,
barPosX.value, barPosY.value, populationRate.value, urbanization.value,
mapSizeOutput.value, latitudeOutput.value, temperatureEquatorOutput.value,
temperaturePoleOutput.value, precOutput.value, JSON.stringify(options.winds),
temperaturePoleOutput.value, precOutput.value, JSON.stringify(options),
mapName.value].join("|");
const coords = JSON.stringify(mapCoordinates);
const biomes = [biomesData.color, biomesData.habitability, biomesData.name].join("|");
@ -575,7 +575,7 @@ function parseLoadedData(data) {
if (settings[16]) temperatureEquatorInput.value = temperatureEquatorOutput.value = settings[16];
if (settings[17]) temperaturePoleInput.value = temperaturePoleOutput.value = settings[17];
if (settings[18]) precInput.value = precOutput.value = settings[18];
if (settings[19]) options.winds = JSON.parse(settings[19]);
if (settings[19]) options = JSON.parse(settings[19]);
if (settings[20]) mapName.value = settings[20];
}()
@ -721,6 +721,7 @@ function parseLoadedData(data) {
if (prec.selectAll("circle").size()) turnButtonOn("togglePrec"); else turnButtonOff("togglePrec");
if (labels.style("display") !== "none") turnButtonOn("toggleLabels"); else turnButtonOff("toggleLabels");
if (icons.style("display") !== "none") turnButtonOn("toggleIcons"); else turnButtonOff("toggleIcons");
if (armies.selectAll("*").size() && armies.style("display") !== "none") turnButtonOn("toggleMilitary"); else turnButtonOff("toggleMilitary");
if (markers.selectAll("*").size() && markers.style("display") !== "none") turnButtonOn("toggleMarkers"); else turnButtonOff("toggleMarkers");
if (ruler.style("display") !== "none") turnButtonOn("toggleRulers"); else turnButtonOff("toggleRulers");
if (scaleBar.style("display") !== "none") turnButtonOn("toggleScaleBar"); else turnButtonOff("toggleScaleBar");
@ -774,6 +775,7 @@ function parseLoadedData(data) {
// 1.0 adds state relations, provinces, forms and full names
provs = viewbox.insert("g", "#borders").attr("id", "provs").attr("opacity", .6);
BurgsAndStates.collectStatistics();
BurgsAndStates.generateCampaigns();
BurgsAndStates.generateDiplomacy();
BurgsAndStates.defineStateForms();
drawStates();
@ -905,7 +907,6 @@ function parseLoadedData(data) {
});
// v 1.21 added rivers data to pack
pack.rivers = []; // rivers data
rivers.selectAll("path").each(function() {
const i = +this.id.slice(5);
@ -937,6 +938,24 @@ function parseLoadedData(data) {
if (!f.i) continue;
f.ports = pack.burgs.filter(b => !b.removed && b.port === f.i).length;
}
// v 1.3 added global options object
const winds = options.slice(); // previostly wnd was saved in settings[19]
const year = rand(100, 2000);
const era = Names.getBaseShort(P(.7) ? 1 : rand(nameBases.length)) + " Era";
const eraShort = era[0] + "E";
const military = Military.getDefaultOptions();
options = {winds, year, era, eraShort, military};
// v 1.3 added campaings data for all states
BurgsAndStates.generateCampaigns();
// v 1.3 added militry layer
svg.select("defs").append("style").text(armiesStyle()); // add armies style
armies = viewbox.insert("g", "#icons").attr("id", "armies");
armies.attr("opacity", 1).attr("fill-opacity", 1).attr("font-size", 6).attr("box-size", 3).attr("stroke", "#000").attr("stroke-width", .3);
turnButtonOn("toggleMilitary");
Military.generate();
}
}()

View file

@ -45,8 +45,8 @@ function showDataTip(e) {
let dataTip = e.target.dataset.tip;
if (!dataTip && e.target.parentNode.dataset.tip) dataTip = e.target.parentNode.dataset.tip;
if (!dataTip) return;
const tooltip = lang === "en" ? dataTip : translate(e.target.dataset.t || e.target.parentNode.dataset.t, dataTip);
tip(tooltip);
//const tooltip = lang === "en" ? dataTip : translate(e.target.dataset.t || e.target.parentNode.dataset.t, dataTip);
tip(dataTip);
}
function moved() {
@ -403,7 +403,8 @@ document.addEventListener("keyup", event => {
else if (key === 65) togglePrec(); // "A" to toggle Precipitation layer
else if (key === 76) toggleLabels(); // "L" to toggle Labels layer
else if (key === 73) toggleIcons(); // "I" to toggle Icons layer
else if (key === 77) toggleMarkers(); // "M" to toggle Markers layer
else if (key === 77) toggleMilitary(); // "M" to toggle Military layer
else if (key === 75) toggleMarkers(); // "K" to toggle Markers layer
else if (key === 187) toggleRulers(); // Equal (=) to toggle Rulers
else if (key === 189) toggleScaleBar(); // Minus (-) to toggle Scale bar

View file

@ -36,6 +36,7 @@ function getDefaultPresets() {
"heightmap": ["toggleHeight", "toggleRivers"],
"physical": ["toggleCoordinates", "toggleHeight", "toggleRivers", "toggleScaleBar"],
"poi": ["toggleBorders", "toggleHeight", "toggleIcons", "toggleMarkers", "toggleRivers", "toggleRoutes", "toggleScaleBar"],
"military": ["toggleBorders", "toggleIcons", "toggleLabels", "toggleMilitary", "toggleRivers", "toggleRoutes", "toggleScaleBar", "toggleStates"],
"landmass": ["toggleScaleBar"]
}
}
@ -1058,6 +1059,18 @@ function toggleRoutes(event) {
}
}
function toggleMilitary() {
if (!layerIsOn("toggleMilitary")) {
turnButtonOn("toggleMilitary");
$('#armies').fadeIn();
if (event && isCtrlClick(event)) editStyle("armies");
} else {
if (event && isCtrlClick(event)) {editStyle("armies"); return;}
$('#armies').fadeOut();
turnButtonOff("toggleMilitary");
}
}
function toggleMarkers(event) {
if (!layerIsOn("toggleMarkers")) {
turnButtonOn("toggleMarkers");

View file

@ -4,6 +4,7 @@ function overviewMilitary() {
closeDialogs("#militaryOverview, .stable");
if (!layerIsOn("toggleStates")) toggleStates();
if (!layerIsOn("toggleBorders")) toggleBorders();
if (!layerIsOn("toggleMilitary")) toggleMilitary();
const body = document.getElementById("militaryBody");
addLines();
@ -22,6 +23,7 @@ function overviewMilitary() {
document.getElementById("militaryOverviewRefresh").addEventListener("click", addLines);
document.getElementById("militaryPercentage").addEventListener("click", togglePercentageMode);
document.getElementById("militaryOptionsButton").addEventListener("click", militaryCustomize);
document.getElementById("militaryRegimentsList").addEventListener("click", () => overviewRegiments(-1));
document.getElementById("militaryOverviewRecalculate").addEventListener("click", militaryRecalculate);
document.getElementById("militaryExport").addEventListener("click", downloadMilitaryData);
@ -120,9 +122,11 @@ function overviewMilitary() {
}
function stateHighlightOn(event) {
if (!layerIsOn("toggleStates")) return;
const state = +event.target.dataset.id;
if (customization || !state) return;
armies.select("#army"+state).transition().duration(2000).style("fill", "#ff0000");
if (!layerIsOn("toggleStates")) return;
const d = regions.select("#state"+state).attr("d");
const path = debug.append("path").attr("class", "highlight").attr("d", d)
@ -132,8 +136,6 @@ function overviewMilitary() {
const l = path.node().getTotalLength(), dur = (l + 5000) / 2;
const i = d3.interpolateString("0," + l, l + "," + l);
path.transition().duration(dur).attrTween("stroke-dasharray", function() {return t => i(t)});
armies.select("#army"+state).transition().duration(dur).style("fill", "#ff0000");
}
function stateHighlightOff(event) {
@ -161,7 +163,7 @@ function overviewMilitary() {
el.querySelectorAll("div").forEach(function(div) {
const type = div.dataset.type;
if (type === "rate") return;
div.textContent = rn(+el.dataset[type] / total(type) * 100) + "%";
div.textContent = total(type) ? rn(+el.dataset[type] / total(type) * 100) + "%" : "0%";
});
});
} else {
@ -181,7 +183,7 @@ function overviewMilitary() {
position: {my: "center", at: "center", of: "svg"},
buttons: {
Apply: function() {applyMilitaryOptions(); $(this).dialog("close");},
Add: () => addUnitLine({name: "custom", rural: 0.2, urban: 0.5, crew: 1, type: "default"}),
Add: () => addUnitLine({name: "custom"+rand(1000), rural: .2, urban: .5, crew: 1, type: "default"}),
Restore: restoreDefaultUnits,
Cancel: function() {$(this).dialog("close");}
}, open: function() {
@ -215,11 +217,7 @@ function overviewMilitary() {
function restoreDefaultUnits() {
removeUnitLines();
[{name:"infantry", rural:.25, urban:.2, crew:1, type:"melee", separate:0},
{name:"archers", rural:.12, urban:.2, crew:1, type:"ranged", separate:0},
{name:"cavalry", rural:.12, urban:.03, crew:3, type:"mounted", separate:0},
{name:"artillery", rural:0, urban:.03, crew:8, type:"machinery", separate:0},
{name:"fleet", rural:0, urban:.015, crew:100, type:"naval", separate:1}].map(u => addUnitLine(u));
Military.getDefaultOptions().map(u => addUnitLine(u));
}
function applyMilitaryOptions() {
@ -236,7 +234,7 @@ function overviewMilitary() {
}
function militaryRecalculate() {
alertMessage.innerHTML = "Are you sure you want to recalculate military forces for all states?";
alertMessage.innerHTML = "Are you sure you want to recalculate military forces for all states?<br>Regiments for all states will be regenerated";
$("#alert").dialog({resizable: false, title: "Remove regiment",
buttons: {
Recalculate: function() {

View file

@ -1,12 +1,12 @@
"use strict";
function editRegiment() {
function editRegiment(selector) {
if (customization) return;
closeDialogs(".stable");
// if (!layerIsOn("toggleArmies")) toggleArmies();
if (!layerIsOn("toggleMilitary")) toggleMilitary();
armies.selectAll(":scope > g").classed("draggable", true);
armies.selectAll(":scope > g > g").call(d3.drag().on("drag", dragRegiment));
elSelected = d3.event.target.parentElement; // select g element
elSelected = selector ? document.querySelector(selector) : d3.event.target.parentElement; // select g element
if (!pack.states[elSelected.dataset.state]) return;
if (!regiment()) return;
updateRegimentData(regiment());

View file

@ -2,9 +2,10 @@
function overviewRegiments(state) {
if (customization) return;
closeDialogs(".stable");
if (!layerIsOn("toggleMilitary")) toggleMilitary();
const body = document.getElementById("regimentsBody");
updateFilter();
updateFilter(state);
addLines();
$("#regimentsOverview").dialog();
@ -13,21 +14,16 @@ function overviewRegiments(state) {
updateHeaders();
$("#regimentsOverview").dialog({
title: "Regiments Overview", resizable: false, width: fitContent(),
position: {my: "center", at: "center", of: "svg"}
title: "Military Overview", resizable: false, width: fitContent(),
position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}
});
// add listeners
document.getElementById("regimentsOverviewRefresh").addEventListener("click", addLines);
document.getElementById("regimentsPercentage").addEventListener("click", togglePercentageMode);
document.getElementById("regimentsAddNew").addEventListener("click", toggleAddRegiment);
document.getElementById("regimentsAddNew").addEventListener("click", toggleAdd);
document.getElementById("regimentsExport").addEventListener("click", downloadRegimentsData);
document.getElementById("regimentsFilter").addEventListener("change", filterRegiments);
body.addEventListener("click", function(ev) {
const el = ev.target, line = el.parentNode, state = +line.dataset.id;
//if (el.tagName === "SPAN") showRegimentList(state);
});
document.getElementById("regimentsFilter").addEventListener("change", addLines);
// update military types in header and tooltips
function updateHeaders() {
@ -45,6 +41,7 @@ function overviewRegiments(state) {
// add line for each state
function addLines() {
const state = +regimentsFilter.value;
body.innerHTML = "";
let lines = "";
const regiments = [];
@ -64,7 +61,7 @@ function overviewRegiments(state) {
<input data-tip="Regiment's name" style="width:13em" value="${r.name}" readonly>
${lineData}
<div data-type="total" data-tip="Total military personnel (not considering crew)" style="font-weight: bold">${r.a}</div>
<span data-tip="Edit regiment" class="icon-pencil pointer"></span>
<span data-tip="Edit regiment" onclick="editRegiment('#regiment${s.i}-${r.i}')" class="icon-pencil pointer"></span>
</div>`;
regiments.push(r);
@ -86,7 +83,7 @@ function overviewRegiments(state) {
body.querySelectorAll("div.states").forEach(el => el.addEventListener("mouseleave", ev => regimentHighlightOff(ev)));
}
function updateFilter() {
function updateFilter(state) {
const filter = document.getElementById("regimentsFilter");
filter.options.length = 0; // remove all options
filter.options.add(new Option(`all`, -1, false, state === -1));
@ -94,11 +91,6 @@ function overviewRegiments(state) {
statesSorted.forEach(s => filter.options.add(new Option(s.name, s.i, false, s.i == state)));
}
function filterRegiments() {
state = +this.value;
addLines();
}
function regimentHighlightOn(event) {
const state = +event.target.dataset.s;
const id = +event.target.dataset.id;
@ -128,7 +120,7 @@ function overviewRegiments(state) {
el.querySelectorAll("div").forEach(function(div) {
const type = div.dataset.type;
if (type === "rate") return;
div.textContent = rn(+el.dataset[type] / total(type) * 100) + "%";
div.textContent = total(type) ? rn(+el.dataset[type] / total(type) * 100) + "%" : "0%";
});
});
} else {
@ -137,8 +129,35 @@ function overviewRegiments(state) {
}
}
function toggleAddRegiment() {
function toggleAdd() {
document.getElementById("regimentsAddNew").classList.toggle("pressed");
if (document.getElementById("regimentsAddNew").classList.contains("pressed")) {
viewbox.style("cursor", "crosshair").on("click", addRegimentOnClick);
tip("Click on map to create new regiment or fleet", true);
if (regimentAdd.offsetParent) regimentAdd.classList.add("pressed");
} else {
clearMainTip();
viewbox.on("click", clicked).style("cursor", "default");
addLines();
if (regimentAdd.offsetParent) regimentAdd.classList.remove("pressed");
}
}
function addRegimentOnClick() {
const state = +regimentsFilter.value;
if (state === -1) {tip("Please select state from the list", false, "error"); return;}
const point = d3.mouse(this);
const cell = findCell(point[0], point[1]);
const x = pack.cells.p[cell][0], y = pack.cells.p[cell][1];
const military = pack.states[state].military;
const i = military.length ? last(military).i + 1 : 0;
const n = +(pack.cells.h[cell] < 20); // naval or land
const reg = {a:0, cell, i, n, u:{}, x, y, icon:"🛡️"};
reg.name = Military.getName(reg, military);
military.push(reg);
Military.drawRegiment(reg, state);
toggleAdd();
}
function downloadRegimentsData() {

File diff suppressed because one or more lines are too long

View file

@ -64,6 +64,7 @@ function processFeatureRegeneration(event, button) {
if (button === "regenerateStates") regenerateStates(); else
if (button === "regenerateProvinces") regenerateProvinces(); else
if (button === "regenerateReligions") regenerateReligions(); else
if (button === "regenerateMilitary") regenerateMilitary(); else
if (button === "regenerateMarkers") regenerateMarkers(event); else
if (button === "regenerateZones") regenerateZones(event);
}
@ -238,6 +239,12 @@ function regenerateReligions() {
if (!layerIsOn("toggleReligions")) toggleReligions(); else drawReligions();
}
function regenerateMilitary() {
Military.generate();
if (!layerIsOn("toggleMilitary")) toggleMilitary();
if (document.getElementById("militaryOverviewRefresh").offsetParent) militaryOverviewRefresh.click();
}
function regenerateMarkers(event) {
let number = gauss(1, .5, .3, 5, 2);