From de068425200d5254181c6806ebeb4a72d68895ec Mon Sep 17 00:00:00 2001 From: David Athay Date: Wed, 14 Jan 2009 14:11:35 +0000 Subject: Added linking to item's just using [Item Name] in chat --- src/gui/chat.cpp | 21 +++++++++++++++++++++ src/resources/itemdb.cpp | 32 ++++++++++++++++++++++++++++++++ src/resources/itemdb.h | 3 +++ src/resources/iteminfo.h | 7 +++++++ 4 files changed, 63 insertions(+) (limited to 'src') diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index 3e9b292e..84666df4 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -43,8 +43,12 @@ #include "../net/messageout.h" #include "../net/protocol.h" +#include "../resources/iteminfo.h" +#include "../resources/itemdb.h" + #include "../utils/gettext.h" #include "../utils/strprintf.h" +#include "../utils/tostring.h" #include "../utils/trim.h" ChatWindow::ChatWindow(Network * network): @@ -352,6 +356,23 @@ void ChatWindow::chatSend(const std::string &nick, std::string msg) return; } + // check for item link + std::string::size_type start = msg.find('['); + if (start != std::string::npos) + { + std::string::size_type end = msg.find(']', start); + if (end != std::string::npos) + { + std::string temp = msg.substr(start+1, end-1); + ItemInfo itemInfo = ItemDB::get(temp); + msg.insert(end, "@@"); + msg.insert(start+1, "|"); + msg.insert(start+1, toString(itemInfo.getId())); + msg.insert(start+1, "@@"); + + } + } + // Prepare ordinary message if (msg.substr(0, 1) != "/") { diff --git a/src/resources/itemdb.cpp b/src/resources/itemdb.cpp index 7091abed..53e7530b 100644 --- a/src/resources/itemdb.cpp +++ b/src/resources/itemdb.cpp @@ -36,6 +36,7 @@ namespace { ItemDB::ItemInfos mItemInfos; + ItemDB::NamedItemInfos mNamedItemInfos; ItemInfo *mUnknown; bool mLoaded = false; } @@ -95,6 +96,7 @@ void ItemDB::load() if (id) { ItemInfo *itemInfo = new ItemInfo; + itemInfo->setId(id); itemInfo->setImageName(image); itemInfo->setName(name.empty() ? _("Unnamed") : name); itemInfo->setDescription(description); @@ -117,6 +119,19 @@ void ItemDB::load() } mItemInfos[id] = itemInfo; + if (!name.empty()) + { + NamedItemInfoIterator itr = mNamedItemInfos.find(name); + if (itr == mNamedItemInfos.end()) + { + mNamedItemInfos[name] = itemInfo; + } + else + { + logger->log(_("ItemDB: Duplicate name of item found item %d"), + id); + } + } } #define CHECK_PARAM(param, error_value) \ @@ -166,6 +181,23 @@ const ItemInfo& ItemDB::get(int id) } } +const ItemInfo& ItemDB::get(const std::string &name) +{ + assert(mLoaded && !name.empty()); + + NamedItemInfoIterator i = mNamedItemInfos.find(name); + + if (i == mNamedItemInfos.end()) + { + logger->log("ItemDB: Error, unknown item name %s", name.c_str()); + return *mUnknown; + } + else + { + return *(i->second); + } +} + void loadSpriteRef(ItemInfo *itemInfo, xmlNodePtr node) { std::string gender = XML::getProperty(node, "gender", "unisex"); diff --git a/src/resources/itemdb.h b/src/resources/itemdb.h index 9b661a60..2cc23ec9 100644 --- a/src/resources/itemdb.h +++ b/src/resources/itemdb.h @@ -42,10 +42,13 @@ namespace ItemDB void unload(); const ItemInfo& get(int id); + const ItemInfo& get(const std::string &name); // Items database typedef std::map ItemInfos; + typedef std::map NamedItemInfos; typedef ItemInfos::iterator ItemInfoIterator; + typedef NamedItemInfos::iterator NamedItemInfoIterator; } #endif diff --git a/src/resources/iteminfo.h b/src/resources/iteminfo.h index b7729d2c..773fd17c 100644 --- a/src/resources/iteminfo.h +++ b/src/resources/iteminfo.h @@ -54,6 +54,12 @@ class ItemInfo { } + void setId(int id) + { mId = id; } + + int getId() const + { return mId; } + void setName(const std::string &name) { mName = name; } @@ -115,6 +121,7 @@ class ItemInfo char mType; /**< Item type. */ short mWeight; /**< Weight in grams. */ int mView; /**< Item ID of how this item looks. */ + int mId; /**< Item ID */ // Equipment related members SpriteAction mAttackType; /**< Attack type, in case of weapon. */ -- cgit v1.2.3-60-g2f50