fix: a worker that was down runs one barbarian wave for all it missed, then the next comes an interval from now

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-03 09:36:26 +01:00
parent e1121056c0
commit b3a73c647d
3 changed files with 19 additions and 3 deletions

View File

@ -20,8 +20,7 @@ final class Waves
{ {
/** /**
* Start the timer of every open world that has none, and run each world's wave once * Start the timer of every open world that has none, and run each world's wave once
* it is due: one wave per world and tick, so a worker that was down catches up loop * it is due.
* by loop.
*/ */
public static function tick(CarbonImmutable $now): void public static function tick(CarbonImmutable $now): void
{ {
@ -90,6 +89,10 @@ final class Waves
// Back to an unpredictable stream for everything after the wave. // Back to an unpredictable stream for everything after the wave.
mt_srand(); mt_srand();
} }
// A worker that was down runs one wave, not every one it missed.
if ($next->lessThanOrEqualTo($now)) {
$next = $now->addSeconds($next->getTimestamp() - $at->getTimestamp());
}
$world->update(['barbarians_at' => $next]); $world->update(['barbarians_at' => $next]);
return array_keys($neighbours); return array_keys($neighbours);

View File

@ -212,7 +212,7 @@ The world grows the way Die Stämme's does (measured on de261, see the research
- **Frontier** (`Frontier`): a new player's village lands at a random angle on a ring of radius `start_radius + sqrt(villages / (π · density))`, ± `band` fields, where `villages` counts every village of the world, barbarians included. `density` is DS's 0.29 villages per field (about one in 3.4 fields). The first tries keep clear of the 8 fields around other players; barbarians may stand right next to a new village. If the ring has no room, the nearest free field around 0|0 is taken. - **Frontier** (`Frontier`): a new player's village lands at a random angle on a ring of radius `start_radius + sqrt(villages / (π · density))`, ± `band` fields, where `villages` counts every village of the world, barbarians included. `density` is DS's 0.29 villages per field (about one in 3.4 fields). The first tries keep clear of the 8 fields around other players; barbarians may stand right next to a new village. If the ring has no room, the nearest free field around 0|0 is taken.
- **Direction**: when joining (`world.join {world_id, direction}`) a player picks random, nw, ne, sw or se, and the angle stays inside that quarter. - **Direction**: when joining (`world.join {world_id, direction}`) a player picks random, nw, ne, sw or se, and the angle stays inside that quarter.
- **Barbarians come with players** (`SpawnBarbarians`): each join brings `barbarians.per_join` of them on average (1.3: one, and a second with 30% chance, as in DS), placed within `barbarians.near` fields of the new village so a small world has something to loot from the start (DS scatters them on the frontier instead; with thousands of players that ends up the same). Their buildings start at random levels up to `max_level`, and each has a `barbarian.grows` event: every `growth.every_hours` (at world speed) one building goes up a level, up to `growth.max_level`. Bonus villages (DS adds 0.4 per join) come later. - **Barbarians come with players** (`SpawnBarbarians`): each join brings `barbarians.per_join` of them on average (1.3: one, and a second with 30% chance, as in DS), placed within `barbarians.near` fields of the new village so a small world has something to loot from the start (DS scatters them on the frontier instead; with thousands of players that ends up the same). Their buildings start at random levels up to `max_level`, and each has a `barbarian.grows` event: every `growth.every_hours` (at world speed) one building goes up a level, up to `growth.max_level`. Bonus villages (DS adds 0.4 per join) come later.
- **Barbarian waves** (`BarbarianWaves`, `Waves`): joins alone bring nothing new to a world with few players, so barbarian villages also come in waves. Every `waves.every_days` (5 to 9 world days, at world speed, drawn evenly per wave) each active player gets `per_player` of them (1.5: one, and a second with 50% chance), each within `near` (8) fields of a random village of theirs, placed and started like the ones that come with a join. Active means seen within `world.inactive_days`; when that rule is off, every player with a village counts. The world keeps its next wave in `barbarians_at`. The `game:events` worker ticks `Waves::tick` before every loop: it runs a due wave with the world row locked and plans the next from a draw seeded by the world and the wave, so a replay draws the same. A worker that was down catches up one wave per loop. A world without a time gets one an interval out, on deploy (`WorldSeeder`) or from the worker for a new or reset world. A wave without active players brings nothing, so a world still starts empty. Ended worlds get none. Each player village that got new neighbours is pushed `village:updated`, so an open client refetches. - **Barbarian waves** (`BarbarianWaves`, `Waves`): joins alone bring nothing new to a world with few players, so barbarian villages also come in waves. Every `waves.every_days` (5 to 9 world days, at world speed, drawn evenly per wave) each active player gets `per_player` of them (1.5: one, and a second with 50% chance), each within `near` (8) fields of a random village of theirs, placed and started like the ones that come with a join. Active means seen within `world.inactive_days`; when that rule is off, every player with a village counts. The world keeps its next wave in `barbarians_at`. The `game:events` worker ticks `Waves::tick` before every loop: it runs a due wave with the world row locked and plans the next from a draw seeded by the world and the wave, so a replay draws the same. A worker that was down runs one wave for all it missed, and the next comes an interval later. A world without a time gets one an interval out, on deploy (`WorldSeeder`) or from the worker for a new or reset world. A wave without active players brings nothing, so a world still starts empty. Ended worlds get none. Each player village that got new neighbours is pushed `village:updated`, so an open client refetches.
- **Garrisons** (`BarbarianGarrison`, `Barbarians`): a barbarian village holds troops, so raiding it takes scouting and an army sized for it. Full strength is `garrison.per_point × (points − free_points)` heads, split by the village's profile (spearmen, swordsmen, archers or riders, fixed by its id, so a scout report tells what to send), and its upkeep stays under `max_food_share` (50%) of the village's food budget, so it never starves. A new barbarian village starts at full strength. After a battle there, or when it grows, a `barbarian.regroups` event gives back `regroup.share` (25%, at least one per unit) every `regroup.every_hours` (2) until it is full again. Their stock grows from their own buildings, settled like any village, up to their storage. Barbarian villages from before garrisons get theirs on deploy (`WorldSeeder`). - **Garrisons** (`BarbarianGarrison`, `Barbarians`): a barbarian village holds troops, so raiding it takes scouting and an army sized for it. Full strength is `garrison.per_point × (points − free_points)` heads, split by the village's profile (spearmen, swordsmen, archers or riders, fixed by its id, so a scout report tells what to send), and its upkeep stays under `max_food_share` (50%) of the village's food budget, so it never starves. A new barbarian village starts at full strength. After a battle there, or when it grows, a `barbarian.regroups` event gives back `regroup.share` (25%, at least one per unit) every `regroup.every_hours` (2) until it is full again. Their stock grows from their own buildings, settled like any village, up to their storage. Barbarian villages from before garrisons get theirs on deploy (`WorldSeeder`).
- A village whose player has been inactive for too long turns barbarian (M6). - A village whose player has been inactive for too long turns barbarian (M6).
- `php artisan game:reset-world {slug}` wipes a world back to empty (villages, players, movements, reports, events) and keeps user accounts and the world's config. - `php artisan game:reset-world {slug}` wipes a world back to empty (villages, players, movements, reports, events) and keeps user accounts and the world's config.

View File

@ -129,3 +129,16 @@ it('plans the first wave on seed and after a reset, and none while waves are off
Waves::tick(now()->toImmutable()); Waves::tick(now()->toImmutable());
expect($w1->fresh()->barbarians_at->between(now()->addDays(5), now()->addDays(9)))->toBeTrue(); expect($w1->fresh()->barbarians_at->between(now()->addDays(5), now()->addDays(9)))->toBeTrue();
}); });
it('runs one wave for a worker that was down for weeks, and plans the next from now', function () {
$village = startVillage();
$world = $village->world->fresh();
$this->travelTo($world->barbarians_at->addDays(60));
$this->artisan('game:events --once')->assertSuccessful();
Waves::tick(now()->toImmutable());
// The crown forts that came meanwhile are no wave.
expect(waveBarbarians($world)->where('crown', false))->toHaveCount(2)
->and($world->fresh()->barbarians_at->between(now()->addDays(5), now()->addDays(9)))->toBeTrue();
});