feat: one continuous degree-7 ballistic arc for flights (t1138)
Replace the multi-segment rise/quarter-turn/straight-cruise/dive chain with a single degree-7 Bézier (C^∞). Horizontal travel sits on a high rounded apex so the curve never cuts the field interior; ends stay vertical (up at y=13, down the land_col at fallSpeed). Parameterise by arc length with gentle Hermite easing, including enemy→enemy parabolas behind the field.
@@ -551,6 +551,155 @@ Online.cubicBezierDeriv = function (p0, p1, p2, p3, t)
551
551
};
552
552
};
553
553
554
Online.cubicBezierDeriv2 = function (p0, p1, p2, p3, t)
555
{
556
if (t < 0)
557
{ t = 0; }
558
if (t > 1)
559
{ t = 1; }
560
var u = 1 - t;
561
// C''(t) = 6(1−t)(P2−2P1+P0) + 6t(P3−2P2+P1)
562
return {
563
x: 6 * u * (p2.x - 2 * p1.x + p0.x) + 6 * t * (p3.x - 2 * p2.x + p1.x),
564
y: 6 * u * (p2.y - 2 * p1.y + p0.y) + 6 * t * (p3.y - 2 * p2.y + p1.y)
565
};
566
};
567
568
/** Degree-5 Bézier. One polynomial ⇒ C^∞ on (0, 1) (t1138). */
569
Online.quinticBezier = function (pts, t)
570
{
571
if (t < 0)
572
{ t = 0; }
573
if (t > 1)
574
{ t = 1; }
575
var u = 1 - t;
576
var u2 = u * u, u3 = u2 * u, u4 = u3 * u, u5 = u4 * u;
577
var t2 = t * t, t3 = t2 * t, t4 = t3 * t, t5 = t4 * t;
578
var b0 = u5, b1 = 5 * u4 * t, b2 = 10 * u3 * t2;
579
var b3 = 10 * u2 * t3, b4 = 5 * u * t4, b5 = t5;
580
return {
581
x: b0 * pts[0].x + b1 * pts[1].x + b2 * pts[2].x
582
+ b3 * pts[3].x + b4 * pts[4].x + b5 * pts[5].x,
583
y: b0 * pts[0].y + b1 * pts[1].y + b2 * pts[2].y
584
+ b3 * pts[3].y + b4 * pts[4].y + b5 * pts[5].y
585
};
586
};
587
588
Online.quinticBezierDeriv = function (pts, t)
589
{
590
if (t < 0)
591
{ t = 0; }
592
if (t > 1)
593
{ t = 1; }
594
var d = [];
595
for (var i = 0; i < 5; i++)
596
{ d.push({ x: pts[i + 1].x - pts[i].x, y: pts[i + 1].y - pts[i].y }); }
597
var u = 1 - t;
598
var u2 = u * u, u3 = u2 * u, u4 = u3 * u;
599
var t2 = t * t, t3 = t2 * t, t4 = t3 * t;
600
var b0 = u4, b1 = 4 * u3 * t, b2 = 6 * u2 * t2, b3 = 4 * u * t3, b4 = t4;
601
return {
602
x: 5 * (b0 * d[0].x + b1 * d[1].x + b2 * d[2].x + b3 * d[3].x + b4 * d[4].x),
603
y: 5 * (b0 * d[0].y + b1 * d[1].y + b2 * d[2].y + b3 * d[3].y + b4 * d[4].y)
604
};
605
};
606
607
Online.quinticBezierDeriv2 = function (pts, t)
608
{
609
if (t < 0)
610
{ t = 0; }
611
if (t > 1)
612
{ t = 1; }
613
var d2 = [];
614
for (var i = 0; i < 4; i++)
615
{
616
d2.push({
617
x: pts[i + 2].x - 2 * pts[i + 1].x + pts[i].x,
618
y: pts[i + 2].y - 2 * pts[i + 1].y + pts[i].y
619
});
620
}
621
var u = 1 - t;
622
var u2 = u * u, u3 = u2 * u;
623
var t2 = t * t, t3 = t2 * t;
624
var b0 = u3, b1 = 3 * u2 * t, b2 = 3 * u * t2, b3 = t3;
625
return {
626
x: 20 * (b0 * d2[0].x + b1 * d2[1].x + b2 * d2[2].x + b3 * d2[3].x),
627
y: 20 * (b0 * d2[0].y + b1 * d2[1].y + b2 * d2[2].y + b3 * d2[3].y)
628
};
629
};
630
631
/** Signed curvature κ = (x'y'' − y'x'') / |r'|³ (independent of parametrisation). */
632
Online.curvature = function (d1, d2)
633
{
634
var sp = d1.x * d1.x + d1.y * d1.y;
635
var den = Math.pow(sp, 1.5);
636
if (den < 1e-12)
637
{ return 0; }
638
return (d1.x * d2.y - d1.y * d2.x) / den;
639
};
640
641
/**
642
* de Casteljau evaluation of a Bézier of any degree, plus first
643
* and second derivatives (t1138). A single polynomial is C^∞ on (0, 1).
644
*/
645
Online.bezierPoint = function (pts, t)
646
{
647
if (!pts || !pts.length)
648
{ return { x: 0, y: 0 }; }
649
if (t < 0)
650
{ t = 0; }
651
if (t > 1)
652
{ t = 1; }
653
var tmp = [];
654
var i, k, u = 1 - t;
655
for (i = 0; i < pts.length; i++)
656
{ tmp.push({ x: pts[i].x, y: pts[i].y }); }
657
for (k = 1; k < pts.length; k++)
658
{
659
for (i = 0; i < pts.length - k; i++)
660
{
661
tmp[i] = {
662
x: u * tmp[i].x + t * tmp[i + 1].x,
663
y: u * tmp[i].y + t * tmp[i + 1].y
664
};
665
}
666
}
667
return tmp[0];
668
};
669
670
Online.bezierDeriv = function (pts, t)
671
{
672
var n = pts ? pts.length - 1 : 0;
673
if (n < 1)
674
{ return { x: 0, y: 0 }; }
675
var d = [];
676
for (var i = 0; i < n; i++)
677
{
678
d.push({
679
x: n * (pts[i + 1].x - pts[i].x),
680
y: n * (pts[i + 1].y - pts[i].y)
681
});
682
}
683
return this.bezierPoint(d, t);
684
};
685
686
Online.bezierDeriv2 = function (pts, t)
687
{
688
var n = pts ? pts.length - 1 : 0;
689
if (n < 2)
690
{ return { x: 0, y: 0 }; }
691
var s = n * (n - 1);
692
var d2 = [];
693
for (var i = 0; i < n - 1; i++)
694
{
695
d2.push({
696
x: s * (pts[i + 2].x - 2 * pts[i + 1].x + pts[i].x),
697
y: s * (pts[i + 2].y - 2 * pts[i + 1].y + pts[i].y)
698
});
699
}
700
return this.bezierPoint(d2, t);
701
};
702
554
703
/**
555
704
* Hermite parameter with p(0)=0, p(1)=1, p'(0)=v0, p'(1)=v1.
556
705
* v0=v1=1 is linear (constant speed in t, no endpoint halt).
@@ -571,29 +720,18 @@ Online.smoothParam = function (t, v0, v1)
571
720
};
572
721
573
722
/**
574
* A simple up-and-over cubic, used when there is no field to route
575
* around (enemy→enemy) or both ends already sit above the apex.
576
*/
577
Online._singleCubic = function (start, end, apexY, opts)
578
{
579
opts = opts || {};
580
var rise = Math.max(40, Math.abs(start.y - apexY) * 0.9);
581
if (opts.startSpeedPx)
582
{ rise = Math.max(rise, opts.startSpeedPx); }
583
var fall = Math.max(40, Math.abs(end.y - apexY) * 0.9);
584
if (opts.endSpeedPx)
585
{ fall = Math.max(40, opts.endSpeedPx); }
586
var p1 = { x: start.x, y: Math.min(start.y - rise, apexY) };
587
var p2 = { x: end.x, y: Math.min(end.y - fall, apexY) };
588
return { p0: start, p1: p1, p2: p2, p3: end, apexY: apexY };
589
};
590
591
/**
592
* Smooth C¹ path: rise up (launch column or outside), cruise above
593
* the field, dive into the landing column (t1134 / t1135). Joints
594
* are quarter-turn cubics with matching tangents, so there are no
595
* corners and no speed jerks. The curve may use the launch and
596
* landing corridors but does not cut across the field interior.
723
* One continuous degree-7 Bézier: a single big, round, ballistic
724
* arch (t1138). Vertical at the exit (heading up) and at the
725
* landing (heading down). Horizontal travel sits on the apex,
726
* above the field — a cluster of high control points on each
727
* side keeps the hull out of the interior, with no straight
728
* cruise and no quarter-turn kinks. C^∞ on (0, 1).
729
*
730
* Incoming (low mini → high field): the first mid controls stay
731
* on the outside so the rise clears the top before crossing.
732
* Outgoing (high exit → low mini): mid controls sit above the
733
* target so the dive happens outside. Enemy→enemy (no field)
734
* is a symmetric parabola behind the field.
597
735
*/
598
736
Online.makeAroundCurve = function (start, end, field, opts)
599
737
{
@@ -602,191 +740,289 @@ Online.makeAroundCurve = function (start, end, field, opts)
602
740
{ start = { x: 0, y: 0 }; }
603
741
if (!end)
604
742
{ end = { x: 0, y: 0 }; }
605
var clearance = opts.clearance || 80;
606
if (!field)
743
var clearance = opts.clearance || 150;
744
if (typeof MiniLayout !== 'undefined' && MiniLayout.ARC_TOP_PAD)
745
{ clearance = Math.max(clearance, MiniLayout.ARC_TOP_PAD); }
746
if (clearance < 150)
747
{ clearance = 150; }
748
749
var apexY;
750
var left, right, top;
751
if (field)
607
752
{
608
var looseApex = Math.min(start.y, end.y) - Math.max(40, clearance * 0.5);
609
if (looseApex < 8)
610
{ looseApex = Math.min(start.y, end.y) - 24; }
611
return this._singleCubic(start, end, looseApex, opts);
753
top = field.top;
754
left = field.left;
755
right = field.right;
756
apexY = top - clearance;
757
if (apexY < 8)
758
{ apexY = Math.max(8, top - Math.max(28, clearance * 0.5)); }
759
}
760
else
761
{
762
apexY = Math.min(start.y, end.y) - Math.max(56, clearance * 0.6);
763
if (apexY < 8)
764
{ apexY = Math.min(start.y, end.y) - 40; }
765
left = right = top = null;
612
766
}
613
var top = field.top;
614
var left = field.left;
615
var right = field.right;
616
var apexY = top - clearance;
617
if (apexY < 8)
618
{ apexY = Math.max(8, top - Math.max(24, clearance * 0.5)); }
619
620
if (start.y <= apexY + 8 && end.y <= apexY + 8)
621
{ return this._singleCubic(start, end, apexY, opts); }
622
767
623
var pad = 14;
624
var dir = end.x >= start.x ? 1 : -1;
625
var span = Math.abs(end.x - start.x);
626
var horiz = Math.max(28, Math.min(80, span * 0.15 + 8));
627
768
function outsideX (x)
628
769
{
770
if (!field)
771
{ return x; }
772
var pad = 40;
629
773
if (x < left + 8)
630
774
{ return Math.min(x, left - pad); }
631
775
if (x > right - 8)
632
776
{ return Math.max(x, right + pad); }
633
777
return x;
634
778
}
635
var riseX = outsideX(start.x);
636
var diveX = outsideX(end.x);
637
var segments = [];
638
779
639
// Rise ends already turned toward the cruise, at apex.
640
var riseEnd = { x: riseX + dir * horiz, y: apexY };
641
if (start.y > apexY + 4)
780
var startLow = field && start.y > top + 8;
781
var endLow = field && end.y > top + 8;
782
var startInside = field && start.x > left + 8 && start.x < right - 8;
783
var endInside = field && end.x > left + 8 && end.x < right - 8;
784
// Incoming: rise on the outside first. Outgoing: cross at
785
// the apex, then dive outside. Otherwise a symmetric arch.
786
var kind;
787
if (startLow && !endLow)
788
{ kind = 'in'; }
789
else if (!startLow && endLow)
790
{ kind = 'out'; }
791
else if (startLow && endLow)
792
{ kind = startInside ? 'out' : (endInside ? 'in' : 'sym'); }
793
else
794
{ kind = 'sym'; }
795
796
var sx = outsideX(start.x);
797
var ex = outsideX(end.x);
798
var rise = Math.max(80, Math.abs(start.y - apexY) * 0.35);
799
var P0 = { x: start.x, y: start.y };
800
var P1 = { x: start.x, y: start.y - rise };
801
var P2, P3, P4, P5;
802
var P6 = { x: end.x, y: apexY };
803
var P7 = { x: end.x, y: end.y };
804
if (kind === 'in')
805
{
806
P2 = { x: sx, y: apexY };
807
P3 = { x: sx, y: apexY };
808
P4 = { x: sx + (ex - sx) * 0.5, y: apexY };
809
P5 = { x: sx + (ex - sx) * 0.85, y: apexY };
810
}
811
else if (kind === 'out')
642
812
{
643
var p1 = { x: start.x, y: apexY };
644
var p2 = { x: riseX, y: apexY };
645
segments.push({ p0: start, p1: p1, p2: p2, p3: riseEnd });
813
P2 = { x: start.x, y: apexY };
814
P3 = { x: sx + (ex - sx) * 0.55, y: apexY };
815
P4 = { x: ex, y: apexY };
816
P5 = { x: end.x, y: apexY };
646
817
}
647
818
else
648
{ riseEnd = { x: start.x, y: Math.min(start.y, apexY) }; }
649
650
// Cruise stays on the apex line (convex hull is above the field).
651
var diveStart = { x: diveX - dir * horiz, y: apexY };
652
if (Math.abs(riseEnd.x - diveStart.x) > 4 || Math.abs(riseEnd.y - diveStart.y) > 4)
653
819
{
654
// Keep the cruise direction consistent; if the two joints
655
// would cross, just connect them directly.
656
var cdir = diveStart.x >= riseEnd.x ? 1 : -1;
657
var c1 = { x: riseEnd.x + cdir * horiz, y: riseEnd.y };
658
if (c1.y > apexY)
659
{ c1.y = apexY; }
660
var c2 = { x: diveStart.x - cdir * horiz, y: diveStart.y };
661
segments.push({ p0: { x: riseEnd.x, y: riseEnd.y }, p1: c1, p2: c2, p3: diveStart });
820
P2 = { x: sx, y: apexY };
821
P3 = { x: sx + (ex - sx) * 0.35, y: apexY };
822
P4 = { x: sx + (ex - sx) * 0.65, y: apexY };
823
P5 = { x: ex, y: apexY };
662
824
}
825
var pts = [P0, P1, P2, P3, P4, P5, P6, P7];
826
827
var curve = {
828
degree: 7,
829
pts: pts,
830
p0: P0,
831
p3: P7,
832
apexY: apexY,
833
kind: kind,
834
arcLength: true
835
};
836
this._buildArcLut(curve);
837
return curve;
838
};
663
839
664
if (end.y > apexY + 4 || Math.abs(end.x - (diveStart.x + dir * horiz)) > 2)
840
Online._evalPoly = function (curve, t)
841
{
842
if (curve && curve.pts && curve.pts.length >= 2)
843
{ return this.bezierPoint(curve.pts, t); }
844
if (curve && curve.p1)
845
{ return this.cubicBezier(curve.p0, curve.p1, curve.p2, curve.p3, t); }
846
if (curve && curve.segments)
665
847
{
666
var u0 = (Math.abs(riseEnd.x - diveStart.x) > 4 || segments.length === 0)
667
? diveStart
668
: riseEnd;
669
if (segments.length && segments[segments.length - 1].p3.x === u0.x
670
&& segments[segments.length - 1].p3.y === u0.y)
671
{ /* already joined */ }
672
else if (segments.length === 0)
673
{ u0 = riseEnd; }
674
var u1 = { x: u0.x + dir * horiz, y: u0.y };
675
var fall = Math.max(24, Math.abs(end.y - apexY) * 0.45);
676
if (opts.endSpeedPx)
677
{
678
// |P3-P2| so arrival ≈ fallSpeed after equal-param scaling.
679
// velocity = 3*|P3-P2|*n*v1/duration; endSpeedPx is
680
// (cells*px*duration)/3, so |P3-P2| = endSpeedPx/(n*v1).
681
var nEst = Math.max(1, segments.length + 1);
682
fall = Math.max(20, opts.endSpeedPx / (nEst * 0.85));
683
}
684
var u2 = { x: end.x, y: end.y - fall };
685
// P2 may sit between the apex and the end (slower arrival);
686
// only clamp if it would pass the end or fly far past the apex.
687
if (u2.y < apexY - 240)
688
{ u2.y = apexY - 240; }
689
if (u2.y > end.y - 8)
690
{ u2.y = end.y - 8; }
691
segments.push({ p0: u0, p1: u1, p2: u2, p3: end });
848
var n = curve.segments.length;
849
var at = (t < 0 ? 0 : (t > 1 ? 1 : t)) * n;
850
var i = Math.floor(at);
851
if (i >= n)
852
{ i = n - 1; }
853
var s = curve.segments[i];
854
return this.cubicBezier(s.p0, s.p1, s.p2, s.p3, at - i);
692
855
}
856
return { x: 0, y: 0 };
857
};
693
858
694
if (segments.length === 0)
695
{ return this._singleCubic(start, end, apexY, opts); }
696
if (segments.length === 1 && !segments[0].p0)
697
{ return this._singleCubic(start, end, apexY, opts); }
859
Online._evalPolyDeriv = function (curve, t)
860
{
861
if (curve && curve.pts && curve.pts.length >= 2)
862
{ return this.bezierDeriv(curve.pts, t); }
863
if (curve && curve.p1)
864
{ return this.cubicBezierDeriv(curve.p0, curve.p1, curve.p2, curve.p3, t); }
865
return { x: 0, y: 0 };
866
};
698
867
699
return { segments: segments, apexY: apexY, p0: start, p3: end };
868
Online._evalPolyDeriv2 = function (curve, t)
869
{
870
if (curve && curve.pts && curve.pts.length >= 3)
871
{ return this.bezierDeriv2(curve.pts, t); }
872
if (curve && curve.p1)
873
{ return this.cubicBezierDeriv2(curve.p0, curve.p1, curve.p2, curve.p3, t); }
874
return { x: 0, y: 0 };
700
875
};
701
876
702
Online._measureCurve = function (curve)
877
/** Cumulative-length LUT so u is a fraction of arc length (t1138). */
878
Online._buildArcLut = function (curve, steps)
703
879
{
704
if (!curve.segments)
705
{ return curve; }
706
var lens = [];
707
var total = 0;
708
for (var i = 0; i < curve.segments.length; i++)
880
steps = steps || 48;
881
var acc = [0];
882
var prev = this._evalPoly(curve, 0);
883
for (var i = 1; i <= steps; i++)
884
{
885
var p = this._evalPoly(curve, i / steps);
886
acc.push(acc[i - 1] + Math.sqrt((p.x - prev.x) * (p.x - prev.x)
887
+ (p.y - prev.y) * (p.y - prev.y)));
888
prev = p;
889
}
890
curve.lutS = acc;
891
curve.lutN = steps;
892
curve.totalLen = Math.max(acc[steps], 1);
893
return curve;
894
};
895
896
/** Polynomial t whose arc length fraction is u. */
897
Online._arcT = function (curve, u)
898
{
899
if (u <= 0)
900
{ return 0; }
901
if (u >= 1)
902
{ return 1; }
903
if (!curve.lutS)
904
{ this._buildArcLut(curve); }
905
var s = u * curve.totalLen;
906
var acc = curve.lutS;
907
var n = curve.lutN;
908
var lo = 0;
909
var hi = n;
910
while (lo < hi)
709
911
{
710
var L = this._segLen(curve.segments[i]);
711
lens.push(L);
712
total += L;
912
var mid = (lo + hi) >> 1;
913
if (acc[mid] < s)
914
{ lo = mid + 1; }
915
else
916
{ hi = mid; }
713
917
}
714
curve.lens = lens;
715
curve.totalLen = Math.max(total, 1);
918
var i = lo;
919
if (i <= 0)
920
{ return 0; }
921
var s0 = acc[i - 1];
922
var s1 = acc[i];
923
var f = (s1 > s0) ? (s - s0) / (s1 - s0) : 0;
924
return ((i - 1) + f) / n;
925
};
926
927
Online._measureCurve = function (curve)
928
{
929
if (!curve)
930
{ return curve; }
931
if (!curve.lutS)
932
{ this._buildArcLut(curve); }
716
933
return curve;
717
934
};
718
935
719
936
Online._segLen = function (seg, steps)
720
937
{
721
938
steps = steps || 10;
939
if (seg && seg.degree === 5)
940
{
941
if (!seg.lutS)
942
{ this._buildArcLut(seg, steps * 4); }
943
return seg.totalLen;
944
}
722
945
var len = 0;
723
946
var prev = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, 0);
724
947
for (var i = 1; i <= steps; i++)
725
948
{
726
949
var p = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, i / steps);
727
var dx = p.x - prev.x;
728
var dy = p.y - prev.y;
729
len += Math.sqrt(dx * dx + dy * dy);
950
len += Math.sqrt((p.x - prev.x) * (p.x - prev.x) + (p.y - prev.y) * (p.y - prev.y));
730
951
prev = p;
731
952
}
732
953
return Math.max(len, 1);
733
954
};
734
955
956
/** Point at arc-length fraction u ∈ [0, 1]. */
957
Online.curvePoint = function (curve, u)
958
{
959
if (!curve)
960
{ return { x: 0, y: 0 }; }
961
var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));
962
return this._evalPoly(curve, t);
963
};
964
735
965
/**
736
* Equal-parameter split across segments so matching local C'(t)
737
* at joints also matches dP/du (no speed jerk).
966
* dP/du under arc-length parametrisation: unit tangent × total length
967
* (constant speed in u, direction from the polynomial).
738
968
*/
739
Online._curveSegmentAt = function (curve, u)
969
Online.curveDeriv = function (curve, u)
740
970
{
741
971
if (!curve)
742
{ return null; }
743
if (!curve.segments)
972
{ return { x: 0, y: 0 }; }
973
var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));
974
var d = this._evalPolyDeriv(curve, t);
975
if (curve.arcLength !== false)
744
976
{
745
return { seg: curve, t: u < 0 ? 0 : (u > 1 ? 1 : u), scale: 1 };
977
if (!curve.lutS)
978
{ this._buildArcLut(curve); }
979
var mag = Math.sqrt(d.x * d.x + d.y * d.y);
980
if (mag < 1e-9)
981
{ return { x: 0, y: 0 }; }
982
var s = curve.totalLen / mag;
983
return { x: d.x * s, y: d.y * s };
746
984
}
747
var segs = curve.segments;
748
var n = segs.length;
749
if (u <= 0)
750
{ return { seg: segs[0], t: 0, scale: n }; }
751
if (u >= 1)
752
{ return { seg: segs[n - 1], t: 1, scale: n }; }
753
var at = u * n;
754
var i = Math.floor(at);
755
if (i >= n)
756
{ i = n - 1; }
757
var t = at - i;
758
return { seg: segs[i], t: t, scale: n };
985
return d;
759
986
};
760
987
761
/** Point on a (possibly multi-segment C¹) curve at u ∈ [0, 1]. */
762
Online.curvePoint = function (curve, u)
988
Online.curveDeriv2 = function (curve, u)
763
989
{
764
var at = this._curveSegmentAt(curve, u);
765
if (!at)
990
if (!curve)
766
991
{ return { x: 0, y: 0 }; }
767
var s = at.seg;
768
if (s && s.p1)
769
{ return this.cubicBezier(s.p0, s.p1, s.p2, s.p3, at.t); }
770
if (s && s.p0)
771
{ return { x: s.p0.x, y: s.p0.y }; }
772
return { x: 0, y: 0 };
992
var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));
993
return this._evalPolyDeriv2(curve, t);
773
994
};
774
995
775
/** dP/du of the curve (C¹ across joints). */
776
Online.curveDeriv = function (curve, u)
996
/** Curvature at arc-length fraction u (uses polynomial t, κ is invariant). */
997
Online.curveCurvature = function (curve, u)
777
998
{
778
var at = this._curveSegmentAt(curve, u);
779
if (!at || !at.seg || !at.seg.p1)
780
{ return { x: 0, y: 0 }; }
781
var s = at.seg;
782
var d = this.cubicBezierDeriv(s.p0, s.p1, s.p2, s.p3, at.t);
783
return { x: d.x * at.scale, y: d.y * at.scale };
999
var t = curve && curve.arcLength !== false ? this._arcT(curve, u) : u;
1000
return this.curvature(this._evalPolyDeriv(curve, t), this._evalPolyDeriv2(curve, t));
784
1001
};
785
1002
786
1003
/**
787
* Sample the smooth around-field curve at u ∈ [0, 1] (t1134).
788
* `clearance` may be a number or an options object.
1004
* Endpoint ease rates so arrival speed ≈ fallSpeed under arc-length
1005
* parametrisation: v = (L / duration) * p'(raw).
789
1006
*/
1007
Online.flightEaseRates = function (curve, duration, fallPx)
1008
{
1009
if (!curve || !curve.lutS)
1010
{ this._measureCurve(curve); }
1011
var L = (curve && curve.totalLen) || 400;
1012
duration = duration || this.FLIGHT_DURATION;
1013
var v1 = fallPx && L ? (fallPx * duration) / L : 0.75;
1014
if (v1 < 0.28)
1015
{ v1 = 0.28; }
1016
if (v1 > 1.15)
1017
{ v1 = 1.15; }
1018
var v0 = v1 + 0.2;
1019
if (v0 > 1)
1020
{ v0 = 1; }
1021
if (v0 < 0.45)
1022
{ v0 = 0.45; }
1023
return { v0: v0, v1: v1 };
1024
};
1025
790
1026
Online.aroundFieldPoint = function (start, end, u, field, clearance)
791
1027
{
792
1028
var opts = (clearance && typeof clearance === 'object') ? clearance : { clearance: clearance };
@@ -794,17 +1030,11 @@ Online.aroundFieldPoint = function (start, end, u, field, clearance)
794
1030
return this.curvePoint(curve, u);
795
1031
};
796
1032
797
/** Guide points (start, joints, end) for older tests. */
798
1033
Online.waypointsAroundField = function (start, end, field, clearance)
799
1034
{
800
var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 64 });
801
if (c.segments && c.segments.length)
802
{
803
var pts = [{ x: c.segments[0].p0.x, y: c.segments[0].p0.y }];
804
for (var i = 0; i < c.segments.length; i++)
805
{ pts.push({ x: c.segments[i].p3.x, y: c.segments[i].p3.y }); }
806
return pts;
807
}
1035
var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 100 });
1036
if (c.pts && c.pts.length)
1037
{ return c.pts.slice(); }
808
1038
return [c.p0, c.p1, c.p2, c.p3];
809
1039
};
810
1040
@@ -1088,14 +1318,12 @@ Online.spawnFlights = function (results, locals)
1088
1318
: this.arcHeight(start, end);
1089
1319
var field = this.playerFieldRect();
1090
1320
var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');
1091
var clearance = Math.max(64, height * 0.45);
1321
var clearance = Math.max(150, height * 0.55);
1092
1322
var endSpeed = this._fallSpeedPx(field, duration / 1000);
1093
1323
var curve = miss ? null : this.makeAroundCurve(start, end, around ? field : null, {
1094
clearance: clearance,
1095
startSpeedPx: endSpeed,
1096
endSpeedPx: endSpeed,
1097
duration: duration / 1000
1324
clearance: clearance
1098
1325
});
1326
var easeRates = curve ? this.flightEaseRates(curve, duration / 1000, endSpeed) : null;
1099
1327
this._flights.push({
1100
1328
el: el,
1101
1329
start: start,
@@ -1104,6 +1332,7 @@ Online.spawnFlights = function (results, locals)
1104
1332
height: height,
1105
1333
t0: t0,
1106
1334
duration: duration,
1335
easeRates: easeRates,
1107
1336
spin: miss ? ((Math.random() * 2 - 1) * 720) : ((Math.random() * 2 - 1) * 120),
1108
1337
miss: miss,
1109
1338
kind: kind,
@@ -1254,10 +1483,9 @@ Online.previewExit = function (info)
1254
1483
front.appendChild(el);
1255
1484
var endSpeed = this._fallSpeedPx(field, this.FLIGHT_DURATION);
1256
1485
var curve = this.makeAroundCurve(start, end, info.from_id ? null : field, {
1257
clearance: clearance,
1258
startSpeedPx: endSpeed,
1259
endSpeedPx: endSpeed
1486
clearance: clearance
1260
1487
});
1488
var easeRates = this.flightEaseRates(curve, this.FLIGHT_DURATION, endSpeed);
1261
1489
var now = performance.now();
1262
1490
var mini = info.from_id ? (this._miniCellSize(info.from_id) / full) : 1;
1263
1491
if (mini < 0.12)
@@ -1280,6 +1508,7 @@ Online.previewExit = function (info)
1280
1508
landCol: null,
1281
1509
field: field,
1282
1510
curve: curve,
1511
easeRates: easeRates,
1283
1512
preview: true,
1284
1513
color: info.color,
1285
1514
from_id: info.from_id || null
@@ -1371,19 +1600,19 @@ Online.retargetFlight = function (f, r, meta, now)
1371
1600
var height = miss
1372
1601
? Math.max(40, Math.min(160, Math.abs(end.y - pt.y) * 0.35))
1373
1602
: this.arcHeight(pt, end);
1374
var clearance = Math.max(64, height * 0.45);
1603
var clearance = Math.max(150, height * 0.55);
1375
1604
var endSpeed = this._fallSpeedPx(field, duration / 1000);
1376
1605
var curve = miss ? null : this.makeAroundCurve(pt, end, around ? field : null, {
1377
clearance: clearance,
1378
startSpeedPx: endSpeed,
1379
endSpeedPx: endSpeed
1606
clearance: clearance
1380
1607
});
1608
var easeRates = curve ? this.flightEaseRates(curve, duration / 1000, endSpeed) : null;
1381
1609
1382
1610
f.start = pt;
1383
1611
f.end = end;
1384
1612
f.t0 = now;
1385
1613
f.duration = duration;
1386
1614
f.curve = curve;
1615
f.easeRates = easeRates;
1387
1616
f.ctrl = miss ? this._missCtrl(pt, end) : null;
1388
1617
f.height = height;
1389
1618
f.miss = miss;
@@ -1461,8 +1690,10 @@ Online.tickFlights = function (now)
1461
1690
}
1462
1691
if (raw < 0)
1463
1692
{ raw = 0; }
1464
// Non-zero endpoint speed so the handoff matches fallSpeed (t1135).
1465
var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, 0.85, 0.85);
1693
// Non-zero endpoint speed so the handoff matches fallSpeed (t1135/t1138).
1694
var v0 = (f.easeRates && f.easeRates.v0 != null) ? f.easeRates.v0 : 0.85;
1695
var v1 = (f.easeRates && f.easeRates.v1 != null) ? f.easeRates.v1 : 0.85;
1696
var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, v0, v1);
1466
1697
var x, y;
1467
1698
if (f.miss && f.ctrl)
1468
1699
{
@@ -551,6 +551,155 @@ Online.cubicBezierDeriv = function (p0, p1, p2, p3, t)551
};551
};552
};552
};553
553
554
Online.cubicBezierDeriv2 = function (p0, p1, p2, p3, t)555
{556
if (t < 0)557
{ t = 0; }558
if (t > 1)559
{ t = 1; }560
var u = 1 - t;561
// C''(t) = 6(1−t)(P2−2P1+P0) + 6t(P3−2P2+P1)562
return {563
x: 6 * u * (p2.x - 2 * p1.x + p0.x) + 6 * t * (p3.x - 2 * p2.x + p1.x),564
y: 6 * u * (p2.y - 2 * p1.y + p0.y) + 6 * t * (p3.y - 2 * p2.y + p1.y)565
};566
};567
568
/** Degree-5 Bézier. One polynomial ⇒ C^∞ on (0, 1) (t1138). */569
Online.quinticBezier = function (pts, t)570
{571
if (t < 0)572
{ t = 0; }573
if (t > 1)574
{ t = 1; }575
var u = 1 - t;576
var u2 = u * u, u3 = u2 * u, u4 = u3 * u, u5 = u4 * u;577
var t2 = t * t, t3 = t2 * t, t4 = t3 * t, t5 = t4 * t;578
var b0 = u5, b1 = 5 * u4 * t, b2 = 10 * u3 * t2;579
var b3 = 10 * u2 * t3, b4 = 5 * u * t4, b5 = t5;580
return {581
x: b0 * pts[0].x + b1 * pts[1].x + b2 * pts[2].x582
+ b3 * pts[3].x + b4 * pts[4].x + b5 * pts[5].x,583
y: b0 * pts[0].y + b1 * pts[1].y + b2 * pts[2].y584
+ b3 * pts[3].y + b4 * pts[4].y + b5 * pts[5].y585
};586
};587
588
Online.quinticBezierDeriv = function (pts, t)589
{590
if (t < 0)591
{ t = 0; }592
if (t > 1)593
{ t = 1; }594
var d = [];595
for (var i = 0; i < 5; i++)596
{ d.push({ x: pts[i + 1].x - pts[i].x, y: pts[i + 1].y - pts[i].y }); }597
var u = 1 - t;598
var u2 = u * u, u3 = u2 * u, u4 = u3 * u;599
var t2 = t * t, t3 = t2 * t, t4 = t3 * t;600
var b0 = u4, b1 = 4 * u3 * t, b2 = 6 * u2 * t2, b3 = 4 * u * t3, b4 = t4;601
return {602
x: 5 * (b0 * d[0].x + b1 * d[1].x + b2 * d[2].x + b3 * d[3].x + b4 * d[4].x),603
y: 5 * (b0 * d[0].y + b1 * d[1].y + b2 * d[2].y + b3 * d[3].y + b4 * d[4].y)604
};605
};606
607
Online.quinticBezierDeriv2 = function (pts, t)608
{609
if (t < 0)610
{ t = 0; }611
if (t > 1)612
{ t = 1; }613
var d2 = [];614
for (var i = 0; i < 4; i++)615
{616
d2.push({617
x: pts[i + 2].x - 2 * pts[i + 1].x + pts[i].x,618
y: pts[i + 2].y - 2 * pts[i + 1].y + pts[i].y619
});620
}621
var u = 1 - t;622
var u2 = u * u, u3 = u2 * u;623
var t2 = t * t, t3 = t2 * t;624
var b0 = u3, b1 = 3 * u2 * t, b2 = 3 * u * t2, b3 = t3;625
return {626
x: 20 * (b0 * d2[0].x + b1 * d2[1].x + b2 * d2[2].x + b3 * d2[3].x),627
y: 20 * (b0 * d2[0].y + b1 * d2[1].y + b2 * d2[2].y + b3 * d2[3].y)628
};629
};630
631
/** Signed curvature κ = (x'y'' − y'x'') / |r'|³ (independent of parametrisation). */632
Online.curvature = function (d1, d2)633
{634
var sp = d1.x * d1.x + d1.y * d1.y;635
var den = Math.pow(sp, 1.5);636
if (den < 1e-12)637
{ return 0; }638
return (d1.x * d2.y - d1.y * d2.x) / den;639
};640
641
/**642
* de Casteljau evaluation of a Bézier of any degree, plus first643
* and second derivatives (t1138). A single polynomial is C^∞ on (0, 1).644
*/645
Online.bezierPoint = function (pts, t)646
{647
if (!pts || !pts.length)648
{ return { x: 0, y: 0 }; }649
if (t < 0)650
{ t = 0; }651
if (t > 1)652
{ t = 1; }653
var tmp = [];654
var i, k, u = 1 - t;655
for (i = 0; i < pts.length; i++)656
{ tmp.push({ x: pts[i].x, y: pts[i].y }); }657
for (k = 1; k < pts.length; k++)658
{659
for (i = 0; i < pts.length - k; i++)660
{661
tmp[i] = {662
x: u * tmp[i].x + t * tmp[i + 1].x,663
y: u * tmp[i].y + t * tmp[i + 1].y664
};665
}666
}667
return tmp[0];668
};669
670
Online.bezierDeriv = function (pts, t)671
{672
var n = pts ? pts.length - 1 : 0;673
if (n < 1)674
{ return { x: 0, y: 0 }; }675
var d = [];676
for (var i = 0; i < n; i++)677
{678
d.push({679
x: n * (pts[i + 1].x - pts[i].x),680
y: n * (pts[i + 1].y - pts[i].y)681
});682
}683
return this.bezierPoint(d, t);684
};685
686
Online.bezierDeriv2 = function (pts, t)687
{688
var n = pts ? pts.length - 1 : 0;689
if (n < 2)690
{ return { x: 0, y: 0 }; }691
var s = n * (n - 1);692
var d2 = [];693
for (var i = 0; i < n - 1; i++)694
{695
d2.push({696
x: s * (pts[i + 2].x - 2 * pts[i + 1].x + pts[i].x),697
y: s * (pts[i + 2].y - 2 * pts[i + 1].y + pts[i].y)698
});699
}700
return this.bezierPoint(d2, t);701
};702
554
/**703
/**555
* Hermite parameter with p(0)=0, p(1)=1, p'(0)=v0, p'(1)=v1.704
* Hermite parameter with p(0)=0, p(1)=1, p'(0)=v0, p'(1)=v1.556
* v0=v1=1 is linear (constant speed in t, no endpoint halt).705
* v0=v1=1 is linear (constant speed in t, no endpoint halt).@@ -571,29 +720,18 @@ Online.smoothParam = function (t, v0, v1)571
};720
};572
721
573
/**722
/**574
* A simple up-and-over cubic, used when there is no field to route723
* One continuous degree-7 Bézier: a single big, round, ballistic575
* around (enemy→enemy) or both ends already sit above the apex.724
* arch (t1138). Vertical at the exit (heading up) and at the576
*/725
* landing (heading down). Horizontal travel sits on the apex,577
Online._singleCubic = function (start, end, apexY, opts)726
* above the field — a cluster of high control points on each578
{727
* side keeps the hull out of the interior, with no straight579
opts = opts || {};728
* cruise and no quarter-turn kinks. C^∞ on (0, 1).580
var rise = Math.max(40, Math.abs(start.y - apexY) * 0.9);729
*581
if (opts.startSpeedPx)730
* Incoming (low mini → high field): the first mid controls stay582
{ rise = Math.max(rise, opts.startSpeedPx); }731
* on the outside so the rise clears the top before crossing.583
var fall = Math.max(40, Math.abs(end.y - apexY) * 0.9);732
* Outgoing (high exit → low mini): mid controls sit above the584
if (opts.endSpeedPx)733
* target so the dive happens outside. Enemy→enemy (no field)585
{ fall = Math.max(40, opts.endSpeedPx); }734
* is a symmetric parabola behind the field.586
var p1 = { x: start.x, y: Math.min(start.y - rise, apexY) };587
var p2 = { x: end.x, y: Math.min(end.y - fall, apexY) };588
return { p0: start, p1: p1, p2: p2, p3: end, apexY: apexY };589
};590
591
/**592
* Smooth C¹ path: rise up (launch column or outside), cruise above593
* the field, dive into the landing column (t1134 / t1135). Joints594
* are quarter-turn cubics with matching tangents, so there are no595
* corners and no speed jerks. The curve may use the launch and596
* landing corridors but does not cut across the field interior.597
*/735
*/598
Online.makeAroundCurve = function (start, end, field, opts)736
Online.makeAroundCurve = function (start, end, field, opts)599
{737
{@@ -602,191 +740,289 @@ Online.makeAroundCurve = function (start, end, field, opts)602
{ start = { x: 0, y: 0 }; }740
{ start = { x: 0, y: 0 }; }603
if (!end)741
if (!end)604
{ end = { x: 0, y: 0 }; }742
{ end = { x: 0, y: 0 }; }605
var clearance = opts.clearance || 80;743
var clearance = opts.clearance || 150;606
if (!field)744
if (typeof MiniLayout !== 'undefined' && MiniLayout.ARC_TOP_PAD)745
{ clearance = Math.max(clearance, MiniLayout.ARC_TOP_PAD); }746
if (clearance < 150)747
{ clearance = 150; }748
749
var apexY;750
var left, right, top;751
if (field)607
{752
{608
var looseApex = Math.min(start.y, end.y) - Math.max(40, clearance * 0.5);753
top = field.top;609
if (looseApex < 8)754
left = field.left;610
{ looseApex = Math.min(start.y, end.y) - 24; }755
right = field.right;611
return this._singleCubic(start, end, looseApex, opts);756
apexY = top - clearance;757
if (apexY < 8)758
{ apexY = Math.max(8, top - Math.max(28, clearance * 0.5)); }759
}760
else761
{762
apexY = Math.min(start.y, end.y) - Math.max(56, clearance * 0.6);763
if (apexY < 8)764
{ apexY = Math.min(start.y, end.y) - 40; }765
left = right = top = null;612
}766
}613
var top = field.top;614
var left = field.left;615
var right = field.right;616
var apexY = top - clearance;617
if (apexY < 8)618
{ apexY = Math.max(8, top - Math.max(24, clearance * 0.5)); }619
620
if (start.y <= apexY + 8 && end.y <= apexY + 8)621
{ return this._singleCubic(start, end, apexY, opts); }622
767
623
var pad = 14;624
var dir = end.x >= start.x ? 1 : -1;625
var span = Math.abs(end.x - start.x);626
var horiz = Math.max(28, Math.min(80, span * 0.15 + 8));627
function outsideX (x)768
function outsideX (x)628
{769
{770
if (!field)771
{ return x; }772
var pad = 40;629
if (x < left + 8)773
if (x < left + 8)630
{ return Math.min(x, left - pad); }774
{ return Math.min(x, left - pad); }631
if (x > right - 8)775
if (x > right - 8)632
{ return Math.max(x, right + pad); }776
{ return Math.max(x, right + pad); }633
return x;777
return x;634
}778
}635
var riseX = outsideX(start.x);636
var diveX = outsideX(end.x);637
var segments = [];638
779
639
// Rise ends already turned toward the cruise, at apex.780
var startLow = field && start.y > top + 8;640
var riseEnd = { x: riseX + dir * horiz, y: apexY };781
var endLow = field && end.y > top + 8;641
if (start.y > apexY + 4)782
var startInside = field && start.x > left + 8 && start.x < right - 8;783
var endInside = field && end.x > left + 8 && end.x < right - 8;784
// Incoming: rise on the outside first. Outgoing: cross at785
// the apex, then dive outside. Otherwise a symmetric arch.786
var kind;787
if (startLow && !endLow)788
{ kind = 'in'; }789
else if (!startLow && endLow)790
{ kind = 'out'; }791
else if (startLow && endLow)792
{ kind = startInside ? 'out' : (endInside ? 'in' : 'sym'); }793
else794
{ kind = 'sym'; }795
796
var sx = outsideX(start.x);797
var ex = outsideX(end.x);798
var rise = Math.max(80, Math.abs(start.y - apexY) * 0.35);799
var P0 = { x: start.x, y: start.y };800
var P1 = { x: start.x, y: start.y - rise };801
var P2, P3, P4, P5;802
var P6 = { x: end.x, y: apexY };803
var P7 = { x: end.x, y: end.y };804
if (kind === 'in')805
{806
P2 = { x: sx, y: apexY };807
P3 = { x: sx, y: apexY };808
P4 = { x: sx + (ex - sx) * 0.5, y: apexY };809
P5 = { x: sx + (ex - sx) * 0.85, y: apexY };810
}811
else if (kind === 'out')642
{812
{643
var p1 = { x: start.x, y: apexY };813
P2 = { x: start.x, y: apexY };644
var p2 = { x: riseX, y: apexY };814
P3 = { x: sx + (ex - sx) * 0.55, y: apexY };645
segments.push({ p0: start, p1: p1, p2: p2, p3: riseEnd });815
P4 = { x: ex, y: apexY };816
P5 = { x: end.x, y: apexY };646
}817
}647
else818
else648
{ riseEnd = { x: start.x, y: Math.min(start.y, apexY) }; }649
650
// Cruise stays on the apex line (convex hull is above the field).651
var diveStart = { x: diveX - dir * horiz, y: apexY };652
if (Math.abs(riseEnd.x - diveStart.x) > 4 || Math.abs(riseEnd.y - diveStart.y) > 4)653
{819
{654
// Keep the cruise direction consistent; if the two joints820
P2 = { x: sx, y: apexY };655
// would cross, just connect them directly.821
P3 = { x: sx + (ex - sx) * 0.35, y: apexY };656
var cdir = diveStart.x >= riseEnd.x ? 1 : -1;822
P4 = { x: sx + (ex - sx) * 0.65, y: apexY };657
var c1 = { x: riseEnd.x + cdir * horiz, y: riseEnd.y };823
P5 = { x: ex, y: apexY };658
if (c1.y > apexY)659
{ c1.y = apexY; }660
var c2 = { x: diveStart.x - cdir * horiz, y: diveStart.y };661
segments.push({ p0: { x: riseEnd.x, y: riseEnd.y }, p1: c1, p2: c2, p3: diveStart });662
}824
}825
var pts = [P0, P1, P2, P3, P4, P5, P6, P7];826
827
var curve = {828
degree: 7,829
pts: pts,830
p0: P0,831
p3: P7,832
apexY: apexY,833
kind: kind,834
arcLength: true835
};836
this._buildArcLut(curve);837
return curve;838
};663
839
664
if (end.y > apexY + 4 || Math.abs(end.x - (diveStart.x + dir * horiz)) > 2)840
Online._evalPoly = function (curve, t)841
{842
if (curve && curve.pts && curve.pts.length >= 2)843
{ return this.bezierPoint(curve.pts, t); }844
if (curve && curve.p1)845
{ return this.cubicBezier(curve.p0, curve.p1, curve.p2, curve.p3, t); }846
if (curve && curve.segments)665
{847
{666
var u0 = (Math.abs(riseEnd.x - diveStart.x) > 4 || segments.length === 0)848
var n = curve.segments.length;667
? diveStart849
var at = (t < 0 ? 0 : (t > 1 ? 1 : t)) * n;668
: riseEnd;850
var i = Math.floor(at);669
if (segments.length && segments[segments.length - 1].p3.x === u0.x851
if (i >= n)670
&& segments[segments.length - 1].p3.y === u0.y)852
{ i = n - 1; }671
{ /* already joined */ }853
var s = curve.segments[i];672
else if (segments.length === 0)854
return this.cubicBezier(s.p0, s.p1, s.p2, s.p3, at - i);673
{ u0 = riseEnd; }674
var u1 = { x: u0.x + dir * horiz, y: u0.y };675
var fall = Math.max(24, Math.abs(end.y - apexY) * 0.45);676
if (opts.endSpeedPx)677
{678
// |P3-P2| so arrival ≈ fallSpeed after equal-param scaling.679
// velocity = 3*|P3-P2|*n*v1/duration; endSpeedPx is680
// (cells*px*duration)/3, so |P3-P2| = endSpeedPx/(n*v1).681
var nEst = Math.max(1, segments.length + 1);682
fall = Math.max(20, opts.endSpeedPx / (nEst * 0.85));683
}684
var u2 = { x: end.x, y: end.y - fall };685
// P2 may sit between the apex and the end (slower arrival);686
// only clamp if it would pass the end or fly far past the apex.687
if (u2.y < apexY - 240)688
{ u2.y = apexY - 240; }689
if (u2.y > end.y - 8)690
{ u2.y = end.y - 8; }691
segments.push({ p0: u0, p1: u1, p2: u2, p3: end });692
}855
}856
return { x: 0, y: 0 };857
};693
858
694
if (segments.length === 0)859
Online._evalPolyDeriv = function (curve, t)695
{ return this._singleCubic(start, end, apexY, opts); }860
{696
if (segments.length === 1 && !segments[0].p0)861
if (curve && curve.pts && curve.pts.length >= 2)697
{ return this._singleCubic(start, end, apexY, opts); }862
{ return this.bezierDeriv(curve.pts, t); }863
if (curve && curve.p1)864
{ return this.cubicBezierDeriv(curve.p0, curve.p1, curve.p2, curve.p3, t); }865
return { x: 0, y: 0 };866
};698
867
699
return { segments: segments, apexY: apexY, p0: start, p3: end };868
Online._evalPolyDeriv2 = function (curve, t)869
{870
if (curve && curve.pts && curve.pts.length >= 3)871
{ return this.bezierDeriv2(curve.pts, t); }872
if (curve && curve.p1)873
{ return this.cubicBezierDeriv2(curve.p0, curve.p1, curve.p2, curve.p3, t); }874
return { x: 0, y: 0 };700
};875
};701
876
702
Online._measureCurve = function (curve)877
/** Cumulative-length LUT so u is a fraction of arc length (t1138). */878
Online._buildArcLut = function (curve, steps)703
{879
{704
if (!curve.segments)880
steps = steps || 48;705
{ return curve; }881
var acc = [0];706
var lens = [];882
var prev = this._evalPoly(curve, 0);707
var total = 0;883
for (var i = 1; i <= steps; i++)708
for (var i = 0; i < curve.segments.length; i++)884
{885
var p = this._evalPoly(curve, i / steps);886
acc.push(acc[i - 1] + Math.sqrt((p.x - prev.x) * (p.x - prev.x)887
+ (p.y - prev.y) * (p.y - prev.y)));888
prev = p;889
}890
curve.lutS = acc;891
curve.lutN = steps;892
curve.totalLen = Math.max(acc[steps], 1);893
return curve;894
};895
896
/** Polynomial t whose arc length fraction is u. */897
Online._arcT = function (curve, u)898
{899
if (u <= 0)900
{ return 0; }901
if (u >= 1)902
{ return 1; }903
if (!curve.lutS)904
{ this._buildArcLut(curve); }905
var s = u * curve.totalLen;906
var acc = curve.lutS;907
var n = curve.lutN;908
var lo = 0;909
var hi = n;910
while (lo < hi)709
{911
{710
var L = this._segLen(curve.segments[i]);912
var mid = (lo + hi) >> 1;711
lens.push(L);913
if (acc[mid] < s)712
total += L;914
{ lo = mid + 1; }915
else916
{ hi = mid; }713
}917
}714
curve.lens = lens;918
var i = lo;715
curve.totalLen = Math.max(total, 1);919
if (i <= 0)920
{ return 0; }921
var s0 = acc[i - 1];922
var s1 = acc[i];923
var f = (s1 > s0) ? (s - s0) / (s1 - s0) : 0;924
return ((i - 1) + f) / n;925
};926
927
Online._measureCurve = function (curve)928
{929
if (!curve)930
{ return curve; }931
if (!curve.lutS)932
{ this._buildArcLut(curve); }716
return curve;933
return curve;717
};934
};718
935
719
Online._segLen = function (seg, steps)936
Online._segLen = function (seg, steps)720
{937
{721
steps = steps || 10;938
steps = steps || 10;939
if (seg && seg.degree === 5)940
{941
if (!seg.lutS)942
{ this._buildArcLut(seg, steps * 4); }943
return seg.totalLen;944
}722
var len = 0;945
var len = 0;723
var prev = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, 0);946
var prev = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, 0);724
for (var i = 1; i <= steps; i++)947
for (var i = 1; i <= steps; i++)725
{948
{726
var p = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, i / steps);949
var p = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, i / steps);727
var dx = p.x - prev.x;950
len += Math.sqrt((p.x - prev.x) * (p.x - prev.x) + (p.y - prev.y) * (p.y - prev.y));728
var dy = p.y - prev.y;729
len += Math.sqrt(dx * dx + dy * dy);730
prev = p;951
prev = p;731
}952
}732
return Math.max(len, 1);953
return Math.max(len, 1);733
};954
};734
955
956
/** Point at arc-length fraction u ∈ [0, 1]. */957
Online.curvePoint = function (curve, u)958
{959
if (!curve)960
{ return { x: 0, y: 0 }; }961
var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));962
return this._evalPoly(curve, t);963
};964
735
/**965
/**736
* Equal-parameter split across segments so matching local C'(t)966
* dP/du under arc-length parametrisation: unit tangent × total length737
* at joints also matches dP/du (no speed jerk).967
* (constant speed in u, direction from the polynomial).738
*/968
*/739
Online._curveSegmentAt = function (curve, u)969
Online.curveDeriv = function (curve, u)740
{970
{741
if (!curve)971
if (!curve)742
{ return null; }972
{ return { x: 0, y: 0 }; }743
if (!curve.segments)973
var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));974
var d = this._evalPolyDeriv(curve, t);975
if (curve.arcLength !== false)744
{976
{745
return { seg: curve, t: u < 0 ? 0 : (u > 1 ? 1 : u), scale: 1 };977
if (!curve.lutS)978
{ this._buildArcLut(curve); }979
var mag = Math.sqrt(d.x * d.x + d.y * d.y);980
if (mag < 1e-9)981
{ return { x: 0, y: 0 }; }982
var s = curve.totalLen / mag;983
return { x: d.x * s, y: d.y * s };746
}984
}747
var segs = curve.segments;985
return d;748
var n = segs.length;749
if (u <= 0)750
{ return { seg: segs[0], t: 0, scale: n }; }751
if (u >= 1)752
{ return { seg: segs[n - 1], t: 1, scale: n }; }753
var at = u * n;754
var i = Math.floor(at);755
if (i >= n)756
{ i = n - 1; }757
var t = at - i;758
return { seg: segs[i], t: t, scale: n };759
};986
};760
987
761
/** Point on a (possibly multi-segment C¹) curve at u ∈ [0, 1]. */988
Online.curveDeriv2 = function (curve, u)762
Online.curvePoint = function (curve, u)763
{989
{764
var at = this._curveSegmentAt(curve, u);990
if (!curve)765
if (!at)766
{ return { x: 0, y: 0 }; }991
{ return { x: 0, y: 0 }; }767
var s = at.seg;992
var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));768
if (s && s.p1)993
return this._evalPolyDeriv2(curve, t);769
{ return this.cubicBezier(s.p0, s.p1, s.p2, s.p3, at.t); }770
if (s && s.p0)771
{ return { x: s.p0.x, y: s.p0.y }; }772
return { x: 0, y: 0 };773
};994
};774
995
775
/** dP/du of the curve (C¹ across joints). */996
/** Curvature at arc-length fraction u (uses polynomial t, κ is invariant). */776
Online.curveDeriv = function (curve, u)997
Online.curveCurvature = function (curve, u)777
{998
{778
var at = this._curveSegmentAt(curve, u);999
var t = curve && curve.arcLength !== false ? this._arcT(curve, u) : u;779
if (!at || !at.seg || !at.seg.p1)1000
return this.curvature(this._evalPolyDeriv(curve, t), this._evalPolyDeriv2(curve, t));780
{ return { x: 0, y: 0 }; }781
var s = at.seg;782
var d = this.cubicBezierDeriv(s.p0, s.p1, s.p2, s.p3, at.t);783
return { x: d.x * at.scale, y: d.y * at.scale };784
};1001
};785
1002
786
/**1003
/**787
* Sample the smooth around-field curve at u ∈ [0, 1] (t1134).1004
* Endpoint ease rates so arrival speed ≈ fallSpeed under arc-length788
* `clearance` may be a number or an options object.1005
* parametrisation: v = (L / duration) * p'(raw).789
*/1006
*/1007
Online.flightEaseRates = function (curve, duration, fallPx)1008
{1009
if (!curve || !curve.lutS)1010
{ this._measureCurve(curve); }1011
var L = (curve && curve.totalLen) || 400;1012
duration = duration || this.FLIGHT_DURATION;1013
var v1 = fallPx && L ? (fallPx * duration) / L : 0.75;1014
if (v1 < 0.28)1015
{ v1 = 0.28; }1016
if (v1 > 1.15)1017
{ v1 = 1.15; }1018
var v0 = v1 + 0.2;1019
if (v0 > 1)1020
{ v0 = 1; }1021
if (v0 < 0.45)1022
{ v0 = 0.45; }1023
return { v0: v0, v1: v1 };1024
};1025
790
Online.aroundFieldPoint = function (start, end, u, field, clearance)1026
Online.aroundFieldPoint = function (start, end, u, field, clearance)791
{1027
{792
var opts = (clearance && typeof clearance === 'object') ? clearance : { clearance: clearance };1028
var opts = (clearance && typeof clearance === 'object') ? clearance : { clearance: clearance };@@ -794,17 +1030,11 @@ Online.aroundFieldPoint = function (start, end, u, field, clearance)794
return this.curvePoint(curve, u);1030
return this.curvePoint(curve, u);795
};1031
};796
1032
797
/** Guide points (start, joints, end) for older tests. */798
Online.waypointsAroundField = function (start, end, field, clearance)1033
Online.waypointsAroundField = function (start, end, field, clearance)799
{1034
{800
var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 64 });1035
var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 100 });801
if (c.segments && c.segments.length)1036
if (c.pts && c.pts.length)802
{1037
{ return c.pts.slice(); }803
var pts = [{ x: c.segments[0].p0.x, y: c.segments[0].p0.y }];804
for (var i = 0; i < c.segments.length; i++)805
{ pts.push({ x: c.segments[i].p3.x, y: c.segments[i].p3.y }); }806
return pts;807
}808
return [c.p0, c.p1, c.p2, c.p3];1038
return [c.p0, c.p1, c.p2, c.p3];809
};1039
};810
1040
@@ -1088,14 +1318,12 @@ Online.spawnFlights = function (results, locals)1088
: this.arcHeight(start, end);1318
: this.arcHeight(start, end);1089
var field = this.playerFieldRect();1319
var field = this.playerFieldRect();1090
var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');1320
var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');1091
var clearance = Math.max(64, height * 0.45);1321
var clearance = Math.max(150, height * 0.55);1092
var endSpeed = this._fallSpeedPx(field, duration / 1000);1322
var endSpeed = this._fallSpeedPx(field, duration / 1000);1093
var curve = miss ? null : this.makeAroundCurve(start, end, around ? field : null, {1323
var curve = miss ? null : this.makeAroundCurve(start, end, around ? field : null, {1094
clearance: clearance,1324
clearance: clearance1095
startSpeedPx: endSpeed,1096
endSpeedPx: endSpeed,1097
duration: duration / 10001098
});1325
});1326
var easeRates = curve ? this.flightEaseRates(curve, duration / 1000, endSpeed) : null;1099
this._flights.push({1327
this._flights.push({1100
el: el,1328
el: el,1101
start: start,1329
start: start,@@ -1104,6 +1332,7 @@ Online.spawnFlights = function (results, locals)1104
height: height,1332
height: height,1105
t0: t0,1333
t0: t0,1106
duration: duration,1334
duration: duration,1335
easeRates: easeRates,1107
spin: miss ? ((Math.random() * 2 - 1) * 720) : ((Math.random() * 2 - 1) * 120),1336
spin: miss ? ((Math.random() * 2 - 1) * 720) : ((Math.random() * 2 - 1) * 120),1108
miss: miss,1337
miss: miss,1109
kind: kind,1338
kind: kind,@@ -1254,10 +1483,9 @@ Online.previewExit = function (info)1254
front.appendChild(el);1483
front.appendChild(el);1255
var endSpeed = this._fallSpeedPx(field, this.FLIGHT_DURATION);1484
var endSpeed = this._fallSpeedPx(field, this.FLIGHT_DURATION);1256
var curve = this.makeAroundCurve(start, end, info.from_id ? null : field, {1485
var curve = this.makeAroundCurve(start, end, info.from_id ? null : field, {1257
clearance: clearance,1486
clearance: clearance1258
startSpeedPx: endSpeed,1259
endSpeedPx: endSpeed1260
});1487
});1488
var easeRates = this.flightEaseRates(curve, this.FLIGHT_DURATION, endSpeed);1261
var now = performance.now();1489
var now = performance.now();1262
var mini = info.from_id ? (this._miniCellSize(info.from_id) / full) : 1;1490
var mini = info.from_id ? (this._miniCellSize(info.from_id) / full) : 1;1263
if (mini < 0.12)1491
if (mini < 0.12)@@ -1280,6 +1508,7 @@ Online.previewExit = function (info)1280
landCol: null,1508
landCol: null,1281
field: field,1509
field: field,1282
curve: curve,1510
curve: curve,1511
easeRates: easeRates,1283
preview: true,1512
preview: true,1284
color: info.color,1513
color: info.color,1285
from_id: info.from_id || null1514
from_id: info.from_id || null@@ -1371,19 +1600,19 @@ Online.retargetFlight = function (f, r, meta, now)1371
var height = miss1600
var height = miss1372
? Math.max(40, Math.min(160, Math.abs(end.y - pt.y) * 0.35))1601
? Math.max(40, Math.min(160, Math.abs(end.y - pt.y) * 0.35))1373
: this.arcHeight(pt, end);1602
: this.arcHeight(pt, end);1374
var clearance = Math.max(64, height * 0.45);1603
var clearance = Math.max(150, height * 0.55);1375
var endSpeed = this._fallSpeedPx(field, duration / 1000);1604
var endSpeed = this._fallSpeedPx(field, duration / 1000);1376
var curve = miss ? null : this.makeAroundCurve(pt, end, around ? field : null, {1605
var curve = miss ? null : this.makeAroundCurve(pt, end, around ? field : null, {1377
clearance: clearance,1606
clearance: clearance1378
startSpeedPx: endSpeed,1379
endSpeedPx: endSpeed1380
});1607
});1608
var easeRates = curve ? this.flightEaseRates(curve, duration / 1000, endSpeed) : null;1381
1609
1382
f.start = pt;1610
f.start = pt;1383
f.end = end;1611
f.end = end;1384
f.t0 = now;1612
f.t0 = now;1385
f.duration = duration;1613
f.duration = duration;1386
f.curve = curve;1614
f.curve = curve;1615
f.easeRates = easeRates;1387
f.ctrl = miss ? this._missCtrl(pt, end) : null;1616
f.ctrl = miss ? this._missCtrl(pt, end) : null;1388
f.height = height;1617
f.height = height;1389
f.miss = miss;1618
f.miss = miss;@@ -1461,8 +1690,10 @@ Online.tickFlights = function (now)1461
}1690
}1462
if (raw < 0)1691
if (raw < 0)1463
{ raw = 0; }1692
{ raw = 0; }1464
// Non-zero endpoint speed so the handoff matches fallSpeed (t1135).1693
// Non-zero endpoint speed so the handoff matches fallSpeed (t1135/t1138).1465
var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, 0.85, 0.85);1694
var v0 = (f.easeRates && f.easeRates.v0 != null) ? f.easeRates.v0 : 0.85;1695
var v1 = (f.easeRates && f.easeRates.v1 != null) ? f.easeRates.v1 : 0.85;1696
var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, v0, v1);1466
var x, y;1697
var x, y;1467
if (f.miss && f.ctrl)1698
if (f.miss && f.ctrl)1468
{1699
{