234 lines
9.0 KiB
PHP
234 lines
9.0 KiB
PHP
<?php
|
||
|
||
namespace App\Game\Combat;
|
||
|
||
use App\Game\WorldConfig;
|
||
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 − 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
|
||
{
|
||
public function __construct(private readonly WorldConfig $config) {}
|
||
|
||
/**
|
||
* @param array<string, int> $attack units sent
|
||
* @param array<string, int> $defense units in the village; `militia` for levied villagers
|
||
* @param float $luck between −luck and +luck, see luck()
|
||
* @param float $night defence multiplier, see night()
|
||
*/
|
||
public function fight(array $attack, array $defense, int $wall, float $morale, float $luck, float $night = 1.0): Battle
|
||
{
|
||
// Both sides shoot at once, from their numbers at the start. The defenders shoot
|
||
// from the wall, and it covers them from the attackers' volley.
|
||
$cover = $this->config->get('combat.wall.factor', 1.0) ** $wall;
|
||
$attackVolley = $this->volley($defense, $attack, $cover);
|
||
$defenseVolley = $this->volley($attack, $defense, 1 / $cover);
|
||
$attack = self::left($attack, $attackVolley);
|
||
$defense = self::left($defense, $defenseVolley);
|
||
|
||
// 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);
|
||
$total = array_sum($byClass);
|
||
if ($total <= 0) {
|
||
return new Battle(false, 1.0, 0.0, $wall, $wall, 0.0, 0.0, $attackVolley, $defenseVolley);
|
||
}
|
||
|
||
$attackPower = $total * $morale * (1 + $luck);
|
||
$defensePower = $this->defense($defense, $byClass, $total, $wallAfter) * $night;
|
||
|
||
if ($attackPower > $defensePower) {
|
||
return new Battle(true, $this->winnerLoss($defensePower / $attackPower), 1.0, $wall, $wallAfter, $attackPower, $defensePower, $attackVolley, $defenseVolley);
|
||
}
|
||
|
||
return new Battle(false, 1.0, $this->winnerLoss($attackPower / $defensePower), $wall, $wallAfter, $attackPower, $defensePower, $attackVolley, $defenseVolley);
|
||
}
|
||
|
||
/**
|
||
* The volley before the melee: the shooters' summed `volley` × $factor, soaked by the
|
||
* other side's food upkeep (a levied villager's for militia). It kills that share of
|
||
* every unit it hits, all of them at most. Scouts neither shoot nor get hit.
|
||
*
|
||
* @param array<string, int> $shooters
|
||
* @param array<string, int> $targets
|
||
* @return array<string, float> unit => share killed; empty when nobody shoots or nobody is there
|
||
*/
|
||
public function volley(array $shooters, array $targets, float $factor = 1.0): array
|
||
{
|
||
$volley = 0.0;
|
||
foreach ($shooters as $unit => $count) {
|
||
if ($unit !== 'militia' && ! $this->isScout($unit)) {
|
||
$volley += $count * (float) $this->config->get("units.$unit.volley", 0);
|
||
}
|
||
}
|
||
$soak = 0.0;
|
||
$hit = [];
|
||
foreach ($targets as $unit => $count) {
|
||
if ($unit === 'militia' || ! $this->isScout($unit)) {
|
||
$soak += $count * ($unit === 'militia' ? (float) $this->config->get('economy.upkeep.villager', 1) : (float) $this->config->get("units.$unit.upkeep"));
|
||
$hit[] = $unit;
|
||
}
|
||
}
|
||
$volley *= $factor;
|
||
if ($volley <= 0 || $soak <= 0) {
|
||
return [];
|
||
}
|
||
|
||
return array_fill_keys($hit, min(1.0, $volley / $soak));
|
||
}
|
||
|
||
/**
|
||
* 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));
|
||
}
|
||
|
||
/**
|
||
* Attacking scouts against the scouts in the village. They get through when they
|
||
* outnumber the defence (scouting.defense × defenders) and lose (defence / attack)^1.5
|
||
* of themselves; otherwise all of them die. Defending scouts never die here.
|
||
*
|
||
* @return array{0: bool, 1: int} whether they got through, and how many died
|
||
*/
|
||
public function scout(int $attackers, int $defenders): array
|
||
{
|
||
if ($attackers <= 0) {
|
||
return [false, 0];
|
||
}
|
||
$defense = $defenders * (float) $this->config->get('combat.scouting.defense', 1.0);
|
||
if ($attackers <= $defense) {
|
||
return [false, $attackers];
|
||
}
|
||
$loss = ($defense / $attackers) ** (float) $this->config->get('combat.loss_exponent', 1.5);
|
||
|
||
return [true, min($attackers, (int) round($attackers * $loss))];
|
||
}
|
||
|
||
/**
|
||
* The level a building is left at after $count rams or catapults hit it. Each level
|
||
* L down needs base × factor^(L − 1) of them; nothing drops below level 1.
|
||
*/
|
||
public function siege(string $kind, int $level, int $count): int
|
||
{
|
||
$siege = $this->config->get("combat.siege.$kind");
|
||
$left = (float) $count;
|
||
while ($level > 1) {
|
||
$needed = $siege['base'] * $siege['factor'] ** ($level - 1);
|
||
if ($left < $needed) {
|
||
break;
|
||
}
|
||
$left -= $needed;
|
||
$level--;
|
||
}
|
||
|
||
return $level;
|
||
}
|
||
|
||
/** Attack multiplier from points: 1 against barbarians (null), less against smaller players. */
|
||
public function morale(?int $defenderPoints, int $attackerPoints): float
|
||
{
|
||
$morale = $this->config->get('combat.morale');
|
||
if (! ($morale['enabled'] ?? false) || $defenderPoints === null || $attackerPoints <= 0) {
|
||
return 1.0;
|
||
}
|
||
|
||
return min(1.0, $morale['floor'] + $morale['points_factor'] * $defenderPoints / $attackerPoints);
|
||
}
|
||
|
||
/** Luck from a roll in [0, 1): spread evenly over [−luck, +luck]. */
|
||
public function luck(float $roll): float
|
||
{
|
||
return (2 * $roll - 1) * (float) $this->config->get('combat.luck', 0);
|
||
}
|
||
|
||
/** Defence multiplier at $at: the night factor while it is night on the world's clock. */
|
||
public function night(CarbonImmutable $at): float
|
||
{
|
||
$night = $this->config->get('combat.night');
|
||
if (! ($night['enabled'] ?? false)) {
|
||
return 1.0;
|
||
}
|
||
$hour = $at->setTimezone($night['timezone'] ?? 'UTC')->hour;
|
||
$inside = $night['start'] <= $night['end']
|
||
? $hour >= $night['start'] && $hour < $night['end']
|
||
: $hour >= $night['start'] || $hour < $night['end'];
|
||
|
||
return $inside ? (float) $night['factor'] : 1.0;
|
||
}
|
||
|
||
private function isScout(string $unit): bool
|
||
{
|
||
return (bool) $this->config->get("units.$unit.scout", false);
|
||
}
|
||
|
||
/**
|
||
* Who is left of a side after the volley, rounded per unit type like Battle::dead.
|
||
*
|
||
* @param array<string, int> $units
|
||
* @param array<string, float> $shares
|
||
* @return array<string, int>
|
||
*/
|
||
private static function left(array $units, array $shares): array
|
||
{
|
||
$shot = Battle::shot($units, $shares);
|
||
foreach ($units as $unit => $n) {
|
||
$units[$unit] = $n - $shot[$unit];
|
||
}
|
||
|
||
return $units;
|
||
}
|
||
|
||
/**
|
||
* @param array<string, int> $units
|
||
* @return array<string, float> class => attack strength
|
||
*/
|
||
private function attackByClass(array $units): array
|
||
{
|
||
$byClass = ['infantry' => 0.0, 'cavalry' => 0.0, 'archer' => 0.0];
|
||
foreach ($units as $unit => $count) {
|
||
$values = $this->config->get("units.$unit");
|
||
$byClass[$values['class']] += $count * $values['attack'];
|
||
}
|
||
|
||
return $byClass;
|
||
}
|
||
|
||
/**
|
||
* Defence against this attack mix, with the wall's basic defence and bonus.
|
||
*
|
||
* @param array<string, int> $units
|
||
* @param array<string, float> $byClass
|
||
*/
|
||
private function defense(array $units, array $byClass, float $total, int $wall): float
|
||
{
|
||
$sum = 0.0;
|
||
foreach ($units as $unit => $count) {
|
||
$values = $unit === 'militia'
|
||
? $this->config->get('combat.militia.defense')
|
||
: $this->config->get("units.$unit.defense");
|
||
foreach ($byClass as $class => $strength) {
|
||
$sum += $count * $values[$class] * $strength / $total;
|
||
}
|
||
}
|
||
$walls = $this->config->get('combat.wall');
|
||
|
||
return ($sum + $walls['basic_defense'] * $wall) * $walls['factor'] ** $wall;
|
||
}
|
||
}
|