93 lines
4.4 KiB
PHP
93 lines
4.4 KiB
PHP
<?php
|
||
|
||
use App\Game\Combat\Battle;
|
||
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 () {
|
||
$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]);
|
||
});
|
||
|
||
it('kills every attacker when the defence holds', function () {
|
||
$battle = (new CombatResolver(gameConfig()))->fight(['spear' => 10], ['sword' => 20], 0, 1.0, 0.0);
|
||
|
||
expect($battle->attackerWon)->toBeFalse()
|
||
->and($battle->attackLoss)->toBe(1.0)
|
||
->and(Battle::dead(['sword' => 20], $battle->defenseLoss))->toBe(['sword' => 1]);
|
||
});
|
||
|
||
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, 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);
|
||
|
||
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();
|
||
});
|
||
|
||
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);
|
||
});
|