summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPhilipp Sehmisch <mana@crushnet.org>2011-03-15 20:51:38 +0100
committerPhilipp Sehmisch <mana@crushnet.org>2011-03-16 08:36:52 +0100
commit05ad0a28aa4bada4569417c0d292d6960a8af5a2 (patch)
treea618fb12b92a575f50a942746880c65b80f2cf49 /src
parentb6e7feedc82c3c549af0d3cd53be1a981945f42d (diff)
downloadmanaserv-05ad0a28aa4bada4569417c0d292d6960a8af5a2.tar.gz
manaserv-05ad0a28aa4bada4569417c0d292d6960a8af5a2.tar.bz2
manaserv-05ad0a28aa4bada4569417c0d292d6960a8af5a2.tar.xz
manaserv-05ad0a28aa4bada4569417c0d292d6960a8af5a2.zip
Allowed item names instead of IDs in @commands
The @drop and @item commands can now accept an item name instead of an item ID. In addition the amount can be omitted to create a single item.
Diffstat (limited to 'src')
-rw-r--r--src/game-server/commandhandler.cpp85
-rw-r--r--src/game-server/item.h14
-rw-r--r--src/game-server/itemmanager.cpp18
-rw-r--r--src/game-server/itemmanager.h8
4 files changed, 84 insertions, 41 deletions
diff --git a/src/game-server/commandhandler.cpp b/src/game-server/commandhandler.cpp
index bf43d6f4..3e385360 100644
--- a/src/game-server/commandhandler.cpp
+++ b/src/game-server/commandhandler.cpp
@@ -446,8 +446,7 @@ static void handleItem(Character *player, std::string &args)
{
Character *other;
ItemClass *ic;
- int value;
- int id;
+ int value = 0;
// get arguments
std::string character = getArgument(args);
@@ -455,10 +454,10 @@ static void handleItem(Character *player, std::string &args)
std::string valuestr = getArgument(args);
// check all arguments are there
- if (character == "" || itemclass == "" || valuestr == "")
+ if (character == "" || itemclass == "")
{
say("Invalid number of arguments given.", player);
- say("Usage: @item <character> <itemID> <amount>", player);
+ say("Usage: @item <character> <item> [amount]", player);
return;
}
@@ -478,36 +477,35 @@ static void handleItem(Character *player, std::string &args)
}
}
- // check we have a valid item
- if (!utils::isNumeric(itemclass))
+ // identify the item type
+ if (utils::isNumeric(itemclass))
{
- say("Invalid item", player);
- return;
+ int id = utils::stringToInt(itemclass);
+ ic = itemManager->getItem(id);
+ }
+ else
+ {
+ ic = itemManager->getItemByName(itemclass);
}
-
- // put the itemclass id into an integer
- id = utils::stringToInt(itemclass);
-
- // check for valid item class
- ic = itemManager->getItem(id);
-
if (!ic)
{
say("Invalid item", player);
return;
}
- if (!utils::isNumeric(valuestr))
+ //identify the amount
+ if (valuestr == "")
{
- say("Invalid value", player);
- return;
+ value = 1;
}
-
- value = utils::stringToInt(valuestr);
-
- if (value < 0)
+ else if (utils::isNumeric(valuestr))
{
- say("Invalid amount", player);
+ value = utils::stringToInt(valuestr);
+ }
+ // check for valid amount
+ if (value <= 0)
+ {
+ say("Invalid number of items", player);
return;
}
@@ -523,44 +521,49 @@ static void handleItem(Character *player, std::string &args)
static void handleDrop(Character *player, std::string &args)
{
ItemClass *ic;
- int value, id;
+ int value = 0;
// get arguments
std::string itemclass = getArgument(args);
std::string valuestr = getArgument(args);
// check all arguments are there
- if (itemclass == "" || valuestr == "")
+ if (itemclass == "" )
{
say("Invalid number of arguments given.", player);
- say("Usage: @drop <itemID> <amount]>", player);
+ say("Usage: @drop <item> [amount]", player);
return;
}
- // check that itemclass id and value are really integers
- if (!utils::isNumeric(itemclass) || !utils::isNumeric(valuestr))
+ // identify the item type
+ if (utils::isNumeric(itemclass))
{
- say("Invalid arguments passed.", player);
- return;
+ int id = utils::stringToInt(itemclass);
+ ic = itemManager->getItem(id);
+ }
+ else
+ {
+ ic = itemManager->getItemByName(itemclass);
}
-
- // put the item class id into an integer
- id = utils::stringToInt(itemclass);
-
- // check for valid item
- ic = itemManager->getItem(id);
if (!ic)
{
say("Invalid item", player);
return;
}
- // put the value into an integer
- value = utils::stringToInt(valuestr);
-
- if (value < 0)
+ //identify the amount
+ if (valuestr == "")
{
- say("Invalid amount", player);
+ value = 1;
+ }
+ else if (utils::isNumeric(valuestr))
+ {
+ value = utils::stringToInt(valuestr);
+ }
+ // check for valid amount
+ if (value <= 0)
+ {
+ say("Invalid number of items", player);
return;
}
diff --git a/src/game-server/item.h b/src/game-server/item.h
index 50619536..f03e951a 100644
--- a/src/game-server/item.h
+++ b/src/game-server/item.h
@@ -149,6 +149,7 @@ class ItemClass
public:
ItemClass(int id, unsigned int maxperslot)
: mDatabaseID(id)
+ , mName("unnamed")
, mSpriteID(0)
, mCost(0)
, mMaxPerSlot(maxperslot)
@@ -157,6 +158,18 @@ class ItemClass
~ItemClass() { resetEffects(); }
/**
+ * Returns the name of the item type
+ */
+ const std::string &getName() const
+ { return mName; }
+
+ /**
+ * Sets the name of the item type
+ */
+ void setName(const std::string &name)
+ { mName = name; }
+
+ /**
* Applies the modifiers of an item to a given user.
* @return true if item should be removed.
*/
@@ -232,6 +245,7 @@ class ItemClass
}
unsigned short mDatabaseID; /**< Item reference information */
+ std::string mName; /**< name used to identify the item class */
/** The sprite that should be shown to the character */
unsigned short mSpriteID;
unsigned short mCost; /**< Unit cost the item. */
diff --git a/src/game-server/itemmanager.cpp b/src/game-server/itemmanager.cpp
index 72f8acfe..33bced02 100644
--- a/src/game-server/itemmanager.cpp
+++ b/src/game-server/itemmanager.cpp
@@ -27,6 +27,7 @@
#include "game-server/skillmanager.h"
#include "scripting/script.h"
#include "utils/logger.h"
+#include "utils/string.h"
#include "utils/xml.h"
#include <map>
@@ -162,6 +163,9 @@ void ItemManager::reload()
item = i->second;
}
+ std::string name = XML::getProperty(node, "name", "unnamed");
+ item->setName(name);
+
int value = XML::getProperty(node, "value", 0);
// Should have multiple value definitions for multiple currencies?
item->mCost = value;
@@ -334,6 +338,20 @@ ItemClass *ItemManager::getItem(int itemId) const
return i != itemClasses.end() ? i->second : NULL;
}
+ItemClass *ItemManager::getItemByName(std::string name) const
+{
+ name = utils::toLower(name);
+ for (ItemClasses::const_iterator i = itemClasses.begin(),
+ i_end = itemClasses.end(); i != i_end; ++i)
+ {
+ if(utils::toLower(i->second->getName()) == name)
+ {
+ return i->second;
+ }
+ }
+ return 0;
+}
+
unsigned int ItemManager::getDatabaseVersion() const
{
return mItemDatabaseVersion;
diff --git a/src/game-server/itemmanager.h b/src/game-server/itemmanager.h
index ea0641c7..00739785 100644
--- a/src/game-server/itemmanager.h
+++ b/src/game-server/itemmanager.h
@@ -55,6 +55,14 @@ class ItemManager
ItemClass *getItem(int itemId) const;
/**
+ * Gets the first item type with a specific name.
+ * (comparison is case-insensitive).
+ * Returns null when there is no item with such
+ * a name.
+ */
+ ItemClass *getItemByName(std::string name) const;
+
+ /**
* Gets the version of the loaded item database.
*/
unsigned int getDatabaseVersion() const;