From 007e80b26a7fcf84c5428ae4a472e8e5b1c42a9c Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Sat, 31 Oct 2026 11:05:05 +0100 Subject: [PATCH] feat: raid camps size the village up first: a band that would lose with less than 30% of the defence's strength stays home until the next raid Co-Authored-By: Claude Opus 5.5 (1M context) --- app/Engine/Raiders.php | 26 +++++++++++++++++++++++++- app/Engine/Rally.php | 29 ++++++++++++++++++++++------- app/Game/Combat/Raids.php | 15 +++++++++++++-- app/Game/ConfigLint.php | 3 +++ config/game/raids.php | 5 +++++ tests/Feature/RaidTest.php | 24 +++++++++++++++++++++++- tests/Unit/Game/ConfigLintTest.php | 3 ++- tests/Unit/Game/RaidsTest.php | 12 ++++++++++++ 8 files changed, 105 insertions(+), 12 deletions(-) diff --git a/app/Engine/Raiders.php b/app/Engine/Raiders.php index 492b741..f90cb1b 100644 --- a/app/Engine/Raiders.php +++ b/app/Engine/Raiders.php @@ -2,6 +2,7 @@ namespace App\Engine; +use App\Game\Combat\CombatResolver; use App\Game\Combat\Raids; use App\Game\Travel\TravelTime; use App\Models\GameEvent; @@ -12,7 +13,8 @@ use Carbon\CarbonImmutable; /** * Raids on one player village (architecture.md, "Raids"). A `raid.planned` event on the * village picks the nearest barbarian camp and sends a band that gathers there first, - * so the village sees it coming hours ahead; the next raid is planned at once. Runs + * so the village sees it coming hours ahead, unless the band would be beaten hollow; + * the next raid is planned at once. Runs * with the village locked; the camp is only read. */ final class Raiders @@ -75,6 +77,12 @@ final class Raiders $travel = new TravelTime($this->sim->config()); $marches = $at->addSeconds($this->raids->gatherSeconds()); $arrives = $marches->addSeconds($travel->seconds($units, TravelTime::distance($camp->x, $camp->y, $this->village->x, $this->village->y), $this->raids->mode())); + // The camp sizes the village up first; against a defence far too strong it stays home. + if (! $this->dares($units, $wall, $arrives)) { + $this->schedule($at->addSeconds($interval)); + + return; + } $movement = Movement::create([ 'world_id' => $this->village->world_id, 'player_id' => null, @@ -116,6 +124,22 @@ final class Raiders return $this->raids->muster($movement->units, Barbarians::of($camp)->strength()); } + /** + * The battle on paper, as the full band would fight it on arrival: no luck, full + * morale, against everyone in the village now, its wall, a military village's bonus + * and the night at arrival. + * + * @param array $units + */ + private function dares(array $units, int $wall, CarbonImmutable $arrives): bool + { + $combat = new CombatResolver($this->sim->config()); + $fort = 1 + $this->sim->specialisation()->defenseBonus($this->sim->role()); + $battle = $combat->fight($units, Rally::of($this->village, $this->sim)->defense(), $wall, 1.0, 0.0, $combat->night($arrives) * $fort); + + return $this->raids->dares($battle); + } + /** The nearest barbarian village in range; crown forts never raid. */ private function camp(): ?Village { diff --git a/app/Engine/Rally.php b/app/Engine/Rally.php index 78db254..33f5b5c 100644 --- a/app/Engine/Rally.php +++ b/app/Engine/Rally.php @@ -65,6 +65,27 @@ final class Rally return $this->village->buildings->sum(fn (Building $b) => $rules->hides($b->type, $b->level) * $this->sim->share($b)); } + /** + * Who defends the village: every unit standing in it, support included, and the + * levied villagers as `militia`. + * + * @return array + */ + public function defense(): array + { + $defense = []; + foreach ($this->village->troops as $stack) { + if ($stack->count > 0) { + $defense[$stack->unit] = ($defense[$stack->unit] ?? 0) + $stack->count; + } + } + if ($this->village->militia > 0) { + $defense['militia'] = (int) $this->village->militia; + } + + return $defense; + } + /** When beginner protection ends for this player. */ public function protectedUntil(Player $player): CarbonImmutable { @@ -274,13 +295,7 @@ final class Rally } $combat = new CombatResolver($config); $stacks = $this->village->troops->filter(fn (UnitStack $s) => $s->count > 0); - $defense = []; - foreach ($stacks as $stack) { - $defense[$stack->unit] = ($defense[$stack->unit] ?? 0) + $stack->count; - } - if ($this->village->militia > 0) { - $defense['militia'] = (int) $this->village->militia; - } + $defense = $this->defense(); $wall = $this->village->buildings->firstWhere('type', 'wall'); $defenderPoints = $this->village->player_id ? (int) Village::where('player_id', $this->village->player_id)->sum('points') : null; mt_srand($movement->rng_seed); diff --git a/app/Game/Combat/Raids.php b/app/Game/Combat/Raids.php index 4b89cf2..e023986 100644 --- a/app/Game/Combat/Raids.php +++ b/app/Game/Combat/Raids.php @@ -8,8 +8,8 @@ use App\Game\WorldConfig; /** * Raiders from barbarian camps (config/game/raids.php, architecture.md "Raids"): how - * big a band is, how often it comes, how its camp's strength shrinks it on arrival, - * and what a beaten band leaves behind. + * big a band is, how often it comes, whether it dares to come, how its camp's strength + * shrinks it on arrival, and what a beaten band leaves behind. */ final class Raids { @@ -109,6 +109,17 @@ final class Raids return (int) round($this->config->get('raids.gather_hours', 0) * 3600); } + /** + * Whether a camp sends its band after fighting the battle on paper: it does unless + * the band would lose with less than `dare` × the defence's strength, or be shot + * down to nobody before the melee. + */ + public function dares(Battle $battle): bool + { + return $battle->attackerWon + || ($battle->attackPower > 0 && $battle->attackPower >= (float) $this->config->get('raids.dare', 0) * $battle->defensePower); + } + public function mode(): string { return (string) $this->config->get('raids.mode', 'normal'); diff --git a/app/Game/ConfigLint.php b/app/Game/ConfigLint.php index dbd9ff1..e206b84 100644 --- a/app/Game/ConfigLint.php +++ b/app/Game/ConfigLint.php @@ -145,6 +145,9 @@ final class ConfigLint if ($config->get('raids.gather_hours', 0) < 0 || $config->get('raids.range', 0) <= 0) { $problems[] = '[raids.gather_hours] cannot be negative and [raids.range] must be above 0.'; } + if ($config->get('raids.dare', 0) < 0) { + $problems[] = '[raids.dare] cannot be negative.'; + } $scatter = $config->get('raids.scatter_below', 0); if ($scatter <= 0 || $scatter > 1) { $problems[] = '[raids.scatter_below] must be above 0 and at most 1, or beating the camp would not stop its raid.'; diff --git a/config/game/raids.php b/config/game/raids.php index 12f4f44..1f7d218 100644 --- a/config/game/raids.php +++ b/config/game/raids.php @@ -26,6 +26,11 @@ return [ // level, rounded. 'rams' => ['from_points' => 300, 'per_wall_level' => 0.5], + // Before it sends a band, the camp fights the battle on paper (no luck, full morale) + // against the troops in the village, its wall and the night at arrival. When the band + // would lose with less than dare × the defence's strength, it stays home this time. + 'dare' => 0.3, + // On arrival the band is as strong as its camp's garrison is (share of full strength); // below scatter_below it scatters, so hitting the camp first breaks the raid. 'scatter_below' => 0.25, diff --git a/tests/Feature/RaidTest.php b/tests/Feature/RaidTest.php index 4bf5b55..92fa0e5 100644 --- a/tests/Feature/RaidTest.php +++ b/tests/Feature/RaidTest.php @@ -93,6 +93,27 @@ it('times raids in real hours on a fast world; only the march runs at world spee ->and(app(ListWorlds::class)->settings($village->world_id)['settings']['raids'])->toMatchArray(['every' => [20 * 3600, 40 * 3600], 'warning' => 3 * 3600]); }); +it('stays home when the village is far too strong for the band, and tries again later', function () { + $village = startVillage(); + raidCamp($village); + // 100 swordsmen hold 5000 against the band's 90: less than 30% of the defence. + $village->troops()->create(['home_village_id' => $village->id, 'unit' => 'sword', 'count' => 100]); + $this->travel(3)->days(); + $this->artisan('game:events --once')->assertSuccessful(); + + $next = pendingRaid($village); + expect(Movement::count())->toBe(0) + ->and(Report::count())->toBe(0) + ->and($next->greaterThanOrEqualTo(now()->addHours(20)))->toBeTrue() + ->and($next->lessThanOrEqualTo(now()->addHours(40)))->toBeTrue(); + + // Once the swordsmen are gone, the next band comes. + $village->troops()->delete(); + $this->travelTo($next); + $this->artisan('game:events --once')->assertSuccessful(); + expect(Movement::where('target_id', $village->id)->count())->toBe(1); +}); + it('brings a ram for every two wall levels; they fire half before the battle and the rest after winning it', function () { $village = startVillage(); raidCamp($village); @@ -133,7 +154,8 @@ it('loots an undefended village and carries the loot back to its camp', function it('pays a bounty when the village beats the band', function () { $village = startVillage(); raidCamp($village); - $village->troops()->create(['home_village_id' => $village->id, 'unit' => 'sword', 'count' => 20]); + // 4 swordsmen hold 200 against the band's 90: strong enough to win, not to scare it off. + $village->troops()->create(['home_village_id' => $village->id, 'unit' => 'sword', 'count' => 4]); $movement = announced($village); $wood = shown($village)['resources']['wood']; arrive($movement); diff --git a/tests/Unit/Game/ConfigLintTest.php b/tests/Unit/Game/ConfigLintTest.php index c1c0289..44f9a76 100644 --- a/tests/Unit/Game/ConfigLintTest.php +++ b/tests/Unit/Game/ConfigLintTest.php @@ -85,10 +85,11 @@ it('reports garrisons that would starve their village or never regroup', functio ->toContain('[garrison.max_food_share] must stay below [economy.villager_share], or a garrison starves its village.'); }); 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']]); + $config = gameConfig(['raids' => ['every_hours' => [0, 5], 'scatter_below' => 0, 'mode' => 'teleport', 'dare' => -1]]); expect(ConfigLint::check($config)) ->toContain('[raids.every_hours] must be two hours above 0, the smaller first, so a village gets time between raids.') + ->toContain('[raids.dare] cannot be negative.') ->toContain('[raids.scatter_below] must be above 0 and at most 1, or beating the camp would not stop its raid.') ->toContain('[raids.mode] is not a travel mode.'); }); diff --git a/tests/Unit/Game/RaidsTest.php b/tests/Unit/Game/RaidsTest.php index 101c816..651f6c8 100644 --- a/tests/Unit/Game/RaidsTest.php +++ b/tests/Unit/Game/RaidsTest.php @@ -1,5 +1,6 @@ and($fast->gatherSeconds())->toBe(3 * 3600); }); +it('stays home when the band would lose with less than 30% of the defence\'s strength', function () { + $raids = new Raids(gameConfig()); + + expect($raids->dares(new Battle(true, 0.0, 1.0, 0, 0, 100.0, 1.0)))->toBeTrue() + ->and($raids->dares(new Battle(false, 1.0, 0.0, 0, 0, 30.0, 100.0)))->toBeTrue() + ->and($raids->dares(new Battle(false, 1.0, 0.0, 0, 0, 29.0, 100.0)))->toBeFalse() + // Shot to pieces before the melee. + ->and($raids->dares(new Battle(false, 1.0, 0.0, 0, 0, 0.0, 0.0)))->toBeFalse() + ->and((new Raids(gameConfig(['raids' => ['dare' => 0]])))->dares(new Battle(false, 1.0, 0.0, 0, 0, 1.0, 100.0)))->toBeTrue(); +}); + it('picks the nearest camp in range', function () { $raids = new Raids(gameConfig()); $camps = [['id' => 7, 'x' => 10, 'y' => 0], ['id' => 3, 'x' => 0, 'y' => 4], ['id' => 2, 'x' => 4, 'y' => 0], ['id' => 9, 'x' => 16, 'y' => 0]];