feat: smooth C¹ arcs that fly out of and into the field (t1134, t1135)
Replace the cornered waypoint polyline with a C¹ chain of cubic Béziers: rise out of the launch cell, cruise above the field, dive into the landing column. End tangent and speed match fallSpeed at the ingest spawn pose (y=13). Outgoing flights take over a rocket near the top row at its real cell, so blocks no longer pop in above the canvas. Hits stay grey; enemy→enemy stays behind the field.
@@ -2011,7 +2011,8 @@ GameCore.prototype.cullOffTop = function ()
2011
2011
id: block.id,
2012
2012
color: block.color,
2013
2013
col: block.col,
2014
y: block.y
2014
y: block.y,
2015
vy: block.vy
2015
2016
});
2016
2017
this.score += 1;
2017
2018
this.removeBlock(block);
@@ -2011,7 +2011,8 @@ GameCore.prototype.cullOffTop = function ()2011
id: block.id,2011
id: block.id,2012
color: block.color,2012
color: block.color,2013
col: block.col,2013
col: block.col,2014
y: block.y2014
y: block.y,2015
vy: block.vy2015
});2016
});2016
this.score += 1;2017
this.score += 1;2017
this.removeBlock(block);2018
this.removeBlock(block);@@ -489,6 +489,11 @@ function syncSprites (dt)
489
489
applyBlockVisual(obj, block);
490
490
obj.renderer.visible = true;
491
491
obj.renderer.alpha = 1;
492
if (typeof Online !== 'undefined' && Online.hasExitVisual && Online.hasExitVisual(block.id))
493
{
494
obj.renderer.visible = false;
495
obj.renderer.alpha = 0;
496
}
492
497
493
498
if (block.flame)
494
499
{
@@ -515,6 +520,10 @@ function syncSprites (dt)
515
520
}
516
521
else if (app.flames[block.id])
517
522
{ app.flames[block.id].renderer.visible = false; }
523
if (block.flame && app.flames[block.id]
524
&& typeof Online !== 'undefined' && Online.hasExitVisual
525
&& Online.hasExitVisual(block.id))
526
{ app.flames[block.id].renderer.visible = false; }
518
527
}
519
528
520
529
var ids = Object.keys(app.sprites);
@@ -752,6 +761,7 @@ function tickView (dt)
752
761
placeTile(app.cells[i], col, row);
753
762
}
754
763
764
previewOutgoingFlights();
755
765
app.core.tick(dt);
756
766
if (app.offline && app.battle)
757
767
{ tickOffline(dt); }
@@ -1272,7 +1282,7 @@ function tickOnline (dt)
1272
1282
for (var i = 0; i < shots.length; i++)
1273
1283
{
1274
1284
var s = shots[i];
1275
var start = fieldToViewport(s.col, s.y + 1);
1285
var start = fieldToViewport(s.col, s.y);
1276
1286
Online.rememberFlightLocal(s, start.x, start.y);
1277
1287
}
1278
1288
Online.queueShots(shots);
@@ -1350,7 +1360,9 @@ function tickOffline (dt)
1350
1360
var s = list[i];
1351
1361
var start;
1352
1362
if (s.from_id === 'player')
1353
{ start = fieldToViewport(s.col, s.y + 1); }
1363
{ start = fieldToViewport(s.col, s.y); }
1364
else if (typeof Online !== 'undefined' && Online.miniCellViewport)
1365
{ start = Online.miniCellViewport(s.from_id, s.col, s.y != null ? s.y : 12); }
1354
1366
else
1355
1367
{ start = miniViewportCenter(s.from_id); }
1356
1368
locals[s.client_id] = {
@@ -1594,6 +1606,58 @@ function updateColumnWarn (dt)
1594
1606
}
1595
1607
}
1596
1608
1609
/**
1610
* Take over a rocket that is about to leave the top of a field (the
1611
* player's, or a bot's) so the flight starts at the real cell and
1612
* continues its upward motion (t1134). Shared by offline and online.
1613
*/
1614
function previewOutgoingFlights ()
1615
{
1616
if (typeof Online === 'undefined' || !Online.previewExit)
1617
{ return; }
1618
if (app.core)
1619
{ previewCoreExits(app.core, null); }
1620
if (app.offline && app.battle && app.battle.bots)
1621
{
1622
var bots = app.battle.bots;
1623
for (var i = 0; i < bots.length; i++)
1624
{
1625
var bot = bots[i];
1626
if (bot && bot.core && !bot.eliminated)
1627
{ previewCoreExits(bot.core, bot.id); }
1628
}
1629
}
1630
}
1631
1632
function previewCoreExits (core, fromId)
1633
{
1634
if (!core || !core.blocks)
1635
{ return; }
1636
var blocks = core.blocks;
1637
for (var i = 0; i < blocks.length; i++)
1638
{
1639
var b = blocks[i];
1640
if (b.y < 11.6)
1641
{ continue; }
1642
if (!(b.vy > 0))
1643
{ continue; }
1644
var start = fromId
1645
? (Online.miniCellViewport
1646
? Online.miniCellViewport(fromId, b.col, b.y)
1647
: miniViewportCenter(fromId))
1648
: fieldToViewport(b.col, b.y);
1649
Online.previewExit({
1650
id: b.id,
1651
col: b.col,
1652
y: b.y,
1653
vy: b.vy,
1654
color: b.color,
1655
start: start,
1656
from_id: fromId || null
1657
});
1658
}
1659
}
1660
1597
1661
function fieldToViewport (col, fieldY)
1598
1662
{
1599
1663
var canvas = app.scene && app.scene.canvas;
@@ -1601,7 +1665,8 @@ function fieldToViewport (col, fieldY)
1601
1665
{ return { x: window.innerWidth / 2, y: 40 }; }
1602
1666
var rect = canvas.getBoundingClientRect();
1603
1667
var x = rect.left + ((col + 0.5) / 10) * rect.width;
1604
var y = rect.top + (1 - (fieldY / 13)) * rect.height;
1668
// Cell centre matches placeTile: y = fieldH - (fieldY + 0.5) * tile (t1134).
1669
var y = rect.top + (1 - ((fieldY + 0.5) / 13)) * rect.height;
1605
1670
return { x: x, y: y };
1606
1671
}
1607
1672
@@ -489,6 +489,11 @@ function syncSprites (dt)489
applyBlockVisual(obj, block);489
applyBlockVisual(obj, block);490
obj.renderer.visible = true;490
obj.renderer.visible = true;491
obj.renderer.alpha = 1;491
obj.renderer.alpha = 1;492
if (typeof Online !== 'undefined' && Online.hasExitVisual && Online.hasExitVisual(block.id))493
{494
obj.renderer.visible = false;495
obj.renderer.alpha = 0;496
}492
497
493
if (block.flame)498
if (block.flame)494
{499
{@@ -515,6 +520,10 @@ function syncSprites (dt)515
}520
}516
else if (app.flames[block.id])521
else if (app.flames[block.id])517
{ app.flames[block.id].renderer.visible = false; }522
{ app.flames[block.id].renderer.visible = false; }523
if (block.flame && app.flames[block.id]524
&& typeof Online !== 'undefined' && Online.hasExitVisual525
&& Online.hasExitVisual(block.id))526
{ app.flames[block.id].renderer.visible = false; }518
}527
}519
528
520
var ids = Object.keys(app.sprites);529
var ids = Object.keys(app.sprites);@@ -752,6 +761,7 @@ function tickView (dt)752
placeTile(app.cells[i], col, row);761
placeTile(app.cells[i], col, row);753
}762
}754
763
764
previewOutgoingFlights();755
app.core.tick(dt);765
app.core.tick(dt);756
if (app.offline && app.battle)766
if (app.offline && app.battle)757
{ tickOffline(dt); }767
{ tickOffline(dt); }@@ -1272,7 +1282,7 @@ function tickOnline (dt)1272
for (var i = 0; i < shots.length; i++)1282
for (var i = 0; i < shots.length; i++)1273
{1283
{1274
var s = shots[i];1284
var s = shots[i];1275
var start = fieldToViewport(s.col, s.y + 1);1285
var start = fieldToViewport(s.col, s.y);1276
Online.rememberFlightLocal(s, start.x, start.y);1286
Online.rememberFlightLocal(s, start.x, start.y);1277
}1287
}1278
Online.queueShots(shots);1288
Online.queueShots(shots);@@ -1350,7 +1360,9 @@ function tickOffline (dt)1350
var s = list[i];1360
var s = list[i];1351
var start;1361
var start;1352
if (s.from_id === 'player')1362
if (s.from_id === 'player')1353
{ start = fieldToViewport(s.col, s.y + 1); }1363
{ start = fieldToViewport(s.col, s.y); }1364
else if (typeof Online !== 'undefined' && Online.miniCellViewport)1365
{ start = Online.miniCellViewport(s.from_id, s.col, s.y != null ? s.y : 12); }1354
else1366
else1355
{ start = miniViewportCenter(s.from_id); }1367
{ start = miniViewportCenter(s.from_id); }1356
locals[s.client_id] = {1368
locals[s.client_id] = {@@ -1594,6 +1606,58 @@ function updateColumnWarn (dt)1594
}1606
}1595
}1607
}1596
1608
1609
/**1610
* Take over a rocket that is about to leave the top of a field (the1611
* player's, or a bot's) so the flight starts at the real cell and1612
* continues its upward motion (t1134). Shared by offline and online.1613
*/1614
function previewOutgoingFlights ()1615
{1616
if (typeof Online === 'undefined' || !Online.previewExit)1617
{ return; }1618
if (app.core)1619
{ previewCoreExits(app.core, null); }1620
if (app.offline && app.battle && app.battle.bots)1621
{1622
var bots = app.battle.bots;1623
for (var i = 0; i < bots.length; i++)1624
{1625
var bot = bots[i];1626
if (bot && bot.core && !bot.eliminated)1627
{ previewCoreExits(bot.core, bot.id); }1628
}1629
}1630
}1631
1632
function previewCoreExits (core, fromId)1633
{1634
if (!core || !core.blocks)1635
{ return; }1636
var blocks = core.blocks;1637
for (var i = 0; i < blocks.length; i++)1638
{1639
var b = blocks[i];1640
if (b.y < 11.6)1641
{ continue; }1642
if (!(b.vy > 0))1643
{ continue; }1644
var start = fromId1645
? (Online.miniCellViewport1646
? Online.miniCellViewport(fromId, b.col, b.y)1647
: miniViewportCenter(fromId))1648
: fieldToViewport(b.col, b.y);1649
Online.previewExit({1650
id: b.id,1651
col: b.col,1652
y: b.y,1653
vy: b.vy,1654
color: b.color,1655
start: start,1656
from_id: fromId || null1657
});1658
}1659
}1660
1597
function fieldToViewport (col, fieldY)1661
function fieldToViewport (col, fieldY)1598
{1662
{1599
var canvas = app.scene && app.scene.canvas;1663
var canvas = app.scene && app.scene.canvas;@@ -1601,7 +1665,8 @@ function fieldToViewport (col, fieldY)1601
{ return { x: window.innerWidth / 2, y: 40 }; }1665
{ return { x: window.innerWidth / 2, y: 40 }; }1602
var rect = canvas.getBoundingClientRect();1666
var rect = canvas.getBoundingClientRect();1603
var x = rect.left + ((col + 0.5) / 10) * rect.width;1667
var x = rect.left + ((col + 0.5) / 10) * rect.width;1604
var y = rect.top + (1 - (fieldY / 13)) * rect.height;1668
// Cell centre matches placeTile: y = fieldH - (fieldY + 0.5) * tile (t1134).1669
var y = rect.top + (1 - ((fieldY + 0.5) / 13)) * rect.height;1605
return { x: x, y: y };1670
return { x: x, y: y };1606
}1671
}1607
1672
@@ -508,7 +508,7 @@ Online.playerFieldRect = function ()
508
508
509
509
/**
510
510
* True if (x, y) is inside the field's interior. The top edge (a few
511
* pixels) is not interior: that is where arcs may land (t1128).
511
* pixels) is not interior: launch and landing may use that band.
512
512
*/
513
513
Online.pointInFieldInterior = function (x, y, field, slop)
514
514
{
@@ -519,107 +519,363 @@ Online.pointInFieldInterior = function (x, y, field, slop)
519
519
&& y > field.top + slop && y < field.bottom - 0.5;
520
520
};
521
521
522
/** Cubic Bezier C(t), t ∈ [0, 1]. C¹ (in fact C^∞) on (0, 1) — no corners (t1134). */
523
Online.cubicBezier = function (p0, p1, p2, p3, t)
524
{
525
if (t < 0)
526
{ t = 0; }
527
if (t > 1)
528
{ t = 1; }
529
var u = 1 - t;
530
var a = u * u * u;
531
var b = 3 * u * u * t;
532
var c = 3 * u * t * t;
533
var d = t * t * t;
534
return {
535
x: a * p0.x + b * p1.x + c * p2.x + d * p3.x,
536
y: a * p0.y + b * p1.y + c * p2.y + d * p3.y
537
};
538
};
539
540
/** C'(t) = 3(1−t)²(P1−P0) + 6(1−t)t(P2−P1) + 3t²(P3−P2). */
541
Online.cubicBezierDeriv = function (p0, p1, p2, p3, t)
542
{
543
if (t < 0)
544
{ t = 0; }
545
if (t > 1)
546
{ t = 1; }
547
var u = 1 - t;
548
return {
549
x: 3 * u * u * (p1.x - p0.x) + 6 * u * t * (p2.x - p1.x) + 3 * t * t * (p3.x - p2.x),
550
y: 3 * u * u * (p1.y - p0.y) + 6 * u * t * (p2.y - p1.y) + 3 * t * t * (p3.y - p2.y)
551
};
552
};
553
522
554
/**
523
* Waypoints that route a player↔enemy arc around the field, never
524
* through its interior: up the outside, across above the top edge,
525
* then into the target (t1128). If both ends already sit at or above
526
* the top, a simple two-point path is enough.
555
* 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).
527
557
*/
528
Online.waypointsAroundField = function (start, end, field, clearance)
558
Online.smoothParam = function (t, v0, v1)
529
559
{
530
if (!start || !end)
531
{ return [start || { x: 0, y: 0 }, end || { x: 0, y: 0 }]; }
560
if (t < 0)
561
{ return 0; }
562
if (t > 1)
563
{ return 1; }
564
if (v0 == null)
565
{ v0 = 1; }
566
if (v1 == null)
567
{ v1 = 1; }
568
var b = 3 - 2 * v0 - v1;
569
var a = v0 + v1 - 2;
570
return ((a * t + b) * t + v0) * t;
571
};
572
573
/**
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.
597
*/
598
Online.makeAroundCurve = function (start, end, field, opts)
599
{
600
opts = opts || {};
601
if (!start)
602
{ start = { x: 0, y: 0 }; }
603
if (!end)
604
{ end = { x: 0, y: 0 }; }
605
var clearance = opts.clearance || 80;
532
606
if (!field)
533
{ return [start, end]; }
534
clearance = clearance || 64;
607
{
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);
612
}
535
613
var top = field.top;
536
614
var left = field.left;
537
615
var right = field.right;
538
var pad = 10;
539
var apexY = Math.min(start.y, end.y, top) - clearance;
616
var apexY = top - clearance;
540
617
if (apexY < 8)
541
618
{ apexY = Math.max(8, top - Math.max(24, clearance * 0.5)); }
542
619
543
if (start.y <= top + 6 && end.y <= top + 6)
544
{ return [start, end]; }
620
if (start.y <= apexY + 8 && end.y <= apexY + 8)
621
{ return this._singleCubic(start, end, apexY, opts); }
622
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)
628
{
629
if (x < left + 8)
630
{ return Math.min(x, left - pad); }
631
if (x > right - 8)
632
{ return Math.max(x, right + pad); }
633
return x;
634
}
635
var riseX = outsideX(start.x);
636
var diveX = outsideX(end.x);
637
var segments = [];
638
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)
642
{
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 });
646
}
647
else
648
{ riseEnd = { x: start.x, y: Math.min(start.y, apexY) }; }
545
649
546
var w1x = start.x;
547
var w2x = end.x;
548
if (start.x < left)
549
{ w1x = Math.min(start.x, left - pad); }
550
else if (start.x > right)
551
{ w1x = Math.max(start.x, right + pad); }
552
if (end.x < left)
553
{ w2x = Math.min(end.x, left - pad); }
554
else if (end.x > right)
555
{ w2x = Math.max(end.x, right + pad); }
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
{
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 });
662
}
556
663
557
return [
558
start,
559
{ x: w1x, y: apexY },
560
{ x: w2x, y: apexY },
561
end
562
];
664
if (end.y > apexY + 4 || Math.abs(end.x - (diveStart.x + dir * horiz)) > 2)
665
{
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 });
692
}
693
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); }
698
699
return { segments: segments, apexY: apexY, p0: start, p3: end };
563
700
};
564
701
565
/**
566
* Point at parameter u ∈ [0, 1] along a polyline, with a small upward
567
* bulge on each segment so the path reads as a "boogje".
568
*/
569
Online.pointOnPath = function (points, u)
702
Online._measureCurve = function (curve)
570
703
{
571
u = u < 0 ? 0 : (u > 1 ? 1 : u);
572
if (!points || !points.length)
573
{ return { x: 0, y: 0 }; }
574
if (points.length === 1)
575
{ return { x: points[0].x, y: points[0].y }; }
704
if (!curve.segments)
705
{ return curve; }
576
706
var lens = [];
577
707
var total = 0;
578
for (var i = 1; i < points.length; i++)
708
for (var i = 0; i < curve.segments.length; i++)
579
709
{
580
var dx = points[i].x - points[i - 1].x;
581
var dy = points[i].y - points[i - 1].y;
582
var L = Math.sqrt(dx * dx + dy * dy);
583
if (L < 1)
584
{ L = 1; }
710
var L = this._segLen(curve.segments[i]);
585
711
lens.push(L);
586
712
total += L;
587
713
}
588
var dist = u * total;
589
var acc = 0;
590
for (var i = 0; i < lens.length; i++)
714
curve.lens = lens;
715
curve.totalLen = Math.max(total, 1);
716
return curve;
717
};
718
719
Online._segLen = function (seg, steps)
720
{
721
steps = steps || 10;
722
var len = 0;
723
var prev = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, 0);
724
for (var i = 1; i <= steps; i++)
591
725
{
592
if (acc + lens[i] >= dist || i === lens.length - 1)
593
{
594
var t = (dist - acc) / lens[i];
595
if (t < 0)
596
{ t = 0; }
597
if (t > 1)
598
{ t = 1; }
599
var a = points[i];
600
var b = points[i + 1];
601
var bump = 4 * 16 * t * (1 - t);
602
return {
603
x: a.x + (b.x - a.x) * t,
604
y: a.y + (b.y - a.y) * t - bump
605
};
606
}
607
acc += lens[i];
726
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);
730
prev = p;
608
731
}
609
var last = points[points.length - 1];
610
return { x: last.x, y: last.y };
732
return Math.max(len, 1);
733
};
734
735
/**
736
* Equal-parameter split across segments so matching local C'(t)
737
* at joints also matches dP/du (no speed jerk).
738
*/
739
Online._curveSegmentAt = function (curve, u)
740
{
741
if (!curve)
742
{ return null; }
743
if (!curve.segments)
744
{
745
return { seg: curve, t: u < 0 ? 0 : (u > 1 ? 1 : u), scale: 1 };
746
}
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 };
759
};
760
761
/** Point on a (possibly multi-segment C¹) curve at u ∈ [0, 1]. */
762
Online.curvePoint = function (curve, u)
763
{
764
var at = this._curveSegmentAt(curve, u);
765
if (!at)
766
{ 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 };
773
};
774
775
/** dP/du of the curve (C¹ across joints). */
776
Online.curveDeriv = function (curve, u)
777
{
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 };
611
784
};
612
785
613
/** Sample a player↔enemy path; used by tests and the ticker. */
786
/**
787
* Sample the smooth around-field curve at u ∈ [0, 1] (t1134).
788
* `clearance` may be a number or an options object.
789
*/
614
790
Online.aroundFieldPoint = function (start, end, u, field, clearance)
615
791
{
616
var pts = this.waypointsAroundField(start, end, field, clearance);
617
if (pts.length === 2)
792
var opts = (clearance && typeof clearance === 'object') ? clearance : { clearance: clearance };
793
var curve = opts.curve || this.makeAroundCurve(start, end, field, opts);
794
return this.curvePoint(curve, u);
795
};
796
797
/** Guide points (start, joints, end) for older tests. */
798
Online.waypointsAroundField = function (start, end, field, clearance)
799
{
800
var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 64 });
801
if (c.segments && c.segments.length)
618
802
{
619
var h = this.arcHeight(start, end);
620
return this.arcPoint(start, end, u, h);
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;
621
807
}
622
return this.pointOnPath(pts, u);
808
return [c.p0, c.p1, c.p2, c.p3];
809
};
810
811
Online.pointOnPath = function (points, u)
812
{
813
// Legacy polyline; new flights use cubicBezier.
814
u = u < 0 ? 0 : (u > 1 ? 1 : u);
815
if (!points || !points.length)
816
{ return { x: 0, y: 0 }; }
817
if (points.length === 4 && points[0] && points[3])
818
{ return this.cubicBezier(points[0], points[1], points[2], points[3], u); }
819
if (points.length === 1)
820
{ return { x: points[0].x, y: points[0].y }; }
821
var n = points.length - 1;
822
var at = u * n;
823
var i = Math.floor(at);
824
if (i >= n)
825
{ i = n - 1; }
826
var t = at - i;
827
var a = points[i];
828
var b = points[i + 1];
829
return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
830
};
831
832
/** Viewport centre of a field cell (y=0 is the bottom row). */
833
Online.cellViewport = function (col, y, rect)
834
{
835
if (!rect)
836
{
837
var field = this.playerFieldRect();
838
if (!field)
839
{ return { x: 0, y: 0 }; }
840
rect = field;
841
}
842
var w = rect.width != null ? rect.width : (rect.right - rect.left);
843
var h = rect.height != null ? rect.height : (rect.bottom - rect.top);
844
return {
845
x: rect.left + ((col + 0.5) / 10) * w,
846
y: rect.top + (1 - (y + 0.5) / 13) * h
847
};
848
};
849
850
/** Handoff point of an incoming garbage block: y = 13, just into the column (t1135). */
851
Online.fieldEntry = function (col)
852
{
853
return this.cellViewport(col == null ? 4.5 : col, 13);
854
};
855
856
Online.miniCellViewport = function (playerId, col, y)
857
{
858
var el = this.miniElement(playerId);
859
var canvas = el && (el.querySelector ? el.querySelector('canvas') : null);
860
var box = canvas || el;
861
if (!box || !box.getBoundingClientRect)
862
{ return { x: window.innerWidth / 2, y: 48 }; }
863
return this.cellViewport(col == null ? 4.5 : col, y == null ? 13 : y, box.getBoundingClientRect());
864
};
865
866
Online._fallSpeedPx = function (field, duration)
867
{
868
var px = 40;
869
if (field && field.height)
870
{ px = field.height / 13; }
871
else if (typeof app !== 'undefined' && app.tile)
872
{ px = app.tile; }
873
var cells = 13 / 3;
874
if (typeof app !== 'undefined' && app.planet && app.planet.fallSpeed)
875
{ cells = app.planet.fallSpeed; }
876
duration = duration || this.FLIGHT_DURATION;
877
// Distance of P2 from P3 so C'(1)/duration ≈ fall speed (t1135).
878
return (cells * px * duration) / 3;
623
879
};
624
880
625
881
Online.rememberLandCol = function (id, col)
@@ -687,17 +943,17 @@ Online._miniAnchor = function (playerId, dir)
687
943
688
944
Online._fieldTop = function (col)
689
945
{
690
return this._flightStart(col);
946
return this.fieldEntry(col);
691
947
};
692
948
693
949
/**
694
950
* Animate shot results. `locals` is a map of client_id → {col, y, color,
695
951
* startX, startY, from_id} in viewport pixels.
696
952
*
697
* Visual-only (t1125): a parabolic arc whose scale goes from mini-cell
698
* to full tile (or the reverse). Enemy-to-enemy arcs stay small and
699
* are drawn behind the player's field. Delivery timing is unchanged;
700
* a late-seen flight starts mid-arc via `age`.
953
* Visual-only: a smooth cubic that leaves the real cell, arcs around
954
* the field, and dives into the landing column at the spawn pose
955
* (t1134 / t1135). Enemy-to-enemy arcs stay small and are drawn
956
* behind the player's field. A late-seen flight starts mid-arc via `age`.
701
957
*/
702
958
Online.spawnFlights = function (results, locals)
703
959
{
@@ -708,6 +964,8 @@ Online.spawnFlights = function (results, locals)
708
964
{ return; }
709
965
if (!this._flightSeen)
710
966
{ this._flightSeen = Object.create(null); }
967
if (!this._previewIds)
968
{ this._previewIds = Object.create(null); }
711
969
var now = performance.now();
712
970
var full = this._fullTileSize();
713
971
for (var i = 0; i < results.length; i++)
@@ -715,7 +973,18 @@ Online.spawnFlights = function (results, locals)
715
973
var r = results[i];
716
974
var sid = String(r.id != null ? r.id : (r.client_id || ('tmp-' + i)));
717
975
if (this._flightSeen[sid])
718
{ continue; }
976
{
977
if (this._previewIds[sid])
978
{
979
var existing = this._findFlight(sid);
980
if (existing)
981
{
982
var meta0 = locals[r.client_id] || locals[sid] || {};
983
this.retargetFlight(existing, r, meta0, now);
984
}
985
}
986
continue;
987
}
719
988
this._flightSeen[sid] = now;
720
989
721
990
var meta = locals[r.client_id] || locals[sid] || {};
@@ -742,11 +1011,15 @@ Online.spawnFlights = function (results, locals)
742
1011
{ this.rememberLandCol(r.client_id, landCol); }
743
1012
}
744
1013
1014
var startCol = meta.col != null ? meta.col : r.col;
1015
var startY = meta.y != null ? meta.y : r.y;
745
1016
var start;
746
1017
if (meta.startX != null && isFinite(meta.startX))
747
1018
{ start = { x: meta.startX, y: meta.startY }; }
748
1019
else if (kind === 'player-to-enemy' || kind === 'player-miss')
749
{ start = this._flightStart(meta.col != null ? meta.col : r.col); }
1020
{ start = this._flightStart(startCol, startY); }
1021
else if (startCol != null || startY != null)
1022
{ start = this.miniCellViewport(fromId, startCol, startY != null ? startY : 12); }
750
1023
else
751
1024
{ start = this._miniAnchor(fromId, 'out'); }
752
1025
@@ -754,7 +1027,7 @@ Online.spawnFlights = function (results, locals)
754
1027
if (miss)
755
1028
{ end = this._missEnd(start, kind === 'enemy-miss'); }
756
1029
else if (kind === 'enemy-to-player')
757
{ end = this._fieldTop(landCol != null ? landCol : 4.5); }
1030
{ end = this.fieldEntry(landCol != null ? landCol : 4.5); }
758
1031
else
759
1032
{ end = this._targetEnd(targetId, start, landCol); }
760
1033
@@ -815,7 +1088,14 @@ Online.spawnFlights = function (results, locals)
815
1088
: this.arcHeight(start, end);
816
1089
var field = this.playerFieldRect();
817
1090
var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');
818
var waypoints = around ? this.waypointsAroundField(start, end, field, height * 0.45) : null;
1091
var clearance = Math.max(64, height * 0.45);
1092
var endSpeed = this._fallSpeedPx(field, duration / 1000);
1093
var curve = miss ? null : this.makeAroundCurve(start, end, around ? field : null, {
1094
clearance: clearance,
1095
startSpeedPx: endSpeed,
1096
endSpeedPx: endSpeed,
1097
duration: duration / 1000
1098
});
819
1099
this._flights.push({
820
1100
el: el,
821
1101
start: start,
@@ -824,16 +1104,16 @@ Online.spawnFlights = function (results, locals)
824
1104
height: height,
825
1105
t0: t0,
826
1106
duration: duration,
827
spin: (Math.random() * 2 - 1) * 720,
1107
spin: miss ? ((Math.random() * 2 - 1) * 720) : ((Math.random() * 2 - 1) * 120),
828
1108
miss: miss,
829
1109
kind: kind,
830
1110
startScale: startScale,
831
1111
endScale: endScale,
832
angle: Math.random() * 360,
1112
angle: 0,
833
1113
id: sid,
834
1114
landCol: landCol,
835
1115
field: field,
836
waypoints: waypoints,
1116
curve: curve,
837
1117
color: flightColor
838
1118
});
839
1119
}
@@ -850,30 +1130,17 @@ Online.rememberFlightLocal = function (shot, startX, startY)
850
1130
color: shot.color,
851
1131
startX: startX,
852
1132
startY: startY,
853
from_id: shot.from_id
1133
from_id: shot.from_id,
1134
land_col: shot.land_col,
1135
vy: shot.vy
854
1136
};
855
1137
};
856
1138
857
Online._flightStart = function (col)
1139
Online._flightStart = function (col, y)
858
1140
{
859
var appRef = typeof app !== 'undefined' ? app : null;
860
if (appRef && appRef.scene && appRef.scene.canvas)
861
{
862
var canvas = appRef.scene.canvas;
863
var rect = canvas.getBoundingClientRect();
864
var c = col == null ? 4.5 : col;
865
return {
866
x: rect.left + (c + 0.5) * (rect.width / 10),
867
y: rect.top + 4
868
};
869
}
870
var field = document.querySelector('canvas');
871
if (field)
872
{
873
var rect = field.getBoundingClientRect();
874
return { x: rect.left + rect.width / 2, y: rect.top };
875
}
876
return { x: window.innerWidth / 2, y: window.innerHeight * 0.3 };
1141
if (y == null || !isFinite(y))
1142
{ y = 12; }
1143
return this.cellViewport(col == null ? 4.5 : col, y);
877
1144
};
878
1145
879
1146
Online._targetEnd = function (targetId, start, col)
@@ -881,12 +1148,8 @@ Online._targetEnd = function (targetId, start, col)
881
1148
var el = targetId && this.miniElement(targetId);
882
1149
if (el)
883
1150
{
884
var canvas = el.querySelector('canvas') || el;
885
var r = canvas.getBoundingClientRect();
886
var x = r.left + r.width / 2;
887
if (col != null && isFinite(col))
888
{ x = r.left + ((col + 0.5) / 10) * r.width; }
889
return { x: x, y: r.top + 4 };
1151
// Slightly inside the mini, in the landing column (t1135).
1152
return this.miniCellViewport(targetId, col, 12);
890
1153
}
891
1154
return { x: start.x + (Math.random() - 0.5) * 80, y: 40 };
892
1155
};
@@ -921,6 +1184,251 @@ Online._hitCtrl = function (start, end)
921
1184
};
922
1185
};
923
1186
1187
Online._findFlight = function (sid)
1188
{
1189
sid = String(sid);
1190
if (!this._flights)
1191
{ return null; }
1192
for (var i = 0; i < this._flights.length; i++)
1193
{
1194
if (String(this._flights[i].id) === sid)
1195
{ return this._flights[i]; }
1196
}
1197
return null;
1198
};
1199
1200
/**
1201
* Early visual takeover: a flying rocket near the top of a field
1202
* becomes a flight sprite at its exact cell, so it is seen flying
1203
* *out* rather than popping in above the canvas (t1134). When the
1204
* shot is later dispatched, `retargetFlight` picks up from the
1205
* current pose and aims at the real landing column.
1206
*/
1207
Online.previewExit = function (info)
1208
{
1209
if (!info || info.id == null)
1210
{ return; }
1211
var sid = String(info.id);
1212
if (!this._flights)
1213
{ this._flights = []; }
1214
if (!this._flightSeen)
1215
{ this._flightSeen = Object.create(null); }
1216
if (!this._previewIds)
1217
{ this._previewIds = Object.create(null); }
1218
if (this._previewIds[sid] || this._flightSeen[sid])
1219
{ return; }
1220
var front = document.getElementById('flights');
1221
if (!front || !info.start)
1222
{ return; }
1223
var start = info.start;
1224
var field = this.playerFieldRect();
1225
var clearance = 80;
1226
if (typeof MiniLayout !== 'undefined' && MiniLayout.ARC_TOP_PAD)
1227
{ clearance = Math.max(clearance, MiniLayout.ARC_TOP_PAD * 0.7); }
1228
var apexY = (field ? field.top : start.y) - clearance;
1229
if (apexY < 8)
1230
{ apexY = Math.max(8, start.y - 80); }
1231
var end = { x: start.x, y: apexY };
1232
var duration = this.FLIGHT_DURATION * 1000;
1233
var full = this._fullTileSize();
1234
var el = document.createElement('img');
1235
el.src = colorSpriteUrl(info.color);
1236
el.alt = '';
1237
el.className = 'flight';
1238
el.style.width = full + 'px';
1239
el.style.height = full + 'px';
1240
front.appendChild(el);
1241
var endSpeed = this._fallSpeedPx(field, this.FLIGHT_DURATION);
1242
var curve = this.makeAroundCurve(start, end, info.from_id ? null : field, {
1243
clearance: clearance,
1244
startSpeedPx: endSpeed,
1245
endSpeedPx: endSpeed
1246
});
1247
var now = performance.now();
1248
var mini = info.from_id ? (this._miniCellSize(info.from_id) / full) : 1;
1249
if (mini < 0.12)
1250
{ mini = 0.2; }
1251
this._flights.push({
1252
el: el,
1253
start: start,
1254
end: end,
1255
ctrl: null,
1256
height: clearance,
1257
t0: now,
1258
duration: duration,
1259
spin: (Math.random() * 2 - 1) * 90,
1260
miss: false,
1261
kind: info.from_id ? 'enemy-to-enemy' : 'player-to-enemy',
1262
startScale: mini,
1263
endScale: mini,
1264
angle: 0,
1265
id: sid,
1266
landCol: null,
1267
field: field,
1268
curve: curve,
1269
preview: true,
1270
color: info.color,
1271
from_id: info.from_id || null
1272
});
1273
this._previewIds[sid] = true;
1274
this._flightSeen[sid] = now;
1275
};
1276
1277
/** True while a field sprite should stay hidden — the flight is its visual. */
1278
Online.hasExitVisual = function (id)
1279
{
1280
if (id == null)
1281
{ return false; }
1282
var sid = String(id);
1283
if (this._previewIds && this._previewIds[sid])
1284
{ return true; }
1285
var f = this._findFlight(sid);
1286
if (!f || f.miss)
1287
{ return false; }
1288
var raw = (performance.now() - f.t0) / f.duration;
1289
return raw < 0.4;
1290
};
1291
1292
/**
1293
* Once the shot is scored, pick up the preview at its current pose
1294
* and fly the rest of the way to the real end (no jump).
1295
*/
1296
Online.retargetFlight = function (f, r, meta, now)
1297
{
1298
now = now || performance.now();
1299
meta = meta || {};
1300
var raw = (now - f.t0) / f.duration;
1301
if (raw < 0)
1302
{ raw = 0; }
1303
if (raw > 0.95)
1304
{ raw = 0.95; }
1305
var u = this.smoothParam(raw, 0.9, 0.9);
1306
var pt = f.curve
1307
? this.curvePoint(f.curve, u)
1308
: { x: f.start.x, y: f.start.y };
1309
var curScale = f.startScale + (f.endScale - f.startScale) * u;
1310
1311
var fromId = r.from_id || r.from_user || meta.from_id || f.from_id || null;
1312
var targetId = r.target_id != null ? r.target_id : r.to_user;
1313
var miss = !!r.miss;
1314
var kind = this.flightKind(fromId, targetId, miss);
1315
var duration = (r.flight_duration || this.FLIGHT_DURATION) * 1000;
1316
1317
var landCol = null;
1318
if (r.land_col != null && isFinite(r.land_col))
1319
{ landCol = r.land_col | 0; }
1320
else if (meta.land_col != null && isFinite(meta.land_col))
1321
{ landCol = meta.land_col | 0; }
1322
else if (f.landCol != null)
1323
{ landCol = f.landCol; }
1324
if (landCol != null)
1325
{ this.rememberLandCol(String(f.id), landCol); }
1326
1327
var end;
1328
if (miss)
1329
{ end = this._missEnd(pt, kind === 'enemy-miss'); }
1330
else if (kind === 'enemy-to-player')
1331
{ end = this.fieldEntry(landCol != null ? landCol : 4.5); }
1332
else
1333
{ end = this._targetEnd(targetId, pt, landCol); }
1334
1335
var flightColor = miss
1336
? (r.color != null ? r.color : meta.color)
1337
: -1;
1338
if (f.el && flightColor !== f.color)
1339
{ f.el.src = colorSpriteUrl(flightColor); }
1340
1341
var full = this._fullTileSize();
1342
var mini = this._miniCellSize(fromId || targetId) / full;
1343
if (mini < 0.12)
1344
{ mini = 0.2; }
1345
var endScale;
1346
if (kind === 'player-to-enemy')
1347
{ endScale = mini; }
1348
else if (kind === 'enemy-to-player')
1349
{ endScale = 1; }
1350
else if (kind === 'enemy-to-enemy')
1351
{ endScale = mini; }
1352
else
1353
{ endScale = 0; }
1354
1355
var field = this.playerFieldRect();
1356
var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');
1357
var height = miss
1358
? Math.max(40, Math.min(160, Math.abs(end.y - pt.y) * 0.35))
1359
: this.arcHeight(pt, end);
1360
var clearance = Math.max(64, height * 0.45);
1361
var endSpeed = this._fallSpeedPx(field, duration / 1000);
1362
var curve = miss ? null : this.makeAroundCurve(pt, end, around ? field : null, {
1363
clearance: clearance,
1364
startSpeedPx: endSpeed,
1365
endSpeedPx: endSpeed
1366
});
1367
1368
f.start = pt;
1369
f.end = end;
1370
f.t0 = now;
1371
f.duration = duration;
1372
f.curve = curve;
1373
f.ctrl = miss ? this._missCtrl(pt, end) : null;
1374
f.height = height;
1375
f.miss = miss;
1376
f.kind = kind;
1377
f.startScale = curScale;
1378
f.endScale = endScale;
1379
f.landCol = landCol;
1380
f.field = field;
1381
f.preview = false;
1382
f.color = flightColor;
1383
f.from_id = fromId;
1384
if (this._previewIds)
1385
{ delete this._previewIds[String(f.id)]; }
1386
1387
if (kind === 'enemy-to-enemy' && f.el)
1388
{
1389
var back = document.getElementById('flights-back');
1390
if (back && f.el.parentNode !== back)
1391
{
1392
f.el.className = 'flight flight-behind';
1393
back.appendChild(f.el);
1394
}
1395
}
1396
};
1397
1398
/** Point and (optional) derivative of a flight at parameter u. */
1399
Online.flightPointAt = function (f, u)
1400
{
1401
if (!f)
1402
{ return { x: 0, y: 0 }; }
1403
if (f.curve)
1404
{ return this.curvePoint(f.curve, u); }
1405
if (f.miss && f.ctrl)
1406
{
1407
var e = u;
1408
var mt = 1 - e;
1409
return {
1410
x: mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x,
1411
y: mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y
1412
};
1413
}
1414
return this.aroundFieldPoint(f.start, f.end, u, f.field, (f.height || 80) * 0.45);
1415
};
1416
1417
Online.pointAllowedOnArc = function (p, field, start, end, slop)
1418
{
1419
if (!this.pointInFieldInterior(p.x, p.y, field, slop))
1420
{ return true; }
1421
if (!field)
1422
{ return true; }
1423
var w = (field.right - field.left) / 10;
1424
var corridor = w * 0.7;
1425
if (start && Math.abs(p.x - start.x) <= corridor)
1426
{ return true; }
1427
if (end && Math.abs(p.x - end.x) <= corridor)
1428
{ return true; }
1429
return false;
1430
};
1431
924
1432
Online.tickFlights = function (now)
925
1433
{
926
1434
now = now || performance.now();
@@ -933,11 +1441,14 @@ Online.tickFlights = function (now)
933
1441
{
934
1442
if (f.el && f.el.parentNode)
935
1443
{ f.el.parentNode.removeChild(f.el); }
1444
if (this._previewIds)
1445
{ delete this._previewIds[String(f.id)]; }
936
1446
continue;
937
1447
}
938
1448
if (raw < 0)
939
1449
{ raw = 0; }
940
var u = this.easeInOutCubic(raw);
1450
// Non-zero endpoint speed so the handoff matches fallSpeed (t1135).
1451
var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, 0.85, 0.85);
941
1452
var x, y;
942
1453
if (f.miss && f.ctrl)
943
1454
{
@@ -946,21 +1457,15 @@ Online.tickFlights = function (now)
946
1457
x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x;
947
1458
y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y;
948
1459
}
949
else if (f.waypoints && f.waypoints.length > 2)
1460
else if (f.curve)
950
1461
{
951
var pt = this.pointOnPath(f.waypoints, u);
952
x = pt.x;
953
y = pt.y;
954
}
955
else if (f.waypoints && f.field)
956
{
957
var pt = this.aroundFieldPoint(f.start, f.end, u, f.field, (f.height || 80) * 0.45);
1462
var pt = this.curvePoint(f.curve, u);
958
1463
x = pt.x;
959
1464
y = pt.y;
960
1465
}
961
1466
else
962
1467
{
963
var pt = this.arcPoint(f.start, f.end, u, f.height || 80);
1468
var pt = this.aroundFieldPoint(f.start, f.end, u, f.field, (f.height || 80) * 0.45);
964
1469
x = pt.x;
965
1470
y = pt.y;
966
1471
}
@@ -1001,6 +1506,7 @@ Online.clearFlights = function ()
1001
1506
this._flightLocals = {};
1002
1507
this._flightSeen = Object.create(null);
1003
1508
this._pendingLandCol = Object.create(null);
1509
this._previewIds = Object.create(null);
1004
1510
};
1005
1511
1006
1512
if (typeof globalThis !== 'undefined')
@@ -508,7 +508,7 @@ Online.playerFieldRect = function ()508
508
509
/**509
/**510
* True if (x, y) is inside the field's interior. The top edge (a few510
* True if (x, y) is inside the field's interior. The top edge (a few511
* pixels) is not interior: that is where arcs may land (t1128).511
* pixels) is not interior: launch and landing may use that band.512
*/512
*/513
Online.pointInFieldInterior = function (x, y, field, slop)513
Online.pointInFieldInterior = function (x, y, field, slop)514
{514
{@@ -519,107 +519,363 @@ Online.pointInFieldInterior = function (x, y, field, slop)519
&& y > field.top + slop && y < field.bottom - 0.5;519
&& y > field.top + slop && y < field.bottom - 0.5;520
};520
};521
521
522
/** Cubic Bezier C(t), t ∈ [0, 1]. C¹ (in fact C^∞) on (0, 1) — no corners (t1134). */523
Online.cubicBezier = function (p0, p1, p2, p3, t)524
{525
if (t < 0)526
{ t = 0; }527
if (t > 1)528
{ t = 1; }529
var u = 1 - t;530
var a = u * u * u;531
var b = 3 * u * u * t;532
var c = 3 * u * t * t;533
var d = t * t * t;534
return {535
x: a * p0.x + b * p1.x + c * p2.x + d * p3.x,536
y: a * p0.y + b * p1.y + c * p2.y + d * p3.y537
};538
};539
540
/** C'(t) = 3(1−t)²(P1−P0) + 6(1−t)t(P2−P1) + 3t²(P3−P2). */541
Online.cubicBezierDeriv = function (p0, p1, p2, p3, t)542
{543
if (t < 0)544
{ t = 0; }545
if (t > 1)546
{ t = 1; }547
var u = 1 - t;548
return {549
x: 3 * u * u * (p1.x - p0.x) + 6 * u * t * (p2.x - p1.x) + 3 * t * t * (p3.x - p2.x),550
y: 3 * u * u * (p1.y - p0.y) + 6 * u * t * (p2.y - p1.y) + 3 * t * t * (p3.y - p2.y)551
};552
};553
522
/**554
/**523
* Waypoints that route a player↔enemy arc around the field, never555
* Hermite parameter with p(0)=0, p(1)=1, p'(0)=v0, p'(1)=v1.524
* through its interior: up the outside, across above the top edge,556
* v0=v1=1 is linear (constant speed in t, no endpoint halt).525
* then into the target (t1128). If both ends already sit at or above526
* the top, a simple two-point path is enough.527
*/557
*/528
Online.waypointsAroundField = function (start, end, field, clearance)558
Online.smoothParam = function (t, v0, v1)529
{559
{530
if (!start || !end)560
if (t < 0)531
{ return [start || { x: 0, y: 0 }, end || { x: 0, y: 0 }]; }561
{ return 0; }562
if (t > 1)563
{ return 1; }564
if (v0 == null)565
{ v0 = 1; }566
if (v1 == null)567
{ v1 = 1; }568
var b = 3 - 2 * v0 - v1;569
var a = v0 + v1 - 2;570
return ((a * t + b) * t + v0) * t;571
};572
573
/**574
* A simple up-and-over cubic, used when there is no field to route575
* 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 above593
* the field, dive into the landing column (t1134 / t1135). Joints594
* are quarter-turn cubics with matching tangents, so there are no595
* corners and no speed jerks. The curve may use the launch and596
* landing corridors but does not cut across the field interior.597
*/598
Online.makeAroundCurve = function (start, end, field, opts)599
{600
opts = opts || {};601
if (!start)602
{ start = { x: 0, y: 0 }; }603
if (!end)604
{ end = { x: 0, y: 0 }; }605
var clearance = opts.clearance || 80;532
if (!field)606
if (!field)533
{ return [start, end]; }607
{534
clearance = clearance || 64;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);612
}535
var top = field.top;613
var top = field.top;536
var left = field.left;614
var left = field.left;537
var right = field.right;615
var right = field.right;538
var pad = 10;616
var apexY = top - clearance;539
var apexY = Math.min(start.y, end.y, top) - clearance;540
if (apexY < 8)617
if (apexY < 8)541
{ apexY = Math.max(8, top - Math.max(24, clearance * 0.5)); }618
{ apexY = Math.max(8, top - Math.max(24, clearance * 0.5)); }542
619
543
if (start.y <= top + 6 && end.y <= top + 6)620
if (start.y <= apexY + 8 && end.y <= apexY + 8)544
{ return [start, end]; }621
{ return this._singleCubic(start, end, apexY, opts); }622
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)628
{629
if (x < left + 8)630
{ return Math.min(x, left - pad); }631
if (x > right - 8)632
{ return Math.max(x, right + pad); }633
return x;634
}635
var riseX = outsideX(start.x);636
var diveX = outsideX(end.x);637
var segments = [];638
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)642
{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 });646
}647
else648
{ riseEnd = { x: start.x, y: Math.min(start.y, apexY) }; }545
649
546
var w1x = start.x;650
// Cruise stays on the apex line (convex hull is above the field).547
var w2x = end.x;651
var diveStart = { x: diveX - dir * horiz, y: apexY };548
if (start.x < left)652
if (Math.abs(riseEnd.x - diveStart.x) > 4 || Math.abs(riseEnd.y - diveStart.y) > 4)549
{ w1x = Math.min(start.x, left - pad); }653
{550
else if (start.x > right)654
// Keep the cruise direction consistent; if the two joints551
{ w1x = Math.max(start.x, right + pad); }655
// would cross, just connect them directly.552
if (end.x < left)656
var cdir = diveStart.x >= riseEnd.x ? 1 : -1;553
{ w2x = Math.min(end.x, left - pad); }657
var c1 = { x: riseEnd.x + cdir * horiz, y: riseEnd.y };554
else if (end.x > right)658
if (c1.y > apexY)555
{ w2x = Math.max(end.x, right + pad); }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
}556
663
557
return [664
if (end.y > apexY + 4 || Math.abs(end.x - (diveStart.x + dir * horiz)) > 2)558
start,665
{559
{ x: w1x, y: apexY },666
var u0 = (Math.abs(riseEnd.x - diveStart.x) > 4 || segments.length === 0)560
{ x: w2x, y: apexY },667
? diveStart561
end668
: riseEnd;562
];669
if (segments.length && segments[segments.length - 1].p3.x === u0.x670
&& 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 is680
// (cells*px*duration)/3, so |P3-P2| = endSpeedPx/(n*v1).681
var nEst = Math.max(1, segments.length + 1);682
fall = Math.max(20, opts.endSpeedPx / (nEst * 0.85));683
}684
var u2 = { x: end.x, y: end.y - fall };685
// P2 may sit between the apex and the end (slower arrival);686
// only clamp if it would pass the end or fly far past the apex.687
if (u2.y < apexY - 240)688
{ u2.y = apexY - 240; }689
if (u2.y > end.y - 8)690
{ u2.y = end.y - 8; }691
segments.push({ p0: u0, p1: u1, p2: u2, p3: end });692
}693
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); }698
699
return { segments: segments, apexY: apexY, p0: start, p3: end };563
};700
};564
701
565
/**702
Online._measureCurve = function (curve)566
* Point at parameter u ∈ [0, 1] along a polyline, with a small upward567
* bulge on each segment so the path reads as a "boogje".568
*/569
Online.pointOnPath = function (points, u)570
{703
{571
u = u < 0 ? 0 : (u > 1 ? 1 : u);704
if (!curve.segments)572
if (!points || !points.length)705
{ return curve; }573
{ return { x: 0, y: 0 }; }574
if (points.length === 1)575
{ return { x: points[0].x, y: points[0].y }; }576
var lens = [];706
var lens = [];577
var total = 0;707
var total = 0;578
for (var i = 1; i < points.length; i++)708
for (var i = 0; i < curve.segments.length; i++)579
{709
{580
var dx = points[i].x - points[i - 1].x;710
var L = this._segLen(curve.segments[i]);581
var dy = points[i].y - points[i - 1].y;582
var L = Math.sqrt(dx * dx + dy * dy);583
if (L < 1)584
{ L = 1; }585
lens.push(L);711
lens.push(L);586
total += L;712
total += L;587
}713
}588
var dist = u * total;714
curve.lens = lens;589
var acc = 0;715
curve.totalLen = Math.max(total, 1);590
for (var i = 0; i < lens.length; i++)716
return curve;717
};718
719
Online._segLen = function (seg, steps)720
{721
steps = steps || 10;722
var len = 0;723
var prev = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, 0);724
for (var i = 1; i <= steps; i++)591
{725
{592
if (acc + lens[i] >= dist || i === lens.length - 1)726
var p = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, i / steps);593
{727
var dx = p.x - prev.x;594
var t = (dist - acc) / lens[i];728
var dy = p.y - prev.y;595
if (t < 0)729
len += Math.sqrt(dx * dx + dy * dy);596
{ t = 0; }730
prev = p;597
if (t > 1)598
{ t = 1; }599
var a = points[i];600
var b = points[i + 1];601
var bump = 4 * 16 * t * (1 - t);602
return {603
x: a.x + (b.x - a.x) * t,604
y: a.y + (b.y - a.y) * t - bump605
};606
}607
acc += lens[i];608
}731
}609
var last = points[points.length - 1];732
return Math.max(len, 1);610
return { x: last.x, y: last.y };733
};734
735
/**736
* Equal-parameter split across segments so matching local C'(t)737
* at joints also matches dP/du (no speed jerk).738
*/739
Online._curveSegmentAt = function (curve, u)740
{741
if (!curve)742
{ return null; }743
if (!curve.segments)744
{745
return { seg: curve, t: u < 0 ? 0 : (u > 1 ? 1 : u), scale: 1 };746
}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 };759
};760
761
/** Point on a (possibly multi-segment C¹) curve at u ∈ [0, 1]. */762
Online.curvePoint = function (curve, u)763
{764
var at = this._curveSegmentAt(curve, u);765
if (!at)766
{ 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 };773
};774
775
/** dP/du of the curve (C¹ across joints). */776
Online.curveDeriv = function (curve, u)777
{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 };611
};784
};612
785
613
/** Sample a player↔enemy path; used by tests and the ticker. */786
/**787
* Sample the smooth around-field curve at u ∈ [0, 1] (t1134).788
* `clearance` may be a number or an options object.789
*/614
Online.aroundFieldPoint = function (start, end, u, field, clearance)790
Online.aroundFieldPoint = function (start, end, u, field, clearance)615
{791
{616
var pts = this.waypointsAroundField(start, end, field, clearance);792
var opts = (clearance && typeof clearance === 'object') ? clearance : { clearance: clearance };617
if (pts.length === 2)793
var curve = opts.curve || this.makeAroundCurve(start, end, field, opts);794
return this.curvePoint(curve, u);795
};796
797
/** Guide points (start, joints, end) for older tests. */798
Online.waypointsAroundField = function (start, end, field, clearance)799
{800
var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 64 });801
if (c.segments && c.segments.length)618
{802
{619
var h = this.arcHeight(start, end);803
var pts = [{ x: c.segments[0].p0.x, y: c.segments[0].p0.y }];620
return this.arcPoint(start, end, u, h);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;621
}807
}622
return this.pointOnPath(pts, u);808
return [c.p0, c.p1, c.p2, c.p3];809
};810
811
Online.pointOnPath = function (points, u)812
{813
// Legacy polyline; new flights use cubicBezier.814
u = u < 0 ? 0 : (u > 1 ? 1 : u);815
if (!points || !points.length)816
{ return { x: 0, y: 0 }; }817
if (points.length === 4 && points[0] && points[3])818
{ return this.cubicBezier(points[0], points[1], points[2], points[3], u); }819
if (points.length === 1)820
{ return { x: points[0].x, y: points[0].y }; }821
var n = points.length - 1;822
var at = u * n;823
var i = Math.floor(at);824
if (i >= n)825
{ i = n - 1; }826
var t = at - i;827
var a = points[i];828
var b = points[i + 1];829
return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };830
};831
832
/** Viewport centre of a field cell (y=0 is the bottom row). */833
Online.cellViewport = function (col, y, rect)834
{835
if (!rect)836
{837
var field = this.playerFieldRect();838
if (!field)839
{ return { x: 0, y: 0 }; }840
rect = field;841
}842
var w = rect.width != null ? rect.width : (rect.right - rect.left);843
var h = rect.height != null ? rect.height : (rect.bottom - rect.top);844
return {845
x: rect.left + ((col + 0.5) / 10) * w,846
y: rect.top + (1 - (y + 0.5) / 13) * h847
};848
};849
850
/** Handoff point of an incoming garbage block: y = 13, just into the column (t1135). */851
Online.fieldEntry = function (col)852
{853
return this.cellViewport(col == null ? 4.5 : col, 13);854
};855
856
Online.miniCellViewport = function (playerId, col, y)857
{858
var el = this.miniElement(playerId);859
var canvas = el && (el.querySelector ? el.querySelector('canvas') : null);860
var box = canvas || el;861
if (!box || !box.getBoundingClientRect)862
{ return { x: window.innerWidth / 2, y: 48 }; }863
return this.cellViewport(col == null ? 4.5 : col, y == null ? 13 : y, box.getBoundingClientRect());864
};865
866
Online._fallSpeedPx = function (field, duration)867
{868
var px = 40;869
if (field && field.height)870
{ px = field.height / 13; }871
else if (typeof app !== 'undefined' && app.tile)872
{ px = app.tile; }873
var cells = 13 / 3;874
if (typeof app !== 'undefined' && app.planet && app.planet.fallSpeed)875
{ cells = app.planet.fallSpeed; }876
duration = duration || this.FLIGHT_DURATION;877
// Distance of P2 from P3 so C'(1)/duration ≈ fall speed (t1135).878
return (cells * px * duration) / 3;623
};879
};624
880
625
Online.rememberLandCol = function (id, col)881
Online.rememberLandCol = function (id, col)@@ -687,17 +943,17 @@ Online._miniAnchor = function (playerId, dir)687
943
688
Online._fieldTop = function (col)944
Online._fieldTop = function (col)689
{945
{690
return this._flightStart(col);946
return this.fieldEntry(col);691
};947
};692
948
693
/**949
/**694
* Animate shot results. `locals` is a map of client_id → {col, y, color,950
* Animate shot results. `locals` is a map of client_id → {col, y, color,695
* startX, startY, from_id} in viewport pixels.951
* startX, startY, from_id} in viewport pixels.696
*952
*697
* Visual-only (t1125): a parabolic arc whose scale goes from mini-cell953
* Visual-only: a smooth cubic that leaves the real cell, arcs around698
* to full tile (or the reverse). Enemy-to-enemy arcs stay small and954
* the field, and dives into the landing column at the spawn pose699
* are drawn behind the player's field. Delivery timing is unchanged;955
* (t1134 / t1135). Enemy-to-enemy arcs stay small and are drawn700
* a late-seen flight starts mid-arc via `age`.956
* behind the player's field. A late-seen flight starts mid-arc via `age`.701
*/957
*/702
Online.spawnFlights = function (results, locals)958
Online.spawnFlights = function (results, locals)703
{959
{@@ -708,6 +964,8 @@ Online.spawnFlights = function (results, locals)708
{ return; }964
{ return; }709
if (!this._flightSeen)965
if (!this._flightSeen)710
{ this._flightSeen = Object.create(null); }966
{ this._flightSeen = Object.create(null); }967
if (!this._previewIds)968
{ this._previewIds = Object.create(null); }711
var now = performance.now();969
var now = performance.now();712
var full = this._fullTileSize();970
var full = this._fullTileSize();713
for (var i = 0; i < results.length; i++)971
for (var i = 0; i < results.length; i++)@@ -715,7 +973,18 @@ Online.spawnFlights = function (results, locals)715
var r = results[i];973
var r = results[i];716
var sid = String(r.id != null ? r.id : (r.client_id || ('tmp-' + i)));974
var sid = String(r.id != null ? r.id : (r.client_id || ('tmp-' + i)));717
if (this._flightSeen[sid])975
if (this._flightSeen[sid])718
{ continue; }976
{977
if (this._previewIds[sid])978
{979
var existing = this._findFlight(sid);980
if (existing)981
{982
var meta0 = locals[r.client_id] || locals[sid] || {};983
this.retargetFlight(existing, r, meta0, now);984
}985
}986
continue;987
}719
this._flightSeen[sid] = now;988
this._flightSeen[sid] = now;720
989
721
var meta = locals[r.client_id] || locals[sid] || {};990
var meta = locals[r.client_id] || locals[sid] || {};@@ -742,11 +1011,15 @@ Online.spawnFlights = function (results, locals)742
{ this.rememberLandCol(r.client_id, landCol); }1011
{ this.rememberLandCol(r.client_id, landCol); }743
}1012
}744
1013
1014
var startCol = meta.col != null ? meta.col : r.col;1015
var startY = meta.y != null ? meta.y : r.y;745
var start;1016
var start;746
if (meta.startX != null && isFinite(meta.startX))1017
if (meta.startX != null && isFinite(meta.startX))747
{ start = { x: meta.startX, y: meta.startY }; }1018
{ start = { x: meta.startX, y: meta.startY }; }748
else if (kind === 'player-to-enemy' || kind === 'player-miss')1019
else if (kind === 'player-to-enemy' || kind === 'player-miss')749
{ start = this._flightStart(meta.col != null ? meta.col : r.col); }1020
{ start = this._flightStart(startCol, startY); }1021
else if (startCol != null || startY != null)1022
{ start = this.miniCellViewport(fromId, startCol, startY != null ? startY : 12); }750
else1023
else751
{ start = this._miniAnchor(fromId, 'out'); }1024
{ start = this._miniAnchor(fromId, 'out'); }752
1025
@@ -754,7 +1027,7 @@ Online.spawnFlights = function (results, locals)754
if (miss)1027
if (miss)755
{ end = this._missEnd(start, kind === 'enemy-miss'); }1028
{ end = this._missEnd(start, kind === 'enemy-miss'); }756
else if (kind === 'enemy-to-player')1029
else if (kind === 'enemy-to-player')757
{ end = this._fieldTop(landCol != null ? landCol : 4.5); }1030
{ end = this.fieldEntry(landCol != null ? landCol : 4.5); }758
else1031
else759
{ end = this._targetEnd(targetId, start, landCol); }1032
{ end = this._targetEnd(targetId, start, landCol); }760
1033
@@ -815,7 +1088,14 @@ Online.spawnFlights = function (results, locals)815
: this.arcHeight(start, end);1088
: this.arcHeight(start, end);816
var field = this.playerFieldRect();1089
var field = this.playerFieldRect();817
var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');1090
var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');818
var waypoints = around ? this.waypointsAroundField(start, end, field, height * 0.45) : null;1091
var clearance = Math.max(64, height * 0.45);1092
var endSpeed = this._fallSpeedPx(field, duration / 1000);1093
var curve = miss ? null : this.makeAroundCurve(start, end, around ? field : null, {1094
clearance: clearance,1095
startSpeedPx: endSpeed,1096
endSpeedPx: endSpeed,1097
duration: duration / 10001098
});819
this._flights.push({1099
this._flights.push({820
el: el,1100
el: el,821
start: start,1101
start: start,@@ -824,16 +1104,16 @@ Online.spawnFlights = function (results, locals)824
height: height,1104
height: height,825
t0: t0,1105
t0: t0,826
duration: duration,1106
duration: duration,827
spin: (Math.random() * 2 - 1) * 720,1107
spin: miss ? ((Math.random() * 2 - 1) * 720) : ((Math.random() * 2 - 1) * 120),828
miss: miss,1108
miss: miss,829
kind: kind,1109
kind: kind,830
startScale: startScale,1110
startScale: startScale,831
endScale: endScale,1111
endScale: endScale,832
angle: Math.random() * 360,1112
angle: 0,833
id: sid,1113
id: sid,834
landCol: landCol,1114
landCol: landCol,835
field: field,1115
field: field,836
waypoints: waypoints,1116
curve: curve,837
color: flightColor1117
color: flightColor838
});1118
});839
}1119
}@@ -850,30 +1130,17 @@ Online.rememberFlightLocal = function (shot, startX, startY)850
color: shot.color,1130
color: shot.color,851
startX: startX,1131
startX: startX,852
startY: startY,1132
startY: startY,853
from_id: shot.from_id1133
from_id: shot.from_id,1134
land_col: shot.land_col,1135
vy: shot.vy854
};1136
};855
};1137
};856
1138
857
Online._flightStart = function (col)1139
Online._flightStart = function (col, y)858
{1140
{859
var appRef = typeof app !== 'undefined' ? app : null;1141
if (y == null || !isFinite(y))860
if (appRef && appRef.scene && appRef.scene.canvas)1142
{ y = 12; }861
{1143
return this.cellViewport(col == null ? 4.5 : col, y);862
var canvas = appRef.scene.canvas;863
var rect = canvas.getBoundingClientRect();864
var c = col == null ? 4.5 : col;865
return {866
x: rect.left + (c + 0.5) * (rect.width / 10),867
y: rect.top + 4868
};869
}870
var field = document.querySelector('canvas');871
if (field)872
{873
var rect = field.getBoundingClientRect();874
return { x: rect.left + rect.width / 2, y: rect.top };875
}876
return { x: window.innerWidth / 2, y: window.innerHeight * 0.3 };877
};1144
};878
1145
879
Online._targetEnd = function (targetId, start, col)1146
Online._targetEnd = function (targetId, start, col)@@ -881,12 +1148,8 @@ Online._targetEnd = function (targetId, start, col)881
var el = targetId && this.miniElement(targetId);1148
var el = targetId && this.miniElement(targetId);882
if (el)1149
if (el)883
{1150
{884
var canvas = el.querySelector('canvas') || el;1151
// Slightly inside the mini, in the landing column (t1135).885
var r = canvas.getBoundingClientRect();1152
return this.miniCellViewport(targetId, col, 12);886
var x = r.left + r.width / 2;887
if (col != null && isFinite(col))888
{ x = r.left + ((col + 0.5) / 10) * r.width; }889
return { x: x, y: r.top + 4 };890
}1153
}891
return { x: start.x + (Math.random() - 0.5) * 80, y: 40 };1154
return { x: start.x + (Math.random() - 0.5) * 80, y: 40 };892
};1155
};@@ -921,6 +1184,251 @@ Online._hitCtrl = function (start, end)921
};1184
};922
};1185
};923
1186
1187
Online._findFlight = function (sid)1188
{1189
sid = String(sid);1190
if (!this._flights)1191
{ return null; }1192
for (var i = 0; i < this._flights.length; i++)1193
{1194
if (String(this._flights[i].id) === sid)1195
{ return this._flights[i]; }1196
}1197
return null;1198
};1199
1200
/**1201
* Early visual takeover: a flying rocket near the top of a field1202
* becomes a flight sprite at its exact cell, so it is seen flying1203
* *out* rather than popping in above the canvas (t1134). When the1204
* shot is later dispatched, `retargetFlight` picks up from the1205
* current pose and aims at the real landing column.1206
*/1207
Online.previewExit = function (info)1208
{1209
if (!info || info.id == null)1210
{ return; }1211
var sid = String(info.id);1212
if (!this._flights)1213
{ this._flights = []; }1214
if (!this._flightSeen)1215
{ this._flightSeen = Object.create(null); }1216
if (!this._previewIds)1217
{ this._previewIds = Object.create(null); }1218
if (this._previewIds[sid] || this._flightSeen[sid])1219
{ return; }1220
var front = document.getElementById('flights');1221
if (!front || !info.start)1222
{ return; }1223
var start = info.start;1224
var field = this.playerFieldRect();1225
var clearance = 80;1226
if (typeof MiniLayout !== 'undefined' && MiniLayout.ARC_TOP_PAD)1227
{ clearance = Math.max(clearance, MiniLayout.ARC_TOP_PAD * 0.7); }1228
var apexY = (field ? field.top : start.y) - clearance;1229
if (apexY < 8)1230
{ apexY = Math.max(8, start.y - 80); }1231
var end = { x: start.x, y: apexY };1232
var duration = this.FLIGHT_DURATION * 1000;1233
var full = this._fullTileSize();1234
var el = document.createElement('img');1235
el.src = colorSpriteUrl(info.color);1236
el.alt = '';1237
el.className = 'flight';1238
el.style.width = full + 'px';1239
el.style.height = full + 'px';1240
front.appendChild(el);1241
var endSpeed = this._fallSpeedPx(field, this.FLIGHT_DURATION);1242
var curve = this.makeAroundCurve(start, end, info.from_id ? null : field, {1243
clearance: clearance,1244
startSpeedPx: endSpeed,1245
endSpeedPx: endSpeed1246
});1247
var now = performance.now();1248
var mini = info.from_id ? (this._miniCellSize(info.from_id) / full) : 1;1249
if (mini < 0.12)1250
{ mini = 0.2; }1251
this._flights.push({1252
el: el,1253
start: start,1254
end: end,1255
ctrl: null,1256
height: clearance,1257
t0: now,1258
duration: duration,1259
spin: (Math.random() * 2 - 1) * 90,1260
miss: false,1261
kind: info.from_id ? 'enemy-to-enemy' : 'player-to-enemy',1262
startScale: mini,1263
endScale: mini,1264
angle: 0,1265
id: sid,1266
landCol: null,1267
field: field,1268
curve: curve,1269
preview: true,1270
color: info.color,1271
from_id: info.from_id || null1272
});1273
this._previewIds[sid] = true;1274
this._flightSeen[sid] = now;1275
};1276
1277
/** True while a field sprite should stay hidden — the flight is its visual. */1278
Online.hasExitVisual = function (id)1279
{1280
if (id == null)1281
{ return false; }1282
var sid = String(id);1283
if (this._previewIds && this._previewIds[sid])1284
{ return true; }1285
var f = this._findFlight(sid);1286
if (!f || f.miss)1287
{ return false; }1288
var raw = (performance.now() - f.t0) / f.duration;1289
return raw < 0.4;1290
};1291
1292
/**1293
* Once the shot is scored, pick up the preview at its current pose1294
* and fly the rest of the way to the real end (no jump).1295
*/1296
Online.retargetFlight = function (f, r, meta, now)1297
{1298
now = now || performance.now();1299
meta = meta || {};1300
var raw = (now - f.t0) / f.duration;1301
if (raw < 0)1302
{ raw = 0; }1303
if (raw > 0.95)1304
{ raw = 0.95; }1305
var u = this.smoothParam(raw, 0.9, 0.9);1306
var pt = f.curve1307
? this.curvePoint(f.curve, u)1308
: { x: f.start.x, y: f.start.y };1309
var curScale = f.startScale + (f.endScale - f.startScale) * u;1310
1311
var fromId = r.from_id || r.from_user || meta.from_id || f.from_id || null;1312
var targetId = r.target_id != null ? r.target_id : r.to_user;1313
var miss = !!r.miss;1314
var kind = this.flightKind(fromId, targetId, miss);1315
var duration = (r.flight_duration || this.FLIGHT_DURATION) * 1000;1316
1317
var landCol = null;1318
if (r.land_col != null && isFinite(r.land_col))1319
{ landCol = r.land_col | 0; }1320
else if (meta.land_col != null && isFinite(meta.land_col))1321
{ landCol = meta.land_col | 0; }1322
else if (f.landCol != null)1323
{ landCol = f.landCol; }1324
if (landCol != null)1325
{ this.rememberLandCol(String(f.id), landCol); }1326
1327
var end;1328
if (miss)1329
{ end = this._missEnd(pt, kind === 'enemy-miss'); }1330
else if (kind === 'enemy-to-player')1331
{ end = this.fieldEntry(landCol != null ? landCol : 4.5); }1332
else1333
{ end = this._targetEnd(targetId, pt, landCol); }1334
1335
var flightColor = miss1336
? (r.color != null ? r.color : meta.color)1337
: -1;1338
if (f.el && flightColor !== f.color)1339
{ f.el.src = colorSpriteUrl(flightColor); }1340
1341
var full = this._fullTileSize();1342
var mini = this._miniCellSize(fromId || targetId) / full;1343
if (mini < 0.12)1344
{ mini = 0.2; }1345
var endScale;1346
if (kind === 'player-to-enemy')1347
{ endScale = mini; }1348
else if (kind === 'enemy-to-player')1349
{ endScale = 1; }1350
else if (kind === 'enemy-to-enemy')1351
{ endScale = mini; }1352
else1353
{ endScale = 0; }1354
1355
var field = this.playerFieldRect();1356
var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');1357
var height = miss1358
? Math.max(40, Math.min(160, Math.abs(end.y - pt.y) * 0.35))1359
: this.arcHeight(pt, end);1360
var clearance = Math.max(64, height * 0.45);1361
var endSpeed = this._fallSpeedPx(field, duration / 1000);1362
var curve = miss ? null : this.makeAroundCurve(pt, end, around ? field : null, {1363
clearance: clearance,1364
startSpeedPx: endSpeed,1365
endSpeedPx: endSpeed1366
});1367
1368
f.start = pt;1369
f.end = end;1370
f.t0 = now;1371
f.duration = duration;1372
f.curve = curve;1373
f.ctrl = miss ? this._missCtrl(pt, end) : null;1374
f.height = height;1375
f.miss = miss;1376
f.kind = kind;1377
f.startScale = curScale;1378
f.endScale = endScale;1379
f.landCol = landCol;1380
f.field = field;1381
f.preview = false;1382
f.color = flightColor;1383
f.from_id = fromId;1384
if (this._previewIds)1385
{ delete this._previewIds[String(f.id)]; }1386
1387
if (kind === 'enemy-to-enemy' && f.el)1388
{1389
var back = document.getElementById('flights-back');1390
if (back && f.el.parentNode !== back)1391
{1392
f.el.className = 'flight flight-behind';1393
back.appendChild(f.el);1394
}1395
}1396
};1397
1398
/** Point and (optional) derivative of a flight at parameter u. */1399
Online.flightPointAt = function (f, u)1400
{1401
if (!f)1402
{ return { x: 0, y: 0 }; }1403
if (f.curve)1404
{ return this.curvePoint(f.curve, u); }1405
if (f.miss && f.ctrl)1406
{1407
var e = u;1408
var mt = 1 - e;1409
return {1410
x: mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x,1411
y: mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y1412
};1413
}1414
return this.aroundFieldPoint(f.start, f.end, u, f.field, (f.height || 80) * 0.45);1415
};1416
1417
Online.pointAllowedOnArc = function (p, field, start, end, slop)1418
{1419
if (!this.pointInFieldInterior(p.x, p.y, field, slop))1420
{ return true; }1421
if (!field)1422
{ return true; }1423
var w = (field.right - field.left) / 10;1424
var corridor = w * 0.7;1425
if (start && Math.abs(p.x - start.x) <= corridor)1426
{ return true; }1427
if (end && Math.abs(p.x - end.x) <= corridor)1428
{ return true; }1429
return false;1430
};1431
924
Online.tickFlights = function (now)1432
Online.tickFlights = function (now)925
{1433
{926
now = now || performance.now();1434
now = now || performance.now();@@ -933,11 +1441,14 @@ Online.tickFlights = function (now)933
{1441
{934
if (f.el && f.el.parentNode)1442
if (f.el && f.el.parentNode)935
{ f.el.parentNode.removeChild(f.el); }1443
{ f.el.parentNode.removeChild(f.el); }1444
if (this._previewIds)1445
{ delete this._previewIds[String(f.id)]; }936
continue;1446
continue;937
}1447
}938
if (raw < 0)1448
if (raw < 0)939
{ raw = 0; }1449
{ raw = 0; }940
var u = this.easeInOutCubic(raw);1450
// Non-zero endpoint speed so the handoff matches fallSpeed (t1135).1451
var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, 0.85, 0.85);941
var x, y;1452
var x, y;942
if (f.miss && f.ctrl)1453
if (f.miss && f.ctrl)943
{1454
{@@ -946,21 +1457,15 @@ Online.tickFlights = function (now)946
x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x;1457
x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x;947
y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y;1458
y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y;948
}1459
}949
else if (f.waypoints && f.waypoints.length > 2)1460
else if (f.curve)950
{1461
{951
var pt = this.pointOnPath(f.waypoints, u);1462
var pt = this.curvePoint(f.curve, u);952
x = pt.x;953
y = pt.y;954
}955
else if (f.waypoints && f.field)956
{957
var pt = this.aroundFieldPoint(f.start, f.end, u, f.field, (f.height || 80) * 0.45);958
x = pt.x;1463
x = pt.x;959
y = pt.y;1464
y = pt.y;960
}1465
}961
else1466
else962
{1467
{963
var pt = this.arcPoint(f.start, f.end, u, f.height || 80);1468
var pt = this.aroundFieldPoint(f.start, f.end, u, f.field, (f.height || 80) * 0.45);964
x = pt.x;1469
x = pt.x;965
y = pt.y;1470
y = pt.y;966
}1471
}@@ -1001,6 +1506,7 @@ Online.clearFlights = function ()1001
this._flightLocals = {};1506
this._flightLocals = {};1002
this._flightSeen = Object.create(null);1507
this._flightSeen = Object.create(null);1003
this._pendingLandCol = Object.create(null);1508
this._pendingLandCol = Object.create(null);1509
this._previewIds = Object.create(null);1004
};1510
};1005
1511
1006
if (typeof globalThis !== 'undefined')1512
if (typeof globalThis !== 'undefined')