refactoring for row based code

This commit is contained in:
GoteGuru 2022-06-19 00:11:46 +02:00
parent b640f977b9
commit db49f1c924
7 changed files with 50 additions and 21 deletions

View file

@ -1505,9 +1505,10 @@
<td>Grid algorithm</td>
<td>
<select id="gridAlgorithm">
<option value="voronoiPoints" selected>Voronoi</option>
<option value="hexPoints">Hex Grid</option>
<option value="squarePoints">Square Grid</option>
<option value="voronoiPoints" selected>Default</option>
<option value="hexPointsF">Hex flat</option>
<option value="hexPointsP">Hex pointy</option>
<option value="squarePoints">Squares</option>
</select>
</td>
<td></td>

View file

@ -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 (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;

View file

@ -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() {

View file

@ -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("|");

View file

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

View file

@ -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

View file

@ -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) {
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