2026-10-28 18:17:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Game\Economy;
|
|
|
|
|
|
|
|
|
|
use App\Game\Buildings\BuildingRules;
|
|
|
|
|
use App\Game\Buildings\BuildingState;
|
|
|
|
|
use App\Game\WorldConfig;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Food budget, villagers and who works where.
|
|
|
|
|
*
|
|
|
|
|
* Capacity uses nominal (fully staffed) farm output, so farms, workers and capacity
|
2026-10-28 20:28:16 +00:00
|
|
|
* can't feed back into each other. Villagers fill `villager_share` of the budget,
|
|
|
|
|
* minus what soldiers eat; the rest of the farms' output is surplus for storage.
|
2026-10-28 18:17:34 +00:00
|
|
|
*/
|
|
|
|
|
final class Population
|
|
|
|
|
{
|
|
|
|
|
private BuildingRules $rules;
|
|
|
|
|
|
|
|
|
|
public function __construct(private readonly WorldConfig $config)
|
|
|
|
|
{
|
|
|
|
|
$this->rules = new BuildingRules($config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param list<BuildingState> $buildings */
|
|
|
|
|
public function foodBudget(array $buildings): float
|
|
|
|
|
{
|
|
|
|
|
$budget = 0.0;
|
|
|
|
|
foreach ($buildings as $b) {
|
|
|
|
|
$output = $this->rules->output($b->type, $b->level);
|
|
|
|
|
if ($output && $output['resource'] === 'food') {
|
|
|
|
|
$budget += $output['per_hour'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $budget;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param list<BuildingState> $buildings
|
|
|
|
|
* @param float $soldierUpkeep food/h eaten by soldiers and stationed support
|
|
|
|
|
*/
|
|
|
|
|
public function workforce(array $buildings, float $soldierUpkeep = 0.0): Workforce
|
|
|
|
|
{
|
|
|
|
|
$budget = $this->foodBudget($buildings);
|
|
|
|
|
$perVillager = $this->config->get('economy.upkeep.villager', 1);
|
2026-10-28 20:28:16 +00:00
|
|
|
$share = $this->config->get('economy.villager_share', 0.8);
|
|
|
|
|
$villagers = max(0, (int) floor(($budget * $share - $soldierUpkeep) / $perVillager));
|
2026-10-28 18:17:34 +00:00
|
|
|
|
|
|
|
|
$required = [];
|
|
|
|
|
foreach ($buildings as $b) {
|
|
|
|
|
$required[$b->id] = $this->rules->workers($b->type, $b->level);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Workforce(
|
|
|
|
|
foodBudget: $budget,
|
|
|
|
|
villagers: $villagers,
|
|
|
|
|
upkeep: $villagers * $perVillager + $soldierUpkeep,
|
|
|
|
|
assigned: $this->assign($buildings, $required, $villagers),
|
|
|
|
|
required: $required,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Farms first, then by priority (highest first). Buildings with the same priority
|
|
|
|
|
* share what is left evenly, one villager at a time in id order.
|
|
|
|
|
*
|
|
|
|
|
* @param list<BuildingState> $buildings
|
|
|
|
|
* @param array<int, int> $required
|
|
|
|
|
* @return array<int, int>
|
|
|
|
|
*/
|
|
|
|
|
private function assign(array $buildings, array $required, int $villagers): array
|
|
|
|
|
{
|
|
|
|
|
$groups = [];
|
|
|
|
|
foreach ($buildings as $b) {
|
|
|
|
|
$isFarm = ($this->rules->output($b->type, $b->level)['resource'] ?? null) === 'food';
|
|
|
|
|
$groups[($isFarm ? 'a' : 'b').str_pad((string) (99 - $b->priority), 3, '0', STR_PAD_LEFT)][] = $b->id;
|
|
|
|
|
}
|
|
|
|
|
ksort($groups);
|
|
|
|
|
|
|
|
|
|
$assigned = array_fill_keys(array_keys($required), 0);
|
|
|
|
|
$free = $villagers;
|
|
|
|
|
foreach ($groups as $ids) {
|
|
|
|
|
sort($ids);
|
|
|
|
|
$open = array_values(array_filter($ids, fn (int $id) => $required[$id] > 0));
|
|
|
|
|
while ($free > 0 && $open) {
|
|
|
|
|
foreach ($open as $i => $id) {
|
|
|
|
|
if ($free === 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$assigned[$id]++;
|
|
|
|
|
$free--;
|
|
|
|
|
if ($assigned[$id] >= $required[$id]) {
|
|
|
|
|
unset($open[$i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $assigned;
|
|
|
|
|
}
|
|
|
|
|
}
|