BlueGit

projects-timfalken-com meteors

chore: rebuild game.js with landing-column arcs (t1126–t1130)

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
830561e599b0be5de0a194d4165aded4c9cc643d
Parent
0875b5d9
1 file changed +283 −33
build/game.js +283 −33
@@ -2745,29 +2745,51 @@ GameCore.prototype.fieldSnapshot = function ()
2745 2745 * `col` is optional; a random eligible column is used otherwise.
2746 2746 * Returns the new block, or null if every column is locked or full.
2747 2747 */
2748 GameCore.prototype.ingestGarbage = function (col)
2748 /**
2749 * Open columns a garbage block can fall into (not locked, not full).
2750 */
2751 GameCore.prototype.eligibleGarbageColumns = function ()
2749 2752 {
2750 if (this.state !== 'playing')
2753 var eligible = [];
2754 for (var c = 0; c < this.width; c++)
2755 {
2756 if (this.isColumnLocked(c))
2757 { continue; }
2758 if (this.groundedCount(c) >= this.height)
2759 { continue; }
2760 eligible.push(c);
2761 }
2762 return eligible;
2763 };
2764
2765 /**
2766 * Choose a garbage column. If `preferred` is still open, it wins;
2767 * otherwise a random eligible column (t1126). Returns null if none.
2768 */
2769 GameCore.prototype.pickGarbageColumn = function (preferred)
2770 {
2771 var eligible = this.eligibleGarbageColumns();
2772 if (!eligible.length)
2751 2773 { return null; }
2752 var chosen = null;
2753 if (col != null && col >= 0 && col < this.width
2754 && !this.isColumnLocked(col) && this.groundedCount(col) < this.height)
2755 { chosen = col | 0; }
2756 else
2774 if (preferred != null && isFinite(preferred))
2757 2775 {
2758 var eligible = [];
2759 for (var c = 0; c < this.width; c++)
2776 var want = preferred | 0;
2777 for (var i = 0; i < eligible.length; i++)
2760 2778 {
2761 if (this.isColumnLocked(c))
2762 { continue; }
2763 if (this.groundedCount(c) >= this.height)
2764 { continue; }
2765 eligible.push(c);
2779 if (eligible[i] === want)
2780 { return want; }
2766 2781 }
2767 if (!eligible.length)
2768 { return null; }
2769 chosen = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
2770 2782 }
2783 return eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
2784 };
2785
2786 GameCore.prototype.ingestGarbage = function (col)
2787 {
2788 if (this.state !== 'playing')
2789 { return null; }
2790 var chosen = this.pickGarbageColumn(col);
2791 if (chosen == null)
2792 { return null; }
2771 2793 var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING);
2772 2794 block.vy = -this.planet.fallSpeed;
2773 2795 block.flame = false;
@@ -3770,6 +3792,9 @@ var EDGE = 16;
3770 3792 var MIN_TILE_FOR_SIDES = 18;
3771 3793 var MIN_VIEWPORT_FOR_SIDES = 960;
3772 3794 var LABEL_H = 32;
3795 /* Extra space above the field in the wide (sides) layout so
3796 enemy↔player arcs can route around the top edge (t1129). */
3797 var ARC_TOP_PAD = 100;
3773 3798
3774 3799 function canvasSize (n, mode, availH)
3775 3800 {
@@ -3864,7 +3889,8 @@ function plan (vw, vh, n, hudH, margin)
3864 3889 var availHSide = Math.max(80, vh - hudH - margin);
3865 3890 var probe = canvasSize(n, 'sides', availHSide);
3866 3891 var availWSide = Math.max(80, vw - margin * 2 - 2 * (probe.outerW + GAP));
3867 var tileSide = Math.max(14, Math.floor(Math.min(availWSide / 10, availHSide / 13)));
3892 var availHForField = Math.max(80, availHSide - ARC_TOP_PAD);
3893 var tileSide = Math.max(14, Math.floor(Math.min(availWSide / 10, availHForField / 13)));
3868 3894 var fieldWSide = tileSide * 10;
3869 3895 var fieldHSide = tileSide * 13;
3870 3896 var leftover = (vw - fieldWSide) / 2;
@@ -3873,9 +3899,10 @@ function plan (vw, vh, n, hudH, margin)
3873 3899 && tileSide >= MIN_TILE_FOR_SIDES)
3874 3900 {
3875 3901 var fieldX = Math.floor((vw - fieldWSide) / 2);
3876 var fieldY = hudH + Math.floor((availHSide - fieldHSide) / 2);
3877 if (fieldY < hudH)
3878 { fieldY = hudH; }
3902 var fieldY = hudH + ARC_TOP_PAD
3903 + Math.floor((availHForField - fieldHSide) / 2);
3904 if (fieldY < hudH + ARC_TOP_PAD)
3905 { fieldY = hudH + ARC_TOP_PAD; }
3879 3906 return finish('sides', tileSide, fieldX, fieldY, 0, probe,
3880 3907 sidePositions(n, fieldX, fieldY, fieldWSide, fieldHSide, probe, hudH));
3881 3908 }
@@ -3907,6 +3934,7 @@ var api = {
3907 3934 EDGE: EDGE,
3908 3935 MIN_TILE_FOR_SIDES: MIN_TILE_FOR_SIDES,
3909 3936 MIN_VIEWPORT_FOR_SIDES: MIN_VIEWPORT_FOR_SIDES,
3937 ARC_TOP_PAD: ARC_TOP_PAD,
3910 3938 LABEL_H: LABEL_H,
3911 3939 canvasSize: canvasSize,
3912 3940 sidePositions: sidePositions,
@@ -4421,6 +4449,174 @@ Online.easeInOutCubic = function (t)
4421 4449 return 1 - (u * u * u) / 2;
4422 4450 };
4423 4451
4452 /** Bounding box of the player's field in viewport pixels. */
4453 Online.playerFieldRect = function ()
4454 {
4455 var appRef = typeof app !== 'undefined' ? app : null;
4456 var canvas = appRef && appRef.scene && appRef.scene.canvas;
4457 if (canvas && canvas.getBoundingClientRect)
4458 { return canvas.getBoundingClientRect(); }
4459 if (appRef && appRef.fieldW)
4460 {
4461 return {
4462 left: appRef.fieldX,
4463 top: appRef.fieldY,
4464 right: appRef.fieldX + appRef.fieldW,
4465 bottom: appRef.fieldY + appRef.fieldH,
4466 width: appRef.fieldW,
4467 height: appRef.fieldH
4468 };
4469 }
4470 return null;
4471 };
4472
4473 /**
4474 * True if (x, y) is inside the field's interior. The top edge (a few
4475 * pixels) is not interior: that is where arcs may land (t1128).
4476 */
4477 Online.pointInFieldInterior = function (x, y, field, slop)
4478 {
4479 if (!field)
4480 { return false; }
4481 slop = slop == null ? 6 : slop;
4482 return x > field.left + 0.5 && x < field.right - 0.5
4483 && y > field.top + slop && y < field.bottom - 0.5;
4484 };
4485
4486 /**
4487 * Waypoints that route a player↔enemy arc around the field, never
4488 * through its interior: up the outside, across above the top edge,
4489 * then into the target (t1128). If both ends already sit at or above
4490 * the top, a simple two-point path is enough.
4491 */
4492 Online.waypointsAroundField = function (start, end, field, clearance)
4493 {
4494 if (!start || !end)
4495 { return [start || { x: 0, y: 0 }, end || { x: 0, y: 0 }]; }
4496 if (!field)
4497 { return [start, end]; }
4498 clearance = clearance || 64;
4499 var top = field.top;
4500 var left = field.left;
4501 var right = field.right;
4502 var pad = 10;
4503 var apexY = Math.min(start.y, end.y, top) - clearance;
4504 if (apexY < 8)
4505 { apexY = Math.max(8, top - Math.max(24, clearance * 0.5)); }
4506
4507 if (start.y <= top + 6 && end.y <= top + 6)
4508 { return [start, end]; }
4509
4510 var w1x = start.x;
4511 var w2x = end.x;
4512 if (start.x < left)
4513 { w1x = Math.min(start.x, left - pad); }
4514 else if (start.x > right)
4515 { w1x = Math.max(start.x, right + pad); }
4516 if (end.x < left)
4517 { w2x = Math.min(end.x, left - pad); }
4518 else if (end.x > right)
4519 { w2x = Math.max(end.x, right + pad); }
4520
4521 return [
4522 start,
4523 { x: w1x, y: apexY },
4524 { x: w2x, y: apexY },
4525 end
4526 ];
4527 };
4528
4529 /**
4530 * Point at parameter u ∈ [0, 1] along a polyline, with a small upward
4531 * bulge on each segment so the path reads as a "boogje".
4532 */
4533 Online.pointOnPath = function (points, u)
4534 {
4535 u = u < 0 ? 0 : (u > 1 ? 1 : u);
4536 if (!points || !points.length)
4537 { return { x: 0, y: 0 }; }
4538 if (points.length === 1)
4539 { return { x: points[0].x, y: points[0].y }; }
4540 var lens = [];
4541 var total = 0;
4542 for (var i = 1; i < points.length; i++)
4543 {
4544 var dx = points[i].x - points[i - 1].x;
4545 var dy = points[i].y - points[i - 1].y;
4546 var L = Math.sqrt(dx * dx + dy * dy);
4547 if (L < 1)
4548 { L = 1; }
4549 lens.push(L);
4550 total += L;
4551 }
4552 var dist = u * total;
4553 var acc = 0;
4554 for (var i = 0; i < lens.length; i++)
4555 {
4556 if (acc + lens[i] >= dist || i === lens.length - 1)
4557 {
4558 var t = (dist - acc) / lens[i];
4559 if (t < 0)
4560 { t = 0; }
4561 if (t > 1)
4562 { t = 1; }
4563 var a = points[i];
4564 var b = points[i + 1];
4565 var bump = 4 * 16 * t * (1 - t);
4566 return {
4567 x: a.x + (b.x - a.x) * t,
4568 y: a.y + (b.y - a.y) * t - bump
4569 };
4570 }
4571 acc += lens[i];
4572 }
4573 var last = points[points.length - 1];
4574 return { x: last.x, y: last.y };
4575 };
4576
4577 /** Sample a player↔enemy path; used by tests and the ticker. */
4578 Online.aroundFieldPoint = function (start, end, u, field, clearance)
4579 {
4580 var pts = this.waypointsAroundField(start, end, field, clearance);
4581 if (pts.length === 2)
4582 {
4583 var h = this.arcHeight(start, end);
4584 return this.arcPoint(start, end, u, h);
4585 }
4586 return this.pointOnPath(pts, u);
4587 };
4588
4589 Online.rememberLandCol = function (id, col)
4590 {
4591 if (id == null || col == null || !isFinite(col))
4592 { return; }
4593 if (!this._pendingLandCol)
4594 { this._pendingLandCol = Object.create(null); }
4595 this._pendingLandCol[String(id)] = col | 0;
4596 };
4597
4598 /** Resolve the landing column for an incoming shot (t1126). */
4599 Online.takeLandCol = function (incoming)
4600 {
4601 if (!incoming)
4602 { return null; }
4603 if (incoming.land_col != null && isFinite(incoming.land_col))
4604 { return incoming.land_col | 0; }
4605 if (!this._pendingLandCol)
4606 { return null; }
4607 var keys = [];
4608 if (incoming.id != null)
4609 { keys.push(String(incoming.id)); }
4610 if (incoming.client_id)
4611 { keys.push(String(incoming.client_id)); }
4612 for (var i = 0; i < keys.length; i++)
4613 {
4614 if (this._pendingLandCol[keys[i]] != null)
4615 { return this._pendingLandCol[keys[i]]; }
4616 }
4617 return null;
4618 };
4619
4424 4620 Online._miniCellSize = function (playerId)
4425 4621 {
4426 4622 var el = this.miniElement(playerId);
@@ -4495,6 +4691,21 @@ Online.spawnFlights = function (results, locals)
4495 4691 if (r.age != null && isFinite(r.age) && r.age * 1000 >= duration)
4496 4692 { continue; }
4497 4693
4694 var landCol = null;
4695 if (r.land_col != null && isFinite(r.land_col))
4696 { landCol = r.land_col | 0; }
4697 else if (meta.land_col != null && isFinite(meta.land_col))
4698 { landCol = meta.land_col | 0; }
4699 else if (kind === 'enemy-to-player'
4700 && typeof app !== 'undefined' && app.core && app.core.pickGarbageColumn)
4701 { landCol = app.core.pickGarbageColumn(); }
4702 if (landCol != null)
4703 {
4704 this.rememberLandCol(sid, landCol);
4705 if (r.client_id)
4706 { this.rememberLandCol(r.client_id, landCol); }
4707 }
4708
4498 4709 var start;
4499 4710 if (meta.startX != null && isFinite(meta.startX))
4500 4711 { start = { x: meta.startX, y: meta.startY }; }
@@ -4507,9 +4718,15 @@ Online.spawnFlights = function (results, locals)
4507 4718 if (miss)
4508 4719 { end = this._missEnd(start, kind === 'enemy-miss'); }
4509 4720 else if (kind === 'enemy-to-player')
4510 { end = this._fieldTop(null); }
4721 { end = this._fieldTop(landCol != null ? landCol : 4.5); }
4511 4722 else
4512 { end = this._targetEnd(targetId, start); }
4723 { end = this._targetEnd(targetId, start, landCol); }
4724
4725 // Hits are ingested as grey garbage; draw them grey (t1130).
4726 // Misses keep the sender's colour — nothing is received.
4727 var flightColor = miss
4728 ? (r.color != null ? r.color : meta.color)
4729 : -1;
4513 4730
4514 4731 var startScale;
4515 4732 var endScale;
@@ -4544,7 +4761,7 @@ Online.spawnFlights = function (results, locals)
4544 4761
4545 4762 var layer = (kind === 'enemy-to-enemy' && back) ? back : front;
4546 4763 var el = document.createElement('img');
4547 el.src = colorSpriteUrl(r.color != null ? r.color : meta.color);
4764 el.src = colorSpriteUrl(flightColor);
4548 4765 el.alt = '';
4549 4766 el.className = 'flight' + (kind === 'enemy-to-enemy' ? ' flight-behind' : '');
4550 4767 el.style.width = full + 'px';
@@ -4560,6 +4777,9 @@ Online.spawnFlights = function (results, locals)
4560 4777 var height = miss
4561 4778 ? Math.max(40, Math.min(160, Math.abs(end.y - start.y) * 0.35))
4562 4779 : this.arcHeight(start, end);
4780 var field = this.playerFieldRect();
4781 var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');
4782 var waypoints = around ? this.waypointsAroundField(start, end, field, height * 0.45) : null;
4563 4783 this._flights.push({
4564 4784 el: el,
4565 4785 start: start,
@@ -4574,7 +4794,11 @@ Online.spawnFlights = function (results, locals)
4574 4794 startScale: startScale,
4575 4795 endScale: endScale,
4576 4796 angle: Math.random() * 360,
4577 id: sid
4797 id: sid,
4798 landCol: landCol,
4799 field: field,
4800 waypoints: waypoints,
4801 color: flightColor
4578 4802 });
4579 4803 }
4580 4804 this._flightLocals = {};
@@ -4616,14 +4840,17 @@ Online._flightStart = function (col)
4616 4840 return { x: window.innerWidth / 2, y: window.innerHeight * 0.3 };
4617 4841 };
4618 4842
4619 Online._targetEnd = function (targetId, start)
4843 Online._targetEnd = function (targetId, start, col)
4620 4844 {
4621 4845 var el = targetId && this.miniElement(targetId);
4622 4846 if (el)
4623 4847 {
4624 4848 var canvas = el.querySelector('canvas') || el;
4625 4849 var r = canvas.getBoundingClientRect();
4626 return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
4850 var x = r.left + r.width / 2;
4851 if (col != null && isFinite(col))
4852 { x = r.left + ((col + 0.5) / 10) * r.width; }
4853 return { x: x, y: r.top + 4 };
4627 4854 }
4628 4855 return { x: start.x + (Math.random() - 0.5) * 80, y: 40 };
4629 4856 };
@@ -4683,6 +4910,18 @@ Online.tickFlights = function (now)
4683 4910 x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x;
4684 4911 y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y;
4685 4912 }
4913 else if (f.waypoints && f.waypoints.length > 2)
4914 {
4915 var pt = this.pointOnPath(f.waypoints, u);
4916 x = pt.x;
4917 y = pt.y;
4918 }
4919 else if (f.waypoints && f.field)
4920 {
4921 var pt = this.aroundFieldPoint(f.start, f.end, u, f.field, (f.height || 80) * 0.45);
4922 x = pt.x;
4923 y = pt.y;
4924 }
4686 4925 else
4687 4926 {
4688 4927 var pt = this.arcPoint(f.start, f.end, u, f.height || 80);
@@ -4725,6 +4964,7 @@ Online.clearFlights = function ()
4725 4964 this._flights = [];
4726 4965 this._flightLocals = {};
4727 4966 this._flightSeen = Object.create(null);
4967 this._pendingLandCol = Object.create(null);
4728 4968 };
4729 4969
4730 4970 if (typeof globalThis !== 'undefined')
@@ -5184,6 +5424,9 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
5184 5424 target = targets[idx];
5185 5425 }
5186 5426 var clientId = fromId + '-' + shot.id + '-' + this.clock;
5427 var landCol = null;
5428 if (!miss && target && target.core && target.core.pickGarbageColumn)
5429 { landCol = target.core.pickGarbageColumn(); }
5187 5430 var result = {
5188 5431 client_id: clientId,
5189 5432 miss: miss,
@@ -5192,7 +5435,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
5192 5435 flight_duration: FLIGHT_DURATION,
5193 5436 from_id: fromId,
5194 5437 col: shot.col,
5195 y: shot.y
5438 y: shot.y,
5439 land_col: landCol
5196 5440 };
5197 5441 this.flightResults.push(result);
5198 5442 this.shotLocals.push({
@@ -5200,7 +5444,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
5200 5444 col: shot.col,
5201 5445 y: shot.y,
5202 5446 color: shot.color,
5203 from_id: fromId
5447 from_id: fromId,
5448 land_col: landCol
5204 5449 });
5205 5450 if (!miss && target)
5206 5451 {
@@ -5208,7 +5453,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
5208 5453 at: this.clock + FLIGHT_DURATION,
5209 5454 targetId: target.id,
5210 5455 color: shot.color,
5211 fromId: fromId
5456 fromId: fromId,
5457 landCol: landCol
5212 5458 });
5213 5459 }
5214 5460 }
@@ -5227,7 +5473,7 @@ OfflineBattle.prototype._deliver = function (playerCore)
5227 5473 }
5228 5474 var core = this.coreFor(ev.targetId, playerCore);
5229 5475 if (core && core.state === 'playing' && core.ingestGarbage)
5230 { core.ingestGarbage(); }
5476 { core.ingestGarbage(ev.landCol); }
5231 5477 }
5232 5478 this.pending = keep;
5233 5479 };
@@ -6716,7 +6962,8 @@ function tickOffline (dt)
6716 6962 color: s.color,
6717 6963 startX: start.x,
6718 6964 startY: start.y,
6719 from_id: s.from_id
6965 from_id: s.from_id,
6966 land_col: s.land_col
6720 6967 };
6721 6968 }
6722 6969 Online.spawnFlights(result.flights, locals);
@@ -7149,7 +7396,10 @@ function bindOnlineUi ()
7149 7396 if (!app.core || app.core.state !== 'playing')
7150 7397 { return; }
7151 7398 for (var i = 0; i < incoming.length; i++)
7152 { app.core.ingestGarbage(); }
7399 {
7400 var col = Online.takeLandCol ? Online.takeLandCol(incoming[i]) : incoming[i].land_col;
7401 app.core.ingestGarbage(col);
7402 }
7153 7403 };
7154 7404 Online.onFinished = function (lobby)
7155 7405 { showPodium(lobby); };
@@ -2745,29 +2745,51 @@ GameCore.prototype.fieldSnapshot = function ()
2745 * `col` is optional; a random eligible column is used otherwise.
2745 * `col` is optional; a random eligible column is used otherwise.
2746 * Returns the new block, or null if every column is locked or full.
2746 * Returns the new block, or null if every column is locked or full.
2747 */
2747 */
2748GameCore.prototype.ingestGarbage = function (col)
2748/**
2749 * Open columns a garbage block can fall into (not locked, not full).
2750 */
2751GameCore.prototype.eligibleGarbageColumns = function ()
2749{
2752{
2750 if (this.state !== 'playing')
2753 var eligible = [];
2754 for (var c = 0; c < this.width; c++)
2755 {
2756 if (this.isColumnLocked(c))
2757 { continue; }
2758 if (this.groundedCount(c) >= this.height)
2759 { continue; }
2760 eligible.push(c);
2761 }
2762 return eligible;
2763};
2764
2765/**
2766 * Choose a garbage column. If `preferred` is still open, it wins;
2767 * otherwise a random eligible column (t1126). Returns null if none.
2768 */
2769GameCore.prototype.pickGarbageColumn = function (preferred)
2770{
2771 var eligible = this.eligibleGarbageColumns();
2772 if (!eligible.length)
2751 { return null; }
2773 { return null; }
2752 var chosen = null;
2774 if (preferred != null && isFinite(preferred))
2753 if (col != null && col >= 0 && col < this.width
2754 && !this.isColumnLocked(col) && this.groundedCount(col) < this.height)
2755 { chosen = col | 0; }
2756 else
2757 {
2775 {
2758 var eligible = [];
2776 var want = preferred | 0;
2759 for (var c = 0; c < this.width; c++)
2777 for (var i = 0; i < eligible.length; i++)
2760 {
2778 {
2761 if (this.isColumnLocked(c))
2779 if (eligible[i] === want)
2762 { continue; }
2780 { return want; }
2763 if (this.groundedCount(c) >= this.height)
2764 { continue; }
2765 eligible.push(c);
2766 }
2781 }
2767 if (!eligible.length)
2768 { return null; }
2769 chosen = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
2770 }
2782 }
2783 return eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
2784};
2785
2786GameCore.prototype.ingestGarbage = function (col)
2787{
2788 if (this.state !== 'playing')
2789 { return null; }
2790 var chosen = this.pickGarbageColumn(col);
2791 if (chosen == null)
2792 { return null; }
2771 var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING);
2793 var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING);
2772 block.vy = -this.planet.fallSpeed;
2794 block.vy = -this.planet.fallSpeed;
2773 block.flame = false;
2795 block.flame = false;
@@ -3770,6 +3792,9 @@ var EDGE = 16;
3770var MIN_TILE_FOR_SIDES = 18;
3792var MIN_TILE_FOR_SIDES = 18;
3771var MIN_VIEWPORT_FOR_SIDES = 960;
3793var MIN_VIEWPORT_FOR_SIDES = 960;
3772var LABEL_H = 32;
3794var LABEL_H = 32;
3795/* Extra space above the field in the wide (sides) layout so
3796 enemy↔player arcs can route around the top edge (t1129). */
3797var ARC_TOP_PAD = 100;
3773
3798
3774function canvasSize (n, mode, availH)
3799function canvasSize (n, mode, availH)
3775{
3800{
@@ -3864,7 +3889,8 @@ function plan (vw, vh, n, hudH, margin)
3864 var availHSide = Math.max(80, vh - hudH - margin);
3889 var availHSide = Math.max(80, vh - hudH - margin);
3865 var probe = canvasSize(n, 'sides', availHSide);
3890 var probe = canvasSize(n, 'sides', availHSide);
3866 var availWSide = Math.max(80, vw - margin * 2 - 2 * (probe.outerW + GAP));
3891 var availWSide = Math.max(80, vw - margin * 2 - 2 * (probe.outerW + GAP));
3867 var tileSide = Math.max(14, Math.floor(Math.min(availWSide / 10, availHSide / 13)));
3892 var availHForField = Math.max(80, availHSide - ARC_TOP_PAD);
3893 var tileSide = Math.max(14, Math.floor(Math.min(availWSide / 10, availHForField / 13)));
3868 var fieldWSide = tileSide * 10;
3894 var fieldWSide = tileSide * 10;
3869 var fieldHSide = tileSide * 13;
3895 var fieldHSide = tileSide * 13;
3870 var leftover = (vw - fieldWSide) / 2;
3896 var leftover = (vw - fieldWSide) / 2;
@@ -3873,9 +3899,10 @@ function plan (vw, vh, n, hudH, margin)
3873 && tileSide >= MIN_TILE_FOR_SIDES)
3899 && tileSide >= MIN_TILE_FOR_SIDES)
3874 {
3900 {
3875 var fieldX = Math.floor((vw - fieldWSide) / 2);
3901 var fieldX = Math.floor((vw - fieldWSide) / 2);
3876 var fieldY = hudH + Math.floor((availHSide - fieldHSide) / 2);
3902 var fieldY = hudH + ARC_TOP_PAD
3877 if (fieldY < hudH)
3903 + Math.floor((availHForField - fieldHSide) / 2);
3878 { fieldY = hudH; }
3904 if (fieldY < hudH + ARC_TOP_PAD)
3905 { fieldY = hudH + ARC_TOP_PAD; }
3879 return finish('sides', tileSide, fieldX, fieldY, 0, probe,
3906 return finish('sides', tileSide, fieldX, fieldY, 0, probe,
3880 sidePositions(n, fieldX, fieldY, fieldWSide, fieldHSide, probe, hudH));
3907 sidePositions(n, fieldX, fieldY, fieldWSide, fieldHSide, probe, hudH));
3881 }
3908 }
@@ -3907,6 +3934,7 @@ var api = {
3907 EDGE: EDGE,
3934 EDGE: EDGE,
3908 MIN_TILE_FOR_SIDES: MIN_TILE_FOR_SIDES,
3935 MIN_TILE_FOR_SIDES: MIN_TILE_FOR_SIDES,
3909 MIN_VIEWPORT_FOR_SIDES: MIN_VIEWPORT_FOR_SIDES,
3936 MIN_VIEWPORT_FOR_SIDES: MIN_VIEWPORT_FOR_SIDES,
3937 ARC_TOP_PAD: ARC_TOP_PAD,
3910 LABEL_H: LABEL_H,
3938 LABEL_H: LABEL_H,
3911 canvasSize: canvasSize,
3939 canvasSize: canvasSize,
3912 sidePositions: sidePositions,
3940 sidePositions: sidePositions,
@@ -4421,6 +4449,174 @@ Online.easeInOutCubic = function (t)
4421 return 1 - (u * u * u) / 2;
4449 return 1 - (u * u * u) / 2;
4422};
4450};
4423
4451
4452/** Bounding box of the player's field in viewport pixels. */
4453Online.playerFieldRect = function ()
4454{
4455 var appRef = typeof app !== 'undefined' ? app : null;
4456 var canvas = appRef && appRef.scene && appRef.scene.canvas;
4457 if (canvas && canvas.getBoundingClientRect)
4458 { return canvas.getBoundingClientRect(); }
4459 if (appRef && appRef.fieldW)
4460 {
4461 return {
4462 left: appRef.fieldX,
4463 top: appRef.fieldY,
4464 right: appRef.fieldX + appRef.fieldW,
4465 bottom: appRef.fieldY + appRef.fieldH,
4466 width: appRef.fieldW,
4467 height: appRef.fieldH
4468 };
4469 }
4470 return null;
4471};
4472
4473/**
4474 * True if (x, y) is inside the field's interior. The top edge (a few
4475 * pixels) is not interior: that is where arcs may land (t1128).
4476 */
4477Online.pointInFieldInterior = function (x, y, field, slop)
4478{
4479 if (!field)
4480 { return false; }
4481 slop = slop == null ? 6 : slop;
4482 return x > field.left + 0.5 && x < field.right - 0.5
4483 && y > field.top + slop && y < field.bottom - 0.5;
4484};
4485
4486/**
4487 * Waypoints that route a player↔enemy arc around the field, never
4488 * through its interior: up the outside, across above the top edge,
4489 * then into the target (t1128). If both ends already sit at or above
4490 * the top, a simple two-point path is enough.
4491 */
4492Online.waypointsAroundField = function (start, end, field, clearance)
4493{
4494 if (!start || !end)
4495 { return [start || { x: 0, y: 0 }, end || { x: 0, y: 0 }]; }
4496 if (!field)
4497 { return [start, end]; }
4498 clearance = clearance || 64;
4499 var top = field.top;
4500 var left = field.left;
4501 var right = field.right;
4502 var pad = 10;
4503 var apexY = Math.min(start.y, end.y, top) - clearance;
4504 if (apexY < 8)
4505 { apexY = Math.max(8, top - Math.max(24, clearance * 0.5)); }
4506
4507 if (start.y <= top + 6 && end.y <= top + 6)
4508 { return [start, end]; }
4509
4510 var w1x = start.x;
4511 var w2x = end.x;
4512 if (start.x < left)
4513 { w1x = Math.min(start.x, left - pad); }
4514 else if (start.x > right)
4515 { w1x = Math.max(start.x, right + pad); }
4516 if (end.x < left)
4517 { w2x = Math.min(end.x, left - pad); }
4518 else if (end.x > right)
4519 { w2x = Math.max(end.x, right + pad); }
4520
4521 return [
4522 start,
4523 { x: w1x, y: apexY },
4524 { x: w2x, y: apexY },
4525 end
4526 ];
4527};
4528
4529/**
4530 * Point at parameter u ∈ [0, 1] along a polyline, with a small upward
4531 * bulge on each segment so the path reads as a "boogje".
4532 */
4533Online.pointOnPath = function (points, u)
4534{
4535 u = u < 0 ? 0 : (u > 1 ? 1 : u);
4536 if (!points || !points.length)
4537 { return { x: 0, y: 0 }; }
4538 if (points.length === 1)
4539 { return { x: points[0].x, y: points[0].y }; }
4540 var lens = [];
4541 var total = 0;
4542 for (var i = 1; i < points.length; i++)
4543 {
4544 var dx = points[i].x - points[i - 1].x;
4545 var dy = points[i].y - points[i - 1].y;
4546 var L = Math.sqrt(dx * dx + dy * dy);
4547 if (L < 1)
4548 { L = 1; }
4549 lens.push(L);
4550 total += L;
4551 }
4552 var dist = u * total;
4553 var acc = 0;
4554 for (var i = 0; i < lens.length; i++)
4555 {
4556 if (acc + lens[i] >= dist || i === lens.length - 1)
4557 {
4558 var t = (dist - acc) / lens[i];
4559 if (t < 0)
4560 { t = 0; }
4561 if (t > 1)
4562 { t = 1; }
4563 var a = points[i];
4564 var b = points[i + 1];
4565 var bump = 4 * 16 * t * (1 - t);
4566 return {
4567 x: a.x + (b.x - a.x) * t,
4568 y: a.y + (b.y - a.y) * t - bump
4569 };
4570 }
4571 acc += lens[i];
4572 }
4573 var last = points[points.length - 1];
4574 return { x: last.x, y: last.y };
4575};
4576
4577/** Sample a player↔enemy path; used by tests and the ticker. */
4578Online.aroundFieldPoint = function (start, end, u, field, clearance)
4579{
4580 var pts = this.waypointsAroundField(start, end, field, clearance);
4581 if (pts.length === 2)
4582 {
4583 var h = this.arcHeight(start, end);
4584 return this.arcPoint(start, end, u, h);
4585 }
4586 return this.pointOnPath(pts, u);
4587};
4588
4589Online.rememberLandCol = function (id, col)
4590{
4591 if (id == null || col == null || !isFinite(col))
4592 { return; }
4593 if (!this._pendingLandCol)
4594 { this._pendingLandCol = Object.create(null); }
4595 this._pendingLandCol[String(id)] = col | 0;
4596};
4597
4598/** Resolve the landing column for an incoming shot (t1126). */
4599Online.takeLandCol = function (incoming)
4600{
4601 if (!incoming)
4602 { return null; }
4603 if (incoming.land_col != null && isFinite(incoming.land_col))
4604 { return incoming.land_col | 0; }
4605 if (!this._pendingLandCol)
4606 { return null; }
4607 var keys = [];
4608 if (incoming.id != null)
4609 { keys.push(String(incoming.id)); }
4610 if (incoming.client_id)
4611 { keys.push(String(incoming.client_id)); }
4612 for (var i = 0; i < keys.length; i++)
4613 {
4614 if (this._pendingLandCol[keys[i]] != null)
4615 { return this._pendingLandCol[keys[i]]; }
4616 }
4617 return null;
4618};
4619
4424Online._miniCellSize = function (playerId)
4620Online._miniCellSize = function (playerId)
4425{
4621{
4426 var el = this.miniElement(playerId);
4622 var el = this.miniElement(playerId);
@@ -4495,6 +4691,21 @@ Online.spawnFlights = function (results, locals)
4495 if (r.age != null && isFinite(r.age) && r.age * 1000 >= duration)
4691 if (r.age != null && isFinite(r.age) && r.age * 1000 >= duration)
4496 { continue; }
4692 { continue; }
4497
4693
4694 var landCol = null;
4695 if (r.land_col != null && isFinite(r.land_col))
4696 { landCol = r.land_col | 0; }
4697 else if (meta.land_col != null && isFinite(meta.land_col))
4698 { landCol = meta.land_col | 0; }
4699 else if (kind === 'enemy-to-player'
4700 && typeof app !== 'undefined' && app.core && app.core.pickGarbageColumn)
4701 { landCol = app.core.pickGarbageColumn(); }
4702 if (landCol != null)
4703 {
4704 this.rememberLandCol(sid, landCol);
4705 if (r.client_id)
4706 { this.rememberLandCol(r.client_id, landCol); }
4707 }
4708
4498 var start;
4709 var start;
4499 if (meta.startX != null && isFinite(meta.startX))
4710 if (meta.startX != null && isFinite(meta.startX))
4500 { start = { x: meta.startX, y: meta.startY }; }
4711 { start = { x: meta.startX, y: meta.startY }; }
@@ -4507,9 +4718,15 @@ Online.spawnFlights = function (results, locals)
4507 if (miss)
4718 if (miss)
4508 { end = this._missEnd(start, kind === 'enemy-miss'); }
4719 { end = this._missEnd(start, kind === 'enemy-miss'); }
4509 else if (kind === 'enemy-to-player')
4720 else if (kind === 'enemy-to-player')
4510 { end = this._fieldTop(null); }
4721 { end = this._fieldTop(landCol != null ? landCol : 4.5); }
4511 else
4722 else
4512 { end = this._targetEnd(targetId, start); }
4723 { end = this._targetEnd(targetId, start, landCol); }
4724
4725 // Hits are ingested as grey garbage; draw them grey (t1130).
4726 // Misses keep the sender's colour — nothing is received.
4727 var flightColor = miss
4728 ? (r.color != null ? r.color : meta.color)
4729 : -1;
4513
4730
4514 var startScale;
4731 var startScale;
4515 var endScale;
4732 var endScale;
@@ -4544,7 +4761,7 @@ Online.spawnFlights = function (results, locals)
4544
4761
4545 var layer = (kind === 'enemy-to-enemy' && back) ? back : front;
4762 var layer = (kind === 'enemy-to-enemy' && back) ? back : front;
4546 var el = document.createElement('img');
4763 var el = document.createElement('img');
4547 el.src = colorSpriteUrl(r.color != null ? r.color : meta.color);
4764 el.src = colorSpriteUrl(flightColor);
4548 el.alt = '';
4765 el.alt = '';
4549 el.className = 'flight' + (kind === 'enemy-to-enemy' ? ' flight-behind' : '');
4766 el.className = 'flight' + (kind === 'enemy-to-enemy' ? ' flight-behind' : '');
4550 el.style.width = full + 'px';
4767 el.style.width = full + 'px';
@@ -4560,6 +4777,9 @@ Online.spawnFlights = function (results, locals)
4560 var height = miss
4777 var height = miss
4561 ? Math.max(40, Math.min(160, Math.abs(end.y - start.y) * 0.35))
4778 ? Math.max(40, Math.min(160, Math.abs(end.y - start.y) * 0.35))
4562 : this.arcHeight(start, end);
4779 : this.arcHeight(start, end);
4780 var field = this.playerFieldRect();
4781 var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy');
4782 var waypoints = around ? this.waypointsAroundField(start, end, field, height * 0.45) : null;
4563 this._flights.push({
4783 this._flights.push({
4564 el: el,
4784 el: el,
4565 start: start,
4785 start: start,
@@ -4574,7 +4794,11 @@ Online.spawnFlights = function (results, locals)
4574 startScale: startScale,
4794 startScale: startScale,
4575 endScale: endScale,
4795 endScale: endScale,
4576 angle: Math.random() * 360,
4796 angle: Math.random() * 360,
4577 id: sid
4797 id: sid,
4798 landCol: landCol,
4799 field: field,
4800 waypoints: waypoints,
4801 color: flightColor
4578 });
4802 });
4579 }
4803 }
4580 this._flightLocals = {};
4804 this._flightLocals = {};
@@ -4616,14 +4840,17 @@ Online._flightStart = function (col)
4616 return { x: window.innerWidth / 2, y: window.innerHeight * 0.3 };
4840 return { x: window.innerWidth / 2, y: window.innerHeight * 0.3 };
4617};
4841};
4618
4842
4619Online._targetEnd = function (targetId, start)
4843Online._targetEnd = function (targetId, start, col)
4620{
4844{
4621 var el = targetId && this.miniElement(targetId);
4845 var el = targetId && this.miniElement(targetId);
4622 if (el)
4846 if (el)
4623 {
4847 {
4624 var canvas = el.querySelector('canvas') || el;
4848 var canvas = el.querySelector('canvas') || el;
4625 var r = canvas.getBoundingClientRect();
4849 var r = canvas.getBoundingClientRect();
4626 return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
4850 var x = r.left + r.width / 2;
4851 if (col != null && isFinite(col))
4852 { x = r.left + ((col + 0.5) / 10) * r.width; }
4853 return { x: x, y: r.top + 4 };
4627 }
4854 }
4628 return { x: start.x + (Math.random() - 0.5) * 80, y: 40 };
4855 return { x: start.x + (Math.random() - 0.5) * 80, y: 40 };
4629};
4856};
@@ -4683,6 +4910,18 @@ Online.tickFlights = function (now)
4683 x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x;
4910 x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x;
4684 y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y;
4911 y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y;
4685 }
4912 }
4913 else if (f.waypoints && f.waypoints.length > 2)
4914 {
4915 var pt = this.pointOnPath(f.waypoints, u);
4916 x = pt.x;
4917 y = pt.y;
4918 }
4919 else if (f.waypoints && f.field)
4920 {
4921 var pt = this.aroundFieldPoint(f.start, f.end, u, f.field, (f.height || 80) * 0.45);
4922 x = pt.x;
4923 y = pt.y;
4924 }
4686 else
4925 else
4687 {
4926 {
4688 var pt = this.arcPoint(f.start, f.end, u, f.height || 80);
4927 var pt = this.arcPoint(f.start, f.end, u, f.height || 80);
@@ -4725,6 +4964,7 @@ Online.clearFlights = function ()
4725 this._flights = [];
4964 this._flights = [];
4726 this._flightLocals = {};
4965 this._flightLocals = {};
4727 this._flightSeen = Object.create(null);
4966 this._flightSeen = Object.create(null);
4967 this._pendingLandCol = Object.create(null);
4728};
4968};
4729
4969
4730if (typeof globalThis !== 'undefined')
4970if (typeof globalThis !== 'undefined')
@@ -5184,6 +5424,9 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
5184 target = targets[idx];
5424 target = targets[idx];
5185 }
5425 }
5186 var clientId = fromId + '-' + shot.id + '-' + this.clock;
5426 var clientId = fromId + '-' + shot.id + '-' + this.clock;
5427 var landCol = null;
5428 if (!miss && target && target.core && target.core.pickGarbageColumn)
5429 { landCol = target.core.pickGarbageColumn(); }
5187 var result = {
5430 var result = {
5188 client_id: clientId,
5431 client_id: clientId,
5189 miss: miss,
5432 miss: miss,
@@ -5192,7 +5435,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
5192 flight_duration: FLIGHT_DURATION,
5435 flight_duration: FLIGHT_DURATION,
5193 from_id: fromId,
5436 from_id: fromId,
5194 col: shot.col,
5437 col: shot.col,
5195 y: shot.y
5438 y: shot.y,
5439 land_col: landCol
5196 };
5440 };
5197 this.flightResults.push(result);
5441 this.flightResults.push(result);
5198 this.shotLocals.push({
5442 this.shotLocals.push({
@@ -5200,7 +5444,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
5200 col: shot.col,
5444 col: shot.col,
5201 y: shot.y,
5445 y: shot.y,
5202 color: shot.color,
5446 color: shot.color,
5203 from_id: fromId
5447 from_id: fromId,
5448 land_col: landCol
5204 });
5449 });
5205 if (!miss && target)
5450 if (!miss && target)
5206 {
5451 {
@@ -5208,7 +5453,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
5208 at: this.clock + FLIGHT_DURATION,
5453 at: this.clock + FLIGHT_DURATION,
5209 targetId: target.id,
5454 targetId: target.id,
5210 color: shot.color,
5455 color: shot.color,
5211 fromId: fromId
5456 fromId: fromId,
5457 landCol: landCol
5212 });
5458 });
5213 }
5459 }
5214 }
5460 }
@@ -5227,7 +5473,7 @@ OfflineBattle.prototype._deliver = function (playerCore)
5227 }
5473 }
5228 var core = this.coreFor(ev.targetId, playerCore);
5474 var core = this.coreFor(ev.targetId, playerCore);
5229 if (core && core.state === 'playing' && core.ingestGarbage)
5475 if (core && core.state === 'playing' && core.ingestGarbage)
5230 { core.ingestGarbage(); }
5476 { core.ingestGarbage(ev.landCol); }
5231 }
5477 }
5232 this.pending = keep;
5478 this.pending = keep;
5233};
5479};
@@ -6716,7 +6962,8 @@ function tickOffline (dt)
6716 color: s.color,
6962 color: s.color,
6717 startX: start.x,
6963 startX: start.x,
6718 startY: start.y,
6964 startY: start.y,
6719 from_id: s.from_id
6965 from_id: s.from_id,
6966 land_col: s.land_col
6720 };
6967 };
6721 }
6968 }
6722 Online.spawnFlights(result.flights, locals);
6969 Online.spawnFlights(result.flights, locals);
@@ -7149,7 +7396,10 @@ function bindOnlineUi ()
7149 if (!app.core || app.core.state !== 'playing')
7396 if (!app.core || app.core.state !== 'playing')
7150 { return; }
7397 { return; }
7151 for (var i = 0; i < incoming.length; i++)
7398 for (var i = 0; i < incoming.length; i++)
7152 { app.core.ingestGarbage(); }
7399 {
7400 var col = Online.takeLandCol ? Online.takeLandCol(incoming[i]) : incoming[i].land_col;
7401 app.core.ingestGarbage(col);
7402 }
7153 };
7403 };
7154 Online.onFinished = function (lobby)
7404 Online.onFinished = function (lobby)
7155 { showPodium(lobby); };
7405 { showPodium(lobby); };