BlueGit

projects-timfalken-com meteors

Merge branch 'feature/t1138-smoother-arcs'

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
d4cca6794a31553cece36db15b4c6671fdb13bc9
Pipeline
Passed
5 files changed +977 −412
build/game.js +411 −180
@@ -4611,6 +4611,155 @@ Online.cubicBezierDeriv = function (p0, p1, p2, p3, t)
4611 4611 };
4612 4612 };
4613 4613
4614 Online.cubicBezierDeriv2 = function (p0, p1, p2, p3, t)
4615 {
4616 if (t < 0)
4617 { t = 0; }
4618 if (t > 1)
4619 { t = 1; }
4620 var u = 1 - t;
4621 // C''(t) = 6(1−t)(P2−2P1+P0) + 6t(P3−2P2+P1)
4622 return {
4623 x: 6 * u * (p2.x - 2 * p1.x + p0.x) + 6 * t * (p3.x - 2 * p2.x + p1.x),
4624 y: 6 * u * (p2.y - 2 * p1.y + p0.y) + 6 * t * (p3.y - 2 * p2.y + p1.y)
4625 };
4626 };
4627
4628 /** Degree-5 Bézier. One polynomial ⇒ C^∞ on (0, 1) (t1138). */
4629 Online.quinticBezier = function (pts, t)
4630 {
4631 if (t < 0)
4632 { t = 0; }
4633 if (t > 1)
4634 { t = 1; }
4635 var u = 1 - t;
4636 var u2 = u * u, u3 = u2 * u, u4 = u3 * u, u5 = u4 * u;
4637 var t2 = t * t, t3 = t2 * t, t4 = t3 * t, t5 = t4 * t;
4638 var b0 = u5, b1 = 5 * u4 * t, b2 = 10 * u3 * t2;
4639 var b3 = 10 * u2 * t3, b4 = 5 * u * t4, b5 = t5;
4640 return {
4641 x: b0 * pts[0].x + b1 * pts[1].x + b2 * pts[2].x
4642 + b3 * pts[3].x + b4 * pts[4].x + b5 * pts[5].x,
4643 y: b0 * pts[0].y + b1 * pts[1].y + b2 * pts[2].y
4644 + b3 * pts[3].y + b4 * pts[4].y + b5 * pts[5].y
4645 };
4646 };
4647
4648 Online.quinticBezierDeriv = function (pts, t)
4649 {
4650 if (t < 0)
4651 { t = 0; }
4652 if (t > 1)
4653 { t = 1; }
4654 var d = [];
4655 for (var i = 0; i < 5; i++)
4656 { d.push({ x: pts[i + 1].x - pts[i].x, y: pts[i + 1].y - pts[i].y }); }
4657 var u = 1 - t;
4658 var u2 = u * u, u3 = u2 * u, u4 = u3 * u;
4659 var t2 = t * t, t3 = t2 * t, t4 = t3 * t;
4660 var b0 = u4, b1 = 4 * u3 * t, b2 = 6 * u2 * t2, b3 = 4 * u * t3, b4 = t4;
4661 return {
4662 x: 5 * (b0 * d[0].x + b1 * d[1].x + b2 * d[2].x + b3 * d[3].x + b4 * d[4].x),
4663 y: 5 * (b0 * d[0].y + b1 * d[1].y + b2 * d[2].y + b3 * d[3].y + b4 * d[4].y)
4664 };
4665 };
4666
4667 Online.quinticBezierDeriv2 = function (pts, t)
4668 {
4669 if (t < 0)
4670 { t = 0; }
4671 if (t > 1)
4672 { t = 1; }
4673 var d2 = [];
4674 for (var i = 0; i < 4; i++)
4675 {
4676 d2.push({
4677 x: pts[i + 2].x - 2 * pts[i + 1].x + pts[i].x,
4678 y: pts[i + 2].y - 2 * pts[i + 1].y + pts[i].y
4679 });
4680 }
4681 var u = 1 - t;
4682 var u2 = u * u, u3 = u2 * u;
4683 var t2 = t * t, t3 = t2 * t;
4684 var b0 = u3, b1 = 3 * u2 * t, b2 = 3 * u * t2, b3 = t3;
4685 return {
4686 x: 20 * (b0 * d2[0].x + b1 * d2[1].x + b2 * d2[2].x + b3 * d2[3].x),
4687 y: 20 * (b0 * d2[0].y + b1 * d2[1].y + b2 * d2[2].y + b3 * d2[3].y)
4688 };
4689 };
4690
4691 /** Signed curvature κ = (x'y'' − y'x'') / |r'|³ (independent of parametrisation). */
4692 Online.curvature = function (d1, d2)
4693 {
4694 var sp = d1.x * d1.x + d1.y * d1.y;
4695 var den = Math.pow(sp, 1.5);
4696 if (den < 1e-12)
4697 { return 0; }
4698 return (d1.x * d2.y - d1.y * d2.x) / den;
4699 };
4700
4701 /**
4702 * de Casteljau evaluation of a Bézier of any degree, plus first
4703 * and second derivatives (t1138). A single polynomial is C^∞ on (0, 1).
4704 */
4705 Online.bezierPoint = function (pts, t)
4706 {
4707 if (!pts || !pts.length)
4708 { return { x: 0, y: 0 }; }
4709 if (t < 0)
4710 { t = 0; }
4711 if (t > 1)
4712 { t = 1; }
4713 var tmp = [];
4714 var i, k, u = 1 - t;
4715 for (i = 0; i < pts.length; i++)
4716 { tmp.push({ x: pts[i].x, y: pts[i].y }); }
4717 for (k = 1; k < pts.length; k++)
4718 {
4719 for (i = 0; i < pts.length - k; i++)
4720 {
4721 tmp[i] = {
4722 x: u * tmp[i].x + t * tmp[i + 1].x,
4723 y: u * tmp[i].y + t * tmp[i + 1].y
4724 };
4725 }
4726 }
4727 return tmp[0];
4728 };
4729
4730 Online.bezierDeriv = function (pts, t)
4731 {
4732 var n = pts ? pts.length - 1 : 0;
4733 if (n < 1)
4734 { return { x: 0, y: 0 }; }
4735 var d = [];
4736 for (var i = 0; i < n; i++)
4737 {
4738 d.push({
4739 x: n * (pts[i + 1].x - pts[i].x),
4740 y: n * (pts[i + 1].y - pts[i].y)
4741 });
4742 }
4743 return this.bezierPoint(d, t);
4744 };
4745
4746 Online.bezierDeriv2 = function (pts, t)
4747 {
4748 var n = pts ? pts.length - 1 : 0;
4749 if (n < 2)
4750 { return { x: 0, y: 0 }; }
4751 var s = n * (n - 1);
4752 var d2 = [];
4753 for (var i = 0; i < n - 1; i++)
4754 {
4755 d2.push({
4756 x: s * (pts[i + 2].x - 2 * pts[i + 1].x + pts[i].x),
4757 y: s * (pts[i + 2].y - 2 * pts[i + 1].y + pts[i].y)
4758 });
4759 }
4760 return this.bezierPoint(d2, t);
4761 };
4762
4614 4763 /**
4615 4764 * Hermite parameter with p(0)=0, p(1)=1, p'(0)=v0, p'(1)=v1.
4616 4765 * v0=v1=1 is linear (constant speed in t, no endpoint halt).
@@ -4631,29 +4780,18 @@ Online.smoothParam = function (t, v0, v1)
4631 4780 };
4632 4781
4633 4782 /**
4634 * A simple up-and-over cubic, used when there is no field to route
4635 * around (enemy→enemy) or both ends already sit above the apex.
4636 */
4637 Online._singleCubic = function (start, end, apexY, opts)
4638 {
4639 opts = opts || {};
4640 var rise = Math.max(40, Math.abs(start.y - apexY) * 0.9);
4641 if (opts.startSpeedPx)
4642 { rise = Math.max(rise, opts.startSpeedPx); }
4643 var fall = Math.max(40, Math.abs(end.y - apexY) * 0.9);
4644 if (opts.endSpeedPx)
4645 { fall = Math.max(40, opts.endSpeedPx); }
4646 var p1 = { x: start.x, y: Math.min(start.y - rise, apexY) };
4647 var p2 = { x: end.x, y: Math.min(end.y - fall, apexY) };
4648 return { p0: start, p1: p1, p2: p2, p3: end, apexY: apexY };
4649 };
4650
4651 /**
4652 * Smooth C¹ path: rise up (launch column or outside), cruise above
4653 * the field, dive into the landing column (t1134 / t1135). Joints
4654 * are quarter-turn cubics with matching tangents, so there are no
4655 * corners and no speed jerks. The curve may use the launch and
4656 * landing corridors but does not cut across the field interior.
4783 * One continuous degree-7 Bézier: a single big, round, ballistic
4784 * arch (t1138). Vertical at the exit (heading up) and at the
4785 * landing (heading down). Horizontal travel sits on the apex,
4786 * above the field — a cluster of high control points on each
4787 * side keeps the hull out of the interior, with no straight
4788 * cruise and no quarter-turn kinks. C^∞ on (0, 1).
4789 *
4790 * Incoming (low mini → high field): the first mid controls stay
4791 * on the outside so the rise clears the top before crossing.
4792 * Outgoing (high exit → low mini): mid controls sit above the
4793 * target so the dive happens outside. Enemy→enemy (no field)
4794 * is a symmetric parabola behind the field.
4657 4795 */
4658 4796 Online.makeAroundCurve = function (start, end, field, opts)
4659 4797 {
@@ -4662,191 +4800,289 @@ Online.makeAroundCurve = function (start, end, field, opts)
4662 4800 { start = { x: 0, y: 0 }; }
4663 4801 if (!end)
4664 4802 { end = { x: 0, y: 0 }; }
4665 var clearance = opts.clearance || 80;
4666 if (!field)
4803 var clearance = opts.clearance || 150;
4804 if (typeof MiniLayout !== 'undefined' && MiniLayout.ARC_TOP_PAD)
4805 { clearance = Math.max(clearance, MiniLayout.ARC_TOP_PAD); }
4806 if (clearance < 150)
4807 { clearance = 150; }
4808
4809 var apexY;
4810 var left, right, top;
4811 if (field)
4667 4812 {
4668 var looseApex = Math.min(start.y, end.y) - Math.max(40, clearance * 0.5);
4669 if (looseApex < 8)
4670 { looseApex = Math.min(start.y, end.y) - 24; }
4671 return this._singleCubic(start, end, looseApex, opts);
4813 top = field.top;
4814 left = field.left;
4815 right = field.right;
4816 apexY = top - clearance;
4817 if (apexY < 8)
4818 { apexY = Math.max(8, top - Math.max(28, clearance * 0.5)); }
4819 }
4820 else
4821 {
4822 apexY = Math.min(start.y, end.y) - Math.max(56, clearance * 0.6);
4823 if (apexY < 8)
4824 { apexY = Math.min(start.y, end.y) - 40; }
4825 left = right = top = null;
4672 4826 }
4673 var top = field.top;
4674 var left = field.left;
4675 var right = field.right;
4676 var apexY = top - clearance;
4677 if (apexY < 8)
4678 { apexY = Math.max(8, top - Math.max(24, clearance * 0.5)); }
4679
4680 if (start.y <= apexY + 8 && end.y <= apexY + 8)
4681 { return this._singleCubic(start, end, apexY, opts); }
4682 4827
4683 var pad = 14;
4684 var dir = end.x >= start.x ? 1 : -1;
4685 var span = Math.abs(end.x - start.x);
4686 var horiz = Math.max(28, Math.min(80, span * 0.15 + 8));
4687 4828 function outsideX (x)
4688 4829 {
4830 if (!field)
4831 { return x; }
4832 var pad = 40;
4689 4833 if (x < left + 8)
4690 4834 { return Math.min(x, left - pad); }
4691 4835 if (x > right - 8)
4692 4836 { return Math.max(x, right + pad); }
4693 4837 return x;
4694 4838 }
4695 var riseX = outsideX(start.x);
4696 var diveX = outsideX(end.x);
4697 var segments = [];
4698 4839
4699 // Rise ends already turned toward the cruise, at apex.
4700 var riseEnd = { x: riseX + dir * horiz, y: apexY };
4701 if (start.y > apexY + 4)
4702 {
4703 var p1 = { x: start.x, y: apexY };
4704 var p2 = { x: riseX, y: apexY };
4705 segments.push({ p0: start, p1: p1, p2: p2, p3: riseEnd });
4840 var startLow = field && start.y > top + 8;
4841 var endLow = field && end.y > top + 8;
4842 var startInside = field && start.x > left + 8 && start.x < right - 8;
4843 var endInside = field && end.x > left + 8 && end.x < right - 8;
4844 // Incoming: rise on the outside first. Outgoing: cross at
4845 // the apex, then dive outside. Otherwise a symmetric arch.
4846 var kind;
4847 if (startLow && !endLow)
4848 { kind = 'in'; }
4849 else if (!startLow && endLow)
4850 { kind = 'out'; }
4851 else if (startLow && endLow)
4852 { kind = startInside ? 'out' : (endInside ? 'in' : 'sym'); }
4853 else
4854 { kind = 'sym'; }
4855
4856 var sx = outsideX(start.x);
4857 var ex = outsideX(end.x);
4858 var rise = Math.max(80, Math.abs(start.y - apexY) * 0.35);
4859 var P0 = { x: start.x, y: start.y };
4860 var P1 = { x: start.x, y: start.y - rise };
4861 var P2, P3, P4, P5;
4862 var P6 = { x: end.x, y: apexY };
4863 var P7 = { x: end.x, y: end.y };
4864 if (kind === 'in')
4865 {
4866 P2 = { x: sx, y: apexY };
4867 P3 = { x: sx, y: apexY };
4868 P4 = { x: sx + (ex - sx) * 0.5, y: apexY };
4869 P5 = { x: sx + (ex - sx) * 0.85, y: apexY };
4870 }
4871 else if (kind === 'out')
4872 {
4873 P2 = { x: start.x, y: apexY };
4874 P3 = { x: sx + (ex - sx) * 0.55, y: apexY };
4875 P4 = { x: ex, y: apexY };
4876 P5 = { x: end.x, y: apexY };
4706 4877 }
4707 4878 else
4708 { riseEnd = { x: start.x, y: Math.min(start.y, apexY) }; }
4709
4710 // Cruise stays on the apex line (convex hull is above the field).
4711 var diveStart = { x: diveX - dir * horiz, y: apexY };
4712 if (Math.abs(riseEnd.x - diveStart.x) > 4 || Math.abs(riseEnd.y - diveStart.y) > 4)
4713 {
4714 // Keep the cruise direction consistent; if the two joints
4715 // would cross, just connect them directly.
4716 var cdir = diveStart.x >= riseEnd.x ? 1 : -1;
4717 var c1 = { x: riseEnd.x + cdir * horiz, y: riseEnd.y };
4718 if (c1.y > apexY)
4719 { c1.y = apexY; }
4720 var c2 = { x: diveStart.x - cdir * horiz, y: diveStart.y };
4721 segments.push({ p0: { x: riseEnd.x, y: riseEnd.y }, p1: c1, p2: c2, p3: diveStart });
4722 }
4723
4724 if (end.y > apexY + 4 || Math.abs(end.x - (diveStart.x + dir * horiz)) > 2)
4725 {
4726 var u0 = (Math.abs(riseEnd.x - diveStart.x) > 4 || segments.length === 0)
4727 ? diveStart
4728 : riseEnd;
4729 if (segments.length && segments[segments.length - 1].p3.x === u0.x
4730 && segments[segments.length - 1].p3.y === u0.y)
4731 { /* already joined */ }
4732 else if (segments.length === 0)
4733 { u0 = riseEnd; }
4734 var u1 = { x: u0.x + dir * horiz, y: u0.y };
4735 var fall = Math.max(24, Math.abs(end.y - apexY) * 0.45);
4736 if (opts.endSpeedPx)
4737 {
4738 // |P3-P2| so arrival ≈ fallSpeed after equal-param scaling.
4739 // velocity = 3*|P3-P2|*n*v1/duration; endSpeedPx is
4740 // (cells*px*duration)/3, so |P3-P2| = endSpeedPx/(n*v1).
4741 var nEst = Math.max(1, segments.length + 1);
4742 fall = Math.max(20, opts.endSpeedPx / (nEst * 0.85));
4743 }
4744 var u2 = { x: end.x, y: end.y - fall };
4745 // P2 may sit between the apex and the end (slower arrival);
4746 // only clamp if it would pass the end or fly far past the apex.
4747 if (u2.y < apexY - 240)
4748 { u2.y = apexY - 240; }
4749 if (u2.y > end.y - 8)
4750 { u2.y = end.y - 8; }
4751 segments.push({ p0: u0, p1: u1, p2: u2, p3: end });
4752 }
4753
4754 if (segments.length === 0)
4755 { return this._singleCubic(start, end, apexY, opts); }
4756 if (segments.length === 1 && !segments[0].p0)
4757 { return this._singleCubic(start, end, apexY, opts); }
4758
4759 return { segments: segments, apexY: apexY, p0: start, p3: end };
4879 {
4880 P2 = { x: sx, y: apexY };
4881 P3 = { x: sx + (ex - sx) * 0.35, y: apexY };
4882 P4 = { x: sx + (ex - sx) * 0.65, y: apexY };
4883 P5 = { x: ex, y: apexY };
4884 }
4885 var pts = [P0, P1, P2, P3, P4, P5, P6, P7];
4886
4887 var curve = {
4888 degree: 7,
4889 pts: pts,
4890 p0: P0,
4891 p3: P7,
4892 apexY: apexY,
4893 kind: kind,
4894 arcLength: true
4895 };
4896 this._buildArcLut(curve);
4897 return curve;
4760 4898 };
4761 4899
4762 Online._measureCurve = function (curve)
4900 Online._evalPoly = function (curve, t)
4763 4901 {
4764 if (!curve.segments)
4765 { return curve; }
4766 var lens = [];
4767 var total = 0;
4768 for (var i = 0; i < curve.segments.length; i++)
4902 if (curve && curve.pts && curve.pts.length >= 2)
4903 { return this.bezierPoint(curve.pts, t); }
4904 if (curve && curve.p1)
4905 { return this.cubicBezier(curve.p0, curve.p1, curve.p2, curve.p3, t); }
4906 if (curve && curve.segments)
4769 4907 {
4770 var L = this._segLen(curve.segments[i]);
4771 lens.push(L);
4772 total += L;
4908 var n = curve.segments.length;
4909 var at = (t < 0 ? 0 : (t > 1 ? 1 : t)) * n;
4910 var i = Math.floor(at);
4911 if (i >= n)
4912 { i = n - 1; }
4913 var s = curve.segments[i];
4914 return this.cubicBezier(s.p0, s.p1, s.p2, s.p3, at - i);
4915 }
4916 return { x: 0, y: 0 };
4917 };
4918
4919 Online._evalPolyDeriv = function (curve, t)
4920 {
4921 if (curve && curve.pts && curve.pts.length >= 2)
4922 { return this.bezierDeriv(curve.pts, t); }
4923 if (curve && curve.p1)
4924 { return this.cubicBezierDeriv(curve.p0, curve.p1, curve.p2, curve.p3, t); }
4925 return { x: 0, y: 0 };
4926 };
4927
4928 Online._evalPolyDeriv2 = function (curve, t)
4929 {
4930 if (curve && curve.pts && curve.pts.length >= 3)
4931 { return this.bezierDeriv2(curve.pts, t); }
4932 if (curve && curve.p1)
4933 { return this.cubicBezierDeriv2(curve.p0, curve.p1, curve.p2, curve.p3, t); }
4934 return { x: 0, y: 0 };
4935 };
4936
4937 /** Cumulative-length LUT so u is a fraction of arc length (t1138). */
4938 Online._buildArcLut = function (curve, steps)
4939 {
4940 steps = steps || 48;
4941 var acc = [0];
4942 var prev = this._evalPoly(curve, 0);
4943 for (var i = 1; i <= steps; i++)
4944 {
4945 var p = this._evalPoly(curve, i / steps);
4946 acc.push(acc[i - 1] + Math.sqrt((p.x - prev.x) * (p.x - prev.x)
4947 + (p.y - prev.y) * (p.y - prev.y)));
4948 prev = p;
4949 }
4950 curve.lutS = acc;
4951 curve.lutN = steps;
4952 curve.totalLen = Math.max(acc[steps], 1);
4953 return curve;
4954 };
4955
4956 /** Polynomial t whose arc length fraction is u. */
4957 Online._arcT = function (curve, u)
4958 {
4959 if (u <= 0)
4960 { return 0; }
4961 if (u >= 1)
4962 { return 1; }
4963 if (!curve.lutS)
4964 { this._buildArcLut(curve); }
4965 var s = u * curve.totalLen;
4966 var acc = curve.lutS;
4967 var n = curve.lutN;
4968 var lo = 0;
4969 var hi = n;
4970 while (lo < hi)
4971 {
4972 var mid = (lo + hi) >> 1;
4973 if (acc[mid] < s)
4974 { lo = mid + 1; }
4975 else
4976 { hi = mid; }
4773 4977 }
4774 curve.lens = lens;
4775 curve.totalLen = Math.max(total, 1);
4978 var i = lo;
4979 if (i <= 0)
4980 { return 0; }
4981 var s0 = acc[i - 1];
4982 var s1 = acc[i];
4983 var f = (s1 > s0) ? (s - s0) / (s1 - s0) : 0;
4984 return ((i - 1) + f) / n;
4985 };
4986
4987 Online._measureCurve = function (curve)
4988 {
4989 if (!curve)
4990 { return curve; }
4991 if (!curve.lutS)
4992 { this._buildArcLut(curve); }
4776 4993 return curve;
4777 4994 };
4778 4995
4779 4996 Online._segLen = function (seg, steps)
4780 4997 {
4781 4998 steps = steps || 10;
4999 if (seg && seg.degree === 5)
5000 {
5001 if (!seg.lutS)
5002 { this._buildArcLut(seg, steps * 4); }
5003 return seg.totalLen;
5004 }
4782 5005 var len = 0;
4783 5006 var prev = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, 0);
4784 5007 for (var i = 1; i <= steps; i++)
4785 5008 {
4786 5009 var p = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, i / steps);
4787 var dx = p.x - prev.x;
4788 var dy = p.y - prev.y;
4789 len += Math.sqrt(dx * dx + dy * dy);
5010 len += Math.sqrt((p.x - prev.x) * (p.x - prev.x) + (p.y - prev.y) * (p.y - prev.y));
4790 5011 prev = p;
4791 5012 }
4792 5013 return Math.max(len, 1);
4793 5014 };
4794 5015
5016 /** Point at arc-length fraction u ∈ [0, 1]. */
5017 Online.curvePoint = function (curve, u)
5018 {
5019 if (!curve)
5020 { return { x: 0, y: 0 }; }
5021 var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));
5022 return this._evalPoly(curve, t);
5023 };
5024
4795 5025 /**
4796 * Equal-parameter split across segments so matching local C'(t)
4797 * at joints also matches dP/du (no speed jerk).
5026 * dP/du under arc-length parametrisation: unit tangent × total length
5027 * (constant speed in u, direction from the polynomial).
4798 5028 */
4799 Online._curveSegmentAt = function (curve, u)
5029 Online.curveDeriv = function (curve, u)
4800 5030 {
4801 5031 if (!curve)
4802 { return null; }
4803 if (!curve.segments)
4804 {
4805 return { seg: curve, t: u < 0 ? 0 : (u > 1 ? 1 : u), scale: 1 };
5032 { return { x: 0, y: 0 }; }
5033 var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));
5034 var d = this._evalPolyDeriv(curve, t);
5035 if (curve.arcLength !== false)
5036 {
5037 if (!curve.lutS)
5038 { this._buildArcLut(curve); }
5039 var mag = Math.sqrt(d.x * d.x + d.y * d.y);
5040 if (mag < 1e-9)
5041 { return { x: 0, y: 0 }; }
5042 var s = curve.totalLen / mag;
5043 return { x: d.x * s, y: d.y * s };
4806 5044 }
4807 var segs = curve.segments;
4808 var n = segs.length;
4809 if (u <= 0)
4810 { return { seg: segs[0], t: 0, scale: n }; }
4811 if (u >= 1)
4812 { return { seg: segs[n - 1], t: 1, scale: n }; }
4813 var at = u * n;
4814 var i = Math.floor(at);
4815 if (i >= n)
4816 { i = n - 1; }
4817 var t = at - i;
4818 return { seg: segs[i], t: t, scale: n };
5045 return d;
4819 5046 };
4820 5047
4821 /** Point on a (possibly multi-segment C¹) curve at u ∈ [0, 1]. */
4822 Online.curvePoint = function (curve, u)
5048 Online.curveDeriv2 = function (curve, u)
4823 5049 {
4824 var at = this._curveSegmentAt(curve, u);
4825 if (!at)
5050 if (!curve)
4826 5051 { return { x: 0, y: 0 }; }
4827 var s = at.seg;
4828 if (s && s.p1)
4829 { return this.cubicBezier(s.p0, s.p1, s.p2, s.p3, at.t); }
4830 if (s && s.p0)
4831 { return { x: s.p0.x, y: s.p0.y }; }
4832 return { x: 0, y: 0 };
5052 var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));
5053 return this._evalPolyDeriv2(curve, t);
4833 5054 };
4834 5055
4835 /** dP/du of the curve (C¹ across joints). */
4836 Online.curveDeriv = function (curve, u)
5056 /** Curvature at arc-length fraction u (uses polynomial t, κ is invariant). */
5057 Online.curveCurvature = function (curve, u)
4837 5058 {
4838 var at = this._curveSegmentAt(curve, u);
4839 if (!at || !at.seg || !at.seg.p1)
4840 { return { x: 0, y: 0 }; }
4841 var s = at.seg;
4842 var d = this.cubicBezierDeriv(s.p0, s.p1, s.p2, s.p3, at.t);
4843 return { x: d.x * at.scale, y: d.y * at.scale };
5059 var t = curve && curve.arcLength !== false ? this._arcT(curve, u) : u;
5060 return this.curvature(this._evalPolyDeriv(curve, t), this._evalPolyDeriv2(curve, t));
4844 5061 };
4845 5062
4846 5063 /**
4847 * Sample the smooth around-field curve at u ∈ [0, 1] (t1134).
4848 * `clearance` may be a number or an options object.
5064 * Endpoint ease rates so arrival speed ≈ fallSpeed under arc-length
5065 * parametrisation: v = (L / duration) * p'(raw).
4849 5066 */
5067 Online.flightEaseRates = function (curve, duration, fallPx)
5068 {
5069 if (!curve || !curve.lutS)
5070 { this._measureCurve(curve); }
5071 var L = (curve && curve.totalLen) || 400;
5072 duration = duration || this.FLIGHT_DURATION;
5073 var v1 = fallPx && L ? (fallPx * duration) / L : 0.75;
5074 if (v1 < 0.28)
5075 { v1 = 0.28; }
5076 if (v1 > 1.15)
5077 { v1 = 1.15; }
5078 var v0 = v1 + 0.2;
5079 if (v0 > 1)
5080 { v0 = 1; }
5081 if (v0 < 0.45)
5082 { v0 = 0.45; }
5083 return { v0: v0, v1: v1 };
5084 };
5085
4850 5086 Online.aroundFieldPoint = function (start, end, u, field, clearance)
4851 5087 {
4852 5088 var opts = (clearance && typeof clearance === 'object') ? clearance : { clearance: clearance };
@@ -4854,17 +5090,11 @@ Online.aroundFieldPoint = function (start, end, u, field, clearance)
4854 5090 return this.curvePoint(curve, u);
4855 5091 };
4856 5092
4857 /** Guide points (start, joints, end) for older tests. */
4858 5093 Online.waypointsAroundField = function (start, end, field, clearance)
4859 5094 {
4860 var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 64 });
4861 if (c.segments && c.segments.length)
4862 {
4863 var pts = [{ x: c.segments[0].p0.x, y: c.segments[0].p0.y }];
4864 for (var i = 0; i < c.segments.length; i++)
4865 { pts.push({ x: c.segments[i].p3.x, y: c.segments[i].p3.y }); }
4866 return pts;
4867 }
5095 var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 100 });
5096 if (c.pts && c.pts.length)
5097 { return c.pts.slice(); }
4868 5098 return [c.p0, c.p1, c.p2, c.p3];
4869 5099 };
4870 5100
@@ -5148,14 +5378,12 @@ Online.spawnFlights = function (results, locals)
5148 5378 : this.arcHeight(start, end);
5149 5379 var field = this.playerFieldRect();
5150 5380 var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');
5151 var clearance = Math.max(64, height * 0.45);
5381 var clearance = Math.max(150, height * 0.55);
5152 5382 var endSpeed = this._fallSpeedPx(field, duration / 1000);
5153 5383 var curve = miss ? null : this.makeAroundCurve(start, end, around ? field : null, {
5154 clearance: clearance,
5155 startSpeedPx: endSpeed,
5156 endSpeedPx: endSpeed,
5157 duration: duration / 1000
5384 clearance: clearance
5158 5385 });
5386 var easeRates = curve ? this.flightEaseRates(curve, duration / 1000, endSpeed) : null;
5159 5387 this._flights.push({
5160 5388 el: el,
5161 5389 start: start,
@@ -5164,6 +5392,7 @@ Online.spawnFlights = function (results, locals)
5164 5392 height: height,
5165 5393 t0: t0,
5166 5394 duration: duration,
5395 easeRates: easeRates,
5167 5396 spin: miss ? ((Math.random() * 2 - 1) * 720) : ((Math.random() * 2 - 1) * 120),
5168 5397 miss: miss,
5169 5398 kind: kind,
@@ -5314,10 +5543,9 @@ Online.previewExit = function (info)
5314 5543 front.appendChild(el);
5315 5544 var endSpeed = this._fallSpeedPx(field, this.FLIGHT_DURATION);
5316 5545 var curve = this.makeAroundCurve(start, end, info.from_id ? null : field, {
5317 clearance: clearance,
5318 startSpeedPx: endSpeed,
5319 endSpeedPx: endSpeed
5546 clearance: clearance
5320 5547 });
5548 var easeRates = this.flightEaseRates(curve, this.FLIGHT_DURATION, endSpeed);
5321 5549 var now = performance.now();
5322 5550 var mini = info.from_id ? (this._miniCellSize(info.from_id) / full) : 1;
5323 5551 if (mini < 0.12)
@@ -5340,6 +5568,7 @@ Online.previewExit = function (info)
5340 5568 landCol: null,
5341 5569 field: field,
5342 5570 curve: curve,
5571 easeRates: easeRates,
5343 5572 preview: true,
5344 5573 color: info.color,
5345 5574 from_id: info.from_id || null
@@ -5431,19 +5660,19 @@ Online.retargetFlight = function (f, r, meta, now)
5431 5660 var height = miss
5432 5661 ? Math.max(40, Math.min(160, Math.abs(end.y - pt.y) * 0.35))
5433 5662 : this.arcHeight(pt, end);
5434 var clearance = Math.max(64, height * 0.45);
5663 var clearance = Math.max(150, height * 0.55);
5435 5664 var endSpeed = this._fallSpeedPx(field, duration / 1000);
5436 5665 var curve = miss ? null : this.makeAroundCurve(pt, end, around ? field : null, {
5437 clearance: clearance,
5438 startSpeedPx: endSpeed,
5439 endSpeedPx: endSpeed
5666 clearance: clearance
5440 5667 });
5668 var easeRates = curve ? this.flightEaseRates(curve, duration / 1000, endSpeed) : null;
5441 5669
5442 5670 f.start = pt;
5443 5671 f.end = end;
5444 5672 f.t0 = now;
5445 5673 f.duration = duration;
5446 5674 f.curve = curve;
5675 f.easeRates = easeRates;
5447 5676 f.ctrl = miss ? this._missCtrl(pt, end) : null;
5448 5677 f.height = height;
5449 5678 f.miss = miss;
@@ -5521,8 +5750,10 @@ Online.tickFlights = function (now)
5521 5750 }
5522 5751 if (raw < 0)
5523 5752 { raw = 0; }
5524 // Non-zero endpoint speed so the handoff matches fallSpeed (t1135).
5525 var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, 0.85, 0.85);
5753 // Non-zero endpoint speed so the handoff matches fallSpeed (t1135/t1138).
5754 var v0 = (f.easeRates && f.easeRates.v0 != null) ? f.easeRates.v0 : 0.85;
5755 var v1 = (f.easeRates && f.easeRates.v1 != null) ? f.easeRates.v1 : 0.85;
5756 var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, v0, v1);
5526 5757 var x, y;
5527 5758 if (f.miss && f.ctrl)
5528 5759 {
@@ -4611,6 +4611,155 @@ Online.cubicBezierDeriv = function (p0, p1, p2, p3, t)
4611 };
4611 };
4612};
4612};
4613
4613
4614Online.cubicBezierDeriv2 = function (p0, p1, p2, p3, t)
4615{
4616 if (t < 0)
4617 { t = 0; }
4618 if (t > 1)
4619 { t = 1; }
4620 var u = 1 - t;
4621 // C''(t) = 6(1−t)(P2−2P1+P0) + 6t(P3−2P2+P1)
4622 return {
4623 x: 6 * u * (p2.x - 2 * p1.x + p0.x) + 6 * t * (p3.x - 2 * p2.x + p1.x),
4624 y: 6 * u * (p2.y - 2 * p1.y + p0.y) + 6 * t * (p3.y - 2 * p2.y + p1.y)
4625 };
4626};
4627
4628/** Degree-5 Bézier. One polynomial ⇒ C^∞ on (0, 1) (t1138). */
4629Online.quinticBezier = function (pts, t)
4630{
4631 if (t < 0)
4632 { t = 0; }
4633 if (t > 1)
4634 { t = 1; }
4635 var u = 1 - t;
4636 var u2 = u * u, u3 = u2 * u, u4 = u3 * u, u5 = u4 * u;
4637 var t2 = t * t, t3 = t2 * t, t4 = t3 * t, t5 = t4 * t;
4638 var b0 = u5, b1 = 5 * u4 * t, b2 = 10 * u3 * t2;
4639 var b3 = 10 * u2 * t3, b4 = 5 * u * t4, b5 = t5;
4640 return {
4641 x: b0 * pts[0].x + b1 * pts[1].x + b2 * pts[2].x
4642 + b3 * pts[3].x + b4 * pts[4].x + b5 * pts[5].x,
4643 y: b0 * pts[0].y + b1 * pts[1].y + b2 * pts[2].y
4644 + b3 * pts[3].y + b4 * pts[4].y + b5 * pts[5].y
4645 };
4646};
4647
4648Online.quinticBezierDeriv = function (pts, t)
4649{
4650 if (t < 0)
4651 { t = 0; }
4652 if (t > 1)
4653 { t = 1; }
4654 var d = [];
4655 for (var i = 0; i < 5; i++)
4656 { d.push({ x: pts[i + 1].x - pts[i].x, y: pts[i + 1].y - pts[i].y }); }
4657 var u = 1 - t;
4658 var u2 = u * u, u3 = u2 * u, u4 = u3 * u;
4659 var t2 = t * t, t3 = t2 * t, t4 = t3 * t;
4660 var b0 = u4, b1 = 4 * u3 * t, b2 = 6 * u2 * t2, b3 = 4 * u * t3, b4 = t4;
4661 return {
4662 x: 5 * (b0 * d[0].x + b1 * d[1].x + b2 * d[2].x + b3 * d[3].x + b4 * d[4].x),
4663 y: 5 * (b0 * d[0].y + b1 * d[1].y + b2 * d[2].y + b3 * d[3].y + b4 * d[4].y)
4664 };
4665};
4666
4667Online.quinticBezierDeriv2 = function (pts, t)
4668{
4669 if (t < 0)
4670 { t = 0; }
4671 if (t > 1)
4672 { t = 1; }
4673 var d2 = [];
4674 for (var i = 0; i < 4; i++)
4675 {
4676 d2.push({
4677 x: pts[i + 2].x - 2 * pts[i + 1].x + pts[i].x,
4678 y: pts[i + 2].y - 2 * pts[i + 1].y + pts[i].y
4679 });
4680 }
4681 var u = 1 - t;
4682 var u2 = u * u, u3 = u2 * u;
4683 var t2 = t * t, t3 = t2 * t;
4684 var b0 = u3, b1 = 3 * u2 * t, b2 = 3 * u * t2, b3 = t3;
4685 return {
4686 x: 20 * (b0 * d2[0].x + b1 * d2[1].x + b2 * d2[2].x + b3 * d2[3].x),
4687 y: 20 * (b0 * d2[0].y + b1 * d2[1].y + b2 * d2[2].y + b3 * d2[3].y)
4688 };
4689};
4690
4691/** Signed curvature κ = (x'y'' − y'x'') / |r'|³ (independent of parametrisation). */
4692Online.curvature = function (d1, d2)
4693{
4694 var sp = d1.x * d1.x + d1.y * d1.y;
4695 var den = Math.pow(sp, 1.5);
4696 if (den < 1e-12)
4697 { return 0; }
4698 return (d1.x * d2.y - d1.y * d2.x) / den;
4699};
4700
4701/**
4702 * de Casteljau evaluation of a Bézier of any degree, plus first
4703 * and second derivatives (t1138). A single polynomial is C^∞ on (0, 1).
4704 */
4705Online.bezierPoint = function (pts, t)
4706{
4707 if (!pts || !pts.length)
4708 { return { x: 0, y: 0 }; }
4709 if (t < 0)
4710 { t = 0; }
4711 if (t > 1)
4712 { t = 1; }
4713 var tmp = [];
4714 var i, k, u = 1 - t;
4715 for (i = 0; i < pts.length; i++)
4716 { tmp.push({ x: pts[i].x, y: pts[i].y }); }
4717 for (k = 1; k < pts.length; k++)
4718 {
4719 for (i = 0; i < pts.length - k; i++)
4720 {
4721 tmp[i] = {
4722 x: u * tmp[i].x + t * tmp[i + 1].x,
4723 y: u * tmp[i].y + t * tmp[i + 1].y
4724 };
4725 }
4726 }
4727 return tmp[0];
4728};
4729
4730Online.bezierDeriv = function (pts, t)
4731{
4732 var n = pts ? pts.length - 1 : 0;
4733 if (n < 1)
4734 { return { x: 0, y: 0 }; }
4735 var d = [];
4736 for (var i = 0; i < n; i++)
4737 {
4738 d.push({
4739 x: n * (pts[i + 1].x - pts[i].x),
4740 y: n * (pts[i + 1].y - pts[i].y)
4741 });
4742 }
4743 return this.bezierPoint(d, t);
4744};
4745
4746Online.bezierDeriv2 = function (pts, t)
4747{
4748 var n = pts ? pts.length - 1 : 0;
4749 if (n < 2)
4750 { return { x: 0, y: 0 }; }
4751 var s = n * (n - 1);
4752 var d2 = [];
4753 for (var i = 0; i < n - 1; i++)
4754 {
4755 d2.push({
4756 x: s * (pts[i + 2].x - 2 * pts[i + 1].x + pts[i].x),
4757 y: s * (pts[i + 2].y - 2 * pts[i + 1].y + pts[i].y)
4758 });
4759 }
4760 return this.bezierPoint(d2, t);
4761};
4762
4614/**
4763/**
4615 * Hermite parameter with p(0)=0, p(1)=1, p'(0)=v0, p'(1)=v1.
4764 * Hermite parameter with p(0)=0, p(1)=1, p'(0)=v0, p'(1)=v1.
4616 * v0=v1=1 is linear (constant speed in t, no endpoint halt).
4765 * v0=v1=1 is linear (constant speed in t, no endpoint halt).
@@ -4631,29 +4780,18 @@ Online.smoothParam = function (t, v0, v1)
4631};
4780};
4632
4781
4633/**
4782/**
4634 * A simple up-and-over cubic, used when there is no field to route
4783 * One continuous degree-7 Bézier: a single big, round, ballistic
4635 * around (enemy→enemy) or both ends already sit above the apex.
4784 * arch (t1138). Vertical at the exit (heading up) and at the
4636 */
4785 * landing (heading down). Horizontal travel sits on the apex,
4637Online._singleCubic = function (start, end, apexY, opts)
4786 * above the field — a cluster of high control points on each
4638{
4787 * side keeps the hull out of the interior, with no straight
4639 opts = opts || {};
4788 * cruise and no quarter-turn kinks. C^∞ on (0, 1).
4640 var rise = Math.max(40, Math.abs(start.y - apexY) * 0.9);
4789 *
4641 if (opts.startSpeedPx)
4790 * Incoming (low mini → high field): the first mid controls stay
4642 { rise = Math.max(rise, opts.startSpeedPx); }
4791 * on the outside so the rise clears the top before crossing.
4643 var fall = Math.max(40, Math.abs(end.y - apexY) * 0.9);
4792 * Outgoing (high exit → low mini): mid controls sit above the
4644 if (opts.endSpeedPx)
4793 * target so the dive happens outside. Enemy→enemy (no field)
4645 { fall = Math.max(40, opts.endSpeedPx); }
4794 * is a symmetric parabola behind the field.
4646 var p1 = { x: start.x, y: Math.min(start.y - rise, apexY) };
4647 var p2 = { x: end.x, y: Math.min(end.y - fall, apexY) };
4648 return { p0: start, p1: p1, p2: p2, p3: end, apexY: apexY };
4649};
4650
4651/**
4652 * Smooth C¹ path: rise up (launch column or outside), cruise above
4653 * the field, dive into the landing column (t1134 / t1135). Joints
4654 * are quarter-turn cubics with matching tangents, so there are no
4655 * corners and no speed jerks. The curve may use the launch and
4656 * landing corridors but does not cut across the field interior.
4657 */
4795 */
4658Online.makeAroundCurve = function (start, end, field, opts)
4796Online.makeAroundCurve = function (start, end, field, opts)
4659{
4797{
@@ -4662,191 +4800,289 @@ Online.makeAroundCurve = function (start, end, field, opts)
4662 { start = { x: 0, y: 0 }; }
4800 { start = { x: 0, y: 0 }; }
4663 if (!end)
4801 if (!end)
4664 { end = { x: 0, y: 0 }; }
4802 { end = { x: 0, y: 0 }; }
4665 var clearance = opts.clearance || 80;
4803 var clearance = opts.clearance || 150;
4666 if (!field)
4804 if (typeof MiniLayout !== 'undefined' && MiniLayout.ARC_TOP_PAD)
4805 { clearance = Math.max(clearance, MiniLayout.ARC_TOP_PAD); }
4806 if (clearance < 150)
4807 { clearance = 150; }
4808
4809 var apexY;
4810 var left, right, top;
4811 if (field)
4667 {
4812 {
4668 var looseApex = Math.min(start.y, end.y) - Math.max(40, clearance * 0.5);
4813 top = field.top;
4669 if (looseApex < 8)
4814 left = field.left;
4670 { looseApex = Math.min(start.y, end.y) - 24; }
4815 right = field.right;
4671 return this._singleCubic(start, end, looseApex, opts);
4816 apexY = top - clearance;
4817 if (apexY < 8)
4818 { apexY = Math.max(8, top - Math.max(28, clearance * 0.5)); }
4819 }
4820 else
4821 {
4822 apexY = Math.min(start.y, end.y) - Math.max(56, clearance * 0.6);
4823 if (apexY < 8)
4824 { apexY = Math.min(start.y, end.y) - 40; }
4825 left = right = top = null;
4672 }
4826 }
4673 var top = field.top;
4674 var left = field.left;
4675 var right = field.right;
4676 var apexY = top - clearance;
4677 if (apexY < 8)
4678 { apexY = Math.max(8, top - Math.max(24, clearance * 0.5)); }
4679
4680 if (start.y <= apexY + 8 && end.y <= apexY + 8)
4681 { return this._singleCubic(start, end, apexY, opts); }
4682
4827
4683 var pad = 14;
4684 var dir = end.x >= start.x ? 1 : -1;
4685 var span = Math.abs(end.x - start.x);
4686 var horiz = Math.max(28, Math.min(80, span * 0.15 + 8));
4687 function outsideX (x)
4828 function outsideX (x)
4688 {
4829 {
4830 if (!field)
4831 { return x; }
4832 var pad = 40;
4689 if (x < left + 8)
4833 if (x < left + 8)
4690 { return Math.min(x, left - pad); }
4834 { return Math.min(x, left - pad); }
4691 if (x > right - 8)
4835 if (x > right - 8)
4692 { return Math.max(x, right + pad); }
4836 { return Math.max(x, right + pad); }
4693 return x;
4837 return x;
4694 }
4838 }
4695 var riseX = outsideX(start.x);
4696 var diveX = outsideX(end.x);
4697 var segments = [];
4698
4839
4699 // Rise ends already turned toward the cruise, at apex.
4840 var startLow = field && start.y > top + 8;
4700 var riseEnd = { x: riseX + dir * horiz, y: apexY };
4841 var endLow = field && end.y > top + 8;
4701 if (start.y > apexY + 4)
4842 var startInside = field && start.x > left + 8 && start.x < right - 8;
4702 {
4843 var endInside = field && end.x > left + 8 && end.x < right - 8;
4703 var p1 = { x: start.x, y: apexY };
4844 // Incoming: rise on the outside first. Outgoing: cross at
4704 var p2 = { x: riseX, y: apexY };
4845 // the apex, then dive outside. Otherwise a symmetric arch.
4705 segments.push({ p0: start, p1: p1, p2: p2, p3: riseEnd });
4846 var kind;
4847 if (startLow && !endLow)
4848 { kind = 'in'; }
4849 else if (!startLow && endLow)
4850 { kind = 'out'; }
4851 else if (startLow && endLow)
4852 { kind = startInside ? 'out' : (endInside ? 'in' : 'sym'); }
4853 else
4854 { kind = 'sym'; }
4855
4856 var sx = outsideX(start.x);
4857 var ex = outsideX(end.x);
4858 var rise = Math.max(80, Math.abs(start.y - apexY) * 0.35);
4859 var P0 = { x: start.x, y: start.y };
4860 var P1 = { x: start.x, y: start.y - rise };
4861 var P2, P3, P4, P5;
4862 var P6 = { x: end.x, y: apexY };
4863 var P7 = { x: end.x, y: end.y };
4864 if (kind === 'in')
4865 {
4866 P2 = { x: sx, y: apexY };
4867 P3 = { x: sx, y: apexY };
4868 P4 = { x: sx + (ex - sx) * 0.5, y: apexY };
4869 P5 = { x: sx + (ex - sx) * 0.85, y: apexY };
4870 }
4871 else if (kind === 'out')
4872 {
4873 P2 = { x: start.x, y: apexY };
4874 P3 = { x: sx + (ex - sx) * 0.55, y: apexY };
4875 P4 = { x: ex, y: apexY };
4876 P5 = { x: end.x, y: apexY };
4706 }
4877 }
4707 else
4878 else
4708 { riseEnd = { x: start.x, y: Math.min(start.y, apexY) }; }
4879 {
4709
4880 P2 = { x: sx, y: apexY };
4710 // Cruise stays on the apex line (convex hull is above the field).
4881 P3 = { x: sx + (ex - sx) * 0.35, y: apexY };
4711 var diveStart = { x: diveX - dir * horiz, y: apexY };
4882 P4 = { x: sx + (ex - sx) * 0.65, y: apexY };
4712 if (Math.abs(riseEnd.x - diveStart.x) > 4 || Math.abs(riseEnd.y - diveStart.y) > 4)
4883 P5 = { x: ex, y: apexY };
4713 {
4884 }
4714 // Keep the cruise direction consistent; if the two joints
4885 var pts = [P0, P1, P2, P3, P4, P5, P6, P7];
4715 // would cross, just connect them directly.
4886
4716 var cdir = diveStart.x >= riseEnd.x ? 1 : -1;
4887 var curve = {
4717 var c1 = { x: riseEnd.x + cdir * horiz, y: riseEnd.y };
4888 degree: 7,
4718 if (c1.y > apexY)
4889 pts: pts,
4719 { c1.y = apexY; }
4890 p0: P0,
4720 var c2 = { x: diveStart.x - cdir * horiz, y: diveStart.y };
4891 p3: P7,
4721 segments.push({ p0: { x: riseEnd.x, y: riseEnd.y }, p1: c1, p2: c2, p3: diveStart });
4892 apexY: apexY,
4722 }
4893 kind: kind,
4723
4894 arcLength: true
4724 if (end.y > apexY + 4 || Math.abs(end.x - (diveStart.x + dir * horiz)) > 2)
4895 };
4725 {
4896 this._buildArcLut(curve);
4726 var u0 = (Math.abs(riseEnd.x - diveStart.x) > 4 || segments.length === 0)
4897 return curve;
4727 ? diveStart
4728 : riseEnd;
4729 if (segments.length && segments[segments.length - 1].p3.x === u0.x
4730 && segments[segments.length - 1].p3.y === u0.y)
4731 { /* already joined */ }
4732 else if (segments.length === 0)
4733 { u0 = riseEnd; }
4734 var u1 = { x: u0.x + dir * horiz, y: u0.y };
4735 var fall = Math.max(24, Math.abs(end.y - apexY) * 0.45);
4736 if (opts.endSpeedPx)
4737 {
4738 // |P3-P2| so arrival ≈ fallSpeed after equal-param scaling.
4739 // velocity = 3*|P3-P2|*n*v1/duration; endSpeedPx is
4740 // (cells*px*duration)/3, so |P3-P2| = endSpeedPx/(n*v1).
4741 var nEst = Math.max(1, segments.length + 1);
4742 fall = Math.max(20, opts.endSpeedPx / (nEst * 0.85));
4743 }
4744 var u2 = { x: end.x, y: end.y - fall };
4745 // P2 may sit between the apex and the end (slower arrival);
4746 // only clamp if it would pass the end or fly far past the apex.
4747 if (u2.y < apexY - 240)
4748 { u2.y = apexY - 240; }
4749 if (u2.y > end.y - 8)
4750 { u2.y = end.y - 8; }
4751 segments.push({ p0: u0, p1: u1, p2: u2, p3: end });
4752 }
4753
4754 if (segments.length === 0)
4755 { return this._singleCubic(start, end, apexY, opts); }
4756 if (segments.length === 1 && !segments[0].p0)
4757 { return this._singleCubic(start, end, apexY, opts); }
4758
4759 return { segments: segments, apexY: apexY, p0: start, p3: end };
4760};
4898};
4761
4899
4762Online._measureCurve = function (curve)
4900Online._evalPoly = function (curve, t)
4763{
4901{
4764 if (!curve.segments)
4902 if (curve && curve.pts && curve.pts.length >= 2)
4765 { return curve; }
4903 { return this.bezierPoint(curve.pts, t); }
4766 var lens = [];
4904 if (curve && curve.p1)
4767 var total = 0;
4905 { return this.cubicBezier(curve.p0, curve.p1, curve.p2, curve.p3, t); }
4768 for (var i = 0; i < curve.segments.length; i++)
4906 if (curve && curve.segments)
4769 {
4907 {
4770 var L = this._segLen(curve.segments[i]);
4908 var n = curve.segments.length;
4771 lens.push(L);
4909 var at = (t < 0 ? 0 : (t > 1 ? 1 : t)) * n;
4772 total += L;
4910 var i = Math.floor(at);
4911 if (i >= n)
4912 { i = n - 1; }
4913 var s = curve.segments[i];
4914 return this.cubicBezier(s.p0, s.p1, s.p2, s.p3, at - i);
4915 }
4916 return { x: 0, y: 0 };
4917};
4918
4919Online._evalPolyDeriv = function (curve, t)
4920{
4921 if (curve && curve.pts && curve.pts.length >= 2)
4922 { return this.bezierDeriv(curve.pts, t); }
4923 if (curve && curve.p1)
4924 { return this.cubicBezierDeriv(curve.p0, curve.p1, curve.p2, curve.p3, t); }
4925 return { x: 0, y: 0 };
4926};
4927
4928Online._evalPolyDeriv2 = function (curve, t)
4929{
4930 if (curve && curve.pts && curve.pts.length >= 3)
4931 { return this.bezierDeriv2(curve.pts, t); }
4932 if (curve && curve.p1)
4933 { return this.cubicBezierDeriv2(curve.p0, curve.p1, curve.p2, curve.p3, t); }
4934 return { x: 0, y: 0 };
4935};
4936
4937/** Cumulative-length LUT so u is a fraction of arc length (t1138). */
4938Online._buildArcLut = function (curve, steps)
4939{
4940 steps = steps || 48;
4941 var acc = [0];
4942 var prev = this._evalPoly(curve, 0);
4943 for (var i = 1; i <= steps; i++)
4944 {
4945 var p = this._evalPoly(curve, i / steps);
4946 acc.push(acc[i - 1] + Math.sqrt((p.x - prev.x) * (p.x - prev.x)
4947 + (p.y - prev.y) * (p.y - prev.y)));
4948 prev = p;
4949 }
4950 curve.lutS = acc;
4951 curve.lutN = steps;
4952 curve.totalLen = Math.max(acc[steps], 1);
4953 return curve;
4954};
4955
4956/** Polynomial t whose arc length fraction is u. */
4957Online._arcT = function (curve, u)
4958{
4959 if (u <= 0)
4960 { return 0; }
4961 if (u >= 1)
4962 { return 1; }
4963 if (!curve.lutS)
4964 { this._buildArcLut(curve); }
4965 var s = u * curve.totalLen;
4966 var acc = curve.lutS;
4967 var n = curve.lutN;
4968 var lo = 0;
4969 var hi = n;
4970 while (lo < hi)
4971 {
4972 var mid = (lo + hi) >> 1;
4973 if (acc[mid] < s)
4974 { lo = mid + 1; }
4975 else
4976 { hi = mid; }
4773 }
4977 }
4774 curve.lens = lens;
4978 var i = lo;
4775 curve.totalLen = Math.max(total, 1);
4979 if (i <= 0)
4980 { return 0; }
4981 var s0 = acc[i - 1];
4982 var s1 = acc[i];
4983 var f = (s1 > s0) ? (s - s0) / (s1 - s0) : 0;
4984 return ((i - 1) + f) / n;
4985};
4986
4987Online._measureCurve = function (curve)
4988{
4989 if (!curve)
4990 { return curve; }
4991 if (!curve.lutS)
4992 { this._buildArcLut(curve); }
4776 return curve;
4993 return curve;
4777};
4994};
4778
4995
4779Online._segLen = function (seg, steps)
4996Online._segLen = function (seg, steps)
4780{
4997{
4781 steps = steps || 10;
4998 steps = steps || 10;
4999 if (seg && seg.degree === 5)
5000 {
5001 if (!seg.lutS)
5002 { this._buildArcLut(seg, steps * 4); }
5003 return seg.totalLen;
5004 }
4782 var len = 0;
5005 var len = 0;
4783 var prev = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, 0);
5006 var prev = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, 0);
4784 for (var i = 1; i <= steps; i++)
5007 for (var i = 1; i <= steps; i++)
4785 {
5008 {
4786 var p = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, i / steps);
5009 var p = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, i / steps);
4787 var dx = p.x - prev.x;
5010 len += Math.sqrt((p.x - prev.x) * (p.x - prev.x) + (p.y - prev.y) * (p.y - prev.y));
4788 var dy = p.y - prev.y;
4789 len += Math.sqrt(dx * dx + dy * dy);
4790 prev = p;
5011 prev = p;
4791 }
5012 }
4792 return Math.max(len, 1);
5013 return Math.max(len, 1);
4793};
5014};
4794
5015
5016/** Point at arc-length fraction u ∈ [0, 1]. */
5017Online.curvePoint = function (curve, u)
5018{
5019 if (!curve)
5020 { return { x: 0, y: 0 }; }
5021 var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));
5022 return this._evalPoly(curve, t);
5023};
5024
4795/**
5025/**
4796 * Equal-parameter split across segments so matching local C'(t)
5026 * dP/du under arc-length parametrisation: unit tangent × total length
4797 * at joints also matches dP/du (no speed jerk).
5027 * (constant speed in u, direction from the polynomial).
4798 */
5028 */
4799Online._curveSegmentAt = function (curve, u)
5029Online.curveDeriv = function (curve, u)
4800{
5030{
4801 if (!curve)
5031 if (!curve)
4802 { return null; }
5032 { return { x: 0, y: 0 }; }
4803 if (!curve.segments)
5033 var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));
4804 {
5034 var d = this._evalPolyDeriv(curve, t);
4805 return { seg: curve, t: u < 0 ? 0 : (u > 1 ? 1 : u), scale: 1 };
5035 if (curve.arcLength !== false)
5036 {
5037 if (!curve.lutS)
5038 { this._buildArcLut(curve); }
5039 var mag = Math.sqrt(d.x * d.x + d.y * d.y);
5040 if (mag < 1e-9)
5041 { return { x: 0, y: 0 }; }
5042 var s = curve.totalLen / mag;
5043 return { x: d.x * s, y: d.y * s };
4806 }
5044 }
4807 var segs = curve.segments;
5045 return d;
4808 var n = segs.length;
4809 if (u <= 0)
4810 { return { seg: segs[0], t: 0, scale: n }; }
4811 if (u >= 1)
4812 { return { seg: segs[n - 1], t: 1, scale: n }; }
4813 var at = u * n;
4814 var i = Math.floor(at);
4815 if (i >= n)
4816 { i = n - 1; }
4817 var t = at - i;
4818 return { seg: segs[i], t: t, scale: n };
4819};
5046};
4820
5047
4821/** Point on a (possibly multi-segment C¹) curve at u ∈ [0, 1]. */
5048Online.curveDeriv2 = function (curve, u)
4822Online.curvePoint = function (curve, u)
4823{
5049{
4824 var at = this._curveSegmentAt(curve, u);
5050 if (!curve)
4825 if (!at)
4826 { return { x: 0, y: 0 }; }
5051 { return { x: 0, y: 0 }; }
4827 var s = at.seg;
5052 var t = curve.arcLength !== false ? this._arcT(curve, u) : (u < 0 ? 0 : (u > 1 ? 1 : u));
4828 if (s && s.p1)
5053 return this._evalPolyDeriv2(curve, t);
4829 { return this.cubicBezier(s.p0, s.p1, s.p2, s.p3, at.t); }
4830 if (s && s.p0)
4831 { return { x: s.p0.x, y: s.p0.y }; }
4832 return { x: 0, y: 0 };
4833};
5054};
4834
5055
4835/** dP/du of the curve (C¹ across joints). */
5056/** Curvature at arc-length fraction u (uses polynomial t, κ is invariant). */
4836Online.curveDeriv = function (curve, u)
5057Online.curveCurvature = function (curve, u)
4837{
5058{
4838 var at = this._curveSegmentAt(curve, u);
5059 var t = curve && curve.arcLength !== false ? this._arcT(curve, u) : u;
4839 if (!at || !at.seg || !at.seg.p1)
5060 return this.curvature(this._evalPolyDeriv(curve, t), this._evalPolyDeriv2(curve, t));
4840 { return { x: 0, y: 0 }; }
4841 var s = at.seg;
4842 var d = this.cubicBezierDeriv(s.p0, s.p1, s.p2, s.p3, at.t);
4843 return { x: d.x * at.scale, y: d.y * at.scale };
4844};
5061};
4845
5062
4846/**
5063/**
4847 * Sample the smooth around-field curve at u ∈ [0, 1] (t1134).
5064 * Endpoint ease rates so arrival speed ≈ fallSpeed under arc-length
4848 * `clearance` may be a number or an options object.
5065 * parametrisation: v = (L / duration) * p'(raw).
4849 */
5066 */
5067Online.flightEaseRates = function (curve, duration, fallPx)
5068{
5069 if (!curve || !curve.lutS)
5070 { this._measureCurve(curve); }
5071 var L = (curve && curve.totalLen) || 400;
5072 duration = duration || this.FLIGHT_DURATION;
5073 var v1 = fallPx && L ? (fallPx * duration) / L : 0.75;
5074 if (v1 < 0.28)
5075 { v1 = 0.28; }
5076 if (v1 > 1.15)
5077 { v1 = 1.15; }
5078 var v0 = v1 + 0.2;
5079 if (v0 > 1)
5080 { v0 = 1; }
5081 if (v0 < 0.45)
5082 { v0 = 0.45; }
5083 return { v0: v0, v1: v1 };
5084};
5085
4850Online.aroundFieldPoint = function (start, end, u, field, clearance)
5086Online.aroundFieldPoint = function (start, end, u, field, clearance)
4851{
5087{
4852 var opts = (clearance && typeof clearance === 'object') ? clearance : { clearance: clearance };
5088 var opts = (clearance && typeof clearance === 'object') ? clearance : { clearance: clearance };
@@ -4854,17 +5090,11 @@ Online.aroundFieldPoint = function (start, end, u, field, clearance)
4854 return this.curvePoint(curve, u);
5090 return this.curvePoint(curve, u);
4855};
5091};
4856
5092
4857/** Guide points (start, joints, end) for older tests. */
4858Online.waypointsAroundField = function (start, end, field, clearance)
5093Online.waypointsAroundField = function (start, end, field, clearance)
4859{
5094{
4860 var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 64 });
5095 var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 100 });
4861 if (c.segments && c.segments.length)
5096 if (c.pts && c.pts.length)
4862 {
5097 { return c.pts.slice(); }
4863 var pts = [{ x: c.segments[0].p0.x, y: c.segments[0].p0.y }];
4864 for (var i = 0; i < c.segments.length; i++)
4865 { pts.push({ x: c.segments[i].p3.x, y: c.segments[i].p3.y }); }
4866 return pts;
4867 }
4868 return [c.p0, c.p1, c.p2, c.p3];
5098 return [c.p0, c.p1, c.p2, c.p3];
4869};
5099};
4870
5100
@@ -5148,14 +5378,12 @@ Online.spawnFlights = function (results, locals)
5148 : this.arcHeight(start, end);
5378 : this.arcHeight(start, end);
5149 var field = this.playerFieldRect();
5379 var field = this.playerFieldRect();
5150 var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');
5380 var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');
5151 var clearance = Math.max(64, height * 0.45);
5381 var clearance = Math.max(150, height * 0.55);
5152 var endSpeed = this._fallSpeedPx(field, duration / 1000);
5382 var endSpeed = this._fallSpeedPx(field, duration / 1000);
5153 var curve = miss ? null : this.makeAroundCurve(start, end, around ? field : null, {
5383 var curve = miss ? null : this.makeAroundCurve(start, end, around ? field : null, {
5154 clearance: clearance,
5384 clearance: clearance
5155 startSpeedPx: endSpeed,
5156 endSpeedPx: endSpeed,
5157 duration: duration / 1000
5158 });
5385 });
5386 var easeRates = curve ? this.flightEaseRates(curve, duration / 1000, endSpeed) : null;
5159 this._flights.push({
5387 this._flights.push({
5160 el: el,
5388 el: el,
5161 start: start,
5389 start: start,
@@ -5164,6 +5392,7 @@ Online.spawnFlights = function (results, locals)
5164 height: height,
5392 height: height,
5165 t0: t0,
5393 t0: t0,
5166 duration: duration,
5394 duration: duration,
5395 easeRates: easeRates,
5167 spin: miss ? ((Math.random() * 2 - 1) * 720) : ((Math.random() * 2 - 1) * 120),
5396 spin: miss ? ((Math.random() * 2 - 1) * 720) : ((Math.random() * 2 - 1) * 120),
5168 miss: miss,
5397 miss: miss,
5169 kind: kind,
5398 kind: kind,
@@ -5314,10 +5543,9 @@ Online.previewExit = function (info)
5314 front.appendChild(el);
5543 front.appendChild(el);
5315 var endSpeed = this._fallSpeedPx(field, this.FLIGHT_DURATION);
5544 var endSpeed = this._fallSpeedPx(field, this.FLIGHT_DURATION);
5316 var curve = this.makeAroundCurve(start, end, info.from_id ? null : field, {
5545 var curve = this.makeAroundCurve(start, end, info.from_id ? null : field, {
5317 clearance: clearance,
5546 clearance: clearance
5318 startSpeedPx: endSpeed,
5319 endSpeedPx: endSpeed
5320 });
5547 });
5548 var easeRates = this.flightEaseRates(curve, this.FLIGHT_DURATION, endSpeed);
5321 var now = performance.now();
5549 var now = performance.now();
5322 var mini = info.from_id ? (this._miniCellSize(info.from_id) / full) : 1;
5550 var mini = info.from_id ? (this._miniCellSize(info.from_id) / full) : 1;
5323 if (mini < 0.12)
5551 if (mini < 0.12)
@@ -5340,6 +5568,7 @@ Online.previewExit = function (info)
5340 landCol: null,
5568 landCol: null,
5341 field: field,
5569 field: field,
5342 curve: curve,
5570 curve: curve,
5571 easeRates: easeRates,
5343 preview: true,
5572 preview: true,
5344 color: info.color,
5573 color: info.color,
5345 from_id: info.from_id || null
5574 from_id: info.from_id || null
@@ -5431,19 +5660,19 @@ Online.retargetFlight = function (f, r, meta, now)
5431 var height = miss
5660 var height = miss
5432 ? Math.max(40, Math.min(160, Math.abs(end.y - pt.y) * 0.35))
5661 ? Math.max(40, Math.min(160, Math.abs(end.y - pt.y) * 0.35))
5433 : this.arcHeight(pt, end);
5662 : this.arcHeight(pt, end);
5434 var clearance = Math.max(64, height * 0.45);
5663 var clearance = Math.max(150, height * 0.55);
5435 var endSpeed = this._fallSpeedPx(field, duration / 1000);
5664 var endSpeed = this._fallSpeedPx(field, duration / 1000);
5436 var curve = miss ? null : this.makeAroundCurve(pt, end, around ? field : null, {
5665 var curve = miss ? null : this.makeAroundCurve(pt, end, around ? field : null, {
5437 clearance: clearance,
5666 clearance: clearance
5438 startSpeedPx: endSpeed,
5439 endSpeedPx: endSpeed
5440 });
5667 });
5668 var easeRates = curve ? this.flightEaseRates(curve, duration / 1000, endSpeed) : null;
5441
5669
5442 f.start = pt;
5670 f.start = pt;
5443 f.end = end;
5671 f.end = end;
5444 f.t0 = now;
5672 f.t0 = now;
5445 f.duration = duration;
5673 f.duration = duration;
5446 f.curve = curve;
5674 f.curve = curve;
5675 f.easeRates = easeRates;
5447 f.ctrl = miss ? this._missCtrl(pt, end) : null;
5676 f.ctrl = miss ? this._missCtrl(pt, end) : null;
5448 f.height = height;
5677 f.height = height;
5449 f.miss = miss;
5678 f.miss = miss;
@@ -5521,8 +5750,10 @@ Online.tickFlights = function (now)
5521 }
5750 }
5522 if (raw < 0)
5751 if (raw < 0)
5523 { raw = 0; }
5752 { raw = 0; }
5524 // Non-zero endpoint speed so the handoff matches fallSpeed (t1135).
5753 // Non-zero endpoint speed so the handoff matches fallSpeed (t1135/t1138).
5525 var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, 0.85, 0.85);
5754 var v0 = (f.easeRates && f.easeRates.v0 != null) ? f.easeRates.v0 : 0.85;
5755 var v1 = (f.easeRates && f.easeRates.v1 != null) ? f.easeRates.v1 : 0.85;
5756 var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, v0, v1);
5526 var x, y;
5757 var x, y;
5527 if (f.miss && f.ctrl)
5758 if (f.miss && f.ctrl)
5528 {
5759 {
docs/ASSUMPTIONS.md +27 −0
@@ -736,3 +736,30 @@ Aanname:
736 736 - Een raket die onder de rand piekt produceert geen `outgoingShot`
737 737 en geen vlucht; een raket die `y ≥ 13` kruist precies één schot
738 738 en één vlucht, beginnend op die exit-pose.
739
740 ## t1138 — één grote, vloeiende ballistische boog
741
742 - Tim vond de bogen na t1134–t1137 al beter, maar wilde ze nóg
743 vloeiender: geen ketting van stukjes (kwart-draai, rechte
744 kruising op de top, duik) maar één grote, ronde, doorlopende
745 curve, als een ballistische parabool.
746 - Speler↔vijand is nu **één enkele Bézier van graad 7** (één
747 polynoom, dus C^∞ op (0, 1)): start op de exit-pose (`y = 13`,
748 raaklijn recht omhoog), een ruime ronde kroon **boven** het veld
749 (mag hoger — we gebruiken de top-padding ruim, apex zo'n 150 px
750 boven de veldrand), en een duik de `land_col` in, recht naar
751 beneden, met een eindsnelheid afgestemd op `fallSpeed`. Het
752 binnenveld wordt nooit gekruist: horizontaal verkeer zit op de
753 top, verticaal alleen in de lanceer-/landingskolom of buiten het
754 veld. Geen rechte cruise, geen zichtbare knikken; de kromming
755 verandert geleidelijk. De parameter loopt over de **booglengte**
756 (arc-length) met een zachte Hermite-easing, zodat de snelheid
757 langs het pad glad is en de aankomst bij `fallSpeed` past.
758 - Vijand→vijand krijgt dezelfde soepele parabool, getekend achter
759 het veld (`#flights-back`, clip-path). De startregel van t1137
760 (`y ≥ 13`) en de grijze raak-schoten blijven ongewijzigd.
761 - Unit-tests toetsen krommingscontinuïteit, maximale
762 krommingsverandering, geen lange rechte middenstukken, verticale
763 uiteinden, corridors, en de fallSpeed-overdracht. Playwright
764 legt early/mid/late vast op 1024×532 en 1920×1080, plus een
765 webm van beide formaten.
@@ -736,3 +736,30 @@ Aanname:
736- Een raket die onder de rand piekt produceert geen `outgoingShot`
736- Een raket die onder de rand piekt produceert geen `outgoingShot`
737 en geen vlucht; een raket die `y ≥ 13` kruist precies één schot
737 en geen vlucht; een raket die `y ≥ 13` kruist precies één schot
738 en één vlucht, beginnend op die exit-pose.
738 en één vlucht, beginnend op die exit-pose.
739
740## t1138 — één grote, vloeiende ballistische boog
741
742- Tim vond de bogen na t1134–t1137 al beter, maar wilde ze nóg
743 vloeiender: geen ketting van stukjes (kwart-draai, rechte
744 kruising op de top, duik) maar één grote, ronde, doorlopende
745 curve, als een ballistische parabool.
746- Speler↔vijand is nu **één enkele Bézier van graad 7** (één
747 polynoom, dus C^∞ op (0, 1)): start op de exit-pose (`y = 13`,
748 raaklijn recht omhoog), een ruime ronde kroon **boven** het veld
749 (mag hoger — we gebruiken de top-padding ruim, apex zo'n 150 px
750 boven de veldrand), en een duik de `land_col` in, recht naar
751 beneden, met een eindsnelheid afgestemd op `fallSpeed`. Het
752 binnenveld wordt nooit gekruist: horizontaal verkeer zit op de
753 top, verticaal alleen in de lanceer-/landingskolom of buiten het
754 veld. Geen rechte cruise, geen zichtbare knikken; de kromming
755 verandert geleidelijk. De parameter loopt over de **booglengte**
756 (arc-length) met een zachte Hermite-easing, zodat de snelheid
757 langs het pad glad is en de aankomst bij `fallSpeed` past.
758- Vijand→vijand krijgt dezelfde soepele parabool, getekend achter
759 het veld (`#flights-back`, clip-path). De startregel van t1137
760 (`y ≥ 13`) en de grijze raak-schoten blijven ongewijzigd.
761- Unit-tests toetsen krommingscontinuïteit, maximale
762 krommingsverandering, geen lange rechte middenstukken, verticale
763 uiteinden, corridors, en de fallSpeed-overdracht. Playwright
764 legt early/mid/late vast op 1024×532 en 1920×1080, plus een
765 webm van beide formaten.
src/online.js +402 −171
@@ -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
554Online.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). */
569Online.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
588Online.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
607Online.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). */
632Online.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 */
645Online.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
670Online.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
686Online.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 route
723 * One continuous degree-7 Bézier: a single big, round, ballistic
575 * around (enemy→enemy) or both ends already sit above the apex.
724 * arch (t1138). Vertical at the exit (heading up) and at the
576 */
725 * landing (heading down). Horizontal travel sits on the apex,
577Online._singleCubic = function (start, end, apexY, opts)
726 * above the field — a cluster of high control points on each
578{
727 * side keeps the hull out of the interior, with no straight
579 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 stay
582 { 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 the
584 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 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.
597 */
735 */
598Online.makeAroundCurve = function (start, end, field, opts)
736Online.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 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 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 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 };
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 else
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
820 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: 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)
840Online._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 ? diveStart
849 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.x
851 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 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 });
692 }
855 }
856 return { x: 0, y: 0 };
857};
693
858
694 if (segments.length === 0)
859Online._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 };
868Online._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
702Online._measureCurve = function (curve)
877/** Cumulative-length LUT so u is a fraction of arc length (t1138). */
878Online._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. */
897Online._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 else
916 { 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
927Online._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
719Online._segLen = function (seg, steps)
936Online._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]. */
957Online.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 length
737 * at joints also matches dP/du (no speed jerk).
967 * (constant speed in u, direction from the polynomial).
738 */
968 */
739Online._curveSegmentAt = function (curve, u)
969Online.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]. */
988Online.curveDeriv2 = function (curve, u)
762Online.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). */
776Online.curveDeriv = function (curve, u)
997Online.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-length
788 * `clearance` may be a number or an options object.
1005 * parametrisation: v = (L / duration) * p'(raw).
789 */
1006 */
1007Online.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
790Online.aroundFieldPoint = function (start, end, u, field, clearance)
1026Online.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. */
798Online.waypointsAroundField = function (start, end, field, clearance)
1033Online.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: clearance
1095 startSpeedPx: endSpeed,
1096 endSpeedPx: endSpeed,
1097 duration: duration / 1000
1098 });
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: clearance
1258 startSpeedPx: endSpeed,
1259 endSpeedPx: endSpeed
1260 });
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 || null
1514 from_id: info.from_id || null
@@ -1371,19 +1600,19 @@ Online.retargetFlight = function (f, r, meta, now)
1371 var height = miss
1600 var height = miss
1372 ? 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: clearance
1378 startSpeedPx: endSpeed,
1379 endSpeedPx: endSpeed
1380 });
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 {
test/flight-path.test.js +116 −45
@@ -160,47 +160,109 @@ describe('Smooth C1 arcs (t1134)', () =>
160 160 return Online.cellViewport(col, y, field);
161 161 }
162 162
163 it('is a C1 curve: tangents and speeds match at joints, no corners', () =>
163 it('is one continuous polynomial: C^∞, gradual curvature, no straight stretches (t1138)', () =>
164 164 {
165 const start = { x: 250, y: 700 };
166 const end = entry(3);
167 const curve = Online.makeAroundCurve(start, end, field, { clearance: 80 });
168 assert.ok(curve.segments && curve.segments.length >= 2, 'multi-segment around-field path');
169 const n = curve.segments.length;
170 for (let i = 1; i < n; i++)
165 const cases = [
166 { name: 'incoming left-mini → col3', start: { x: 250, y: 700 }, end: entry(3) },
167 { name: 'incoming far-left → col9', start: { x: 200, y: 800 }, end: entry(9) },
168 { name: 'outgoing mid → right mini', start: entry(6), end: { x: field.right + 80, y: 700 } },
169 { name: 'outgoing col0 → left mini', start: entry(0), end: { x: 200, y: 700 } },
170 { name: 'incoming right-mini → col1', start: { x: field.right + 90, y: 720 }, end: entry(1) },
171 { name: 'enemy→enemy behind (no field)', start: { x: 180, y: 750 }, end: { x: field.right + 90, y: 680 }, noField: true }
172 ];
173 for (const c of cases)
171 174 {
172 const u = i / n;
173 const left = Online.curveDeriv(curve, u - 1e-6);
174 const right = Online.curveDeriv(curve, u + 1e-6);
175 const aL = Math.atan2(left.y, left.x);
176 const aR = Math.atan2(right.y, right.x);
177 let da = Math.abs(aR - aL);
178 if (da > Math.PI)
179 { da = 2 * Math.PI - da; }
180 assert.ok(da < 0.02, 'joint ' + i + ' direction jump ' + da);
181 const sL = Math.hypot(left.x, left.y);
182 const sR = Math.hypot(right.x, right.y);
183 assert.ok(Math.abs(sL - sR) / Math.max(sL, 1) < 0.05,
184 'joint ' + i + ' speed jump ' + sL + ' vs ' + sR);
185 }
186 // Densely sampled tangent never snaps by a large angle (no corner).
187 let prev = null;
188 let maxDa = 0;
189 for (let i = 0; i <= 80; i++)
190 {
191 const d = Online.curveDeriv(curve, i / 80);
192 const a = Math.atan2(d.y, d.x);
193 if (prev != null)
175 const curve = Online.makeAroundCurve(c.start, c.end, c.noField ? null : field, { clearance: 110 });
176 assert.equal(curve.degree, 7, c.name + ' is a single degree-7 Bézier');
177 assert.ok(curve.pts && curve.pts.length === 8, c.name + ' has eight control points');
178 assert.ok(!curve.segments, c.name + ' is not a chain of segments');
179 assert.ok(curve.totalLen > 50, c.name + ' has a measured arc length');
180
181 // Single polynomial ⇒ C^∞: tangent and curvature vary continuously.
182 let prevA = null;
183 let maxDa = 0;
184 const kappas = [];
185 let zeroRun = 0;
186 let maxZeroRun = 0;
187 let maxDKappa = 0;
188 let prevK = null;
189 const N = 100;
190 for (let i = 0; i <= N; i++)
191 {
192 const u = i / N;
193 const d = Online.curveDeriv(curve, u);
194 const a = Math.atan2(d.y, d.x);
195 if (prevA != null)
196 {
197 let da = Math.abs(a - prevA);
198 if (da > Math.PI)
199 { da = 2 * Math.PI - da; }
200 if (da > maxDa)
201 { maxDa = da; }
202 }
203 prevA = a;
204 const k = Online.curveCurvature(curve, u);
205 kappas.push(k);
206 // A nearly-straight vertical heading near the ends is
207 // expected (exit up, land down). The forbidden stretch
208 // is a mid-arc cruise like the old κ=0 apex segment.
209 if (u > 0.12 && u < 0.72 && Math.abs(k) < 1e-5)
210 {
211 zeroRun++;
212 if (zeroRun > maxZeroRun)
213 { maxZeroRun = zeroRun; }
214 }
215 else
216 { zeroRun = 0; }
217 if (prevK != null)
218 {
219 const dk = Math.abs(k - prevK);
220 if (dk > maxDKappa)
221 { maxDKappa = dk; }
222 }
223 prevK = k;
224 }
225 assert.ok(maxDa < 0.28, c.name + ' max step angle ' + maxDa + ' looks like a kink');
226 // No long κ≈0 cruise (the old straight apex segment). A short
227 // almost-flat inflection (a few samples) is fine.
228 assert.ok(maxZeroRun <= 8, c.name + ' straight stretch of ' + maxZeroRun + ' samples (κ≈0)');
229 // Curvature changes gradually: no spike between neighbouring samples.
230 const maxAbsK = Math.max(...kappas.map(Math.abs), 1e-9);
231 assert.ok(maxDKappa / maxAbsK < 0.85, c.name + ' curvature jump ' + maxDKappa + ' vs max |κ| ' + maxAbsK);
232 // Ends are vertical: up at the start, down at the landing.
233 const d0 = Online.curveDeriv(curve, 0);
234 const d1 = Online.curveDeriv(curve, 1);
235 assert.ok(d0.y < 0, c.name + ' starts heading up, vy=' + d0.y);
236 assert.ok(Math.abs(d0.x) < 1e-6, c.name + ' start tangent is vertical, dx=' + d0.x);
237 assert.ok(d1.y > 0, c.name + ' ends heading down, vy=' + d1.y);
238 assert.ok(Math.abs(d1.x) < 1e-6, c.name + ' end tangent is vertical, dx=' + d1.x);
239 // Peak sits above both ends (a real arch, not a sag).
240 let minY = Infinity;
241 for (let i = 0; i <= N; i++)
242 {
243 const p = Online.curvePoint(curve, i / N);
244 if (p.y < minY)
245 { minY = p.y; }
246 }
247 assert.ok(minY < Math.min(c.start.y, c.end.y) - 20, c.name + ' peak ' + minY + ' is not a high arch');
248 if (!c.noField)
194 249 {
195 let da = Math.abs(a - prev);
196 if (da > Math.PI)
197 { da = 2 * Math.PI - da; }
198 if (da > maxDa)
199 { maxDa = da; }
250 for (let i = 0; i <= 80; i++)
251 {
252 const p = Online.curvePoint(curve, i / 80);
253 assert.equal(Online.pointAllowedOnArc(p, field, c.start, c.end), true,
254 c.name + ' u=' + (i / 80) + ' at ' + p.x + ',' + p.y + ' crosses the field');
255 }
256 }
257 // Arc-length: |dP/du| is nearly constant (= totalLen).
258 for (const u of [0, 0.25, 0.5, 0.75, 1])
259 {
260 const d = Online.curveDeriv(curve, u);
261 const speed = Math.hypot(d.x, d.y);
262 assert.ok(Math.abs(speed - curve.totalLen) / curve.totalLen < 0.08,
263 c.name + ' arc-length speed at u=' + u + ' is ' + speed + ' vs L=' + curve.totalLen);
200 264 }
201 prev = a;
202 265 }
203 assert.ok(maxDa < 0.35, 'max step angle ' + maxDa + ' looks like a corner');
204 266 });
205 267
206 268 it('leaves a mid-field cell going up and stays in launch/land corridors', () =>
@@ -228,20 +290,29 @@ describe('Smooth C1 arcs (t1134)', () =>
228 290 assert.ok(Math.abs(end.x - (field.left + 3.5 / 10 * field.width)) < 0.5);
229 291 const duration = 1.2;
230 292 const fallPx = (13 / 3) * (field.height / 13);
231 const endSpeedPx = (fallPx * duration) / 3;
232 const curve = Online.makeAroundCurve(start, end, field, {
233 clearance: 80, endSpeedPx: endSpeedPx, duration: duration
234 });
293 const curve = Online.makeAroundCurve(start, end, field, { clearance: 110 });
235 294 const landed = Online.curvePoint(curve, 1);
236 295 assert.ok(Math.abs(landed.x - end.x) < 0.5);
237 296 assert.ok(Math.abs(landed.y - end.y) < 0.5);
238 297 const d1 = Online.curveDeriv(curve, 1);
239 298 assert.ok(d1.y > 0, 'end tangent points down into the column');
240 assert.ok(Math.abs(d1.x) < 20, 'end tangent is vertical, dx=' + d1.x);
241 const v1 = 0.85;
242 const pxPerSec = d1.y * v1 / duration;
243 assert.ok(Math.abs(pxPerSec - fallPx) / fallPx < 0.08,
244 'arrival ' + pxPerSec + ' px/s vs fallSpeed ' + fallPx);
299 assert.ok(Math.abs(d1.x) < 1, 'end tangent is vertical, dx=' + d1.x);
300 // Under arc-length parametrisation |dP/du| = L, so arrival
301 // speed is (L/duration)*v1. flightEaseRates picks v1 to
302 // match fallSpeed, clamped so a very long path does not rush.
303 const rates = Online.flightEaseRates(curve, duration, fallPx);
304 const pxPerSec = (curve.totalLen / duration) * rates.v1;
305 const unclamped = fallPx * duration / curve.totalLen;
306 if (unclamped >= 0.28 && unclamped <= 1.15)
307 {
308 assert.ok(Math.abs(pxPerSec - fallPx) / fallPx < 0.02,
309 'arrival ' + pxPerSec + ' px/s vs fallSpeed ' + fallPx);
310 }
311 else
312 {
313 assert.ok(pxPerSec > 0 && rates.v1 >= 0.28 && rates.v1 <= 1.15,
314 'clamped arrival ' + pxPerSec + ' with v1=' + rates.v1);
315 }
245 316 });
246 317
247 318 it('smoothParam has non-zero endpoint derivatives (no visual stop)', () =>
@@ -160,47 +160,109 @@ describe('Smooth C1 arcs (t1134)', () =>
160 return Online.cellViewport(col, y, field);
160 return Online.cellViewport(col, y, field);
161 }
161 }
162
162
163 it('is a C1 curve: tangents and speeds match at joints, no corners', () =>
163 it('is one continuous polynomial: C^∞, gradual curvature, no straight stretches (t1138)', () =>
164 {
164 {
165 const start = { x: 250, y: 700 };
165 const cases = [
166 const end = entry(3);
166 { name: 'incoming left-mini → col3', start: { x: 250, y: 700 }, end: entry(3) },
167 const curve = Online.makeAroundCurve(start, end, field, { clearance: 80 });
167 { name: 'incoming far-left → col9', start: { x: 200, y: 800 }, end: entry(9) },
168 assert.ok(curve.segments && curve.segments.length >= 2, 'multi-segment around-field path');
168 { name: 'outgoing mid → right mini', start: entry(6), end: { x: field.right + 80, y: 700 } },
169 const n = curve.segments.length;
169 { name: 'outgoing col0 → left mini', start: entry(0), end: { x: 200, y: 700 } },
170 for (let i = 1; i < n; i++)
170 { name: 'incoming right-mini → col1', start: { x: field.right + 90, y: 720 }, end: entry(1) },
171 { name: 'enemy→enemy behind (no field)', start: { x: 180, y: 750 }, end: { x: field.right + 90, y: 680 }, noField: true }
172 ];
173 for (const c of cases)
171 {
174 {
172 const u = i / n;
175 const curve = Online.makeAroundCurve(c.start, c.end, c.noField ? null : field, { clearance: 110 });
173 const left = Online.curveDeriv(curve, u - 1e-6);
176 assert.equal(curve.degree, 7, c.name + ' is a single degree-7 Bézier');
174 const right = Online.curveDeriv(curve, u + 1e-6);
177 assert.ok(curve.pts && curve.pts.length === 8, c.name + ' has eight control points');
175 const aL = Math.atan2(left.y, left.x);
178 assert.ok(!curve.segments, c.name + ' is not a chain of segments');
176 const aR = Math.atan2(right.y, right.x);
179 assert.ok(curve.totalLen > 50, c.name + ' has a measured arc length');
177 let da = Math.abs(aR - aL);
180
178 if (da > Math.PI)
181 // Single polynomial ⇒ C^∞: tangent and curvature vary continuously.
179 { da = 2 * Math.PI - da; }
182 let prevA = null;
180 assert.ok(da < 0.02, 'joint ' + i + ' direction jump ' + da);
183 let maxDa = 0;
181 const sL = Math.hypot(left.x, left.y);
184 const kappas = [];
182 const sR = Math.hypot(right.x, right.y);
185 let zeroRun = 0;
183 assert.ok(Math.abs(sL - sR) / Math.max(sL, 1) < 0.05,
186 let maxZeroRun = 0;
184 'joint ' + i + ' speed jump ' + sL + ' vs ' + sR);
187 let maxDKappa = 0;
185 }
188 let prevK = null;
186 // Densely sampled tangent never snaps by a large angle (no corner).
189 const N = 100;
187 let prev = null;
190 for (let i = 0; i <= N; i++)
188 let maxDa = 0;
191 {
189 for (let i = 0; i <= 80; i++)
192 const u = i / N;
190 {
193 const d = Online.curveDeriv(curve, u);
191 const d = Online.curveDeriv(curve, i / 80);
194 const a = Math.atan2(d.y, d.x);
192 const a = Math.atan2(d.y, d.x);
195 if (prevA != null)
193 if (prev != null)
196 {
197 let da = Math.abs(a - prevA);
198 if (da > Math.PI)
199 { da = 2 * Math.PI - da; }
200 if (da > maxDa)
201 { maxDa = da; }
202 }
203 prevA = a;
204 const k = Online.curveCurvature(curve, u);
205 kappas.push(k);
206 // A nearly-straight vertical heading near the ends is
207 // expected (exit up, land down). The forbidden stretch
208 // is a mid-arc cruise like the old κ=0 apex segment.
209 if (u > 0.12 && u < 0.72 && Math.abs(k) < 1e-5)
210 {
211 zeroRun++;
212 if (zeroRun > maxZeroRun)
213 { maxZeroRun = zeroRun; }
214 }
215 else
216 { zeroRun = 0; }
217 if (prevK != null)
218 {
219 const dk = Math.abs(k - prevK);
220 if (dk > maxDKappa)
221 { maxDKappa = dk; }
222 }
223 prevK = k;
224 }
225 assert.ok(maxDa < 0.28, c.name + ' max step angle ' + maxDa + ' looks like a kink');
226 // No long κ≈0 cruise (the old straight apex segment). A short
227 // almost-flat inflection (a few samples) is fine.
228 assert.ok(maxZeroRun <= 8, c.name + ' straight stretch of ' + maxZeroRun + ' samples (κ≈0)');
229 // Curvature changes gradually: no spike between neighbouring samples.
230 const maxAbsK = Math.max(...kappas.map(Math.abs), 1e-9);
231 assert.ok(maxDKappa / maxAbsK < 0.85, c.name + ' curvature jump ' + maxDKappa + ' vs max |κ| ' + maxAbsK);
232 // Ends are vertical: up at the start, down at the landing.
233 const d0 = Online.curveDeriv(curve, 0);
234 const d1 = Online.curveDeriv(curve, 1);
235 assert.ok(d0.y < 0, c.name + ' starts heading up, vy=' + d0.y);
236 assert.ok(Math.abs(d0.x) < 1e-6, c.name + ' start tangent is vertical, dx=' + d0.x);
237 assert.ok(d1.y > 0, c.name + ' ends heading down, vy=' + d1.y);
238 assert.ok(Math.abs(d1.x) < 1e-6, c.name + ' end tangent is vertical, dx=' + d1.x);
239 // Peak sits above both ends (a real arch, not a sag).
240 let minY = Infinity;
241 for (let i = 0; i <= N; i++)
242 {
243 const p = Online.curvePoint(curve, i / N);
244 if (p.y < minY)
245 { minY = p.y; }
246 }
247 assert.ok(minY < Math.min(c.start.y, c.end.y) - 20, c.name + ' peak ' + minY + ' is not a high arch');
248 if (!c.noField)
194 {
249 {
195 let da = Math.abs(a - prev);
250 for (let i = 0; i <= 80; i++)
196 if (da > Math.PI)
251 {
197 { da = 2 * Math.PI - da; }
252 const p = Online.curvePoint(curve, i / 80);
198 if (da > maxDa)
253 assert.equal(Online.pointAllowedOnArc(p, field, c.start, c.end), true,
199 { maxDa = da; }
254 c.name + ' u=' + (i / 80) + ' at ' + p.x + ',' + p.y + ' crosses the field');
255 }
256 }
257 // Arc-length: |dP/du| is nearly constant (= totalLen).
258 for (const u of [0, 0.25, 0.5, 0.75, 1])
259 {
260 const d = Online.curveDeriv(curve, u);
261 const speed = Math.hypot(d.x, d.y);
262 assert.ok(Math.abs(speed - curve.totalLen) / curve.totalLen < 0.08,
263 c.name + ' arc-length speed at u=' + u + ' is ' + speed + ' vs L=' + curve.totalLen);
200 }
264 }
201 prev = a;
202 }
265 }
203 assert.ok(maxDa < 0.35, 'max step angle ' + maxDa + ' looks like a corner');
204 });
266 });
205
267
206 it('leaves a mid-field cell going up and stays in launch/land corridors', () =>
268 it('leaves a mid-field cell going up and stays in launch/land corridors', () =>
@@ -228,20 +290,29 @@ describe('Smooth C1 arcs (t1134)', () =>
228 assert.ok(Math.abs(end.x - (field.left + 3.5 / 10 * field.width)) < 0.5);
290 assert.ok(Math.abs(end.x - (field.left + 3.5 / 10 * field.width)) < 0.5);
229 const duration = 1.2;
291 const duration = 1.2;
230 const fallPx = (13 / 3) * (field.height / 13);
292 const fallPx = (13 / 3) * (field.height / 13);
231 const endSpeedPx = (fallPx * duration) / 3;
293 const curve = Online.makeAroundCurve(start, end, field, { clearance: 110 });
232 const curve = Online.makeAroundCurve(start, end, field, {
233 clearance: 80, endSpeedPx: endSpeedPx, duration: duration
234 });
235 const landed = Online.curvePoint(curve, 1);
294 const landed = Online.curvePoint(curve, 1);
236 assert.ok(Math.abs(landed.x - end.x) < 0.5);
295 assert.ok(Math.abs(landed.x - end.x) < 0.5);
237 assert.ok(Math.abs(landed.y - end.y) < 0.5);
296 assert.ok(Math.abs(landed.y - end.y) < 0.5);
238 const d1 = Online.curveDeriv(curve, 1);
297 const d1 = Online.curveDeriv(curve, 1);
239 assert.ok(d1.y > 0, 'end tangent points down into the column');
298 assert.ok(d1.y > 0, 'end tangent points down into the column');
240 assert.ok(Math.abs(d1.x) < 20, 'end tangent is vertical, dx=' + d1.x);
299 assert.ok(Math.abs(d1.x) < 1, 'end tangent is vertical, dx=' + d1.x);
241 const v1 = 0.85;
300 // Under arc-length parametrisation |dP/du| = L, so arrival
242 const pxPerSec = d1.y * v1 / duration;
301 // speed is (L/duration)*v1. flightEaseRates picks v1 to
243 assert.ok(Math.abs(pxPerSec - fallPx) / fallPx < 0.08,
302 // match fallSpeed, clamped so a very long path does not rush.
244 'arrival ' + pxPerSec + ' px/s vs fallSpeed ' + fallPx);
303 const rates = Online.flightEaseRates(curve, duration, fallPx);
304 const pxPerSec = (curve.totalLen / duration) * rates.v1;
305 const unclamped = fallPx * duration / curve.totalLen;
306 if (unclamped >= 0.28 && unclamped <= 1.15)
307 {
308 assert.ok(Math.abs(pxPerSec - fallPx) / fallPx < 0.02,
309 'arrival ' + pxPerSec + ' px/s vs fallSpeed ' + fallPx);
310 }
311 else
312 {
313 assert.ok(pxPerSec > 0 && rates.v1 >= 0.28 && rates.v1 <= 1.15,
314 'clamped arrival ' + pxPerSec + ' with v1=' + rates.v1);
315 }
245 });
316 });
246
317
247 it('smoothParam has non-zero endpoint derivatives (no visual stop)', () =>
318 it('smoothParam has non-zero endpoint derivatives (no visual stop)', () =>
test/minis-flights-playwright.py +21 −16
@@ -411,7 +411,7 @@ def run_viewport(browser, url, name, width, height, is_mobile, round_n, expect_m
411 411 n=n_minis)
412 412 else:
413 413 assert_top(info, name)
414 shot(page, f"t1134-{name}-layout.png")
414 shot(page, f"t1138-{name}-layout.png")
415 415
416 416 flights = page.evaluate(INJECT_FLIGHTS, 0.55)
417 417 print(f"--- {name} flights:", {k: flights[k] for k in ('front', 'back', 'expectedEndX', 'expectedEndY', 'expectedStartY', 'hudH', 'mode') if k in flights})
@@ -420,27 +420,32 @@ def run_viewport(browser, url, name, width, height, is_mobile, round_n, expect_m
420 420 f"src={knd.get('src')} curve={knd.get('hasCurve')}/{knd.get('segments')} "
421 421 f"start={knd.get('start')} end={knd.get('end')} pos={knd.get('pos')}")
422 422 assert_flights(flights, name)
423 shot(page, f"t1134-{name}-midarc.png")
423 shot(page, f"t1138-{name}-midarc.png")
424 424
425 425 if name in ("1024x532-3bots", "1920x1080-8bots", "desktop-wide"):
426 426 for age, label in ((0.18, "early"), (0.85, "late")):
427 427 page.evaluate(INJECT_FLIGHTS, age)
428 shot(page, f"t1134-{name}-midarc-{label}.png")
428 shot(page, f"t1138-{name}-midarc-{label}.png")
429 429 context.close()
430 430
431 431
432 def record_arcs_video(browser, url):
433 """A short webm of two volleys flying out, over, and into the field (t1134/t1135)."""
432 def record_arcs_video(browser, url, width, height, round_n):
433 """A short webm of two volleys flying out, over, and into the field (t1138)."""
434 434 VIDEO.mkdir(parents=True, exist_ok=True)
435 dest = VIDEO / "t1134-arcs-1920x1080.webm"
435 raw_dir = VIDEO / f"_raw_{width}x{height}"
436 if raw_dir.exists():
437 for old in raw_dir.glob("*"):
438 old.unlink()
439 raw_dir.mkdir(parents=True, exist_ok=True)
440 dest = VIDEO / f"t1138-arcs-{width}x{height}.webm"
436 441 context = browser.new_context(
437 viewport={"width": 1920, "height": 1080},
442 viewport={"width": width, "height": height},
438 443 device_scale_factor=1,
439 record_video_dir=str(VIDEO / "_raw"),
440 record_video_size={"width": 1920, "height": 1080},
444 record_video_dir=str(raw_dir),
445 record_video_size={"width": width, "height": height},
441 446 )
442 447 page = context.new_page()
443 open_offline(page, url, round_n=25)
448 open_offline(page, url, round_n=round_n)
444 449 page.wait_for_timeout(400)
445 450 # Live volleys at age 0 so tickFlights animates them in the game loop.
446 451 page.evaluate(INJECT_FLIGHTS, 0)
@@ -449,17 +454,16 @@ def record_arcs_video(browser, url):
449 454 page.wait_for_timeout(1400)
450 455 page.close()
451 456 context.close()
452 # Playwright writes the webm next to the context; move it into place.
453 raw = list((VIDEO / "_raw").glob("*.webm"))
457 raw = list(raw_dir.glob("*.webm"))
454 458 if raw:
455 459 raw[0].replace(dest)
456 460 print(f"wrote {dest} ({dest.stat().st_size} bytes)")
457 461 else:
458 FAILURES.append("video: no webm produced")
459 print("FAIL video: no webm produced")
462 FAILURES.append(f"video {width}x{height}: no webm produced")
463 print(f"FAIL video {width}x{height}: no webm produced")
460 464 return
461 465 if dest.stat().st_size < 10000:
462 FAILURES.append(f"video: suspiciously small ({dest.stat().st_size} bytes)")
466 FAILURES.append(f"video {width}x{height}: suspiciously small ({dest.stat().st_size} bytes)")
463 467
464 468
465 469 def main():
@@ -484,7 +488,8 @@ def main():
484 488 expect_one_row=True, n_minis=8)
485 489 run_viewport(browser, url, "1920x1080-8bots", 1920, 1080, False, 25, "sides",
486 490 expect_one_row=True, n_minis=8)
487 record_arcs_video(browser, url)
491 record_arcs_video(browser, url, 1920, 1080, 25)
492 record_arcs_video(browser, url, 1024, 532, 3)
488 493 browser.close()
489 494 finally:
490 495 http.terminate()
@@ -411,7 +411,7 @@ def run_viewport(browser, url, name, width, height, is_mobile, round_n, expect_m
411 n=n_minis)
411 n=n_minis)
412 else:
412 else:
413 assert_top(info, name)
413 assert_top(info, name)
414 shot(page, f"t1134-{name}-layout.png")
414 shot(page, f"t1138-{name}-layout.png")
415
415
416 flights = page.evaluate(INJECT_FLIGHTS, 0.55)
416 flights = page.evaluate(INJECT_FLIGHTS, 0.55)
417 print(f"--- {name} flights:", {k: flights[k] for k in ('front', 'back', 'expectedEndX', 'expectedEndY', 'expectedStartY', 'hudH', 'mode') if k in flights})
417 print(f"--- {name} flights:", {k: flights[k] for k in ('front', 'back', 'expectedEndX', 'expectedEndY', 'expectedStartY', 'hudH', 'mode') if k in flights})
@@ -420,27 +420,32 @@ def run_viewport(browser, url, name, width, height, is_mobile, round_n, expect_m
420 f"src={knd.get('src')} curve={knd.get('hasCurve')}/{knd.get('segments')} "
420 f"src={knd.get('src')} curve={knd.get('hasCurve')}/{knd.get('segments')} "
421 f"start={knd.get('start')} end={knd.get('end')} pos={knd.get('pos')}")
421 f"start={knd.get('start')} end={knd.get('end')} pos={knd.get('pos')}")
422 assert_flights(flights, name)
422 assert_flights(flights, name)
423 shot(page, f"t1134-{name}-midarc.png")
423 shot(page, f"t1138-{name}-midarc.png")
424
424
425 if name in ("1024x532-3bots", "1920x1080-8bots", "desktop-wide"):
425 if name in ("1024x532-3bots", "1920x1080-8bots", "desktop-wide"):
426 for age, label in ((0.18, "early"), (0.85, "late")):
426 for age, label in ((0.18, "early"), (0.85, "late")):
427 page.evaluate(INJECT_FLIGHTS, age)
427 page.evaluate(INJECT_FLIGHTS, age)
428 shot(page, f"t1134-{name}-midarc-{label}.png")
428 shot(page, f"t1138-{name}-midarc-{label}.png")
429 context.close()
429 context.close()
430
430
431
431
432def record_arcs_video(browser, url):
432def record_arcs_video(browser, url, width, height, round_n):
433 """A short webm of two volleys flying out, over, and into the field (t1134/t1135)."""
433 """A short webm of two volleys flying out, over, and into the field (t1138)."""
434 VIDEO.mkdir(parents=True, exist_ok=True)
434 VIDEO.mkdir(parents=True, exist_ok=True)
435 dest = VIDEO / "t1134-arcs-1920x1080.webm"
435 raw_dir = VIDEO / f"_raw_{width}x{height}"
436 if raw_dir.exists():
437 for old in raw_dir.glob("*"):
438 old.unlink()
439 raw_dir.mkdir(parents=True, exist_ok=True)
440 dest = VIDEO / f"t1138-arcs-{width}x{height}.webm"
436 context = browser.new_context(
441 context = browser.new_context(
437 viewport={"width": 1920, "height": 1080},
442 viewport={"width": width, "height": height},
438 device_scale_factor=1,
443 device_scale_factor=1,
439 record_video_dir=str(VIDEO / "_raw"),
444 record_video_dir=str(raw_dir),
440 record_video_size={"width": 1920, "height": 1080},
445 record_video_size={"width": width, "height": height},
441 )
446 )
442 page = context.new_page()
447 page = context.new_page()
443 open_offline(page, url, round_n=25)
448 open_offline(page, url, round_n=round_n)
444 page.wait_for_timeout(400)
449 page.wait_for_timeout(400)
445 # Live volleys at age 0 so tickFlights animates them in the game loop.
450 # Live volleys at age 0 so tickFlights animates them in the game loop.
446 page.evaluate(INJECT_FLIGHTS, 0)
451 page.evaluate(INJECT_FLIGHTS, 0)
@@ -449,17 +454,16 @@ def record_arcs_video(browser, url):
449 page.wait_for_timeout(1400)
454 page.wait_for_timeout(1400)
450 page.close()
455 page.close()
451 context.close()
456 context.close()
452 # Playwright writes the webm next to the context; move it into place.
457 raw = list(raw_dir.glob("*.webm"))
453 raw = list((VIDEO / "_raw").glob("*.webm"))
454 if raw:
458 if raw:
455 raw[0].replace(dest)
459 raw[0].replace(dest)
456 print(f"wrote {dest} ({dest.stat().st_size} bytes)")
460 print(f"wrote {dest} ({dest.stat().st_size} bytes)")
457 else:
461 else:
458 FAILURES.append("video: no webm produced")
462 FAILURES.append(f"video {width}x{height}: no webm produced")
459 print("FAIL video: no webm produced")
463 print(f"FAIL video {width}x{height}: no webm produced")
460 return
464 return
461 if dest.stat().st_size < 10000:
465 if dest.stat().st_size < 10000:
462 FAILURES.append(f"video: suspiciously small ({dest.stat().st_size} bytes)")
466 FAILURES.append(f"video {width}x{height}: suspiciously small ({dest.stat().st_size} bytes)")
463
467
464
468
465def main():
469def main():
@@ -484,7 +488,8 @@ def main():
484 expect_one_row=True, n_minis=8)
488 expect_one_row=True, n_minis=8)
485 run_viewport(browser, url, "1920x1080-8bots", 1920, 1080, False, 25, "sides",
489 run_viewport(browser, url, "1920x1080-8bots", 1920, 1080, False, 25, "sides",
486 expect_one_row=True, n_minis=8)
490 expect_one_row=True, n_minis=8)
487 record_arcs_video(browser, url)
491 record_arcs_video(browser, url, 1920, 1080, 25)
492 record_arcs_video(browser, url, 1024, 532, 3)
488 browser.close()
493 browser.close()
489 finally:
494 finally:
490 http.terminate()
495 http.terminate()