game-village-politics-backend/app/Game/Map/BarbarianGarrison.php

148 lines
4.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Game\Map;
use App\Game\Military\UnitRules;
use App\Game\WorldConfig;
/**
* The troops a barbarian village holds (world.map.barbarians.garrison). Full strength
* follows its points, in one of a few profiles fixed by the village id, so scouting a
* village tells an attacker what to send. After a hit it regroups step by step.
*/
final class BarbarianGarrison
{
private UnitRules $units;
public function __construct(private readonly WorldConfig $config)
{
$this->units = new UnitRules($config);
}
/** @return list<string> */
public function profiles(): array
{
return array_keys($this->config->get('world.map.barbarians.garrison.profiles', []));
}
/** The profile a barbarian village fields, or null when the world has none. */
public function profile(int $villageId): ?string
{
$profiles = $this->profiles();
return $profiles ? $profiles[$villageId % count($profiles)] : null;
}
/** @return array<string, float> unit => share of heads */
public function shares(string $profile): array
{
return $this->config->get("world.map.barbarians.garrison.profiles.$profile", []);
}
/**
* Full strength: per_point × (points − free_points) heads split by the profile's
* shares, cut down so its upkeep stays within max_food_share of the food budget.
*
* @return array<string, int>
*/
public function full(?string $profile, int $points, float $foodBudget): array
{
if ($profile === null) {
return [];
}
$garrison = $this->config->get('world.map.barbarians.garrison');
$shares = $this->shares($profile);
$heads = (int) floor($garrison['per_point'] * max(0, $points - $garrison['free_points']));
$perHead = array_sum(array_map(fn (string $u, float $s) => $s * $this->units->upkeep($u), array_keys($shares), $shares));
if ($perHead > 0) {
$heads = min($heads, (int) floor($garrison['max_food_share'] * $foodBudget / $perHead));
}
return self::split(max(0, $heads), $shares);
}
/**
* One regroup step: every unit gets regroup.share of its full count back (at least
* one), never above full.
*
* @param array<string, int> $current
* @param array<string, int> $full
* @return array<string, int>
*/
public function regroup(array $current, array $full): array
{
$share = (float) $this->config->get('world.map.barbarians.garrison.regroup.share', 0.25);
$next = $current;
foreach ($full as $unit => $count) {
$have = $current[$unit] ?? 0;
if ($have < $count) {
$next[$unit] = min($count, $have + max(1, (int) ceil($share * $count)));
}
}
return $next;
}
/**
* How much of full strength stands, by food upkeep (a rider weighs three spears), 0..1.
* A village without a garrison to field counts as full.
*
* @param array<string, int> $current
* @param array<string, int> $full
*/
public function strength(array $current, array $full): float
{
$weigh = fn (array $units) => array_sum(array_map(fn (string $u, int $n) => $n * $this->units->upkeep($u), array_keys($units), $units));
$whole = $weigh($full);
if ($whole <= 0) {
return 1.0;
}
return min(1.0, $weigh(array_intersect_key($current, $full)) / $whole);
}
public function isFull(array $current, array $full): bool
{
foreach ($full as $unit => $count) {
if (($current[$unit] ?? 0) < $count) {
return false;
}
}
return true;
}
/** Seconds between two regroup steps, at world speed. */
public function interval(): int
{
$hours = $this->config->get('world.map.barbarians.garrison.regroup.every_hours', 2);
return max(1, (int) round($hours * 3600 / $this->config->speed()));
}
/**
* Whole heads by share, largest remainders first, so they add up to $heads.
*
* @param array<string, float> $shares
* @return array<string, int>
*/
public static function split(int $heads, array $shares): array
{
$total = array_sum($shares);
if ($heads <= 0 || $total <= 0) {
return [];
}
$exact = array_map(fn (float $s) => $heads * $s / $total, $shares);
$counts = array_map(fn (float $n) => (int) floor($n), $exact);
$rest = $heads - array_sum($counts);
$remainders = array_map(fn (float $n, int $c) => $n - $c, $exact, $counts);
arsort($remainders);
$keys = array_keys($shares);
foreach (array_slice(array_keys($remainders), 0, $rest) as $i) {
$counts[$keys[$i]]++;
}
return array_filter($counts);
}
}