diff --git a/main.js b/main.js index 3ab242d7..3add6200 100644 --- a/main.js +++ b/main.js @@ -530,7 +530,6 @@ function generate() { reGraph(); drawCoastline(); - elevateLakes(); Rivers.generate(); defineBiomes(); @@ -1122,22 +1121,6 @@ function reMarkFeatures() { TIME && console.timeEnd("reMarkFeatures"); } -// temporary elevate some lakes to resolve depressions and flux the water to form an open (exorheic) lake -function elevateLakes() { - if (templateInput.value === "Atoll") return; // no need for Atolls - TIME && console.time('elevateLakes'); - const cells = pack.cells, features = pack.features; - const maxCells = cells.i.length / 100; // size limit; let big lakes be closed (endorheic) - cells.i.forEach(i => { - if (cells.h[i] >= 20) return; - if (features[cells.f[i]].group !== "freshwater" || features[cells.f[i]].cells > maxCells) return; - cells.h[i] = 20; - //debug.append("circle").attr("cx", cells.p[i][0]).attr("cy", cells.p[i][1]).attr("r", .5).attr("fill", "blue"); - }); - - TIME && console.timeEnd('elevateLakes'); -} - // assign biome id for each cell function defineBiomes() { TIME && console.time("defineBiomes"); @@ -1145,7 +1128,6 @@ function defineBiomes() { cells.biome = new Uint8Array(cells.i.length); // biomes array for (const i of cells.i) { - if (f[cells.f[i]].group === "freshwater") cells.h[i] = 19; // de-elevate lakes; here to save some resources const t = temp[cells.g[i]]; // cell temperature const h = cells.h[i]; // cell height const m = h < 20 ? 0 : calculateMoisture(i); // cell moisture diff --git a/modules/river-generator.js b/modules/river-generator.js index 078392e1..54fc1b3a 100644 --- a/modules/river-generator.js +++ b/modules/river-generator.js @@ -25,7 +25,7 @@ .map((h, i) => h < 20 || cells.t[i] < 1 ? h : h + d3.mean(cells.c[i].map(c => cells.t[c])) / 10000); resolveDepressions(h); - features.forEach(f => {delete f.river; delete f.flux;}); + features.forEach(f => {delete f.river; delete f.flux; delete f.inlets}); const riversData = []; // rivers data cells.fl = new Uint16Array(cells.i.length); // water flux array @@ -35,10 +35,95 @@ void function drainWater() { const land = cells.i.filter(i => h[i] >= 20).sort((a,b) => h[b] - h[a]); + const outlets = new Uint32Array(features.length); + // enumerate lake outlet positions + features.filter(f => f.type === "lake" && (f.group === "freshwater" || f.group === "frozen")).forEach(l => { + let outlet = 0; + if (l.shoreline) { + outlet = l.shoreline[d3.scan(l.shoreline, (a,b) => h[a] - h[b])]; + } else { // in case it got missed or deleted + WARN && console.warn('Re-scanning shoreline of a lake'); + const shallows = cells.i.filter(j => cells.t[j] === -1 && cells.f[j] === l.i); + let shoreline = []; + shallows.map(w => cells.c[w]).forEach(cList => cList.forEach(s => shoreline.push(s))); + outlet = shoreline[d3.scan(shoreline, (a,b) => h[a] - h[b])]; + } + outlets[l.i] = outlet; + delete l.shoreline // cleanup temp data once used + }); + + const flowDown = function(min, mFlux, iFlux, ri, i = 0){ + if (cells.r[min]) { // downhill cell already has river assigned + if (mFlux < iFlux) { + cells.conf[min] = cells.fl[min]; // mark confluence + if (h[min] >= 20) riversData.find(r => r.river === cells.r[min]).parent = ri; // min river is a tributary of current river + cells.r[min] = ri; // re-assign river if downhill part has less flux + } else { + cells.conf[min] += iFlux; // mark confluence + if (h[min] >= 20) riversData.find(r => r.river === ri).parent = cells.r[min]; // current river is a tributary of min river + } + } else cells.r[min] = ri; // assign the river to the downhill cell + + if (h[min] < 20) { + // pour water to the sea haven + const oh = i ? cells.haven[i] : min; + riversData.push({river: ri, cell: oh, x: p[min][0], y: p[min][1]}); + const mf = features[cells.f[min]]; // feature of min cell + if (mf.type === "lake") { + if (!mf.river || iFlux > mf.flux) { + mf.river = ri; // pour water to temporaly elevated lake + mf.flux = iFlux; // entering flux + } + mf.totalFlux += iFlux; + if (mf.inlets) { + mf.inlets.push(ri); + } else { + mf.inlets = [ri]; + } + } + } else { + cells.fl[min] += iFlux; // propagate flux + riversData.push({river: ri, cell: min, x: p[min][0], y: p[min][1]}); // add next River segment + } + } + land.forEach(function(i) { cells.fl[i] += grid.cells.prec[cells.g[i]]; // flux from precipitation const x = p[i][0], y = p[i][1]; + // lake outlets draw from lake + let n = -1, out2 = 0; + while (outlets.includes(i, n+1)) { + n = outlets.indexOf(i, n+1); + const l = features[n]; + if ( ! l ) {continue;} + const j = cells.haven[i]; + // allow chain lakes to retain identity + if(cells.r[j] !== l.river) { + let touch = false; + for (const c of cells.c[j]){ + if (cells.r[c] === l.river) { + touch = true; + break; + } + } + if (touch) { + cells.r[j] = l.river; + riversData.push({river: l.river, cell: j, x: p[j][0], y: p[j][1]}); + } else { + cells.r[j] = riverNext; + riversData.push({river: riverNext, cell: j, x: p[j][0], y: p[j][1]}); + riverNext++; + } + } + cells.fl[j] = l.totalFlux; // signpost river size + flowDown(i, cells.fl[i], l.totalFlux, cells.r[j]); + // prevent dropping imediately back into the lake + out2 = cells.c[i].filter(c => (h[c] >= 20 || cells.f[c] !== cells.f[j])).sort((a,b) => h[a] - h[b])[0]; // downhill cell not in the source lake + // assign all to outlet basin + if (l.inlets) l.inlets.forEach(fork => riversData.find(r => r.river === fork).parent = cells.r[j]); + } + // near-border cell: pour out of the screen if (cells.b[i]) { if (cells.r[i]) { @@ -53,14 +138,7 @@ return; } - //const min = cells.c[i][d3.scan(cells.c[i], (a, b) => h[a] - h[b])]; // downhill cell - let min = cells.c[i][d3.scan(cells.c[i], (a, b) => h[a] - h[b])]; // downhill cell - - // allow only one river can flow through a lake - const cf = features[cells.f[i]]; // current cell feature - if (cf.river && cf.river !== cells.r[i]) { - cells.fl[i] = 0; - } + const min = out2 ? out2 : cells.c[i][d3.scan(cells.c[i], (a, b) => h[a] - h[b])]; // downhill cell if (cells.fl[i] < 30) { if (h[min] >= 20) cells.fl[min] += cells.fl[i]; @@ -74,32 +152,7 @@ riverNext++; } - if (cells.r[min]) { // downhill cell already has river assigned - if (cells.fl[min] < cells.fl[i]) { - cells.conf[min] = cells.fl[min]; // mark confluence - if (h[min] >= 20) riversData.find(r => r.river === cells.r[min]).parent = cells.r[i]; // min river is a tributary of current river - cells.r[min] = cells.r[i]; // re-assign river if downhill part has less flux - } else { - cells.conf[min] += cells.fl[i]; // mark confluence - if (h[min] >= 20) riversData.find(r => r.river === cells.r[i]).parent = cells.r[min]; // current river is a tributary of min river - } - } else cells.r[min] = cells.r[i]; // assign the river to the downhill cell - - const nx = p[min][0], ny = p[min][1]; - if (h[min] < 20) { - // pour water to the sea haven - riversData.push({river: cells.r[i], cell: cells.haven[i], x: nx, y: ny}); - } else { - const mf = features[cells.f[min]]; // feature of min cell - if (mf.type === "lake") { - if (!mf.river || cells.fl[i] > mf.flux) { - mf.river = cells.r[i]; // pour water to temporaly elevated lake - mf.flux = cells.fl[i]; // entering flux - } - } - cells.fl[min] += cells.fl[i]; // propagate flux - riversData.push({river: cells.r[i], cell: min, x: nx, y: ny}); // add next River segment - } + flowDown(min, cells.fl[min], cells.fl[i], cells.r[i], i); }); }() @@ -112,12 +165,12 @@ const riverSegments = riversData.filter(d => d.river === r); if (riverSegments.length > 2) { - const riverEnhanced = addMeandring(riverSegments); - const width = rn(.8 + Math.random() * .4, 1); // river width modifier - const increment = rn(.8 + Math.random() * .6, 1); // river bed widening modifier - const [path, length] = getPath(riverEnhanced, width, increment); - riverPaths.push([r, path, width, increment]); const source = riverSegments[0], mouth = riverSegments[riverSegments.length-2]; + const riverEnhanced = addMeandring(riverSegments); + let width = rn(.8 + Math.random() * .4, 1); // river width modifier [.2, 10] + let increment = rn(.8 + Math.random() * .6, 1); // river bed widening modifier [.01, 3] + const [path, length] = getPath(riverEnhanced, width, increment, cells.h[source.cell] >= 20 ? .1 : .6); + riverPaths.push([r, path, width, increment]); const parent = source.parent || 0; pack.rivers.push({i:r, parent, length, source:source.cell, mouth:mouth.cell}); } else { @@ -143,13 +196,37 @@ const resolveDepressions = function(h) { const cells = pack.cells; const land = cells.i.filter(i => h[i] >= 20 && h[i] < 100 && !cells.b[i]); // exclude near-border cells + const lakes = pack.features.filter(f => f.type === "lake" && (f.group === "freshwater" || f.group === "frozen")); // to keep lakes flat + lakes.forEach(l => { + l.shoreline = []; + l.height = 21; + l.totalFlux = grid.cells.prec[cells.g[l.firstCell]]; + }); + for (let i of land.filter(i => cells.t[i] === 1)) { // select shoreline cells + cells.c[i].map(c => pack.features[cells.f[c]]).forEach(cf => { + if (lakes.includes(cf) && !cf.shoreline.includes(i)) { + cf.shoreline.push(i); + } + }) + } land.sort((a,b) => h[b] - h[a]); // highest cells go first let depressed = false; for (let l = 0, depression = Infinity; depression && l < 100; l++) { depression = 0; + for (const l of lakes) { + const minHeight = d3.min(l.shoreline.map(s => h[s])); + if (minHeight === 100) continue; // already max height + if (l.height <= minHeight) { + l.height = Math.min(minHeight + 1, 100); + depression++; + depressed = true; + } + } for (const i of land) { - const minHeight = d3.min(cells.c[i].map(c => h[c])); + const minHeight = d3.min(cells.c[i].map(c => cells.t[c] > 0 ? h[c] : + pack.features[cells.f[c]].height || h[c] // NB undefined is falsy (a || b is short for a ? a : b) + )); if (minHeight === 100) continue; // already max height if (h[i] <= minHeight) { h[i] = Math.min(minHeight + 1, 100); @@ -200,8 +277,8 @@ return riverEnhanced; } - const getPath = function(points, width = 1, increment = 1) { - let offset, extraOffset = .1; // starting river width (to make river source visible) + const getPath = function(points, width = 1, increment = 1, starting = .1) { + let offset, extraOffset = starting; // starting river width (to make river source visible) const riverLength = points.reduce((s, v, i, p) => s + (i ? Math.hypot(v[0] - p[i-1][0], v[1] - p[i-1][1]) : 0), 0); // summ of segments length const widening = rn((1000 + (riverLength * 30)) * increment); const riverPointsLeft = [], riverPointsRight = []; // store points on both sides to build a valid polygon diff --git a/modules/ui/heightmap-editor.js b/modules/ui/heightmap-editor.js index 66fba656..b2f7712a 100644 --- a/modules/ui/heightmap-editor.js +++ b/modules/ui/heightmap-editor.js @@ -176,7 +176,6 @@ function editHeightmap() { reGraph(); drawCoastline(); - elevateLakes(); Rivers.generate(change); if (!change) { @@ -289,7 +288,6 @@ function editHeightmap() { drawCoastline(); if (changeHeights.checked) { - elevateLakes(); Rivers.generate(changeHeights.checked); } @@ -314,7 +312,6 @@ function editHeightmap() { for (const i of pack.cells.i) { const g = pack.cells.g[i]; - if (pack.features[pack.cells.f[i]].group === "freshwater") pack.cells.h[i] = 19; // de-elevate lakes const land = pack.cells.h[i] >= 20; // check biome diff --git a/modules/ui/tools.js b/modules/ui/tools.js index 066eda0b..15988bf8 100644 --- a/modules/ui/tools.js +++ b/modules/ui/tools.js @@ -72,12 +72,7 @@ function processFeatureRegeneration(event, button) { } function regenerateRivers() { - elevateLakes(); Rivers.generate(); - for (const i of pack.cells.i) { - const f = pack.features[pack.cells.f[i]]; // feature - if (f.group === "freshwater") pack.cells.h[i] = 19; // de-elevate lakes - } Rivers.specify(); if (!layerIsOn("toggleRivers")) toggleRivers(); } diff --git a/modules/ui/world-configurator.js b/modules/ui/world-configurator.js index ffcef39f..fc3f93b6 100644 --- a/modules/ui/world-configurator.js +++ b/modules/ui/world-configurator.js @@ -45,7 +45,6 @@ function editWorld() { updateGlobePosition(); calculateTemperatures(); generatePrecipitation(); - elevateLakes(); const heights = new Uint8Array(pack.cells.h); Rivers.generate(); Rivers.specify();