mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-23 04:21:24 +01:00
experimental submapping feature
This commit is contained in:
parent
468d17da4e
commit
36248e36b5
4 changed files with 395 additions and 4 deletions
159
modules/submap.js
Normal file
159
modules/submap.js
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
"use strict";
|
||||
/*
|
||||
Experimental submaping module
|
||||
*/
|
||||
|
||||
window.Submap = (function () {
|
||||
function resample(baseState, projection, monitor=null) {
|
||||
// resample original map instead of regenerating
|
||||
// based on a parent map (baseState)
|
||||
// projection: map function from old to new coordinates: f(x,y) -> [x2,y2]
|
||||
// monitor: progress signaling object. MUST have at least 2 properties:
|
||||
// stage: function (string) dispatched at state change
|
||||
// progress: function (float) dispatched at progress change (@long process)
|
||||
|
||||
const stage = s => monitor && monitor.stage && monitor.stage(s)
|
||||
const progress = p => monitor && monitor.progress && monitor.progress(p)
|
||||
const timeStart = performance.now();
|
||||
invokeActiveZooming();
|
||||
|
||||
// copy seed
|
||||
seed = baseState.seed;
|
||||
Math.random = aleaPRNG(seed);
|
||||
INFO && console.group("SubMap with seed: " + seed);
|
||||
|
||||
// create new grid
|
||||
applyMapSize();
|
||||
placePoints();
|
||||
calculateVoronoi(grid, grid.points);
|
||||
|
||||
drawScaleBar();
|
||||
|
||||
const resampler = (points, qtree, f) => {
|
||||
for(const [i,[x, y]] of points.entries()) {
|
||||
const [tx, ty] = projection(x, y);
|
||||
const oldid = qtree.find(tx,ty,Infinity)[2];
|
||||
f(i, oldid);
|
||||
}
|
||||
}
|
||||
|
||||
stage("Resampling heightmap, temperature and precipitation.")
|
||||
// resample heightmap from old WorldState
|
||||
const n = grid.points.length;
|
||||
grid.cells.h = new Uint8Array(n); // heightmap
|
||||
grid.cells.temp = new Int8Array(n); // temperature
|
||||
grid.cells.prec = new Int8Array(n); // precipitation
|
||||
|
||||
const gridCells = baseState.grid.cells;
|
||||
resampler(grid.points, baseState.pack.cells.q, (id, oldid) => {
|
||||
const cid = baseState.pack.cells.g[oldid]
|
||||
grid.cells.h[id] = gridCells.h[cid];
|
||||
grid.cells.temp[id] = gridCells.temp[cid];
|
||||
grid.cells.prec[id] = gridCells.prec[cid];
|
||||
id%50 || progress(id * 100.0 / grid.points.length)
|
||||
})
|
||||
// TODO: add smooth/noise function for h, temp, prec n times
|
||||
|
||||
stage("Detect features, ocean and generating lakes.")
|
||||
markFeatures();
|
||||
markupGridOcean();
|
||||
addLakesInDeepDepressions();
|
||||
// openNearSeaLakes();
|
||||
OceanLayers();
|
||||
// defineMapSize(); // not needed (not random)
|
||||
// TODO: update UI inputs before calculating according to new boundaries.
|
||||
calculateMapCoordinates();
|
||||
// calculateTemperatures();
|
||||
// generatePrecipitation();
|
||||
stage("Cleaning cell network.")
|
||||
reGraph();
|
||||
drawCoastline();
|
||||
|
||||
// resample packed graph
|
||||
const packCells = baseState.pack.cells;
|
||||
const reverseMap = new Map(); // cellmap from new -> oldcell
|
||||
const forwardMap = baseState.pack.cells.p.map(_=>[]); // old -> [newcelllist]
|
||||
|
||||
const pn = pack.cells.i.length;
|
||||
pack.cells.culture = new Uint16Array(pn);
|
||||
pack.cells.state = new Uint16Array(pn);
|
||||
pack.cells.burg = new Uint16Array(pn);
|
||||
pack.cells.religion = new Uint16Array(pn);
|
||||
pack.cells.road = new Uint16Array(pn);
|
||||
pack.cells.crossroad = new Uint16Array(pn);
|
||||
|
||||
stage("Resampling culture, state and religion map.")
|
||||
|
||||
resampler(pack.cells.p, packCells.q, (id, oldid) => {
|
||||
pack.cells.culture[id] = packCells.culture[oldid];
|
||||
pack.cells.state[id] = packCells.state[oldid];
|
||||
pack.cells.religion[id] = packCells.religion[oldid];
|
||||
reverseMap.set(id, oldid)
|
||||
forwardMap[oldid].push(id)
|
||||
})
|
||||
|
||||
console.log('reversemap:',forwardMap)
|
||||
console.log('forwardmap:',reverseMap)
|
||||
|
||||
Rivers.generate();
|
||||
drawRivers();
|
||||
Lakes.defineGroup();
|
||||
|
||||
// biome calculation based on (resampled) grid.cells.temp and prec
|
||||
// it's safe to recalculate.
|
||||
defineBiomes();
|
||||
// recalculate suitability and population
|
||||
// TODO: normalize according to the base-map
|
||||
rankCells();
|
||||
|
||||
// transfer basemap cultures
|
||||
pack.cultures = baseState.pack.cultures
|
||||
// Cultures.generate();
|
||||
// Cultures.expand();
|
||||
// TODO: update culture centers
|
||||
|
||||
// transfer states and burgs. filter states without land
|
||||
const validStates = new Set(pack.cells.state)
|
||||
console.log(validStates);
|
||||
pack.states = baseState.pack.states;
|
||||
console.log(pack.states)
|
||||
pack.validStates = pack.states.filter(s => validStates.has(s.i))
|
||||
|
||||
// BurgsAndStates.generate();
|
||||
// Religions.generate();
|
||||
// BurgsAndStates.defineStateForms();
|
||||
BurgsAndStates.generateProvinces();
|
||||
// BurgsAndStates.defineBurgFeatures();
|
||||
|
||||
// update burg list
|
||||
//const validBurgs = new Set(pack.cells.burgs);
|
||||
pack.burgs = baseState.pack.burgs
|
||||
// .filter(b => validBurgs.has(b.i))
|
||||
|
||||
// remap burgs to the best new cell
|
||||
pack.burgs.forEach((b, i) => {
|
||||
[b.x, b.y] = projection(b.x, b.y);
|
||||
b.cell = findCell(b.x, b.y);
|
||||
pack.cells.burg[b.cell] = i;
|
||||
// TODO: move port burgs to coast b.x, b.y,
|
||||
});
|
||||
|
||||
drawStates();
|
||||
drawBorders();
|
||||
BurgsAndStates.drawStateLabels();
|
||||
|
||||
Rivers.specify();
|
||||
Lakes.generateName();
|
||||
|
||||
Military.generate();
|
||||
addMarkers();
|
||||
addZones();
|
||||
Names.getMapName();
|
||||
|
||||
WARN && console.warn(`TOTAL: ${rn((performance.now() - timeStart) / 1000, 2)}s`);
|
||||
showStatistics();
|
||||
INFO && console.groupEnd("Generated Map " + seed);
|
||||
}
|
||||
|
||||
return { resample }
|
||||
})();
|
||||
85
modules/ui/submap.js
Normal file
85
modules/ui/submap.js
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
"use strict";
|
||||
|
||||
/*
|
||||
UI elements for submap generation
|
||||
*/
|
||||
|
||||
function openSubmapOptions() {
|
||||
$("#submapOptionsDialog").dialog({
|
||||
title: "Submap options",
|
||||
resizable: false,
|
||||
width: fitContent(),
|
||||
position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}
|
||||
});
|
||||
}
|
||||
|
||||
const generateSubmap = debounce(function (x, y, w, h) {
|
||||
// Create submap from the current map
|
||||
// x,y -> top left corner of desired submap
|
||||
// w,h -> width and height of the submap
|
||||
|
||||
WARN && console.warn("Resampling current map");
|
||||
closeDialogs("#worldConfigurator, #options3d");
|
||||
const stageUI = document.getElementById("submapStage");
|
||||
const progressUI = document.getElementById("submapProgress");
|
||||
const monitor = {
|
||||
stage: s => stageUI.innerHTML = s,
|
||||
progress: p => progressUI.innerHTML = p,
|
||||
}
|
||||
|
||||
const projection = (x, y) => {
|
||||
const [[x0, y0], [x1, y1]] = getViewBoxExtent();
|
||||
return [x * (x1-x0) / graphWidth + x0, y * (y1-y0) / graphHeight + y0]
|
||||
}
|
||||
|
||||
customization = 0;
|
||||
undraw();
|
||||
resetZoom(1000);
|
||||
|
||||
let oldstate = {
|
||||
grid: _.cloneDeep(grid),
|
||||
pack: _.cloneDeep(pack),
|
||||
seed,
|
||||
graphWidth,
|
||||
graphHeight,
|
||||
}
|
||||
|
||||
try {
|
||||
Submap.resample(oldstate, projection, monitor);
|
||||
} catch (error) {
|
||||
generateSubmapErrorHandler(error);
|
||||
}
|
||||
oldstate = null; // destroy old state to free memory
|
||||
|
||||
restoreLayers();
|
||||
if (ThreeD.options.isOn) ThreeD.redraw();
|
||||
if ($("#worldConfigurator").is(":visible")) editWorld();
|
||||
}, 1000);
|
||||
|
||||
function generateSubmapErrorHandler(error) {
|
||||
ERROR && console.error(error);
|
||||
clearMainTip();
|
||||
|
||||
alertMessage.innerHTML = `An error is occured on map resampling. Please retry.
|
||||
<br>If error is critical, clear the stored data and try again.
|
||||
<p id="errorBox">${parseError(error)}</p>`;
|
||||
$("#alert").dialog({
|
||||
resizable: false,
|
||||
title: "Generation error",
|
||||
width: "32em",
|
||||
buttons: {
|
||||
"Clear data": function () {
|
||||
localStorage.clear();
|
||||
localStorage.setItem("version", version);
|
||||
},
|
||||
Regenerate: function () {
|
||||
generateSubmap();
|
||||
$(this).dialog("close");
|
||||
},
|
||||
Ignore: function () {
|
||||
$(this).dialog("close");
|
||||
}
|
||||
},
|
||||
position: {my: "center", at: "center", of: "svg"}
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue