From 66004de6f40fb2bf9a8bbe5484133e6917f9e6a9 Mon Sep 17 00:00:00 2001 From: Azgaar Date: Mon, 29 Jul 2024 12:59:28 +0200 Subject: [PATCH] feat: Update main.js and modules/io/load.js versions to 1.99.00 --- index.html | 4 +-- main.js | 63 ++++++++++++++++++++++++++----------------- modules/ui/general.js | 2 ++ utils/graphUtils.js | 55 +++++++++++++++++++++++++++++++++++++ versioning.js | 2 +- 5 files changed, 98 insertions(+), 28 deletions(-) diff --git a/index.html b/index.html index 12be4899..d7e9f116 100644 --- a/index.html +++ b/index.html @@ -8134,7 +8134,7 @@ - + @@ -8176,7 +8176,7 @@ - + diff --git a/main.js b/main.js index 3c8f798f..23cb662e 100644 --- a/main.js +++ b/main.js @@ -311,35 +311,48 @@ async function checkLoadParameters() { } function debugGrids() { - debug - .selectAll("circle.grid") - .data(grid.points) - .enter() - .append("circle") - .attr("data-id", (d, i) => "point-" + i) - .attr("cx", d => d[0]) - .attr("cy", d => d[1]) - .attr("r", 0.5) - .attr("fill", "blue"); + // debug + // .selectAll("circle.grid") + // .data(grid.points) + // .enter() + // .append("circle") + // .attr("data-id", (d, i) => "point-" + i) + // .attr("cx", d => d[0]) + // .attr("cy", d => d[1]) + // .attr("r", 0.5) + // .attr("fill", "blue"); let path = ""; grid.cells.i.forEach(i => (path += "M" + getGridPolygon(i))); debug.append("path").attr("fill", "none").attr("stroke", "blue").attr("stroke-width", 0.3).attr("d", path); - debug - .selectAll("circle.boundary") - .data(grid.boundary) - .enter() - .append("circle") - .attr("cx", d => d[0]) - .attr("cy", d => d[1]) - .attr("r", 0.3) - .attr("fill", "white"); + // debug + // .selectAll("circle.boundary") + // .data(grid.boundary) + // .enter() + // .append("circle") + // .attr("cx", d => d[0]) + // .attr("cy", d => d[1]) + // .attr("r", 0.3) + // .attr("fill", "white"); zoom.translateExtent([ [-graphWidth / 2, -graphHeight / 2], [graphWidth * 1.5, graphHeight * 1.5] ]); + + const text = debug + .append("g") + .style("font-size", "0.5px") + .attr("text-anchor", "middle") + .attr("dominant-baseline", "central"); + + for (let x = 0; x < 100; x++) { + for (let y = 0; y < 100; y++) { + const cellId = findGridCell(x, y, grid); + text.append("text").attr("x", x).attr("y", y).text(cellId); + } + } } async function generateMapOnLoad() { @@ -1068,12 +1081,12 @@ function generatePrecipitation() { // define wind directions based on cells latitude and prevailing winds there d3.range(0, cells.i.length, cellsX).forEach(function (cellId, i) { - debug - .append("circle") - .attr("cx", grid.points[cellId][0]) - .attr("cy", grid.points[cellId][1]) - .attr("r", 2) - .attr("fill", "blue"); + // debug + // .append("circle") + // .attr("cx", grid.points[cellId][0]) + // .attr("cy", grid.points[cellId][1]) + // .attr("r", 2) + // .attr("fill", "blue"); const lat = mapCoordinates.latN - (i / cellsY) * mapCoordinates.latT; const latBand = ((Math.abs(lat) - 1) / 5) | 0; diff --git a/modules/ui/general.js b/modules/ui/general.js index 6782db87..03c4fbe2 100644 --- a/modules/ui/general.js +++ b/modules/ui/general.js @@ -120,6 +120,8 @@ function showMapTooltip(point, e, i, g) { const subgroup = path[path.length - 8].id; const land = pack.cells.h[i] >= 20; + // console.log(findGridCell(point[0], point[1], grid)); + // specific elements if (group === "armies") return tip(e.target.parentNode.dataset.name + ". Click to edit"); diff --git a/utils/graphUtils.js b/utils/graphUtils.js index a734cd85..a333600f 100644 --- a/utils/graphUtils.js +++ b/utils/graphUtils.js @@ -45,6 +45,61 @@ function calculateVoronoi(points, boundary) { // return cell index on a regular square grid function findGridCell(x, y, grid) { + if (grid.type === "hexFlat") return findHexCellIndex(x, y, false, grid.spacing, grid.cellsX); + if (grid.type === "hexPointy") return findHexCellIndex(x, y, true, grid.spacing, grid.cellsX); + return findSquareGridCell(x, y, grid); +} + +const hexRatio = Math.sqrt(3) / 2; +function findHexCellIndex(x, y, isPointy, spacing, cellsX) { + const spacingX = isPointy ? spacing / hexRatio : spacing * 2; + const spacingY = isPointy ? spacing : spacing / hexRatio / 2; + + let col = Math.floor(x / spacingX); + let row = Math.floor((y + spacingY * 1.5) / spacingY); + + if (isPointy) { + if (row % 2 === 1 && x < col * spacingX + spacingX / 2) col -= 1; + } else { + if (col % 2 === 1 && y < row * spacingY + spacingY / 2) row -= 1; + } + + const suspect = row * cellsX + col; + const candidates = isPointy + ? [ + suspect, + suspect - cellsX - 1, + suspect - cellsX, + suspect - 1, + suspect + 1, + suspect + cellsX - 1, + suspect + cellsX + ] + : [ + suspect, + suspect - cellsX, + suspect - cellsX * 2, + suspect - cellsX + 1, + suspect + cellsX, + suspect + cellsX + 1, + suspect + cellsX * 2 + ]; + + const closest = candidates.reduce( + (acc, candidate) => { + const point = grid.points[candidate]; + if (!point) return acc; + const dist2 = Math.abs(x - point[0]) + Math.abs(y - point[1]); + if (dist2 < acc.dist2) return {dist2, cell: candidate}; + return acc; + }, + {dist2: Infinity, cell: suspect} + ); + + return closest.cell; +} + +function findSquareGridCell(x, y, grid) { return ( Math.floor(Math.min(y / grid.spacing, grid.cellsY - 1)) * grid.cellsX + Math.floor(Math.min(x / grid.spacing, grid.cellsX - 1)) diff --git a/versioning.js b/versioning.js index d7484173..d23111fa 100644 --- a/versioning.js +++ b/versioning.js @@ -1,7 +1,7 @@ "use strict"; // version and caching control -const version = "1.98.05"; // generator version, update each time +const version = "1.99.00"; // generator version, update each time { document.title += " v" + version;