feat: population counts heads, the same on every world speed: farms feed their output at speed 1, and the food rate scales farm output and upkeep alike

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-03 09:38:17 +01:00
parent b3a73c647d
commit 25c5007d92
7 changed files with 50 additions and 18 deletions

View File

@ -126,8 +126,9 @@ final class Simulation
} }
/** /**
* Food per hour eaten by troops standing here, by units still in training and by the * Heads (food per hour at world speed 1) eaten by troops standing here, by units still
* wounded: a queued unit takes its villager's place at once, a wounded one keeps it. * in training and by the wounded: a queued unit takes its villager's place at once, a
* wounded one keeps it.
*/ */
public function soldierUpkeep(): float public function soldierUpkeep(): float
{ {

View File

@ -128,6 +128,27 @@ final class BuildingRules
]; ];
} }
/** Whether the type makes food. Farms feed the village and are staffed first. */
public function isFarm(string $type): bool
{
return ($this->config->building($type)['production']['resource'] ?? null) === 'food';
}
/**
* Heads one instance at this level feeds: its food output at full staffing and world
* speed 1, 0 for a building that makes no food. Population counts in heads (food per
* hour at world speed 1), so a village holds the same people on every world speed.
*/
public function feeds(string $type, int $level): float
{
if (! $this->isFarm($type)) {
return 0.0;
}
$production = $this->config->building($type)['production'];
return $production['base'] * $production['factor'] ** self::above($level);
}
/** Storage capacity this building adds, 0 for non-storage. */ /** Storage capacity this building adds, 0 for non-storage. */
public function capacity(string $type, int $level): float public function capacity(string $type, int $level): float
{ {

View File

@ -297,8 +297,8 @@ final class ConfigLint
continue; continue;
} }
$output = $rules->output($type, $level); // Heads, like the garrison's upkeep below.
$food += ($output['resource'] ?? null) === 'food' ? $output['per_hour'] : 0; $food += $rules->feeds($type, $level);
} }
$upkeep = 0.0; $upkeep = 0.0;
foreach ($season['garrison'] ?? [] as $unit => $count) { foreach ($season['garrison'] ?? [] as $unit => $count) {
@ -386,12 +386,13 @@ final class ConfigLint
$problems = []; $problems = [];
$perVillager = $config->get('economy.upkeep.villager', 1); $perVillager = $config->get('economy.upkeep.villager', 1);
foreach ($config->buildingTypes() as $type) { foreach ($config->buildingTypes() as $type) {
if (($rules->output($type, 1)['resource'] ?? null) !== 'food') { if (! $rules->isFarm($type)) {
continue; continue;
} }
for ($level = 1; $level <= $rules->maxLevel($type); $level++) { for ($level = 1; $level <= $rules->maxLevel($type); $level++) {
$output = $rules->output($type, $level)['per_hour']; // Both in heads, so this holds on every world speed.
if ($output <= 0 || $rules->workers($type, $level) * $perVillager >= $output) { $feeds = $rules->feeds($type, $level);
if ($feeds <= 0 || $rules->workers($type, $level) * $perVillager >= $feeds) {
$problems[] = "[$type] level $level feeds fewer people than it needs as workers."; $problems[] = "[$type] level $level feeds fewer people than it needs as workers.";
} }
} }

View File

@ -12,6 +12,10 @@ use App\Game\WorldConfig;
* Capacity uses nominal (fully staffed) farm output, so farms, workers and capacity * 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, * 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. * 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 final class Population
{ {
@ -23,7 +27,8 @@ final class Population
} }
/** /**
* Nominal farm output, a copy at its instance share. * Heads the farms feed: their nominal output at world speed 1, a copy at its
* instance share.
* *
* @param list<BuildingState> $buildings * @param list<BuildingState> $buildings
*/ */
@ -32,10 +37,7 @@ final class Population
$budget = 0.0; $budget = 0.0;
$shares = $this->rules->instanceShares($buildings); $shares = $this->rules->instanceShares($buildings);
foreach ($buildings as $b) { foreach ($buildings as $b) {
$output = $this->rules->output($b->type, $b->level); $budget += $this->rules->feeds($b->type, $b->level) * $shares[$b->id];
if ($output && $output['resource'] === 'food') {
$budget += $output['per_hour'] * $shares[$b->id];
}
} }
return $budget; return $budget;
@ -43,7 +45,7 @@ final class Population
/** /**
* @param list<BuildingState> $buildings * @param list<BuildingState> $buildings
* @param float $soldierUpkeep food/h eaten by soldiers and stationed support * @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 * @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 public function workforce(array $buildings, float $soldierUpkeep = 0.0, int $militia = 0): Workforce
@ -81,8 +83,7 @@ final class Population
{ {
$groups = []; $groups = [];
foreach ($buildings as $b) { foreach ($buildings as $b) {
$isFarm = ($this->rules->output($b->type, $b->level)['resource'] ?? null) === 'food'; $groups[($this->rules->isFarm($b->type) ? 'a' : 'b').str_pad((string) (99 - $b->priority), 3, '0', STR_PAD_LEFT)][] = $b->id;
$groups[($isFarm ? 'a' : 'b').str_pad((string) (99 - $b->priority), 3, '0', STR_PAD_LEFT)][] = $b->id;
} }
ksort($groups); ksort($groups);

View File

@ -46,7 +46,8 @@ final class Production
/** /**
* Net change per hour, with the village role's bonus on its buildings and copies at * Net change per hour, with the village role's bonus on its buildings and copies at
* their instance share. Food is farm output minus upkeep, so it can go negative. * their instance share. Food is farm output minus upkeep, so it can go negative. The
* upkeep is in heads, and each head eats world speed times one food per hour.
* *
* @param list<BuildingState> $buildings * @param list<BuildingState> $buildings
* @return array{wood: float, clay: float, iron: float, food: float} * @return array{wood: float, clay: float, iron: float, food: float}
@ -64,7 +65,7 @@ final class Production
$rates[$output['resource']] += $output['per_hour']; $rates[$output['resource']] += $output['per_hour'];
} }
} }
$rates['food'] -= $workforce->upkeep; $rates['food'] -= $workforce->upkeep * $this->config->speed();
return $rates; return $rates;
} }

View File

@ -2,7 +2,10 @@
namespace App\Game\Economy; namespace App\Game\Economy;
/** Who works where in a village, as computed by Population::workforce(). */ /**
* Who works where in a village, as computed by Population::workforce(). The budget and
* the upkeep are heads (food per hour at world speed 1), not the food rate.
*/
final class Workforce final class Workforce
{ {
/** /**
@ -10,8 +13,10 @@ final class Workforce
* @param array<int, int> $required building id => villagers it needs * @param array<int, int> $required building id => villagers it needs
*/ */
public function __construct( public function __construct(
/** Heads the farms feed, nominally. */
public readonly float $foodBudget, public readonly float $foodBudget,
public readonly int $villagers, public readonly int $villagers,
/** Heads that eat: villagers, soldiers, stationed support, units in training, the wounded. */
public readonly float $upkeep, public readonly float $upkeep,
public readonly array $assigned, public readonly array $assigned,
public readonly array $required, public readonly array $required,

View File

@ -50,6 +50,8 @@ final class VillagePresenter
'resources_at' => self::iso($village->resources_at), 'resources_at' => self::iso($village->resources_at),
'rates' => array_map(fn (float $n) => round($n, 4), $sim->rates()), 'rates' => array_map(fn (float $n) => round($n, 4), $sim->rates()),
'storage_cap' => round($sim->storageCap()), 'storage_cap' => round($sim->storageCap()),
// Heads (food per hour at world speed 1), the same on every world speed; the
// food rate at world speed is rates.food.
'population' => [ 'population' => [
'food_budget' => round($workforce->foodBudget, 2), 'food_budget' => round($workforce->foodBudget, 2),
'upkeep' => round($workforce->upkeep, 2), 'upkeep' => round($workforce->upkeep, 2),