game-village-politics-backend/app/Game/Economy/Population.php

134 lines
4.7 KiB
PHP

<?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
* 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.
*
* 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).
*/
final class Population
{
private BuildingRules $rules;
public function __construct(private readonly WorldConfig $config)
{
$this->rules = new BuildingRules($config);
}
/**
* Heads the farms feed: their nominal output at world speed 1, a copy at its
* instance share.
*
* @param list<BuildingState> $buildings
*/
public function foodBudget(array $buildings): float
{
$budget = 0.0;
$shares = $this->rules->instanceShares($buildings);
foreach ($buildings as $b) {
$budget += $this->rules->feeds($b->type, $b->level) * $shares[$b->id];
}
return $budget;
}
/**
* @param list<BuildingState> $buildings
* @param float $soldierUpkeep heads eaten by soldiers, stationed support, units in training and the wounded
* @param int $militia villagers drafted by a levy; they eat as before but don't work
*/
public function workforce(array $buildings, float $soldierUpkeep = 0.0, int $militia = 0): Workforce
{
$budget = $this->foodBudget($buildings);
$perVillager = $this->config->get('economy.upkeep.villager', 1);
$share = $this->config->get('economy.villager_share', 0.8);
$villagers = max(0, (int) floor(($budget * $share - $soldierUpkeep) / $perVillager));
$militia = min($militia, $villagers);
$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 - $militia),
required: $required,
militia: $militia,
);
}
/**
* 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;
}
/**
* 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) {
$groups[($this->rules->isFarm($b->type) ? '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;
}
}