chore: rebuild game.js with smooth ballistic arcs (t1138)
@@ -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
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].x4642
+ 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].y4644
+ b3 * pts[3].y + b4 * pts[4].y + b5 * pts[5].y4645
};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].y4679
});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 first4703
* 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].y4724
};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
* 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 route4783
* One continuous degree-7 Bézier: a single big, round, ballistic4635
* around (enemy→enemy) or both ends already sit above the apex.4784
* arch (t1138). Vertical at the exit (heading up) and at the4636
*/4785
* landing (heading down). Horizontal travel sits on the apex,4637
Online._singleCubic = function (start, end, apexY, opts)4786
* above the field — a cluster of high control points on each4638
{4787
* side keeps the hull out of the interior, with no straight4639
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 stay4642
{ 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 the4644
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 above4653
* the field, dive into the landing column (t1134 / t1135). Joints4654
* are quarter-turn cubics with matching tangents, so there are no4655
* corners and no speed jerks. The curve may use the launch and4656
* landing corridors but does not cut across the field interior.4657
*/4795
*/4658
Online.makeAroundCurve = function (start, end, field, opts)4796
Online.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
else4821
{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 at4704
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
else4854
{ 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
else4878
else4708
{ 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 joints4885
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: true4724
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
? diveStart4728
: riseEnd;4729
if (segments.length && segments[segments.length - 1].p3.x === u0.x4730
&& 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 is4740
// (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
4762
Online._measureCurve = function (curve)4900
Online._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
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
else4976
{ 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
4987
Online._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
4779
Online._segLen = function (seg, steps)4996
Online._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]. */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)5026
* dP/du under arc-length parametrisation: unit tangent × total length4797
* at joints also matches dP/du (no speed jerk).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
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]. */5048
Online.curveDeriv2 = function (curve, u)4822
Online.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). */4836
Online.curveDeriv = function (curve, u)5057
Online.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-length4848
* `clearance` may be a number or an options object.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
Online.aroundFieldPoint = function (start, end, u, field, clearance)5086
Online.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. */4858
Online.waypointsAroundField = function (start, end, field, clearance)5093
Online.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: clearance5155
startSpeedPx: endSpeed,5156
endSpeedPx: endSpeed,5157
duration: duration / 10005158
});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: clearance5318
startSpeedPx: endSpeed,5319
endSpeedPx: endSpeed5320
});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 || null5574
from_id: info.from_id || null@@ -5431,19 +5660,19 @@ Online.retargetFlight = function (f, r, meta, now)5431
var height = miss5660
var height = miss5432
? 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: clearance5438
startSpeedPx: endSpeed,5439
endSpeedPx: endSpeed5440
});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
{