diff --git a/modules/river-generator.js b/modules/river-generator.js index 2a2e3776..74efb393 100644 --- a/modules/river-generator.js +++ b/modules/river-generator.js @@ -160,31 +160,35 @@ const generate = function(changeHeights = true) { } function defineRivers() { + cells.r = new Uint16Array(cells.i.length); // re-initiate rivers array pack.rivers = []; // rivers data const riverPaths = []; for (let r = 1; r <= riverNext; r++) { const riverSegments = riversData.filter(d => d.river === r); + if (riverSegments.length < 3) continue; - if (riverSegments.length > 2) { - const source = riverSegments[0].cell; - const mouth = riverSegments[riverSegments.length-2].cell; - - const widthFactor = rn(.8 + Math.random() * .4, 1); // river width modifier [.8, 1.2] - const sourceWidth = cells.h[source] >= 20 ? .1 : rn(Math.min(Math.max((cells.fl[source] / 500) ** .4, .5), 1.7), 2); - - const riverMeandered = addMeandering(riverSegments, sourceWidth * 10, .5); - const [path, length, offset] = getPath(riverMeandered, widthFactor, sourceWidth); - riverPaths.push([path, r]); - - const parent = riverSegments[0].parent || 0; - const width = rn(offset ** 2, 2); // mounth width in km - const discharge = last(riverSegments).flux; // in m3/s - pack.rivers.push({i:r, source, mouth, discharge, length, width, widthFactor, sourceWidth, parent}); - } else { - // remove too short rivers - riverSegments.filter(s => cells.r[s.cell] === r).forEach(s => cells.r[s.cell] = 0); + for (const segment of riverSegments) { + const i = segment.cell; + if (cells.r[i]) continue; + if (cells.h[i] < 20) continue; + cells.r[i] = r; } + + const source = riverSegments[0].cell; + const mouth = riverSegments[riverSegments.length-2].cell; + + const widthFactor = rn(.8 + Math.random() * .4, 1); // river width modifier [.8, 1.2] + const sourceWidth = cells.h[source] >= 20 ? .1 : rn(Math.min(Math.max((cells.fl[source] / 500) ** .4, .5), 1.7), 2); + + const riverMeandered = addMeandering(riverSegments, sourceWidth * 10, .5); + const [path, length, offset] = getPath(riverMeandered, widthFactor, sourceWidth); + riverPaths.push([path, r]); + + const parent = riverSegments[0].parent || 0; + const width = rn(offset ** 2, 2); // mounth width in km + const discharge = last(riverSegments).flux; // in m3/s + pack.rivers.push({i:r, source, mouth, discharge, length, width, widthFactor, sourceWidth, parent}); } // draw rivers diff --git a/modules/save-and-load.js b/modules/save-and-load.js index 3a75b9e4..3e145e98 100644 --- a/modules/save-and-load.js +++ b/modules/save-and-load.js @@ -1178,6 +1178,14 @@ function parseLoadedData(data) { ERROR && console.error("Data Integrity Check. Invalid burg", b, "is assigned to cells", invalidCells); }); + const invalidRivers = [...new Set(cells.r)].filter(r => r && !pack.rivers.find(river => river.i === r)); + invalidRivers.forEach(r => { + const invalidCells = cells.i.filter(i => cells.r[i] === r); + invalidCells.forEach(i => cells.r[i] = 0); + rivers.select("river"+r).remove(); + ERROR && console.error("Data Integrity Check. Invalid river", r, "is assigned to cells", invalidCells); + }); + pack.burgs.forEach(b => { if (!b.i || b.removed) return; if (b.port < 0) {ERROR && console.error("Data Integrity Check. Burg", b.i, "has invalid port value", b.port); b.port = 0;} diff --git a/modules/ui/rivers-overview.js b/modules/ui/rivers-overview.js index 170390d3..f96e9857 100644 --- a/modules/ui/rivers-overview.js +++ b/modules/ui/rivers-overview.js @@ -33,7 +33,7 @@ function overviewRivers() { const discharge = r.discharge + " m³/s"; const length = rn(r.length * distanceScaleInput.value) + " " + unit; const width = rn(r.width * distanceScaleInput.value, 3) + " " + unit; - const basin = pack.rivers.find(river => river.i === r.basin).name; + const basin = pack.rivers.find(river => river.i === r.basin)?.name; lines += `