2026-10-28 18:17:34 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Game\Economy;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Game\Buildings\BuildingRules;
|
|
|
|
|
|
use App\Game\Buildings\BuildingState;
|
2026-10-29 15:20:52 +00:00
|
|
|
|
use App\Game\Buildings\Specialisation;
|
2026-10-28 18:17:34 +00:00
|
|
|
|
use App\Game\WorldConfig;
|
|
|
|
|
|
|
|
|
|
|
|
/** Resource rates per hour from the base trickle, buildings and their staffing. */
|
|
|
|
|
|
final class Production
|
|
|
|
|
|
{
|
|
|
|
|
|
public const RESOURCES = ['wood', 'clay', 'iron', 'food'];
|
|
|
|
|
|
|
|
|
|
|
|
private BuildingRules $rules;
|
|
|
|
|
|
|
2026-10-29 15:20:52 +00:00
|
|
|
|
private Specialisation $specialisation;
|
|
|
|
|
|
|
2026-10-28 18:17:34 +00:00
|
|
|
|
public function __construct(private readonly WorldConfig $config)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->rules = new BuildingRules($config);
|
2026-10-29 15:20:52 +00:00
|
|
|
|
$this->specialisation = new Specialisation($config);
|
2026-10-28 18:17:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Output share at staffing ratio r: e^(k·(r−1)). Full is 1, empty about 0.1. */
|
|
|
|
|
|
public function staffingFactor(float $ratio): float
|
|
|
|
|
|
{
|
|
|
|
|
|
return exp($this->config->get('economy.staffing.k', 2.3) * (min(1.0, max(0.0, $ratio)) - 1));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-29 15:20:52 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @param float $bonus extra share from the village's role (0.2 = +20%)
|
|
|
|
|
|
* @return array{resource: string, per_hour: float}|null
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function output(BuildingState $building, Workforce $workforce, float $bonus = 0.0): ?array
|
2026-10-28 18:17:34 +00:00
|
|
|
|
{
|
|
|
|
|
|
$output = $this->rules->output($building->type, $building->level);
|
|
|
|
|
|
if (! $output) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2026-10-29 15:20:52 +00:00
|
|
|
|
$output['per_hour'] *= $this->staffingFactor($workforce->ratio($building->id)) * (1 + $bonus);
|
2026-10-28 18:17:34 +00:00
|
|
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-29 15:20:52 +00:00
|
|
|
|
* Net change per hour, with the village role's bonus on its buildings. Food is farm
|
|
|
|
|
|
* output minus upkeep, so it can go negative.
|
2026-10-28 18:17:34 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param list<BuildingState> $buildings
|
|
|
|
|
|
* @return array{wood: float, clay: float, iron: float, food: float}
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function rates(array $buildings, Workforce $workforce): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$rates = array_fill_keys(self::RESOURCES, 0.0);
|
|
|
|
|
|
foreach ($this->config->get('economy.base_production', []) as $resource => $perHour) {
|
|
|
|
|
|
$rates[$resource] += $perHour * $this->config->speed();
|
|
|
|
|
|
}
|
2026-10-29 15:20:52 +00:00
|
|
|
|
$role = $this->specialisation->role(array_map(fn (BuildingState $b) => $b->type, $buildings));
|
2026-10-28 18:17:34 +00:00
|
|
|
|
foreach ($buildings as $b) {
|
2026-10-29 15:20:52 +00:00
|
|
|
|
if ($output = $this->output($b, $workforce, $this->specialisation->productionBonus($role, $b->type))) {
|
2026-10-28 18:17:34 +00:00
|
|
|
|
$rates[$output['resource']] += $output['per_hour'];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
$rates['food'] -= $workforce->upkeep;
|
|
|
|
|
|
|
|
|
|
|
|
return $rates;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|