blockbench/lib/molang.umd.js

2 lines
6.4 KiB
JavaScript

!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self).Molang=r()}(this,(function(){"use strict";const e=e=>((e+180)%360+180)%360;var r={clamp:(e,r,t)=>(e>t&&(e=t),(e<r||isNaN(e))&&(e=r),e),random:(e,r)=>e+Math.random()*(r-e),randomInt:(e,r)=>(e=Math.ceil(e),r=Math.floor(r),e+Math.floor(Math.random()*(r-e+1))),dieRoll(e,r,t){e=this.clamp(e,0,1e9);let n=0;for(var a=0;a<e;a++)n+=this.random(r,t);return n},dieRollInt(e,r,t){e=this.clamp(e,0,1e9);let n=0;for(var a=0;a<e;a++)n+=this.randomInt(r,t);return n},lerp:(e,r,t)=>e+(r-e)*t,lerpRotate(r,t,n){let a=e(r),s=e(t);a>s&&([a,s]=[s,a]);var u=s-a;return u>180?e(s+n*(360-u)):a+n*u}};const t={true:1,false:0};return function(){const e=this;this.global_variables={},this.cache_enabled=!0,this.use_radians=!1,this.variableHandler=null;let n={},a={},s=!1;function u(e){this.lines=e.split(";").map((e=>f(e)))}function i(e,r,t,n){this.operator=e,this.a=f(r),void 0!==t&&(this.b=f(t)),void 0!==n&&(this.c=f(n))}function c(e,r){this.value=f(r),this.name=e}function l(e,r){this.value=f(r),this.type=e}let o=()=>this.use_radians?1:Math.PI/180;function f(e){if(!e)return 0;if(!isNaN(e))return parseFloat(e);for(e=e.replace(/\s/g,"");h(e);)e=e.substr(1,e.length-2);var r;if(r=e.length>5&&e.match(/^return/))return new l(r[0],e.substr(r[0].length));if((r=e.length>4&&e.match(/(temp|variable|t|v)\.\w+=/))&&"="!==e[r.index+r[0].length]){return new c(r[0].replace(/=$/,"").replace(/^v\./,"variable.").replace(/^t\./,"temp."),e.substr(r.index+r[0].length))}if(t=b(e,"??",19))return t;var t,n=p(e,"?");if(n){let e=p(n[1],":");return e&&e.length?new i(10,n[0],e[0],e[1]):new i(10,n[0],n[1],0)}if(t=b(e,"&&",11)||b(e,"||",12)||b(e,"<",13)||b(e,"<=",14)||b(e,">",15)||b(e,">=",16)||b(e,"==",17)||b(e,"!=",18)||b(e,"+",1,!0)||function(e,r,t,n){var a=p(e,r,n);if(a){if(0===a[0].length)return new i(t,0,a[1]);if(!1==="+*/<>=|&?:".includes(a[0].substr(-1)))return new i(t,a[0],a[1])}}(e,"-",2,!0)||b(e,"*",3)||b(e,"/",4))return t;if("math."===e.substr(0,5)){if("math.pi"===e.substr(0,7))return Math.PI;let r=e.search(/\(/),t=e.substr(5,r-5),n=e.substr(r+1,e.length-r-2),s=p(n,",")||[n];if(s.length>1){var a=p(s[1],",");a&&a.length>1&&(s[1]=a[0],s[2]=a[1])}switch(t){case"abs":return new i(100,s[0]);case"sin":return new i(101,s[0]);case"cos":return new i(102,s[0]);case"exp":return new i(103,s[0]);case"ln":return new i(104,s[0]);case"pow":return new i(105,s[0],s[1]);case"sqrt":return new i(106,s[0]);case"random":return new i(107,s[0],s[1]);case"ceil":return new i(108,s[0]);case"round":return new i(109,s[0]);case"trunc":return new i(110,s[0]);case"floor":return new i(111,s[0]);case"mod":return new i(112,s[0],s[1]);case"min":return new i(113,s[0],s[1]);case"max":return new i(114,s[0],s[1]);case"clamp":return new i(115,s[0],s[1],s[2]);case"lerp":return new i(116,s[0],s[1],s[2]);case"lerprotate":return new i(117,s[0],s[1],s[2]);case"asin":return new i(118,s[0]);case"acos":return new i(119,s[0]);case"atan":return new i(120,s[0]);case"atan2":return new i(121,s[0],s[1]);case"die_roll":return new i(122,s[0],s[1],s[2]);case"die_roll_integer":return new i(123,s[0],s[1],s[2]);case"hermite_blend":return new i(124,s[0]);case"random_integer":return new i(125,s[0],s[1],s[2])}}return(n=e.match(/[a-zA-Z0-9._]{2,}/g))&&1===n.length?e:0}function h(e){if("("===e.substr(0,1)&&")"===e.substr(-1)){let t=0;for(var r=0;r<e.length-1;r++){switch(e[r]){case"(":t++;break;case")":t--}if(0==t)return!1}return!0}}function b(e,r,t,n){var a=p(e,r,n);if(a)return new i(t,a[0],a[1])}function p(e,r,t){for(var n=t?-1:1,a=t?e.length-1:0,s=0,u="string"==typeof r;t?a>=0:a<e.length;){if("("===e[a])s+=n;else if(")"===e[a])s-=n;else if(0===s){var i=e.substr(a,r.length);if(u&&i===r)return[e.substr(0,a),e.substr(a+r.length)];if(!u)for(var c=0;c<r.length;c++)if(r[c]===i)return[e.substr(0,a),e.substr(a+r[c].length)]}a+=n}}function d(n){if(s=!1,"number"==typeof n)return n;if("string"==typeof n){if(null!=t[n])return t[n];if("."==n.substr(1,1)){let e=n.substr(0,1);"q"==e&&(n="query"+n.substr(1)),"v"==e&&(n="variable"+n.substr(1)),"t"==e&&(n="temp"+n.substr(1))}var u=a[n];return void 0===u&&(u=e.global_variables[n]),void 0===u&&"function"==typeof e.variableHandler&&(u=e.variableHandler(n,a)),"string"==typeof u?u=e.parse(u,a):void 0===u&&(s=!0),u||0}if(n instanceof l)return d(n.value);if(n instanceof c)return a[n.name]=d(n.value);if(n instanceof i)switch(n.operator){case 1:return d(n.a)+d(n.b);case 2:return d(n.a)-d(n.b);case 3:return d(n.a)*d(n.b);case 4:return d(n.a)/d(n.b);case 10:return d(n.a)?d(n.b):d(n.c);case 11:return d(n.a)&&d(n.b)?1:0;case 12:return d(n.a)||d(n.b)?1:0;case 13:return d(n.a)<d(n.b)?1:0;case 14:return d(n.a)<=d(n.b)?1:0;case 15:return d(n.a)>d(n.b)?1:0;case 16:return d(n.a)>=d(n.b)?1:0;case 17:return d(n.a)===d(n.b)?1:0;case 18:return d(n.a)!==d(n.b)?1:0;case 19:var f=d(n.a);return s?d(n.b):f;case 100:return Math.abs(d(n.a));case 101:return Math.sin(d(n.a)*o());case 102:return Math.cos(d(n.a)*o());case 103:return Math.exp(d(n.a));case 104:return Math.log(d(n.a));case 105:return Math.pow(d(n.a),d(n.b));case 106:return Math.sqrt(d(n.a));case 107:return r.random(d(n.a),d(n.b));case 108:return Math.ceil(d(n.a));case 109:return Math.round(d(n.a));case 110:return Math.trunc(d(n.a));case 111:return Math.floor(d(n.a));case 112:return d(n.a)%d(n.b);case 113:return Math.min(d(n.a),d(n.b));case 114:return Math.max(d(n.a),d(n.b));case 115:return r.clamp(d(n.a),d(n.b),d(n.c));case 116:return r.lerp(d(n.a),d(n.b),d(n.c));case 117:return r.lerpRotate(d(n.a),d(n.b),d(n.c));case 118:return Math.asin(d(n.a))/o();case 119:return Math.acos(d(n.a))/o();case 120:return Math.atan(d(n.a))/o();case 121:return Math.atan2(d(n.a),d(n.b))/o();case 122:return r.dieRoll(d(n.a),d(n.b),d(n.c));case 123:return r.dieRollInt(d(n.a),d(n.b),d(n.c));case 124:let e=d(n.a);return 3*Math.pow(e,2)-2*Math.pow(e,3);case 125:return r.randomInt(d(n.a),d(n.b))}return 0}this.parse=(e,r)=>{if("number"==typeof e)return isNaN(e)?0:e;if("string"!=typeof e)return 0;var t;if((t=(t=e).toLowerCase().trim()).includes(";")&&(t=t.replace(/;\s+/g,";").replace(/;\s*$/,"")),e=t,this.cache_enabled&&n[e])var s=n[e];else{s=new u(e);this.cache_enabled&&(n[e]=s)}return function(e,r){if(r)for(var t in r)a[t]=r[t];var n=0;for(var s of e.lines){let r=d(s);if(++n==e.lines.length||s instanceof l&&"return"===s.type)return r}return 0}(s,r)},this.resetVariables=()=>{a={}}}}));