$attackVolley unit => share of it the defenders' volley killed; units missing were not hit * @param array $defenseVolley unit => share of it the attackers' volley killed */ public function __construct( public bool $attackerWon, public float $attackLoss, public float $defenseLoss, public int $wallBefore, public int $wallAfter, public float $attackPower, public float $defensePower, public array $attackVolley = [], public array $defenseVolley = [], ) {} /** Whether anybody shot before the melee. */ public function volleyed(): bool { return $this->attackVolley !== [] || $this->defenseVolley !== []; } /** * Attackers the defenders' volley killed. * * @param array $units * @return array */ public function attackVolleyDead(array $units): array { return self::shot($units, $this->attackVolley); } /** * Defenders the attackers' volley killed, for one group or the whole defence. * * @param array $units * @return array */ public function defenseVolleyDead(array $units): array { return self::shot($units, $this->defenseVolley); } /** * Every attacker who fell: the volley's dead, then the melee share of the rest. * * @param array $units * @return array */ public function attackDead(array $units): array { return self::fallen($units, $this->attackVolley, $this->attackLoss); } /** * Every defender who fell, for one group (a stack, the militia) or the whole defence. * * @param array $units * @return array */ public function defenseDead(array $units): array { return self::fallen($units, $this->defenseVolley, $this->defenseLoss); } /** * Dead units of a group at a loss share, rounded per unit type. * * @param array $units * @return array */ public static function dead(array $units, float $loss): array { return array_map(fn (int $n) => min($n, (int) round($n * $loss)), $units); } /** * Units a volley killed at these shares, rounded per unit type like dead(). * * @param array $units * @param array $shares unit => share; units missing were not hit * @return array */ public static function shot(array $units, array $shares): array { $dead = []; foreach ($units as $unit => $n) { $dead[$unit] = self::dead([$unit => $n], $shares[$unit] ?? 0.0)[$unit]; } return $dead; } /** * @param array $units * @param array $volley * @return array */ private static function fallen(array $units, array $volley, float $melee): array { $shot = self::shot($units, $volley); $dead = []; foreach ($units as $unit => $n) { $dead[$unit] = $shot[$unit] + self::dead([$unit => $n - $shot[$unit]], $melee)[$unit]; } return $dead; } }