implement unlock dependencies
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -383,6 +384,11 @@ RecipesConfig ConfigLoader::loadRecipes(const std::string& path)
|
||||
{
|
||||
def.unlockAtStationLevel = static_cast<int>(*level);
|
||||
}
|
||||
if (mt.contains("unlock_requires"))
|
||||
{
|
||||
def.unlockRequires = requireStringArray(mt["unlock_requires"], file,
|
||||
elemPath + ".unlock_requires");
|
||||
}
|
||||
}
|
||||
|
||||
// inputs may be omitted (e.g. miner recipes). An empty array is fine.
|
||||
@@ -422,6 +428,10 @@ ShipsConfig ConfigLoader::loadShips(const std::string& path)
|
||||
ShipDef def;
|
||||
def.id = requireString(mt["id"], file, elemPath + ".id");
|
||||
def.unlockAtStationLevel = static_cast<int>(requireInt(mt["unlock_at_station_level"], file, elemPath + ".unlock_at_station_level"));
|
||||
if (mt.contains("unlock_requires"))
|
||||
{
|
||||
def.unlockRequires = requireStringArray(mt["unlock_requires"], file, elemPath + ".unlock_requires");
|
||||
}
|
||||
def.layout = requireStringArray(mt["layout"], file, elemPath + ".layout");
|
||||
|
||||
// Schematic
|
||||
@@ -573,6 +583,10 @@ ModulesConfig ConfigLoader::loadModules(const std::string& path)
|
||||
def.id = requireString(mt["id"], file, elemPath + ".id");
|
||||
def.unlockAtStationLevel = static_cast<int>(
|
||||
mt["unlock_at_station_level"].value_or<int64_t>(-1));
|
||||
if (mt.contains("unlock_requires"))
|
||||
{
|
||||
def.unlockRequires = requireStringArray(mt["unlock_requires"], file, elemPath + ".unlock_requires");
|
||||
}
|
||||
def.surfaceMask = requireStringArray(mt["surface_mask"], file, elemPath + ".surface_mask");
|
||||
def.productionTimeSeconds = requireDouble(
|
||||
mt["production_time_seconds"], file, elemPath + ".production_time_seconds");
|
||||
@@ -685,6 +699,67 @@ ModulesConfig ConfigLoader::loadModules(const std::string& path)
|
||||
return cfg;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// Throws if any id in requiredIds is not a valid explicitly-unlockable schematic.
|
||||
void checkUnlockRequires(const std::vector<std::string>& requiredIds,
|
||||
const std::unordered_set<std::string>& schematicIds,
|
||||
const std::string& file,
|
||||
const std::string& path)
|
||||
{
|
||||
for (const std::string& requiredId : requiredIds)
|
||||
{
|
||||
if (schematicIds.count(requiredId) == 0)
|
||||
{
|
||||
throw makeError(file, path,
|
||||
"references unknown schematic '" + requiredId + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validates that every id listed in an `unlock_requires` (REQ-LOCK-PREREQ)
|
||||
// resolves to an explicitly-unlockable schematic: a ship, a module, or an
|
||||
// assembler recipe that carries `unlock_at_station_level` (a recipe schematic,
|
||||
// per REQ-LOCK-EXPLICIT). An unresolved id is a config error surfaced at load.
|
||||
void validateUnlockRequires(const GameConfig& cfg)
|
||||
{
|
||||
std::unordered_set<std::string> schematicIds;
|
||||
for (const ShipDef& def : cfg.ships.ships)
|
||||
{
|
||||
schematicIds.insert(def.id);
|
||||
}
|
||||
for (const ModuleDef& def : cfg.modules.modules)
|
||||
{
|
||||
schematicIds.insert(def.id);
|
||||
}
|
||||
for (const RecipeDef& def : cfg.recipes.recipes)
|
||||
{
|
||||
if (def.building == BuildingType::Assembler && def.unlockAtStationLevel.has_value())
|
||||
{
|
||||
schematicIds.insert(def.id);
|
||||
}
|
||||
}
|
||||
|
||||
for (const ShipDef& def : cfg.ships.ships)
|
||||
{
|
||||
checkUnlockRequires(def.unlockRequires, schematicIds, "ships.toml",
|
||||
"ship '" + def.id + "'.unlock_requires");
|
||||
}
|
||||
for (const ModuleDef& def : cfg.modules.modules)
|
||||
{
|
||||
checkUnlockRequires(def.unlockRequires, schematicIds, "modules.toml",
|
||||
"module '" + def.id + "'.unlock_requires");
|
||||
}
|
||||
for (const RecipeDef& def : cfg.recipes.recipes)
|
||||
{
|
||||
checkUnlockRequires(def.unlockRequires, schematicIds, "recipes.toml",
|
||||
"recipe '" + def.id + "'.unlock_requires");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
GameConfig ConfigLoader::loadFromDirectory(const std::string& configDir)
|
||||
{
|
||||
GameConfig cfg;
|
||||
@@ -694,6 +769,7 @@ GameConfig ConfigLoader::loadFromDirectory(const std::string& configDir)
|
||||
cfg.ships = loadShips(configDir + "/ships.toml");
|
||||
cfg.stations = loadStations(configDir + "/stations.toml");
|
||||
cfg.modules = loadModules(configDir + "/modules.toml");
|
||||
validateUnlockRequires(cfg);
|
||||
cfg.threatCosts = computeThreatCostTable(cfg);
|
||||
return cfg;
|
||||
}
|
||||
|
||||
@@ -41,6 +41,9 @@ struct ModuleDef
|
||||
{
|
||||
std::string id;
|
||||
int unlockAtStationLevel;
|
||||
// Prerequisite schematic ids that must be explicitly unlocked before this
|
||||
// schematic can enter the drop pool (REQ-LOCK-PREREQ). Empty = none.
|
||||
std::vector<std::string> unlockRequires;
|
||||
std::vector<std::string> surfaceMask;
|
||||
std::vector<RecipeIngredient> materials;
|
||||
double productionTimeSeconds;
|
||||
|
||||
@@ -36,6 +36,10 @@ struct RecipeDef
|
||||
// at game start. >= 0 = locked; schematic enters drop pool at that station
|
||||
// level once the output item is implicitly unlocked (REQ-LOCK-EXPLICIT).
|
||||
std::optional<int> unlockAtStationLevel;
|
||||
// Assembler recipe schematics only. Prerequisite schematic ids that must be
|
||||
// explicitly unlocked before this schematic can enter the drop pool
|
||||
// (REQ-LOCK-PREREQ). Empty = none.
|
||||
std::vector<std::string> unlockRequires;
|
||||
};
|
||||
|
||||
struct RecipesConfig
|
||||
|
||||
@@ -36,6 +36,9 @@ struct ShipDef
|
||||
{
|
||||
std::string id;
|
||||
int unlockAtStationLevel;
|
||||
// Prerequisite schematic ids that must be explicitly unlocked before this
|
||||
// schematic can enter the drop pool (REQ-LOCK-PREREQ). Empty = none.
|
||||
std::vector<std::string> unlockRequires;
|
||||
std::vector<std::string> layout;
|
||||
|
||||
ShipSchematic schematic;
|
||||
|
||||
@@ -635,12 +635,14 @@ void Simulation::generateSchematicChoices(int destroyedStationLevel)
|
||||
{
|
||||
if (def.unlockAtStationLevel < 0 || def.unlockAtStationLevel > destroyedStationLevel) { continue; }
|
||||
if (m_schematicLevels.at(def.id).unlocked) { continue; }
|
||||
if (!prerequisitesSatisfied(def.unlockRequires)) { continue; }
|
||||
pool.push_back({def.id, DropType::Ship});
|
||||
}
|
||||
for (const ModuleDef& def : m_config.modules.modules)
|
||||
{
|
||||
if (def.unlockAtStationLevel < 0 || def.unlockAtStationLevel > destroyedStationLevel) { continue; }
|
||||
if (m_moduleSchematicLevels.at(def.id).unlocked) { continue; }
|
||||
if (!prerequisitesSatisfied(def.unlockRequires)) { continue; }
|
||||
pool.push_back({def.id, DropType::Module});
|
||||
}
|
||||
for (const RecipeDef& def : m_config.recipes.recipes)
|
||||
@@ -656,6 +658,7 @@ void Simulation::generateSchematicChoices(int destroyedStationLevel)
|
||||
if (m_unlockedItemIds.count(out.item) > 0) { outputUnlocked = true; break; }
|
||||
}
|
||||
if (!outputUnlocked) { continue; }
|
||||
if (!prerequisitesSatisfied(def.unlockRequires)) { continue; }
|
||||
pool.push_back({def.id, DropType::Recipe});
|
||||
}
|
||||
|
||||
@@ -808,6 +811,32 @@ std::set<std::string> Simulation::getUnlockedModuleSchematicIds() const
|
||||
return ids;
|
||||
}
|
||||
|
||||
bool Simulation::isSchematicIdExplicitlyUnlocked(const std::string& schematicId) const
|
||||
{
|
||||
// A prerequisite is satisfied only by an explicitly-unlocked schematic
|
||||
// (REQ-LOCK-PREREQ): a ship or module schematic marked unlocked, or a recipe
|
||||
// schematic in the explicit-unlock set. Cross-type ids are looked up with
|
||||
// find/count so an id from one namespace never throws against another.
|
||||
const std::map<std::string, SchematicState>::const_iterator shipIt =
|
||||
m_schematicLevels.find(schematicId);
|
||||
if (shipIt != m_schematicLevels.end() && shipIt->second.unlocked) { return true; }
|
||||
|
||||
const std::map<std::string, SchematicState>::const_iterator moduleIt =
|
||||
m_moduleSchematicLevels.find(schematicId);
|
||||
if (moduleIt != m_moduleSchematicLevels.end() && moduleIt->second.unlocked) { return true; }
|
||||
|
||||
return m_unlockedRecipeSchematicIds.count(schematicId) > 0;
|
||||
}
|
||||
|
||||
bool Simulation::prerequisitesSatisfied(const std::vector<std::string>& unlockRequires) const
|
||||
{
|
||||
for (const std::string& requiredId : unlockRequires)
|
||||
{
|
||||
if (!isSchematicIdExplicitlyUnlocked(requiredId)) { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Simulation::UnlockedSets Simulation::computeUnlockedSets(
|
||||
const std::set<std::string>& unlockedShipSchematicIds,
|
||||
const std::set<std::string>& unlockedModuleSchematicIds,
|
||||
|
||||
@@ -215,6 +215,12 @@ private:
|
||||
std::set<std::string> getUnlockedShipSchematicIds() const;
|
||||
std::set<std::string> getUnlockedModuleSchematicIds() const;
|
||||
|
||||
// True if schematicId names a currently explicitly-unlocked ship, module, or
|
||||
// assembler recipe schematic (REQ-LOCK-PREREQ satisfaction basis).
|
||||
bool isSchematicIdExplicitlyUnlocked(const std::string& schematicId) const;
|
||||
// True if every prerequisite in unlockRequires is explicitly unlocked (REQ-LOCK-PREREQ).
|
||||
bool prerequisitesSatisfied(const std::vector<std::string>& unlockRequires) const;
|
||||
|
||||
// Display names (deduplicated, alphabetical) of output items of recipes in
|
||||
// hypothetical.recipeIds that are not yet in m_unlockedRecipeIds.
|
||||
std::vector<std::string> computeNewlyUnlockedItemNames(const UnlockedSets& hypothetical) const;
|
||||
|
||||
Reference in New Issue
Block a user