-
Notifications
You must be signed in to change notification settings - Fork 63
/
rivers.js
51 lines (45 loc) · 1.46 KB
/
rivers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* From http://www.redblobgames.com/maps/mapgen2/
* Copyright 2017 Red Blob Games <[email protected]>
* License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
*/
'use strict';
const MIN_SPRING_ELEVATION = 0.3;
const MAX_SPRING_ELEVATION = 0.9;
/**
* Find candidates for river sources
*
* Unlike the assign_* functions this does not write into an existing array
*/
exports.find_spring_t = function(mesh, r_water, t_elevation, t_downslope_s) {
const t_water = (t) =>
( r_water[mesh.s_begin_r(3*t)]
|| r_water[mesh.s_begin_r(3*t+1)]
|| r_water[mesh.s_begin_r(3*t+2)] );
let spring_t = new Set();
// Add everything above some elevation, but not lakes
for (let t = 0; t < mesh.numSolidTriangles; t++) {
if (t_elevation[t] >= MIN_SPRING_ELEVATION &&
t_elevation[t] <= MAX_SPRING_ELEVATION &&
!t_water(t)) {
spring_t.add(t);
}
}
return Array.from(spring_t);
};
exports.assign_s_flow = function(s_flow, mesh, t_downslope_s, river_t) {
// Each river in river_t contributes 1 flow down to the coastline
s_flow.length = mesh.numSides;
s_flow.fill(0);
for (let t of river_t) {
for (;;) {
let s = t_downslope_s[t];
if (s === -1) { break; }
s_flow[s]++;
let next_t = mesh.s_outer_t(s);
if (next_t === t) { break; }
t = next_t;
}
}
return s_flow;
};