mirror of
https://github.com/JannisX11/blockbench.git
synced 2025-01-30 15:42:42 +08:00
79e371f621
Fix issue where variables set by keyframes could no longer Fix sign of camera rotation queries on X axis
2 lines
7.2 KiB
JavaScript
2 lines
7.2 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.variables={},this.variableHandler=null;let n={},a=!1;function s(e){this.lines=e.split(";").map((e=>f(e)))}function u(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 i(e,r){this.query=e,this.args=r.map((e=>f(e)))}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("."==e.substring(1,2)){let r=e.substring(0,1);"q"==r&&(e="query"+e.substring(1)),"v"==r&&(e="variable"+e.substring(1)),"t"==r&&(e="temp"+e.substring(1)),"c"==r&&(e="context"+e.substring(1))}if((r=e.length>4&&e.match(/(temp|variable)\.\w+=/))&&"="!==e[r.index+r[0].length]){return new c(r[0].replace(/=$/,""),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 u(10,n[0],e[0],e[1]):new u(10,n[0],n[1],0)}if(t=b(e,"&&",11)||b(e,"||",12)||b(e,"<=",14)||b(e,"<",13)||b(e,">=",16)||b(e,">",15)||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 u(t,0,a[1]);if(!1==="+*/<>=|&?:".includes(a[0].substr(-1)))return new u(t,a[0],a[1])}}(e,"-",2,!0)||b(e,"*",3)||b(e,"/",4,!0)||function(e,r){if(e[0]==r&&e.length>1)return new u(5,e.substr(1),0)}(e,"!"))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 u(100,s[0]);case"sin":return new u(101,s[0]);case"cos":return new u(102,s[0]);case"exp":return new u(103,s[0]);case"ln":return new u(104,s[0]);case"pow":return new u(105,s[0],s[1]);case"sqrt":return new u(106,s[0]);case"random":return new u(107,s[0],s[1]);case"ceil":return new u(108,s[0]);case"round":return new u(109,s[0]);case"trunc":return new u(110,s[0]);case"floor":return new u(111,s[0]);case"mod":return new u(112,s[0],s[1]);case"min":return new u(113,s[0],s[1]);case"max":return new u(114,s[0],s[1]);case"clamp":return new u(115,s[0],s[1],s[2]);case"lerp":return new u(116,s[0],s[1],s[2]);case"lerprotate":return new u(117,s[0],s[1],s[2]);case"asin":return new u(118,s[0]);case"acos":return new u(119,s[0]);case"atan":return new u(120,s[0]);case"atan2":return new u(121,s[0],s[1]);case"die_roll":return new u(122,s[0],s[1],s[2]);case"die_roll_integer":return new u(123,s[0],s[1],s[2]);case"hermite_blend":return new u(124,s[0]);case"random_integer":return new u(125,s[0],s[1],s[2])}}if((n=e.match(/[a-zA-Z0-9._]{2,}/g))&&1===n.length&&n[0].length>=e.length-2)return e;if(e.includes("(")&&")"==e[e.length-1]){let r,t=e.search(/\(/),n=e.substr(0,t),a=[e.substr(t+1,e.length-t-2)];for(;r=p(a[a.length-1],",");)a.splice(a.length-1,1,...r);return new i(n,a)}return 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 u(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 g(e,r){return"string"==typeof e&&"'"==e[0]||(e=v(e,!0)),"string"==typeof r&&"'"==r[0]||(r=v(r,!0)),e===r}function v(n,s){if(a=!1,"number"==typeof n)return n;if("string"==typeof n){if(null!=t[n])return t[n];var f=e.variables[n];return void 0===f&&(f=e.global_variables[n]),void 0===f&&"function"==typeof e.variableHandler&&(f=e.variableHandler(n,e.variables)),"string"!=typeof f||s?void 0===f?a=!0:"function"==typeof f&&(f=f()):f=e.parse(f,e.variables),f||0}if(n instanceof l)return v(n.value);if(n instanceof c)return e.variables[n.name]=v(n.value);if(n instanceof i){let r=n.args.map((e=>v(e)));return"function"==typeof e.variables[n.query]?e.variables[n.query](...r):"function"==typeof e.global_variables[n.query]?e.global_variables[n.query](...r):("function"==typeof e.variableHandler&&(f=e.variableHandler(n.query,e.variables,r)),0)}if(n instanceof u)switch(n.operator){case 1:return v(n.a)+v(n.b);case 2:return v(n.a)-v(n.b);case 3:return v(n.a)*v(n.b);case 4:return v(n.a)/v(n.b);case 5:return 0==v(n.a)?1:0;case 10:return v(n.a)?v(n.b):v(n.c);case 11:return v(n.a)&&v(n.b)?1:0;case 12:return v(n.a)||v(n.b)?1:0;case 13:return v(n.a)<v(n.b)?1:0;case 14:return v(n.a)<=v(n.b)?1:0;case 15:return v(n.a)>v(n.b)?1:0;case 16:return v(n.a)>=v(n.b)?1:0;case 17:return g(n.a,n.b)?1:0;case 18:return g(n.a,n.b)?0:1;case 19:var h=v(n.a);return a?v(n.b):h;case 100:return Math.abs(v(n.a));case 101:return Math.sin(v(n.a)*o());case 102:return Math.cos(v(n.a)*o());case 103:return Math.exp(v(n.a));case 104:return Math.log(v(n.a));case 105:return Math.pow(v(n.a),v(n.b));case 106:return Math.sqrt(v(n.a));case 107:return r.random(v(n.a),v(n.b));case 108:return Math.ceil(v(n.a));case 109:return Math.round(v(n.a));case 110:return Math.trunc(v(n.a));case 111:return Math.floor(v(n.a));case 112:return v(n.a)%v(n.b);case 113:return Math.min(v(n.a),v(n.b));case 114:return Math.max(v(n.a),v(n.b));case 115:return r.clamp(v(n.a),v(n.b),v(n.c));case 116:return r.lerp(v(n.a),v(n.b),v(n.c));case 117:return r.lerpRotate(v(n.a),v(n.b),v(n.c));case 118:return Math.asin(v(n.a))/o();case 119:return Math.acos(v(n.a))/o();case 120:return Math.atan(v(n.a))/o();case 121:return Math.atan2(v(n.a),v(n.b))/o();case 122:return r.dieRoll(v(n.a),v(n.b),v(n.c));case 123:return r.dieRollInt(v(n.a),v(n.b),v(n.c));case 124:let e=v(n.a);return 3*Math.pow(e,2)-2*Math.pow(e,3);case 125:return r.randomInt(v(n.a),v(n.b))}return 0}this.parse=(r,t)=>{if("number"==typeof r)return isNaN(r)?0:r;if("string"!=typeof r)return 0;var a;if((a=(a=r).toLowerCase().trim()).includes(";")&&(a=a.replace(/;\s+/g,";").replace(/;\s*$/,"")),r=a,this.cache_enabled&&n[r])var u=n[r];else{u=new s(r);this.cache_enabled&&(n[r]=u)}return function(r,t){if(t)for(var n in t)e.variables[n]=t[n];var a=0;for(var s of r.lines){let e=v(s);if(++a==r.lines.length||s instanceof l&&"return"===s.type)return e}return 0}(u,t)},this.resetVariables=()=>{e.variables={}}}}));
|