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) - 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' => 30], 0, 1.0, 0.0); // 100 attack against 1500 defence. expect($battle->attackerWon)->toBeFalse() ->and($battle->attackLoss)->toBe(1.0) ->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 () { $battle = (new CombatResolver(gameConfig()))->fight(['spear' => 10, 'light_cavalry' => 1], ['spear' => 10], 0, 1.0, 0.0); // 100 infantry and 130 cavalry: spears defend 15 against the one and 45 against the other. expect($battle->defensePower)->toEqualWithDelta(150 * 100 / 230 + 450 * 130 / 230, 1e-9) ->and($battle->attackerWon)->toBeFalse(); }); it('wins every fight against an empty village without losses', function () { $battle = (new CombatResolver(gameConfig()))->fight(['spear' => 1], [], 0, 1.0, 0.0); expect($battle->attackerWon)->toBeTrue() ->and($battle->attackLoss)->toBe(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($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 () { $combat = new CombatResolver(gameConfig()); expect($combat->siege('ram', 10, 20))->toBe(5) ->and($combat->siege('ram', 10, 0))->toBe(10) ->and($combat->siege('catapult', 3, 1000))->toBe(1) ->and($combat->siege('ram', 0, 50))->toBe(0); }); it('scales attacks on smaller players by morale and draws luck evenly', function () { $combat = new CombatResolver(gameConfig()); $off = new CombatResolver(gameConfig(['combat' => ['morale' => ['enabled' => false], 'luck' => 0]])); expect($combat->morale(1000, 10000))->toEqualWithDelta(0.6, 1e-9) ->and($combat->morale(5000, 10000))->toBe(1.0) ->and($combat->morale(null, 10000))->toBe(1.0) ->and($off->morale(1000, 10000))->toBe(1.0) ->and($combat->luck(0.0))->toBe(-0.25) ->and($combat->luck(0.5))->toBe(0.0) ->and($off->luck(0.9))->toBe(0.0); }); it('doubles the defence at night where the world has a night bonus', function () { $combat = new CombatResolver(gameConfig(['combat' => ['night' => ['enabled' => true]]])); // 03:00 and 13:00 in Berlin. expect($combat->night(CarbonImmutable::parse('2026-10-29 02:00:00', 'UTC')))->toBe(2.0) ->and($combat->night(CarbonImmutable::parse('2026-10-29 12:00:00', 'UTC')))->toBe(1.0) ->and((new CombatResolver(gameConfig()))->night(CarbonImmutable::parse('2026-10-29 02:00:00', 'UTC')))->toBe(1.0); }); it('loots evenly and lets the other resources make up for a short one', function () { expect(LootCalculator::take(['wood' => 1000.0, 'clay' => 100.0, 'iron' => 500.0], 0, 900))->toBe(['wood' => 400, 'clay' => 100, 'iron' => 400]) ->and(LootCalculator::take(['wood' => 1000.0, 'clay' => 1000.0, 'iron' => 1000.0], 0, 10))->toBe(['wood' => 4, 'clay' => 3, 'iron' => 3]) ->and(LootCalculator::take(['wood' => 300.0, 'clay' => 120.0, 'iron' => 151.9], 150, 1000))->toBe(['wood' => 150, 'clay' => 0, 'iron' => 1]); }); it('carries what the survivors can carry', function () { expect((new LootCalculator(gameConfig()))->capacity(['spear' => 10, 'light_cavalry' => 2, 'ram' => 3]))->toBe(250 + 160); }); it('lets scouts through when they outnumber the defending ones', function () { $combat = new CombatResolver(gameConfig()); expect($combat->scout(10, 0))->toBe([true, 0]) ->and($combat->scout(10, 4))->toBe([true, (int) round(10 * 0.4 ** 1.5)]) ->and($combat->scout(5, 5))->toBe([false, 5]) ->and($combat->scout(0, 3))->toBe([false, 0]); }); it('lets archers shoot the attackers down before they reach the defence', function () { $battle = (new CombatResolver(gameConfig()))->fight(['spear' => 50], ['spear' => 2000, 'archer' => 2000], 0, 1.0, 0.0); // 2000 archers shoot 200 food of enemies, the 50 spears eat 50: all of them fall // before the melee, so there is none and the defence loses nobody. expect($battle->attackerWon)->toBeFalse() ->and($battle->attackVolley)->toBe(['spear' => 1.0]) ->and($battle->defenseVolley)->toBe([]) ->and($battle->attackDead(['spear' => 50]))->toBe(['spear' => 50]) ->and($battle->defenseDead(['spear' => 2000, 'archer' => 2000]))->toBe(['spear' => 0, 'archer' => 0]); }); it('beats a raid band without a loss when the defence has archers and catapults', function () { // A live raid: the defenders lost 8 spears and 8 archers every time before the volley. $band = ['spear' => 136, 'sword' => 58, 'ram' => 2]; $defense = ['spear' => 502, 'archer' => 470, 'catapult' => 23, 'sword' => 28, 'light_cavalry' => 2, 'nobleman' => 1]; $battle = (new CombatResolver(gameConfig()))->fight($band, $defense, 2, 1.0, 0.25); // (470 × 0.1 + 23 × 0.5) × 1.037² = 62.9 against 136 + 58 + 2 × 4 = 202 food: 31% fall. expect($battle->attackerWon)->toBeFalse() ->and($battle->attackVolleyDead($band))->toBe(['spear' => 42, 'sword' => 18, 'ram' => 1]) ->and($battle->attackDead($band))->toBe($band) ->and(array_sum($battle->defenseDead($defense)))->toBe(0); }); it('shoots from the wall and through it', function () { $combat = new CombatResolver(gameConfig()); $open = $combat->fight(['archer' => 100], ['spear' => 100, 'archer' => 100], 0, 1.0, 0.0); $walled = $combat->fight(['archer' => 100], ['spear' => 100, 'archer' => 100], 10, 1.0, 0.0); // Each side shoots 10 food of 200: 5% of every unit. Behind wall 10 the defenders // shoot 1.037^10 more, the attackers 1.037^10 less. expect($open->attackVolley)->toEqualWithDelta(['archer' => 0.1], 1e-9) ->and($open->defenseVolley)->toEqualWithDelta(['spear' => 0.05, 'archer' => 0.05], 1e-9) ->and($walled->attackVolley['archer'])->toEqualWithDelta(0.1 * 1.037 ** 10, 1e-9) ->and($walled->defenseVolley['spear'])->toEqualWithDelta(0.05 / 1.037 ** 10, 1e-9) ->and($walled->defenseVolleyDead(['spear' => 100, 'archer' => 100]))->toBe(['spear' => 3, 'archer' => 3]); }); it('weighs a volley by what its targets eat; militia soak a villager each and scouts stay out of it', function () { $combat = new CombatResolver(gameConfig()); // 10 catapults shoot 5 food: 10 spears and 10 militia soak 20, a heavy rider 4. expect($combat->volley(['catapult' => 10], ['spear' => 10, 'militia' => 10, 'scout' => 50]))->toBe(['spear' => 0.25, 'militia' => 0.25]) ->and($combat->volley(['catapult' => 10], ['heavy_cavalry' => 1]))->toBe(['heavy_cavalry' => 1.0]) ->and($combat->volley(['spear' => 100, 'scout' => 100, 'militia' => 5], ['spear' => 10]))->toBe([]) ->and($combat->volley(['archer' => 10], ['scout' => 10]))->toBe([]); }); it('wins outright when the volley leaves no defender standing', function () { $battle = (new CombatResolver(gameConfig()))->fight(['archer' => 100], ['spear' => 5, 'scout' => 3], 0, 1.0, 0.0); // 10 food shot at 5 spears: none left, the scouts fall with the village. expect($battle->attackerWon)->toBeTrue() ->and($battle->attackLoss)->toBe(0.0) ->and($battle->defenseDead(['spear' => 5, 'scout' => 3]))->toBe(['spear' => 5, 'scout' => 3]) ->and($battle->defenseVolleyDead(['spear' => 5, 'scout' => 3]))->toBe(['spear' => 5, 'scout' => 0]); }); it('kills the volley share first and the melee share of the rest, per group', function () { $battle = new Battle(false, 1.0, 0.5, 0, 0, 1.0, 2.0, [], ['spear' => 0.25]); // 10 spears: 3 shot (2.5 rounded), half of the 7 left in the melee (3.5 → 4). expect($battle->defenseVolleyDead(['spear' => 10, 'sword' => 10]))->toBe(['spear' => 3, 'sword' => 0]) ->and($battle->defenseDead(['spear' => 10, 'sword' => 10]))->toBe(['spear' => 7, 'sword' => 5]) ->and($battle->volleyed())->toBeTrue() ->and((new Battle(true, 0.0, 1.0, 0, 0, 2.0, 1.0))->volleyed())->toBeFalse(); });