diff options
Diffstat (limited to 'src/resources/itemdb.cpp')
-rw-r--r-- | src/resources/itemdb.cpp | 571 |
1 files changed, 387 insertions, 184 deletions
diff --git a/src/resources/itemdb.cpp b/src/resources/itemdb.cpp index cb91e8d3..2271abef 100644 --- a/src/resources/itemdb.cpp +++ b/src/resources/itemdb.cpp @@ -23,6 +23,8 @@ #include "log.h" +#include "net/net.h" + #include "resources/iteminfo.h" #include "resources/resourcemanager.h" @@ -36,29 +38,7 @@ #include <cassert> -namespace -{ - ItemDB::ItemInfos mItemInfos; - ItemDB::NamedItemInfos mNamedItemInfos; - ItemInfo *mUnknown; - bool mLoaded = false; -} - -// Forward declarations -static void loadSpriteRef(ItemInfo *itemInfo, xmlNodePtr node); -static void loadSoundRef(ItemInfo *itemInfo, xmlNodePtr node); - -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<ItemDB::Stat> &stats) +void setStatsList(const std::list<ItemStat> &stats) { extraStats = stats; } @@ -84,119 +64,172 @@ static ItemType itemTypeFromString(const std::string &name, int id = 0) else return ITEM_UNUSABLE; } -static WeaponType weaponTypeFromString(const std::string &name, int id = 0) +/* + * Common itemDB functions + */ + +bool ItemDB::exists(int id) { - if (name=="knife") return WPNTYPE_KNIFE; - else if (name=="sword") return WPNTYPE_SWORD; - else if (name=="polearm") return WPNTYPE_POLEARM; - else if (name=="staff") return WPNTYPE_STAFF; - else if (name=="whip") return WPNTYPE_WHIP; - else if (name=="bow") return WPNTYPE_BOW; - else if (name=="shooting") return WPNTYPE_SHOOTING; - else if (name=="mace") return WPNTYPE_MACE; - else if (name=="axe") return WPNTYPE_AXE; - else if (name=="thrown") return WPNTYPE_THROWN; - - else return WPNTYPE_NONE; + assert(mLoaded); + + ItemInfos::const_iterator i = mItemInfos.find(id); + + return i != mItemInfos.end(); } -static std::string normalized(const std::string &name) +const ItemInfo &ItemDB::get(int id) { - std::string normalized = name; - return toLower(trim(normalized));; + 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); } -void ItemDB::load() +const ItemInfo &ItemDB::get(const std::string &name) { - if (mLoaded) - unload(); + assert(mLoaded); - logger->log("Initializing item database..."); + NamedItemInfos::const_iterator i = mNamedItemInfos.find(normalize(name)); - mUnknown = new ItemInfo; - mUnknown->setName(_("Unknown item")); - mUnknown->setImageName(""); - std::string errFile = paths.getValue("spriteErrorFile", "error.xml"); - mUnknown->setSprite(errFile, GENDER_MALE); - mUnknown->setSprite(errFile, GENDER_FEMALE); + if (i == mNamedItemInfos.end()) + { + if (!name.empty()) + { + logger->log("ItemDB: Warning, unknown item name \"%s\"", + name.c_str()); + } + return *mUnknown; + } - XML::Document doc("items.xml"); - xmlNodePtr rootNode = doc.rootNode(); + return *(i->second); +} - if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "items")) +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") { - logger->error("ItemDB: Error while loading items.xml!"); + 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()); + } +} - for_each_xml_child_node(node, rootNode) +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 == 0) + if (!id) { - logger->log("ItemDB: Invalid or missing item ID in items.xml!"); - continue; + logger->log("ItemDB: Invalid or missing item Id in " + ITEMS_DB_FILE "!"); + 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", ""); std::string image = XML::getProperty(node, "image", ""); std::string description = XML::getProperty(node, "description", ""); - int weaponType = weaponTypeFromString(XML::getProperty(node, "weapon-type", "")); + std::string attackAction = XML::getProperty(node, "attack-action", ""); int attackRange = XML::getProperty(node, "attack-range", 0); std::string missileParticle = XML::getProperty(node, "missile-particle", ""); - ItemInfo *itemInfo = new ItemInfo; - itemInfo->setId(id); - itemInfo->setImageName(image); - itemInfo->setName(name.empty() ? _("unnamed") : name); - itemInfo->setDescription(description); - itemInfo->setType(itemTypeFromString(typeStr)); - itemInfo->setView(view); - itemInfo->setWeight(weight); - itemInfo->setWeaponType(weaponType); - itemInfo->setAttackRange(attackRange); - itemInfo->setMissileParticle(missileParticle); + // Load Ta Item Type + std::string typeStr = XML::getProperty(node, "type", "other"); + itemInfo->mType = itemTypeFromString(typeStr); - 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; - if (!effect.empty()) effect += " / "; - effect += 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; - if (!effect.empty()) effect += " / "; - effect += strprintf(it->format.c_str(), value); - } - std::string temp = XML::getProperty(node, "effect", ""); - if (!effect.empty() && !temp.empty()) - effect += " / "; - effect += temp; - itemInfo->setEffect(effect); + int weight = XML::getProperty(node, "weight", 0); + itemInfo->mWeight = weight > 0 ? weight : 0; + + SpriteDisplay display; + display.image = image; + + itemInfo->mId = id; + itemInfo->mName = name; + itemInfo->mDescription = description; + itemInfo->mView = view; + itemInfo->mWeight = weight; + itemInfo->setAttackAction(attackAction); + itemInfo->mAttackRange = attackRange; + itemInfo->setMissileParticle(missileParticle); + // Load <sprite>, <sound>, and <floor> for_each_xml_child_node(itemChild, node) { if (xmlStrEqual(itemChild->name, BAD_CAST "sprite")) { std::string attackParticle = XML::getProperty( itemChild, "particle-effect", ""); - itemInfo->setParticleEffect(attackParticle); + itemInfo->mParticle = attackParticle; loadSpriteRef(itemInfo, itemChild); } @@ -204,136 +237,306 @@ void ItemDB::load() { loadSoundRef(itemInfo, itemChild); } - } - - mItemInfos[id] = itemInfo; - if (!name.empty()) - { - std::string temp = normalized(name); - - NamedItemInfos::const_iterator itr = mNamedItemInfos.find(temp); - if (itr == mNamedItemInfos.end()) - { - mNamedItemInfos[temp] = itemInfo; - } - else + else if (xmlStrEqual(itemChild->name, BAD_CAST "floor")) { - logger->log("ItemDB: Duplicate name of item found item %d", id); + loadFloorSprite(&display, itemChild); } + } - if (weaponType > 0) - if (attackRange == 0) - logger->log("ItemDB: Missing attack range from weapon %i!", id); + // If the item has got a floor image, we bind the good reference. + itemInfo->mDisplay = display; +} -#define CHECK_PARAM(param, error_value) \ - if (param == error_value) \ - logger->log("ItemDB: Missing " #param " attribute for item %i!",id) +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); - 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); + 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); -#undef CHECK_PARAM } - - mLoaded = true; } -void ItemDB::unload() +template <class T> +static void checkParameter(int id, const T param, const T errorValue) { - logger->log("Unloading item database..."); + if (param == errorValue) + { + std::stringstream errMsg; + errMsg << "ItemDB: Missing " << param << " attribute for item id " + << id << "!"; + logger->log(errMsg.str().c_str()); + } +} - delete mUnknown; - mUnknown = NULL; +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); - delete_all(mItemInfos); - mItemInfos.clear(); - mNamedItemInfos.clear(); - mLoaded = false; + 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); + } } -bool ItemDB::exists(int id) -{ - assert(mLoaded); +namespace TmwAthena { - ItemInfos::const_iterator i = mItemInfos.find(id); +// Description fields used by TaItemDB *itemInfo->mEffect. - return i != mItemInfos.end(); -} +static char const *const fields[][2] = +{ + { "attack", N_("Attack %+d") }, + { "defense", N_("Defense %+d") }, + { "hp", N_("HP %+d") }, + { "mp", N_("MP %+d") } +}; -const ItemInfo &ItemDB::get(int id) +void TaItemDB::load() { - assert(mLoaded); + if (mLoaded) + unload(); - ItemInfos::const_iterator i = mItemInfos.find(id); + logger->log("Initializing TmwAthena item database..."); - if (i == mItemInfos.end()) + 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->log("ItemDB: Warning, unknown item ID# %d", id); - return *mUnknown; + logger->error("ItemDB: Error while loading " ITEMS_DB_FILE "!"); + return; } - return *(i->second); -} + for_each_xml_child_node(node, rootNode) + { + TaItemInfo *itemInfo = new TaItemInfo; -const ItemInfo &ItemDB::get(const std::string &name) -{ - assert(mLoaded); + loadCommonRef(itemInfo, node); - NamedItemInfos::const_iterator i = mNamedItemInfos.find(normalized(name)); + // Everything not unusable or usable is equippable by the Ta type system. + itemInfo->mEquippable = itemInfo->mType != ITEM_UNUSABLE + && itemInfo->mType != ITEM_USABLE; - if (i == mNamedItemInfos.end()) - { - if (!name.empty()) + // Load nano description + std::vector<std::string> effect; + for (int i = 0; i < int(sizeof(fields) / sizeof(fields[0])); ++i) { - logger->log("ItemDB: Warning, unknown item name \"%s\"", - name.c_str()); + int value = XML::getProperty(node, fields[i][0], 0); + if (!value) + continue; + effect.push_back(strprintf(gettext(fields[i][1]), value)); } - return *mUnknown; + 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); } - return *(i->second); + checkHairWeaponsRacesSpecialIds(); + + mLoaded = true; } -void loadSpriteRef(ItemInfo *itemInfo, xmlNodePtr node) +void TaItemDB::checkItemInfo(ItemInfo* itemInfo) { - std::string gender = XML::getProperty(node, "gender", "unisex"); - std::string filename = (const char*) node->xmlChildrenNode->content; + ItemDB::checkItemInfo(itemInfo); - if (gender == "male" || gender == "unisex") - { - itemInfo->setSprite(filename, GENDER_MALE); - } - if (gender == "female" || gender == "unisex") + // 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()) { - itemInfo->setSprite(filename, GENDER_FEMALE); + // 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 loadSoundRef(ItemInfo *itemInfo, xmlNodePtr node) +void ManaServItemDB::load() { - std::string event = XML::getProperty(node, "event", ""); - std::string filename = (const char*) node->xmlChildrenNode->content; + if (mLoaded) + unload(); - if (event == "hit") - { - itemInfo->addSound(EQUIP_EVENT_HIT, filename); - } - else if (event == "strike") + // 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")) { - itemInfo->addSound(EQUIP_EVENT_STRIKE, filename); + logger->log("ItemDB: Error while loading " ITEMS_DB_FILE "!"); + return; } - else + + for_each_xml_child_node(node, rootNode) { - logger->log("ItemDB: Ignoring unknown sound event '%s'", - event.c_str()); + 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. + itemInfo->mEquippable = true; + } + else if (xmlStrEqual(itemChild->name, BAD_CAST "effect")) + { + std::string trigger = XML::getProperty( + itemChild, "trigger", ""); + if (trigger.empty()) + { + logger->log("Found empty trigger effect label, skipping."); + continue; + } + + if (trigger == "activation") + itemInfo->mActivatable = true; + + 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(), itemInfo->mId); + continue; + } + + for_each_xml_child_node(effectChild, itemChild) + { + if (xmlStrEqual(effectChild->name, BAD_CAST "modifier")) + { + std::string attribute = XML::getProperty( + effectChild, "attribute", ""); + double value = XML::getFloatProperty( + effectChild, "value", 0.0); + int duration = XML::getProperty( + effectChild, "duration", 0); + if (attribute.empty() || !value) + { + logger->log("Warning: incomplete modifier definition, skipping."); + continue; + } + std::list<ItemStat>::const_iterator + it = extraStats.begin(), + it_end = extraStats.end(); + while (it != it_end && !(*it == attribute)) + ++it; + if (it == extraStats.end()) + { + logger->log("Warning: unknown modifier tag %s, skipping.", attribute.c_str()); + continue; + } + effect.push_back( + strprintf(strprintf( + duration ? + strprintf("%%s%%s. This effect lasts %d ticks.", duration).c_str() + : "%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.", + triggerLabel->second)); + else if (xmlStrEqual(effectChild->name, BAD_CAST "consumes")) + effect.push_back(strprintf("This will be consumed%s.", + triggerLabel->second)); + else if (xmlStrEqual(effectChild->name, BAD_CAST "label")) + effect.push_back( + (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; + + checkItemInfo(itemInfo); + + addItem(itemInfo); } + + mLoaded = true; +} + +void ManaServItemDB::checkItemInfo(ItemInfo* itemInfo) +{ + ItemDB::checkItemInfo(itemInfo); + + // Add specific Manaserv checks here } + +}; // namespace ManaServ |