game-village-politics-backend/app/Game/Buildings/BuildingRules.php

181 lines
6.1 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));
}
/** 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;
}
/** 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;
}
}