diff --git a/index.html b/index.html index 143b0cdc..8493b0e8 100644 --- a/index.html +++ b/index.html @@ -1505,9 +1505,10 @@ Grid algorithm diff --git a/main.js b/main.js index 8633cf4d..78c89dd1 100644 --- a/main.js +++ b/main.js @@ -631,6 +631,7 @@ void (function addDragToUpload() { }); })(); +const gridOptimizationRequired = () => window[document.getElementById('gridAlgorithm').value] == voronoiPoints; async function generate(options) { try { const timeStart = performance.now(); @@ -1166,23 +1167,27 @@ function generatePrecipitation() { } // recalculate Voronoi Graph to pack cells +// pars: optimize -> optimize cells structure, copy original graph otherwise. function reGraph() { TIME && console.time("reGraph"); const {cells: gridCells, points, features} = grid; const newCells = {p: [], g: [], h: []}; // store new data const spacing2 = grid.spacing ** 2; + const optimize = gridOptimizationRequired() for (const i of gridCells.i) { const height = gridCells.h[i]; const type = gridCells.t[i]; - if (height < 20 && type !== -1 && type !== -2) continue; // exclude all deep ocean points - if (type === -2 && (i % 4 === 0 || features[gridCells.f[i]].type === "lake")) continue; // exclude non-coastal lake points + if (optimize) { + if (height < 20 && type !== -1 && type !== -2) continue; // exclude all deep ocean points + if (type === -2 && (i % 4 === 0 || features[gridCells.f[i]].type === "lake")) continue; // exclude non-coastal lake points + } const [x, y] = points[i]; addNewPoint(i, x, y, height); // add additional points for cells along coast - if (type === 1 || type === -1) { + if (optimize && (type === 1 || type === -1)) { if (gridCells.b[i]) continue; // not for near-border cells gridCells.c[i].forEach(function (e) { if (i > e) return; diff --git a/modules/io/load.js b/modules/io/load.js index 946df93f..c6dbf26b 100644 --- a/modules/io/load.js +++ b/modules/io/load.js @@ -229,6 +229,7 @@ async function parseLoadedData(data) { if (settings[22]) stylePreset.value = settings[22]; if (settings[23]) rescaleLabels.checked = +settings[23]; if (settings[24]) urbanDensity = urbanDensityInput.value = urbanDensityOutput.value = +settings[24]; + if (settings[25]) gridAlgorithm.value = settings[25]; })(); void (function applyOptionsToUI() { diff --git a/modules/io/save.js b/modules/io/save.js index db303eff..211c7f6d 100644 --- a/modules/io/save.js +++ b/modules/io/save.js @@ -34,7 +34,8 @@ function getMapData() { +hideLabels.checked, stylePreset.value, +rescaleLabels.checked, - urbanDensity + urbanDensity, + byId('gridAlgorithm').value, ].join("|"); const coords = JSON.stringify(mapCoordinates); const biomes = [biomesData.color, biomesData.habitability, biomesData.name].join("|"); diff --git a/modules/submap.js b/modules/submap.js index 85408487..ddd195c6 100644 --- a/modules/submap.js +++ b/modules/submap.js @@ -40,7 +40,7 @@ window.Submap = (function () { // create new grid applyMapSize(); - grid = generateGrid(); + grid = generateGrid(options.gridAlgorithm); drawScaleBar(scale); diff --git a/modules/ui/submap.js b/modules/ui/submap.js index 737560c3..c8781d34 100644 --- a/modules/ui/submap.js +++ b/modules/ui/submap.js @@ -169,6 +169,7 @@ window.UISubmap = (function () { WARN && console.warn("Resampling current map"); const cellNumId = +byId("submapPointsInput").value; if (!cellsDensityMap[cellNumId]) return console.error("Unknown cell number!"); + const gridAlgorithm = window[document.getElementById('gridAlgorithm').value]; const {angle, shiftX, shiftY, ratio, mirrorH, mirrorV} = getTransformInput(); @@ -203,6 +204,7 @@ window.UISubmap = (function () { smoothHeightMap: false, rescaleStyles: false, scale: 1, + gridAlgorithm, projection, inverse }); @@ -229,7 +231,8 @@ window.UISubmap = (function () { smoothHeightMap: scale > 2, inverse: (x, y) => [x / origScale + x0, y / origScale + y0], projection: (x, y) => [(x - x0) * origScale, (y - y0) * origScale], - scale: origScale + scale: origScale, + gridAlgorithm: window[byId('gridAlgorithm').value], }; // converting map position on the planet diff --git a/utils/graphUtils.js b/utils/graphUtils.js index d357ff67..8e033a1e 100644 --- a/utils/graphUtils.js +++ b/utils/graphUtils.js @@ -37,28 +37,34 @@ function voronoiPoints() { } // alternatively generate hex-grid -function hexPoints() { +function hexPointsP() { return hexPoints(true) } +function hexPointsF() { return hexPoints(false) } +const hexRatio = Math.sqrt(3)/2; +function hexPoints(pointy) { // pointy must be 0 or 1 TIME && console.time("hexPoints"); const cellsDesired = +byId("pointsInput").dataset.cells; - const hexRatio = 0.86602540378; - const spacing = rn(Math.sqrt(graphWidth * graphHeight / hexRatio / cellsDesired), 2); // spacing between points - const lineOffset = spacing * hexRatio; + let spacing = rn(Math.sqrt(graphWidth * graphHeight / hexRatio / cellsDesired), 2); // spacing between points + let xSpacing, ySpacing; + if (pointy) { + xSpacing = spacing; + ySpacing = spacing * hexRatio; + } else { + xSpacing = spacing * hexRatio * 2; + ySpacing = spacing / 2 ; + } const boundary = getBoundaryPoints(graphWidth, graphHeight, spacing); - const cellsX = Math.floor((graphWidth + 0.5 * spacing - 1e-10) / spacing); - const cellsY = Math.floor((graphHeight + 0.5 * lineOffset - 1e-10) / lineOffset); - console.log('desired hexgrid:', cellsX, cellsY); - let points = []; - for (let y = lineOffset / 2, lc = 0 ; y < graphHeight; y += lineOffset, lc++) { - for (let x = lc % 2 ? 0 : spacing / 2; x < graphWidth; x += spacing) { + let rc, lc, x, y; + for (y = ySpacing / 2, lc = 0 ; y < graphHeight; y += ySpacing, lc++) { + for (x = lc % 2 ? 0 : xSpacing / 2, rc=0; x < graphWidth; x += xSpacing, rc++) { points.push([x, y]); } } TIME && console.timeEnd("hexPoints"); - return {spacing, cellsDesired, boundary, points, cellsX, cellsY}; + return {spacing, cellsDesired, boundary, points, cellsX: rc, cellsY: lc}; } // square grid @@ -143,7 +149,19 @@ function getJitteredGrid(width, height, spacing) { // return cell index on a regular square grid function findGridCell(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)); + let xSpacing = grid.spacing; + let ySpacing = grid.spacing * Math.sqrt(3) / 2; + const maxindex = grid.cells.i.length; // safety belt + switch (grid.generator) { + case voronoiPoints: + case squarePoints: + return Math.floor(Math.min(y / grid.spacing, grid.cellsY - 1)) * grid.cellsX + Math.floor(Math.min(x / grid.spacing, grid.cellsX - 1)); + case hexPointsF: + xSpacing = grid.spacing * hexRatio; + ySpacing = grid.spacing / 2; + case hexPointsP: + return Math.min(Math.floor(Math.min(y / ySpacing + 1e-10, grid.cellsY - 1)) * grid.cellsX + Math.floor(Math.min(x / xSpacing + 1e-10, grid.cellsX - 1)), maxindex); + } } // return array of cell indexes in radius on a regular square grid