refactor and fix saved grid algo.

This commit is contained in:
GoteGuru 2022-06-20 18:39:50 +02:00
parent 4291777bc4
commit f4b3662cba
7 changed files with 37 additions and 33 deletions

View file

@ -1502,10 +1502,10 @@
<tr data-tip="Select grid generation algorithm">
<td></td>
<td>Grid algorithm</td>
<td>Grid type</td>
<td>
<select id="gridAlgorithm">
<option value="voronoiPoints" selected>Default</option>
<option value="jitteredGridPoints" selected>Default</option>
<option value="hexPointsF">Hex flat</option>
<option value="hexPointsP">Hex pointy</option>
<option value="squarePoints">Squares</option>

View file

@ -631,7 +631,7 @@ void (function addDragToUpload() {
});
})();
const gridOptimizationRequired = () => window[document.getElementById('gridAlgorithm').value] == voronoiPoints;
const gridOptimizationRequired = () => globalThis[byId('gridAlgorithm').value] == jitteredGridPoints;
async function generate(options) {
try {
const timeStart = performance.now();
@ -643,10 +643,10 @@ async function generate(options) {
applyMapSize();
randomizeOptions();
const method = window[document.getElementById('gridAlgorithm').value];
console.log('gird generation method', method);
const method = globalThis[byId('gridAlgorithm').value];
if (shouldRegenerateGrid(grid, method)) grid = precreatedGraph || generateGrid(method);
const cellsDesired = +byId("pointsInput").dataset.cells;
if (shouldRegenerateGrid(grid, method)) grid = precreatedGraph || generateGrid(cellsDesired, method);
else delete grid.cells.h;
grid.cells.h = await HeightmapGenerator.generate(grid);

View file

@ -263,7 +263,9 @@ function getName(id) {
}
function getGraph(currentGraph) {
const newGraph = shouldRegenerateGrid(currentGraph) ? generateGrid() : deepCopy(currentGraph);
const newGraph = shouldRegenerateGrid(currentGraph)
? generateGrid(currentGraph.cellsDesired, currentGraph.generator)
: deepCopy(currentGraph);
delete newGraph.cells.h;
return newGraph;
}

View file

@ -35,7 +35,7 @@ function getMapData() {
stylePreset.value,
+rescaleLabels.checked,
urbanDensity,
byId('gridAlgorithm').value,
grid.generator.name,
].join("|");
const coords = JSON.stringify(mapCoordinates);
const biomes = [biomesData.color, biomesData.habitability, biomesData.name].join("|");

View file

@ -40,7 +40,7 @@ window.Submap = (function () {
// create new grid
applyMapSize();
grid = generateGrid(options.gridAlgorithm);
grid = generateGrid(options.cellsDesired, options.gridAlgorithm);
drawScaleBar(scale);

View file

@ -169,7 +169,6 @@ 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();
@ -204,7 +203,8 @@ window.UISubmap = (function () {
smoothHeightMap: false,
rescaleStyles: false,
scale: 1,
gridAlgorithm,
gridAlgorithm: globalThis[byId('gridAlgorithm').value],
cellsDesired: +byId("pointsInput").dataset.cells,
projection,
inverse
});
@ -232,7 +232,8 @@ window.UISubmap = (function () {
inverse: (x, y) => [x / origScale + x0, y / origScale + y0],
projection: (x, y) => [(x - x0) * origScale, (y - y0) * origScale],
scale: origScale,
gridAlgorithm: window[byId('gridAlgorithm').value],
gridAlgorithm: globalThis[byId('gridAlgorithm').value],
cellsDesired: +byId("pointsInput").dataset.cells,
};
// converting map position on the planet

View file

@ -13,19 +13,23 @@ function shouldRegenerateGrid(grid, method) {
return grid.generator !== method || grid.spacing !== newSpacing || grid.cellsX !== newCellsX || grid.cellsY !== newCellsY;
}
function generateGrid(generator = voronoiPoints) {
function generateGrid(numberOfCells, generator = jitteredGridPoints) {
Math.random = aleaPRNG(seed); // reset PRNG
//const {spacing, cellsDesired, boundary, points, cellsX, cellsY} = placePoints();
const {spacing, cellsDesired, boundary, points, cellsX, cellsY} = generator();
const {spacing, cellsDesired, boundary, points, cellsX, cellsY} = generator(numberOfCells);
const {cells, vertices} = calculateVoronoi(points, boundary);
return {spacing, cellsDesired, boundary, points, cellsX, cellsY, cells, vertices, generator};
}
// place random points to calculate Voronoi diagram
function voronoiPoints() {
TIME && console.time("placePoints");
function squareSpacing() {
const cellsDesired = +byId("pointsInput").dataset.cells;
const spacing = rn(Math.sqrt((graphWidth * graphHeight) / cellsDesired), 2); // spacing between points before jittering
return rn(Math.sqrt((graphWidth * graphHeight) / cellsDesired), 2); // spacing between points before jittering
}
// place random points to calculate Voronoi diagram
function jitteredGridPoints(cellsDesired) {
TIME && console.time("placePoints");
const spacing = squareSpacing();
const boundary = getBoundaryPoints(graphWidth, graphHeight, spacing);
const points = getJitteredGrid(graphWidth, graphHeight, spacing); // points of jittered square grid
@ -37,21 +41,19 @@ function voronoiPoints() {
}
// alternatively generate hex-grid
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
function hexPointsP(cellsDesired) {
const spacing = squareSpacing();
return hexPoints(cellsDesired, spacing / hexRatio , spacing);
}
function hexPointsF(cellsDesired) {
const spacing = squareSpacing();
return hexPoints(cellsDesired, spacing * 2, spacing / hexRatio / 2);
}
function hexPoints(cellsDesired, xSpacing, ySpacing) {
TIME && console.time("hexPoints");
const cellsDesired = +byId("pointsInput").dataset.cells;
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 spacing = (xSpacing + ySpacing) / 2;
const boundary = getBoundaryPoints(graphWidth, graphHeight, spacing);
let points = [];
@ -70,7 +72,6 @@ function hexPoints(pointy) { // pointy must be 0 or 1
// square grid
function squarePoints() {
TIME && console.time("squarePoints");
const cellsDesired = +byId("pointsInput").dataset.cells;
const spacing = rn(Math.sqrt((graphWidth * graphHeight) / cellsDesired), 2);
const boundary = getBoundaryPoints(graphWidth, graphHeight, spacing);
@ -153,7 +154,7 @@ function findGridCell(x, y, grid) {
let ySpacing = grid.spacing * Math.sqrt(3) / 2;
const maxindex = grid.cells.i.length; // safety belt
switch (grid.generator) {
case voronoiPoints:
case jitteredGridPoints:
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: