From 4902a321ea2d5037e1faa02eb7118a06e34e15c9 Mon Sep 17 00:00:00 2001 From: Azgaar Date: Fri, 23 Jul 2021 20:48:38 +0300 Subject: [PATCH] river type - memoize what is small size for river --- modules/river-generator.js | 23 ++++++++++++++--------- modules/ui/tools.js | 3 +-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/modules/river-generator.js b/modules/river-generator.js index 3859ec9d..e86b5c64 100644 --- a/modules/river-generator.js +++ b/modules/river-generator.js @@ -388,14 +388,11 @@ const specify = function () { const rivers = pack.rivers; if (!rivers.length) return; - Math.random = aleaPRNG(seed); - const thresholdElement = Math.ceil(rivers.length * 0.15); - const smallLength = rivers.map(r => r.length || 0).sort((a, b) => a - b)[thresholdElement]; - for (const r of rivers) { - r.basin = getBasin(r.i); - r.name = getName(r.mouth); - r.type = getType(r, r.length < smallLength); + for (const river of rivers) { + river.basin = getBasin(river.i); + river.name = getName(river.mouth); + river.type = getType(river); } }; @@ -414,8 +411,16 @@ small: {Branch: 1} } }; - const getType = function (river, isSmall) { - const isFork = each(3)(river.i) && river.parent && river.parent !== river.i; + + let smallLength = null; + const getType = function ({i, length, parent}) { + if (smallLength === null) { + const threshold = Math.ceil(pack.rivers.length * 0.15); + smallLength = pack.rivers.map(r => r.length || 0).sort((a, b) => a - b)[threshold]; + } + + const isSmall = length < smallLength; + const isFork = each(3)(i) && parent && parent !== i; return rw(riverTypes[isFork ? "fork" : "main"][isSmall ? "small" : "big"]); }; diff --git a/modules/ui/tools.js b/modules/ui/tools.js index f358d517..5c13b6e3 100644 --- a/modules/ui/tools.js +++ b/modules/ui/tools.js @@ -639,8 +639,7 @@ function addRiverOnClick() { } else { const basin = Rivers.getBasin(parent); const name = Rivers.getName(mouth); - const smallLength = rivers.map(r => r.length || 0).sort((a, b) => a - b)[Math.ceil(pack.rivers.length * 0.15)]; - const type = length < smallLength ? rw({Creek: 9, River: 3, Brook: 3, Stream: 1}) : "River"; + const type = Rivers.getType({i: riverId, length, parent}); rivers.push({i: riverId, source, mouth, discharge, length, width, widthFactor, sourceWidth, parent, cells: riverCells, basin, name, type}); }