"use strict"; // Tectonic Plate Editor // Visualizes tectonic plates and allows editing plate properties (type, velocity) // then regenerates terrain from the modified plate configuration let tectonicViewMode = "plates"; // "plates" or "heights" function editTectonics() { if (customization) return tip("Please exit the customization mode first", false, "error"); if (!window.tectonicGenerator || !window.tectonicMetadata) { return tip("Tectonic data not available. Generate a map using a Tectonic template first.", false, "error"); } closeDialogs(".stable"); tectonicViewMode = "plates"; const plates = window.tectonicGenerator.getPlates(); const plateIds = window.tectonicMetadata.plateIds; const plateColors = generatePlateColors(plates.length); drawPlateOverlay(plateIds, plateColors, plates); buildPlateList(plates, plateColors); $("#tectonicEditor").dialog({ title: "Tectonic Plate Editor", resizable: false, width: "22em", position: {my: "right top", at: "right-10 top+10", of: "svg"}, close: closeTectonicEditor }); if (modules.editTectonics) return; modules.editTectonics = true; byId("tectonicRegenerate").addEventListener("click", regenerateFromEditor); byId("tectonicToggleOverlay").addEventListener("click", togglePlateOverlay); byId("tectonicApplyMap").addEventListener("click", applyToMap); byId("tectonicClose").addEventListener("click", () => $("#tectonicEditor").dialog("close")); } function generatePlateColors(count) { const colors = []; for (let i = 0; i < count; i++) { const hue = (i * 360 / count + 15) % 360; const sat = 60 + (i % 3) * 15; const lit = 45 + (i % 2) * 15; colors.push(`hsl(${hue}, ${sat}%, ${lit}%)`); } return colors; } // Height-to-color function matching FMG's heightmap editor function tectonicHeightColor(h) { if (h < 20) { // Ocean: deep blue to light blue const t = h / 20; const r = Math.round(30 + t * 40); const g = Math.round(60 + t * 80); const b = Math.round(120 + t * 100); return `rgb(${r},${g},${b})`; } else { // Land: green to brown to white const t = (h - 20) / 80; if (t < 0.3) { const s = t / 0.3; return `rgb(${Math.round(80 + s * 60)},${Math.round(160 + s * 40)},${Math.round(60 + s * 20)})`; } else if (t < 0.7) { const s = (t - 0.3) / 0.4; return `rgb(${Math.round(140 + s * 60)},${Math.round(200 - s * 80)},${Math.round(80 - s * 40)})`; } else { const s = (t - 0.7) / 0.3; return `rgb(${Math.round(200 + s * 55)},${Math.round(120 + s * 135)},${Math.round(40 + s * 215)})`; } } } function drawPlateOverlay(plateIds, plateColors, plates) { viewbox.select("#tectonicOverlay").remove(); const overlay = viewbox.insert("g", "#terrs").attr("id", "tectonicOverlay"); const numCells = plateIds.length; for (let i = 0; i < numCells; i++) { const pid = plateIds[i]; if (pid < 0 || pid >= plates.length) continue; const points = getGridPolygon(i); if (!points) continue; overlay.append("polygon") .attr("points", points) .attr("fill", plateColors[pid]) .attr("fill-opacity", 0.35) .attr("stroke", plateColors[pid]) .attr("stroke-opacity", 0.5) .attr("stroke-width", 0.2) .attr("data-plate", pid) .on("click", function () { highlightPlate(pid, plateColors); }); } drawVelocityArrows(overlay, plates, plateIds, plateColors); } function drawHeightOverlay(heights) { viewbox.select("#tectonicOverlay").remove(); const overlay = viewbox.insert("g", "#terrs").attr("id", "tectonicOverlay"); const numCells = heights.length; for (let i = 0; i < numCells; i++) { const points = getGridPolygon(i); if (!points) continue; overlay.append("polygon") .attr("points", points) .attr("fill", tectonicHeightColor(heights[i])) .attr("fill-opacity", 0.85) .attr("stroke", tectonicHeightColor(heights[i])) .attr("stroke-opacity", 0.5) .attr("stroke-width", 0.1); } } function drawVelocityArrows(overlay, plates, plateIds, plateColors) { const arrowGroup = overlay.append("g").attr("id", "velocityArrows"); for (const plate of plates) { const centroid = computeGridPlateCentroid(plate.id, plateIds); if (!centroid) continue; const [cx, cy] = centroid; const vel = plate.velocity; const arrowScale = 30; const dx = vel[0] * arrowScale; const dy = -vel[1] * arrowScale; const mag = Math.sqrt(dx * dx + dy * dy); if (mag < 2) continue; arrowGroup.append("line") .attr("x1", cx).attr("y1", cy) .attr("x2", cx + dx).attr("y2", cy + dy) .attr("stroke", plateColors[plate.id]) .attr("stroke-width", 2) .attr("stroke-opacity", 0.9) .attr("marker-end", "url(#tectonicArrowhead)"); arrowGroup.append("text") .attr("x", cx).attr("y", cy - 5) .attr("text-anchor", "middle") .attr("font-size", "8px") .attr("fill", plateColors[plate.id]) .attr("stroke", "#000") .attr("stroke-width", 0.3) .attr("paint-order", "stroke") .text(`P${plate.id}`); } if (!document.getElementById("tectonicArrowhead")) { const defs = d3.select("svg").select("defs"); defs.append("marker") .attr("id", "tectonicArrowhead") .attr("viewBox", "0 0 10 10") .attr("refX", 8).attr("refY", 5) .attr("markerWidth", 6).attr("markerHeight", 6) .attr("orient", "auto-start-reverse") .append("path") .attr("d", "M 0 0 L 10 5 L 0 10 z") .attr("fill", "#fff") .attr("stroke", "#333") .attr("stroke-width", 0.5); } } function computeGridPlateCentroid(plateId, plateIds) { let sumX = 0, sumY = 0, count = 0; for (let i = 0; i < plateIds.length; i++) { if (plateIds[i] !== plateId) continue; const [x, y] = grid.points[i]; sumX += x; sumY += y; count++; } if (count === 0) return null; return [sumX / count, sumY / count]; } function highlightPlate(plateId, plateColors) { viewbox.select("#tectonicOverlay").selectAll("polygon") .attr("fill-opacity", function () { return +this.getAttribute("data-plate") === plateId ? 0.6 : 0.15; }); const row = byId(`tectonicPlate_${plateId}`); if (row) { row.scrollIntoView({behavior: "smooth", block: "nearest"}); row.style.outline = "2px solid " + plateColors[plateId]; setTimeout(() => row.style.outline = "", 1500); } } function buildPlateList(plates, plateColors) { const container = byId("tectonicPlateList"); container.innerHTML = ""; const table = document.createElement("table"); table.style.width = "100%"; table.style.borderCollapse = "collapse"; table.style.fontSize = "11px"; const header = document.createElement("tr"); header.innerHTML = `