49 lines
1.7 KiB
PHP
49 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Game\Military;
|
||
|
|
|
||
|
|
use App\Game\WorldConfig;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The Lazarett (architecture.md, "Hospital"): of the own defenders who fall, a share
|
||
|
|
* comes back wounded, as far as there are free beds, and stands again after a while.
|
||
|
|
*/
|
||
|
|
final class Wounds
|
||
|
|
{
|
||
|
|
public function __construct(private readonly WorldConfig $config) {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The wounded among the fallen: `share` of each unit, rounded down. When they need
|
||
|
|
* more beds than are free, every unit gets its even part of the beds, and the beds
|
||
|
|
* left over by rounding go to the largest remainders.
|
||
|
|
*
|
||
|
|
* @param array<string, int> $dead
|
||
|
|
* @return array<string, int>
|
||
|
|
*/
|
||
|
|
public function admit(array $dead, int $freeBeds): array
|
||
|
|
{
|
||
|
|
$share = (float) $this->config->get('combat.hospital.share', 0);
|
||
|
|
$wounded = array_filter(array_map(fn (int $n) => (int) floor($n * $share), $dead));
|
||
|
|
$total = array_sum($wounded);
|
||
|
|
if ($total <= $freeBeds) {
|
||
|
|
return $wounded;
|
||
|
|
}
|
||
|
|
$exact = array_map(fn (int $n) => $n * max(0, $freeBeds) / $total, $wounded);
|
||
|
|
$fits = array_map(fn (float $n) => (int) floor($n), $exact);
|
||
|
|
$rest = array_map(fn (float $n, int $floor) => $n - $floor, $exact, $fits);
|
||
|
|
$rest = array_combine(array_keys($exact), $rest);
|
||
|
|
arsort($rest);
|
||
|
|
foreach (array_slice(array_keys($rest), 0, max(0, $freeBeds) - array_sum($fits)) as $unit) {
|
||
|
|
$fits[$unit]++;
|
||
|
|
}
|
||
|
|
|
||
|
|
return array_filter($fits);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Seconds from the battle until its wounded stand again, at world speed. */
|
||
|
|
public function healSeconds(): int
|
||
|
|
{
|
||
|
|
return max(1, (int) ceil($this->config->get('combat.hospital.heal_time', 0) / $this->config->speed()));
|
||
|
|
}
|
||
|
|
}
|