feat: barbarian waves config, the pure wave rules (interval, villages per player) and their lint

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-03 09:31:21 +01:00
parent 4c8a002718
commit 118104ddef
7 changed files with 133 additions and 4 deletions

View File

@ -27,6 +27,7 @@ final class ConfigLint
...self::timersRun($config), ...self::timersRun($config),
...self::questsReachable($config, $rules), ...self::questsReachable($config, $rules),
...self::garrisonsRegroup($config), ...self::garrisonsRegroup($config),
...self::wavesSpread($config),
...self::raidsPause($config), ...self::raidsPause($config),
...self::battlesSettle($config), ...self::battlesSettle($config),
...self::rolesReachable($config), ...self::rolesReachable($config),
@ -162,6 +163,31 @@ final class ConfigLint
return $problems; return $problems;
} }
/**
* Barbarian waves come apart in time, bring a number of villages that exists, and
* settle them off the player's own field.
*/
private static function wavesSpread(WorldConfig $config): array
{
$waves = $config->get('world.map.barbarians.waves');
if (! ($waves['enabled'] ?? false)) {
return [];
}
$problems = [];
[$min, $max] = ($waves['every_days'] ?? [0, 0]) + [0, 0];
if ($min <= 0 || $max < $min) {
$problems[] = '[barbarians.waves.every_days] must be two days above 0, the smaller first, so waves come apart in time.';
}
if (($waves['per_player'] ?? -1) < 0) {
$problems[] = '[barbarians.waves.per_player] cannot be negative.';
}
if (($waves['near'] ?? 0) < 1) {
$problems[] = '[barbarians.waves.near] must be at least 1 field.';
}
return $problems;
}
/** /**
* Raids come apart in time, with a warning, from camps in range, in a travel mode * Raids come apart in time, with a warning, from camps in range, in a travel mode
* that exists, and a camp beaten down to nothing sends no band. * that exists, and a camp beaten down to nothing sends no band.

View File

@ -0,0 +1,45 @@
<?php
namespace App\Game\Map;
use App\Game\WorldConfig;
/**
* Barbarian waves (world.map.barbarians.waves): every few world days new barbarian
* villages settle near the active players, so a world with few players keeps getting
* new targets. How long until the next wave, and how many come per player.
*/
final class BarbarianWaves
{
public function __construct(private readonly WorldConfig $config) {}
public function enabled(): bool
{
return (bool) $this->config->get('world.map.barbarians.waves.enabled', false);
}
/** Seconds until the next wave, from a roll in [0, 1): every_days drawn evenly, at world speed. */
public function interval(float $roll): int
{
[$min, $max] = $this->config->get('world.map.barbarians.waves.every_days', [7, 7]) + [7, 7];
return max(1, (int) round(($min + ($max - $min) * $roll) * 86400 / $this->config->speed()));
}
/**
* New villages for one player in a wave, from a roll in [0, 1): the whole part of
* per_player always, one more with the fractional part as its chance.
*/
public function perPlayer(float $roll): int
{
$average = max(0.0, (float) $this->config->get('world.map.barbarians.waves.per_player', 0));
return (int) floor($average) + ($roll < $average - floor($average) ? 1 : 0);
}
/** At most this many fields from the player's village they settle. */
public function near(): float
{
return (float) $this->config->get('world.map.barbarians.waves.near', 8);
}
}

View File

@ -69,17 +69,17 @@ final class Frontier
} }
/** /**
* A free field for a barbarian village close to a new player's village: between 1.5 * A free field for a barbarian village close to a player's village: between 1.5
* fields and `barbarians.near` fields away, at a random angle. * fields and $reach fields away (`barbarians.near` unless given), at a random angle.
* *
* @param array<string, bool> $taken * @param array<string, bool> $taken
* @param Closure(): float $random * @param Closure(): float $random
* @return array{0: int, 1: int}|null * @return array{0: int, 1: int}|null
*/ */
public function near(int $x, int $y, array $taken, Closure $random): ?array public function near(int $x, int $y, array $taken, Closure $random, ?float $reach = null): ?array
{ {
$bounds = MapBounds::of($this->config); $bounds = MapBounds::of($this->config);
$reach = (float) $this->config->get('world.map.barbarians.near', 4); $reach ??= (float) $this->config->get('world.map.barbarians.near', 4);
for ($try = 0; $try < self::TRIES; $try++) { for ($try = 0; $try < self::TRIES; $try++) {
$angle = $random() * 2 * M_PI; $angle = $random() * 2 * M_PI;

View File

@ -72,6 +72,18 @@ return [
'near' => 4, 'near' => 4,
'max_level' => 3, 'max_level' => 3,
'growth' => ['every_hours' => 8, 'max_level' => 10], 'growth' => ['every_hours' => 8, 'max_level' => 10],
// Waves keep a quiet world moving: every every_days (world days, at world speed,
// drawn evenly between the two for each wave) new barbarian villages settle near
// the active players, per_player on average for each (1.5 = one, and a second
// with 50% chance), within near fields of a random village of theirs. Active
// means seen within inactive_days (every player with a village when that is 0).
// They start like the ones that come with a join.
'waves' => [
'enabled' => true,
'every_days' => [5, 9],
'per_player' => 1.5,
'near' => 8,
],
// Troops a barbarian village holds at full strength: per_point × (points − // Troops a barbarian village holds at full strength: per_point × (points −
// free_points) heads, split by its profile's shares (the profile follows from // free_points) heads, split by its profile's shares (the profile follows from
// the village id), and never eating more than max_food_share of its farms' // the village id), and never eating more than max_food_share of its farms'

View File

@ -0,0 +1,32 @@
<?php
use App\Game\Map\BarbarianWaves;
it('comes every 5 to 9 world days, drawn evenly, at world speed', function () {
$waves = new BarbarianWaves(gameConfig());
$dev = new BarbarianWaves(gameConfig(['world' => ['speed' => 100]]));
expect($waves->interval(0.0))->toBe(5 * 86400)
->and($waves->interval(0.5))->toBe(7 * 86400)
->and($waves->interval(0.999999))->toBe(9 * 86400)
// On the speed-100 dev world: 1.2 to 2.16 real hours.
->and($dev->interval(0.0))->toBe(4320)
->and($dev->interval(0.999999))->toBe(7776);
});
it('brings 1.5 villages per player on average: one, and a second with 50% chance', function () {
$waves = new BarbarianWaves(gameConfig());
expect($waves->perPlayer(0.0))->toBe(2)
->and($waves->perPlayer(0.49))->toBe(2)
->and($waves->perPlayer(0.5))->toBe(1)
->and($waves->perPlayer(0.99))->toBe(1)
->and((new BarbarianWaves(gameConfig(['world' => ['map' => ['barbarians' => ['waves' => ['per_player' => 3]]]]])))->perPlayer(0.0))->toBe(3)
->and((new BarbarianWaves(gameConfig(['world' => ['map' => ['barbarians' => ['waves' => ['per_player' => 0]]]]])))->perPlayer(0.0))->toBe(0);
});
it('settles them within 8 fields, on by default', function () {
$waves = new BarbarianWaves(gameConfig());
expect($waves->near())->toBe(8.0)->and($waves->enabled())->toBeTrue();
});

View File

@ -84,6 +84,18 @@ it('reports garrisons that would starve their village or never regroup', functio
->toContain('Barbarian garrisons must regroup: [regroup.every_hours] and [regroup.share] above 0.') ->toContain('Barbarian garrisons must regroup: [regroup.every_hours] and [regroup.share] above 0.')
->toContain('[garrison.max_food_share] must stay below [economy.villager_share], or a garrison starves its village.'); ->toContain('[garrison.max_food_share] must stay below [economy.villager_share], or a garrison starves its village.');
}); });
it('reports barbarian waves that never pause, bring fewer than none or settle on the village', function () {
$waves = fn (array $waves) => ConfigLint::check(gameConfig(['world' => ['map' => ['barbarians' => ['waves' => $waves]]]]));
expect($waves(['every_days' => [0, 3], 'per_player' => -1, 'near' => 0.5]))->toBe([
'[barbarians.waves.every_days] must be two days above 0, the smaller first, so waves come apart in time.',
'[barbarians.waves.per_player] cannot be negative.',
'[barbarians.waves.near] must be at least 1 field.',
])
->and($waves(['every_days' => [9, 5]]))->toBe(['[barbarians.waves.every_days] must be two days above 0, the smaller first, so waves come apart in time.'])
->and($waves(['enabled' => false, 'every_days' => [0, 0]]))->toBe([]);
});
it('reports raids that never pause or that a beaten camp still sends', function () { it('reports raids that never pause or that a beaten camp still sends', function () {
$config = gameConfig(['raids' => ['every_hours' => [0, 5], 'scatter_below' => 0, 'mode' => 'teleport', 'dare' => -1]]); $config = gameConfig(['raids' => ['every_hours' => [0, 5], 'scatter_below' => 0, 'mode' => 'teleport', 'dare' => -1]]);

View File

@ -44,6 +44,8 @@ it('places barbarians a few fields from the new village and brings 1.3 per join
// Angle 0, distance 1.5 + 0.5 × 2.5 = 2.75: three fields east. // Angle 0, distance 1.5 + 0.5 × 2.5 = 2.75: three fields east.
expect($frontier->near(5, 5, [], sequence(0.0, 0.5)))->toBe([8, 5]) expect($frontier->near(5, 5, [], sequence(0.0, 0.5)))->toBe([8, 5])
// A wave reaches further: 1.5 + 0.5 × 6.5 = 4.75, five fields east.
->and($frontier->near(5, 5, [], sequence(0.0, 0.5), 8.0))->toBe([10, 5])
->and($frontier->barbariansPerJoin(sequence(0.29)))->toBe(2) ->and($frontier->barbariansPerJoin(sequence(0.29)))->toBe(2)
->and($frontier->barbariansPerJoin(sequence(0.31)))->toBe(1); ->and($frontier->barbariansPerJoin(sequence(0.31)))->toBe(1);
}); });