104 lines
3.7 KiB
C++
104 lines
3.7 KiB
C++
#include "unitconfig.h"
|
|
#include "yamlparser.h"
|
|
#include <QDir>
|
|
#include <QFileInfo>
|
|
#include <QDebug>
|
|
|
|
UnitConfigManager::UnitConfigManager()
|
|
{
|
|
}
|
|
|
|
bool UnitConfigManager::loadFromDirectory(const QString &dirPath)
|
|
{
|
|
QDir dir(dirPath);
|
|
if (!dir.exists()) {
|
|
qWarning() << "Unit config directory does not exist:" << dirPath;
|
|
return false;
|
|
}
|
|
|
|
QStringList filters;
|
|
filters << "*.yaml" << "*.yml";
|
|
QFileInfoList fileList = dir.entryInfoList(filters, QDir::Files);
|
|
|
|
for (const QFileInfo &fileInfo : fileList) {
|
|
loadConfigFile(fileInfo.absoluteFilePath());
|
|
}
|
|
|
|
return !m_configs.isEmpty();
|
|
}
|
|
|
|
bool UnitConfigManager::loadConfigFile(const QString &filePath)
|
|
{
|
|
YamlNode root = YamlParser::parseFile(filePath);
|
|
if (root.children.isEmpty()) {
|
|
qWarning() << "Failed to parse unit config YAML:" << filePath;
|
|
return false;
|
|
}
|
|
|
|
UnitConfig cfg;
|
|
cfg.id = root.hasChild("id") ? root["id"].asString() : QFileInfo(filePath).baseName();
|
|
cfg.name = root.hasChild("name") ? root["name"].asString() : cfg.id;
|
|
cfg.category = stringToUnitCategory(root.hasChild("category") ? root["category"].asString() : "bouwen");
|
|
cfg.health = root.hasChild("health") ? root["health"].asInt(100) : 100;
|
|
cfg.movementSpeed = root.hasChild("movement_speed") ? root["movement_speed"].asFloat(2.0f) : 2.0f;
|
|
cfg.cost = root.hasChild("cost") ? root["cost"].asInt(20) : 20;
|
|
if (root.hasChild("labor_cost")) {
|
|
cfg.laborCost = root["labor_cost"].asInt(1000);
|
|
} else if (root.hasChild("production_cost")) {
|
|
cfg.laborCost = root["production_cost"].asInt(1000);
|
|
} else {
|
|
cfg.laborCost = cfg.cost * 20;
|
|
}
|
|
cfg.attackDamage = root.hasChild("attack_damage") ? root["attack_damage"].asInt(0) : 0;
|
|
cfg.attackRange = root.hasChild("attack_range") ? root["attack_range"].asInt(1) : 1;
|
|
cfg.buildCharges = root.hasChild("build_charges") ? root["build_charges"].asInt(-1) : -1;
|
|
cfg.infiniteCharges = root.hasChild("infinite_charges") ? root["infinite_charges"].asBool(cfg.buildCharges < 0) : (cfg.buildCharges < 0);
|
|
cfg.requiredTech = root.hasChild("required_tech") ? root["required_tech"].asString("") : "";
|
|
cfg.obsoleteTech = root.hasChild("obsolete_tech") ? root["obsolete_tech"].asString("") : "";
|
|
cfg.iconPath = root.hasChild("icon") ? root["icon"].asString() : "";
|
|
|
|
if (root.hasChild("domain")) {
|
|
cfg.movementDomain = stringToMovementDomain(root["domain"].asString());
|
|
} else if (root.hasChild("movement_domain")) {
|
|
cfg.movementDomain = stringToMovementDomain(root["movement_domain"].asString());
|
|
}
|
|
|
|
if (root.hasChild("impassable_terrains")) {
|
|
cfg.impassableTerrains = root["impassable_terrains"].listValues;
|
|
}
|
|
if (root.hasChild("passable_terrains")) {
|
|
cfg.passableTerrains = root["passable_terrains"].listValues;
|
|
}
|
|
|
|
if (root.hasChild("actions")) {
|
|
cfg.actions = root["actions"].listValues;
|
|
}
|
|
// Always ensure verplaatsen action is present
|
|
if (!cfg.actions.contains("verplaatsen")) {
|
|
cfg.actions.prepend("verplaatsen");
|
|
}
|
|
|
|
auto parseActionMap = [&](const QString &key) {
|
|
if (root.hasChild(key)) {
|
|
const YamlNode &sNode = root[key];
|
|
for (auto it = sNode.children.begin(); it != sNode.children.end(); ++it) {
|
|
cfg.actionSpeeds[it.key()] = it.value().asFloat(2.0f);
|
|
}
|
|
}
|
|
};
|
|
parseActionMap("action_durations");
|
|
parseActionMap("action_durations");
|
|
|
|
m_configs[cfg.id] = cfg;
|
|
return true;
|
|
}
|
|
|
|
const UnitConfig* UnitConfigManager::getConfig(const QString &id) const
|
|
{
|
|
auto it = m_configs.find(id);
|
|
if (it != m_configs.end()) {
|
|
return &it.value();
|
|
}
|
|
return nullptr;
|
|
}
|