diff --git a/app/Game/Combat/CombatResolver.php b/app/Game/Combat/CombatResolver.php index a64790d..63cb49e 100644 --- a/app/Game/Combat/CombatResolver.php +++ b/app/Game/Combat/CombatResolver.php @@ -6,12 +6,14 @@ use App\Game\WorldConfig; use Carbon\CarbonImmutable; /** - * The Die Stämme battle (architecture.md, "Combat"). Attack strength is split by class - * (infantry, cavalry, archer); every defender counts with its defence against each class, - * weighted by the attacker's mix. The wall adds basic defence and multiplies it, and rams - * lower the wall first. The winner loses ((weaker / stronger − t) / (1 − t))^1.5 of every - * unit, none at all when it is 1 / t times stronger; the loser loses everything. Luck, - * morale and the night bonus come in as numbers. + * The Die Stämme battle (architecture.md, "Combat"). First both sides' archers and + * catapults shoot a volley. Then the melee: attack strength is split by class (infantry, + * cavalry, archer); every defender counts with its defence against each class, weighted + * by the attacker's mix. The wall adds basic defence and multiplies it; rams lower it in + * part before the melee and with the rest after a won one. The winner loses + * ((weaker / stronger − t) / (1 − t))^1.5 of every unit, none at all when it is 1 / t + * times stronger; the loser loses everything. Luck, morale and the night bonus come in + * as numbers. */ final class CombatResolver { @@ -33,22 +35,33 @@ final class CombatResolver $attack = self::left($attack, $attackVolley); $defense = self::left($defense, $defenseVolley); + // The rams left fire their first share before the melee, which is fought at the + // lower wall. + $rams = $attack['ram'] ?? 0; + $first = (float) $this->config->get('combat.siege.ram.before_battle', 1.0); + [$wallFought, $work] = $this->batter('ram', $wall, $rams * $first); + // The melee runs with who is left. An attack shot to pieces has nobody to fight. - $wallAfter = $this->siege('ram', $wall, $attack['ram'] ?? 0); $byClass = $this->attackByClass($attack); $total = array_sum($byClass); if ($total <= 0) { - return new Battle(false, 1.0, 0.0, $wall, $wall, 0.0, 0.0, $attackVolley, $defenseVolley); + return new Battle(false, 1.0, 0.0, $wall, $wallFought, 0.0, 0.0, $attackVolley, $defenseVolley); } $attackPower = $total * $morale * (1 + $luck); - $defensePower = $this->defense($defense, $byClass, $total, $wallAfter) * $night; + $defensePower = $this->defense($defense, $byClass, $total, $wallFought) * $night; if ($attackPower > $defensePower) { - return new Battle(true, $this->winnerLoss($defensePower / $attackPower), 1.0, $wall, $wallAfter, $attackPower, $defensePower, $attackVolley, $defenseVolley); + // After a won battle the surviving rams fire the rest; the first ones' work on + // the level they stopped at counts. + $loss = $this->winnerLoss($defensePower / $attackPower); + $survivors = $rams - Battle::dead(['ram' => $rams], $loss)['ram']; + [$wallAfter] = $this->batter('ram', $wallFought, $work + $survivors * (1 - $first)); + + return new Battle(true, $loss, 1.0, $wall, $wallAfter, $attackPower, $defensePower, $attackVolley, $defenseVolley); } - return new Battle(false, 1.0, $this->winnerLoss($attackPower / $defensePower), $wall, $wallAfter, $attackPower, $defensePower, $attackVolley, $defenseVolley); + return new Battle(false, 1.0, $this->winnerLoss($attackPower / $defensePower), $wall, $wallFought, $attackPower, $defensePower, $attackVolley, $defenseVolley); } /** @@ -125,9 +138,20 @@ final class CombatResolver * L down needs base × factor^(L − 1) of them; nothing drops below level 1. */ public function siege(string $kind, int $level, int $count): int + { + return $this->batter($kind, $level, $count)[0]; + } + + /** + * The siege rule for a share of siege units: the level left, and the work left over + * towards the next level down. + * + * @return array{0: int, 1: float} + */ + private function batter(string $kind, int $level, float $count): array { $siege = $this->config->get("combat.siege.$kind"); - $left = (float) $count; + $left = $count; while ($level > 1) { $needed = $siege['base'] * $siege['factor'] ** ($level - 1); if ($left < $needed) { @@ -137,7 +161,7 @@ final class CombatResolver $level--; } - return $level; + return [$level, $left]; } /** Attack multiplier from points: 1 against barbarians (null), less against smaller players. */ diff --git a/app/Game/ConfigLint.php b/app/Game/ConfigLint.php index 4188e92..dbd9ff1 100644 --- a/app/Game/ConfigLint.php +++ b/app/Game/ConfigLint.php @@ -158,7 +158,8 @@ final class ConfigLint /** * Battle shares stay shares: the winner's loss threshold below 1, so the loss curve - * has room to rise, and no volley below 0, which would raise the dead. + * has room to rise, the rams' first share between 0 and 1, and no volley below 0, + * which would raise the dead. */ private static function battlesSettle(WorldConfig $config): array { @@ -167,6 +168,10 @@ final class ConfigLint if ($threshold < 0 || $threshold >= 1) { $problems[] = '[combat.loss_threshold] must be at least 0 and below 1.'; } + $first = $config->get('combat.siege.ram.before_battle', 1); + if ($first < 0 || $first > 1) { + $problems[] = '[combat.siege.ram.before_battle] must be between 0 and 1, or rams fire more than they are.'; + } foreach ((new UnitRules($config))->units() as $unit) { if ($config->get("units.$unit.volley", 0) < 0) { $problems[] = "[units.$unit.volley] cannot be negative."; diff --git a/config/game/combat.php b/config/game/combat.php index 0b09ec5..169ef3c 100644 --- a/config/game/combat.php +++ b/config/game/combat.php @@ -22,11 +22,14 @@ return [ // food upkeep) of every unit but scouts; the melee runs with who is left. 'wall' => ['basic_defense' => 20, 'factor' => 1.037], - // Rams hit the wall before the battle, surviving catapults the chosen building after - // a won one. Taking a building from level L to L − 1 needs base × factor^(L − 1) - // of them; buildings never drop below level 1. + // Rams hit the wall, surviving catapults the chosen building after a won battle. + // Taking a building from level L to L − 1 needs base × factor^(L − 1) of them; + // buildings never drop below level 1. The rams left after the volley fire + // before_battle of their count before the melee, which is fought at the lower wall; + // after a won battle the surviving rams fire the rest, and the first ones' work on + // the level they stopped at counts. A lost battle ends after the first share. 'siege' => [ - 'ram' => ['base' => 2, 'factor' => 1.09], + 'ram' => ['base' => 2, 'factor' => 1.09, 'before_battle' => 0.5], 'catapult' => ['base' => 2, 'factor' => 1.09], ], diff --git a/tests/Unit/Game/CombatTest.php b/tests/Unit/Game/CombatTest.php index 21175db..8534497 100644 --- a/tests/Unit/Game/CombatTest.php +++ b/tests/Unit/Game/CombatTest.php @@ -51,16 +51,50 @@ it('wins every fight against an empty village without losses', function () { ->and($battle->attackLoss)->toBe(0.0); }); -it('lets the wall defend on its own, and rams take it down first', function () { - $combat = new CombatResolver(gameConfig()); - $held = $combat->fight(['sword' => 10], [], 10, 1.0, 0.0); - $rammed = $combat->fight(['sword' => 10, 'ram' => 20], [], 10, 1.0, 0.0); +it('lets the wall defend on its own', function () { + $held = (new CombatResolver(gameConfig()))->fight(['sword' => 10], [], 10, 1.0, 0.0); expect($held->defensePower)->toEqualWithDelta(200 * 1.037 ** 10, 1e-9) ->and($held->attackerWon)->toBeFalse() - ->and($rammed->wallBefore)->toBe(10) - ->and($rammed->wallAfter)->toBe(5) - ->and($rammed->attackerWon)->toBeTrue(); + ->and($held->wallAfter)->toBe(10); +}); + +it('fires half the rams before the melee, at the lower wall, and the rest after a won one', function () { + $combat = new CombatResolver(gameConfig()); + $won = $combat->fight(['light_cavalry' => 100, 'ram' => 20], [], 10, 1.0, 0.0); + $lost = $combat->fight(['sword' => 10, 'ram' => 20], ['spear' => 100], 10, 1.0, 0.0); + $once = (new CombatResolver(gameConfig(['combat' => ['siege' => ['ram' => ['before_battle' => 1.0]]]]))) + ->fight(['light_cavalry' => 100, 'ram' => 20], [], 10, 1.0, 0.0); + + // 10 rams take wall 10 to 8 and the melee is fought there; no ram falls, so the + // other 10 take it on to 5, as all 20 at once would. + expect($won->defensePower)->toEqualWithDelta(160 * 1.037 ** 8, 1e-9) + ->and($won->wallBefore)->toBe(10) + ->and($won->wallAfter)->toBe(5) + // A lost battle ends after the first half. + ->and($lost->attackerWon)->toBeFalse() + ->and($lost->wallAfter)->toBe(8) + ->and($once->defensePower)->toEqualWithDelta(100 * 1.037 ** 5, 1e-9) + ->and($once->wallAfter)->toBe(5); +}); + +it('counts the first rams\' work on a level towards the rest', function () { + $combat = new CombatResolver(gameConfig()); + + // Wall 3 needs 2 × 1.09² = 2.38 rams: 2 before the melee don't do it, 2 more after it do. + expect($combat->fight(['light_cavalry' => 10, 'ram' => 4], [], 3, 1.0, 0.0)->wallAfter)->toBe(2) + ->and($combat->fight(['spear' => 1, 'ram' => 4], ['sword' => 100], 3, 1.0, 0.0)->wallAfter)->toBe(3); +}); + +it('fires only the rams the volley left', function () { + $attack = ['light_cavalry' => 50, 'ram' => 20]; + $shot = (new CombatResolver(gameConfig()))->fight($attack, ['archer' => 1000], 10, 1.0, 0.0); + $unshot = (new CombatResolver(gameConfig(['units' => ['archer' => ['volley' => 0]]])))->fight($attack, ['archer' => 1000], 10, 1.0, 0.0); + + // 100 × 1.037^10 = 144 food shot at 230: 13 rams fall, and 3.5 of the 7 left don't take a level. + expect($shot->attackVolleyDead($attack))->toBe(['light_cavalry' => 31, 'ram' => 13]) + ->and($shot->wallAfter)->toBe(10) + ->and($unshot->wallAfter)->toBe(8); }); it('needs more siege units for every higher level and never goes below level 1', function () { diff --git a/tests/Unit/Game/ConfigLintTest.php b/tests/Unit/Game/ConfigLintTest.php index 76816d3..c1c0289 100644 --- a/tests/Unit/Game/ConfigLintTest.php +++ b/tests/Unit/Game/ConfigLintTest.php @@ -93,8 +93,13 @@ it('reports raids that never pause or that a beaten camp still sends', function ->toContain('[raids.mode] is not a travel mode.'); }); it('reports battle shares out of range', function () { - expect(ConfigLint::check(gameConfig(['combat' => ['loss_threshold' => 1.0], 'units' => ['archer' => ['volley' => -0.1]]]))) - ->toBe(['[combat.loss_threshold] must be at least 0 and below 1.', '[units.archer.volley] cannot be negative.']); + $config = gameConfig(['combat' => ['loss_threshold' => 1.0, 'siege' => ['ram' => ['before_battle' => 1.5]]], 'units' => ['archer' => ['volley' => -0.1]]]); + + expect(ConfigLint::check($config))->toBe([ + '[combat.loss_threshold] must be at least 0 and below 1.', + '[combat.siege.ram.before_battle] must be between 0 and 1, or rams fire more than they are.', + '[units.archer.volley] cannot be negative.', + ]); }); it('reports roles that share buildings or can never be reached', function () { $config = gameConfig(['world' => ['village' => ['specialisation' => [