diff --git a/app/Game/ConfigLint.php b/app/Game/ConfigLint.php index 95266ba..f63f4a9 100644 --- a/app/Game/ConfigLint.php +++ b/app/Game/ConfigLint.php @@ -27,6 +27,7 @@ final class ConfigLint ...self::timersRun($config), ...self::questsReachable($config, $rules), ...self::garrisonsRegroup($config), + ...self::wavesSpread($config), ...self::raidsPause($config), ...self::battlesSettle($config), ...self::rolesReachable($config), @@ -162,6 +163,31 @@ final class ConfigLint 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 * that exists, and a camp beaten down to nothing sends no band. diff --git a/app/Game/Map/BarbarianWaves.php b/app/Game/Map/BarbarianWaves.php new file mode 100644 index 0000000..896c1c8 --- /dev/null +++ b/app/Game/Map/BarbarianWaves.php @@ -0,0 +1,45 @@ +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); + } +} diff --git a/app/Game/Map/Frontier.php b/app/Game/Map/Frontier.php index 0a7e6ad..0b1f9a0 100644 --- a/app/Game/Map/Frontier.php +++ b/app/Game/Map/Frontier.php @@ -69,17 +69,17 @@ final class Frontier } /** - * A free field for a barbarian village close to a new player's village: between 1.5 - * fields and `barbarians.near` fields away, at a random angle. + * A free field for a barbarian village close to a player's village: between 1.5 + * fields and $reach fields away (`barbarians.near` unless given), at a random angle. * * @param array $taken * @param Closure(): float $random * @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); - $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++) { $angle = $random() * 2 * M_PI; diff --git a/config/game/world.php b/config/game/world.php index 69ac560..a723d2f 100644 --- a/config/game/world.php +++ b/config/game/world.php @@ -72,6 +72,18 @@ return [ 'near' => 4, 'max_level' => 3, '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 − // 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' diff --git a/tests/Unit/Game/BarbarianWavesTest.php b/tests/Unit/Game/BarbarianWavesTest.php new file mode 100644 index 0000000..571fa4d --- /dev/null +++ b/tests/Unit/Game/BarbarianWavesTest.php @@ -0,0 +1,32 @@ + ['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(); +}); diff --git a/tests/Unit/Game/ConfigLintTest.php b/tests/Unit/Game/ConfigLintTest.php index 44f9a76..e7aac8c 100644 --- a/tests/Unit/Game/ConfigLintTest.php +++ b/tests/Unit/Game/ConfigLintTest.php @@ -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('[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 () { $config = gameConfig(['raids' => ['every_hours' => [0, 5], 'scatter_below' => 0, 'mode' => 'teleport', 'dare' => -1]]); diff --git a/tests/Unit/Game/FrontierTest.php b/tests/Unit/Game/FrontierTest.php index 682f226..f8d95ee 100644 --- a/tests/Unit/Game/FrontierTest.php +++ b/tests/Unit/Game/FrontierTest.php @@ -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. 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.31)))->toBe(1); });