game-village-politics-backend/app/Game/Combat/Battle.php

125 lines
3.9 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Game\Combat;
/**
* The outcome of one battle, from CombatResolver. The volley kills first; the melee
* share then applies to who is left. Every group of a side, each stack of support
* included, falls alike: the volley share of each unit, then the melee share of the
* rest, rounded per group and unit type. attackDead() and defenseDead() give that total.
*/
final readonly class Battle
{
/**
* @param float $attackLoss share of every attacking unit left after the volley that died in the melee, 0..1
* @param float $defenseLoss share of every defending unit left after the volley that died in the melee, 0..1
* @param array<string, float> $attackVolley unit => share of it the defenders' volley killed; units missing were not hit
* @param array<string, float> $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<string, int> $units
* @return array<string, int>
*/
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<string, int> $units
* @return array<string, int>
*/
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<string, int> $units
* @return array<string, int>
*/
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<string, int> $units
* @return array<string, int>
*/
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<string, int> $units
* @return array<string, int>
*/
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<string, int> $units
* @param array<string, float> $shares unit => share; units missing were not hit
* @return array<string, int>
*/
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<string, int> $units
* @param array<string, float> $volley
* @return array<string, int>
*/
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;
}
}