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) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-31 11:05:05 +01:00
parent 1780ee510a
commit 007e80b26a
8 changed files with 105 additions and 12 deletions

View File

@ -2,6 +2,7 @@
namespace App\Engine; namespace App\Engine;
use App\Game\Combat\CombatResolver;
use App\Game\Combat\Raids; use App\Game\Combat\Raids;
use App\Game\Travel\TravelTime; use App\Game\Travel\TravelTime;
use App\Models\GameEvent; 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 * 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, * 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. * with the village locked; the camp is only read.
*/ */
final class Raiders final class Raiders
@ -75,6 +77,12 @@ final class Raiders
$travel = new TravelTime($this->sim->config()); $travel = new TravelTime($this->sim->config());
$marches = $at->addSeconds($this->raids->gatherSeconds()); $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())); $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([ $movement = Movement::create([
'world_id' => $this->village->world_id, 'world_id' => $this->village->world_id,
'player_id' => null, 'player_id' => null,
@ -116,6 +124,22 @@ final class Raiders
return $this->raids->muster($movement->units, Barbarians::of($camp)->strength()); 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<string, int> $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. */ /** The nearest barbarian village in range; crown forts never raid. */
private function camp(): ?Village private function camp(): ?Village
{ {

View File

@ -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)); 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<string, int>
*/
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. */ /** When beginner protection ends for this player. */
public function protectedUntil(Player $player): CarbonImmutable public function protectedUntil(Player $player): CarbonImmutable
{ {
@ -274,13 +295,7 @@ final class Rally
} }
$combat = new CombatResolver($config); $combat = new CombatResolver($config);
$stacks = $this->village->troops->filter(fn (UnitStack $s) => $s->count > 0); $stacks = $this->village->troops->filter(fn (UnitStack $s) => $s->count > 0);
$defense = []; $defense = $this->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;
}
$wall = $this->village->buildings->firstWhere('type', 'wall'); $wall = $this->village->buildings->firstWhere('type', 'wall');
$defenderPoints = $this->village->player_id ? (int) Village::where('player_id', $this->village->player_id)->sum('points') : null; $defenderPoints = $this->village->player_id ? (int) Village::where('player_id', $this->village->player_id)->sum('points') : null;
mt_srand($movement->rng_seed); mt_srand($movement->rng_seed);

View File

@ -8,8 +8,8 @@ use App\Game\WorldConfig;
/** /**
* Raiders from barbarian camps (config/game/raids.php, architecture.md "Raids"): how * 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, * big a band is, how often it comes, whether it dares to come, how its camp's strength
* and what a beaten band leaves behind. * shrinks it on arrival, and what a beaten band leaves behind.
*/ */
final class Raids final class Raids
{ {
@ -109,6 +109,17 @@ final class Raids
return (int) round($this->config->get('raids.gather_hours', 0) * 3600); 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 public function mode(): string
{ {
return (string) $this->config->get('raids.mode', 'normal'); return (string) $this->config->get('raids.mode', 'normal');

View File

@ -145,6 +145,9 @@ final class ConfigLint
if ($config->get('raids.gather_hours', 0) < 0 || $config->get('raids.range', 0) <= 0) { 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.'; $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); $scatter = $config->get('raids.scatter_below', 0);
if ($scatter <= 0 || $scatter > 1) { 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.'; $problems[] = '[raids.scatter_below] must be above 0 and at most 1, or beating the camp would not stop its raid.';

View File

@ -26,6 +26,11 @@ return [
// level, rounded. // level, rounded.
'rams' => ['from_points' => 300, 'per_wall_level' => 0.5], '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); // 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. // below scatter_below it scatters, so hitting the camp first breaks the raid.
'scatter_below' => 0.25, 'scatter_below' => 0.25,

View File

@ -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]); ->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 () { it('brings a ram for every two wall levels; they fire half before the battle and the rest after winning it', function () {
$village = startVillage(); $village = startVillage();
raidCamp($village); 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 () { it('pays a bounty when the village beats the band', function () {
$village = startVillage(); $village = startVillage();
raidCamp($village); 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); $movement = announced($village);
$wood = shown($village)['resources']['wood']; $wood = shown($village)['resources']['wood'];
arrive($movement); arrive($movement);

View File

@ -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.'); ->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 () { 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)) 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.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.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.'); ->toContain('[raids.mode] is not a travel mode.');
}); });

View File

@ -1,5 +1,6 @@
<?php <?php
use App\Game\Combat\Battle;
use App\Game\Combat\Raids; use App\Game\Combat\Raids;
it('sizes a band by the village points in its camp profile', function () { it('sizes a band by the village points in its camp profile', function () {
@ -49,6 +50,17 @@ it('comes every 20 to 40 real hours on any world, after three real hours of warn
->and($fast->gatherSeconds())->toBe(3 * 3600); ->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 () { it('picks the nearest camp in range', function () {
$raids = new Raids(gameConfig()); $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]]; $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]];