feat: rams in two phases: half fire before the melee, which is fought at the lower wall, the survivors fire the rest after a won battle

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-31 11:01:11 +01:00
parent c344eb3a39
commit 8fa72392c7
5 changed files with 98 additions and 27 deletions

View File

@ -6,12 +6,14 @@ use App\Game\WorldConfig;
use Carbon\CarbonImmutable; use Carbon\CarbonImmutable;
/** /**
* The Die Stämme battle (architecture.md, "Combat"). Attack strength is split by class * The Die Stämme battle (architecture.md, "Combat"). First both sides' archers and
* (infantry, cavalry, archer); every defender counts with its defence against each class, * catapults shoot a volley. Then the melee: attack strength is split by class (infantry,
* weighted by the attacker's mix. The wall adds basic defence and multiplies it, and rams * cavalry, archer); every defender counts with its defence against each class, weighted
* lower the wall first. The winner loses ((weaker / stronger − t) / (1 − t))^1.5 of every * by the attacker's mix. The wall adds basic defence and multiplies it; rams lower it in
* unit, none at all when it is 1 / t times stronger; the loser loses everything. Luck, * part before the melee and with the rest after a won one. The winner loses
* morale and the night bonus come in as numbers. * ((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 final class CombatResolver
{ {
@ -33,22 +35,33 @@ final class CombatResolver
$attack = self::left($attack, $attackVolley); $attack = self::left($attack, $attackVolley);
$defense = self::left($defense, $defenseVolley); $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. // 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); $byClass = $this->attackByClass($attack);
$total = array_sum($byClass); $total = array_sum($byClass);
if ($total <= 0) { 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); $attackPower = $total * $morale * (1 + $luck);
$defensePower = $this->defense($defense, $byClass, $total, $wallAfter) * $night; $defensePower = $this->defense($defense, $byClass, $total, $wallFought) * $night;
if ($attackPower > $defensePower) { 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. * L down needs base × factor^(L − 1) of them; nothing drops below level 1.
*/ */
public function siege(string $kind, int $level, int $count): int 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"); $siege = $this->config->get("combat.siege.$kind");
$left = (float) $count; $left = $count;
while ($level > 1) { while ($level > 1) {
$needed = $siege['base'] * $siege['factor'] ** ($level - 1); $needed = $siege['base'] * $siege['factor'] ** ($level - 1);
if ($left < $needed) { if ($left < $needed) {
@ -137,7 +161,7 @@ final class CombatResolver
$level--; $level--;
} }
return $level; return [$level, $left];
} }
/** Attack multiplier from points: 1 against barbarians (null), less against smaller players. */ /** Attack multiplier from points: 1 against barbarians (null), less against smaller players. */

View File

@ -158,7 +158,8 @@ final class ConfigLint
/** /**
* Battle shares stay shares: the winner's loss threshold below 1, so the loss curve * 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 private static function battlesSettle(WorldConfig $config): array
{ {
@ -167,6 +168,10 @@ final class ConfigLint
if ($threshold < 0 || $threshold >= 1) { if ($threshold < 0 || $threshold >= 1) {
$problems[] = '[combat.loss_threshold] must be at least 0 and below 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) { foreach ((new UnitRules($config))->units() as $unit) {
if ($config->get("units.$unit.volley", 0) < 0) { if ($config->get("units.$unit.volley", 0) < 0) {
$problems[] = "[units.$unit.volley] cannot be negative."; $problems[] = "[units.$unit.volley] cannot be negative.";

View File

@ -22,11 +22,14 @@ return [
// food upkeep) of every unit but scouts; the melee runs with who is left. // food upkeep) of every unit but scouts; the melee runs with who is left.
'wall' => ['basic_defense' => 20, 'factor' => 1.037], 'wall' => ['basic_defense' => 20, 'factor' => 1.037],
// Rams hit the wall before the battle, surviving catapults the chosen building after // Rams hit the wall, surviving catapults the chosen building after a won battle.
// a won one. Taking a building from level L to L − 1 needs base × factor^(L − 1) // Taking a building from level L to L − 1 needs base × factor^(L − 1) of them;
// of them; buildings never drop below level 1. // 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' => [ 'siege' => [
'ram' => ['base' => 2, 'factor' => 1.09], 'ram' => ['base' => 2, 'factor' => 1.09, 'before_battle' => 0.5],
'catapult' => ['base' => 2, 'factor' => 1.09], 'catapult' => ['base' => 2, 'factor' => 1.09],
], ],

View File

@ -51,16 +51,50 @@ it('wins every fight against an empty village without losses', function () {
->and($battle->attackLoss)->toBe(0.0); ->and($battle->attackLoss)->toBe(0.0);
}); });
it('lets the wall defend on its own, and rams take it down first', function () { it('lets the wall defend on its own', function () {
$combat = new CombatResolver(gameConfig()); $held = (new CombatResolver(gameConfig()))->fight(['sword' => 10], [], 10, 1.0, 0.0);
$held = $combat->fight(['sword' => 10], [], 10, 1.0, 0.0);
$rammed = $combat->fight(['sword' => 10, 'ram' => 20], [], 10, 1.0, 0.0);
expect($held->defensePower)->toEqualWithDelta(200 * 1.037 ** 10, 1e-9) expect($held->defensePower)->toEqualWithDelta(200 * 1.037 ** 10, 1e-9)
->and($held->attackerWon)->toBeFalse() ->and($held->attackerWon)->toBeFalse()
->and($rammed->wallBefore)->toBe(10) ->and($held->wallAfter)->toBe(10);
->and($rammed->wallAfter)->toBe(5) });
->and($rammed->attackerWon)->toBeTrue();
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 () { it('needs more siege units for every higher level and never goes below level 1', function () {

View File

@ -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.'); ->toContain('[raids.mode] is not a travel mode.');
}); });
it('reports battle shares out of range', function () { it('reports battle shares out of range', function () {
expect(ConfigLint::check(gameConfig(['combat' => ['loss_threshold' => 1.0], 'units' => ['archer' => ['volley' => -0.1]]]))) $config = gameConfig(['combat' => ['loss_threshold' => 1.0, 'siege' => ['ram' => ['before_battle' => 1.5]]], 'units' => ['archer' => ['volley' => -0.1]]]);
->toBe(['[combat.loss_threshold] must be at least 0 and below 1.', '[units.archer.volley] cannot be negative.']);
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 () { it('reports roles that share buildings or can never be reached', function () {
$config = gameConfig(['world' => ['village' => ['specialisation' => [ $config = gameConfig(['world' => ['village' => ['specialisation' => [