mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-22 20:11:24 +01:00
refactoring for row based code
This commit is contained in:
parent
b640f977b9
commit
db49f1c924
7 changed files with 50 additions and 21 deletions
|
|
@ -1505,9 +1505,10 @@
|
||||||
<td>Grid algorithm</td>
|
<td>Grid algorithm</td>
|
||||||
<td>
|
<td>
|
||||||
<select id="gridAlgorithm">
|
<select id="gridAlgorithm">
|
||||||
<option value="voronoiPoints" selected>Voronoi</option>
|
<option value="voronoiPoints" selected>Default</option>
|
||||||
<option value="hexPoints">Hex Grid</option>
|
<option value="hexPointsF">Hex flat</option>
|
||||||
<option value="squarePoints">Square Grid</option>
|
<option value="hexPointsP">Hex pointy</option>
|
||||||
|
<option value="squarePoints">Squares</option>
|
||||||
</select>
|
</select>
|
||||||
</td>
|
</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
|
|
|
||||||
7
main.js
7
main.js
|
|
@ -631,6 +631,7 @@ void (function addDragToUpload() {
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
const gridOptimizationRequired = () => window[document.getElementById('gridAlgorithm').value] == voronoiPoints;
|
||||||
async function generate(options) {
|
async function generate(options) {
|
||||||
try {
|
try {
|
||||||
const timeStart = performance.now();
|
const timeStart = performance.now();
|
||||||
|
|
@ -1166,23 +1167,27 @@ function generatePrecipitation() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// recalculate Voronoi Graph to pack cells
|
// recalculate Voronoi Graph to pack cells
|
||||||
|
// pars: optimize -> optimize cells structure, copy original graph otherwise.
|
||||||
function reGraph() {
|
function reGraph() {
|
||||||
TIME && console.time("reGraph");
|
TIME && console.time("reGraph");
|
||||||
const {cells: gridCells, points, features} = grid;
|
const {cells: gridCells, points, features} = grid;
|
||||||
const newCells = {p: [], g: [], h: []}; // store new data
|
const newCells = {p: [], g: [], h: []}; // store new data
|
||||||
const spacing2 = grid.spacing ** 2;
|
const spacing2 = grid.spacing ** 2;
|
||||||
|
const optimize = gridOptimizationRequired()
|
||||||
|
|
||||||
for (const i of gridCells.i) {
|
for (const i of gridCells.i) {
|
||||||
const height = gridCells.h[i];
|
const height = gridCells.h[i];
|
||||||
const type = gridCells.t[i];
|
const type = gridCells.t[i];
|
||||||
|
if (optimize) {
|
||||||
if (height < 20 && type !== -1 && type !== -2) continue; // exclude all deep ocean points
|
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 (type === -2 && (i % 4 === 0 || features[gridCells.f[i]].type === "lake")) continue; // exclude non-coastal lake points
|
||||||
|
}
|
||||||
const [x, y] = points[i];
|
const [x, y] = points[i];
|
||||||
|
|
||||||
addNewPoint(i, x, y, height);
|
addNewPoint(i, x, y, height);
|
||||||
|
|
||||||
// add additional points for cells along coast
|
// 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
|
if (gridCells.b[i]) continue; // not for near-border cells
|
||||||
gridCells.c[i].forEach(function (e) {
|
gridCells.c[i].forEach(function (e) {
|
||||||
if (i > e) return;
|
if (i > e) return;
|
||||||
|
|
|
||||||
|
|
@ -229,6 +229,7 @@ async function parseLoadedData(data) {
|
||||||
if (settings[22]) stylePreset.value = settings[22];
|
if (settings[22]) stylePreset.value = settings[22];
|
||||||
if (settings[23]) rescaleLabels.checked = +settings[23];
|
if (settings[23]) rescaleLabels.checked = +settings[23];
|
||||||
if (settings[24]) urbanDensity = urbanDensityInput.value = urbanDensityOutput.value = +settings[24];
|
if (settings[24]) urbanDensity = urbanDensityInput.value = urbanDensityOutput.value = +settings[24];
|
||||||
|
if (settings[25]) gridAlgorithm.value = settings[25];
|
||||||
})();
|
})();
|
||||||
|
|
||||||
void (function applyOptionsToUI() {
|
void (function applyOptionsToUI() {
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,8 @@ function getMapData() {
|
||||||
+hideLabels.checked,
|
+hideLabels.checked,
|
||||||
stylePreset.value,
|
stylePreset.value,
|
||||||
+rescaleLabels.checked,
|
+rescaleLabels.checked,
|
||||||
urbanDensity
|
urbanDensity,
|
||||||
|
byId('gridAlgorithm').value,
|
||||||
].join("|");
|
].join("|");
|
||||||
const coords = JSON.stringify(mapCoordinates);
|
const coords = JSON.stringify(mapCoordinates);
|
||||||
const biomes = [biomesData.color, biomesData.habitability, biomesData.name].join("|");
|
const biomes = [biomesData.color, biomesData.habitability, biomesData.name].join("|");
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ window.Submap = (function () {
|
||||||
|
|
||||||
// create new grid
|
// create new grid
|
||||||
applyMapSize();
|
applyMapSize();
|
||||||
grid = generateGrid();
|
grid = generateGrid(options.gridAlgorithm);
|
||||||
|
|
||||||
drawScaleBar(scale);
|
drawScaleBar(scale);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -169,6 +169,7 @@ window.UISubmap = (function () {
|
||||||
WARN && console.warn("Resampling current map");
|
WARN && console.warn("Resampling current map");
|
||||||
const cellNumId = +byId("submapPointsInput").value;
|
const cellNumId = +byId("submapPointsInput").value;
|
||||||
if (!cellsDensityMap[cellNumId]) return console.error("Unknown cell number!");
|
if (!cellsDensityMap[cellNumId]) return console.error("Unknown cell number!");
|
||||||
|
const gridAlgorithm = window[document.getElementById('gridAlgorithm').value];
|
||||||
|
|
||||||
const {angle, shiftX, shiftY, ratio, mirrorH, mirrorV} = getTransformInput();
|
const {angle, shiftX, shiftY, ratio, mirrorH, mirrorV} = getTransformInput();
|
||||||
|
|
||||||
|
|
@ -203,6 +204,7 @@ window.UISubmap = (function () {
|
||||||
smoothHeightMap: false,
|
smoothHeightMap: false,
|
||||||
rescaleStyles: false,
|
rescaleStyles: false,
|
||||||
scale: 1,
|
scale: 1,
|
||||||
|
gridAlgorithm,
|
||||||
projection,
|
projection,
|
||||||
inverse
|
inverse
|
||||||
});
|
});
|
||||||
|
|
@ -229,7 +231,8 @@ window.UISubmap = (function () {
|
||||||
smoothHeightMap: scale > 2,
|
smoothHeightMap: scale > 2,
|
||||||
inverse: (x, y) => [x / origScale + x0, y / origScale + y0],
|
inverse: (x, y) => [x / origScale + x0, y / origScale + y0],
|
||||||
projection: (x, y) => [(x - x0) * origScale, (y - y0) * origScale],
|
projection: (x, y) => [(x - x0) * origScale, (y - y0) * origScale],
|
||||||
scale: origScale
|
scale: origScale,
|
||||||
|
gridAlgorithm: window[byId('gridAlgorithm').value],
|
||||||
};
|
};
|
||||||
|
|
||||||
// converting map position on the planet
|
// converting map position on the planet
|
||||||
|
|
|
||||||
|
|
@ -37,28 +37,34 @@ function voronoiPoints() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// alternatively generate hex-grid
|
// 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");
|
TIME && console.time("hexPoints");
|
||||||
const cellsDesired = +byId("pointsInput").dataset.cells;
|
const cellsDesired = +byId("pointsInput").dataset.cells;
|
||||||
const hexRatio = 0.86602540378;
|
let spacing = rn(Math.sqrt(graphWidth * graphHeight / hexRatio / cellsDesired), 2); // spacing between points
|
||||||
const spacing = rn(Math.sqrt(graphWidth * graphHeight / hexRatio / cellsDesired), 2); // spacing between points
|
let xSpacing, ySpacing;
|
||||||
const lineOffset = spacing * hexRatio;
|
if (pointy) {
|
||||||
|
xSpacing = spacing;
|
||||||
|
ySpacing = spacing * hexRatio;
|
||||||
|
} else {
|
||||||
|
xSpacing = spacing * hexRatio * 2;
|
||||||
|
ySpacing = spacing / 2 ;
|
||||||
|
}
|
||||||
|
|
||||||
const boundary = getBoundaryPoints(graphWidth, graphHeight, spacing);
|
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 = [];
|
let points = [];
|
||||||
|
|
||||||
for (let y = lineOffset / 2, lc = 0 ; y < graphHeight; y += lineOffset, lc++) {
|
let rc, lc, x, y;
|
||||||
for (let x = lc % 2 ? 0 : spacing / 2; x < graphWidth; x += spacing) {
|
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]);
|
points.push([x, y]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TIME && console.timeEnd("hexPoints");
|
TIME && console.timeEnd("hexPoints");
|
||||||
return {spacing, cellsDesired, boundary, points, cellsX, cellsY};
|
return {spacing, cellsDesired, boundary, points, cellsX: rc, cellsY: lc};
|
||||||
}
|
}
|
||||||
|
|
||||||
// square grid
|
// square grid
|
||||||
|
|
@ -143,7 +149,19 @@ function getJitteredGrid(width, height, spacing) {
|
||||||
|
|
||||||
// return cell index on a regular square grid
|
// return cell index on a regular square grid
|
||||||
function findGridCell(x, y, 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));
|
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
|
// return array of cell indexes in radius on a regular square grid
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue