feat: pick a landing column at flight start and ingest there (t1126)
pickGarbageColumn prefers a given open column. Offline and the server choose land_col when the shot is fired (stored on the shot, shot_flights and incoming) so the arc and ingest share one value, including mid-arc joins via age.
@@ -74,6 +74,7 @@ CREATE TABLE IF NOT EXISTS shots (
74
74
CREATE INDEX IF NOT EXISTS shots_deliver ON shots (lobby_id, to_user, delivered, deliver_at);
75
75
SQL);
76
76
$this->ensureColumn('members', 'planet_id', "TEXT NOT NULL DEFAULT 'terra'");
77
$this->ensureColumn('shots', 'land_col', 'INTEGER');
77
78
}
78
79
79
80
private function ensureColumn(string $table, string $column, string $ddl): void
@@ -394,8 +395,8 @@ SQL);
394
395
$flight = (float) $lobby['flight_duration'];
395
396
$now = $this->nowf();
396
397
$ins = $this->pdo->prepare(
397
'INSERT INTO shots (lobby_id, from_user, to_user, color, client_id, created, deliver_at, delivered)
398
VALUES (?,?,?,?,?,?,?,0)'
398
'INSERT INTO shots (lobby_id, from_user, to_user, color, client_id, created, deliver_at, delivered, land_col)
399
VALUES (?,?,?,?,?,?,?,0,?)'
399
400
);
400
401
foreach ($shots as $shot) {
401
402
$color = isset($shot['color']) ? (int) $shot['color'] : -1;
@@ -403,13 +404,15 @@ SQL);
403
404
$miss = $targets === [] || (mt_rand() / mt_getrandmax()) < $missChance;
404
405
$to = null;
405
406
$toName = null;
407
$landCol = null;
406
408
if (!$miss) {
407
409
$pick = $targets[random_int(0, count($targets) - 1)];
408
410
$to = $pick['user_id'];
409
411
$toName = $pick['name'];
412
$landCol = random_int(0, 9);
410
413
}
411
414
$deliver = $now + $flight;
412
$ins->execute([$lobby['id'], $userId, $to, $color, $clientId, $now, $deliver]);
415
$ins->execute([$lobby['id'], $userId, $to, $color, $clientId, $now, $deliver, $landCol]);
413
416
$results[] = [
414
417
'id' => (int) $this->pdo->lastInsertId(),
415
418
'client_id' => $clientId,
@@ -422,6 +425,7 @@ SQL);
422
425
'color' => $color,
423
426
'flight_duration' => $flight,
424
427
'age' => 0.0,
428
'land_col' => $landCol,
425
429
];
426
430
}
427
431
return $results;
@@ -452,6 +456,7 @@ SQL);
452
456
'color' => (int) $r['color'],
453
457
'from_user' => $r['from_user'],
454
458
'client_id' => $r['client_id'],
459
'land_col' => isset($r['land_col']) && $r['land_col'] !== null ? (int) $r['land_col'] : null,
455
460
];
456
461
}
457
462
return $incoming;
@@ -603,6 +608,7 @@ SQL);
603
608
'flight_duration' => $flight,
604
609
'age' => $now - (float) $r['created'],
605
610
'deliver_at' => (float) $r['deliver_at'],
611
'land_col' => isset($r['land_col']) && $r['land_col'] !== null ? (int) $r['land_col'] : null,
606
612
];
607
613
}
608
614
return $out;
@@ -74,6 +74,7 @@ CREATE TABLE IF NOT EXISTS shots (74
CREATE INDEX IF NOT EXISTS shots_deliver ON shots (lobby_id, to_user, delivered, deliver_at);74
CREATE INDEX IF NOT EXISTS shots_deliver ON shots (lobby_id, to_user, delivered, deliver_at);75
SQL);75
SQL);76
$this->ensureColumn('members', 'planet_id', "TEXT NOT NULL DEFAULT 'terra'");76
$this->ensureColumn('members', 'planet_id', "TEXT NOT NULL DEFAULT 'terra'");77
$this->ensureColumn('shots', 'land_col', 'INTEGER');77
}78
}78
79
79
private function ensureColumn(string $table, string $column, string $ddl): void80
private function ensureColumn(string $table, string $column, string $ddl): void@@ -394,8 +395,8 @@ SQL);394
$flight = (float) $lobby['flight_duration'];395
$flight = (float) $lobby['flight_duration'];395
$now = $this->nowf();396
$now = $this->nowf();396
$ins = $this->pdo->prepare(397
$ins = $this->pdo->prepare(397
'INSERT INTO shots (lobby_id, from_user, to_user, color, client_id, created, deliver_at, delivered)398
'INSERT INTO shots (lobby_id, from_user, to_user, color, client_id, created, deliver_at, delivered, land_col)398
VALUES (?,?,?,?,?,?,?,0)'399
VALUES (?,?,?,?,?,?,?,0,?)'399
);400
);400
foreach ($shots as $shot) {401
foreach ($shots as $shot) {401
$color = isset($shot['color']) ? (int) $shot['color'] : -1;402
$color = isset($shot['color']) ? (int) $shot['color'] : -1;@@ -403,13 +404,15 @@ SQL);403
$miss = $targets === [] || (mt_rand() / mt_getrandmax()) < $missChance;404
$miss = $targets === [] || (mt_rand() / mt_getrandmax()) < $missChance;404
$to = null;405
$to = null;405
$toName = null;406
$toName = null;407
$landCol = null;406
if (!$miss) {408
if (!$miss) {407
$pick = $targets[random_int(0, count($targets) - 1)];409
$pick = $targets[random_int(0, count($targets) - 1)];408
$to = $pick['user_id'];410
$to = $pick['user_id'];409
$toName = $pick['name'];411
$toName = $pick['name'];412
$landCol = random_int(0, 9);410
}413
}411
$deliver = $now + $flight;414
$deliver = $now + $flight;412
$ins->execute([$lobby['id'], $userId, $to, $color, $clientId, $now, $deliver]);415
$ins->execute([$lobby['id'], $userId, $to, $color, $clientId, $now, $deliver, $landCol]);413
$results[] = [416
$results[] = [414
'id' => (int) $this->pdo->lastInsertId(),417
'id' => (int) $this->pdo->lastInsertId(),415
'client_id' => $clientId,418
'client_id' => $clientId,@@ -422,6 +425,7 @@ SQL);422
'color' => $color,425
'color' => $color,423
'flight_duration' => $flight,426
'flight_duration' => $flight,424
'age' => 0.0,427
'age' => 0.0,428
'land_col' => $landCol,425
];429
];426
}430
}427
return $results;431
return $results;@@ -452,6 +456,7 @@ SQL);452
'color' => (int) $r['color'],456
'color' => (int) $r['color'],453
'from_user' => $r['from_user'],457
'from_user' => $r['from_user'],454
'client_id' => $r['client_id'],458
'client_id' => $r['client_id'],459
'land_col' => isset($r['land_col']) && $r['land_col'] !== null ? (int) $r['land_col'] : null,455
];460
];456
}461
}457
return $incoming;462
return $incoming;@@ -603,6 +608,7 @@ SQL);603
'flight_duration' => $flight,608
'flight_duration' => $flight,604
'age' => $now - (float) $r['created'],609
'age' => $now - (float) $r['created'],605
'deliver_at' => (float) $r['deliver_at'],610
'deliver_at' => (float) $r['deliver_at'],611
'land_col' => isset($r['land_col']) && $r['land_col'] !== null ? (int) $r['land_col'] : null,606
];612
];607
}613
}608
return $out;614
return $out;@@ -2061,29 +2061,51 @@ GameCore.prototype.fieldSnapshot = function ()
2061
2061
* `col` is optional; a random eligible column is used otherwise.
2062
2062
* Returns the new block, or null if every column is locked or full.
2063
2063
*/
2064
GameCore.prototype.ingestGarbage = function (col)
2064
/**
2065
* Open columns a garbage block can fall into (not locked, not full).
2066
*/
2067
GameCore.prototype.eligibleGarbageColumns = function ()
2065
2068
{
2066
if (this.state !== 'playing')
2069
var eligible = [];
2070
for (var c = 0; c < this.width; c++)
2071
{
2072
if (this.isColumnLocked(c))
2073
{ continue; }
2074
if (this.groundedCount(c) >= this.height)
2075
{ continue; }
2076
eligible.push(c);
2077
}
2078
return eligible;
2079
};
2080
2081
/**
2082
* Choose a garbage column. If `preferred` is still open, it wins;
2083
* otherwise a random eligible column (t1126). Returns null if none.
2084
*/
2085
GameCore.prototype.pickGarbageColumn = function (preferred)
2086
{
2087
var eligible = this.eligibleGarbageColumns();
2088
if (!eligible.length)
2067
2089
{ return null; }
2068
var chosen = null;
2069
if (col != null && col >= 0 && col < this.width
2070
&& !this.isColumnLocked(col) && this.groundedCount(col) < this.height)
2071
{ chosen = col | 0; }
2072
else
2090
if (preferred != null && isFinite(preferred))
2073
2091
{
2074
var eligible = [];
2075
for (var c = 0; c < this.width; c++)
2092
var want = preferred | 0;
2093
for (var i = 0; i < eligible.length; i++)
2076
2094
{
2077
if (this.isColumnLocked(c))
2078
{ continue; }
2079
if (this.groundedCount(c) >= this.height)
2080
{ continue; }
2081
eligible.push(c);
2095
if (eligible[i] === want)
2096
{ return want; }
2082
2097
}
2083
if (!eligible.length)
2084
{ return null; }
2085
chosen = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
2086
2098
}
2099
return eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
2100
};
2101
2102
GameCore.prototype.ingestGarbage = function (col)
2103
{
2104
if (this.state !== 'playing')
2105
{ return null; }
2106
var chosen = this.pickGarbageColumn(col);
2107
if (chosen == null)
2108
{ return null; }
2087
2109
var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING);
2088
2110
block.vy = -this.planet.fallSpeed;
2089
2111
block.flame = false;
@@ -2061,29 +2061,51 @@ GameCore.prototype.fieldSnapshot = function ()2061
* `col` is optional; a random eligible column is used otherwise.2061
* `col` is optional; a random eligible column is used otherwise.2062
* Returns the new block, or null if every column is locked or full.2062
* Returns the new block, or null if every column is locked or full.2063
*/2063
*/2064
GameCore.prototype.ingestGarbage = function (col)2064
/**2065
* Open columns a garbage block can fall into (not locked, not full).2066
*/2067
GameCore.prototype.eligibleGarbageColumns = function ()2065
{2068
{2066
if (this.state !== 'playing')2069
var eligible = [];2070
for (var c = 0; c < this.width; c++)2071
{2072
if (this.isColumnLocked(c))2073
{ continue; }2074
if (this.groundedCount(c) >= this.height)2075
{ continue; }2076
eligible.push(c);2077
}2078
return eligible;2079
};2080
2081
/**2082
* Choose a garbage column. If `preferred` is still open, it wins;2083
* otherwise a random eligible column (t1126). Returns null if none.2084
*/2085
GameCore.prototype.pickGarbageColumn = function (preferred)2086
{2087
var eligible = this.eligibleGarbageColumns();2088
if (!eligible.length)2067
{ return null; }2089
{ return null; }2068
var chosen = null;2090
if (preferred != null && isFinite(preferred))2069
if (col != null && col >= 0 && col < this.width2070
&& !this.isColumnLocked(col) && this.groundedCount(col) < this.height)2071
{ chosen = col | 0; }2072
else2073
{2091
{2074
var eligible = [];2092
var want = preferred | 0;2075
for (var c = 0; c < this.width; c++)2093
for (var i = 0; i < eligible.length; i++)2076
{2094
{2077
if (this.isColumnLocked(c))2095
if (eligible[i] === want)2078
{ continue; }2096
{ return want; }2079
if (this.groundedCount(c) >= this.height)2080
{ continue; }2081
eligible.push(c);2082
}2097
}2083
if (!eligible.length)2084
{ return null; }2085
chosen = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];2086
}2098
}2099
return eligible[Math.floor(this.rng() * eligible.length) % eligible.length];2100
};2101
2102
GameCore.prototype.ingestGarbage = function (col)2103
{2104
if (this.state !== 'playing')2105
{ return null; }2106
var chosen = this.pickGarbageColumn(col);2107
if (chosen == null)2108
{ return null; }2087
var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING);2109
var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING);2088
block.vy = -this.planet.fallSpeed;2110
block.vy = -this.planet.fallSpeed;2089
block.flame = false;2111
block.flame = false;@@ -441,6 +441,9 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
441
441
target = targets[idx];
442
442
}
443
443
var clientId = fromId + '-' + shot.id + '-' + this.clock;
444
var landCol = null;
445
if (!miss && target && target.core && target.core.pickGarbageColumn)
446
{ landCol = target.core.pickGarbageColumn(); }
444
447
var result = {
445
448
client_id: clientId,
446
449
miss: miss,
@@ -449,7 +452,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
449
452
flight_duration: FLIGHT_DURATION,
450
453
from_id: fromId,
451
454
col: shot.col,
452
y: shot.y
455
y: shot.y,
456
land_col: landCol
453
457
};
454
458
this.flightResults.push(result);
455
459
this.shotLocals.push({
@@ -457,7 +461,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
457
461
col: shot.col,
458
462
y: shot.y,
459
463
color: shot.color,
460
from_id: fromId
464
from_id: fromId,
465
land_col: landCol
461
466
});
462
467
if (!miss && target)
463
468
{
@@ -465,7 +470,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC
465
470
at: this.clock + FLIGHT_DURATION,
466
471
targetId: target.id,
467
472
color: shot.color,
468
fromId: fromId
473
fromId: fromId,
474
landCol: landCol
469
475
});
470
476
}
471
477
}
@@ -484,7 +490,7 @@ OfflineBattle.prototype._deliver = function (playerCore)
484
490
}
485
491
var core = this.coreFor(ev.targetId, playerCore);
486
492
if (core && core.state === 'playing' && core.ingestGarbage)
487
{ core.ingestGarbage(); }
493
{ core.ingestGarbage(ev.landCol); }
488
494
}
489
495
this.pending = keep;
490
496
};
@@ -441,6 +441,9 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC441
target = targets[idx];441
target = targets[idx];442
}442
}443
var clientId = fromId + '-' + shot.id + '-' + this.clock;443
var clientId = fromId + '-' + shot.id + '-' + this.clock;444
var landCol = null;445
if (!miss && target && target.core && target.core.pickGarbageColumn)446
{ landCol = target.core.pickGarbageColumn(); }444
var result = {447
var result = {445
client_id: clientId,448
client_id: clientId,446
miss: miss,449
miss: miss,@@ -449,7 +452,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC449
flight_duration: FLIGHT_DURATION,452
flight_duration: FLIGHT_DURATION,450
from_id: fromId,453
from_id: fromId,451
col: shot.col,454
col: shot.col,452
y: shot.y455
y: shot.y,456
land_col: landCol453
};457
};454
this.flightResults.push(result);458
this.flightResults.push(result);455
this.shotLocals.push({459
this.shotLocals.push({@@ -457,7 +461,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC457
col: shot.col,461
col: shot.col,458
y: shot.y,462
y: shot.y,459
color: shot.color,463
color: shot.color,460
from_id: fromId464
from_id: fromId,465
land_col: landCol461
});466
});462
if (!miss && target)467
if (!miss && target)463
{468
{@@ -465,7 +470,8 @@ OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerC465
at: this.clock + FLIGHT_DURATION,470
at: this.clock + FLIGHT_DURATION,466
targetId: target.id,471
targetId: target.id,467
color: shot.color,472
color: shot.color,468
fromId: fromId473
fromId: fromId,474
landCol: landCol469
});475
});470
}476
}471
}477
}@@ -484,7 +490,7 @@ OfflineBattle.prototype._deliver = function (playerCore)484
}490
}485
var core = this.coreFor(ev.targetId, playerCore);491
var core = this.coreFor(ev.targetId, playerCore);486
if (core && core.state === 'playing' && core.ingestGarbage)492
if (core && core.state === 'playing' && core.ingestGarbage)487
{ core.ingestGarbage(); }493
{ core.ingestGarbage(ev.landCol); }488
}494
}489
this.pending = keep;495
this.pending = keep;490
};496
};