lootalot
Version:
Library for simulating loot table drops of arbitrary trial counts
3 lines (2 loc) • 6.39 kB
JavaScript
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((t="undefined"!=typeof globalThis?globalThis:t||self).lootalot={})}(this,(function(t){"use strict";function r(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}function e(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}function n(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,i(n.key),n)}}function o(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!e){if(Array.isArray(t)||(e=u(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var n=0,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,i=!0,l=!1;return{s:function(){e=e.call(t)},n:function(){var t=e.next();return i=t.done,t},e:function(t){l=!0,a=t},f:function(){try{i||null==e.return||e.return()}finally{if(l)throw a}}}}function a(t){return function(t){if(Array.isArray(t))return r(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||u(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(t){var r=function(t,r){if("object"!=typeof t||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var n=e.call(t,r||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}(t,"string");return"symbol"==typeof r?r:r+""}function u(t,e){if(t){if("string"==typeof t)return r(t,e);var n={}.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(t,e):void 0}}var l;!function(t){t.equal="equal",t.strict_equal="strict_equal",t.json="json"}(l||(l={}));var f=1e-8,c=20,s={duplicateSearchMode:"equal"};function v(t){var r=.1400122886866665,e=Math.log(1-Math.pow(t,2))/r,n=2/Math.PI/r+Math.log(1-Math.pow(t,2))/2;return Math.sign(t)*Math.sqrt(Math.sqrt(Math.pow(n,2)-e)-n)}function h(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:1,n=1.4142135623730951;return v(2*t-1)*n*e+r}function d(t,r){if(r<=0)return 0;if(r>=1)return t;if(t<=c){for(var e=0,n=0;n<t;n++)e+=+(Math.random()<r);return e}var o=t*r,a=Math.sqrt(o*(1-r));return Math.round(m(h(Math.random(),o,a),0,t))}function p(t,r,e){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:1;if(r==e)return r*t;if(t<=c){for(var o=0,a=0;a<t;a++){var i=Math.random()*(e-r+n)+r;n>0&&(i=Math.floor(i/n)*n),o+=i}return o}var u=(e+r)/2*t,l=Math.sqrt((Math.pow(e-r,2)-1)/12*t);return n>0?(n=y(r,e,n),m(Math.round(h(Math.random(),u/n,l/n))*n,r*t,e*t)):m(h(Math.random(),u,l),r*t,e*t)}function y(){for(var t=arguments.length,r=new Array(t),e=0;e<t;e++)r[e]=arguments[e];var n=r[0],o=r[1];if(r.length>2&&(o=y.apply(void 0,a(r.slice(1)))),n<o){var i=[o,n];n=i[0],o=i[1]}for(;n%o!=0;){var u=[o,n%o];n=u[0],o=u[1]}return o}function m(t,r,e){return Math.max(Math.min(t,e),r)}var b=function(){return function(t,r,e){return r&&n(t.prototype,r),e&&n(t,e),Object.defineProperty(t,"prototype",{writable:!1}),t}((function t(){var r,n,a,i,u,l,c,v,h;e(this,t),this.pools=[],this.prefs=Object.assign({},s),this.prefs=Object.assign({},s);for(var d=arguments.length,p=new Array(d),y=0;y<d;y++)p[y]=arguments[y];for(var m=0,b=p;m<b.length;m++){var w=b[m],M=[];this.pools.push(M);var j,S=!1,A=!1,O=0,P=0,q=o(w);try{for(q.s();!(j=q.n()).done;){var E=j.value;if(S||(S=void 0!==E.w||void 0===E.p),A||(A=void 0!==E.p),O+=null!==(r=E.p)&&void 0!==r?r:0,P+=null!==(n=E.w)&&void 0!==n?n:1,S&&A)throw Error("All loot definitions in a pool must either use `p` for probability or `w` for weight");if((null!==(a=E.w)&&void 0!==a?a:1)<0)throw Error("Weight can not be negative");if((null!==(i=E.p)&&void 0!==i?i:0)<0)throw Error("Probabilty can not be negative");if((null!==(u=E.step)&&void 0!==u?u:1)<0)throw Error("Step can not be negative")}}catch(t){q.e(t)}finally{q.f()}if(O>1+f)throw Error("All loot definitions in a pool must have their `p` values sum to 1 or less (sum = "+O+")");A&&(P=1);var T,I=o(w);try{for(I.s();!(T=I.n()).done;){var x=T.value,_={w:null!==(c=null!==(l=x.w)&&void 0!==l?l:x.p)&&void 0!==c?c:1,cascadeP:0,item:x.item,table:x.table,count:null!==(v=x.count)&&void 0!==v?v:1,step:null!==(h=x.step)&&void 0!==h?h:g(x.count)};M.push(_)}}catch(t){I.e(t)}finally{I.f()}A&&O<1-f&&M.push({w:1-O,cascadeP:0,count:1,step:1}),M.sort((function(t,r){return r.w-t.w}));for(var k=0,N=M;k<N.length;k++){var C=N[k];C.cascadeP=C.w/P,P-=C.w}M[M.length-1].cascadeP=1}}),[{key:"loot",value:function(t){var r=this,e=[];function n(t,n){var o=e.find((function(e){return n=e.item,o=t,r.isDuplicate.call(r,n,o);var n,o}));o?o.count+=n:e.push({item:t,count:n})}var a,i=o(this.pools);try{for(i.s();!(a=i.n()).done;){var u,l=a.value,f=t,c=o(l);try{var s,v=function(){var t=u.value,r=d(f,t.cascadeP);if(r<=0)return 0;f-=r;var e=0;if(e="number"==typeof t.count?r*t.count:p(r,t.count[0],t.count[1],t.step),void 0!==t.table){var a=t.table.loot(e);void 0!==t.item&&(a=a.map((function(r){return Object.assign(Object.assign({},r),{item:Object.assign(Object.assign({},t.item),r.item)})})));var i,l=o(a);try{for(l.s();!(i=l.n()).done;){var c=i.value;n(c.item,c.count)}}catch(t){l.e(t)}finally{l.f()}}else void 0!==t.item&&n(t.item,e);return f<=0?1:void 0};for(c.s();!(u=c.n()).done&&(0===(s=v())||1!==s););}catch(t){c.e(t)}finally{c.f()}}}catch(t){i.e(t)}finally{i.f()}return e}},{key:"isDuplicate",value:function(t,r){switch(this.prefs.duplicateSearchMode){case"equal":return t==r;case"strict_equal":return t===r;case"json":return JSON.stringify(t)==JSON.stringify(r)}}}])}();function g(t){return void 0===t?1:"number"==typeof t?+(t%1==0):+(t[0]%1==0&&t[1]%1==0)}t.LootTable=b,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=lootalot.min.js.map