Index: binaries/data/mods/public/maps/random/rmgen/valueNoise2d.js =================================================================== --- /dev/null +++ binaries/data/mods/public/maps/random/rmgen/valueNoise2d.js @@ -0,0 +1,42 @@ +/** + * @returns {Number} - Range [-1,1] + */ +function randomNoiseHashx(x, y) { + const v = Math.sin(x * 17.19 + y * 7238) * 3452; + return (v - Math.floor(v)) * 2 - 1; +} + +/** + * @returns {Number} - Range [-1,1] + */ +function randomNoiseHashy(x, y) { + const v = Math.sin(x * 7.65 + y * 2345) * 5604; + return (v - Math.floor(v)) * 2 - 1; +} + +/** + * Smooth uniform distributed noise using simple bilinear interpolation. + * @param {Number} x - x coordinate. 4 units equals to 1 map tile unit. + * @param {Number} y - y coordinate. 4 units equals to 1 map tile unit. + * @returns {Number} - Range [-1,1]. + */ +function valueNoise2d(x, y) { + + const ix = Math.floor(x); + const iy = Math.floor(y); + + const v1 = randomNoiseHashx(ix, iy); + const v2 = randomNoiseHashx(ix + 1, iy); + const v3 = randomNoiseHashx(ix, iy + 1); + const v4 = randomNoiseHashx(ix + 1, iy + 1); + + const ux = easeCurve(x - ix); + const uy = easeCurve(y - iy); + + const mix = (x, y, a) => x * (1 - a) + y * a; + const mix12 = mix(v1, v2, ux); + const mix34 = mix(v3, v4, ux); + const mix1234 = mix(mix12, mix34, uy); + + return mix1234; +}