@almadar/evaluator 2.12.2 → 2.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +597 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -733,6 +733,50 @@ function evalMathDefault(args, evaluate2, ctx) {
|
|
|
733
733
|
}
|
|
734
734
|
return n;
|
|
735
735
|
}
|
|
736
|
+
function evalMathSin(args, evaluate2, ctx) {
|
|
737
|
+
return Math.sin(evaluate2(args[0], ctx));
|
|
738
|
+
}
|
|
739
|
+
function evalMathCos(args, evaluate2, ctx) {
|
|
740
|
+
return Math.cos(evaluate2(args[0], ctx));
|
|
741
|
+
}
|
|
742
|
+
function evalMathTan(args, evaluate2, ctx) {
|
|
743
|
+
return Math.tan(evaluate2(args[0], ctx));
|
|
744
|
+
}
|
|
745
|
+
function evalMathAtan2(args, evaluate2, ctx) {
|
|
746
|
+
const y = evaluate2(args[0], ctx);
|
|
747
|
+
const x = evaluate2(args[1], ctx);
|
|
748
|
+
return Math.atan2(y, x);
|
|
749
|
+
}
|
|
750
|
+
function evalMathHypot(args, evaluate2, ctx) {
|
|
751
|
+
const a = evaluate2(args[0], ctx);
|
|
752
|
+
const b = evaluate2(args[1], ctx);
|
|
753
|
+
return Math.hypot(a, b);
|
|
754
|
+
}
|
|
755
|
+
function evalMathDegRad(args, evaluate2, ctx) {
|
|
756
|
+
return evaluate2(args[0], ctx) * (Math.PI / 180);
|
|
757
|
+
}
|
|
758
|
+
function evalMathRadDeg(args, evaluate2, ctx) {
|
|
759
|
+
return evaluate2(args[0], ctx) * (180 / Math.PI);
|
|
760
|
+
}
|
|
761
|
+
function evalMathWrap(args, evaluate2, ctx) {
|
|
762
|
+
const v = evaluate2(args[0], ctx);
|
|
763
|
+
const min = evaluate2(args[1], ctx);
|
|
764
|
+
const max = evaluate2(args[2], ctx);
|
|
765
|
+
const r = max - min;
|
|
766
|
+
if (r <= 0) {
|
|
767
|
+
return min;
|
|
768
|
+
}
|
|
769
|
+
return min + ((v - min) % r + r) % r;
|
|
770
|
+
}
|
|
771
|
+
function evalMathApproach(args, evaluate2, ctx) {
|
|
772
|
+
const cur = evaluate2(args[0], ctx);
|
|
773
|
+
const target = evaluate2(args[1], ctx);
|
|
774
|
+
const maxD = evaluate2(args[2], ctx);
|
|
775
|
+
if (Math.abs(target - cur) <= maxD) {
|
|
776
|
+
return target;
|
|
777
|
+
}
|
|
778
|
+
return cur + Math.sign(target - cur) * maxD;
|
|
779
|
+
}
|
|
736
780
|
|
|
737
781
|
// std/str.ts
|
|
738
782
|
function evalStrLen(args, evaluate2, ctx) {
|
|
@@ -2575,6 +2619,454 @@ function evalAgentSearchCode(args, evaluate2, ctx) {
|
|
|
2575
2619
|
return ctx.agent.searchCode(query, language);
|
|
2576
2620
|
}
|
|
2577
2621
|
|
|
2622
|
+
// std/vector.ts
|
|
2623
|
+
function asVec(v) {
|
|
2624
|
+
const o = v ?? {};
|
|
2625
|
+
const x = typeof o.x === "number" ? o.x : 0;
|
|
2626
|
+
const y = typeof o.y === "number" ? o.y : 0;
|
|
2627
|
+
const z2 = typeof o.z === "number" ? o.z : void 0;
|
|
2628
|
+
return z2 === void 0 ? { x, y } : { x, y, z: z2 };
|
|
2629
|
+
}
|
|
2630
|
+
function has3(v) {
|
|
2631
|
+
return v.z !== void 0;
|
|
2632
|
+
}
|
|
2633
|
+
function z(v) {
|
|
2634
|
+
return v.z ?? 0;
|
|
2635
|
+
}
|
|
2636
|
+
function make(x, y, zVal, is3d) {
|
|
2637
|
+
return is3d ? { x, y, z: zVal } : { x, y };
|
|
2638
|
+
}
|
|
2639
|
+
function evalVecAdd(args, evaluate2, ctx) {
|
|
2640
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2641
|
+
const b = asVec(evaluate2(args[1], ctx));
|
|
2642
|
+
return make(a.x + b.x, a.y + b.y, z(a) + z(b), has3(a));
|
|
2643
|
+
}
|
|
2644
|
+
function evalVecSub(args, evaluate2, ctx) {
|
|
2645
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2646
|
+
const b = asVec(evaluate2(args[1], ctx));
|
|
2647
|
+
return make(a.x - b.x, a.y - b.y, z(a) - z(b), has3(a));
|
|
2648
|
+
}
|
|
2649
|
+
function evalVecScale(args, evaluate2, ctx) {
|
|
2650
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2651
|
+
const k = evaluate2(args[1], ctx);
|
|
2652
|
+
return make(a.x * k, a.y * k, z(a) * k, has3(a));
|
|
2653
|
+
}
|
|
2654
|
+
function evalVecDot(args, evaluate2, ctx) {
|
|
2655
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2656
|
+
const b = asVec(evaluate2(args[1], ctx));
|
|
2657
|
+
return a.x * b.x + a.y * b.y + z(a) * z(b);
|
|
2658
|
+
}
|
|
2659
|
+
function evalVecCross(args, evaluate2, ctx) {
|
|
2660
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2661
|
+
const b = asVec(evaluate2(args[1], ctx));
|
|
2662
|
+
if (has3(a)) {
|
|
2663
|
+
const ax = a.x, ay = a.y, az = z(a);
|
|
2664
|
+
const bx = b.x, by = b.y, bz = z(b);
|
|
2665
|
+
return { x: ay * bz - az * by, y: az * bx - ax * bz, z: ax * by - ay * bx };
|
|
2666
|
+
}
|
|
2667
|
+
return a.x * b.y - a.y * b.x;
|
|
2668
|
+
}
|
|
2669
|
+
function evalVecLength(args, evaluate2, ctx) {
|
|
2670
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2671
|
+
return Math.sqrt(a.x * a.x + a.y * a.y + z(a) * z(a));
|
|
2672
|
+
}
|
|
2673
|
+
function evalVecLengthSq(args, evaluate2, ctx) {
|
|
2674
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2675
|
+
return a.x * a.x + a.y * a.y + z(a) * z(a);
|
|
2676
|
+
}
|
|
2677
|
+
function evalVecNormalize(args, evaluate2, ctx) {
|
|
2678
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2679
|
+
const len = Math.sqrt(a.x * a.x + a.y * a.y + z(a) * z(a));
|
|
2680
|
+
if (len === 0) {
|
|
2681
|
+
return make(0, 0, 0, has3(a));
|
|
2682
|
+
}
|
|
2683
|
+
return make(a.x / len, a.y / len, z(a) / len, has3(a));
|
|
2684
|
+
}
|
|
2685
|
+
function evalVecDistance(args, evaluate2, ctx) {
|
|
2686
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2687
|
+
const b = asVec(evaluate2(args[1], ctx));
|
|
2688
|
+
const dx = a.x - b.x, dy = a.y - b.y, dz = z(a) - z(b);
|
|
2689
|
+
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
2690
|
+
}
|
|
2691
|
+
function evalVecDistanceSq(args, evaluate2, ctx) {
|
|
2692
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2693
|
+
const b = asVec(evaluate2(args[1], ctx));
|
|
2694
|
+
const dx = a.x - b.x, dy = a.y - b.y, dz = z(a) - z(b);
|
|
2695
|
+
return dx * dx + dy * dy + dz * dz;
|
|
2696
|
+
}
|
|
2697
|
+
function evalVecLerp(args, evaluate2, ctx) {
|
|
2698
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2699
|
+
const b = asVec(evaluate2(args[1], ctx));
|
|
2700
|
+
const t = evaluate2(args[2], ctx);
|
|
2701
|
+
return make(
|
|
2702
|
+
a.x + (b.x - a.x) * t,
|
|
2703
|
+
a.y + (b.y - a.y) * t,
|
|
2704
|
+
z(a) + (z(b) - z(a)) * t,
|
|
2705
|
+
has3(a)
|
|
2706
|
+
);
|
|
2707
|
+
}
|
|
2708
|
+
function evalVecAngle(args, evaluate2, ctx) {
|
|
2709
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2710
|
+
if (args.length < 2) {
|
|
2711
|
+
return Math.atan2(a.y, a.x);
|
|
2712
|
+
}
|
|
2713
|
+
const b = asVec(evaluate2(args[1], ctx));
|
|
2714
|
+
const la = Math.sqrt(a.x * a.x + a.y * a.y + z(a) * z(a));
|
|
2715
|
+
const lb = Math.sqrt(b.x * b.x + b.y * b.y + z(b) * z(b));
|
|
2716
|
+
if (la === 0 || lb === 0) {
|
|
2717
|
+
return 0;
|
|
2718
|
+
}
|
|
2719
|
+
const d = (a.x * b.x + a.y * b.y + z(a) * z(b)) / (la * lb);
|
|
2720
|
+
const clamped = Math.min(Math.max(d, -1), 1);
|
|
2721
|
+
return Math.acos(clamped);
|
|
2722
|
+
}
|
|
2723
|
+
function evalVecRotate(args, evaluate2, ctx) {
|
|
2724
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2725
|
+
const rad = evaluate2(args[1], ctx);
|
|
2726
|
+
const c = Math.cos(rad), s = Math.sin(rad);
|
|
2727
|
+
return make(a.x * c - a.y * s, a.x * s + a.y * c, z(a), has3(a));
|
|
2728
|
+
}
|
|
2729
|
+
function evalVecClampLength(args, evaluate2, ctx) {
|
|
2730
|
+
const a = asVec(evaluate2(args[0], ctx));
|
|
2731
|
+
const max = evaluate2(args[1], ctx);
|
|
2732
|
+
const len = Math.sqrt(a.x * a.x + a.y * a.y + z(a) * z(a));
|
|
2733
|
+
if (len > max) {
|
|
2734
|
+
if (len === 0) {
|
|
2735
|
+
return make(0, 0, 0, has3(a));
|
|
2736
|
+
}
|
|
2737
|
+
const k = max / len;
|
|
2738
|
+
return make(a.x * k, a.y * k, z(a) * k, has3(a));
|
|
2739
|
+
}
|
|
2740
|
+
return make(a.x, a.y, z(a), has3(a));
|
|
2741
|
+
}
|
|
2742
|
+
|
|
2743
|
+
// std/geo.ts
|
|
2744
|
+
function num(o, k) {
|
|
2745
|
+
return typeof o[k] === "number" ? o[k] : 0;
|
|
2746
|
+
}
|
|
2747
|
+
function asVec2(v) {
|
|
2748
|
+
const o = v ?? {};
|
|
2749
|
+
const z2 = typeof o.z === "number" ? o.z : void 0;
|
|
2750
|
+
return z2 === void 0 ? { x: num(o, "x"), y: num(o, "y") } : { x: num(o, "x"), y: num(o, "y"), z: z2 };
|
|
2751
|
+
}
|
|
2752
|
+
function asRect(v) {
|
|
2753
|
+
const o = v ?? {};
|
|
2754
|
+
return { x: num(o, "x"), y: num(o, "y"), w: num(o, "w"), h: num(o, "h") };
|
|
2755
|
+
}
|
|
2756
|
+
function asCircle(v) {
|
|
2757
|
+
const o = v ?? {};
|
|
2758
|
+
return { x: num(o, "x"), y: num(o, "y"), r: num(o, "r") };
|
|
2759
|
+
}
|
|
2760
|
+
function asSegment(v) {
|
|
2761
|
+
const o = v ?? {};
|
|
2762
|
+
return { x1: num(o, "x1"), y1: num(o, "y1"), x2: num(o, "x2"), y2: num(o, "y2") };
|
|
2763
|
+
}
|
|
2764
|
+
function clamp(n, lo, hi) {
|
|
2765
|
+
return Math.min(Math.max(n, lo), hi);
|
|
2766
|
+
}
|
|
2767
|
+
function evalGeoAabbOverlap(args, evaluate2, ctx) {
|
|
2768
|
+
const a = asRect(evaluate2(args[0], ctx));
|
|
2769
|
+
const b = asRect(evaluate2(args[1], ctx));
|
|
2770
|
+
return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
|
|
2771
|
+
}
|
|
2772
|
+
function evalGeoCircleOverlap(args, evaluate2, ctx) {
|
|
2773
|
+
const a = asCircle(evaluate2(args[0], ctx));
|
|
2774
|
+
const b = asCircle(evaluate2(args[1], ctx));
|
|
2775
|
+
const dx = a.x - b.x, dy = a.y - b.y;
|
|
2776
|
+
return Math.sqrt(dx * dx + dy * dy) <= a.r + b.r;
|
|
2777
|
+
}
|
|
2778
|
+
function evalGeoRectCircleOverlap(args, evaluate2, ctx) {
|
|
2779
|
+
const r = asRect(evaluate2(args[0], ctx));
|
|
2780
|
+
const c = asCircle(evaluate2(args[1], ctx));
|
|
2781
|
+
const cx = clamp(c.x, r.x, r.x + r.w);
|
|
2782
|
+
const cy = clamp(c.y, r.y, r.y + r.h);
|
|
2783
|
+
const dx = c.x - cx, dy = c.y - cy;
|
|
2784
|
+
return dx * dx + dy * dy <= c.r * c.r;
|
|
2785
|
+
}
|
|
2786
|
+
function evalGeoPointInRect(args, evaluate2, ctx) {
|
|
2787
|
+
const p = asVec2(evaluate2(args[0], ctx));
|
|
2788
|
+
const r = asRect(evaluate2(args[1], ctx));
|
|
2789
|
+
return p.x >= r.x && p.x <= r.x + r.w && p.y >= r.y && p.y <= r.y + r.h;
|
|
2790
|
+
}
|
|
2791
|
+
function evalGeoPointInCircle(args, evaluate2, ctx) {
|
|
2792
|
+
const p = asVec2(evaluate2(args[0], ctx));
|
|
2793
|
+
const c = asCircle(evaluate2(args[1], ctx));
|
|
2794
|
+
const dx = p.x - c.x, dy = p.y - c.y;
|
|
2795
|
+
return dx * dx + dy * dy <= c.r * c.r;
|
|
2796
|
+
}
|
|
2797
|
+
function evalGeoReflect(args, evaluate2, ctx) {
|
|
2798
|
+
const v = asVec2(evaluate2(args[0], ctx));
|
|
2799
|
+
const n = asVec2(evaluate2(args[1], ctx));
|
|
2800
|
+
const is3d = v.z !== void 0;
|
|
2801
|
+
const vz = v.z ?? 0, nz = n.z ?? 0;
|
|
2802
|
+
const d = v.x * n.x + v.y * n.y + vz * nz;
|
|
2803
|
+
const rx = v.x - n.x * 2 * d;
|
|
2804
|
+
const ry = v.y - n.y * 2 * d;
|
|
2805
|
+
const rz = vz - nz * 2 * d;
|
|
2806
|
+
return is3d ? { x: rx, y: ry, z: rz } : { x: rx, y: ry };
|
|
2807
|
+
}
|
|
2808
|
+
function evalGeoSegmentIntersect(args, evaluate2, ctx) {
|
|
2809
|
+
const s1 = asSegment(evaluate2(args[0], ctx));
|
|
2810
|
+
const s2 = asSegment(evaluate2(args[1], ctx));
|
|
2811
|
+
const r_x = s1.x2 - s1.x1, r_y = s1.y2 - s1.y1;
|
|
2812
|
+
const s_x = s2.x2 - s2.x1, s_y = s2.y2 - s2.y1;
|
|
2813
|
+
const denom = r_x * s_y - r_y * s_x;
|
|
2814
|
+
if (denom === 0) {
|
|
2815
|
+
return null;
|
|
2816
|
+
}
|
|
2817
|
+
const qpx = s2.x1 - s1.x1, qpy = s2.y1 - s1.y1;
|
|
2818
|
+
const t = (qpx * s_y - qpy * s_x) / denom;
|
|
2819
|
+
const u = (qpx * r_y - qpy * r_x) / denom;
|
|
2820
|
+
if (t < 0 || t > 1 || u < 0 || u > 1) {
|
|
2821
|
+
return null;
|
|
2822
|
+
}
|
|
2823
|
+
return { x: s1.x1 + t * r_x, y: s1.y1 + t * r_y };
|
|
2824
|
+
}
|
|
2825
|
+
|
|
2826
|
+
// std/grid.ts
|
|
2827
|
+
function num2(o, k) {
|
|
2828
|
+
return typeof o[k] === "number" ? o[k] : 0;
|
|
2829
|
+
}
|
|
2830
|
+
function asCell(v) {
|
|
2831
|
+
const o = v ?? {};
|
|
2832
|
+
return { x: num2(o, "x"), y: num2(o, "y") };
|
|
2833
|
+
}
|
|
2834
|
+
function evalGridToWorld(args, evaluate2, ctx) {
|
|
2835
|
+
const cell = asCell(evaluate2(args[0], ctx));
|
|
2836
|
+
const cellSize = evaluate2(args[1], ctx);
|
|
2837
|
+
return { x: (cell.x + 0.5) * cellSize, y: (cell.y + 0.5) * cellSize };
|
|
2838
|
+
}
|
|
2839
|
+
function evalGridFromWorld(args, evaluate2, ctx) {
|
|
2840
|
+
const point = asCell(evaluate2(args[0], ctx));
|
|
2841
|
+
const cellSize = evaluate2(args[1], ctx);
|
|
2842
|
+
return { x: Math.floor(point.x / cellSize), y: Math.floor(point.y / cellSize) };
|
|
2843
|
+
}
|
|
2844
|
+
function evalGridIsoToScreen(args, evaluate2, ctx) {
|
|
2845
|
+
const cell = asCell(evaluate2(args[0], ctx));
|
|
2846
|
+
const tileW = evaluate2(args[1], ctx);
|
|
2847
|
+
const tileH = evaluate2(args[2], ctx);
|
|
2848
|
+
return { x: (cell.x - cell.y) * tileW / 2, y: (cell.x + cell.y) * tileH / 2 };
|
|
2849
|
+
}
|
|
2850
|
+
function evalGridScreenToIso(args, evaluate2, ctx) {
|
|
2851
|
+
const point = asCell(evaluate2(args[0], ctx));
|
|
2852
|
+
const tileW = evaluate2(args[1], ctx);
|
|
2853
|
+
const tileH = evaluate2(args[2], ctx);
|
|
2854
|
+
const halfW = tileW / 2;
|
|
2855
|
+
const halfH = tileH / 2;
|
|
2856
|
+
return {
|
|
2857
|
+
x: (point.x / halfW + point.y / halfH) / 2,
|
|
2858
|
+
y: (point.y / halfH - point.x / halfW) / 2
|
|
2859
|
+
};
|
|
2860
|
+
}
|
|
2861
|
+
function evalGridDistance(args, evaluate2, ctx) {
|
|
2862
|
+
const a = asCell(evaluate2(args[0], ctx));
|
|
2863
|
+
const b = asCell(evaluate2(args[1], ctx));
|
|
2864
|
+
return Math.max(Math.abs(a.x - b.x), Math.abs(a.y - b.y));
|
|
2865
|
+
}
|
|
2866
|
+
function evalGridManhattanDistance(args, evaluate2, ctx) {
|
|
2867
|
+
const a = asCell(evaluate2(args[0], ctx));
|
|
2868
|
+
const b = asCell(evaluate2(args[1], ctx));
|
|
2869
|
+
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
|
|
2870
|
+
}
|
|
2871
|
+
function evalGridNeighbors(args, evaluate2, ctx) {
|
|
2872
|
+
const cell = asCell(evaluate2(args[0], ctx));
|
|
2873
|
+
const diagonal = args.length > 1 ? Boolean(evaluate2(args[1], ctx)) : false;
|
|
2874
|
+
const result = [
|
|
2875
|
+
{ x: cell.x, y: cell.y - 1 },
|
|
2876
|
+
// N
|
|
2877
|
+
{ x: cell.x + 1, y: cell.y },
|
|
2878
|
+
// E
|
|
2879
|
+
{ x: cell.x, y: cell.y + 1 },
|
|
2880
|
+
// S
|
|
2881
|
+
{ x: cell.x - 1, y: cell.y }
|
|
2882
|
+
// W
|
|
2883
|
+
];
|
|
2884
|
+
if (diagonal) {
|
|
2885
|
+
result.push(
|
|
2886
|
+
{ x: cell.x + 1, y: cell.y - 1 },
|
|
2887
|
+
// NE
|
|
2888
|
+
{ x: cell.x + 1, y: cell.y + 1 },
|
|
2889
|
+
// SE
|
|
2890
|
+
{ x: cell.x - 1, y: cell.y + 1 },
|
|
2891
|
+
// SW
|
|
2892
|
+
{ x: cell.x - 1, y: cell.y - 1 }
|
|
2893
|
+
// NW
|
|
2894
|
+
);
|
|
2895
|
+
}
|
|
2896
|
+
return result;
|
|
2897
|
+
}
|
|
2898
|
+
function evalGridCellsInRadius(args, evaluate2, ctx) {
|
|
2899
|
+
const cell = asCell(evaluate2(args[0], ctx));
|
|
2900
|
+
const r = evaluate2(args[1], ctx);
|
|
2901
|
+
const rf = Math.floor(r);
|
|
2902
|
+
const r2 = r * r;
|
|
2903
|
+
const result = [];
|
|
2904
|
+
for (let dy = -rf; dy <= rf; dy++) {
|
|
2905
|
+
for (let dx = -rf; dx <= rf; dx++) {
|
|
2906
|
+
if (dx * dx + dy * dy <= r2) {
|
|
2907
|
+
result.push({ x: cell.x + dx, y: cell.y + dy });
|
|
2908
|
+
}
|
|
2909
|
+
}
|
|
2910
|
+
}
|
|
2911
|
+
return result;
|
|
2912
|
+
}
|
|
2913
|
+
function evalGridLine(args, evaluate2, ctx) {
|
|
2914
|
+
const a = asCell(evaluate2(args[0], ctx));
|
|
2915
|
+
const b = asCell(evaluate2(args[1], ctx));
|
|
2916
|
+
let x0 = a.x, y0 = a.y;
|
|
2917
|
+
const x1 = b.x, y1 = b.y;
|
|
2918
|
+
const dx = Math.abs(x1 - x0);
|
|
2919
|
+
const dy = Math.abs(y1 - y0);
|
|
2920
|
+
const sx = x0 < x1 ? 1 : -1;
|
|
2921
|
+
const sy = y0 < y1 ? 1 : -1;
|
|
2922
|
+
let err = dx - dy;
|
|
2923
|
+
const result = [];
|
|
2924
|
+
for (; ; ) {
|
|
2925
|
+
result.push({ x: x0, y: y0 });
|
|
2926
|
+
if (x0 === x1 && y0 === y1) {
|
|
2927
|
+
break;
|
|
2928
|
+
}
|
|
2929
|
+
const e2 = 2 * err;
|
|
2930
|
+
if (e2 > -dy) {
|
|
2931
|
+
err -= dy;
|
|
2932
|
+
x0 += sx;
|
|
2933
|
+
}
|
|
2934
|
+
if (e2 < dx) {
|
|
2935
|
+
err += dx;
|
|
2936
|
+
y0 += sy;
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
return result;
|
|
2940
|
+
}
|
|
2941
|
+
function evalGridInBounds(args, evaluate2, ctx) {
|
|
2942
|
+
const cell = asCell(evaluate2(args[0], ctx));
|
|
2943
|
+
const w = evaluate2(args[1], ctx);
|
|
2944
|
+
const h = evaluate2(args[2], ctx);
|
|
2945
|
+
return cell.x >= 0 && cell.x < w && cell.y >= 0 && cell.y < h;
|
|
2946
|
+
}
|
|
2947
|
+
|
|
2948
|
+
// std/anim.ts
|
|
2949
|
+
function evalAnimFrameAt(args, evaluate2, ctx) {
|
|
2950
|
+
const elapsed = evaluate2(args[0], ctx);
|
|
2951
|
+
const frameDur = evaluate2(args[1], ctx);
|
|
2952
|
+
const frameCount = evaluate2(args[2], ctx);
|
|
2953
|
+
const loop = args.length > 3 ? Boolean(evaluate2(args[3], ctx)) : true;
|
|
2954
|
+
if (frameCount <= 0) {
|
|
2955
|
+
return 0;
|
|
2956
|
+
}
|
|
2957
|
+
const i = Math.floor(elapsed / frameDur);
|
|
2958
|
+
if (loop) {
|
|
2959
|
+
return (i % frameCount + frameCount) % frameCount;
|
|
2960
|
+
}
|
|
2961
|
+
return Math.min(i, frameCount - 1);
|
|
2962
|
+
}
|
|
2963
|
+
function evalAnimSheetRect(args, evaluate2, ctx) {
|
|
2964
|
+
const frameIndex = evaluate2(args[0], ctx);
|
|
2965
|
+
const cols = evaluate2(args[1], ctx);
|
|
2966
|
+
const frameW = evaluate2(args[2], ctx);
|
|
2967
|
+
const frameH = evaluate2(args[3], ctx);
|
|
2968
|
+
return {
|
|
2969
|
+
sx: frameIndex % cols * frameW,
|
|
2970
|
+
sy: Math.floor(frameIndex / cols) * frameH,
|
|
2971
|
+
sw: frameW,
|
|
2972
|
+
sh: frameH
|
|
2973
|
+
};
|
|
2974
|
+
}
|
|
2975
|
+
function evalAnimDirectionFromDelta(args, evaluate2, ctx) {
|
|
2976
|
+
const dx = evaluate2(args[0], ctx);
|
|
2977
|
+
const dy = evaluate2(args[1], ctx);
|
|
2978
|
+
const ways = args.length > 2 ? evaluate2(args[2], ctx) : 4;
|
|
2979
|
+
const angle = Math.atan2(dy, dx);
|
|
2980
|
+
if (ways === 8) {
|
|
2981
|
+
const sector2 = Math.round(angle / (Math.PI / 4));
|
|
2982
|
+
const idx2 = (sector2 % 8 + 8) % 8;
|
|
2983
|
+
const dirs8 = [
|
|
2984
|
+
"right",
|
|
2985
|
+
"down-right",
|
|
2986
|
+
"down",
|
|
2987
|
+
"down-left",
|
|
2988
|
+
"left",
|
|
2989
|
+
"up-left",
|
|
2990
|
+
"up",
|
|
2991
|
+
"up-right"
|
|
2992
|
+
];
|
|
2993
|
+
return dirs8[idx2];
|
|
2994
|
+
}
|
|
2995
|
+
const sector = Math.round(angle / (Math.PI / 2));
|
|
2996
|
+
const idx = (sector % 4 + 4) % 4;
|
|
2997
|
+
const dirs4 = ["right", "down", "left", "up"];
|
|
2998
|
+
return dirs4[idx];
|
|
2999
|
+
}
|
|
3000
|
+
|
|
3001
|
+
// std/ease.ts
|
|
3002
|
+
function clamp01(t) {
|
|
3003
|
+
return Math.min(Math.max(t, 0), 1);
|
|
3004
|
+
}
|
|
3005
|
+
function applyCurve(curve, tRaw) {
|
|
3006
|
+
const t = clamp01(tRaw);
|
|
3007
|
+
switch (curve) {
|
|
3008
|
+
case "in-quad":
|
|
3009
|
+
return t * t;
|
|
3010
|
+
case "out-quad":
|
|
3011
|
+
return t * (2 - t);
|
|
3012
|
+
case "in-out-quad":
|
|
3013
|
+
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
|
|
3014
|
+
case "in-cubic":
|
|
3015
|
+
return t * t * t;
|
|
3016
|
+
case "out-cubic": {
|
|
3017
|
+
const u = t - 1;
|
|
3018
|
+
return u * u * u + 1;
|
|
3019
|
+
}
|
|
3020
|
+
case "in-out-cubic":
|
|
3021
|
+
return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
|
|
3022
|
+
case "elastic-out": {
|
|
3023
|
+
if (t === 0) {
|
|
3024
|
+
return 0;
|
|
3025
|
+
}
|
|
3026
|
+
if (t === 1) {
|
|
3027
|
+
return 1;
|
|
3028
|
+
}
|
|
3029
|
+
const c4 = 2 * Math.PI / 3;
|
|
3030
|
+
return Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * c4) + 1;
|
|
3031
|
+
}
|
|
3032
|
+
case "bounce-out": {
|
|
3033
|
+
const n1 = 7.5625;
|
|
3034
|
+
const d1 = 2.75;
|
|
3035
|
+
let u = t;
|
|
3036
|
+
if (u < 1 / d1) {
|
|
3037
|
+
return n1 * u * u;
|
|
3038
|
+
} else if (u < 2 / d1) {
|
|
3039
|
+
u -= 1.5 / d1;
|
|
3040
|
+
return n1 * u * u + 0.75;
|
|
3041
|
+
} else if (u < 2.5 / d1) {
|
|
3042
|
+
u -= 2.25 / d1;
|
|
3043
|
+
return n1 * u * u + 0.9375;
|
|
3044
|
+
} else {
|
|
3045
|
+
u -= 2.625 / d1;
|
|
3046
|
+
return n1 * u * u + 0.984375;
|
|
3047
|
+
}
|
|
3048
|
+
}
|
|
3049
|
+
case "linear":
|
|
3050
|
+
default:
|
|
3051
|
+
return t;
|
|
3052
|
+
}
|
|
3053
|
+
}
|
|
3054
|
+
function evalEaseApply(args, evaluate2, ctx) {
|
|
3055
|
+
const curve = evaluate2(args[0], ctx);
|
|
3056
|
+
const t = evaluate2(args[1], ctx);
|
|
3057
|
+
return applyCurve(curve, t);
|
|
3058
|
+
}
|
|
3059
|
+
function evalEaseSmoothstep(args, evaluate2, ctx) {
|
|
3060
|
+
const edge0 = evaluate2(args[0], ctx);
|
|
3061
|
+
const edge1 = evaluate2(args[1], ctx);
|
|
3062
|
+
const x = evaluate2(args[2], ctx);
|
|
3063
|
+
if (edge0 === edge1) {
|
|
3064
|
+
return 0;
|
|
3065
|
+
}
|
|
3066
|
+
const t = clamp01((x - edge0) / (edge1 - edge0));
|
|
3067
|
+
return t * t * (3 - 2 * t);
|
|
3068
|
+
}
|
|
3069
|
+
|
|
2578
3070
|
// SExpressionEvaluator.ts
|
|
2579
3071
|
var jitCache = /* @__PURE__ */ new Map();
|
|
2580
3072
|
var MAX_JIT_CACHE_SIZE = 1e3;
|
|
@@ -2841,6 +3333,111 @@ var SExpressionEvaluator = class {
|
|
|
2841
3333
|
return evalMathRandomInt(args, evaluate2, ctx);
|
|
2842
3334
|
case "math/default":
|
|
2843
3335
|
return evalMathDefault(args, evaluate2, ctx);
|
|
3336
|
+
case "math/sin":
|
|
3337
|
+
return evalMathSin(args, evaluate2, ctx);
|
|
3338
|
+
case "math/cos":
|
|
3339
|
+
return evalMathCos(args, evaluate2, ctx);
|
|
3340
|
+
case "math/tan":
|
|
3341
|
+
return evalMathTan(args, evaluate2, ctx);
|
|
3342
|
+
case "math/atan2":
|
|
3343
|
+
return evalMathAtan2(args, evaluate2, ctx);
|
|
3344
|
+
case "math/hypot":
|
|
3345
|
+
return evalMathHypot(args, evaluate2, ctx);
|
|
3346
|
+
case "math/deg-rad":
|
|
3347
|
+
return evalMathDegRad(args, evaluate2, ctx);
|
|
3348
|
+
case "math/rad-deg":
|
|
3349
|
+
return evalMathRadDeg(args, evaluate2, ctx);
|
|
3350
|
+
case "math/wrap":
|
|
3351
|
+
return evalMathWrap(args, evaluate2, ctx);
|
|
3352
|
+
case "math/approach":
|
|
3353
|
+
return evalMathApproach(args, evaluate2, ctx);
|
|
3354
|
+
// ===============================
|
|
3355
|
+
// Standard Library: vec/*
|
|
3356
|
+
// ===============================
|
|
3357
|
+
case "vec/add":
|
|
3358
|
+
return evalVecAdd(args, evaluate2, ctx);
|
|
3359
|
+
case "vec/sub":
|
|
3360
|
+
return evalVecSub(args, evaluate2, ctx);
|
|
3361
|
+
case "vec/scale":
|
|
3362
|
+
return evalVecScale(args, evaluate2, ctx);
|
|
3363
|
+
case "vec/dot":
|
|
3364
|
+
return evalVecDot(args, evaluate2, ctx);
|
|
3365
|
+
case "vec/cross":
|
|
3366
|
+
return evalVecCross(args, evaluate2, ctx);
|
|
3367
|
+
case "vec/length":
|
|
3368
|
+
return evalVecLength(args, evaluate2, ctx);
|
|
3369
|
+
case "vec/length-sq":
|
|
3370
|
+
return evalVecLengthSq(args, evaluate2, ctx);
|
|
3371
|
+
case "vec/normalize":
|
|
3372
|
+
return evalVecNormalize(args, evaluate2, ctx);
|
|
3373
|
+
case "vec/distance":
|
|
3374
|
+
return evalVecDistance(args, evaluate2, ctx);
|
|
3375
|
+
case "vec/distance-sq":
|
|
3376
|
+
return evalVecDistanceSq(args, evaluate2, ctx);
|
|
3377
|
+
case "vec/lerp":
|
|
3378
|
+
return evalVecLerp(args, evaluate2, ctx);
|
|
3379
|
+
case "vec/angle":
|
|
3380
|
+
return evalVecAngle(args, evaluate2, ctx);
|
|
3381
|
+
case "vec/rotate":
|
|
3382
|
+
return evalVecRotate(args, evaluate2, ctx);
|
|
3383
|
+
case "vec/clamp-length":
|
|
3384
|
+
return evalVecClampLength(args, evaluate2, ctx);
|
|
3385
|
+
// ===============================
|
|
3386
|
+
// Standard Library: geo/*
|
|
3387
|
+
// ===============================
|
|
3388
|
+
case "geo/aabb-overlap":
|
|
3389
|
+
return evalGeoAabbOverlap(args, evaluate2, ctx);
|
|
3390
|
+
case "geo/circle-overlap":
|
|
3391
|
+
return evalGeoCircleOverlap(args, evaluate2, ctx);
|
|
3392
|
+
case "geo/rect-circle-overlap":
|
|
3393
|
+
return evalGeoRectCircleOverlap(args, evaluate2, ctx);
|
|
3394
|
+
case "geo/point-in-rect":
|
|
3395
|
+
return evalGeoPointInRect(args, evaluate2, ctx);
|
|
3396
|
+
case "geo/point-in-circle":
|
|
3397
|
+
return evalGeoPointInCircle(args, evaluate2, ctx);
|
|
3398
|
+
case "geo/reflect":
|
|
3399
|
+
return evalGeoReflect(args, evaluate2, ctx);
|
|
3400
|
+
case "geo/segment-intersect":
|
|
3401
|
+
return evalGeoSegmentIntersect(args, evaluate2, ctx);
|
|
3402
|
+
// ===============================
|
|
3403
|
+
// Standard Library: grid/*
|
|
3404
|
+
// ===============================
|
|
3405
|
+
case "grid/to-world":
|
|
3406
|
+
return evalGridToWorld(args, evaluate2, ctx);
|
|
3407
|
+
case "grid/from-world":
|
|
3408
|
+
return evalGridFromWorld(args, evaluate2, ctx);
|
|
3409
|
+
case "grid/iso-to-screen":
|
|
3410
|
+
return evalGridIsoToScreen(args, evaluate2, ctx);
|
|
3411
|
+
case "grid/screen-to-iso":
|
|
3412
|
+
return evalGridScreenToIso(args, evaluate2, ctx);
|
|
3413
|
+
case "grid/distance":
|
|
3414
|
+
return evalGridDistance(args, evaluate2, ctx);
|
|
3415
|
+
case "grid/manhattan-distance":
|
|
3416
|
+
return evalGridManhattanDistance(args, evaluate2, ctx);
|
|
3417
|
+
case "grid/neighbors":
|
|
3418
|
+
return evalGridNeighbors(args, evaluate2, ctx);
|
|
3419
|
+
case "grid/cells-in-radius":
|
|
3420
|
+
return evalGridCellsInRadius(args, evaluate2, ctx);
|
|
3421
|
+
case "grid/line":
|
|
3422
|
+
return evalGridLine(args, evaluate2, ctx);
|
|
3423
|
+
case "grid/in-bounds":
|
|
3424
|
+
return evalGridInBounds(args, evaluate2, ctx);
|
|
3425
|
+
// ===============================
|
|
3426
|
+
// Standard Library: anim/*
|
|
3427
|
+
// ===============================
|
|
3428
|
+
case "anim/frame-at":
|
|
3429
|
+
return evalAnimFrameAt(args, evaluate2, ctx);
|
|
3430
|
+
case "anim/sheet-rect":
|
|
3431
|
+
return evalAnimSheetRect(args, evaluate2, ctx);
|
|
3432
|
+
case "anim/direction-from-delta":
|
|
3433
|
+
return evalAnimDirectionFromDelta(args, evaluate2, ctx);
|
|
3434
|
+
// ===============================
|
|
3435
|
+
// Standard Library: ease/*
|
|
3436
|
+
// ===============================
|
|
3437
|
+
case "ease/apply":
|
|
3438
|
+
return evalEaseApply(args, evaluate2, ctx);
|
|
3439
|
+
case "ease/smoothstep":
|
|
3440
|
+
return evalEaseSmoothstep(args, evaluate2, ctx);
|
|
2844
3441
|
// ===============================
|
|
2845
3442
|
// Standard Library: str/*
|
|
2846
3443
|
// ===============================
|