blockbench/lib/molang.umd.js
JannisX11 a1bcfdfb83 Fix #1732 Clicking autocomplete doesn't update text correctly
Fix pressing tab does not switch between UV editor sliders
Fix MolangJS error when not enough arguments are present in functions
2023-01-20 17:59:56 +01:00

2 lines
8.6 KiB
JavaScript

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