refactor: generation script

This commit is contained in:
Azgaar 2022-07-12 20:09:08 +03:00
parent 00d8d28d76
commit c0f6ce00ef
11 changed files with 112 additions and 97 deletions

View file

@ -5,14 +5,10 @@ import {closeDialogs} from "dialogs/utils";
import {initLayers, renderLayer, restoreLayers} from "layers";
// @ts-expect-error js module
import {drawCoastline} from "modules/coastline";
import {calculateMapCoordinates, defineMapSize} from "modules/coordinates";
import {markupGridFeatures, reMarkFeatures} from "modules/markup";
import {reMarkFeatures} from "modules/markup";
// @ts-expect-error js module
import {drawScaleBar, Rulers} from "modules/measurers";
// @ts-expect-error js module
import {generatePrecipitation} from "modules/precipitation";
import {calculateTemperatures} from "modules/temperature";
// @ts-expect-error js module
import {unfog} from "modules/ui/editors";
// @ts-expect-error js module
import {applyMapSize, randomizeOptions} from "modules/ui/options";
@ -26,37 +22,34 @@ import {hideLoading, showLoading} from "scripts/loading";
import {clearMainTip, tip} from "scripts/tooltips";
import {parseError} from "utils/errorUtils";
import {debounce} from "utils/functionUtils";
import {generateGrid, shouldRegenerateGridPoints} from "utils/graphUtils";
import {rn} from "utils/numberUtils";
import {generateSeed} from "utils/probabilityUtils";
import {byId} from "utils/shorthands";
import {rankCells} from "./rankCells";
import {rankCells} from "../rankCells";
import {showStatistics} from "../statistics";
import {createGrid} from "./grid";
import {reGraph} from "./reGraph";
import {showStatistics} from "./statistics";
const {Zoom, Lakes, HeightmapGenerator, OceanLayers} = window;
const {Zoom, Lakes, OceanLayers, Rivers, Biomes, Cultures, BurgsAndStates, Religions, Military, Markers, Names} =
window;
interface IGenerationOptions {
seed: string;
graph: IGrid;
}
export async function generate(options?: IGenerationOptions) {
async function generate(options?: {seed: string; graph: IGrid}) {
try {
const timeStart = performance.now();
const {seed: precreatedSeed, graph: precreatedGraph} = options || {};
Zoom?.invoke();
setSeed(precreatedSeed);
INFO && console.group("Generated Map " + seed);
applyMapSize();
randomizeOptions();
const updatedGrid = await updateGrid(grid, precreatedGraph);
const newGrid = await createGrid(grid, precreatedGraph);
const pack = reGraph(updatedGrid);
reMarkFeatures(pack, grid);
const pack = reGraph(newGrid);
reMarkFeatures(pack, newGrid);
drawCoastline();
Rivers.generate();
@ -84,73 +77,45 @@ export async function generate(options?: IGenerationOptions) {
Markers.generate();
addZones();
OceanLayers(updatedGrid);
OceanLayers(newGrid);
drawScaleBar(scale);
drawScaleBar(window.scale);
Names.getMapName();
WARN && console.warn(`TOTAL: ${rn((performance.now() - timeStart) / 1000, 2)}s`);
showStatistics();
INFO && console.groupEnd("Generated Map " + seed);
INFO && console.groupEnd();
} catch (error) {
ERROR && console.error(error);
const parsedError = parseError(error);
clearMainTip();
alertMessage.innerHTML = /* html */ `An error has occurred on map generation. Please retry. <br />If error is critical, clear the stored data and try again.
<p id="errorBox">${parsedError}</p>`;
$("#alert").dialog({
resizable: false,
title: "Generation error",
width: "32em",
buttons: {
"Clear data": function () {
localStorage.clear();
localStorage.setItem("version", version);
},
Regenerate: function () {
regenerateMap("generation error");
$(this).dialog("close");
},
Ignore: function () {
$(this).dialog("close");
}
},
position: {my: "center", at: "center", of: "svg"}
});
showGenerationError(error as Error);
}
}
async function updateGrid(globalGrid: IGrid, precreatedGraph?: IGrid): Promise<IGrid> {
const baseGrid: IGridBase = shouldRegenerateGridPoints(globalGrid)
? (precreatedGraph && undressGrid(precreatedGraph)) || generateGrid()
: undressGrid(globalGrid);
function showGenerationError(error: Error) {
clearMainTip();
ERROR && console.error(error);
const message = `An error has occurred on map generation. Please retry. <br />If error is critical, clear the stored data and try again.
<p id="errorBox">${parseError(error)}</p>`;
byId("alertMessage")!.innerHTML = message;
const heights: Uint8Array = await HeightmapGenerator.generate(baseGrid);
if (!heights) throw new Error("Heightmap generation failed");
const heightsGrid = {...baseGrid, cells: {...baseGrid.cells, h: heights}};
const {featureIds, distanceField, features} = markupGridFeatures(heightsGrid);
const markedGrid = {...heightsGrid, features, cells: {...heightsGrid.cells, f: featureIds, t: distanceField}};
const touchesEdges = features.some(feature => feature && feature.land && feature.border);
defineMapSize(touchesEdges);
window.mapCoordinates = calculateMapCoordinates();
Lakes.addLakesInDeepDepressions(markedGrid);
Lakes.openNearSeaLakes(markedGrid);
const temperature = calculateTemperatures(markedGrid);
const temperatureGrid = {...markedGrid, cells: {...markedGrid.cells, temp: temperature}};
const prec = generatePrecipitation(temperatureGrid);
return {...temperatureGrid, cells: {...temperatureGrid.cells, prec}};
}
function undressGrid(extendedGrid: IGrid): IGridBase {
const {spacing, cellsDesired, boundary, points, cellsX, cellsY, cells, vertices} = extendedGrid;
const {i, b, c, v} = cells;
return {spacing, cellsDesired, boundary, points, cellsX, cellsY, cells: {i, b, c, v}, vertices};
$("#alert").dialog({
resizable: false,
title: "Generation error",
width: "32em",
buttons: {
"Clear data": function () {
localStorage.clear();
localStorage.setItem("version", APP_VERSION);
},
Regenerate: function () {
regenerateMap("generation error");
$(this).dialog("close");
},
Ignore: function () {
$(this).dialog("close");
}
},
position: {my: "center", at: "center", of: "svg"}
});
}
export async function generateMapOnLoad() {

View file

@ -0,0 +1,54 @@
import {calculateTemperatures} from "modules/temperature";
import {generateGrid} from "utils/graphUtils";
import {calculateMapCoordinates, defineMapSize} from "modules/coordinates";
import {markupGridFeatures} from "modules/markup";
// @ts-expect-error js module
import {generatePrecipitation} from "modules/precipitation";
import {byId} from "utils/shorthands";
import {rn} from "utils/numberUtils";
const {Lakes, HeightmapGenerator} = window;
export async function createGrid(globalGrid: IGrid, precreatedGraph?: IGrid): Promise<IGrid> {
const baseGrid: IGridBase = shouldRegenerateGridPoints(globalGrid)
? (precreatedGraph && undressGrid(precreatedGraph)) || generateGrid()
: undressGrid(globalGrid);
const heights: Uint8Array = await HeightmapGenerator.generate(baseGrid);
if (!heights) throw new Error("Heightmap generation failed");
const heightsGrid = {...baseGrid, cells: {...baseGrid.cells, h: heights}};
const {featureIds, distanceField, features} = markupGridFeatures(heightsGrid);
const markedGrid = {...heightsGrid, features, cells: {...heightsGrid.cells, f: featureIds, t: distanceField}};
const touchesEdges = features.some(feature => feature && feature.land && feature.border);
defineMapSize(touchesEdges);
window.mapCoordinates = calculateMapCoordinates();
Lakes.addLakesInDeepDepressions(markedGrid);
Lakes.openNearSeaLakes(markedGrid);
const temperature = calculateTemperatures(markedGrid);
const temperatureGrid = {...markedGrid, cells: {...markedGrid.cells, temp: temperature}};
const prec = generatePrecipitation(temperatureGrid);
return {...temperatureGrid, cells: {...temperatureGrid.cells, prec}};
}
function undressGrid(extendedGrid: IGrid): IGridBase {
const {spacing, cellsDesired, boundary, points, cellsX, cellsY, cells, vertices} = extendedGrid;
const {i, b, c, v} = cells;
return {spacing, cellsDesired, boundary, points, cellsX, cellsY, cells: {i, b, c, v}, vertices};
}
// check if new grid graph should be generated or we can use the existing one
export function shouldRegenerateGridPoints(grid: IGrid) {
const cellsDesired = Number(byId("pointsInput")?.dataset.cells);
if (cellsDesired !== grid.cellsDesired) return true;
const newSpacing = rn(Math.sqrt((graphWidth * graphHeight) / cellsDesired), 2);
const newCellsX = Math.floor((graphWidth + 0.5 * newSpacing - 1e-10) / newSpacing);
const newCellsY = Math.floor((graphHeight + 0.5 * newSpacing - 1e-10) / newSpacing);
return grid.spacing !== newSpacing || grid.cellsX !== newCellsX || grid.cellsY !== newCellsY;
}

View file

@ -1,11 +1,13 @@
import * as d3 from "d3";
import {ERROR, WARN} from "config/logging";
// @ts-expect-error js module
import {loadMapFromURL} from "modules/io/load";
import {setDefaultEventHandlers} from "scripts/events";
import {ldb} from "scripts/indexedDB";
import {getInputValue} from "utils/nodeUtils";
import {generateMapOnLoad} from "./generation.ts";
import {generateMapOnLoad} from "./generation/generation";
// @ts-expect-error js module
import {showUploadErrorMessage, uploadMap} from "modules/io/load";
export function addOnLoadListener() {