units = new UnitRules($config); } /** @return list */ 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 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 */ 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 $current * @param array $full * @return array */ 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 $current * @param array $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 $shares * @return array */ 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); } }