2026-10-28 18:17:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Game\Buildings;
|
|
|
|
|
|
|
|
|
|
use App\Game\WorldConfig;
|
|
|
|
|
|
|
|
|
|
/** Cost, time, workers, output and capacity of a building type per level. */
|
|
|
|
|
final class BuildingRules
|
|
|
|
|
{
|
|
|
|
|
public function __construct(private readonly WorldConfig $config) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cost of reaching $level for the $instance-th instance of the type (0-based).
|
|
|
|
|
*
|
|
|
|
|
* @return array{wood: int, clay: int, iron: int}
|
|
|
|
|
*/
|
|
|
|
|
public function cost(string $type, int $level, int $instance = 0): array
|
|
|
|
|
{
|
|
|
|
|
$cost = $this->config->building($type)['cost'];
|
|
|
|
|
$multiplier = $cost['factor'] ** $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'] ** $level
|
|
|
|
|
/ $this->config->speed()
|
|
|
|
|
/ (1 + $this->config->get('economy.hq_build_speed_per_level', 0) * $hqLevel)
|
|
|
|
|
/ max($hqStaffing, 0.01);
|
|
|
|
|
|
|
|
|
|
return max(1, (int) ceil($seconds));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Seconds to take $level down by one. */
|
|
|
|
|
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'] ** $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'] ** $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'] ** $level : 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 18:45:02 +00:00
|
|
|
/** Points an instance at this level adds to its village. */
|
|
|
|
|
public function points(string $type, int $level): int
|
2026-10-28 18:17:34 +00:00
|
|
|
{
|
2026-10-28 18:45:02 +00:00
|
|
|
$points = $this->config->building($type)['points'];
|
|
|
|
|
|
|
|
|
|
return (int) round($points['base'] * $points['factor'] ** $level);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Building space one instance takes, at every level. */
|
|
|
|
|
public function space(string $type): int
|
|
|
|
|
{
|
|
|
|
|
return $this->config->building($type)['space'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Space taken by these building types together.
|
|
|
|
|
*
|
|
|
|
|
* @param iterable<string> $types one entry per instance
|
|
|
|
|
*/
|
|
|
|
|
public function spaceUsed(iterable $types): int
|
|
|
|
|
{
|
|
|
|
|
$used = 0;
|
|
|
|
|
foreach ($types as $type) {
|
|
|
|
|
$used += $this->space($type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $used;
|
2026-10-28 18:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<string, int> 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;
|
|
|
|
|
}
|
|
|
|
|
}
|