*/ public static function check(WorldConfig $config): array { $rules = new BuildingRules($config); return [ ...self::startVillageFits($config, $rules), ...self::farmsFeedTheirWorkers($config, $rules), ...self::costsFitStorage($config, $rules), ...self::requirementsReachable($config, $rules), ]; } /** * 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. */ 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. */ 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; } }