mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 09:41:24 +01:00
v1.5.77 - river data change
This commit is contained in:
parent
07f0eff52c
commit
12fd553b0d
4 changed files with 64 additions and 66 deletions
6
main.js
6
main.js
|
|
@ -707,7 +707,6 @@ function openNearSeaLakes() {
|
||||||
if (cells.h[c] >= 20) cells.t[c] = 1; // mark as coastline
|
if (cells.h[c] >= 20) cells.t[c] = 1; // mark as coastline
|
||||||
});
|
});
|
||||||
features[lake].type = "ocean"; // mark former lake as ocean
|
features[lake].type = "ocean"; // mark former lake as ocean
|
||||||
debug.append("circle").attr("cx", grid.points[treshold][0]).attr("cy", grid.points[treshold][1]).attr("r", 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TIME && console.timeEnd("openLakes");
|
TIME && console.timeEnd("openLakes");
|
||||||
|
|
@ -1132,10 +1131,7 @@ function defineLakesGroup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function defineGroup(feature) {
|
function defineGroup(feature) {
|
||||||
const gridCell = pack.cells.g[feature.firstCell];
|
if (feature.temp < -3) return "frozen";
|
||||||
const temp = grid.cells.temp[gridCell];
|
|
||||||
|
|
||||||
if (temp < -3) return "frozen";
|
|
||||||
if (feature.height > 60 && feature.cells < 10 && feature.firstCell % 5 === 0) return "lava";
|
if (feature.height > 60 && feature.cells < 10 && feature.firstCell % 5 === 0) return "lava";
|
||||||
|
|
||||||
if (!feature.inlets && !feature.outlet) {
|
if (!feature.inlets && !feature.outlet) {
|
||||||
|
|
|
||||||
|
|
@ -61,13 +61,11 @@
|
||||||
relief.sort((a, b) => (a.y + a.s) - (b.y + b.s));
|
relief.sort((a, b) => (a.y + a.s) - (b.y + b.s));
|
||||||
|
|
||||||
// append relief icons at once using pure js
|
// append relief icons at once using pure js
|
||||||
void function renderRelief() {
|
|
||||||
let reliefHTML = "";
|
let reliefHTML = "";
|
||||||
for (const r of relief) {
|
for (const r of relief) {
|
||||||
reliefHTML += `<use href="${r.i}" x="${r.x}" y="${r.y}" width="${r.s}" height="${r.s}"/>`;
|
reliefHTML += `<use href="${r.i}" x="${r.x}" y="${r.y}" width="${r.s}" height="${r.s}"/>`;
|
||||||
}
|
}
|
||||||
terrain.html(reliefHTML);
|
terrain.html(reliefHTML);
|
||||||
}()
|
|
||||||
|
|
||||||
TIME && console.timeEnd('drawRelief');
|
TIME && console.timeEnd('drawRelief');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -86,21 +86,22 @@ const generate = function(changeHeights = true) {
|
||||||
for (const lake of lakes) {
|
for (const lake of lakes) {
|
||||||
const lakeCell = cells.c[i].find(c => h[c] < 20 && cells.f[c] === lake.i);
|
const lakeCell = cells.c[i].find(c => h[c] < 20 && cells.f[c] === lake.i);
|
||||||
|
|
||||||
|
cells.fl[lakeCell] += Math.max(lake.flux - lake.evaporation, 0); // not evaporated lake water drains to outlet
|
||||||
|
|
||||||
// allow chain lakes to retain identity
|
// allow chain lakes to retain identity
|
||||||
if (cells.r[lakeCell] !== lake.river) {
|
if (cells.r[lakeCell] !== lake.river) {
|
||||||
const sameRiver = cells.c[lakeCell].some(c => cells.r[c] === lake.river);
|
const sameRiver = cells.c[lakeCell].some(c => cells.r[c] === lake.river);
|
||||||
if (sameRiver) {
|
if (sameRiver) {
|
||||||
cells.r[lakeCell] = lake.river;
|
cells.r[lakeCell] = lake.river;
|
||||||
riversData.push({river: lake.river, cell: lakeCell, x: p[lakeCell][0], y: p[lakeCell][1]});
|
riversData.push({river: lake.river, cell: lakeCell, x: p[lakeCell][0], y: p[lakeCell][1], flux: cells.fl[lakeCell]});
|
||||||
} else {
|
} else {
|
||||||
cells.r[lakeCell] = riverNext;
|
cells.r[lakeCell] = riverNext;
|
||||||
riversData.push({river: riverNext, cell: lakeCell, x: p[lakeCell][0], y: p[lakeCell][1]});
|
riversData.push({river: riverNext, cell: lakeCell, x: p[lakeCell][0], y: p[lakeCell][1], flux: cells.fl[lakeCell]});
|
||||||
riverNext++;
|
riverNext++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lake.outlet = cells.r[lakeCell];
|
lake.outlet = cells.r[lakeCell];
|
||||||
cells.fl[lakeCell] += Math.max(lake.flux - lake.evaporation, 0); // not evaporated lake water drains to outlet
|
|
||||||
flowDown(i, cells.fl[i], cells.fl[lakeCell], lake.outlet);
|
flowDown(i, cells.fl[i], cells.fl[lakeCell], lake.outlet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -117,7 +118,7 @@ const generate = function(changeHeights = true) {
|
||||||
if (min === graphHeight - y) {to[0] = x; to[1] = graphHeight;} else
|
if (min === graphHeight - y) {to[0] = x; to[1] = graphHeight;} else
|
||||||
if (min === x) {to[0] = 0; to[1] = y;} else
|
if (min === x) {to[0] = 0; to[1] = y;} else
|
||||||
if (min === graphWidth - x) {to[0] = graphWidth; to[1] = y;}
|
if (min === graphWidth - x) {to[0] = graphWidth; to[1] = y;}
|
||||||
riversData.push({river: cells.r[i], cell: i, x: to[0], y: to[1]});
|
riversData.push({river: cells.r[i], cell: i, x: to[0], y: to[1], flux: cells.fl[i]});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,7 +135,7 @@ const generate = function(changeHeights = true) {
|
||||||
// proclaim a new river
|
// proclaim a new river
|
||||||
if (!cells.r[i]) {
|
if (!cells.r[i]) {
|
||||||
cells.r[i] = riverNext;
|
cells.r[i] = riverNext;
|
||||||
riversData.push({river: riverNext, cell: i, x, y});
|
riversData.push({river: riverNext, cell: i, x, y, flux: cells.fl[i]});
|
||||||
riverNext++;
|
riverNext++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -158,7 +159,7 @@ const generate = function(changeHeights = true) {
|
||||||
if (h[toCell] < 20) {
|
if (h[toCell] < 20) {
|
||||||
// pour water to the water body
|
// pour water to the water body
|
||||||
const haven = fromCell ? cells.haven[fromCell] : toCell;
|
const haven = fromCell ? cells.haven[fromCell] : toCell;
|
||||||
riversData.push({river, cell: haven, x: p[toCell][0], y: p[toCell][1]});
|
riversData.push({river, cell: haven, x: p[toCell][0], y: p[toCell][1], flux: fromFlux});
|
||||||
|
|
||||||
const waterBody = features[cells.f[toCell]];
|
const waterBody = features[cells.f[toCell]];
|
||||||
if (waterBody.type === "lake") {
|
if (waterBody.type === "lake") {
|
||||||
|
|
@ -172,37 +173,40 @@ const generate = function(changeHeights = true) {
|
||||||
} else {
|
} else {
|
||||||
// propagate flux and add next river segment
|
// propagate flux and add next river segment
|
||||||
cells.fl[toCell] += fromFlux;
|
cells.fl[toCell] += fromFlux;
|
||||||
riversData.push({river, cell: toCell, x: p[toCell][0], y: p[toCell][1]});
|
riversData.push({river, cell: toCell, x: p[toCell][0], y: p[toCell][1], flux: cells.fl[toCell]});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function defineRivers() {
|
function defineRivers() {
|
||||||
pack.rivers = []; // rivers data
|
pack.rivers = []; // rivers data
|
||||||
const riverPaths = []; // temporary data for all rivers
|
const riverPaths = [];
|
||||||
|
|
||||||
for (let r = 1; r <= riverNext; r++) {
|
for (let r = 1; r <= riverNext; r++) {
|
||||||
const riverSegments = riversData.filter(d => d.river === r);
|
const riverSegments = riversData.filter(d => d.river === r);
|
||||||
|
|
||||||
if (riverSegments.length > 2) {
|
if (riverSegments.length > 2) {
|
||||||
const source = riverSegments[0], mouth = riverSegments[riverSegments.length-2];
|
const source = riverSegments[0].cell;
|
||||||
const riverEnhanced = addMeandring(riverSegments);
|
const mouth = riverSegments[riverSegments.length-2].cell;
|
||||||
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 widthFactor = rn(.8 + Math.random() * .4, 1); // river width modifier [.8, 1.2]
|
||||||
const [path, length] = getPath(riverEnhanced, width, increment, cells.h[source.cell] >= 20 ? .1 : .6);
|
const sourceWidth = cells.h[source] >= 20 ? .1 : rn(Math.min(Math.max((cells.fl[source] / 500) ** .4, .5), 1.7), 2);
|
||||||
riverPaths.push([r, path, width, increment]);
|
|
||||||
const parent = source.parent || 0;
|
const riverMeandered = addMeandering(riverSegments, sourceWidth * 10, .5);
|
||||||
pack.rivers.push({i:r, parent, length, source:source.cell, mouth:mouth.cell});
|
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 {
|
} else {
|
||||||
// remove too short rivers
|
// remove too short rivers
|
||||||
riverSegments.filter(s => cells.r[s.cell] === r).forEach(s => cells.r[s.cell] = 0);
|
riverSegments.filter(s => cells.r[s.cell] === r).forEach(s => cells.r[s.cell] = 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// drawRivers
|
// draw rivers
|
||||||
rivers.selectAll("path").remove();
|
rivers.html(riverPaths.map(d => `<path id="river${d[1]}" d="${d[0]}"/>`).join(""));
|
||||||
rivers.selectAll("path").data(riverPaths).enter()
|
|
||||||
.append("path").attr("d", d => d[1]).attr("id", d => "river"+d[0])
|
|
||||||
.attr("data-width", d => d[2]).attr("data-increment", d => d[3]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanupLakeData() {
|
function cleanupLakeData() {
|
||||||
|
|
@ -227,6 +231,7 @@ const generate = function(changeHeights = true) {
|
||||||
// depression filling algorithm (for a correct water flux modeling)
|
// depression filling algorithm (for a correct water flux modeling)
|
||||||
const resolveDepressions = function(h) {
|
const resolveDepressions = function(h) {
|
||||||
const {cells, features} = pack;
|
const {cells, features} = pack;
|
||||||
|
const ITERATIONS = 150;
|
||||||
|
|
||||||
const lakes = features.filter(f => f.type === "lake");
|
const lakes = features.filter(f => f.type === "lake");
|
||||||
lakes.forEach(l => {
|
lakes.forEach(l => {
|
||||||
|
|
@ -236,11 +241,11 @@ const resolveDepressions = function(h) {
|
||||||
l.height = 21;
|
l.height = 21;
|
||||||
});
|
});
|
||||||
|
|
||||||
const land = cells.i.filter(i => h[i] >= 20 && h[i] < 100 && !cells.b[i]); // exclude near-border cells
|
const land = cells.i.filter(i => h[i] >= 20 && !cells.b[i]); // exclude near-border cells
|
||||||
land.sort((a,b) => h[b] - h[a]); // highest cells go first
|
land.sort((a,b) => h[b] - h[a]); // highest cells go first
|
||||||
|
|
||||||
let depressions = Infinity;
|
let depressions = Infinity;
|
||||||
for (let l = 0; depressions && l < 100; l++) {
|
for (let l = 0; depressions && l < ITERATIONS; l++) {
|
||||||
depressions = 0;
|
depressions = 0;
|
||||||
|
|
||||||
for (const l of lakes) {
|
for (const l of lakes) {
|
||||||
|
|
@ -262,47 +267,46 @@ const resolveDepressions = function(h) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// add more river points on 1/3 and 2/3 of length
|
// add more river points on 1/3 and 2/3 of length
|
||||||
const addMeandring = function(segments, rndFactor = 0.3) {
|
const addMeandering = function(segments, width = 1, meandering = .5) {
|
||||||
const riverEnhanced = []; // to store enhanced segments
|
const riverMeandered = []; // to store enhanced segments
|
||||||
let side = 1; // to control meandring direction
|
|
||||||
|
|
||||||
for (let s = 0; s < segments.length; s++) {
|
for (let s = 0; s < segments.length; s++, width++) {
|
||||||
const sX = segments[s].x, sY = segments[s].y; // segment start coordinates
|
const sX = segments[s].x, sY = segments[s].y; // segment start coordinates
|
||||||
const c = pack.cells.conf[segments[s].cell] || 0; // if segment is river confluence
|
const c = pack.cells.conf[segments[s].cell] || 0; // if segment is river confluence
|
||||||
riverEnhanced.push([sX, sY, c]);
|
riverMeandered.push([sX, sY, c]);
|
||||||
|
|
||||||
if (s+1 === segments.length) break; // do not enhance last segment
|
if (s+1 === segments.length) break; // do not meander last segment
|
||||||
|
|
||||||
const eX = segments[s+1].x, eY = segments[s+1].y; // segment end coordinates
|
const eX = segments[s+1].x, eY = segments[s+1].y; // segment end coordinates
|
||||||
const angle = Math.atan2(eY - sY, eX - sX);
|
const angle = Math.atan2(eY - sY, eX - sX);
|
||||||
const sin = Math.sin(angle), cos = Math.cos(angle);
|
const sin = Math.sin(angle), cos = Math.cos(angle);
|
||||||
const serpentine = 1 / (s + 1) + 0.3;
|
|
||||||
const meandr = serpentine + Math.random() * rndFactor;
|
const meander = meandering + 1 / width + Math.random() * Math.max(meandering - width / 100, 0);
|
||||||
if (P(.5)) side *= -1; // change meandring direction in 50%
|
const dist2 = (eX - sX) ** 2 + (eY - sY) ** 2; // square distance between segment start and end
|
||||||
const dist2 = (eX - sX) ** 2 + (eY - sY) ** 2;
|
|
||||||
|
if (width < 10 && (dist2 > 64 || (dist2 > 36 && segments.length < 6))) {
|
||||||
// if dist2 is big or river is small add extra points at 1/3 and 2/3 of segment
|
// if dist2 is big or river is small add extra points at 1/3 and 2/3 of segment
|
||||||
if (dist2 > 64 || (dist2 > 16 && segments.length < 6)) {
|
const p1x = (sX * 2 + eX) / 3 + -sin * meander;
|
||||||
const p1x = (sX * 2 + eX) / 3 + side * -sin * meandr;
|
const p1y = (sY * 2 + eY) / 3 + cos * meander;
|
||||||
const p1y = (sY * 2 + eY) / 3 + side * cos * meandr;
|
const p2x = (sX + eX * 2) / 3 + sin * meander;
|
||||||
if (P(.2)) side *= -1; // change 2nd extra point meandring direction in 20%
|
const p2y = (sY + eY * 2) / 3 + cos * meander;
|
||||||
const p2x = (sX + eX * 2) / 3 + side * sin * meandr;
|
riverMeandered.push([p1x, p1y], [p2x, p2y]);
|
||||||
const p2y = (sY + eY * 2) / 3 + side * cos * meandr;
|
} else if (dist2 > 25 || segments.length < 6) {
|
||||||
riverEnhanced.push([p1x, p1y], [p2x, p2y]);
|
|
||||||
// if dist is medium or river is small add 1 extra middlepoint
|
// if dist is medium or river is small add 1 extra middlepoint
|
||||||
} else if (dist2 > 16 || segments.length < 6) {
|
const p1x = (sX + eX) / 2 + -sin * meander;
|
||||||
const p1x = (sX + eX) / 2 + side * -sin * meandr;
|
const p1y = (sY + eY) / 2 + cos * meander;
|
||||||
const p1y = (sY + eY) / 2 + side * cos * meandr;
|
riverMeandered.push([p1x, p1y]);
|
||||||
riverEnhanced.push([p1x, p1y]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return riverEnhanced;
|
|
||||||
|
return riverMeandered;
|
||||||
}
|
}
|
||||||
|
|
||||||
const getPath = function(points, width = 1, increment = 1, starting = .1) {
|
const getPath = function(points, widthFactor = 1, sourceWidth = .1) {
|
||||||
let offset, extraOffset = starting; // starting river width (to make river source visible)
|
let offset, extraOffset = sourceWidth; // 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 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 widening = 1000 + riverLength * 30;
|
||||||
const riverPointsLeft = [], riverPointsRight = []; // store points on both sides to build a valid polygon
|
const riverPointsLeft = [], riverPointsRight = []; // store points on both sides to build a valid polygon
|
||||||
const last = points.length - 1;
|
const last = points.length - 1;
|
||||||
const factor = riverLength / points.length;
|
const factor = riverLength / points.length;
|
||||||
|
|
@ -323,7 +327,7 @@ const getPath = function(points, width = 1, increment = 1, starting = .1) {
|
||||||
const xNext = points[p+1][0], yNext = points[p + 1][1];
|
const xNext = points[p+1][0], yNext = points[p + 1][1];
|
||||||
angle = Math.atan2(yPrev - yNext, xPrev - xNext);
|
angle = Math.atan2(yPrev - yNext, xPrev - xNext);
|
||||||
sin = Math.sin(angle), cos = Math.cos(angle);
|
sin = Math.sin(angle), cos = Math.cos(angle);
|
||||||
offset = (Math.atan(Math.pow(p * factor, 2) / widening) / 2 * width) + extraOffset;
|
offset = (Math.atan(Math.pow(p * factor, 2) / widening) / 2 * widthFactor) + extraOffset;
|
||||||
const confOffset = Math.atan(c * 5 / widening);
|
const confOffset = Math.atan(c * 5 / widening);
|
||||||
extraOffset += confOffset;
|
extraOffset += confOffset;
|
||||||
xLeft = x + -sin * offset, yLeft = y + cos * (offset + confOffset);
|
xLeft = x + -sin * offset, yLeft = y + cos * (offset + confOffset);
|
||||||
|
|
@ -347,7 +351,7 @@ const getPath = function(points, width = 1, increment = 1, starting = .1) {
|
||||||
const right = lineGen(riverPointsRight);
|
const right = lineGen(riverPointsRight);
|
||||||
let left = lineGen(riverPointsLeft);
|
let left = lineGen(riverPointsLeft);
|
||||||
left = left.substring(left.indexOf("C"));
|
left = left.substring(left.indexOf("C"));
|
||||||
return [round(right + left, 2), rn(riverLength, 2)];
|
return [round(right + left, 2), rn(riverLength, 2), offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
const specify = function() {
|
const specify = function() {
|
||||||
|
|
@ -394,6 +398,6 @@ const getBasin = function(r, p, e) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {generate, resolveDepressions, addMeandring, getPath, specify, getName, getBasin, remove};
|
return {generate, resolveDepressions, addMeandering, getPath, specify, getName, getBasin, remove};
|
||||||
|
|
||||||
})));
|
})));
|
||||||
|
|
@ -521,7 +521,7 @@ function addRiverOnClick() {
|
||||||
i = min;
|
i = min;
|
||||||
}
|
}
|
||||||
|
|
||||||
const points = Rivers.addMeandring(dataRiver, Math.random() * .5 + .1);
|
const points = Rivers.addMeandering(dataRiver, 1, .5);
|
||||||
const width = Math.random() * .5 + .9;
|
const width = Math.random() * .5 + .9;
|
||||||
const increment = Math.random() * .4 + .8;
|
const increment = Math.random() * .4 + .8;
|
||||||
const [path, length] = Rivers.getPath(points, width, increment);
|
const [path, length] = Rivers.getPath(points, width, increment);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue