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) <noreply@anthropic.com>
This commit is contained in:
parent
2c20fd24ea
commit
d55850cec1
|
|
@ -9,8 +9,9 @@ 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"). Attack strength is split by class
|
||||||
* (infantry, cavalry, archer); every defender counts with its defence against each 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
|
* 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
|
* lower the wall first. The winner loses ((weaker / stronger − t) / (1 − t))^1.5 of every
|
||||||
* loser everything. Luck, morale and the night bonus come in as numbers.
|
* 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,13 +34,27 @@ final class CombatResolver
|
||||||
|
|
||||||
$attackPower = $total * $morale * (1 + $luck);
|
$attackPower = $total * $morale * (1 + $luck);
|
||||||
$defensePower = $this->defense($defense, $byClass, $total, $wallAfter) * $night;
|
$defensePower = $this->defense($defense, $byClass, $total, $wallAfter) * $night;
|
||||||
$exponent = (float) $this->config->get('combat.loss_exponent', 1.5);
|
|
||||||
|
|
||||||
if ($attackPower > $defensePower) {
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ final class ConfigLint
|
||||||
...self::questsReachable($config, $rules),
|
...self::questsReachable($config, $rules),
|
||||||
...self::garrisonsRegroup($config),
|
...self::garrisonsRegroup($config),
|
||||||
...self::raidsPause($config),
|
...self::raidsPause($config),
|
||||||
|
...self::battlesSettle($config),
|
||||||
...self::rolesReachable($config),
|
...self::rolesReachable($config),
|
||||||
...self::crownsHold($config),
|
...self::crownsHold($config),
|
||||||
...self::woundedHeal($config),
|
...self::woundedHeal($config),
|
||||||
|
|
@ -155,6 +156,18 @@ final class ConfigLint
|
||||||
return $problems;
|
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
|
* 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.
|
* role's min_space fits a village of the largest headquarters, or nobody reaches it.
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,11 @@ return [
|
||||||
// min(1, floor + points_factor × defender points / attacker points). Barbarians always take 100%.
|
// min(1, floor + points_factor × defender points / attacker points). Barbarians always take 100%.
|
||||||
'morale' => ['enabled' => true, 'floor' => 0.3, 'points_factor' => 3],
|
'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_exponent' => 1.5,
|
||||||
|
'loss_threshold' => 0.1,
|
||||||
|
|
||||||
// Wall at level L: defenders get basic_defense × L extra, then everything × factor^L.
|
// Wall at level L: defenders get basic_defense × L extra, then everything × factor^L.
|
||||||
'wall' => ['basic_defense' => 20, 'factor' => 1.037],
|
'wall' => ['basic_defense' => 20, 'factor' => 1.037],
|
||||||
|
|
|
||||||
|
|
@ -48,25 +48,25 @@ it('brings half the fallen defenders back wounded; they eat meanwhile and stand
|
||||||
|
|
||||||
$at = strike($attacker, $defender, 60);
|
$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;
|
$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($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();
|
->and(Report::where('player_id', $attacker->player_id)->sole()->payload['wounded'])->toBeNull();
|
||||||
|
|
||||||
$state = shown($defender);
|
$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($state['hospital'])->toMatchArray(['beds' => 20, 'occupied' => 4, 'share' => 0.5, 'heal_seconds' => 14400])
|
||||||
->and((array) $state['hospital']['wounded'][0]['units'])->toBe(['sword' => 4])
|
->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.
|
// Farm 10 feeds 0.8 × 223.5 = 178.8 heads: the 12 swordsmen and 4 wounded leave 162 villagers.
|
||||||
->and($state['population']['villagers'])->toBe(163);
|
->and($state['population']['villagers'])->toBe(162);
|
||||||
|
|
||||||
$this->travel(4)->hours();
|
$this->travel(4)->hours();
|
||||||
$this->artisan('game:events --once')->assertSuccessful();
|
$this->artisan('game:events --once')->assertSuccessful();
|
||||||
$state = shown($defender);
|
$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['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 () {
|
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);
|
strike($attacker, $defender, 60);
|
||||||
|
|
||||||
expect(Report::where('player_id', $defender->player_id)->sole()->payload['wounded'])->toBeNull()
|
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' => []]);
|
->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();
|
$ally = startVillage();
|
||||||
$defender->troops()->create(['home_village_id' => $ally->id, 'unit' => 'sword', 'count' => 20]);
|
$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);
|
strike($attacker, $defender->fresh(), 150);
|
||||||
|
|
||||||
$defense = Report::where('player_id', $defender->player_id)->sole()->payload;
|
$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]);
|
->and($defense['wounded']['units'])->toBe(['sword' => 6]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
->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]);
|
$attacker = rallied(startVillage(), ['spear' => 10]);
|
||||||
$defender = startVillage();
|
$defender = startVillage();
|
||||||
$defender->troops()->create(['home_village_id' => $defender->id, 'unit' => 'sword', 'count' => 20]);
|
$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([])
|
expect((array) shown($attacker)['troops'])->toBe([])
|
||||||
->and(shown($attacker)['movements'])->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();
|
$attack = Report::where('player_id', $attacker->player_id)->sole();
|
||||||
$defense = Report::where('player_id', $defender->player_id)->sole();
|
$defense = Report::where('player_id', $defender->player_id)->sole();
|
||||||
expect($attack->type)->toBe('attack')
|
expect($attack->type)->toBe('attack')
|
||||||
->and($attack->payload['won'])->toBeFalse()
|
->and($attack->payload['won'])->toBeFalse()
|
||||||
->and($attack->payload['defender']['units'])->toBeNull()
|
->and($attack->payload['defender']['units'])->toBeNull()
|
||||||
->and($defense->type)->toBe('defense')
|
->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]);
|
->and($defense->payload['attacker']['lost'])->toBe(['spear' => 10]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,8 @@ it('simulates a battle with the world\'s rules and changes nothing', function ()
|
||||||
expect($result['won'])->toBeTrue()
|
expect($result['won'])->toBeTrue()
|
||||||
->and((array) $result['attacker']['units'])->toBe(['light_cavalry' => 100, 'catapult' => 20])
|
->and((array) $result['attacker']['units'])->toBe(['light_cavalry' => 100, 'catapult' => 20])
|
||||||
->and((array) $result['defender']['lost'])->toBe(['spear' => 50, 'militia' => 10])
|
->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.
|
// 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' => 19])
|
->and($result['building'])->toBe(['before' => 20, 'after' => 18])
|
||||||
->and($result['wall'])->toBeNull();
|
->and($result['wall'])->toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,22 +5,35 @@ use App\Game\Combat\CombatResolver;
|
||||||
use App\Game\Combat\LootCalculator;
|
use App\Game\Combat\LootCalculator;
|
||||||
use Carbon\CarbonImmutable;
|
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);
|
$battle = (new CombatResolver(gameConfig()))->fight(['light_cavalry' => 100], ['spear' => 50], 0, 1.0, 0.0);
|
||||||
|
|
||||||
// 13000 cavalry attack against 50 × 45 cavalry defence.
|
// 13000 cavalry attack against 50 × 45 cavalry defence.
|
||||||
expect($battle->attackerWon)->toBeTrue()
|
expect($battle->attackerWon)->toBeTrue()
|
||||||
->and($battle->defenseLoss)->toBe(1.0)
|
->and($battle->defenseLoss)->toBe(1.0)
|
||||||
->and($battle->attackLoss)->toEqualWithDelta((2250 / 13000) ** 1.5, 1e-9)
|
->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' => 7]);
|
->and(Battle::dead(['light_cavalry' => 100], $battle->attackLoss))->toBe(['light_cavalry' => 2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('kills every attacker when the defence holds', function () {
|
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()
|
expect($battle->attackerWon)->toBeFalse()
|
||||||
->and($battle->attackLoss)->toBe(1.0)
|
->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 () {
|
it('weights the defence by the attacker\'s mix of classes', function () {
|
||||||
|
|
|
||||||
|
|
@ -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.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.');
|
->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 () {
|
it('reports roles that share buildings or can never be reached', function () {
|
||||||
$config = gameConfig(['world' => ['village' => ['specialisation' => [
|
$config = gameConfig(['world' => ['village' => ['specialisation' => [
|
||||||
'min_space' => 999,
|
'min_space' => 999,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue