diff --git a/database/migrations/2026_11_03_180000_farms_feed_their_heads.php b/database/migrations/2026_11_03_180000_farms_feed_their_heads.php index daf1023..f7066f5 100644 --- a/database/migrations/2026_11_03_180000_farms_feed_their_heads.php +++ b/database/migrations/2026_11_03_180000_farms_feed_their_heads.php @@ -13,12 +13,14 @@ return new class extends Migration { // Population counts heads now, the same on every world speed (architecture.md, // "Population"). Before, farms fed world speed times as many, so a village on a fast - // world could hold more heads than its farms feed today. Each such player village gets - // its highest farm raised to the lowest level that feeds its soldiers where they eat - // (at home, stationed support, training, wounded) and the workers its buildings need, - // at most the farm's max level. Orders queued on that farm no longer fit and are - // cancelled with a full refund, as after a catapult hit. Points follow the new level. - // A village that fits already is left alone, so running this again changes nothing. + // world could hold more heads than its farms feed today. Each player village whose + // soldiers where they eat (at home, stationed support, training, wounded) and the + // workers its buildings need fitted the old budget but not the new one gets its highest + // farm raised to the lowest level that feeds them, at most the farm's max level. Orders + // queued on that farm no longer fit and are cancelled with a full refund, as after a + // catapult hit. Points follow the new level. A village that fits, or that was short + // under the old rule already (on world speed 1 the rules agree), is left alone, so + // running this again changes nothing. public function up(): void { // Test databases start empty. @@ -60,7 +62,16 @@ return new class extends Migration if (! $farm) { return null; } - $level = (new Population($sim->config()))->lowestFarmLevel($sim->states(), $farm->id, $sim->soldierUpkeep()); + $config = $sim->config(); + $soldiers = $sim->soldierUpkeep(); + $workforce = (new Population($config))->workforce($sim->states(), $soldiers); + $workers = array_sum($workforce->required); + // The old rule fed world speed times the heads. + $old = (int) floor(($workforce->foodBudget * $config->speed() * $config->get('economy.villager_share', 0.8) - $soldiers) / $config->get('economy.upkeep.villager', 1)); + if ($workforce->villagers >= $workers || $old < $workers) { + return null; + } + $level = (new Population($config))->lowestFarmLevel($sim->states(), $farm->id, $soldiers); if ($level <= $farm->level) { return null; } diff --git a/docs/architecture.md b/docs/architecture.md index 11ffb55..8e0897c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -60,7 +60,7 @@ Population counts in heads. A head is one food per hour at world speed 1. Farms - **Upkeep**: every villager takes `upkeep.villager` heads (1), and soldiers take their unit's `upkeep` (for example spear 1, heavy cav 4, noble 10). Foreign support stationed in the village counts against the host's budget. - **Food rate**: `rates.food` is the farms' output at world speed and staffing, minus upkeep × world speed. The food stock, starvation and its desertion waves run at world speed like every other resource; the heads stay the same. - The village payload's `population` is in heads: `food_budget`, `upkeep`, `villagers`, `workers`, `idle` and `reserved` (see "Workers and staffing"); `soldiers` and `training` count units. `rates.food` is food per hour at world speed. -- Villages from before heads had their highest farm raised on deploy to the lowest level that feeds their soldiers and workers (migration `farms_feed_their_heads`); only the speed 100 dev world had such villages. +- Villages that fed their soldiers and workers under the old rule but not under heads had their highest farm raised on deploy to the lowest level that feeds them (migration `farms_feed_their_heads`); only the speed 100 dev world had such villages. - **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 villagers as militia for a fixed time (`economy.levy.duration`, divided by world speed), one levy at a time. The militia still eat but don't work, so their buildings run understaffed; recruiting can only train villagers who are not drafted. A `levy.ends` event at `levy_until` settles the village and sends them back to work; the player can also end the levy early. If villagers drop below the militia count (a farm torn down, soldiers trained), the militia shrink with them. diff --git a/tests/Feature/FarmsFeedHeadsTest.php b/tests/Feature/FarmsFeedHeadsTest.php index 07d0dbe..b2cbef9 100644 --- a/tests/Feature/FarmsFeedHeadsTest.php +++ b/tests/Feature/FarmsFeedHeadsTest.php @@ -73,8 +73,23 @@ it('changes nothing on a second run, and nothing at all on SQLite', function () }); it('raises a farm no higher than its max level', function () { + // Farm 10 fed 80% of 22,350 under the old rule; farm 30 feeds 15,509 heads today. $village = overgrown(); - $village->troops()->update(['count' => 100_000]); + $village->buildings()->where('type', 'farm')->update(['level' => 10]); + $village->troops()->update(['count' => 16_000]); expect(farmsMigration()->raiseFarms(now()->toImmutable()))->toBe([$village->id => 30]); }); + +it('leaves a village alone that was short of villagers under the old rule already', function () { + // On world speed 1 both rules agree: a level 30 camp next to farm 1 was understaffed before. + $short = startVillage(); + $short->buildings()->create(['type' => 'timber_camp', 'level' => 30]); + // On the dev world, 3000 spearmen outeat even the old farm 1's 2400. + $hungry = overgrown(); + $hungry->troops()->update(['count' => 3000]); + + expect(farmsMigration()->raiseFarms(now()->toImmutable()))->toBe([]) + ->and(farmOf($short))->toBe(1) + ->and(farmOf($hungry))->toBe(1); +});