85 lines
3.9 KiB
C++
85 lines
3.9 KiB
C++
#include "buildingconfig.h"
|
|
#include "yamlparser.h"
|
|
#include <QFileInfo>
|
|
#include <QDebug>
|
|
|
|
BuildingConfigManager::BuildingConfigManager()
|
|
{
|
|
}
|
|
|
|
bool BuildingConfigManager::loadConfigFile(const QString &filePath)
|
|
{
|
|
YamlNode root = YamlParser::parseFile(filePath);
|
|
if (root.children.isEmpty()) {
|
|
qWarning() << "Failed to parse buildings config YAML:" << filePath;
|
|
return false;
|
|
}
|
|
|
|
// Parse districts
|
|
if (root.hasChild("districts")) {
|
|
const YamlNode &districtsNode = root["districts"];
|
|
for (auto it = districtsNode.children.begin(); it != districtsNode.children.end(); ++it) {
|
|
DistrictConfig dCfg;
|
|
dCfg.id = it.value().hasChild("id") ? it.value()["id"].asString() : it.key();
|
|
dCfg.name = it.value().hasChild("name") ? it.value()["name"].asString() : dCfg.id;
|
|
dCfg.maxBuildings = it.value().hasChild("max_buildings") ? it.value()["max_buildings"].asInt(10) : 10;
|
|
dCfg.description = it.value().hasChild("description") ? it.value()["description"].asString() : "";
|
|
m_districts[dCfg.id] = dCfg;
|
|
}
|
|
}
|
|
|
|
// Ensure default center district exists
|
|
if (!m_districts.contains("center")) {
|
|
DistrictConfig defaultCenter;
|
|
defaultCenter.id = "center";
|
|
defaultCenter.name = "City Center";
|
|
defaultCenter.maxBuildings = 10;
|
|
m_districts["center"] = defaultCenter;
|
|
}
|
|
|
|
// Parse buildings
|
|
if (root.hasChild("buildings")) {
|
|
const YamlNode &buildingsNode = root["buildings"];
|
|
for (auto it = buildingsNode.children.begin(); it != buildingsNode.children.end(); ++it) {
|
|
BuildingConfig bCfg;
|
|
bCfg.id = it.value().hasChild("id") ? it.value()["id"].asString() : it.key();
|
|
bCfg.name = it.value().hasChild("name") ? it.value()["name"].asString() : bCfg.id;
|
|
bCfg.district = it.value().hasChild("district") ? it.value()["district"].asString("center") : "center";
|
|
bCfg.laborCost = it.value().hasChild("labor_cost") ? it.value()["labor_cost"].asInt(1000) : 1000;
|
|
bCfg.foodModifier = it.value().hasChild("food_modifier") ? it.value()["food_modifier"].asFloat(0.0f) : 0.0f;
|
|
bCfg.foodBonus = it.value().hasChild("food_bonus") ? it.value()["food_bonus"].asInt(0) : 0;
|
|
bCfg.laborBonus = it.value().hasChild("labor_bonus") ? it.value()["labor_bonus"].asInt(0) : 0;
|
|
bCfg.cultureBonus = it.value().hasChild("culture_bonus") ? it.value()["culture_bonus"].asInt(0) : 0;
|
|
bCfg.scienceBonus = it.value().hasChild("science_bonus") ? it.value()["science_bonus"].asInt(0) : 0;
|
|
bCfg.faithBonus = it.value().hasChild("faith_bonus") ? it.value()["faith_bonus"].asInt(0) : 0;
|
|
bCfg.goldBonus = it.value().hasChild("gold_bonus") ? it.value()["gold_bonus"].asInt(0) : 0;
|
|
bCfg.requiredTech = it.value().hasChild("required_tech") ? it.value()["required_tech"].asString("") : "";
|
|
bCfg.obsoleteTech = it.value().hasChild("obsolete_tech") ? it.value()["obsolete_tech"].asString("") : "";
|
|
bCfg.iconPath = it.value().hasChild("icon") ? it.value()["icon"].asString() : "";
|
|
bCfg.description = it.value().hasChild("description") ? it.value()["description"].asString() : "";
|
|
m_buildings[bCfg.id] = bCfg;
|
|
}
|
|
}
|
|
|
|
qDebug() << "BuildingConfigManager loaded" << m_districts.size() << "districts and" << m_buildings.size() << "buildings from" << filePath;
|
|
return true;
|
|
}
|
|
|
|
const BuildingConfig* BuildingConfigManager::getBuilding(const QString &id) const
|
|
{
|
|
auto it = m_buildings.find(id);
|
|
if (it != m_buildings.end()) {
|
|
return &it.value();
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
const DistrictConfig* BuildingConfigManager::getDistrict(const QString &id) const
|
|
{
|
|
auto it = m_districts.find(id);
|
|
if (it != m_districts.end()) {
|
|
return &it.value();
|
|
}
|
|
return nullptr;
|
|
}
|