diff options
author | Andrei Karas <akaras@inbox.ru> | 2016-06-01 22:33:00 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2016-06-02 00:06:13 +0300 |
commit | d37ddaddbfb7e936fbe1994273d4d4ac58358a26 (patch) | |
tree | 1bf51f37ca8ee6889ab3fabfab8af54cd7a35aee /src/resources/db | |
parent | ff53770b5a3d4c9911ffba93f9a62697dd5b6d81 (diff) | |
download | manaplus-d37ddaddbfb7e936fbe1994273d4d4ac58358a26.tar.gz manaplus-d37ddaddbfb7e936fbe1994273d4d4ac58358a26.tar.bz2 manaplus-d37ddaddbfb7e936fbe1994273d4d4ac58358a26.tar.xz manaplus-d37ddaddbfb7e936fbe1994273d4d4ac58358a26.zip |
Add support for configurable item stat fields.
This fields defined in itemfields.xml
Can be used in items.xml
All default fields removed from ManaPlus. If need use it, please add
fields to itemfields.xml in client data.
Diffstat (limited to 'src/resources/db')
-rw-r--r-- | src/resources/db/itemdb.cpp | 122 | ||||
-rw-r--r-- | src/resources/db/itemfielddb.cpp | 145 | ||||
-rw-r--r-- | src/resources/db/itemfielddb.h | 48 |
3 files changed, 224 insertions, 91 deletions
diff --git a/src/resources/db/itemdb.cpp b/src/resources/db/itemdb.cpp index 9cea2b551..726478523 100644 --- a/src/resources/db/itemdb.cpp +++ b/src/resources/db/itemdb.cpp @@ -37,6 +37,7 @@ #include "resources/sprite/spritereference.h" #include "resources/db/itemdbstat.h" +#include "resources/db/itemfielddb.h" #include "net/serverfeatures.h" @@ -77,83 +78,6 @@ static void loadOrderSprite(ItemInfo *const itemInfo, static int parseSpriteName(const std::string &name); static int parseDirectionName(const std::string &name); -namespace -{ - static const ItemFieldType fields[] = - { - // TRANSLATORS: item info label (attack) - { "attack", N_("Attack %s"), true }, - // TRANSLATORS: item info label (min attack) - { "minattack", N_("Min attack %s"), true }, - // TRANSLATORS: item info label (max attack) - { "maxattack", N_("Max attack %s"), true }, - // TRANSLATORS: item info label (attack) - { "criticalattack", N_("Critical attack %s"), true }, - // TRANSLATORS: item info label (magic attack) - { "mattack", N_("M. Attack %s"), true }, - // TRANSLATORS: item info label (defence) - { "defense", N_("Defense %s"), true }, - // TRANSLATORS: item info label (min defence) - { "mindefense", N_("Min defense %s"), true }, - // TRANSLATORS: item info label (max defence) - { "maxdefense", N_("Max defense %s"), true }, - // TRANSLATORS: item info label (critical defence) - { "criticaldefense", N_("Critical defense %s"), true }, - // TRANSLATORS: item info label (magic defence) - { "mdefense", N_("M. Defense %s"), true }, - // TRANSLATORS: item info label (min magic defence) - { "minmdefense", N_("Min M. Defense %s"), true }, - // TRANSLATORS: item info label (max magic defence) - { "maxmdefense", N_("Max M. Defense %s"), true }, - // TRANSLATORS: item info label (health) - { "hp", N_("HP %s"), true }, - // TRANSLATORS: item info label (max health) - { "maxhp", N_("Max HP %s"), true }, - // TRANSLATORS: item info label (mana) - { "mp", N_("MP %s"), true }, - // TRANSLATORS: item info label (max mana) - { "maxmp", N_("Max MP %s"), true }, - // TRANSLATORS: item info label (level) - { "level", N_("Level %s"), false }, - // TRANSLATORS: item info label (moving speed) - { "speed", N_("Speed %s"), true }, - // TRANSLATORS: item info label (moving speed) - { "atkspeed", N_("Attack speed %s"), true }, - // TRANSLATORS: item info label (attack range) - { "range", N_("Range %s"), true }, - // TRANSLATORS: item info label (flee) - { "flee", N_("Flee %s"), true }, - // TRANSLATORS: item info label (min flee) - { "minflee", N_("Min flee %s"), true }, - // TRANSLATORS: item info label (max flee) - { "maxflee", N_("Max flee %s"), true }, - // TRANSLATORS: item info label (accuracy) - { "hit", N_("Perc. accuracy %s"), true }, - // TRANSLATORS: item info label (min flee) - { "minflee", N_("Min flee %s"), true }, - // TRANSLATORS: item info label (max flee) - { "maxflee", N_("Max flee %s"), true }, - // TRANSLATORS: item info label (card slots number) - { "cardslots", N_("Card slots %s"), false }, - // TRANSLATORS: item info label (experience) - { "exp", N_("Experience %s"), true }, - // TRANSLATORS: item info label (karma) - { "karma", N_("Karma %s"), true }, - // TRANSLATORS: item info label (manner) - { "manner", N_("Manner %s"), true }, - // TRANSLATORS: item info label (money) - { "money", N_("Money %s"), true }, - // TRANSLATORS: item info label (max weight) - { "maxweight", N_("Max weight %s"), true }, - // TRANSLATORS: item info label (job experience) - { "jobexp", N_("Job exp. %s"), true }, - // TRANSLATORS: item info label (hp recover rate) - { "hprecover", N_("Hp recov. rate %s"), true }, - // TRANSLATORS: item info label (sp/mana recover rate) - { "sprecover", N_("Sp recov. rate %s"), true } - }; -} // namespace - static std::vector<ItemDB::Stat> extraStats; void ItemDB::setStatsList(const std::vector<ItemDB::Stat> &stats) @@ -208,6 +132,29 @@ static std::string useButton2FromItemType(const ItemDbTypeT &type) return std::string(); } +static void readFields(std::string &effect, + const XmlNodePtr node, + const ItemFieldDb::FieldInfos &fields) +{ + FOR_EACH (ItemFieldDb::FieldInfos::const_iterator, it, fields) + { + const std::string fieldName = (*it).first; + const ItemFieldType *const field = (*it).second; + + std::string value = XML::getProperty(node, + fieldName.c_str(), + ""); + if (value.empty()) + continue; + if (!effect.empty()) + effect.append(" / "); + if (field->sign && isDigit(value)) + value = "+" + value; + effect.append(strprintf(gettext(field->description.c_str()), + value.c_str())); + } +} + static void initStatic() { mConstructed = true; @@ -313,6 +260,11 @@ void ItemDB::loadXmlFile(const std::string &fileName, return; } + const ItemFieldDb::FieldInfos &requiredFields = + ItemFieldDb::getRequiredFields(); + const ItemFieldDb::FieldInfos &addFields = + ItemFieldDb::getAddFields(); + for_each_xml_child_node(node, rootNode) { if (xmlNameEqual(node, "include")) @@ -554,20 +506,8 @@ void ItemDB::loadXmlFile(const std::string &fileName, node, "pickupCursor", "pickup")); std::string effect; - for (size_t i = 0; i < sizeof(fields) / sizeof(fields[0]); ++ i) - { - std::string value = XML::getProperty(node, - fields[i].name.c_str(), - ""); - if (value.empty()) - continue; - if (!effect.empty()) - effect.append(" / "); - if (fields[i].sign && isDigit(value)) - value = "+" + value; - effect.append(strprintf(gettext(fields[i].description.c_str()), - value.c_str())); - } + readFields(effect, node, requiredFields); + readFields(effect, node, addFields); FOR_EACH (std::vector<Stat>::const_iterator, it, extraStats) { std::string value = XML::getProperty( diff --git a/src/resources/db/itemfielddb.cpp b/src/resources/db/itemfielddb.cpp new file mode 100644 index 000000000..d7ea5e82a --- /dev/null +++ b/src/resources/db/itemfielddb.cpp @@ -0,0 +1,145 @@ +/* + * The ManaPlus Client + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ + +#include "resources/db/itemfielddb.h" + +#include "configuration.h" +#include "logger.h" + +#include "utils/checkutils.h" +#include "utils/dtor.h" + +#include "resources/beingcommon.h" + +#include "debug.h" + +namespace +{ + ItemFieldDb::FieldInfos mRequiredInfos; + ItemFieldDb::FieldInfos mAddInfos; + bool mLoaded = false; +} // namespace + +void ItemFieldDb::load() +{ + if (mLoaded) + unload(); + + logger->log1("Initializing item field database..."); + + loadXmlFile(paths.getStringValue("itemFieldsFile"), SkipError_false); + loadXmlFile(paths.getStringValue("itemFieldsPatchFile"), SkipError_true); + loadXmlDir("itemFieldsPatchDir", loadXmlFile); + mLoaded = true; +} + +static void loadFields(const XmlNodePtr groupNode, + ItemFieldDb::FieldInfos &fields1, + ItemFieldDb::FieldInfos &fields2) +{ + for_each_xml_child_node(node, groupNode) + { + if (!xmlNameEqual(node, "field")) + continue; + + const std::string name = XML::getProperty(node, + "name", + ""); + if (name.empty()) + { + reportAlways("Empty name field in ItemFieldDb"); + continue; + } + const std::string description = XML::langProperty(node, + "description", + ""); + if (description.empty()) + { + reportAlways("Empty description field in ItemFieldDb"); + continue; + } + const bool sign = XML::getBoolProperty(node, + "signed", + true); + if (fields2.find(name) != fields2.end()) + { + reportAlways( + "Same field name detected in requeted and add groups: %s", + name.c_str()); + continue; + } + fields1[name] = new ItemFieldType(name, + description, + sign); + } +} + +void ItemFieldDb::loadXmlFile(const std::string &fileName, + const SkipError skipError) +{ + XML::Document doc(fileName, + UseResman_true, + skipError); + const XmlNodePtrConst rootNode = doc.rootNode(); + + if (!rootNode || !xmlNameEqual(rootNode, "itemfields")) + { + logger->log("ItemFieldDb: Error while loading %s!", + fileName.c_str()); + return; + } + + for_each_xml_child_node(node, rootNode) + { + if (xmlNameEqual(node, "include")) + { + const std::string name = XML::getProperty(node, "name", ""); + if (!name.empty()) + loadXmlFile(name, skipError); + continue; + } + + if (xmlNameEqual(node, "required")) + loadFields(node, mRequiredInfos, mAddInfos); + else if (xmlNameEqual(node, "add")) + loadFields(node, mAddInfos, mRequiredInfos); + } +} + +void ItemFieldDb::unload() +{ + logger->log1("Unloading item database..."); + + delete_all(mRequiredInfos); + mRequiredInfos.clear(); + delete_all(mAddInfos); + mAddInfos.clear(); + mLoaded = false; +} + +const ItemFieldDb::FieldInfos &ItemFieldDb::getRequiredFields() +{ + return mRequiredInfos; +} + +const ItemFieldDb::FieldInfos &ItemFieldDb::getAddFields() +{ + return mAddInfos; +} diff --git a/src/resources/db/itemfielddb.h b/src/resources/db/itemfielddb.h new file mode 100644 index 000000000..d2f63e9fe --- /dev/null +++ b/src/resources/db/itemfielddb.h @@ -0,0 +1,48 @@ +/* + * The ManaPlus Client + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef RESOURCES_DB_ITEMFIELDDB_H +#define RESOURCES_DB_ITEMFIELDDB_H + +#include "enums/simpletypes/skiperror.h" + +#include "resources/item/itemfieldtype.h" + +#include <map> + +#include "localconsts.h" + +namespace ItemFieldDb +{ + void load(); + + void unload(); + + void loadXmlFile(const std::string &fileName, + const SkipError skipError); + + typedef std::map<std::string, ItemFieldType*> FieldInfos; + + const FieldInfos &getRequiredFields(); + + const FieldInfos &getAddFields(); +} // namespace ItemFieldDb + +#endif // RESOURCES_DB_ITEMFIELDDB_H |