From d55850cec1b3219cd86f0d7f4223a55eb5a6fe2d Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Sat, 31 Oct 2026 10:56:05 +0100 Subject: [PATCH] feat: loss threshold: a winner ten times stronger loses nobody, the loss curve rises from there to everything at even strength Co-Authored-By: Claude Opus 5.5 (1M context) --- app/Game/Combat/CombatResolver.php | 25 ++++++++++++++++++++----- app/Game/ConfigLint.php | 13 +++++++++++++ config/game/combat.php | 5 ++++- tests/Feature/HospitalTest.php | 20 ++++++++++---------- tests/Feature/RallyTest.php | 6 +++--- tests/Feature/SimulatorTest.php | 4 ++-- tests/Unit/Game/CombatTest.php | 23 ++++++++++++++++++----- tests/Unit/Game/ConfigLintTest.php | 4 ++++ 8 files changed, 74 insertions(+), 26 deletions(-) diff --git a/app/Game/Combat/CombatResolver.php b/app/Game/Combat/CombatResolver.php index 160e4a3..5fa60a8 100644 --- a/app/Game/Combat/CombatResolver.php +++ b/app/Game/Combat/CombatResolver.php @@ -9,8 +9,9 @@ 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)^1.5 of every unit, the - * loser everything. Luck, morale and the night bonus come in as numbers. + * 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. */ final class CombatResolver { @@ -33,13 +34,27 @@ final class CombatResolver $attackPower = $total * $morale * (1 + $luck); $defensePower = $this->defense($defense, $byClass, $total, $wallAfter) * $night; - $exponent = (float) $this->config->get('combat.loss_exponent', 1.5); if ($attackPower > $defensePower) { - return new Battle(true, ($defensePower / $attackPower) ** $exponent, 1.0, $wall, $wallAfter, $attackPower, $defensePower); + return new Battle(true, $this->winnerLoss($defensePower / $attackPower), 1.0, $wall, $wallAfter, $attackPower, $defensePower); } - return new Battle(false, 1.0, ($attackPower / $defensePower) ** $exponent, $wall, $wallAfter, $attackPower, $defensePower); + return new Battle(false, 1.0, $this->winnerLoss($attackPower / $defensePower), $wall, $wallAfter, $attackPower, $defensePower); + } + + /** + * The share of every unit the winner loses at r = loser strength / winner strength: + * ((r − t) / (1 − t))^loss_exponent with t = loss_threshold, and nothing at r ≤ t. + * A side 1 / t times stronger loses nobody; at even strength both sides fall. + */ + public function winnerLoss(float $ratio): float + { + $threshold = (float) $this->config->get('combat.loss_threshold', 0); + if ($ratio <= $threshold) { + return 0.0; + } + + return min(1.0, (($ratio - $threshold) / (1 - $threshold)) ** (float) $this->config->get('combat.loss_exponent', 1.5)); } /** diff --git a/app/Game/ConfigLint.php b/app/Game/ConfigLint.php index 30eff36..e847959 100644 --- a/app/Game/ConfigLint.php +++ b/app/Game/ConfigLint.php @@ -28,6 +28,7 @@ final class ConfigLint ...self::questsReachable($config, $rules), ...self::garrisonsRegroup($config), ...self::raidsPause($config), + ...self::battlesSettle($config), ...self::rolesReachable($config), ...self::crownsHold($config), ...self::woundedHeal($config), @@ -155,6 +156,18 @@ final class ConfigLint return $problems; } + /** Battle shares stay shares: the winner's loss threshold below 1, so the loss curve has room to rise. */ + private static function battlesSettle(WorldConfig $config): array + { + $problems = []; + $threshold = $config->get('combat.loss_threshold', 0); + if ($threshold < 0 || $threshold >= 1) { + $problems[] = '[combat.loss_threshold] must be at least 0 and below 1.'; + } + + return $problems; + } + /** * Every role names real buildings, each building counts for one role at most, and a * role's min_space fits a village of the largest headquarters, or nobody reaches it. diff --git a/config/game/combat.php b/config/game/combat.php index 50da9d8..639fcfc 100644 --- a/config/game/combat.php +++ b/config/game/combat.php @@ -10,8 +10,11 @@ return [ // min(1, floor + points_factor × defender points / attacker points). Barbarians always take 100%. 'morale' => ['enabled' => true, 'floor' => 0.3, 'points_factor' => 3], - // The winner loses (loser strength / winner strength) ^ exponent of every unit; the loser loses all. + // With r = loser strength / winner strength, the winner loses + // max(0, (r − loss_threshold) / (1 − loss_threshold)) ^ loss_exponent of every unit; the + // loser loses all. A side ten times stronger (r ≤ 0.1) loses nobody. 'loss_exponent' => 1.5, + 'loss_threshold' => 0.1, // Wall at level L: defenders get basic_defense × L extra, then everything × factor^L. 'wall' => ['basic_defense' => 20, 'factor' => 1.037], diff --git a/tests/Feature/HospitalTest.php b/tests/Feature/HospitalTest.php index aa8a581..2b54e64 100644 --- a/tests/Feature/HospitalTest.php +++ b/tests/Feature/HospitalTest.php @@ -48,25 +48,25 @@ it('brings half the fallen defenders back wounded; they eat meanwhile and stand $at = strike($attacker, $defender, 60); - // 600 attack against 1000 defence: the defenders lose 0.6^1.5 of 20 → 9, 4 of them wounded. + // 600 attack against 1000 defence: the defenders lose ((0.6 − 0.1) / 0.9)^1.5 of 20 → 8, 4 of them wounded. $defense = Report::where('player_id', $defender->player_id)->sole()->payload; - expect($defense['defender']['lost'])->toBe(['sword' => 9]) + expect($defense['defender']['lost'])->toBe(['sword' => 8]) ->and($defense['wounded'])->toBe(['units' => ['sword' => 4], 'heals_at' => $at->addHours(4)->utc()->format('Y-m-d\TH:i:s.v\Z')]) ->and(Report::where('player_id', $attacker->player_id)->sole()->payload['wounded'])->toBeNull(); $state = shown($defender); - expect((array) $state['troops'])->toBe(['sword' => 11]) + expect((array) $state['troops'])->toBe(['sword' => 12]) ->and($state['hospital'])->toMatchArray(['beds' => 20, 'occupied' => 4, 'share' => 0.5, 'heal_seconds' => 14400]) ->and((array) $state['hospital']['wounded'][0]['units'])->toBe(['sword' => 4]) - // Farm 10 feeds 0.8 × 223.5 = 178.8 heads: the 11 swordsmen and 4 wounded leave 163 villagers. - ->and($state['population']['villagers'])->toBe(163); + // Farm 10 feeds 0.8 × 223.5 = 178.8 heads: the 12 swordsmen and 4 wounded leave 162 villagers. + ->and($state['population']['villagers'])->toBe(162); $this->travel(4)->hours(); $this->artisan('game:events --once')->assertSuccessful(); $state = shown($defender); - expect((array) $state['troops'])->toBe(['sword' => 15]) + expect((array) $state['troops'])->toBe(['sword' => 16]) ->and($state['hospital'])->toMatchArray(['occupied' => 0, 'wounded' => []]) - ->and($state['population']['villagers'])->toBe(163); + ->and($state['population']['villagers'])->toBe(162); }); it('takes in no more wounded than it has free beds', function () { @@ -84,7 +84,7 @@ it('has no wounded without a hospital', function () { strike($attacker, $defender, 60); expect(Report::where('player_id', $defender->player_id)->sole()->payload['wounded'])->toBeNull() - ->and((array) shown($defender)['troops'])->toBe(['sword' => 11]) + ->and((array) shown($defender)['troops'])->toBe(['sword' => 12]) ->and(shown($defender)['hospital'])->toMatchArray(['beds' => 0, 'occupied' => 0, 'wounded' => []]); }); @@ -93,11 +93,11 @@ it('takes in only the own fallen, not the support', function () { $ally = startVillage(); $defender->troops()->create(['home_village_id' => $ally->id, 'unit' => 'sword', 'count' => 20]); - // 1500 attack against 2000 defence: 0.75^1.5 of each stack → 13 of 20 fall. + // 1500 attack against 2000 defence: ((0.75 − 0.1) / 0.9)^1.5 of each stack → 12 of 20 fall. strike($attacker, $defender->fresh(), 150); $defense = Report::where('player_id', $defender->player_id)->sole()->payload; - expect($defense['defender']['lost'])->toBe(['sword' => 26]) + expect($defense['defender']['lost'])->toBe(['sword' => 24]) ->and($defense['wounded']['units'])->toBe(['sword' => 6]); }); diff --git a/tests/Feature/RallyTest.php b/tests/Feature/RallyTest.php index 5159aa1..6e42e67 100644 --- a/tests/Feature/RallyTest.php +++ b/tests/Feature/RallyTest.php @@ -76,7 +76,7 @@ it('raids an empty barbarian village and carries the loot home', function () { ->and($state['resources']['wood'])->toEqualWithDelta($before + 84, 0.1); }); -it('loses an attack against a stronger defence, and both sides get a report', function () { +it('loses an attack against a ten times stronger defence, which loses nobody, and both sides get a report', function () { $attacker = rallied(startVillage(), ['spear' => 10]); $defender = startVillage(); $defender->troops()->create(['home_village_id' => $defender->id, 'unit' => 'sword', 'count' => 20]); @@ -88,14 +88,14 @@ it('loses an attack against a stronger defence, and both sides get a report', fu expect((array) shown($attacker)['troops'])->toBe([]) ->and(shown($attacker)['movements'])->toBe([]) - ->and((array) shown($defender)['troops'])->toBe(['sword' => 19]); + ->and((array) shown($defender)['troops'])->toBe(['sword' => 20]); $attack = Report::where('player_id', $attacker->player_id)->sole(); $defense = Report::where('player_id', $defender->player_id)->sole(); expect($attack->type)->toBe('attack') ->and($attack->payload['won'])->toBeFalse() ->and($attack->payload['defender']['units'])->toBeNull() ->and($defense->type)->toBe('defense') - ->and($defense->payload['defender'])->toMatchArray(['units' => ['sword' => 20], 'lost' => ['sword' => 1]]) + ->and($defense->payload['defender'])->toMatchArray(['units' => ['sword' => 20], 'lost' => ['sword' => 0]]) ->and($defense->payload['attacker']['lost'])->toBe(['spear' => 10]); }); diff --git a/tests/Feature/SimulatorTest.php b/tests/Feature/SimulatorTest.php index adceb7d..761e332 100644 --- a/tests/Feature/SimulatorTest.php +++ b/tests/Feature/SimulatorTest.php @@ -13,8 +13,8 @@ it('simulates a battle with the world\'s rules and changes nothing', function () expect($result['won'])->toBeTrue() ->and((array) $result['attacker']['units'])->toBe(['light_cavalry' => 100, 'catapult' => 20]) ->and((array) $result['defender']['lost'])->toBe(['spear' => 50, 'militia' => 10]) - // One of 20 catapults falls; 19 take the building down one level, 20 would take two. - ->and($result['building'])->toBe(['before' => 20, 'after' => 19]) + // 2100 defence against 15000 attack: under 1% falls, no catapult, so 20 take the building down two levels. + ->and($result['building'])->toBe(['before' => 20, 'after' => 18]) ->and($result['wall'])->toBeNull(); }); diff --git a/tests/Unit/Game/CombatTest.php b/tests/Unit/Game/CombatTest.php index d4a1fef..b014602 100644 --- a/tests/Unit/Game/CombatTest.php +++ b/tests/Unit/Game/CombatTest.php @@ -5,22 +5,35 @@ use App\Game\Combat\CombatResolver; use App\Game\Combat\LootCalculator; use Carbon\CarbonImmutable; -it('lets the stronger attacker win and lose (weaker / stronger)^1.5', function () { +it('lets the stronger attacker win and lose ((weaker / stronger − 0.1) / 0.9)^1.5', function () { $battle = (new CombatResolver(gameConfig()))->fight(['light_cavalry' => 100], ['spear' => 50], 0, 1.0, 0.0); // 13000 cavalry attack against 50 × 45 cavalry defence. expect($battle->attackerWon)->toBeTrue() ->and($battle->defenseLoss)->toBe(1.0) - ->and($battle->attackLoss)->toEqualWithDelta((2250 / 13000) ** 1.5, 1e-9) - ->and(Battle::dead(['light_cavalry' => 100], $battle->attackLoss))->toBe(['light_cavalry' => 7]); + ->and($battle->attackLoss)->toEqualWithDelta((((2250 / 13000) - 0.1) / 0.9) ** 1.5, 1e-9) + ->and(Battle::dead(['light_cavalry' => 100], $battle->attackLoss))->toBe(['light_cavalry' => 2]); }); it('kills every attacker when the defence holds', function () { - $battle = (new CombatResolver(gameConfig()))->fight(['spear' => 10], ['sword' => 20], 0, 1.0, 0.0); + $battle = (new CombatResolver(gameConfig()))->fight(['spear' => 10], ['sword' => 30], 0, 1.0, 0.0); + // 100 attack against 1500 defence. expect($battle->attackerWon)->toBeFalse() ->and($battle->attackLoss)->toBe(1.0) - ->and(Battle::dead(['sword' => 20], $battle->defenseLoss))->toBe(['sword' => 1]); + ->and($battle->defenseLoss)->toBe(0.0); +}); + +it('spares a winner ten times stronger and still costs both sides dearly at even strength', function () { + $combat = new CombatResolver(gameConfig()); + + expect($combat->winnerLoss(0.1))->toBe(0.0) + ->and($combat->winnerLoss(0.05))->toBe(0.0) + ->and($combat->winnerLoss(0.55))->toEqualWithDelta(0.5 ** 1.5, 1e-9) + ->and($combat->winnerLoss(1.0))->toEqualWithDelta(1.0, 1e-9) + // Continuous at the threshold: just above it the loss is tiny. + ->and($combat->winnerLoss(0.1001))->toBeLessThan(1e-5) + ->and((new CombatResolver(gameConfig(['combat' => ['loss_threshold' => 0]])))->winnerLoss(0.25))->toEqualWithDelta(0.125, 1e-9); }); it('weights the defence by the attacker\'s mix of classes', function () { diff --git a/tests/Unit/Game/ConfigLintTest.php b/tests/Unit/Game/ConfigLintTest.php index e82d868..158d7a7 100644 --- a/tests/Unit/Game/ConfigLintTest.php +++ b/tests/Unit/Game/ConfigLintTest.php @@ -92,6 +92,10 @@ it('reports raids that never pause or that a beaten camp still sends', function ->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.'); }); +it('reports battle shares out of range', function () { + expect(ConfigLint::check(gameConfig(['combat' => ['loss_threshold' => 1.0]]))) + ->toBe(['[combat.loss_threshold] must be at least 0 and below 1.']); +}); it('reports roles that share buildings or can never be reached', function () { $config = gameConfig(['world' => ['village' => ['specialisation' => [ 'min_space' => 999,