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-11-03 08:38:17 +00:00
|
|
|
*
|
|
|
|
|
* Everything here counts in heads: food per hour at world speed 1. Farms make and
|
|
|
|
|
* people eat world speed times that, so a village holds the same heads on every world
|
|
|
|
|
* speed (Production::rates scales the food rate).
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-31 09:56:59 +00:00
|
|
|
/**
|
2026-11-03 08:38:17 +00:00
|
|
|
* Heads the farms feed: their nominal output at world speed 1, a copy at its
|
|
|
|
|
* instance share.
|
2026-10-31 09:56:59 +00:00
|
|
|
*
|
|
|
|
|
* @param list<BuildingState> $buildings
|
|
|
|
|
*/
|
2026-10-28 18:17:34 +00:00
|
|
|
public function foodBudget(array $buildings): float
|
|
|
|
|
{
|
|
|
|
|
$budget = 0.0;
|
2026-10-31 09:56:59 +00:00
|
|
|
$shares = $this->rules->instanceShares($buildings);
|
2026-10-28 18:17:34 +00:00
|
|
|
foreach ($buildings as $b) {
|
2026-11-03 08:38:17 +00:00
|
|
|
$budget += $this->rules->feeds($b->type, $b->level) * $shares[$b->id];
|
2026-10-28 18:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $budget;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param list<BuildingState> $buildings
|
2026-11-03 08:38:17 +00:00
|
|
|
* @param float $soldierUpkeep heads eaten by soldiers, stationed support, units in training and the wounded
|
2026-10-28 20:55:44 +00:00
|
|
|
* @param int $militia villagers drafted by a levy; they eat as before but don't work
|
2026-10-28 18:17:34 +00:00
|
|
|
*/
|
2026-10-28 20:55:44 +00:00
|
|
|
public function workforce(array $buildings, float $soldierUpkeep = 0.0, int $militia = 0): Workforce
|
2026-10-28 18:17:34 +00:00
|
|
|
{
|
|
|
|
|
$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 20:55:44 +00:00
|
|
|
$militia = min($militia, $villagers);
|
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,
|
2026-10-28 20:55:44 +00:00
|
|
|
assigned: $this->assign($buildings, $required, $villagers - $militia),
|
2026-10-28 18:17:34 +00:00
|
|
|
required: $required,
|
2026-10-28 20:55:44 +00:00
|
|
|
militia: $militia,
|
2026-10-28 18:17:34 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-03 08:41:12 +00:00
|
|
|
/**
|
|
|
|
|
* The lowest level of this farm, from its own up to its max, at which the villagers
|
|
|
|
|
* left after $soldierUpkeep cover the workers every building needs. The max when
|
|
|
|
|
* even that falls short.
|
|
|
|
|
*
|
|
|
|
|
* @param list<BuildingState> $buildings
|
|
|
|
|
*/
|
|
|
|
|
public function lowestFarmLevel(array $buildings, int $farmId, float $soldierUpkeep): int
|
|
|
|
|
{
|
|
|
|
|
$farm = current(array_filter($buildings, fn (BuildingState $b) => $b->id === $farmId));
|
|
|
|
|
$max = $this->rules->maxLevel($farm->type);
|
|
|
|
|
for ($level = $farm->level; $level < $max; $level++) {
|
|
|
|
|
$states = array_map(fn (BuildingState $b) => $b->id === $farmId ? new BuildingState($b->id, $b->type, $level, $b->priority) : $b, $buildings);
|
|
|
|
|
$workforce = $this->workforce($states, $soldierUpkeep);
|
|
|
|
|
if ($workforce->villagers >= array_sum($workforce->required)) {
|
|
|
|
|
return $level;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $max;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 18:17:34 +00:00
|
|
|
/**
|
|
|
|
|
* 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) {
|
2026-11-03 08:38:17 +00:00
|
|
|
$groups[($this->rules->isFarm($b->type) ? 'a' : 'b').str_pad((string) (99 - $b->priority), 3, '0', STR_PAD_LEFT)][] = $b->id;
|
2026-10-28 18:17:34 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|