Select Git revision
config.cpp 7.37 KiB
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <unordered_map>
#include <string>
#include <iostream>
#include <stdexcept> // Für die Ausnahmebehandlung
#include "unit.hpp" // Include für UnitId
#include "config.hpp"
namespace pt = boost::property_tree;
namespace advanced_wars
{
Config::Config(MatchupTabel_secondaryweapon& secWeapon, MatchupTabel_primaryweapon& primWeapon)
: secondary_weapon_damage(secWeapon), primary_weapon_damage(primWeapon)
{
// Initialisierung hier wenn nötig
}
void Config::loadFromXML(const char *filename)
{
pt::ptree tree;
pt::read_xml(filename, tree);
for (const auto &unit : tree.get_child("Units"))
{
if (unit.first == "Unit")
{
std::string unitKey = unit.second.get<std::string>("<xmlattr>.key");
UnitId unitId;
if (unitKey == "infantry")
{
unitId = UnitId::INFANTERY;
}
else if (unitKey == "Mech")
{
unitId = UnitId::MECHANIZED_INFANTERY;
}
else
{
continue; // Überspringt nicht unterstützte Einheiten
}
int cost = unit.second.get<int>("Cost");
int movementPoints = unit.second.get<int>("MovementPoints");
int ammo = unit.second.get<int>("Ammo");
int minRange = unit.second.get<int>("minRange", 0);
int maxRange = unit.second.get<int>("maxRange", 0);
unit_costs[unitId] = cost;
unit_movementPoints[unitId] = movementPoints;
unit_ammo[unitId] = ammo;
unit_minRange[unitId] = minRange;
unit_maxRange[unitId] = maxRange;
for (const auto &weapon : unit.second.get_child("Weapons"))
{
if (weapon.first == "PrimaryWeapon")
{
std::string weaponName = weapon.second.get<std::string>("<xmlattr>.name");
unit_primaryweapon[unitId] = weaponName;
}
else if (weapon.first == "SecondaryWeapon")
{
std::string weaponName = weapon.second.get<std::string>("<xmlattr>.name");
unit_secondaryweapon[unitId] = weaponName;
}
}
for (const auto &weapon : unit.second.get_child("Weapons"))