diff options
author | Philipp Sehmisch <mana@crushnet.org> | 2011-03-15 20:27:33 +0100 |
---|---|---|
committer | Philipp Sehmisch <mana@crushnet.org> | 2011-03-16 08:36:52 +0100 |
commit | b6e7feedc82c3c549af0d3cd53be1a981945f42d (patch) | |
tree | 5890776fa8f1c70e7d94ac1e1fed6c554f269371 | |
parent | 24f2b307f089558276d1d526f1288d229af11678 (diff) | |
download | manaserv-b6e7feedc82c3c549af0d3cd53be1a981945f42d.tar.gz manaserv-b6e7feedc82c3c549af0d3cd53be1a981945f42d.tar.bz2 manaserv-b6e7feedc82c3c549af0d3cd53be1a981945f42d.tar.xz manaserv-b6e7feedc82c3c549af0d3cd53be1a981945f42d.zip |
Allowed monster names in @spawn command
I rewrote the @spawn command to allow two new things:
1. The monster ID can be replaced with the name of the monster.
2. The amount of monsters can be omitted. In that case a single monster
is spawned.
Reviewed by: Jaxad and Thorbjorn
-rw-r--r-- | src/game-server/commandhandler.cpp | 40 | ||||
-rw-r--r-- | src/game-server/monster.h | 15 | ||||
-rw-r--r-- | src/game-server/monstermanager.cpp | 21 | ||||
-rw-r--r-- | src/game-server/monstermanager.h | 8 |
4 files changed, 67 insertions, 17 deletions
diff --git a/src/game-server/commandhandler.cpp b/src/game-server/commandhandler.cpp index f01e8540..bf43d6f4 100644 --- a/src/game-server/commandhandler.cpp +++ b/src/game-server/commandhandler.cpp @@ -633,44 +633,50 @@ static void handleSpawn(Character *player, std::string &args) MonsterClass *mc; MapComposite *map = player->getMap(); const Point &pos = player->getPosition(); - int value, id; + int value = 0; // get the arguments std::string monsterclass = getArgument(args); std::string valuestr = getArgument(args); // check all arguments are there - if (monsterclass == "" || valuestr == "") + if (monsterclass == "") { say("Invalid amount of arguments given.", player); - say("Usage: @spawn <monsterID> <number>", player); + say("Usage: @spawn <monster> [number]", player); return; } - // check they are really numbers - if (!utils::isNumeric(monsterclass) || !utils::isNumeric(valuestr)) + // identify the monster type + if (utils::isNumeric(monsterclass)) { - say("Invalid arguments", player); - return; + int id = utils::stringToInt(monsterclass); + mc = monsterManager->getMonster(id); + } + else + { + mc = monsterManager->getMonsterByName(monsterclass); } - - // put the monster class id into an integer - id = utils::stringToInt(monsterclass); - // check for valid monster - mc = monsterManager->getMonster(id); if (!mc) { say("Invalid monster", player); return; } - // put the amount 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 monsters", player); return; } diff --git a/src/game-server/monster.h b/src/game-server/monster.h index ff467167..78a4bddc 100644 --- a/src/game-server/monster.h +++ b/src/game-server/monster.h @@ -70,6 +70,7 @@ class MonsterClass public: MonsterClass(int id): mId(id), + mName("unnamed"), mSpeed(1), mSize(16), mExp(-1), @@ -89,6 +90,18 @@ class MonsterClass { return mId; } /** + * Returns the name of the monster type + */ + const std::string &getName() const + { return mName; } + + /** + * Sets the name of the monster type + */ + void setName(const std::string &name) + { mName = name; } + + /** * Sets monster drops. These are the items the monster drops when it * dies. */ @@ -186,6 +199,8 @@ class MonsterClass private: unsigned short mId; + std::string mName; + MonsterDrops mDrops; std::map<int, double> mAttributes; /**< Base attributes of the monster. */ float mSpeed; /**< The monster class speed in tiles per second */ diff --git a/src/game-server/monstermanager.cpp b/src/game-server/monstermanager.cpp index ac5b07ff..0810164f 100644 --- a/src/game-server/monstermanager.cpp +++ b/src/game-server/monstermanager.cpp @@ -25,6 +25,7 @@ #include "game-server/itemmanager.h" #include "game-server/monster.h" #include "utils/logger.h" +#include "utils/string.h" #include "utils/xml.h" #define MAX_MUTATION 99 @@ -106,6 +107,7 @@ void MonsterManager::reload() { monster = i->second; } + monster->setName(name); MonsterDrops drops; bool attributesSet = false; @@ -344,6 +346,25 @@ void MonsterManager::deinitialize() mMonsterClasses.clear(); } +MonsterClass *MonsterManager::getMonsterByName(std::string name) const +{ + // this function is not very fast but neither does it need to be + // because it is only used by the @spawn command. It would be + // possible to speed it up by caching the lowercase_name/MonsterClass + // mapping in a std::map during MonsterManager::reload, should the + // need arise. + name = utils::toLower(name); + for (MonsterClasses::const_iterator i = mMonsterClasses.begin(), + i_end = mMonsterClasses.end(); i != i_end; ++i) + { + if(utils::toLower(i->second->getName()) == name) + { + return i->second; + } + } + return 0; +} + MonsterClass *MonsterManager::getMonster(int id) { MonsterClasses::const_iterator i = mMonsterClasses.find(id); diff --git a/src/game-server/monstermanager.h b/src/game-server/monstermanager.h index 36d11e35..acdb9cad 100644 --- a/src/game-server/monstermanager.h +++ b/src/game-server/monstermanager.h @@ -49,6 +49,14 @@ class MonsterManager */ MonsterClass *getMonster(int id); + /** + * Gets the first monster type with a specific name. + * (comparison is case-insensitive). + * Returns null when there is no monster with such + * a name. + */ + MonsterClass *getMonsterByName(std::string name) const; + private: typedef std::map< int, MonsterClass * > MonsterClasses; |