blockbench/lib/molang.umd.js
JannisX11 38b4386cb6 Fix #1725 issue with nested molang expressions
Issue with variables when using variable placeholders with expressions
2023-02-25 22:28:01 +01:00

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