253 lines
10 KiB
PHP
253 lines
10 KiB
PHP
<?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),
|
|
...self::questsReachable($config, $rules),
|
|
];
|
|
}
|
|
|
|
/** Counter keys the actions bump (App\Engine\Quests::bump), besides recruited.<unit>. */
|
|
public const QUEST_COUNTERS = [
|
|
'attacks.sent', 'attacks.barbarian', 'support.sent', 'scouts.sent', 'village.renamed',
|
|
'priority.set', 'levy.called', 'map.viewed', 'reports.read',
|
|
];
|
|
|
|
/**
|
|
* Every quest must be completable, or its line stops there for good: known buildings
|
|
* at levels that exist, instances that fit the largest village, counters some action
|
|
* bumps, and each key once.
|
|
*/
|
|
private static function questsReachable(WorldConfig $config, BuildingRules $rules): array
|
|
{
|
|
$units = (new UnitRules($config))->units();
|
|
$start = $rules->spaceUsed($config->get('world.village.start_buildings', []));
|
|
$hqMax = $config->hasBuilding('headquarters') ? $rules->maxLevel('headquarters') : 1;
|
|
$spaceMax = $rules->spaceMax((int) $config->get('world.village.space'), $hqMax);
|
|
$seen = [];
|
|
$problems = [];
|
|
foreach ($config->get('quests.lines', []) as $line => $quests) {
|
|
foreach ($quests as $key => $quest) {
|
|
if (isset($seen[$key])) {
|
|
$problems[] = "Quest [$key] appears twice.";
|
|
}
|
|
$seen[$key] = true;
|
|
foreach ($quest['goals'] as [$kind, $subject, $need]) {
|
|
$problem = match (true) {
|
|
$kind === 'counter' => in_array($subject, self::QUEST_COUNTERS, true)
|
|
|| (str_starts_with($subject, 'recruited.') && in_array(substr($subject, 10), $units, true))
|
|
? null : "counts [$subject], which no action bumps",
|
|
! in_array($kind, ['building', 'instances'], true) => "has an unknown goal [$kind]",
|
|
! $config->hasBuilding($subject) => "needs unknown building [$subject]",
|
|
$kind === 'building' && $need > $rules->maxLevel($subject) => "needs [$subject] level $need, above its maximum",
|
|
$kind === 'instances' && (($rules->maxPerVillage($subject) ?? $need) < $need || $start + $need * $rules->space($subject) > $spaceMax) => "needs $need [$subject] in one village, which never fit",
|
|
default => null,
|
|
};
|
|
if ($problem) {
|
|
$problems[] = "Quest [$line.$key] $problem.";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $problems;
|
|
}
|
|
|
|
/** 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
|
|
{
|
|
// The headquarters starts at level 1, so the start village has the base space.
|
|
$max = $config->get('world.village.space');
|
|
if ($config->get('world.village.space_per_hq_level', 0) < 0) {
|
|
return ['[world.village.space_per_hq_level] cannot be negative, or upgrading the headquarters would squeeze the village.'];
|
|
}
|
|
$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, 1)['resource'] ?? null) !== 'food') {
|
|
continue;
|
|
}
|
|
for ($level = 1; $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, 1) > 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 = 1; $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 = 1; $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;
|
|
}
|
|
}
|