From 70fc4db1b5570beceda662e9f34ec1729610512e Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 8 Feb 2017 21:33:36 +0300 Subject: Move quests loading code into quests db. --- src/resources/db/questdb.cpp | 243 +++++++++++++++++++++++++++++++++++++++++++ src/resources/db/questdb.h | 53 ++++++++++ src/resources/questvar.h | 5 + 3 files changed, 301 insertions(+) create mode 100644 src/resources/db/questdb.cpp create mode 100644 src/resources/db/questdb.h (limited to 'src/resources') diff --git a/src/resources/db/questdb.cpp b/src/resources/db/questdb.cpp new file mode 100644 index 000000000..9df240f92 --- /dev/null +++ b/src/resources/db/questdb.cpp @@ -0,0 +1,243 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "resources/db/questdb.h" + +#include "configuration.h" +#include "logger.h" + +#include "utils/dtor.h" +#include "utils/gettext.h" + +#include "utils/translation/podict.h" + +#include "resources/beingcommon.h" +#include "resources/questeffect.h" +#include "resources/questitem.h" + +#include "debug.h" + +namespace +{ + // quest variables: var, (val1, val2, val3, time) + NpcQuestVarMap mVars; + // quests: var, quests + std::map > mQuests; + std::vector mAllEffects; +} + +void QuestDb::load() +{ + unload(); + loadXmlFile(paths.getStringValue("questsFile"), SkipError_false); + loadXmlFile(paths.getStringValue("questsPatchFile"), SkipError_true); + loadXmlDir("questsPatchDir", loadXmlFile); +} + +static void loadQuest(const int var, + const XmlNodePtr node) +{ + if (!node) + return; + QuestItem *const quest = new QuestItem(); + // TRANSLATORS: quests window quest name + quest->name = XML::langProperty(node, "name", _("unknown")); + quest->group = XML::getProperty(node, "group", ""); + std::string incompleteStr = XML::getProperty(node, "incomplete", ""); + std::string completeStr = XML::getProperty(node, "complete", ""); + if (incompleteStr.empty() && completeStr.empty()) + { + logger->log("complete flags incorrect"); + delete quest; + return; + } + splitToIntSet(quest->incomplete, incompleteStr, ','); + splitToIntSet(quest->complete, completeStr, ','); + if (quest->incomplete.empty() && quest->complete.empty()) + { + logger->log("complete flags incorrect"); + delete quest; + return; + } + if (quest->incomplete.empty() || quest->complete.empty()) + quest->broken = true; + + for_each_xml_child_node(dataNode, node) + { + if (!xmlTypeEqual(dataNode, XML_ELEMENT_NODE)) + continue; + const char *const data = reinterpret_cast( + XmlNodeGetContent(dataNode)); + if (!data) + continue; + std::string str = translator->getStr(data); + + for (int f = 1; f < 100; f ++) + { + const std::string key = strprintf("text%d", f); + const std::string val = XML::getProperty(dataNode, + key.c_str(), + ""); + if (val.empty()) + break; + const std::string param = strprintf("{@@%d}", f); + replaceAll(str, param, val); + } + replaceItemLinks(str); + if (xmlNameEqual(dataNode, "text")) + { + quest->texts.push_back(QuestItemText(str, + QuestType::TEXT, + std::string(), + std::string())); + } + else if (xmlNameEqual(dataNode, "name")) + { + quest->texts.push_back(QuestItemText(str, + QuestType::NAME, + std::string(), + std::string())); + } + else if (xmlNameEqual(dataNode, "reward")) + { + quest->texts.push_back(QuestItemText(str, + QuestType::REWARD, + std::string(), + std::string())); + } + else if (xmlNameEqual(dataNode, "questgiver") || + xmlNameEqual(dataNode, "giver")) + { + quest->texts.push_back(QuestItemText(str, + QuestType::GIVER, + std::string(), + std::string())); + } + else if (xmlNameEqual(dataNode, "coordinates")) + { + const std::string str1 = toString(XML::getIntProperty( + dataNode, "x", 0, 1, 1000)); + const std::string str2 = toString(XML::getIntProperty( + dataNode, "y", 0, 1, 1000)); + quest->texts.push_back(QuestItemText(str, + QuestType::COORDINATES, + str1, + str2)); + } + else if (xmlNameEqual(dataNode, "npc")) + { + quest->texts.push_back(QuestItemText(str, + QuestType::NPC, + std::string(), + std::string())); + } + } + quest->var = var; + mQuests[var].push_back(quest); +} + +static void loadEffect(const int var, + const XmlNodePtr node) +{ + QuestEffect *const effect = new QuestEffect; + effect->map = XML::getProperty(node, "map", ""); + effect->id = fromInt(XML::getProperty(node, "npc", -1), BeingTypeId); + effect->effectId = XML::getProperty(node, "effect", -1); + const std::string values = XML::getProperty(node, "value", ""); + splitToIntSet(effect->values, values, ','); + + if (effect->map.empty() || effect->id == BeingTypeId_negOne + || effect->effectId == -1 || values.empty()) + { + delete effect; + return; + } + effect->var = var; + mAllEffects.push_back(effect); +} + +void QuestDb::loadXmlFile(const std::string &fileName, + const SkipError skipError) +{ + XML::Document doc(fileName, + UseResman_true, + skipError); + const XmlNodePtrConst root = doc.rootNode(); + if (!root) + return; + + for_each_xml_child_node(varNode, root) + { + if (xmlNameEqual(varNode, "include")) + { + const std::string name = XML::getProperty(varNode, "name", ""); + if (!name.empty()) + loadXmlFile(name, skipError); + continue; + } + else if (xmlNameEqual(varNode, "var")) + { + const int id = XML::getProperty(varNode, "id", 0); + if (id < 0) + continue; + mVars[id] = QuestVar(); + for_each_xml_child_node(questNode, varNode) + { + if (xmlNameEqual(questNode, "quest")) + loadQuest(id, questNode); + else if (xmlNameEqual(questNode, "effect")) + loadEffect(id, questNode); + } + } + } +} + + +void QuestDb::unload() +{ + for (std::map >::iterator it + = mQuests.begin(), it_end = mQuests.end(); it != it_end; ++ it) + { + std::vector &quests = (*it).second; + for (std::vector::iterator it2 = quests.begin(), + it2_end = quests.end(); it2 != it2_end; ++ it2) + { + delete *it2; + } + } + delete_all(mAllEffects); + mAllEffects.clear(); + mQuests.clear(); +} + +NpcQuestVarMap *QuestDb::getVars() +{ + return &mVars; +} + +std::map > *QuestDb::getQuests() +{ + return &mQuests; +} + +std::vector *QuestDb::getAllEffects() +{ + return &mAllEffects; +} diff --git a/src/resources/db/questdb.h b/src/resources/db/questdb.h new file mode 100644 index 000000000..3b9fa2ef1 --- /dev/null +++ b/src/resources/db/questdb.h @@ -0,0 +1,53 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef RESOURCES_DB_QUESTDB_H +#define RESOURCES_DB_QUESTDB_H + +#include "enums/simpletypes/skiperror.h" + +#include "resources/questvar.h" + +#include +#include +#include + +#include "localconsts.h" + +struct QuestEffect; +struct QuestItem; + +namespace QuestDb +{ + void load(); + + void loadXmlFile(const std::string &fileName, + const SkipError skipError); + + void unload(); + + NpcQuestVarMap *getVars(); + + std::map > *getQuests(); + + std::vector *getAllEffects(); +} // namespace QuestDb + +#endif // RESOURCES_DB_QUESTDB_H diff --git a/src/resources/questvar.h b/src/resources/questvar.h index 3895d2783..1164d0f58 100644 --- a/src/resources/questvar.h +++ b/src/resources/questvar.h @@ -21,6 +21,8 @@ #ifndef RESOURCES_QUESTVAR_H #define RESOURCES_QUESTVAR_H +#include + #include "localconsts.h" struct QuestVar final @@ -52,4 +54,7 @@ struct QuestVar final int time1; }; +typedef std::map NpcQuestVarMap; +typedef NpcQuestVarMap::const_iterator NpcQuestVarMapCIter; + #endif // RESOURCES_QUESTVAR_H -- cgit v1.2.3-70-g09d2