summaryrefslogtreecommitdiff
path: root/src/resources/itemdb.cpp
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-01-24 22:10:02 +0100
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-01-24 22:10:02 +0100
commitd055bee5e6b45737ab89b3f8c70c6d0db7a22eb3 (patch)
tree081556c8fa27ce16372b1657e469ccd8587dd4e5 /src/resources/itemdb.cpp
parentf663f5bbb75b850ae22f451a22d112b6c62b2ad6 (diff)
downloadmana-d055bee5e6b45737ab89b3f8c70c6d0db7a22eb3.tar.gz
mana-d055bee5e6b45737ab89b3f8c70c6d0db7a22eb3.tar.bz2
mana-d055bee5e6b45737ab89b3f8c70c6d0db7a22eb3.tar.xz
mana-d055bee5e6b45737ab89b3f8c70c6d0db7a22eb3.zip
Refactored the item loading in a more extensible and per protocol way.
This will greatly help into upgrading the need of each protocol separately. This is the first step to a new item and equipment system for manaserv. A subclassing of the EquipmentWindow will be done in the next commit, as requested by Thorbjorn. Reviewed-by: Thorbjorn.
Diffstat (limited to 'src/resources/itemdb.cpp')
-rw-r--r--src/resources/itemdb.cpp568
1 files changed, 344 insertions, 224 deletions
diff --git a/src/resources/itemdb.cpp b/src/resources/itemdb.cpp
index 435cdf96..2271abef 100644
--- a/src/resources/itemdb.cpp
+++ b/src/resources/itemdb.cpp
@@ -38,18 +38,7 @@
#include <cassert>
-// Forward declarations
-static char const *const fields[][2] =
-{
- { "attack", N_("Attack %+d") },
- { "defense", N_("Defense %+d") },
- { "hp", N_("HP %+d") },
- { "mp", N_("MP %+d") }
-};
-
-static std::list<ItemDB::Stat> extraStats;
-
-void ItemDB::setStatsList(const std::list<Stat> &stats)
+void setStatsList(const std::list<ItemStat> &stats)
{
extraStats = stats;
}
@@ -75,47 +64,136 @@ static ItemType itemTypeFromString(const std::string &name, int id = 0)
else return ITEM_UNUSABLE;
}
-void ItemDB::load()
+/*
+ * Common itemDB functions
+ */
+
+bool ItemDB::exists(int id)
{
- if (mLoaded)
- unload();
+ assert(mLoaded);
- logger->log("Initializing item database...");
+ ItemInfos::const_iterator i = mItemInfos.find(id);
- mUnknown = new ItemInfo;
- mUnknown->mName = _("Unknown item");
- mUnknown->mDisplay = SpriteDisplay();
- std::string errFile = paths.getStringValue("spriteErrorFile");
- mUnknown->setSprite(errFile, GENDER_MALE);
- mUnknown->setSprite(errFile, GENDER_FEMALE);
+ return i != mItemInfos.end();
+}
- XML::Document doc(ITEMS_DB_FILE);
- xmlNodePtr rootNode = doc.rootNode();
+const ItemInfo &ItemDB::get(int id)
+{
+ assert(mLoaded);
- if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "items"))
+ ItemInfos::const_iterator i = mItemInfos.find(id);
+
+ if (i == mItemInfos.end())
{
- logger->log("ItemDB: Error while loading " ITEMS_DB_FILE "!");
- return;
+ logger->log("ItemDB: Warning, unknown item ID# %d", id);
+ return *mUnknown;
}
- for_each_xml_child_node(node, rootNode)
+ return *(i->second);
+}
+
+const ItemInfo &ItemDB::get(const std::string &name)
+{
+ assert(mLoaded);
+
+ NamedItemInfos::const_iterator i = mNamedItemInfos.find(normalize(name));
+
+ if (i == mNamedItemInfos.end())
+ {
+ if (!name.empty())
+ {
+ logger->log("ItemDB: Warning, unknown item name \"%s\"",
+ name.c_str());
+ }
+ return *mUnknown;
+ }
+
+ return *(i->second);
+}
+
+void ItemDB::loadSpriteRef(ItemInfo *itemInfo, xmlNodePtr node)
+{
+ std::string gender = XML::getProperty(node, "gender", "unisex");
+ std::string filename = (const char*) node->xmlChildrenNode->content;
+
+ if (gender == "male" || gender == "unisex")
+ {
+ itemInfo->setSprite(filename, GENDER_MALE);
+ }
+ if (gender == "female" || gender == "unisex")
+ {
+ itemInfo->setSprite(filename, GENDER_FEMALE);
+ }
+}
+
+void ItemDB::loadSoundRef(ItemInfo *itemInfo, xmlNodePtr node)
+{
+ std::string event = XML::getProperty(node, "event", "");
+ std::string filename = (const char*) node->xmlChildrenNode->content;
+
+ if (event == "hit")
{
+ itemInfo->addSound(EQUIP_EVENT_HIT, filename);
+ }
+ else if (event == "strike")
+ {
+ itemInfo->addSound(EQUIP_EVENT_STRIKE, filename);
+ }
+ else
+ {
+ logger->log("ItemDB: Ignoring unknown sound event '%s'",
+ event.c_str());
+ }
+}
+
+void ItemDB::loadFloorSprite(SpriteDisplay *display, xmlNodePtr floorNode)
+{
+ for_each_xml_child_node(spriteNode, floorNode)
+ {
+ if (xmlStrEqual(spriteNode->name, BAD_CAST "sprite"))
+ {
+ SpriteReference *currentSprite = new SpriteReference;
+ currentSprite->sprite = (const char*)spriteNode->xmlChildrenNode->content;
+ currentSprite->variant = XML::getProperty(spriteNode, "variant", 0);
+ display->sprites.push_back(currentSprite);
+ }
+ else if (xmlStrEqual(spriteNode->name, BAD_CAST "particlefx"))
+ {
+ std::string particlefx = (const char*)spriteNode->xmlChildrenNode->content;
+ display->particles.push_back(particlefx);
+ }
+ }
+}
+
+void ItemDB::unload()
+{
+ logger->log("Unloading item database...");
+
+ delete mUnknown;
+ mUnknown = NULL;
+
+ delete_all(mItemInfos);
+ mItemInfos.clear();
+ mNamedItemInfos.clear();
+ mLoaded = false;
+}
+
+void ItemDB::loadCommonRef(ItemInfo *itemInfo, xmlNodePtr node)
+{
if (!xmlStrEqual(node->name, BAD_CAST "item"))
- continue;
+ return;
int id = XML::getProperty(node, "id", 0);
if (!id)
{
- logger->log("ItemDB: Invalid or missing item ID in "
+ logger->log("ItemDB: Invalid or missing item Id in "
ITEMS_DB_FILE "!");
- continue;
+ return;
}
else if (mItemInfos.find(id) != mItemInfos.end())
- logger->log("ItemDB: Redefinition of item ID %d", id);
+ logger->log("ItemDB: Redefinition of item Id %d", id);
- std::string typeStr = XML::getProperty(node, "type", "other");
- int weight = XML::getProperty(node, "weight", 0);
int view = XML::getProperty(node, "view", 0);
std::string name = XML::getProperty(node, "name", "");
@@ -125,42 +203,26 @@ void ItemDB::load()
int attackRange = XML::getProperty(node, "attack-range", 0);
std::string missileParticle = XML::getProperty(node, "missile-particle", "");
+ // Load Ta Item Type
+ std::string typeStr = XML::getProperty(node, "type", "other");
+ itemInfo->mType = itemTypeFromString(typeStr);
+
+ int weight = XML::getProperty(node, "weight", 0);
+ itemInfo->mWeight = weight > 0 ? weight : 0;
+
SpriteDisplay display;
display.image = image;
- ItemInfo *itemInfo = new ItemInfo;
itemInfo->mId = id;
- itemInfo->mName = name.empty() ? _("unnamed") : name;
+ itemInfo->mName = name;
itemInfo->mDescription = description;
- itemInfo->mType = itemTypeFromString(typeStr);
- itemInfo->mActivatable = itemInfo->mType == ITEM_USABLE;
- // Everything not unusable or usable is equippable by the old type system.
- itemInfo->mEquippable = itemInfo->mType != ITEM_UNUSABLE
- && itemInfo->mType != ITEM_USABLE;
itemInfo->mView = view;
itemInfo->mWeight = weight;
itemInfo->setAttackAction(attackAction);
itemInfo->mAttackRange = attackRange;
itemInfo->setMissileParticle(missileParticle);
- std::vector<std::string> effect;
- for (int i = 0; i < int(sizeof(fields) / sizeof(fields[0])); ++i)
- {
- int value = XML::getProperty(node, fields[i][0], 0);
- if (!value) continue;
- effect.push_back(strprintf(gettext(fields[i][1]), value));
- }
- for (std::list<Stat>::iterator it = extraStats.begin();
- it != extraStats.end(); it++)
- {
- int value = XML::getProperty(node, it->tag.c_str(), 0);
- if (!value) continue;
- effect.push_back(strprintf(it->format.c_str(), value));
- }
- std::string temp = XML::getProperty(node, "effect", "");
- if (!temp.empty())
- effect.push_back(temp);
-
+ // Load <sprite>, <sound>, and <floor>
for_each_xml_child_node(itemChild, node)
{
if (xmlStrEqual(itemChild->name, BAD_CAST "sprite"))
@@ -179,13 +241,209 @@ void ItemDB::load()
{
loadFloorSprite(&display, itemChild);
}
- /*
- * Begin new item definition code. Previous code is left in to
- * maintain backwards compatibility.
- * Exit here if tmwAthena.
- */
- else if (Net::getNetworkType() == ServerInfo::TMWATHENA);
- else if (xmlStrEqual(itemChild->name, BAD_CAST "equip"))
+
+ }
+
+ // If the item has got a floor image, we bind the good reference.
+ itemInfo->mDisplay = display;
+}
+
+void ItemDB::addItem(ItemInfo *itemInfo)
+{
+ std::string itemName = itemInfo->mName;
+ itemInfo->mName = itemName.empty() ? _("unnamed") : itemName;
+ mItemInfos[itemInfo->mId] = itemInfo;
+ if (!itemName.empty())
+ {
+ std::string temp = normalize(itemName);
+
+ NamedItemInfos::const_iterator itr = mNamedItemInfos.find(temp);
+ if (itr == mNamedItemInfos.end())
+ mNamedItemInfos[temp] = itemInfo;
+ else
+ logger->log("ItemDB: Duplicate name (%s) for item id %d found.",
+ temp.c_str(), itemInfo->mId);
+
+ }
+}
+
+template <class T>
+static void checkParameter(int id, const T param, const T errorValue)
+{
+ if (param == errorValue)
+ {
+ std::stringstream errMsg;
+ errMsg << "ItemDB: Missing " << param << " attribute for item id "
+ << id << "!";
+ logger->log(errMsg.str().c_str());
+ }
+}
+
+void ItemDB::checkItemInfo(ItemInfo* itemInfo)
+{
+ int id = itemInfo->mId;
+ if (!itemInfo->getAttackAction().empty())
+ if (itemInfo->mAttackRange == 0)
+ logger->log("ItemDB: Missing attack range from weapon %i!", id);
+
+ if (id >= 0)
+ {
+ checkParameter(id, itemInfo->mName, std::string(""));
+ checkParameter(id, itemInfo->mDescription, std::string(""));
+ checkParameter(id, itemInfo->mDisplay.image, std::string(""));
+ checkParameter(id, itemInfo->mWeight, 0);
+ }
+}
+
+namespace TmwAthena {
+
+// Description fields used by TaItemDB *itemInfo->mEffect.
+
+static char const *const fields[][2] =
+{
+ { "attack", N_("Attack %+d") },
+ { "defense", N_("Defense %+d") },
+ { "hp", N_("HP %+d") },
+ { "mp", N_("MP %+d") }
+};
+
+void TaItemDB::load()
+{
+ if (mLoaded)
+ unload();
+
+ logger->log("Initializing TmwAthena item database...");
+
+ mUnknown = new TaItemInfo;
+ mUnknown->mName = _("Unknown item");
+ mUnknown->mDisplay = SpriteDisplay();
+ std::string errFile = paths.getStringValue("spriteErrorFile");
+ mUnknown->setSprite(errFile, GENDER_MALE);
+ mUnknown->setSprite(errFile, GENDER_FEMALE);
+
+ XML::Document doc(ITEMS_DB_FILE);
+ xmlNodePtr rootNode = doc.rootNode();
+
+ if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "items"))
+ {
+ logger->error("ItemDB: Error while loading " ITEMS_DB_FILE "!");
+ return;
+ }
+
+ for_each_xml_child_node(node, rootNode)
+ {
+ TaItemInfo *itemInfo = new TaItemInfo;
+
+ loadCommonRef(itemInfo, node);
+
+ // Everything not unusable or usable is equippable by the Ta type system.
+ itemInfo->mEquippable = itemInfo->mType != ITEM_UNUSABLE
+ && itemInfo->mType != ITEM_USABLE;
+
+ // Load nano description
+ std::vector<std::string> effect;
+ for (int i = 0; i < int(sizeof(fields) / sizeof(fields[0])); ++i)
+ {
+ int value = XML::getProperty(node, fields[i][0], 0);
+ if (!value)
+ continue;
+ effect.push_back(strprintf(gettext(fields[i][1]), value));
+ }
+ for (std::list<ItemStat>::iterator it = extraStats.begin();
+ it != extraStats.end(); it++)
+ {
+ int value = XML::getProperty(node, it->mTag.c_str(), 0);
+ if (!value)
+ continue;
+ effect.push_back(strprintf(it->mFormat.c_str(), value));
+ }
+ std::string temp = XML::getProperty(node, "effect", "");
+ if (!temp.empty())
+ effect.push_back(temp);
+
+ itemInfo->mEffect = effect;
+
+ checkItemInfo(itemInfo);
+
+ addItem(itemInfo);
+ }
+
+ checkHairWeaponsRacesSpecialIds();
+
+ mLoaded = true;
+}
+
+void TaItemDB::checkItemInfo(ItemInfo* itemInfo)
+{
+ ItemDB::checkItemInfo(itemInfo);
+
+ // Check for unusable items?
+ //checkParameter(id, itemInfo->mType, 0);
+}
+
+}; // namespace TmwAthena
+
+namespace ManaServ {
+
+static std::map<std::string, const char* > triggerTable;
+
+static void initTriggerTable()
+{
+ if (triggerTable.empty())
+ {
+ // FIXME: This should ideally be softcoded via XML or similar.
+ logger->log("Initializing ManaServ trigger table...");
+ triggerTable["existence"] = " when it is in the inventory";
+ triggerTable["activation"] = " upon activation";
+ triggerTable["equip"] = " upon successful equip";
+ triggerTable["leave-inventory"] = " when it leaves the inventory";
+ triggerTable["unequip"] = " when it is unequipped";
+ triggerTable["equip-change"] = " when it changes the way it is equipped";
+ }
+}
+
+void ManaServItemDB::load()
+{
+ if (mLoaded)
+ unload();
+
+ // Initialize the trigger table for effect descriptions
+ initTriggerTable();
+
+ logger->log("Initializing ManaServ item database...");
+
+ mUnknown = new ManaServItemInfo;
+ mUnknown->mName = _("Unknown item");
+ mUnknown->mDisplay = SpriteDisplay();
+ std::string errFile = paths.getStringValue("spriteErrorFile");
+ mUnknown->setSprite(errFile, GENDER_MALE);
+ mUnknown->setSprite(errFile, GENDER_FEMALE);
+
+ XML::Document doc(ITEMS_DB_FILE);
+ xmlNodePtr rootNode = doc.rootNode();
+
+ if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "items"))
+ {
+ logger->log("ItemDB: Error while loading " ITEMS_DB_FILE "!");
+ return;
+ }
+
+ for_each_xml_child_node(node, rootNode)
+ {
+ ManaServItemInfo *itemInfo = new ManaServItemInfo;
+
+ loadCommonRef(itemInfo, node);
+
+ // We default eqippable and activatable to false as their actual value will be set
+ // within the <equip> and <effect> sub-nodes..
+ itemInfo->mActivatable = false;
+ itemInfo->mEquippable = false;
+
+ // Load <equip>, and <effect> sub nodes.
+ std::vector<std::string> effect;
+ for_each_xml_child_node(itemChild, node)
+ {
+ if (xmlStrEqual(itemChild->name, BAD_CAST "equip"))
{
// The fact that there is a way to equip is enough.
// Discard any details, but mark the item as equippable.
@@ -195,7 +453,8 @@ void ItemDB::load()
{
std::string trigger = XML::getProperty(
itemChild, "trigger", "");
- if (trigger.empty()) {
+ if (trigger.empty())
+ {
logger->log("Found empty trigger effect label, skipping.");
continue;
}
@@ -203,22 +462,12 @@ void ItemDB::load()
if (trigger == "activation")
itemInfo->mActivatable = true;
- static std::map<std::string, const char* > triggerTable;
- if (triggerTable.empty())
- {
- // FIXME: This should ideally be softcoded via XML or similar.
- triggerTable["existence"] = " when it is in the inventory";
- triggerTable["activation"] = " upon activation";
- triggerTable["equip"] = " upon successful equip";
- triggerTable["leave-inventory"] = " when it leaves the inventory";
- triggerTable["unequip"] = " when it is unequipped";
- triggerTable["equip-change"] = " when it changes the way it is equipped";
- }
std::map<std::string, const char* >::const_iterator triggerLabel =
triggerTable.find(trigger);
if (triggerLabel == triggerTable.end())
{
- logger->log("Warning: unknown trigger %s in item %d!", trigger.c_str(), id);
+ logger->log("Warning: unknown trigger %s in item %d!",
+ trigger.c_str(), itemInfo->mId);
continue;
}
@@ -237,7 +486,7 @@ void ItemDB::load()
logger->log("Warning: incomplete modifier definition, skipping.");
continue;
}
- std::list<Stat>::const_iterator
+ std::list<ItemStat>::const_iterator
it = extraStats.begin(),
it_end = extraStats.end();
while (it != it_end && !(*it == attribute))
@@ -251,7 +500,7 @@ void ItemDB::load()
strprintf(strprintf(
duration ?
strprintf("%%s%%s. This effect lasts %d ticks.", duration).c_str()
- : "%s%s.", it->format.c_str(), triggerLabel->second).c_str(), value));
+ : "%s%s.", it->mFormat.c_str(), triggerLabel->second).c_str(), value));
}
else if (xmlStrEqual(effectChild->name, BAD_CAST "modifier"))
effect.push_back(strprintf("Provides an autoattack%s.",
@@ -264,159 +513,30 @@ void ItemDB::load()
(const char*)effectChild->xmlChildrenNode->content);
}
}
- }
+ // Set Item Type based on subnodes info
+ // TODO: Improve it once the itemTypes are loaded through xml
+ itemInfo->mType = ITEM_UNUSABLE;
+ if (itemInfo->mActivatable)
+ itemInfo->mType = ITEM_USABLE;
+ else if (itemInfo->mEquippable)
+ itemInfo->mType = ITEM_EQUIPMENT_TORSO;
+ } // end for_each_xml_child_node(itemChild, node)
itemInfo->mEffect = effect;
- itemInfo->mDisplay = display;
-
- mItemInfos[id] = itemInfo;
- if (!name.empty())
- {
- std::string temp = normalize(name);
-
- NamedItemInfos::const_iterator itr = mNamedItemInfos.find(temp);
- if (itr == mNamedItemInfos.end())
- {
- mNamedItemInfos[temp] = itemInfo;
- }
- else
- {
- logger->log("ItemDB: Duplicate name of item found item %d", id);
- }
- }
-
- if (!attackAction.empty())
- if (attackRange == 0)
- logger->log("ItemDB: Missing attack range from weapon %i!", id);
-
-#define CHECK_PARAM(param, error_value) \
- if (param == error_value) \
- logger->log("ItemDB: Missing " #param " attribute for item %i!",id)
-
- if (id >= 0)
- {
- CHECK_PARAM(name, "");
- CHECK_PARAM(description, "");
- CHECK_PARAM(image, "");
- }
- // CHECK_PARAM(effect, "");
- // CHECK_PARAM(type, 0);
- // CHECK_PARAM(weight, 0);
- // CHECK_PARAM(slot, 0);
+ checkItemInfo(itemInfo);
-#undef CHECK_PARAM
+ addItem(itemInfo);
}
mLoaded = true;
}
-void ItemDB::unload()
-{
- logger->log("Unloading item database...");
-
- delete mUnknown;
- mUnknown = NULL;
-
- delete_all(mItemInfos);
- mItemInfos.clear();
- mNamedItemInfos.clear();
- mLoaded = false;
-}
-
-bool ItemDB::exists(int id)
-{
- assert(mLoaded);
-
- ItemInfos::const_iterator i = mItemInfos.find(id);
-
- return i != mItemInfos.end();
-}
-
-const ItemInfo &ItemDB::get(int id)
-{
- assert(mLoaded);
-
- ItemInfos::const_iterator i = mItemInfos.find(id);
-
- if (i == mItemInfos.end())
- {
- logger->log("ItemDB: Warning, unknown item ID# %d", id);
- return *mUnknown;
- }
-
- return *(i->second);
-}
-
-const ItemInfo &ItemDB::get(const std::string &name)
+void ManaServItemDB::checkItemInfo(ItemInfo* itemInfo)
{
- assert(mLoaded);
-
- NamedItemInfos::const_iterator i = mNamedItemInfos.find(normalize(name));
+ ItemDB::checkItemInfo(itemInfo);
- if (i == mNamedItemInfos.end())
- {
- if (!name.empty())
- {
- logger->log("ItemDB: Warning, unknown item name \"%s\"",
- name.c_str());
- }
- return *mUnknown;
- }
-
- return *(i->second);
+ // Add specific Manaserv checks here
}
-void ItemDB::loadSpriteRef(ItemInfo *itemInfo, xmlNodePtr node)
-{
- std::string gender = XML::getProperty(node, "gender", "unisex");
- std::string filename = (const char*) node->xmlChildrenNode->content;
-
- if (gender == "male" || gender == "unisex")
- {
- itemInfo->setSprite(filename, GENDER_MALE);
- }
- if (gender == "female" || gender == "unisex")
- {
- itemInfo->setSprite(filename, GENDER_FEMALE);
- }
-}
-
-void ItemDB::loadSoundRef(ItemInfo *itemInfo, xmlNodePtr node)
-{
- std::string event = XML::getProperty(node, "event", "");
- std::string filename = (const char*) node->xmlChildrenNode->content;
-
- if (event == "hit")
- {
- itemInfo->addSound(EQUIP_EVENT_HIT, filename);
- }
- else if (event == "strike")
- {
- itemInfo->addSound(EQUIP_EVENT_STRIKE, filename);
- }
- else
- {
- logger->log("ItemDB: Ignoring unknown sound event '%s'",
- event.c_str());
- }
-}
-
-void ItemDB::loadFloorSprite(SpriteDisplay *display, xmlNodePtr floorNode)
-{
- for_each_xml_child_node(spriteNode, floorNode)
- {
- if (xmlStrEqual(spriteNode->name, BAD_CAST "sprite"))
- {
- SpriteReference *currentSprite = new SpriteReference;
- currentSprite->sprite = (const char*)spriteNode->xmlChildrenNode->content;
- currentSprite->variant = XML::getProperty(spriteNode, "variant", 0);
- display->sprites.push_back(currentSprite);
- }
- else if (xmlStrEqual(spriteNode->name, BAD_CAST "particlefx"))
- {
- std::string particlefx = (const char*)spriteNode->xmlChildrenNode->content;
- display->particles.push_back(particlefx);
- }
- }
-}
+}; // namespace ManaServ