"use strict"; function editReligions() { if (customization) return; closeDialogs("#religionsEditor, .stable"); if (!layerIsOn("toggleReligions")) toggleReligions(); if (layerIsOn("toggleCultures")) toggleCultures(); if (layerIsOn("toggleStates")) toggleStates(); if (layerIsOn("toggleBiomes")) toggleBiomes(); if (layerIsOn("toggleProvinces")) toggleProvinces(); const body = document.getElementById("religionsBody"); const animate = d3.transition().duration(1500).ease(d3.easeSinIn); drawReligionCenters(); refreshReligionsEditor(); if (modules.editReligions) return; modules.editReligions = true; $("#religionsEditor").dialog({ title: "Religions Editor", resizable: false, width: fitContent(), close: closeReligionsEditor, position: {my: "right top", at: "right-10 top+10", of: "svg"} }); // add listeners document.getElementById("religionsEditorRefresh").addEventListener("click", refreshReligionsEditor); document.getElementById("religionsLegend").addEventListener("click", toggleLegend); document.getElementById("religionsPercentage").addEventListener("click", togglePercentageMode); document.getElementById("religionsManually").addEventListener("click", enterReligionsManualAssignent); document.getElementById("religionsManuallyApply").addEventListener("click", applyReligionsManualAssignent); document.getElementById("religionsManuallyCancel").addEventListener("click", () => exitReligionsManualAssignment()); document.getElementById("religionsAdd").addEventListener("click", enterAddReligionMode); document.getElementById("religionsExport").addEventListener("click", downloadReligionsData); function refreshReligionsEditor() { religionsCollectStatistics(); religionsEditorAddLines(); } function religionsCollectStatistics() { const cells = pack.cells, religions = pack.religions; religions.forEach(r => r.area = r.rural = r.urban = 0); for (const i of cells.i) { if (cells.h[i] < 20) continue; const r = cells.religion[i]; religions[r].area += cells.area[i]; religions[r].rural += cells.pop[i]; if (cells.burg[i]) religions[r].urban += pack.burgs[cells.burg[i]].population; } } // add line for each religion function religionsEditorAddLines() { const unit = areaUnit.value === "square" ? " " + distanceUnitInput.value + "²" : " " + areaUnit.value; let lines = "", totalArea = 0, totalPopulation = 0; for (const r of pack.religions) { if (r.removed) continue; const area = r.area * (distanceScaleInput.value ** 2); const rural = r.rural * populationRate.value; const urban = r.urban * populationRate.value * urbanization.value; const population = rn(rural + urban); const populationTip = `Total population: ${si(population)}; Rural population: ${si(rural)}; Urban population: ${si(urban)}`; totalArea += area; totalPopulation += population; if (r.i) { lines += `
${si(area) + unit}
${si(population)}
`; } else { // No religion (neutral) line lines += `
${si(area) + unit}
${si(population)}
`; } } body.innerHTML = lines; // update footer religionsFooterNumber.innerHTML = pack.religions.filter(r => r.i && !r.removed).length; religionsFooterArea.innerHTML = si(totalArea) + unit; religionsFooterPopulation.innerHTML = si(totalPopulation); religionsFooterArea.dataset.area = totalArea; religionsFooterPopulation.dataset.population = totalPopulation; // add listeners body.querySelectorAll("div.religions").forEach(el => el.addEventListener("mouseenter", ev => religionHighlightOn(ev))); body.querySelectorAll("div.religions").forEach(el => el.addEventListener("mouseleave", ev => religionHighlightOff(ev))); body.querySelectorAll("div.states").forEach(el => el.addEventListener("click", selectReligionOnLineClick)); body.querySelectorAll("rect.zoneFill").forEach(el => el.addEventListener("click", religionChangeColor)); body.querySelectorAll("div > input.religionName").forEach(el => el.addEventListener("input", religionChangeName)); body.querySelectorAll("div > select.religionType").forEach(el => el.addEventListener("input", religionChangeType)); body.querySelectorAll("div > input.religionForm").forEach(el => el.addEventListener("input", religionChangeForm)); body.querySelectorAll("div > input.religionDeity").forEach(el => el.addEventListener("input", religionChangeDeity)); body.querySelectorAll("div > span.icon-arrows-cw").forEach(el => el.addEventListener("click", regenerateDeity)); body.querySelectorAll("div > span.icon-trash-empty").forEach(el => el.addEventListener("click", religionRemove)); if (body.dataset.type === "percentage") {body.dataset.type = "absolute"; togglePercentageMode();} applySorting(religionsHeader); $("#religionsEditor").dialog(); } function getTypeOptions(type) { let options = ""; const types = ["Folk", "Organized", "Cult", "Heresy"]; types.forEach(t => options += ``); return options; } function religionHighlightOn(event) { if (!layerIsOn("toggleReligions")) return; if (customization) return; const religion = +event.target.dataset.id; relig.select("#religion"+religion).raise().transition(animate).attr("stroke-width", 2.5).attr("stroke", "#c13119"); debug.select("#cultureCenter"+religion).raise().transition(animate).attr("r", 8).attr("stroke", "#c13119"); } function religionHighlightOff(event) { if (!layerIsOn("toggleReligions")) return; const religion = +event.target.dataset.id; relig.select("#religion"+religion).transition().attr("stroke-width", null).attr("stroke", null); debug.select("#cultureCenter"+religion).transition().attr("r", 6).attr("stroke", null); } function religionChangeColor() { const el = this; const currentFill = el.getAttribute("fill"); const religion = +el.parentNode.parentNode.dataset.id; const callback = function(fill) { el.setAttribute("fill", fill); pack.religions[religion].color = fill; relig.select("#religion"+religion).attr("fill", fill); debug.select("#cultureCenter"+religion).attr("fill", fill); } openPicker(currentFill, callback); } function religionChangeName() { const religion = +this.parentNode.dataset.id; this.parentNode.dataset.name = this.value; pack.religions[religion].name = this.value; } function religionChangeType() { const religion = +this.parentNode.dataset.id; this.parentNode.dataset.type = this.value; pack.religions[religion].type = this.value; } function religionChangeForm() { const religion = +this.parentNode.dataset.id; this.parentNode.dataset.form = this.value; pack.religions[religion].form = this.value; } function religionChangeDeity() { const religion = +this.parentNode.dataset.id; this.parentNode.dataset.deity = this.value; pack.religions[religion].deity = this.value; } function regenerateDeity() { const religion = +this.parentNode.dataset.id; const culture = pack.religions[religion].culture; const deity = Religions.getDeityName(culture); this.parentNode.dataset.deity = deity; pack.religions[religion].deity = deity; this.nextElementSibling.value = deity; } function religionRemove() { if (customization) return; const religion = +this.parentNode.dataset.id; relig.select("#religion"+religion).remove(); debug.select("#cultureCenter"+religion).remove(); pack.cells.religion.forEach((r, i) => {if(r === religion) pack.cells.religion[i] = 0;}); pack.religions[religion].removed = true; refreshReligionsEditor(); } function drawReligionCenters() { const tooltip = "Drag to move the religion center"; debug.select("#religionCenters").remove(); const religionCenters = debug.append("g").attr("id", "religionCenters") .attr("stroke-width", 2).attr("stroke", "#444444").style("cursor", "move"); const data = pack.religions.filter(r => r.center && !r.removed); religionCenters.selectAll("circle").data(data).enter().append("circle") .attr("id", d => "cultureCenter"+d.i).attr("data-id", d => d.i) .attr("r", 6).attr("fill", d => pack.religions[d.i].color) .attr("cx", d => pack.cells.p[d.center][0]).attr("cy", d => pack.cells.p[d.center][1]) .on("mouseenter", d => {tip(tooltip, true); body.querySelector(`div[data-id='${d.i}']`).classList.add("selected"); religionHighlightOn(event);}) .on("mouseleave", d => {tip('', true); body.querySelector(`div[data-id='${d.i}']`).classList.remove("selected"); religionHighlightOff(event);}) .call(d3.drag().on("start", religionCenterDrag)); } function religionCenterDrag() { const el = d3.select(this); const r = +this.dataset.id; d3.event.on("drag", () => { el.attr("cx", d3.event.x).attr("cy", d3.event.y); const cell = findCell(d3.event.x, d3.event.y); if (pack.cells.h[cell] < 20) return; // ignore dragging on water pack.religions[r].center = cell; }); } function toggleLegend() { if (legend.selectAll("*").size()) {clearLegend(); return;}; // hide legend const data = pack.religions.filter(r => r.i && !r.removed && r.area).sort((a, b) => b.area - a.area).map(r => [r.i, r.color, r.name]); drawLegend("Religions", data); } function togglePercentageMode() { if (body.dataset.type === "absolute") { body.dataset.type = "percentage"; const totalArea = +religionsFooterArea.dataset.area; const totalPopulation = +religionsFooterPopulation.dataset.population; body.querySelectorAll(":scope > div").forEach(function(el) { el.querySelector(".biomeArea").innerHTML = rn(+el.dataset.area / totalArea * 100) + "%"; el.querySelector(".culturePopulation").innerHTML = rn(+el.dataset.population / totalPopulation * 100) + "%"; }); } else { body.dataset.type = "absolute"; religionsEditorAddLines(); } } function enterReligionsManualAssignent() { if (!layerIsOn("toggleReligions")) toggleReligions(); customization = 7; relig.append("g").attr("id", "temp"); document.querySelectorAll("#religionsBottom > button").forEach(el => el.style.display = "none"); document.getElementById("religionsManuallyButtons").style.display = "inline-block"; debug.select("#religionCenters").style("display", "none"); religionsEditor.querySelectorAll(".hide").forEach(el => el.classList.add("hidden")); religionsFooter.style.display = "none"; body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "none"); $("#religionsEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg"}}); tip("Click on religion to select, drag the circle to change religion", true); viewbox.style("cursor", "crosshair") .on("click", selectReligionOnMapClick) .call(d3.drag().on("start", dragReligionBrush)) .on("touchmove mousemove", moveReligionBrush); body.querySelector("div").classList.add("selected"); } function selectReligionOnLineClick(i) { if (customization !== 7) return; body.querySelector("div.selected").classList.remove("selected"); this.classList.add("selected"); } function selectReligionOnMapClick() { const point = d3.mouse(this); const i = findCell(point[0], point[1]); if (pack.cells.h[i] < 20) return; const assigned = relig.select("#temp").select("polygon[data-cell='"+i+"']"); const religion = assigned.size() ? +assigned.attr("data-religion") : pack.cells.religion[i]; body.querySelector("div.selected").classList.remove("selected"); body.querySelector("div[data-id='"+religion+"']").classList.add("selected"); } function dragReligionBrush() { const r = +religionsManuallyBrushNumber.value; d3.event.on("drag", () => { if (!d3.event.dx && !d3.event.dy) return; const p = d3.mouse(this); moveCircle(p[0], p[1], r); const found = r > 5 ? findAll(p[0], p[1], r) : [findCell(p[0], p[1], r)]; const selection = found.filter(isLand); if (selection) changeReligionForSelection(selection); }); } // change religion within selection function changeReligionForSelection(selection) { const temp = relig.select("#temp"); const selected = body.querySelector("div.selected"); const r = +selected.dataset.id; // religionNew const color = pack.religions[r].color || "#ffffff"; selection.forEach(function(i) { const exists = temp.select("polygon[data-cell='"+i+"']"); const religionOld = exists.size() ? +exists.attr("data-religion") : pack.cells.religion[i]; if (r === religionOld) return; // change of append new element if (exists.size()) exists.attr("data-religion", r).attr("fill", color); else temp.append("polygon").attr("data-cell", i).attr("data-religion", r).attr("points", getPackPolygon(i)).attr("fill", color); }); } function moveReligionBrush() { showMainTip(); const point = d3.mouse(this); const radius = +religionsManuallyBrushNumber.value; moveCircle(point[0], point[1], radius); } function applyReligionsManualAssignent() { const changed = relig.select("#temp").selectAll("polygon"); changed.each(function() { const i = +this.dataset.cell; const r = +this.dataset.religion; pack.cells.religion[i] = r; }); if (changed.size()) { drawReligions(); refreshReligionsEditor(); } exitReligionsManualAssignment(); } function exitReligionsManualAssignment(close) { customization = 0; relig.select("#temp").remove(); removeCircle(); document.querySelectorAll("#religionsBottom > button").forEach(el => el.style.display = "inline-block"); document.getElementById("religionsManuallyButtons").style.display = "none"; religionsEditor.querySelectorAll(".hide").forEach(el => el.classList.remove("hidden")); religionsFooter.style.display = "block"; body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "all"); if(!close) $("#religionsEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg"}}); debug.select("#religionCenters").style("display", null); restoreDefaultEvents(); clearMainTip(); const selected = body.querySelector("div.selected"); if (selected) selected.classList.remove("selected"); } function enterAddReligionMode() { if (this.classList.contains("pressed")) {exitAddReligionMode(); return;}; customization = 8; this.classList.add("pressed"); tip("Click on the map to add a new religion", true); viewbox.style("cursor", "crosshair").on("click", addReligion); body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "none"); } function exitAddReligionMode() { customization = 0; restoreDefaultEvents(); clearMainTip(); body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "all"); if (religionsAdd.classList.contains("pressed")) religionsAdd.classList.remove("pressed"); } function addReligion() { const point = d3.mouse(this); const center = findCell(point[0], point[1]); if (pack.cells.h[center] < 20) {tip("You cannot place religion center into the water. Please click on a land cell", false, "error"); return;} const occupied = pack.religions.some(r => !r.removed && r.center === center); if (occupied) {tip("This cell is already a religion center. Please select a different cell", false, "error"); return;} if (d3.event.shiftKey === false) exitAddReligionMode(); Religions.add(center); drawReligionCenters(); religionsEditorAddLines(); } function downloadReligionsData() { const unit = areaUnit.value === "square" ? distanceUnitInput.value + "2" : areaUnit.value; let data = "Id,Religion,Color,Type,Form,Deity,Area "+unit+",Population\n"; // headers body.querySelectorAll(":scope > div").forEach(function(el) { data += el.dataset.id + ","; data += el.dataset.name + ","; data += el.dataset.color + ","; data += el.dataset.type + ","; data += el.dataset.form + ","; data += el.dataset.deity.replace(",", " -") + ","; data += el.dataset.area + ","; data += el.dataset.population + "\n"; }); const dataBlob = new Blob([data], {type: "text/plain"}); const url = window.URL.createObjectURL(dataBlob); const link = document.createElement("a"); document.body.appendChild(link); link.download = "religions_data" + Date.now() + ".csv"; link.href = url; link.click(); window.setTimeout(function() {window.URL.revokeObjectURL(url);}, 2000); } function closeReligionsEditor() { debug.select("#religionCenters").remove(); exitReligionsManualAssignment("close"); exitAddReligionMode(); } }