diff --git a/index.html b/index.html index d6baf5f2..d6c1ecfa 100644 --- a/index.html +++ b/index.html @@ -7676,12 +7676,9 @@ - - - diff --git a/package.json b/package.json index dc5a6c70..7ea67ccc 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "vite": "^3.0.0-beta.3" }, "dependencies": { - "lineclip": "^1.1.5" + "lineclip": "^1.1.5", + "polylabel": "^1.1.0" } } diff --git a/src/layers/renderers/drawIce.js b/src/layers/renderers/drawIce.js index 659e336e..dbeb9434 100644 --- a/src/layers/renderers/drawIce.js +++ b/src/layers/renderers/drawIce.js @@ -1,4 +1,5 @@ import {getGridPolygon} from "utils/graphUtils"; +import {aleaPRNG} from "scripts/aleaPRNG"; export function drawIce() { const {cells, vertices} = grid; diff --git a/src/layers/renderers/drawProvinces.js b/src/layers/renderers/drawProvinces.js index 58db5843..10b4de42 100644 --- a/src/layers/renderers/drawProvinces.js +++ b/src/layers/renderers/drawProvinces.js @@ -1,3 +1,5 @@ +import polylabel from "polylabel"; + export function drawProvinces() { const labelsOn = provs.attr("data-labels") == 1; provs.selectAll("*").remove(); diff --git a/src/layers/renderers/drawStates.js b/src/layers/renderers/drawStates.js index 2a26bb74..069585c5 100644 --- a/src/layers/renderers/drawStates.js +++ b/src/layers/renderers/drawStates.js @@ -1,3 +1,5 @@ +import polylabel from "polylabel"; + export function drawStates() { regions.selectAll("path").remove(); diff --git a/src/libs/alea.min.js b/src/libs/alea.min.js deleted file mode 100644 index f4ecef67..00000000 --- a/src/libs/alea.min.js +++ /dev/null @@ -1,3 +0,0 @@ -/*https://github.com/macmcmeans/aleaPRNG/blob/master/aleaPRNG-1.1.js -©2010 Johannes Baagøe, MIT license; Derivative ©2017-2020 W. Mac" McMeans, BSD license.*/ -const aleaPRNG=function(){return function(n){"use strict";var r,t,e,o,a,u=new Uint32Array(3),i="";function c(n){var a=function(){var n=4022871197,r=function(r){r=r.toString();for(var t=0,e=r.length;t>>0,n=(o*=n)>>>0,n+=4294967296*(o-=n)}return 2.3283064365386963e-10*(n>>>0)};return r.version="Mash 0.9",r}();r=a(" "),t=a(" "),e=a(" "),o=1;for(var u=0;uarguments[1]&&(n=arguments[1],r=arguments[0]),f(n)&&f(r)?Math.floor(l()*(r-n+1))+n:l()*(r-n)+n},l.restart=function(){c(a)},l.seed=function(){c(Array.prototype.slice.call(arguments))},l.version=function(){return"aleaPRNG 1.1.0"},l.versions=function(){return"aleaPRNG 1.1.0, "+i},0===n.length&&(window.crypto.getRandomValues(u),n=[u[0],u[1],u[2]]),a=n,c(n),l}(Array.prototype.slice.call(arguments))}; \ No newline at end of file diff --git a/src/libs/polylabel.min.js b/src/libs/polylabel.min.js deleted file mode 100644 index e2faf0b1..00000000 --- a/src/libs/polylabel.min.js +++ /dev/null @@ -1 +0,0 @@ -!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).polylabel=t()}}(function(){return function t(n,e,r){function o(a,u){if(!e[a]){if(!n[a]){var f="function"==typeof require&&require;if(!u&&f)return f(a,!0);if(i)return i(a,!0);var h=new Error("Cannot find module '"+a+"'");throw h.code="MODULE_NOT_FOUND",h}var s=e[a]={exports:{}};n[a][0].call(s.exports,function(t){var e=n[a][1][t];return o(e||t)},s,s.exports,t,n,e,r)}return e[a].exports}for(var i="function"==typeof require&&require,a=0;an!=l[1]>n&&t<(l[0]-d[0])*(n-d[1])/(l[1]-d[1])+d[0]&&(r=!r),o=Math.min(o,a(t,n,d,l))}return(r?1:-1)*Math.sqrt(o)}(t,n,r),this.max=this.d+this.h*Math.SQRT2}function a(t,n,e,r){var o=e[0],i=e[1],a=r[0]-o,u=r[1]-i;if(0!==a||0!==u){var f=((t-o)*a+(n-i)*u)/(a*a+u*u);f>1?(o=r[0],i=r[1]):f>0&&(o+=a*f,i+=u*f)}return(a=t-o)*a+(u=n-i)*u}n.exports=function(t,n,e){var a,u,f,h;n=n||1;for(var s=0;sf)&&(f=d[0]),(!s||d[1]>h)&&(h=d[1])}for(var l=f-a,p=h-u,c=Math.min(l,p),v=c/2,g=new r(null,o),x=a;xw.d&&(w=b,e&&console.log("found best %d after %d probes",Math.round(1e4*b.d)/1e4,m)),b.max-w.d<=n||(v=b.h/2,g.push(new i(b.x-v,b.y-v,v,t)),g.push(new i(b.x+v,b.y-v,v,t)),g.push(new i(b.x-v,b.y+v,v,t)),g.push(new i(b.x+v,b.y+v,v,t)),m+=4)}e&&(console.log("num probes: "+m),console.log("best distance: "+w.d));return[w.x,w.y]}},{tinyqueue:2}],2:[function(t,n,e){"use strict";function r(t,n){if(!(this instanceof r))return new r(t,n);if(this.data=t||[],this.length=this.data.length,this.compare=n||o,t)for(var e=Math.floor(this.length/2);e>=0;e--)this._down(e)}function o(t,n){return tn?1:0}function i(t,n,e){var r=t[n];t[n]=t[e],t[e]=r}n.exports=r,r.prototype={push:function(t){this.data.push(t),this.length++,this._up(this.length-1)},pop:function(){var t=this.data[0];return this.data[0]=this.data[this.length-1],this.length--,this.data.pop(),this._down(0),t},peek:function(){return this.data[0]},_up:function(t){for(var n=this.data,e=this.compare;t>0;){var r=Math.floor((t-1)/2);if(!(e(n[t],n[r])<0))break;i(n,r,t),t=r}},_down:function(t){for(var n=this.data,e=this.compare,r=this.length;;){var o=2*t+1,a=o+1,u=t;if(o pack.cells.h[id] < 20; diff --git a/src/modules/ui/heightmap-editor.js b/src/modules/ui/heightmap-editor.js index ca607513..08dec214 100644 --- a/src/modules/ui/heightmap-editor.js +++ b/src/modules/ui/heightmap-editor.js @@ -11,6 +11,7 @@ import {turnLayerButtonOff, turnLayerButtonOn, updatePresetInput} from "layers"; import {restoreDefaultEvents} from "scripts/events"; import {prompt} from "scripts/prompt"; import {clearMainTip, showMainTip, tip} from "scripts/tooltips"; +import {aleaPRNG} from "scripts/aleaPRNG"; export function editHeightmap(options) { const {mode, tool} = options || {}; diff --git a/src/modules/ui/tools.js b/src/modules/ui/tools.js index dad6f4a1..3ea66a61 100644 --- a/src/modules/ui/tools.js +++ b/src/modules/ui/tools.js @@ -8,6 +8,7 @@ import {prompt} from "scripts/prompt"; import {getNextId} from "utils/nodeUtils"; import {P, generateSeed} from "utils/probabilityUtils"; import {turnLayerButtonOn} from "layers"; +import {aleaPRNG} from "scripts/aleaPRNG"; toolsContent.addEventListener("click", function (event) { if (customization) return tip("Please exit the customization mode first", false, "warning"); diff --git a/src/scripts/aleaPRNG.js b/src/scripts/aleaPRNG.js new file mode 100644 index 00000000..876d7dc8 --- /dev/null +++ b/src/scripts/aleaPRNG.js @@ -0,0 +1,186 @@ +/*//////////////////////////////////////////////////////////////// +aleaPRNG 1.1 +////////////////////////////////////////////////////////////////// +https://github.com/macmcmeans/aleaPRNG/blob/master/aleaPRNG-1.1.js +////////////////////////////////////////////////////////////////// +Original work copyright © 2010 Johannes Baagøe, under MIT license +This is a derivative work copyright (c) 2017-2020, W. Mac" McMeans, under BSD license. +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +////////////////////////////////////////////////////////////////*/ +export function aleaPRNG() { + return (function (args) { + "use strict"; + + const version = "aleaPRNG 1.1.0"; + + var s0, + s1, + s2, + c, + uinta = new Uint32Array(3), + initialArgs, + mashver = ""; + /* private: initializes generator with specified seed */ + function _initState(_internalSeed) { + var mash = Mash(); + + // internal state of generator + s0 = mash(" "); + s1 = mash(" "); + s2 = mash(" "); + + c = 1; + + for (var i = 0; i < _internalSeed.length; i++) { + s0 -= mash(_internalSeed[i]); + if (s0 < 0) { + s0 += 1; + } + + s1 -= mash(_internalSeed[i]); + if (s1 < 0) { + s1 += 1; + } + + s2 -= mash(_internalSeed[i]); + if (s2 < 0) { + s2 += 1; + } + } + + mashver = mash.version; + + mash = null; + } + + /* private: dependent string hash function */ + function Mash() { + var n = 4022871197; // 0xefc8249d + + var mash = function (data) { + data = data.toString(); + + // cache the length + for (var i = 0, l = data.length; i < l; i++) { + n += data.charCodeAt(i); + + var h = 0.02519603282416938 * n; + + n = h >>> 0; + h -= n; + h *= n; + n = h >>> 0; + h -= n; + n += h * 4294967296; // 0x100000000 2^32 + } + return (n >>> 0) * 2.3283064365386963e-10; // 2^-32 + }; + + mash.version = "Mash 0.9"; + return mash; + } + + /* private: check if number is integer */ + function _isInteger(_int) { + return parseInt(_int, 10) === _int; + } + + /* public: return a 32-bit fraction in the range [0, 1] + This is the main function returned when aleaPRNG is instantiated + */ + var random = function () { + var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32 + + s0 = s1; + s1 = s2; + + return (s2 = t - (c = t | 0)); + }; + + /* public: return a 53-bit fraction in the range [0, 1] */ + random.fract53 = function () { + return random() + ((random() * 0x200000) | 0) * 1.1102230246251565e-16; // 2^-53 + }; + + /* public: return an unsigned integer in the range [0, 2^32] */ + random.int32 = function () { + return random() * 0x100000000; // 2^32 + }; + + /* public: advance the generator the specified amount of cycles */ + random.cycle = function (_run) { + _run = typeof _run === "undefined" ? 1 : +_run; + if (_run < 1) { + _run = 1; + } + for (var i = 0; i < _run; i++) { + random(); + } + }; + + /* public: return inclusive range */ + random.range = function () { + var loBound, hiBound; + + if (arguments.length === 1) { + loBound = 0; + hiBound = arguments[0]; + } else { + loBound = arguments[0]; + hiBound = arguments[1]; + } + + if (arguments[0] > arguments[1]) { + loBound = arguments[1]; + hiBound = arguments[0]; + } + + // return integer + if (_isInteger(loBound) && _isInteger(hiBound)) { + return Math.floor(random() * (hiBound - loBound + 1)) + loBound; + + // return float + } else { + return random() * (hiBound - loBound) + loBound; + } + }; + + /* public: initialize generator with the seed values used upon instantiation */ + random.restart = function () { + _initState(initialArgs); + }; + + /* public: seeding function */ + random.seed = function () { + _initState(Array.prototype.slice.call(arguments)); + }; + + /* public: show the version of the RNG */ + random.version = function () { + return version; + }; + + /* public: show the version of the RNG and the Mash string hasher */ + random.versions = function () { + return version + ", " + mashver; + }; + + // when no seed is specified, create a random one from Windows Crypto (Monte Carlo application) + if (args.length === 0) { + window.crypto.getRandomValues(uinta); + args = [uinta[0], uinta[1], uinta[2]]; + } + + // store the seed used when the RNG was instantiated, if any + initialArgs = args; + + // initialize the RNG + _initState(args); + + return random; + })(Array.prototype.slice.call(arguments)); +} diff --git a/src/utils/graphUtils.ts b/src/utils/graphUtils.ts index 2e1b8aea..b4765801 100644 --- a/src/utils/graphUtils.ts +++ b/src/utils/graphUtils.ts @@ -4,6 +4,7 @@ import {createTypedArray} from "./arrayUtils"; import {rn} from "./numberUtils"; import {byId} from "./shorthands"; import {Voronoi} from "/src/modules/voronoi"; +import {aleaPRNG} from "scripts/aleaPRNG"; const Delaunator = window.Delaunator; diff --git a/yarn.lock b/yarn.lock index a6622aeb..8e631f01 100644 --- a/yarn.lock +++ b/yarn.lock @@ -172,6 +172,13 @@ picocolors@^1.0.0: resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.0.tgz#cb5bdc74ff3f51892236eaf79d68bc44564ab81c" integrity sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ== +polylabel@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/polylabel/-/polylabel-1.1.0.tgz#9483e64fc7a12a49f43e07e7a06752214ed2a8e7" + integrity sha512-bxaGcA40sL3d6M4hH72Z4NdLqxpXRsCFk8AITYg6x1rn1Ei3izf00UMLklerBZTO49aPA3CYrIwVulx2Bce2pA== + dependencies: + tinyqueue "^2.0.3" + postcss@^8.4.14: version "8.4.14" resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.14.tgz#ee9274d5622b4858c1007a74d76e42e56fd21caf" @@ -207,6 +214,11 @@ supports-preserve-symlinks-flag@^1.0.0: resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09" integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w== +tinyqueue@^2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/tinyqueue/-/tinyqueue-2.0.3.tgz#64d8492ebf39e7801d7bd34062e29b45b2035f08" + integrity sha512-ppJZNDuKGgxzkHihX8v9v9G5f+18gzaTfrukGrq6ueg0lmH4nqVnA2IPG0AEH3jKEk2GRJCUhDoqpoiw3PHLBA== + typescript@^4.7.4: version "4.7.4" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.4.tgz#1a88596d1cf47d59507a1bcdfb5b9dfe4d488235"