feat: a building level needs idle villagers for the workers it adds: queued orders reserve them, farms are exempt, build options and upgrade offers carry villagers and blocked villagers, lint proves one farm at max level staffs everything

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 25c5007d92
commit cc2acaa77d
7 changed files with 94 additions and 4 deletions

View File

@ -9,7 +9,10 @@ use App\Models\User;
use App\Models\Village; use App\Models\Village;
use Carbon\CarbonImmutable; use Carbon\CarbonImmutable;
/** Queue a new instance at level 1 if its building space is free. Pays up front. */ /**
* Queue a new instance at level 1 if its building space is free and idle villagers
* stand ready to work it. Pays up front.
*/
final class QueueBuild extends VillageAction final class QueueBuild extends VillageAction
{ {
public function __invoke(User $user, int $villageId, string $type): Village public function __invoke(User $user, int $villageId, string $type): Village
@ -29,6 +32,9 @@ final class QueueBuild extends VillageAction
if (! $sim->spaceFits($type)) { if (! $sim->spaceFits($type)) {
throw new GameRuleViolation('no_space', 'Not enough building space.'); throw new GameRuleViolation('no_space', 'Not enough building space.');
} }
if (! $sim->villagersFit($type, 1)) {
throw new GameRuleViolation('villagers', 'Not enough free villagers: build up the farm.');
}
$cost = $rules->cost($type, 1, $sim->instanceIndex($type)); $cost = $rules->cost($type, 1, $sim->instanceIndex($type));
$sim->pay($cost); $sim->pay($cost);

View File

@ -9,7 +9,10 @@ use App\Models\User;
use App\Models\Village; use App\Models\Village;
use Carbon\CarbonImmutable; use Carbon\CarbonImmutable;
/** Queue the next level after whatever is already queued for this instance. */ /**
* Queue the next level after whatever is already queued for this instance, if idle
* villagers stand ready for the workers it adds (farms need none).
*/
final class QueueUpgrade extends VillageAction final class QueueUpgrade extends VillageAction
{ {
public function __invoke(User $user, int $villageId, int $buildingId): Village public function __invoke(User $user, int $villageId, int $buildingId): Village
@ -17,6 +20,9 @@ final class QueueUpgrade extends VillageAction
return $this->mutate($user, $villageId, function (Simulation $sim, Village $village, CarbonImmutable $now) use ($buildingId) { return $this->mutate($user, $villageId, function (Simulation $sim, Village $village, CarbonImmutable $now) use ($buildingId) {
$building = $this->building($village, $buildingId); $building = $this->building($village, $buildingId);
$offer = $sim->upgradeOffer($building) ?? throw new GameRuleViolation('max_level', 'Already at max level.'); $offer = $sim->upgradeOffer($building) ?? throw new GameRuleViolation('max_level', 'Already at max level.');
if ($offer['blocked'] === 'villagers') {
throw new GameRuleViolation('villagers', 'Not enough free villagers: build up the farm.');
}
$sim->pay($offer['cost']); $sim->pay($offer['cost']);
$sim->enqueue([ $sim->enqueue([
'building_id' => $building->id, 'building_id' => $building->id,

View File

@ -317,6 +317,30 @@ final class Simulation
return $this->spaceUsed() + $this->rules->space($type) <= $this->spaceMax(); return $this->spaceUsed() + $this->rules->space($type) <= $this->spaceMax();
} }
/**
* Workers the queued orders add when they finish: every new instance and upgrade
* reserves its extra workers, the way a new instance reserves its space. A demolition
* frees its workers when it finishes.
*/
public function reservedWorkers(): int
{
return (int) $this->village->orders
->where('action', '!=', BuildOrder::DEMOLISH)
->sum(fn (BuildOrder $o) => $this->rules->extraWorkers($o->type, $o->target_level));
}
/** Villagers free for a new building level: not drafted, not working, not reserved by the queue. */
public function idleVillagers(): int
{
return $this->workforce()->idle($this->reservedWorkers());
}
/** Whether enough idle villagers stand ready for the workers this level adds (farms always). */
public function villagersFit(string $type, int $level): bool
{
return $this->rules->extraWorkers($type, $level) <= $this->idleVillagers();
}
/** The headquarters, and the last storage or farm, never go below level 1. */ /** The headquarters, and the last storage or farm, never go below level 1. */
public function isCore(Building $building): bool public function isCore(Building $building): bool
{ {
@ -350,7 +374,7 @@ final class Simulation
return $missing; return $missing;
} }
/** @return array{target_level: int, cost: array<string, int>, seconds: int, blocked: null}|null */ /** @return array{target_level: int, cost: array<string, int>, seconds: int, villagers: int, blocked: 'villagers'|null}|null */
public function upgradeOffer(Building $building): ?array public function upgradeOffer(Building $building): ?array
{ {
$current = $this->queuedLevel($building); $current = $this->queuedLevel($building);
@ -363,7 +387,9 @@ final class Simulation
'target_level' => $target, 'target_level' => $target,
'cost' => $this->rules->cost($building->type, $target, $this->instanceIndex($building->type, $building)), 'cost' => $this->rules->cost($building->type, $target, $this->instanceIndex($building->type, $building)),
'seconds' => $this->rules->buildSeconds($building->type, $target, $this->hqLevel(), $this->hqStaffing()), 'seconds' => $this->rules->buildSeconds($building->type, $target, $this->hqLevel(), $this->hqStaffing()),
'blocked' => null, // Idle villagers the level needs as extra workers, 0 for farms.
'villagers' => $this->rules->extraWorkers($building->type, $target),
'blocked' => $this->villagersFit($building->type, $target) ? null : 'villagers',
]; ];
} }
@ -391,6 +417,7 @@ final class Simulation
$max !== null && $this->instances($type) >= $max => 'max_per_village', $max !== null && $this->instances($type) >= $max => 'max_per_village',
$this->missingRequirements($type) !== [] => 'requires', $this->missingRequirements($type) !== [] => 'requires',
! $this->spaceFits($type) => 'no_space', ! $this->spaceFits($type) => 'no_space',
! $this->villagersFit($type, 1) => 'villagers',
default => null, default => null,
}; };
@ -402,6 +429,8 @@ final class Simulation
// A new instance ranks after all of its type, standing or queued. // A new instance ranks after all of its type, standing or queued.
'instance_share' => $this->rules->instanceShare($this->instances($type)), 'instance_share' => $this->rules->instanceShare($this->instances($type)),
'seconds' => $this->rules->buildSeconds($type, 1, $this->hqLevel(), $this->hqStaffing()), 'seconds' => $this->rules->buildSeconds($type, 1, $this->hqLevel(), $this->hqStaffing()),
// Idle villagers a new instance needs as workers, 0 for farms.
'villagers' => $this->rules->extraWorkers($type, 1),
'blocked' => $blocked, 'blocked' => $blocked,
// What is still missing, for the "not yet available" list. // What is still missing, for the "not yet available" list.
'requires' => $this->missingRequirements($type), 'requires' => $this->missingRequirements($type),

View File

@ -110,6 +110,21 @@ final class BuildingRules
return (int) round($workers['base'] * $workers['factor'] ** self::above($level)); return (int) round($workers['base'] * $workers['factor'] ** self::above($level));
} }
/**
* Idle villagers an order to reach $level needs: the workers it adds over the level
* below, all of level 1's for a new instance. Farms need none: they bring more people
* than they employ, and without that a full village could never grow.
*/
public function extraWorkers(string $type, int $level): int
{
if ($level < 1 || $this->isFarm($type)) {
return 0;
}
$below = $level > 1 ? $this->workers($type, $level - 1) : 0;
return max(0, $this->workers($type, $level) - $below);
}
/** /**
* Output per hour at full staffing and world speed, or null for non-producers. * Output per hour at full staffing and world speed, or null for non-producers.
* *

View File

@ -20,6 +20,7 @@ final class ConfigLint
...self::startVillageFits($config, $rules), ...self::startVillageFits($config, $rules),
...self::foodCanBeStored($config), ...self::foodCanBeStored($config),
...self::farmsFeedTheirWorkers($config, $rules), ...self::farmsFeedTheirWorkers($config, $rules),
...self::farmStaffsEverything($config, $rules),
...self::costsFitStorage($config, $rules), ...self::costsFitStorage($config, $rules),
...self::copiesPenalised($config), ...self::copiesPenalised($config),
...self::requirementsReachable($config, $rules), ...self::requirementsReachable($config, $rules),
@ -401,6 +402,26 @@ final class ConfigLint
return $problems; return $problems;
} }
/**
* A building level needs idle villagers, and farms are exempt. So one farm at max level
* must feed enough villagers, at the villager share and without soldiers, to work one
* of every building at max level, or some level could never be built.
*/
private static function farmStaffsEverything(WorldConfig $config, BuildingRules $rules): array
{
$farms = array_filter($config->buildingTypes(), fn (string $t) => $rules->isFarm($t));
if (! $farms) {
return ['No building type makes food, so a village has no villagers.'];
}
$feeds = max(array_map(fn (string $t) => $rules->feeds($t, $rules->maxLevel($t)), $farms));
$villagers = (int) floor($feeds * $config->get('economy.villager_share', 0.8) / $config->get('economy.upkeep.villager', 1));
$workers = array_sum(array_map(fn (string $t) => $rules->workers($t, $rules->maxLevel($t)), $config->buildingTypes()));
return $workers <= $villagers
? []
: ["One of every building at max level needs $workers workers, more than the $villagers villagers one farm at max level feeds."];
}
/** /**
* Every level's cost fits one storage at max level, and storage's next level * Every level's cost fits one storage at max level, and storage's next level
* always fits what its current level holds. * always fits what its current level holds.

View File

@ -35,6 +35,15 @@ final class Workforce
return array_sum($this->assigned); return array_sum($this->assigned);
} }
/**
* Villagers free to staff a new building level: not drafted, not needed by the
* buildings standing, not reserved by queued orders ($reserved). Never below 0.
*/
public function idle(int $reserved = 0): int
{
return max(0, $this->undrafted() - array_sum($this->required) - $reserved);
}
/** Staffing ratio 0..1; a building that needs nobody counts as fully staffed. */ /** Staffing ratio 0..1; a building that needs nobody counts as fully staffed. */
public function ratio(int $buildingId): float public function ratio(int $buildingId): float
{ {

View File

@ -57,6 +57,10 @@ final class VillagePresenter
'upkeep' => round($workforce->upkeep, 2), 'upkeep' => round($workforce->upkeep, 2),
'villagers' => $workforce->villagers, 'villagers' => $workforce->villagers,
'workers' => $workforce->workers(), 'workers' => $workforce->workers(),
// Villagers free for a new building level (not drafted, not needed by the
// buildings, not reserved by the queue), and what the queue reserved.
'idle' => $sim->idleVillagers(),
'reserved' => $sim->reservedWorkers(),
'soldiers' => array_sum($troops), 'soldiers' => array_sum($troops),
'training' => $village->recruits->sum(fn (RecruitOrder $o) => $o->count - $o->done), 'training' => $village->recruits->sum(fn (RecruitOrder $o) => $o->count - $o->done),
], ],