319 lines
12 KiB
PHP
319 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Game\Buildings;
|
|
|
|
use App\Game\WorldConfig;
|
|
|
|
/**
|
|
* Cost, time, workers, output and capacity of a building type per level. Levels start
|
|
* at 1: level 1 has the base values, and every level above multiplies by the factor.
|
|
*/
|
|
final class BuildingRules
|
|
{
|
|
public function __construct(private readonly WorldConfig $config) {}
|
|
|
|
/** Levels above the first, the exponent of every per-level factor. */
|
|
public static function above(int $level): int
|
|
{
|
|
return max(0, $level - 1);
|
|
}
|
|
|
|
/**
|
|
* 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'] ** 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));
|
|
}
|
|
|
|
/**
|
|
* The rank of each instance among its type: by level (highest first, then lowest id),
|
|
* 0 for the first or only one. Copies work and pay by this rank.
|
|
*
|
|
* @param list<BuildingState> $buildings
|
|
* @return array<int, int> building id => rank
|
|
*/
|
|
public function instanceRanks(array $buildings): array
|
|
{
|
|
usort($buildings, fn (BuildingState $a, BuildingState $b) => [$b->level, $a->id] <=> [$a->level, $b->id]);
|
|
$ranks = [];
|
|
$seen = [];
|
|
foreach ($buildings as $b) {
|
|
$ranks[$b->id] = $seen[$b->type] = ($seen[$b->type] ?? -1) + 1;
|
|
}
|
|
|
|
return $ranks;
|
|
}
|
|
|
|
/**
|
|
* The share each instance works at: rank n (instanceRanks) works at
|
|
* economy.instance_share^n, so the first or only one works at 1 and copies give less.
|
|
*
|
|
* @param list<BuildingState> $buildings
|
|
* @return array<int, float> building id => share
|
|
*/
|
|
public function instanceShares(array $buildings): array
|
|
{
|
|
return array_map(fn (int $rank) => $this->instanceShare($rank), $this->instanceRanks($buildings));
|
|
}
|
|
|
|
/** The share the instance at this rank among its type works at (0 = the first). */
|
|
public function instanceShare(int $rank): float
|
|
{
|
|
return (float) $this->config->get('economy.instance_share', 1.0) ** $rank;
|
|
}
|
|
|
|
/** 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));
|
|
}
|
|
|
|
/**
|
|
* Idle villagers an order to reach $level needs: the workers it adds over the level
|
|
* below, all of level 1's for a new instance. Farms need none: they bring more people
|
|
* than they employ, and without that a full village could never grow.
|
|
*/
|
|
public function extraWorkers(string $type, int $level): int
|
|
{
|
|
if ($level < 1 || $this->isFarm($type)) {
|
|
return 0;
|
|
}
|
|
$below = $level > 1 ? $this->workers($type, $level - 1) : 0;
|
|
|
|
return max(0, $this->workers($type, $level) - $below);
|
|
}
|
|
|
|
/**
|
|
* 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(),
|
|
];
|
|
}
|
|
|
|
/** Whether the type makes food. Farms feed the village and are staffed first. */
|
|
public function isFarm(string $type): bool
|
|
{
|
|
return ($this->config->building($type)['production']['resource'] ?? null) === 'food';
|
|
}
|
|
|
|
/**
|
|
* Heads one instance at this level feeds: its food output at full staffing and world
|
|
* speed 1, 0 for a building that makes no food. Population counts in heads (food per
|
|
* hour at world speed 1), so a village holds the same people on every world speed.
|
|
*/
|
|
public function feeds(string $type, int $level): float
|
|
{
|
|
if (! $this->isFarm($type)) {
|
|
return 0.0;
|
|
}
|
|
$production = $this->config->building($type)['production'];
|
|
|
|
return $production['base'] * $production['factor'] ** self::above($level);
|
|
}
|
|
|
|
/** 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;
|
|
}
|
|
|
|
/** Merchants this building has at a level (market, from its table; staffing doesn't scale it), else 0. */
|
|
public function merchants(string $type, int $level): int
|
|
{
|
|
$merchants = $this->config->building($type)['merchants'] ?? null;
|
|
if (! $merchants || $level < 1) {
|
|
return 0;
|
|
}
|
|
|
|
return (int) $merchants[min($level, count($merchants)) - 1];
|
|
}
|
|
|
|
/**
|
|
* 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). A copy shows its output, storage,
|
|
* hiding, beds and training speed at its $share (instanceShares), so its training
|
|
* speed can fall below level 1's.
|
|
*
|
|
* @return list<array{key: string, value: float|int, resource?: string}>
|
|
*/
|
|
public function effects(string $type, int $level, float $share = 1.0): array
|
|
{
|
|
$effects = [];
|
|
if ($output = $this->output($type, $level)) {
|
|
$effects[] = ['key' => 'production', 'value' => round($output['per_hour'] * $share, 1), 'resource' => $output['resource']];
|
|
}
|
|
foreach (['capacity', 'hides', 'beds'] as $key) {
|
|
if ($value = $this->{$key}($type, $level)) {
|
|
$effects[] = ['key' => $key, 'value' => (int) round($value * $share)];
|
|
}
|
|
}
|
|
if ($detection = $this->detection($type, $level)) {
|
|
$effects[] = ['key' => 'detection', 'value' => round($detection, 1)];
|
|
}
|
|
if ($merchants = $this->merchants($type, $level)) {
|
|
$effects[] = ['key' => 'merchants', 'value' => $merchants];
|
|
}
|
|
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) {
|
|
$speed = (1 + $this->config->get('economy.recruit_speed_per_level', 0) * self::above($level)) * $share;
|
|
$effects[] = ['key' => 'recruit_speed', 'value' => (int) round(($speed - 1) * 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<string> $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<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;
|
|
}
|
|
}
|