config->building($type)['cost']; $multiplier = $cost['factor'] ** self::above($level) * $this->instanceFactor($type) ** $instance; return [ 'wood' => (int) round($cost['wood'] * $multiplier), 'clay' => (int) round($cost['clay'] * $multiplier), 'iron' => (int) round($cost['iron'] * $multiplier), ]; } /** * Seconds to build $level, at world speed, sped up by the headquarters' level * and slowed by its staffing (output factor 0..1). */ public function buildSeconds(string $type, int $level, int $hqLevel, float $hqStaffing): int { $time = $this->config->building($type)['build_time']; $seconds = $time['base'] * $time['factor'] ** self::above($level) / $this->config->speed() / (1 + $this->config->get('economy.hq_build_speed_per_level', 0) * self::above($hqLevel)) / max($hqStaffing, 0.01); return max(1, (int) ceil($seconds)); } /** Seconds to take $level down by one (level 1 down means removing the instance). */ public function demolishSeconds(string $type, int $level, int $hqLevel, float $hqStaffing): int { $factor = $this->config->get('economy.demolish_time_factor', 0.5); return max(1, (int) ceil($this->buildSeconds($type, $level, $hqLevel, $hqStaffing) * $factor)); } /** @return array{wood: int, clay: int, iron: int} */ public function demolishRefund(string $type, int $level, int $instance = 0): array { $share = $this->config->get('economy.demolish_refund', 0.5); return array_map(fn (int $n) => (int) floor($n * $share), $this->cost($type, $level, $instance)); } public function workers(string $type, int $level): int { $workers = $this->config->building($type)['workers']; return (int) round($workers['base'] * $workers['factor'] ** self::above($level)); } /** * Output per hour at full staffing and world speed, or null for non-producers. * * @return array{resource: string, per_hour: float}|null */ public function output(string $type, int $level): ?array { $production = $this->config->building($type)['production'] ?? null; if (! $production) { return null; } return [ 'resource' => $production['resource'], 'per_hour' => $production['base'] * $production['factor'] ** self::above($level) * $this->config->speed(), ]; } /** Storage capacity this building adds, 0 for non-storage. */ public function capacity(string $type, int $level): float { $capacity = $this->config->building($type)['capacity'] ?? null; return $capacity ? $capacity['base'] * $capacity['factor'] ** self::above($level) : 0.0; } /** Wood, clay and iron each this building keeps from raiders (hiding place), else 0. */ public function hides(string $type, int $level): float { $hides = $this->config->building($type)['hides'] ?? null; return $hides ? $hides['base'] * $hides['factor'] ** self::above($level) : 0.0; } /** Fields this building adds to the village's detection radius (watchtower), else 0. */ public function detection(string $type, int $level): float { $detection = $this->config->building($type)['detection'] ?? null; return $detection ? $detection['base'] * $detection['factor'] ** self::above($level) : 0.0; } /** Beds for the wounded this building has at full staffing (hospital), else 0. */ public function beds(string $type, int $level): float { $beds = $this->config->building($type)['beds'] ?? null; return $beds ? $beds['base'] * $beds['factor'] ** self::above($level) : 0.0; } /** * What one instance does at a level, for the building's own screen: production at * full staffing, storage, hiding, beds, detection, the wall's defence, the headquarters' * build speed, builders and space, a military building's training speed. Percentages * are the bonus over level 1 (or over no wall). * * @return list */ public function effects(string $type, int $level): array { $effects = []; if ($output = $this->output($type, $level)) { $effects[] = ['key' => 'production', 'value' => round($output['per_hour'], 1), 'resource' => $output['resource']]; } foreach (['capacity', 'hides', 'beds'] as $key) { if ($value = $this->{$key}($type, $level)) { $effects[] = ['key' => $key, 'value' => (int) round($value)]; } } if ($detection = $this->detection($type, $level)) { $effects[] = ['key' => 'detection', 'value' => round($detection, 1)]; } if ($type === 'wall') { $wall = $this->config->get('combat.wall'); $effects[] = ['key' => 'wall_defense', 'value' => $wall['basic_defense'] * $level]; $effects[] = ['key' => 'wall_bonus', 'value' => (int) round(($wall['factor'] ** $level - 1) * 100)]; } if ($type === 'headquarters') { $effects[] = ['key' => 'build_speed', 'value' => (int) round($this->config->get('economy.hq_build_speed_per_level', 0) * self::above($level) * 100)]; $effects[] = ['key' => 'builders', 'value' => $this->builders($level)]; $effects[] = ['key' => 'space', 'value' => $this->spaceMax((int) $this->config->get('world.village.space'), $level)]; } $trains = array_filter($this->config->get('units', []), fn (array $u) => ($u['building'] ?? null) === $type); if ($trains) { $effects[] = ['key' => 'recruit_speed', 'value' => (int) round($this->config->get('economy.recruit_speed_per_level', 0) * self::above($level) * 100)]; } return $effects; } /** Points an instance at this level adds to its village. */ public function points(string $type, int $level): int { $points = $this->config->building($type)['points']; return (int) round($points['base'] * $points['factor'] ** self::above($level)); } /** Building space one instance takes, at every level. */ public function space(string $type): int { return $this->config->building($type)['space']; } /** A village's building space with its headquarters at this level; $base is the village's own. */ public function spaceMax(int $base, int $hqLevel): int { return $base + $this->config->get('world.village.space_per_hq_level', 0) * self::above($hqLevel); } /** * Space taken by these building types together. * * @param iterable $types one entry per instance */ public function spaceUsed(iterable $types): int { $used = 0; foreach ($types as $type) { $used += $this->space($type); } return $used; } public function maxLevel(string $type): int { return $this->config->building($type)['max_level']; } public function maxPerVillage(string $type): ?int { return $this->config->building($type)['max_per_village']; } /** @return array type => minimum level of any instance */ public function requires(string $type): array { return $this->config->building($type)['requires'] ?? []; } public function builders(int $hqLevel): int { $extra = array_filter($this->config->get('world.builders.extra_at_hq_levels', []), fn (int $l) => $hqLevel >= $l); return $this->config->get('world.builders.base', 2) + count($extra); } private function instanceFactor(string $type): float { return $this->config->building($type)['instance_cost_factor'] ?? 1.0; } }