This commit is contained in:
cdemeyer-teachx
2025-11-12 11:34:51 +09:00
parent a0f7a0f799
commit a1c9a2e54d
17 changed files with 416 additions and 466 deletions

View File

@@ -33,35 +33,9 @@ struct Achievement
Achievement() = default; Achievement() = default;
}; };
// Achievements configuration manager // Achievements configuration functions
class AchievementsConfig { bool loadAchievementsFromXML(const std::string& filepath);
public: const Achievement* getAchievementById(uint16_t id);
static AchievementsConfig& getInstance() { const std::unordered_map<uint16_t, Achievement>& getAllAchievements();
static AchievementsConfig instance;
return instance;
}
bool loadFromXML(const std::string& filepath);
const Achievement* getAchievementById(int id) const;
const std::unordered_map<int, Achievement>& getAllAchievements() const { return m_achievements; }
private:
AchievementsConfig() = default;
~AchievementsConfig() = default;
AchievementsConfig(const AchievementsConfig&) = delete;
AchievementsConfig& operator=(const AchievementsConfig&) = delete;
std::unordered_map<int, Achievement> m_achievements;
// Helper methods for parsing
void parseAchievement(tinyxml2::XMLElement* achievementElement);
void parseRequirements(tinyxml2::XMLElement* achievementElement, Achievement& achievement);
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
};
// Helper functions for enum conversions
ActionType stringToActionType(const std::string& str);
} // namespace cursebreaker } // namespace cursebreaker

View File

@@ -7,7 +7,6 @@
#include <tinyxml2.h> #include <tinyxml2.h>
#include "constants.h" #include "constants.h"
#include "items.hpp" // for stringToSkillType
namespace cursebreaker { namespace cursebreaker {
@@ -34,32 +33,9 @@ struct Harvestable
Harvestable() = default; Harvestable() = default;
}; };
// Harvestables configuration manager // Harvestables configuration functions
class HarvestablesConfig { bool loadHarvestablesFromXML(const std::string& filepath);
public: const Harvestable* getHarvestableById(uint16_t id);
static HarvestablesConfig& getInstance() { const std::unordered_map<uint16_t, Harvestable>& getAllHarvestables();
static HarvestablesConfig instance;
return instance;
}
bool loadFromXML(const std::string& filepath);
const Harvestable* getHarvestableById(int id) const;
const std::unordered_map<int, Harvestable>& getAllHarvestables() const { return m_harvestables; }
private:
HarvestablesConfig() = default;
~HarvestablesConfig() = default;
HarvestablesConfig(const HarvestablesConfig&) = delete;
HarvestablesConfig& operator=(const HarvestablesConfig&) = delete;
std::unordered_map<int, Harvestable> m_harvestables;
// Helper methods for parsing
void parseHarvestable(tinyxml2::XMLElement* harvestableElement);
void parseLoot(tinyxml2::XMLElement* harvestableElement, Harvestable& harvestable);
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
};
} // namespace cursebreaker } // namespace cursebreaker

View File

@@ -62,42 +62,9 @@ struct Item {
Item() = default; Item() = default;
}; };
// Helper functions for enum conversions // Items configuration functions
StatType stringToStatType(const std::string& str); bool loadItemsFromXML(const std::string& filepath);
ItemCategory stringToItemCategory(const std::string& str); const Item* getItemById(uint16_t id);
ItemType stringToItemType(const std::string& str); const std::unordered_map<uint16_t, Item>& getAllItems();
Tool stringToTool(const std::string& str);
SkillType stringToSkillType(const std::string& str);
WorkbenchType stringToWorkbenchType(const std::string& str);
GenstatType stringToGenstatType(const std::string& str);
// Items configuration manager
class ItemsConfig {
public:
static ItemsConfig& getInstance() {
static ItemsConfig instance;
return instance;
}
bool loadFromXML(const std::string& filepath);
const Item* getItemById(int id) const;
const std::unordered_map<int, Item>& getAllItems() const { return m_items; }
private:
ItemsConfig() = default;
~ItemsConfig() = default;
ItemsConfig(const ItemsConfig&) = delete;
ItemsConfig& operator=(const ItemsConfig&) = delete;
std::unordered_map<int, Item> m_items;
// Helper methods for parsing
void parseItem(tinyxml2::XMLElement* itemElement);
void parseStats(tinyxml2::XMLElement* itemElement, Item& item);
void parseCraftings(tinyxml2::XMLElement* itemElement, Item& item);
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
};
} // namespace cursebreaker } // namespace cursebreaker

View File

@@ -30,32 +30,9 @@ struct LootTable
LootTable() = default; LootTable() = default;
}; };
// Loot configuration manager // Loot configuration functions
class LootConfig { bool loadLootTablesFromXML(const std::string& filepath);
public: const LootTable* getLootTableById(uint16_t id);
static LootConfig& getInstance() { const std::unordered_map<uint16_t, LootTable>& getAllLootTables();
static LootConfig instance;
return instance;
}
bool loadFromXML(const std::string& filepath);
const LootTable* getLootTableById(int id) const;
const std::unordered_map<int, LootTable>& getAllLootTables() const { return m_lootTables; }
private:
LootConfig() = default;
~LootConfig() = default;
LootConfig(const LootConfig&) = delete;
LootConfig& operator=(const LootConfig&) = delete;
std::unordered_map<int, LootTable> m_lootTables;
// Helper methods for parsing
void parseLootTable(tinyxml2::XMLElement* lootTableElement);
void parseEntries(tinyxml2::XMLElement* lootTableElement, LootTable& lootTable);
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
};
} // namespace cursebreaker } // namespace cursebreaker

View File

@@ -7,7 +7,6 @@
#include <tinyxml2.h> #include <tinyxml2.h>
#include "constants.h" #include "constants.h"
#include "items.hpp" // for stringToStatType
namespace cursebreaker { namespace cursebreaker {
@@ -36,33 +35,9 @@ struct NPC
NPC() = default; NPC() = default;
}; };
// NPCs configuration manager // NPCs configuration functions
class NPCsConfig { bool loadNPCsFromXML(const std::string& filepath);
public: const NPC* getNPCById(uint16_t id);
static NPCsConfig& getInstance() { const std::unordered_map<uint16_t, NPC>& getAllNPCs();
static NPCsConfig instance;
return instance;
}
bool loadFromXML(const std::string& filepath);
const NPC* getNPCById(int id) const;
const std::unordered_map<int, NPC>& getAllNPCs() const { return m_npcs; }
private:
NPCsConfig() = default;
~NPCsConfig() = default;
NPCsConfig(const NPCsConfig&) = delete;
NPCsConfig& operator=(const NPCsConfig&) = delete;
std::unordered_map<int, NPC> m_npcs;
// Helper methods for parsing
void parseNPC(tinyxml2::XMLElement* npcElement);
void parseStats(tinyxml2::XMLElement* npcElement, NPC& npc);
void parseLoot(tinyxml2::XMLElement* npcElement, NPC& npc);
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
};
} // namespace cursebreaker } // namespace cursebreaker

View File

@@ -30,32 +30,9 @@ struct Shop
Shop() = default; Shop() = default;
}; };
// Shops configuration manager // Shops configuration functions
class ShopsConfig { bool loadShopsFromXML(const std::string& filepath);
public: const Shop* getShopById(uint16_t id);
static ShopsConfig& getInstance() { const std::unordered_map<uint16_t, Shop>& getAllShops();
static ShopsConfig instance;
return instance;
}
bool loadFromXML(const std::string& filepath);
const Shop* getShopById(int id) const;
const std::unordered_map<int, Shop>& getAllShops() const { return m_shops; }
private:
ShopsConfig() = default;
~ShopsConfig() = default;
ShopsConfig(const ShopsConfig&) = delete;
ShopsConfig& operator=(const ShopsConfig&) = delete;
std::unordered_map<int, Shop> m_shops;
// Helper methods for parsing
void parseShop(tinyxml2::XMLElement* shopElement);
void parseInventory(tinyxml2::XMLElement* shopElement, Shop& shop);
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
};
} // namespace cursebreaker } // namespace cursebreaker

20
include/helpers.hpp Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <string>
#include <unordered_map>
#include "configs/constants.h"
namespace cursebreaker {
// Helper functions for enum conversions
StatType stringToStatType(const std::string& str);
ItemCategory stringToItemCategory(const std::string& str);
ItemType stringToItemType(const std::string& str);
Tool stringToTool(const std::string& str);
SkillType stringToSkillType(const std::string& str);
WorkbenchType stringToWorkbenchType(const std::string& str);
GenstatType stringToGenstatType(const std::string& str);
ActionType stringToActionType(const std::string& str);
} // namespace cursebreaker

View File

@@ -8,6 +8,12 @@
#include "assets/asset_base.hpp" #include "assets/asset_base.hpp"
#include "assets/scene.hpp" #include "assets/scene.hpp"
#include "configs/items.hpp"
#include "configs/loot.hpp"
#include "configs/harvestables.hpp"
#include "configs/npcs.hpp"
#include "configs/shops.hpp"
#include "configs/achievements.hpp"
struct AssetPath struct AssetPath
{ {
@@ -15,6 +21,8 @@ struct AssetPath
AssetGUID GUID{}; AssetGUID GUID{};
}; };
namespace cursebreaker {
struct ParsedProject struct ParsedProject
{ {
std::filesystem::path m_projectRoot; std::filesystem::path m_projectRoot;
@@ -31,8 +39,16 @@ struct ParsedProject
std::unordered_map<AssetGUID, PrefabAsset*> m_prefabsMap; std::unordered_map<AssetGUID, PrefabAsset*> m_prefabsMap;
std::unordered_map<AssetGUID, uint32_t> m_scriptToClassHash; std::unordered_map<AssetGUID, uint32_t> m_scriptToClassHash;
std::unordered_map<uint16_t, Item> m_items;
std::unordered_map<uint16_t, LootTable> m_lootTables;
std::unordered_map<uint16_t, Harvestable> m_harvestables;
std::unordered_map<uint16_t, NPC> m_npcs;
std::unordered_map<uint16_t, Shop> m_shops;
std::unordered_map<uint16_t, Achievement> m_achievements;
}; };
void ParseProject(const std::filesystem::path& projectRoot); void ParseProject(const std::filesystem::path& projectRoot);
extern ParsedProject g_parsedProject; extern ParsedProject g_parsedProject;
} // namespace cursebreaker

View File

@@ -1,41 +1,18 @@
#include "configs/achievements.hpp" #include "configs/achievements.hpp"
#include "project_parser.h"
#include "helpers.hpp"
#include <iostream> #include <iostream>
#include <unordered_map>
namespace cursebreaker { namespace cursebreaker {
ActionType stringToActionType(const std::string& str) { // Static helper functions
static const std::unordered_map<std::string, ActionType> actionMap = { static std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
{"NpcDeath", ActionType::NpcDeath}, static int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
{"PlayerDeath", ActionType::PlayerDeath}, static bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
{"PlayerRespawn", ActionType::PlayerRespawn}, static void parseAchievement(tinyxml2::XMLElement* achievementElement);
{"NpcInteract", ActionType::NpcInteract}, static void parseRequirements(tinyxml2::XMLElement* achievementElement, Achievement& achievement);
{"QuestUpdate", ActionType::QuestUpdate},
{"QuestTimerEnd", ActionType::QuestTimerEnd},
{"UseItemOnItem", ActionType::UseItemOnItem},
{"UseItemOnNpc", ActionType::UseItemOnNpc},
{"ConsumeItem", ActionType::ConsumeItem},
{"NpcCombatInteract", ActionType::NpcCombatInteract},
{"NpcKilledByPlayer", ActionType::NpcKilledByPlayer},
{"EnterMap", ActionType::EnterMap},
{"VarUpdated", ActionType::VarUpdated},
{"GrantAchievement", ActionType::GrantAchievement},
{"PlayerKilledByNpc", ActionType::PlayerKilledByNpc},
{"UseItem", ActionType::UseItem},
{"CraftItem", ActionType::CraftItem},
{"HarvestItem", ActionType::HarvestItem},
{"BuyItem", ActionType::BuyItem},
{"NpcTagKilledByPlayer", ActionType::NpcTagKilledByPlayer},
{"UseAbility", ActionType::UseAbility},
{"LootItem", ActionType::LootItem},
{"UseItemCategoryOnItem", ActionType::UseItemCategoryOnItem}
};
auto it = actionMap.find(str); bool loadAchievementsFromXML(const std::string& filepath) {
return (it != actionMap.end()) ? it->second : ActionType::none;
}
bool AchievementsConfig::loadFromXML(const std::string& filepath) {
tinyxml2::XMLDocument doc; tinyxml2::XMLDocument doc;
tinyxml2::XMLError result = doc.LoadFile(filepath.c_str()); tinyxml2::XMLError result = doc.LoadFile(filepath.c_str());
@@ -50,7 +27,7 @@ bool AchievementsConfig::loadFromXML(const std::string& filepath) {
return false; return false;
} }
m_achievements.clear(); g_parsedProject.m_achievements.clear();
// Parse all achievements // Parse all achievements
for (tinyxml2::XMLElement* achievementElement = root->FirstChildElement("achievement"); for (tinyxml2::XMLElement* achievementElement = root->FirstChildElement("achievement");
@@ -59,11 +36,11 @@ bool AchievementsConfig::loadFromXML(const std::string& filepath) {
parseAchievement(achievementElement); parseAchievement(achievementElement);
} }
std::cout << "Loaded " << m_achievements.size() << " achievements from XML" << std::endl; std::cout << "Loaded " << g_parsedProject.m_achievements.size() << " achievements from XML" << std::endl;
return true; return true;
} }
void AchievementsConfig::parseAchievement(tinyxml2::XMLElement* achievementElement) { void parseAchievement(tinyxml2::XMLElement* achievementElement) {
Achievement achievement; Achievement achievement;
// Parse basic attributes // Parse basic attributes
@@ -79,10 +56,10 @@ void AchievementsConfig::parseAchievement(tinyxml2::XMLElement* achievementEleme
// Parse child elements // Parse child elements
parseRequirements(achievementElement, achievement); parseRequirements(achievementElement, achievement);
m_achievements[achievement.id] = std::move(achievement); g_parsedProject.m_achievements[achievement.id] = std::move(achievement);
} }
void AchievementsConfig::parseRequirements(tinyxml2::XMLElement* achievementElement, Achievement& achievement) { void parseRequirements(tinyxml2::XMLElement* achievementElement, Achievement& achievement) {
for (tinyxml2::XMLElement* reqElement = achievementElement->FirstChildElement("requirement"); for (tinyxml2::XMLElement* reqElement = achievementElement->FirstChildElement("requirement");
reqElement != nullptr; reqElement != nullptr;
reqElement = reqElement->NextSiblingElement("requirement")) { reqElement = reqElement->NextSiblingElement("requirement")) {
@@ -97,23 +74,27 @@ void AchievementsConfig::parseRequirements(tinyxml2::XMLElement* achievementElem
} }
} }
const Achievement* AchievementsConfig::getAchievementById(int id) const { const Achievement* getAchievementById(uint16_t id) {
auto it = m_achievements.find(id); auto it = g_parsedProject.m_achievements.find(id);
return (it != m_achievements.end()) ? &it->second : nullptr; return (it != g_parsedProject.m_achievements.end()) ? &it->second : nullptr;
} }
std::string AchievementsConfig::getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) { const std::unordered_map<uint16_t, Achievement>& getAllAchievements() {
return g_parsedProject.m_achievements;
}
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) {
const char* value = element->Attribute(attribute); const char* value = element->Attribute(attribute);
return value ? std::string(value) : defaultValue; return value ? std::string(value) : defaultValue;
} }
int AchievementsConfig::getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) { int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) {
int value = defaultValue; int value = defaultValue;
element->QueryIntAttribute(attribute, &value); element->QueryIntAttribute(attribute, &value);
return value; return value;
} }
bool AchievementsConfig::getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) { bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) {
bool value = defaultValue; bool value = defaultValue;
element->QueryBoolAttribute(attribute, &value); element->QueryBoolAttribute(attribute, &value);
return value; return value;

View File

@@ -1,11 +1,20 @@
#include "configs/harvestables.hpp" #include "configs/harvestables.hpp"
#include "project_parser.h"
#include "helpers.hpp"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
namespace cursebreaker { namespace cursebreaker {
bool HarvestablesConfig::loadFromXML(const std::string& filepath) { // Static helper functions
static std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
static int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
static bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
static void parseHarvestable(tinyxml2::XMLElement* harvestableElement);
static void parseLoot(tinyxml2::XMLElement* harvestableElement, Harvestable& harvestable);
bool loadHarvestablesFromXML(const std::string& filepath) {
tinyxml2::XMLDocument doc; tinyxml2::XMLDocument doc;
tinyxml2::XMLError result = doc.LoadFile(filepath.c_str()); tinyxml2::XMLError result = doc.LoadFile(filepath.c_str());
@@ -20,7 +29,7 @@ bool HarvestablesConfig::loadFromXML(const std::string& filepath) {
return false; return false;
} }
m_harvestables.clear(); g_parsedProject.m_harvestables.clear();
// Parse all harvestables // Parse all harvestables
for (tinyxml2::XMLElement* harvestableElement = root->FirstChildElement("harvestable"); for (tinyxml2::XMLElement* harvestableElement = root->FirstChildElement("harvestable");
@@ -29,11 +38,11 @@ bool HarvestablesConfig::loadFromXML(const std::string& filepath) {
parseHarvestable(harvestableElement); parseHarvestable(harvestableElement);
} }
std::cout << "Loaded " << m_harvestables.size() << " harvestables from XML" << std::endl; std::cout << "Loaded " << g_parsedProject.m_harvestables.size() << " harvestables from XML" << std::endl;
return true; return true;
} }
void HarvestablesConfig::parseHarvestable(tinyxml2::XMLElement* harvestableElement) { void parseHarvestable(tinyxml2::XMLElement* harvestableElement) {
Harvestable harvestable; Harvestable harvestable;
// Parse basic attributes // Parse basic attributes
@@ -49,10 +58,10 @@ void HarvestablesConfig::parseHarvestable(tinyxml2::XMLElement* harvestableEleme
// Parse child elements // Parse child elements
parseLoot(harvestableElement, harvestable); parseLoot(harvestableElement, harvestable);
m_harvestables[harvestable.id] = std::move(harvestable); g_parsedProject.m_harvestables[harvestable.id] = std::move(harvestable);
} }
void HarvestablesConfig::parseLoot(tinyxml2::XMLElement* harvestableElement, Harvestable& harvestable) { void parseLoot(tinyxml2::XMLElement* harvestableElement, Harvestable& harvestable) {
for (tinyxml2::XMLElement* lootElement = harvestableElement->FirstChildElement("loot"); for (tinyxml2::XMLElement* lootElement = harvestableElement->FirstChildElement("loot");
lootElement != nullptr; lootElement != nullptr;
lootElement = lootElement->NextSiblingElement("loot")) { lootElement = lootElement->NextSiblingElement("loot")) {
@@ -66,23 +75,27 @@ void HarvestablesConfig::parseLoot(tinyxml2::XMLElement* harvestableElement, Har
} }
} }
const Harvestable* HarvestablesConfig::getHarvestableById(int id) const { const Harvestable* getHarvestableById(uint16_t id) {
auto it = m_harvestables.find(id); auto it = g_parsedProject.m_harvestables.find(id);
return (it != m_harvestables.end()) ? &it->second : nullptr; return (it != g_parsedProject.m_harvestables.end()) ? &it->second : nullptr;
} }
std::string HarvestablesConfig::getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) { const std::unordered_map<uint16_t, Harvestable>& getAllHarvestables() {
return g_parsedProject.m_harvestables;
}
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) {
const char* value = element->Attribute(attribute); const char* value = element->Attribute(attribute);
return value ? std::string(value) : defaultValue; return value ? std::string(value) : defaultValue;
} }
int HarvestablesConfig::getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) { int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) {
int value = defaultValue; int value = defaultValue;
element->QueryIntAttribute(attribute, &value); element->QueryIntAttribute(attribute, &value);
return value; return value;
} }
bool HarvestablesConfig::getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) { bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) {
bool value = defaultValue; bool value = defaultValue;
element->QueryBoolAttribute(attribute, &value); element->QueryBoolAttribute(attribute, &value);
return value; return value;

View File

@@ -1,4 +1,6 @@
#include "configs/items.hpp" #include "configs/items.hpp"
#include "project_parser.h"
#include "helpers.hpp"
#include <iostream> #include <iostream>
#include <unordered_map> #include <unordered_map>
#include <sstream> #include <sstream>
@@ -6,169 +8,15 @@
namespace cursebreaker { namespace cursebreaker {
// Helper functions to convert strings to enums // Static helper functions
StatType stringToStatType(const std::string& str) { static std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
static const std::unordered_map<std::string, StatType> statMap = { static int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
{"damagephysical", StatType::damagePhysical}, static bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
{"damagemagical", StatType::damageMagical}, static void parseItem(tinyxml2::XMLElement* itemElement);
{"damageranged", StatType::damageRanged}, static void parseStats(tinyxml2::XMLElement* itemElement, Item& item);
{"accuracyphysical", StatType::accuracyPhysical}, static void parseCraftings(tinyxml2::XMLElement* itemElement, Item& item);
{"accuracymagical", StatType::accuracyMagical},
{"accuracyranged", StatType::accuracyRanged},
{"resistancephysical", StatType::resistancePhysical},
{"resistancemagical", StatType::resistanceMagical},
{"resistanceranged", StatType::resistanceRanged},
{"health", StatType::health},
{"mana", StatType::mana},
{"manaregen", StatType::manaregen},
{"healthregen", StatType::healthregen},
{"movementSpeed", StatType::movementSpeed},
{"power", StatType::power},
{"critical", StatType::critical},
{"healing", StatType::healing},
{"DamageVsUndead", StatType::DamageVsUndead},
{"DamageVsBeasts", StatType::DamageVsBeasts},
{"CritterSlaying", StatType::CritterSlaying},
{"harvestingSpeedWoodcutting", StatType::harvestingSpeedWoodcutting}
};
auto it = statMap.find(str); bool loadItemsFromXML(const std::string& filepath) {
return (it != statMap.end()) ? it->second : StatType::none;
}
ItemCategory stringToItemCategory(const std::string& str) {
static const std::unordered_map<std::string, ItemCategory> categoryMap = {
{"bone", ItemCategory::bone},
{"bow", ItemCategory::bow},
{"crossbow", ItemCategory::crossbow},
{"constructable", ItemCategory::constructable},
{"torch", ItemCategory::torch},
{"blacksmithhammer", ItemCategory::blacksmithhammer},
{"questitem", ItemCategory::questitem},
{"heavyArmor", ItemCategory::heavyArmor},
{"warhammer", ItemCategory::warhammer},
{"shield", ItemCategory::shield},
{"hatchet", ItemCategory::hatchet},
{"blade", ItemCategory::blade},
{"armor", ItemCategory::armor},
{"pickaxe", ItemCategory::pickaxe},
{"fish", ItemCategory::fish},
{"fishingrod", ItemCategory::fishingrod},
{"shears", ItemCategory::shears},
{"hammer", ItemCategory::hammer},
{"battleaxe", ItemCategory::battleaxe},
{"morningstar", ItemCategory::morningstar},
{"wand", ItemCategory::wand},
{"staff", ItemCategory::staff},
{"dagger", ItemCategory::dagger}
};
auto it = categoryMap.find(str);
return (it != categoryMap.end()) ? it->second : ItemCategory::none;
}
ItemType stringToItemType(const std::string& str) {
static const std::unordered_map<std::string, ItemType> typeMap = {
{"weapon", ItemType::weapon},
{"shield", ItemType::shield},
{"armor", ItemType::armor},
{"head", ItemType::head},
{"resource", ItemType::resource},
{"consumable", ItemType::consumable},
{"trinket", ItemType::trinket},
{"bracelet", ItemType::bracelet}
};
auto it = typeMap.find(str);
return (it != typeMap.end()) ? it->second : ItemType::resource;
}
Tool stringToTool(const std::string& str) {
static const std::unordered_map<std::string, Tool> toolMap = {
{"hatchet", Tool::hatchet},
{"pickaxe", Tool::pickaxe},
{"broom", Tool::broom},
{"fishingrod", Tool::fishingrod}
};
auto it = toolMap.find(str);
return (it != toolMap.end()) ? it->second : Tool::none;
}
SkillType stringToSkillType(const std::string& str) {
static const std::unordered_map<std::string, SkillType> skillMap = {
{"woodcutting", SkillType::woodcutting},
{"fishing", SkillType::fishing},
{"swordsmanship", SkillType::swordsmanship},
{"mining", SkillType::mining},
{"archery", SkillType::archery},
{"magic", SkillType::magic},
{"defence", SkillType::defence},
{"blacksmithy", SkillType::blacksmithy},
{"tailoring", SkillType::tailoring},
{"carpentry", SkillType::carpentry},
{"alchemy", SkillType::alchemy},
{"cooking", SkillType::cooking}
};
auto it = skillMap.find(str);
return (it != skillMap.end()) ? it->second : SkillType::none;
}
WorkbenchType stringToWorkbenchType(const std::string& str) {
static const std::unordered_map<std::string, WorkbenchType> workbenchMap = {
{"none", WorkbenchType::none},
{"anvil", WorkbenchType::anvil},
{"oven", WorkbenchType::oven},
{"cooking", WorkbenchType::cooking},
{"carpenter", WorkbenchType::carpenter},
{"tailor", WorkbenchType::tailor},
{"forge", WorkbenchType::forge},
{"alchemist", WorkbenchType::alchemist},
{"mystic", WorkbenchType::mystic}
};
auto it = workbenchMap.find(str);
return (it != workbenchMap.end()) ? it->second : WorkbenchType::none;
}
GenstatType stringToGenstatType(const std::string& str) {
static const std::unordered_map<std::string, GenstatType> genstatMap = {
{"dagger", GenstatType::dagger},
{"broadsword", GenstatType::broadsword},
{"battleaxe", GenstatType::battleaxe},
{"greatsword", GenstatType::greatsword},
{"morningstar", GenstatType::morningstar},
{"hammer", GenstatType::hammer},
{"spear", GenstatType::spear},
{"bow", GenstatType::bow},
{"staff", GenstatType::staff},
{"wand", GenstatType::wand},
{"crossbow", GenstatType::crossbow},
{"woodenshield", GenstatType::woodenshield},
{"wizardhat", GenstatType::wizardhat},
{"wizardrobe", GenstatType::wizardrobe},
{"grandwizardhat", GenstatType::grandwizardhat},
{"grandwizardrobe", GenstatType::grandwizardrobe},
{"leatherhood", GenstatType::leatherhood},
{"leatherbracelet", GenstatType::leatherbracelet},
{"leatherarmor", GenstatType::leatherarmor},
{"studdedleatherhood", GenstatType::studdedleatherhood},
{"studdedleatherbracelet", GenstatType::studdedleatherbracelet},
{"studdedleatherarmor", GenstatType::studdedleatherarmor},
{"helmet", GenstatType::helmet},
{"shield", GenstatType::shield},
{"armor", GenstatType::armor},
{"platehelmet", GenstatType::platehelmet},
{"kiteshield", GenstatType::kiteshield},
{"platearmor", GenstatType::platearmor}
};
auto it = genstatMap.find(str);
return (it != genstatMap.end()) ? it->second : GenstatType::none;
}
bool ItemsConfig::loadFromXML(const std::string& filepath) {
tinyxml2::XMLDocument doc; tinyxml2::XMLDocument doc;
tinyxml2::XMLError result = doc.LoadFile(filepath.c_str()); tinyxml2::XMLError result = doc.LoadFile(filepath.c_str());
@@ -183,7 +31,7 @@ bool ItemsConfig::loadFromXML(const std::string& filepath) {
return false; return false;
} }
m_items.clear(); g_parsedProject.m_items.clear();
// Parse all items // Parse all items
for (tinyxml2::XMLElement* itemElement = root->FirstChildElement("item"); for (tinyxml2::XMLElement* itemElement = root->FirstChildElement("item");
@@ -192,11 +40,11 @@ bool ItemsConfig::loadFromXML(const std::string& filepath) {
parseItem(itemElement); parseItem(itemElement);
} }
std::cout << "Loaded " << m_items.size() << " items from XML" << std::endl; std::cout << "Loaded " << g_parsedProject.m_items.size() << " items from XML" << std::endl;
return true; return true;
} }
void ItemsConfig::parseItem(tinyxml2::XMLElement* itemElement) { void parseItem(tinyxml2::XMLElement* itemElement) {
Item item; Item item;
// Parse basic attributes // Parse basic attributes
@@ -231,10 +79,10 @@ void ItemsConfig::parseItem(tinyxml2::XMLElement* itemElement) {
parseStats(itemElement, item); parseStats(itemElement, item);
parseCraftings(itemElement, item); parseCraftings(itemElement, item);
m_items[item.id] = std::move(item); g_parsedProject.m_items[item.id] = std::move(item);
} }
void ItemsConfig::parseStats(tinyxml2::XMLElement* itemElement, Item& item) { void parseStats(tinyxml2::XMLElement* itemElement, Item& item) {
for (tinyxml2::XMLElement* statElement = itemElement->FirstChildElement("stat"); for (tinyxml2::XMLElement* statElement = itemElement->FirstChildElement("stat");
statElement != nullptr; statElement != nullptr;
statElement = statElement->NextSiblingElement("stat")) { statElement = statElement->NextSiblingElement("stat")) {
@@ -267,7 +115,7 @@ void ItemsConfig::parseStats(tinyxml2::XMLElement* itemElement, Item& item) {
} }
void ItemsConfig::parseCraftings(tinyxml2::XMLElement* itemElement, Item& item) { void parseCraftings(tinyxml2::XMLElement* itemElement, Item& item) {
for (tinyxml2::XMLElement* craftElement = itemElement->FirstChildElement("crafting"); for (tinyxml2::XMLElement* craftElement = itemElement->FirstChildElement("crafting");
craftElement != nullptr; craftElement != nullptr;
craftElement = craftElement->NextSiblingElement("crafting")) { craftElement = craftElement->NextSiblingElement("crafting")) {
@@ -309,23 +157,27 @@ void ItemsConfig::parseCraftings(tinyxml2::XMLElement* itemElement, Item& item)
} }
} }
const Item* ItemsConfig::getItemById(int id) const { const Item* getItemById(uint16_t id) {
auto it = m_items.find(id); auto it = g_parsedProject.m_items.find(id);
return (it != m_items.end()) ? &it->second : nullptr; return (it != g_parsedProject.m_items.end()) ? &it->second : nullptr;
} }
std::string ItemsConfig::getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) { const std::unordered_map<uint16_t, Item>& getAllItems() {
return g_parsedProject.m_items;
}
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) {
const char* value = element->Attribute(attribute); const char* value = element->Attribute(attribute);
return value ? std::string(value) : defaultValue; return value ? std::string(value) : defaultValue;
} }
int ItemsConfig::getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) { int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) {
int value = defaultValue; int value = defaultValue;
element->QueryIntAttribute(attribute, &value); element->QueryIntAttribute(attribute, &value);
return value; return value;
} }
bool ItemsConfig::getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) { bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) {
bool value = defaultValue; bool value = defaultValue;
element->QueryBoolAttribute(attribute, &value); element->QueryBoolAttribute(attribute, &value);
return value; return value;

View File

@@ -1,9 +1,17 @@
#include "configs/loot.hpp" #include "configs/loot.hpp"
#include "project_parser.h"
#include <iostream> #include <iostream>
namespace cursebreaker { namespace cursebreaker {
bool LootConfig::loadFromXML(const std::string& filepath) { // Static helper functions
static std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
static int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
static bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
static void parseLootTable(tinyxml2::XMLElement* lootTableElement);
static void parseEntries(tinyxml2::XMLElement* lootTableElement, LootTable& lootTable);
bool loadLootTablesFromXML(const std::string& filepath) {
tinyxml2::XMLDocument doc; tinyxml2::XMLDocument doc;
tinyxml2::XMLError result = doc.LoadFile(filepath.c_str()); tinyxml2::XMLError result = doc.LoadFile(filepath.c_str());
@@ -18,7 +26,7 @@ bool LootConfig::loadFromXML(const std::string& filepath) {
return false; return false;
} }
m_lootTables.clear(); g_parsedProject.m_lootTables.clear();
// Parse all loot tables // Parse all loot tables
for (tinyxml2::XMLElement* lootTableElement = root->FirstChildElement("lootTable"); for (tinyxml2::XMLElement* lootTableElement = root->FirstChildElement("lootTable");
@@ -27,11 +35,11 @@ bool LootConfig::loadFromXML(const std::string& filepath) {
parseLootTable(lootTableElement); parseLootTable(lootTableElement);
} }
std::cout << "Loaded " << m_lootTables.size() << " loot tables from XML" << std::endl; std::cout << "Loaded " << g_parsedProject.m_lootTables.size() << " loot tables from XML" << std::endl;
return true; return true;
} }
void LootConfig::parseLootTable(tinyxml2::XMLElement* lootTableElement) { void parseLootTable(tinyxml2::XMLElement* lootTableElement) {
LootTable lootTable; LootTable lootTable;
// Parse basic attributes // Parse basic attributes
@@ -42,10 +50,10 @@ void LootConfig::parseLootTable(tinyxml2::XMLElement* lootTableElement) {
// Parse child elements // Parse child elements
parseEntries(lootTableElement, lootTable); parseEntries(lootTableElement, lootTable);
m_lootTables[lootTable.id] = std::move(lootTable); g_parsedProject.m_lootTables[lootTable.id] = std::move(lootTable);
} }
void LootConfig::parseEntries(tinyxml2::XMLElement* lootTableElement, LootTable& lootTable) { void parseEntries(tinyxml2::XMLElement* lootTableElement, LootTable& lootTable) {
for (tinyxml2::XMLElement* entryElement = lootTableElement->FirstChildElement("entry"); for (tinyxml2::XMLElement* entryElement = lootTableElement->FirstChildElement("entry");
entryElement != nullptr; entryElement != nullptr;
entryElement = entryElement->NextSiblingElement("entry")) { entryElement = entryElement->NextSiblingElement("entry")) {
@@ -61,23 +69,27 @@ void LootConfig::parseEntries(tinyxml2::XMLElement* lootTableElement, LootTable&
} }
} }
const LootTable* LootConfig::getLootTableById(int id) const { const LootTable* getLootTableById(uint16_t id) {
auto it = m_lootTables.find(id); auto it = g_parsedProject.m_lootTables.find(id);
return (it != m_lootTables.end()) ? &it->second : nullptr; return (it != g_parsedProject.m_lootTables.end()) ? &it->second : nullptr;
} }
std::string LootConfig::getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) { const std::unordered_map<uint16_t, LootTable>& getAllLootTables() {
return g_parsedProject.m_lootTables;
}
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) {
const char* value = element->Attribute(attribute); const char* value = element->Attribute(attribute);
return value ? std::string(value) : defaultValue; return value ? std::string(value) : defaultValue;
} }
int LootConfig::getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) { int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) {
int value = defaultValue; int value = defaultValue;
element->QueryIntAttribute(attribute, &value); element->QueryIntAttribute(attribute, &value);
return value; return value;
} }
bool LootConfig::getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) { bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) {
bool value = defaultValue; bool value = defaultValue;
element->QueryBoolAttribute(attribute, &value); element->QueryBoolAttribute(attribute, &value);
return value; return value;

View File

@@ -1,11 +1,21 @@
#include "configs/npcs.hpp" #include "configs/npcs.hpp"
#include "project_parser.h"
#include "helpers.hpp"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
namespace cursebreaker { namespace cursebreaker {
bool NPCsConfig::loadFromXML(const std::string& filepath) { // Static helper functions
static std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
static int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
static bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
static void parseNPC(tinyxml2::XMLElement* npcElement);
static void parseStats(tinyxml2::XMLElement* npcElement, NPC& npc);
static void parseLoot(tinyxml2::XMLElement* npcElement, NPC& npc);
bool loadNPCsFromXML(const std::string& filepath) {
tinyxml2::XMLDocument doc; tinyxml2::XMLDocument doc;
tinyxml2::XMLError result = doc.LoadFile(filepath.c_str()); tinyxml2::XMLError result = doc.LoadFile(filepath.c_str());
@@ -20,7 +30,7 @@ bool NPCsConfig::loadFromXML(const std::string& filepath) {
return false; return false;
} }
m_npcs.clear(); g_parsedProject.m_npcs.clear();
// Parse all NPCs // Parse all NPCs
for (tinyxml2::XMLElement* npcElement = root->FirstChildElement("npc"); for (tinyxml2::XMLElement* npcElement = root->FirstChildElement("npc");
@@ -29,11 +39,11 @@ bool NPCsConfig::loadFromXML(const std::string& filepath) {
parseNPC(npcElement); parseNPC(npcElement);
} }
std::cout << "Loaded " << m_npcs.size() << " NPCs from XML" << std::endl; std::cout << "Loaded " << g_parsedProject.m_npcs.size() << " NPCs from XML" << std::endl;
return true; return true;
} }
void NPCsConfig::parseNPC(tinyxml2::XMLElement* npcElement) { void parseNPC(tinyxml2::XMLElement* npcElement) {
NPC npc; NPC npc;
// Parse basic attributes // Parse basic attributes
@@ -53,10 +63,10 @@ void NPCsConfig::parseNPC(tinyxml2::XMLElement* npcElement) {
parseStats(npcElement, npc); parseStats(npcElement, npc);
parseLoot(npcElement, npc); parseLoot(npcElement, npc);
m_npcs[npc.id] = std::move(npc); g_parsedProject.m_npcs[npc.id] = std::move(npc);
} }
void NPCsConfig::parseStats(tinyxml2::XMLElement* npcElement, NPC& npc) { void parseStats(tinyxml2::XMLElement* npcElement, NPC& npc) {
for (tinyxml2::XMLElement* statElement = npcElement->FirstChildElement("stat"); for (tinyxml2::XMLElement* statElement = npcElement->FirstChildElement("stat");
statElement != nullptr; statElement != nullptr;
statElement = statElement->NextSiblingElement("stat")) { statElement = statElement->NextSiblingElement("stat")) {
@@ -70,7 +80,7 @@ void NPCsConfig::parseStats(tinyxml2::XMLElement* npcElement, NPC& npc) {
} }
} }
void NPCsConfig::parseLoot(tinyxml2::XMLElement* npcElement, NPC& npc) { void parseLoot(tinyxml2::XMLElement* npcElement, NPC& npc) {
std::string lootItemsStr = getAttributeValue(npcElement, "lootitems"); std::string lootItemsStr = getAttributeValue(npcElement, "lootitems");
if (!lootItemsStr.empty()) { if (!lootItemsStr.empty()) {
std::stringstream ss(lootItemsStr); std::stringstream ss(lootItemsStr);
@@ -86,23 +96,27 @@ void NPCsConfig::parseLoot(tinyxml2::XMLElement* npcElement, NPC& npc) {
} }
} }
const NPC* NPCsConfig::getNPCById(int id) const { const NPC* getNPCById(uint16_t id) {
auto it = m_npcs.find(id); auto it = g_parsedProject.m_npcs.find(id);
return (it != m_npcs.end()) ? &it->second : nullptr; return (it != g_parsedProject.m_npcs.end()) ? &it->second : nullptr;
} }
std::string NPCsConfig::getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) { const std::unordered_map<uint16_t, NPC>& getAllNPCs() {
return g_parsedProject.m_npcs;
}
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) {
const char* value = element->Attribute(attribute); const char* value = element->Attribute(attribute);
return value ? std::string(value) : defaultValue; return value ? std::string(value) : defaultValue;
} }
int NPCsConfig::getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) { int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) {
int value = defaultValue; int value = defaultValue;
element->QueryIntAttribute(attribute, &value); element->QueryIntAttribute(attribute, &value);
return value; return value;
} }
bool NPCsConfig::getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) { bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) {
bool value = defaultValue; bool value = defaultValue;
element->QueryBoolAttribute(attribute, &value); element->QueryBoolAttribute(attribute, &value);
return value; return value;

View File

@@ -1,9 +1,17 @@
#include "configs/shops.hpp" #include "configs/shops.hpp"
#include "project_parser.h"
#include <iostream> #include <iostream>
namespace cursebreaker { namespace cursebreaker {
bool ShopsConfig::loadFromXML(const std::string& filepath) { // Static helper functions
static std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue = "");
static int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue = 0);
static bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue = false);
static void parseShop(tinyxml2::XMLElement* shopElement);
static void parseInventory(tinyxml2::XMLElement* shopElement, Shop& shop);
bool loadShopsFromXML(const std::string& filepath) {
tinyxml2::XMLDocument doc; tinyxml2::XMLDocument doc;
tinyxml2::XMLError result = doc.LoadFile(filepath.c_str()); tinyxml2::XMLError result = doc.LoadFile(filepath.c_str());
@@ -18,7 +26,7 @@ bool ShopsConfig::loadFromXML(const std::string& filepath) {
return false; return false;
} }
m_shops.clear(); g_parsedProject.m_shops.clear();
// Parse all shops // Parse all shops
for (tinyxml2::XMLElement* shopElement = root->FirstChildElement("shop"); for (tinyxml2::XMLElement* shopElement = root->FirstChildElement("shop");
@@ -27,11 +35,11 @@ bool ShopsConfig::loadFromXML(const std::string& filepath) {
parseShop(shopElement); parseShop(shopElement);
} }
std::cout << "Loaded " << m_shops.size() << " shops from XML" << std::endl; std::cout << "Loaded " << g_parsedProject.m_shops.size() << " shops from XML" << std::endl;
return true; return true;
} }
void ShopsConfig::parseShop(tinyxml2::XMLElement* shopElement) { void parseShop(tinyxml2::XMLElement* shopElement) {
Shop shop; Shop shop;
// Parse basic attributes // Parse basic attributes
@@ -43,10 +51,10 @@ void ShopsConfig::parseShop(tinyxml2::XMLElement* shopElement) {
// Parse child elements // Parse child elements
parseInventory(shopElement, shop); parseInventory(shopElement, shop);
m_shops[shop.id] = std::move(shop); g_parsedProject.m_shops[shop.id] = std::move(shop);
} }
void ShopsConfig::parseInventory(tinyxml2::XMLElement* shopElement, Shop& shop) { void parseInventory(tinyxml2::XMLElement* shopElement, Shop& shop) {
for (tinyxml2::XMLElement* itemElement = shopElement->FirstChildElement("item"); for (tinyxml2::XMLElement* itemElement = shopElement->FirstChildElement("item");
itemElement != nullptr; itemElement != nullptr;
itemElement = itemElement->NextSiblingElement("item")) { itemElement = itemElement->NextSiblingElement("item")) {
@@ -61,23 +69,27 @@ void ShopsConfig::parseInventory(tinyxml2::XMLElement* shopElement, Shop& shop)
} }
} }
const Shop* ShopsConfig::getShopById(int id) const { const Shop* getShopById(uint16_t id) {
auto it = m_shops.find(id); auto it = g_parsedProject.m_shops.find(id);
return (it != m_shops.end()) ? &it->second : nullptr; return (it != g_parsedProject.m_shops.end()) ? &it->second : nullptr;
} }
std::string ShopsConfig::getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) { const std::unordered_map<uint16_t, Shop>& getAllShops() {
return g_parsedProject.m_shops;
}
std::string getAttributeValue(tinyxml2::XMLElement* element, const char* attribute, const std::string& defaultValue) {
const char* value = element->Attribute(attribute); const char* value = element->Attribute(attribute);
return value ? std::string(value) : defaultValue; return value ? std::string(value) : defaultValue;
} }
int ShopsConfig::getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) { int getAttributeValueInt(tinyxml2::XMLElement* element, const char* attribute, int defaultValue) {
int value = defaultValue; int value = defaultValue;
element->QueryIntAttribute(attribute, &value); element->QueryIntAttribute(attribute, &value);
return value; return value;
} }
bool ShopsConfig::getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) { bool getAttributeValueBool(tinyxml2::XMLElement* element, const char* attribute, bool defaultValue) {
bool value = defaultValue; bool value = defaultValue;
element->QueryBoolAttribute(attribute, &value); element->QueryBoolAttribute(attribute, &value);
return value; return value;

198
src/helpers.cpp Normal file
View File

@@ -0,0 +1,198 @@
#include "helpers.hpp"
namespace cursebreaker {
// Helper functions to convert strings to enums
StatType stringToStatType(const std::string& str) {
static const std::unordered_map<std::string, StatType> statMap = {
{"damagephysical", StatType::damagePhysical},
{"damagemagical", StatType::damageMagical},
{"damageranged", StatType::damageRanged},
{"accuracyphysical", StatType::accuracyPhysical},
{"accuracymagical", StatType::accuracyMagical},
{"accuracyranged", StatType::accuracyRanged},
{"resistancephysical", StatType::resistancePhysical},
{"resistancemagical", StatType::resistanceMagical},
{"resistanceranged", StatType::resistanceRanged},
{"health", StatType::health},
{"mana", StatType::mana},
{"manaregen", StatType::manaregen},
{"healthregen", StatType::healthregen},
{"movementSpeed", StatType::movementSpeed},
{"power", StatType::power},
{"critical", StatType::critical},
{"healing", StatType::healing},
{"DamageVsUndead", StatType::DamageVsUndead},
{"DamageVsBeasts", StatType::DamageVsBeasts},
{"CritterSlaying", StatType::CritterSlaying},
{"harvestingSpeedWoodcutting", StatType::harvestingSpeedWoodcutting}
};
auto it = statMap.find(str);
return (it != statMap.end()) ? it->second : StatType::none;
}
ItemCategory stringToItemCategory(const std::string& str) {
static const std::unordered_map<std::string, ItemCategory> categoryMap = {
{"bone", ItemCategory::bone},
{"bow", ItemCategory::bow},
{"crossbow", ItemCategory::crossbow},
{"constructable", ItemCategory::constructable},
{"torch", ItemCategory::torch},
{"blacksmithhammer", ItemCategory::blacksmithhammer},
{"questitem", ItemCategory::questitem},
{"heavyArmor", ItemCategory::heavyArmor},
{"warhammer", ItemCategory::warhammer},
{"shield", ItemCategory::shield},
{"hatchet", ItemCategory::hatchet},
{"blade", ItemCategory::blade},
{"armor", ItemCategory::armor},
{"pickaxe", ItemCategory::pickaxe},
{"fish", ItemCategory::fish},
{"fishingrod", ItemCategory::fishingrod},
{"shears", ItemCategory::shears},
{"hammer", ItemCategory::hammer},
{"battleaxe", ItemCategory::battleaxe},
{"morningstar", ItemCategory::morningstar},
{"wand", ItemCategory::wand},
{"staff", ItemCategory::staff},
{"dagger", ItemCategory::dagger}
};
auto it = categoryMap.find(str);
return (it != categoryMap.end()) ? it->second : ItemCategory::none;
}
ItemType stringToItemType(const std::string& str) {
static const std::unordered_map<std::string, ItemType> typeMap = {
{"weapon", ItemType::weapon},
{"shield", ItemType::shield},
{"armor", ItemType::armor},
{"head", ItemType::head},
{"resource", ItemType::resource},
{"consumable", ItemType::consumable},
{"trinket", ItemType::trinket},
{"bracelet", ItemType::bracelet}
};
auto it = typeMap.find(str);
return (it != typeMap.end()) ? it->second : ItemType::resource;
}
Tool stringToTool(const std::string& str) {
static const std::unordered_map<std::string, Tool> toolMap = {
{"hatchet", Tool::hatchet},
{"pickaxe", Tool::pickaxe},
{"broom", Tool::broom},
{"fishingrod", Tool::fishingrod}
};
auto it = toolMap.find(str);
return (it != toolMap.end()) ? it->second : Tool::none;
}
SkillType stringToSkillType(const std::string& str) {
static const std::unordered_map<std::string, SkillType> skillMap = {
{"woodcutting", SkillType::woodcutting},
{"fishing", SkillType::fishing},
{"swordsmanship", SkillType::swordsmanship},
{"mining", SkillType::mining},
{"archery", SkillType::archery},
{"magic", SkillType::magic},
{"defence", SkillType::defence},
{"blacksmithy", SkillType::blacksmithy},
{"tailoring", SkillType::tailoring},
{"carpentry", SkillType::carpentry},
{"alchemy", SkillType::alchemy},
{"cooking", SkillType::cooking}
};
auto it = skillMap.find(str);
return (it != skillMap.end()) ? it->second : SkillType::none;
}
WorkbenchType stringToWorkbenchType(const std::string& str) {
static const std::unordered_map<std::string, WorkbenchType> workbenchMap = {
{"none", WorkbenchType::none},
{"anvil", WorkbenchType::anvil},
{"oven", WorkbenchType::oven},
{"cooking", WorkbenchType::cooking},
{"carpenter", WorkbenchType::carpenter},
{"tailor", WorkbenchType::tailor},
{"forge", WorkbenchType::forge},
{"alchemist", WorkbenchType::alchemist},
{"mystic", WorkbenchType::mystic}
};
auto it = workbenchMap.find(str);
return (it != workbenchMap.end()) ? it->second : WorkbenchType::none;
}
GenstatType stringToGenstatType(const std::string& str) {
static const std::unordered_map<std::string, GenstatType> genstatMap = {
{"dagger", GenstatType::dagger},
{"broadsword", GenstatType::broadsword},
{"battleaxe", GenstatType::battleaxe},
{"greatsword", GenstatType::greatsword},
{"morningstar", GenstatType::morningstar},
{"hammer", GenstatType::hammer},
{"spear", GenstatType::spear},
{"bow", GenstatType::bow},
{"staff", GenstatType::staff},
{"wand", GenstatType::wand},
{"crossbow", GenstatType::crossbow},
{"woodenshield", GenstatType::woodenshield},
{"wizardhat", GenstatType::wizardhat},
{"wizardrobe", GenstatType::wizardrobe},
{"grandwizardhat", GenstatType::grandwizardhat},
{"grandwizardrobe", GenstatType::grandwizardrobe},
{"leatherhood", GenstatType::leatherhood},
{"leatherbracelet", GenstatType::leatherbracelet},
{"leatherarmor", GenstatType::leatherarmor},
{"studdedleatherhood", GenstatType::studdedleatherhood},
{"studdedleatherbracelet", GenstatType::studdedleatherbracelet},
{"studdedleatherarmor", GenstatType::studdedleatherarmor},
{"helmet", GenstatType::helmet},
{"shield", GenstatType::shield},
{"armor", GenstatType::armor},
{"platehelmet", GenstatType::platehelmet},
{"kiteshield", GenstatType::kiteshield},
{"platearmor", GenstatType::platearmor}
};
auto it = genstatMap.find(str);
return (it != genstatMap.end()) ? it->second : GenstatType::none;
}
ActionType stringToActionType(const std::string& str) {
static const std::unordered_map<std::string, ActionType> actionMap = {
{"NpcDeath", ActionType::NpcDeath},
{"PlayerDeath", ActionType::PlayerDeath},
{"PlayerRespawn", ActionType::PlayerRespawn},
{"NpcInteract", ActionType::NpcInteract},
{"QuestUpdate", ActionType::QuestUpdate},
{"QuestTimerEnd", ActionType::QuestTimerEnd},
{"UseItemOnItem", ActionType::UseItemOnItem},
{"UseItemOnNpc", ActionType::UseItemOnNpc},
{"ConsumeItem", ActionType::ConsumeItem},
{"NpcCombatInteract", ActionType::NpcCombatInteract},
{"NpcKilledByPlayer", ActionType::NpcKilledByPlayer},
{"EnterMap", ActionType::EnterMap},
{"VarUpdated", ActionType::VarUpdated},
{"GrantAchievement", ActionType::GrantAchievement},
{"PlayerKilledByNpc", ActionType::PlayerKilledByNpc},
{"UseItem", ActionType::UseItem},
{"CraftItem", ActionType::CraftItem},
{"HarvestItem", ActionType::HarvestItem},
{"BuyItem", ActionType::BuyItem},
{"NpcTagKilledByPlayer", ActionType::NpcTagKilledByPlayer},
{"UseAbility", ActionType::UseAbility},
{"LootItem", ActionType::LootItem},
{"UseItemCategoryOnItem", ActionType::UseItemCategoryOnItem}
};
auto it = actionMap.find(str);
return (it != actionMap.end()) ? it->second : ActionType::none;
}
} // namespace cursebreaker

View File

@@ -7,6 +7,8 @@
#include "assets/scene.hpp" #include "assets/scene.hpp"
#include "configs/items.hpp" #include "configs/items.hpp"
using namespace cursebreaker;
int main() { int main() {
ParseProject("../../CBAssets/_GameAssets/"); ParseProject("../../CBAssets/_GameAssets/");
@@ -17,25 +19,5 @@ int main() {
BuildTree(scene); BuildTree(scene);
} }
// Test items parser
std::cout << "Testing items parser..." << std::endl;
auto& itemsConfig = cursebreaker::ItemsConfig::getInstance();
bool success = itemsConfig.loadFromXML("../../CBAssets/Data/XMLs/Items/Items.xml");
if (success) {
std::cout << "Successfully loaded items XML!" << std::endl;
// Test getting a specific item
const auto* item = itemsConfig.getItemById(150);
if (item) {
std::cout << "Found item: " << item->name << " (ID: " << item->id << ")" << std::endl;
std::cout << "Description: " << item->description << std::endl;
std::cout << "Level: " << item->level << ", Price: " << item->price << std::endl;
} else {
std::cout << "Item with ID 150 not found" << std::endl;
}
} else {
std::cout << "Failed to load items XML!" << std::endl;
}
return 0; return 0;
} }

View File

@@ -29,6 +29,8 @@
#include "assets/mesh_filter.hpp" #include "assets/mesh_filter.hpp"
#include "assets/prefab_instance.hpp" #include "assets/prefab_instance.hpp"
namespace cursebreaker {
constexpr uint32_t fnv1a_hash(const char* str, uint32_t hash = 2166136261u) constexpr uint32_t fnv1a_hash(const char* str, uint32_t hash = 2166136261u)
{ {
return (*str == '\0') ? hash : fnv1a_hash(str + 1, (hash ^ static_cast<uint32_t>(*str)) * 16777619u); return (*str == '\0') ? hash : fnv1a_hash(str + 1, (hash ^ static_cast<uint32_t>(*str)) * 16777619u);
@@ -461,3 +463,5 @@ void ParseProject(const std::filesystem::path& projectRoot)
project.m_sceneAssets = ParseScenes(std::vector<AssetPath>(tilesFiles.begin(), tilesFiles.end())); project.m_sceneAssets = ParseScenes(std::vector<AssetPath>(tilesFiles.begin(), tilesFiles.end()));
} }
} // namespace cursebreaker