feat: villagers fill 80% of farm output, so food builds up

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-28 21:28:16 +01:00
parent 0aa8a3abfc
commit 3b131c204b
9 changed files with 46 additions and 23 deletions

View File

@ -17,6 +17,7 @@ final class ConfigLint
return [
...self::startVillageFits($config, $rules),
...self::foodCanBeStored($config),
...self::farmsFeedTheirWorkers($config, $rules),
...self::costsFitStorage($config, $rules),
...self::requirementsReachable($config, $rules),
@ -55,6 +56,16 @@ final class ConfigLint
}
/** Capacity is never 0, and each farm level's workers eat less than it makes. */
/** Villagers must leave a surplus, or food never builds up and any soldier starves the village. */
private static function foodCanBeStored(WorldConfig $config): array
{
$share = $config->get('economy.villager_share', 0.8);
return $share > 0 && $share < 1
? []
: ["economy.villager_share must be between 0 and 1 (is $share), so farms leave a surplus."];
}
private static function farmsFeedTheirWorkers(WorldConfig $config, BuildingRules $rules): array
{
$problems = [];

View File

@ -10,7 +10,8 @@ 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 the budget left after soldiers.
* 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.
*/
final class Population
{
@ -43,7 +44,8 @@ final class Population
{
$budget = $this->foodBudget($buildings);
$perVillager = $this->config->get('economy.upkeep.villager', 1);
$villagers = max(0, (int) floor(($budget - $soldierUpkeep) / $perVillager));
$share = $this->config->get('economy.villager_share', 0.8);
$villagers = max(0, (int) floor(($budget * $share - $soldierUpkeep) / $perVillager));
$required = [];
foreach ($buildings as $b) {

View File

@ -7,6 +7,10 @@ return [
// Food per hour each villager eats.
'upkeep' => ['villager' => 1],
// Villagers and soldiers fill this share of the farms' nominal output; the rest is
// surplus that goes into storage. Expensive units eat into it, down to starvation.
'villager_share' => 0.8,
// Output at staffing ratio r is e^(k·(r−1)): 100% full, about 10% empty.
'staffing' => ['k' => 2.3],

View File

@ -51,7 +51,7 @@ Rates are constant between events. Any change to a rate settles the village firs
- **Capacity** is a food budget in food/h: `nominal farm output + 24h average of food delivered to this village`. Nominal means the output at full staffing, which avoids a loop between farms, workers, and capacity (see "No locks"). Capacity is TW-like and instant: no growth timer.
- **Upkeep**: every villager eats `upkeep.villager` (for example 1 food/h), and soldiers eat per unit type (for example spear 1, heavy cav 4, noble 10). Foreign support stationed in the village counts against the host's budget.
- **Villagers** fill instantly up to whatever budget soldiers and stationed support leave free. Villagers are the workforce.
- **Villagers** fill instantly up to `economy.villager_share` (default 80%) of the budget, minus what soldiers and stationed support eat. Villagers are the workforce. The other 20% of the farms' output is surplus that goes into storage, so food builds up; expensive units eat into it, and a big enough army pushes the village into starvation.
- **Training a soldier** turns one villager into that unit and costs resources. The soldier's upkeep takes from the budget, so villagers shrink to match, the same as TW farm space.
- **Levy**: the player can draft workers as militia for a fixed time (world config). Their buildings run understaffed meanwhile, and the militia return to work automatically when the time ends.

View File

@ -15,7 +15,7 @@ it('settles resources by rate and elapsed time, up to the storage cap', function
$village = startVillage();
$this->travel(2)->hours();
expect(shown($village)['resources'])->toBe(['wood' => 510.0, 'clay' => 510.0, 'iron' => 510.0, 'food' => 300.0]);
expect(shown($village)['resources'])->toBe(['wood' => 510.0, 'clay' => 510.0, 'iron' => 510.0, 'food' => 312.0]);
$this->travel(200)->hours();
expect(shown($village)['resources']['wood'])->toBe(1000.0);
@ -165,14 +165,14 @@ it('keeps core buildings at level 0', function () {
});
it('moves workers to the buildings with the highest priority', function () {
// 30 villagers: the farm takes 2, the clay pit at priority 9 the other 28.
// 24 villagers: the farm takes 2, the clay pit at priority 9 the other 22.
$village = startVillage();
$village->buildings()->create(['type' => 'timber_camp', 'level' => 30]);
$clay = $village->buildings()->create(['type' => 'clay_pit', 'level' => 30]);
app(SetPriority::class)(owner($village), $village->id, $clay->id, 12);
$buildings = collect(shown($village)['buildings'])->keyBy('type');
expect($buildings['clay_pit']['workers'])->toBe(['assigned' => 28, 'required' => 60])
expect($buildings['clay_pit']['workers'])->toBe(['assigned' => 22, 'required' => 60])
->and($buildings['clay_pit']['priority'])->toBe(9)
->and($buildings['timber_camp']['workers']['assigned'])->toBe(0)
->and($buildings['farm']['workers']['assigned'])->toBe(2);

View File

@ -47,9 +47,9 @@ it('shows a start village to its owner only', function () {
expect($state['space'])->toBe(['used' => 17, 'max' => 50])
->and($state['continent'])->toBe('K55')
->and($state['rates'])->toBe(['wood' => 5.0, 'clay' => 5.0, 'iron' => 5.0, 'food' => 0.0])
->and($state['rates'])->toBe(['wood' => 5.0, 'clay' => 5.0, 'iron' => 5.0, 'food' => 6.0])
->and($state['storage_cap'])->toEqual(1000)
->and($state['population'])->toEqual(['food_budget' => 30, 'upkeep' => 30, 'villagers' => 30, 'workers' => 5, 'soldiers' => 0])
->and($state['population'])->toEqual(['food_budget' => 30, 'upkeep' => 24, 'villagers' => 24, 'workers' => 5, 'soldiers' => 0])
->and($state['builders'])->toBe(2)
->and(collect($state['buildable'])->firstWhere('type', 'headquarters')['blocked'])->toBe('max_per_village')
->and(collect($state['buildable'])->firstWhere('type', 'wall')['blocked'])->toBeNull()

View File

@ -44,3 +44,8 @@ it('reports a type that can never fit next to the start buildings', function ()
expect(ConfigLint::check($config))->toBe(['[headquarters] does not fit next to the start buildings.']);
});
it('reports a villager share that leaves no food surplus', function () {
expect(ConfigLint::check(gameConfig(['economy' => ['villager_share' => 1.0]])))
->toBe(['economy.villager_share must be between 0 and 1 (is 1), so farms leave a surplus.']);
});

View File

@ -2,24 +2,25 @@
use App\Game\Economy\Population;
it('feeds as many villagers as the farms make food at full staffing', function () {
it('feeds villagers from the share of farm output, leaving a surplus', function () {
$population = new Population(gameConfig());
$workforce = $population->workforce([building(1, 'farm'), building(2, 'farm', 1)]);
// Budget 30 + 35.1 = 65.1 food/h; villagers fill 80% of it.
expect($workforce->foodBudget)->toBe(30 + 30 * 1.17)
->and($workforce->villagers)->toBe(65)
->and($workforce->upkeep)->toEqual(65);
->and($workforce->villagers)->toBe(52)
->and($workforce->upkeep)->toEqual(52);
});
it('leaves soldiers upkeep out of the villagers budget', function () {
it('leaves soldiers upkeep out of the villagers share', function () {
$workforce = (new Population(gameConfig()))->workforce([building(1, 'farm')], soldierUpkeep: 10);
expect($workforce->villagers)->toBe(20)
->and($workforce->upkeep)->toEqual(30);
expect($workforce->villagers)->toBe(14)
->and($workforce->upkeep)->toEqual(24);
});
it('staffs farms first, then by priority', function () {
// Farm 0 makes 30 food, so 30 villagers: the farm takes 2, priority 9 the other 28.
// Farm 0 makes 30 food, so 24 villagers: the farm takes 2, priority 9 the other 22.
$buildings = [
building(1, 'timber_camp', 30, priority: 1),
building(2, 'clay_pit', 30, priority: 9),
@ -27,12 +28,12 @@ it('staffs farms first, then by priority', function () {
];
$workforce = (new Population(gameConfig()))->workforce($buildings);
expect($workforce->assigned)->toBe([1 => 0, 2 => 28, 3 => 2])
->and($workforce->workers())->toBe(30);
expect($workforce->assigned)->toBe([1 => 0, 2 => 22, 3 => 2])
->and($workforce->workers())->toBe(24);
});
it('splits villagers evenly between buildings of equal priority', function () {
// 30 villagers: farm takes 2, the two level-20 camps (19 each) share 28.
// 24 villagers: farm takes 2, the two level-20 camps (19 each) share 22.
$buildings = [
building(1, 'farm'),
building(2, 'timber_camp', 20),
@ -40,8 +41,8 @@ it('splits villagers evenly between buildings of equal priority', function () {
];
$workforce = (new Population(gameConfig()))->workforce($buildings);
expect($workforce->assigned)->toBe([1 => 2, 2 => 14, 3 => 14])
->and($workforce->ratio(2))->toBe(14 / 19);
expect($workforce->assigned)->toBe([1 => 2, 2 => 11, 3 => 11])
->and($workforce->ratio(2))->toBe(11 / 19);
});
it('counts buildings that need nobody as fully staffed', function () {

View File

@ -21,17 +21,17 @@ it('adds buildings to the base trickle and nets food against upkeep', function (
expect($rates['wood'])->toBe(5.0 + 30.0)
->and($rates['clay'])->toBe(5.0)
->and($rates['iron'])->toBe(5.0)
->and($rates['food'])->toBe(0.0);
->and($rates['food'])->toBe(30.0 - 24);
});
it('makes less in an understaffed building', function () {
$config = gameConfig();
// 30 villagers for a timber camp needing round(2 × 1.12^30) = 60.
// 24 villagers (80% of 30 food) for a timber camp needing round(2 × 1.12^30) = 60.
$buildings = [building(1, 'farm'), building(2, 'timber_camp', 30)];
$workforce = (new Population($config))->workforce($buildings);
$production = new Production($config);
$ratio = 28 / 60;
$ratio = 22 / 60;
expect($workforce->ratio(2))->toBe($ratio)
->and($production->output($buildings[1], $workforce)['per_hour'])
->toEqualWithDelta(30 * 1.163 ** 30 * exp(2.3 * ($ratio - 1)), 1e-6);