game-village-politics-backend/app/Game/ConfigLint.php

202 lines
7.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Game;
use App\Game\Buildings\BuildingRules;
use App\Game\Military\UnitRules;
/**
* Proves a world config has no locks (architecture.md, "No locks").
* Every check returns problems as sentences; an empty list means the config is safe.
*/
final class ConfigLint
{
/** @return list<string> */
public static function check(WorldConfig $config): array
{
$rules = new BuildingRules($config);
return [
...self::startVillageFits($config, $rules),
...self::foodCanBeStored($config),
...self::farmsFeedTheirWorkers($config, $rules),
...self::costsFitStorage($config, $rules),
...self::requirementsReachable($config, $rules),
...self::unitsTrainable($config, $rules),
...self::timersRun($config),
];
}
/** A levy has to end and desertion waves have to come apart in time, or the village stays stuck. */
private static function timersRun(WorldConfig $config): array
{
$problems = [];
foreach (['economy.levy.duration', 'economy.starvation.wave_every'] as $key) {
if ($config->get($key, 1) <= 0) {
$problems[] = "[$key] must be more than 0 seconds.";
}
}
if ($config->get('economy.starvation.grace', 0) < 0) {
$problems[] = '[economy.starvation.grace] cannot be negative.';
}
return $problems;
}
/**
* The start buildings must exist and fit the building space together, and every
* type must fit next to them, or it could never be built.
*/
private static function startVillageFits(WorldConfig $config, BuildingRules $rules): array
{
$max = $config->get('world.village.space');
$start = $config->get('world.village.start_buildings', []);
$problems = [];
foreach ($start as $type) {
if (! $config->hasBuilding($type)) {
$problems[] = "Start building [$type] is not a building type.";
}
}
if ($problems) {
return $problems;
}
$used = $rules->spaceUsed($start);
if ($used > $max) {
return ["The start buildings take $used building space, more than the village's $max."];
}
foreach ($config->buildingTypes() as $type) {
if ($used + $rules->space($type) > $max) {
$problems[] = "[$type] does not fit next to the start buildings.";
}
}
return $problems;
}
/** 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 = [];
$perVillager = $config->get('economy.upkeep.villager', 1);
foreach ($config->buildingTypes() as $type) {
if (($rules->output($type, 0)['resource'] ?? null) !== 'food') {
continue;
}
for ($level = 0; $level <= $rules->maxLevel($type); $level++) {
$output = $rules->output($type, $level)['per_hour'];
if ($output <= 0 || $rules->workers($type, $level) * $perVillager >= $output) {
$problems[] = "[$type] level $level feeds fewer people than it needs as workers.";
}
}
}
return $problems;
}
/**
* Every level's cost fits one storage at max level, and storage's next level
* always fits what its current level holds.
*/
private static function costsFitStorage(WorldConfig $config, BuildingRules $rules): array
{
$storages = array_filter($config->buildingTypes(), fn (string $t) => $rules->capacity($t, 0) > 0);
if (! $storages) {
return ['No building type provides storage.'];
}
$problems = [];
$maxCap = max(array_map(fn (string $t) => $rules->capacity($t, $rules->maxLevel($t)), $storages));
foreach ($config->buildingTypes() as $type) {
for ($level = 0; $level <= $rules->maxLevel($type); $level++) {
if (max($rules->cost($type, $level)) > $maxCap) {
$problems[] = "[$type] level $level costs more than the largest storage holds.";
}
}
}
foreach ($storages as $type) {
for ($level = 0; $level < $rules->maxLevel($type); $level++) {
if (max($rules->cost($type, $level + 1)) > $rules->capacity($type, $level)) {
$problems[] = "[$type] level ".($level + 1)." costs more than level $level holds.";
}
}
}
return $problems;
}
/** Requirements name real types and levels, and have no cycles. */
/** Every unit has a real building to train at, reachable requirements, and a cost storage can hold. */
private static function unitsTrainable(WorldConfig $config, BuildingRules $rules): array
{
$units = new UnitRules($config);
$maxCap = max(array_map(fn (string $t) => $rules->capacity($t, $rules->maxLevel($t)), $config->buildingTypes()));
$problems = [];
foreach ($units->units() as $unit) {
if (! $config->hasBuilding($units->building($unit))) {
$problems[] = "[$unit] is trained at unknown building [{$units->building($unit)}].";
}
foreach ($units->requires($unit) as $type => $level) {
if (! $config->hasBuilding($type) || $level > $rules->maxLevel($type)) {
$problems[] = "[$unit] requires [$type] level $level, which can't be built.";
}
}
if (max($units->cost($unit)) > $maxCap) {
$problems[] = "[$unit] costs more than the largest storage holds.";
}
if ($units->upkeep($unit) <= 0) {
$problems[] = "[$unit] must eat food (upkeep > 0), or armies grow without limit.";
}
}
return $problems;
}
private static function requirementsReachable(WorldConfig $config, BuildingRules $rules): array
{
$problems = [];
foreach ($config->buildingTypes() as $type) {
foreach ($rules->requires($type) as $needed => $level) {
if (! $config->hasBuilding($needed)) {
$problems[] = "[$type] requires unknown type [$needed].";
} elseif ($level > $rules->maxLevel($needed)) {
$problems[] = "[$type] requires [$needed] level $level, above its max level.";
}
}
}
$visiting = [];
$done = [];
$visit = function (string $type) use (&$visit, &$visiting, &$done, &$problems, $config, $rules): void {
if (isset($done[$type]) || ! $config->hasBuilding($type)) {
return;
}
if (isset($visiting[$type])) {
$problems[] = "Requirement cycle through [$type].";
return;
}
$visiting[$type] = true;
foreach (array_keys($rules->requires($type)) as $needed) {
$visit($needed);
}
unset($visiting[$type]);
$done[$type] = true;
};
foreach ($config->buildingTypes() as $type) {
$visit($type);
}
return $problems;
}
}