rules = new BuildingRules($config); } /** @return list */ public function roles(): array { return array_keys($this->config->get('world.village.specialisation.roles', [])); } /** * Space each role's buildings take, from one entry per standing instance. * * @param iterable $types * @return array */ public function space(iterable $types): array { $space = array_fill_keys($this->roles(), 0); foreach ($types as $type) { if ($role = $this->roleOf($type)) { $space[$role] += $this->rules->space($type); } } return $space; } /** * The village's role, or null while no role's buildings dominate. * * @param iterable $types one entry per standing instance */ public function role(iterable $types): ?string { $space = $this->space($types); $total = array_sum($space); $share = (float) $this->config->get('world.village.specialisation.share', 1); $min = (int) $this->config->get('world.village.specialisation.min_space', 0); foreach ($space as $role => $taken) { if ($total > 0 && $taken >= $min && $taken / $total >= $share) { return $role; } } return null; } /** The role a building type counts for, if any. */ public function roleOf(string $type): ?string { foreach ($this->config->get('world.village.specialisation.roles', []) as $role => $values) { if (in_array($type, $values['buildings'] ?? [], true)) { return $role; } } return null; } /** Extra output share of this building in a village with that role (0.2 = +20%). */ public function productionBonus(?string $role, string $type): float { return $role !== null && $this->roleOf($type) === $role ? $this->bonus($role, 'production') : 0.0; } /** Extra training speed share in a village with that role. */ public function recruitBonus(?string $role): float { return $role !== null ? $this->bonus($role, 'recruit_speed') : 0.0; } /** Extra defence share for everyone defending a village with that role. */ public function defenseBonus(?string $role): float { return $role !== null ? $this->bonus($role, 'defense') : 0.0; } /** @return array the role's bonuses by key, for the client */ public function bonuses(string $role): array { $values = $this->config->get("world.village.specialisation.roles.$role", []); unset($values['buildings']); return array_map('floatval', $values); } private function bonus(string $role, string $key): float { return (float) $this->config->get("world.village.specialisation.roles.$role.$key", 0); } }