diff --git a/app/Game/ConfigLint.php b/app/Game/ConfigLint.php index ddf60a8..98408a3 100644 --- a/app/Game/ConfigLint.php +++ b/app/Game/ConfigLint.php @@ -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 = []; diff --git a/app/Game/Economy/Population.php b/app/Game/Economy/Population.php index d1f8f16..a66c312 100644 --- a/app/Game/Economy/Population.php +++ b/app/Game/Economy/Population.php @@ -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) { diff --git a/config/game/economy.php b/config/game/economy.php index 977f100..aa7d2c4 100644 --- a/config/game/economy.php +++ b/config/game/economy.php @@ -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], diff --git a/docs/architecture.md b/docs/architecture.md index 5025294..df6c507 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -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. diff --git a/tests/Feature/BuildQueueTest.php b/tests/Feature/BuildQueueTest.php index 354766c..e346090 100644 --- a/tests/Feature/BuildQueueTest.php +++ b/tests/Feature/BuildQueueTest.php @@ -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); diff --git a/tests/Feature/JoinWorldTest.php b/tests/Feature/JoinWorldTest.php index 2196a68..b126d1a 100644 --- a/tests/Feature/JoinWorldTest.php +++ b/tests/Feature/JoinWorldTest.php @@ -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() diff --git a/tests/Unit/Game/ConfigLintTest.php b/tests/Unit/Game/ConfigLintTest.php index 8b15ae3..f40f8e1 100644 --- a/tests/Unit/Game/ConfigLintTest.php +++ b/tests/Unit/Game/ConfigLintTest.php @@ -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.']); +}); diff --git a/tests/Unit/Game/PopulationTest.php b/tests/Unit/Game/PopulationTest.php index 82c8584..245e36c 100644 --- a/tests/Unit/Game/PopulationTest.php +++ b/tests/Unit/Game/PopulationTest.php @@ -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 () { diff --git a/tests/Unit/Game/ProductionTest.php b/tests/Unit/Game/ProductionTest.php index cd71925..94b3892 100644 --- a/tests/Unit/Game/ProductionTest.php +++ b/tests/Unit/Game/ProductionTest.php @@ -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);