diff options
author | Andrei Karas <akaras@inbox.ru> | 2011-12-08 21:50:36 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2011-12-08 21:50:36 +0300 |
commit | 4a4d6bd39b88933f5db4b632b28e1444062ad25a (patch) | |
tree | 1035ec196f0c1c09c4db8ac519357148c747fa09 /src/commandhandler.cpp | |
parent | 3e7208efe20dfdb5578718c2762ee196f73f8168 (diff) | |
download | manaplus-4a4d6bd39b88933f5db4b632b28e1444062ad25a.tar.gz manaplus-4a4d6bd39b88933f5db4b632b28e1444062ad25a.tar.bz2 manaplus-4a4d6bd39b88933f5db4b632b28e1444062ad25a.tar.xz manaplus-4a4d6bd39b88933f5db4b632b28e1444062ad25a.zip |
Add variables for any chat text/commands.
<PLAYER> - target or nearest player nick.
<MONSTER> - target or nearest monster name.
<PEOPLE> - comma separated list of visible players (except self).
<PARTY> - all party members (except self).
Example usage:
type in chat: hello <PEOPLE>.
Diffstat (limited to 'src/commandhandler.cpp')
-rw-r--r-- | src/commandhandler.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/commandhandler.cpp b/src/commandhandler.cpp index d04fda150..862ac56b4 100644 --- a/src/commandhandler.cpp +++ b/src/commandhandler.cpp @@ -32,6 +32,7 @@ #include "localplayer.h" #include "logger.h" #include "main.h" +#include "party.h" #include "gui/chatwindow.h" #include "gui/helpwindow.h" @@ -1194,3 +1195,82 @@ void CommandHandler::handleDump(const std::string &args A_UNUSED, { } #endif + +void CommandHandler::replaceVars(std::string &str) +{ + if (!player_node || !actorSpriteManager) + return; + + if (str.find("<PLAYER>") != std::string::npos) + { + Being *target = player_node->getTarget(); + if (!target || target->getType() != ActorSprite::PLAYER) + { + target = actorSpriteManager->findNearestLivingBeing( + player_node, 20, ActorSprite::PLAYER); + } + if (target) + replaceAll(str, "<PLAYER>", target->getName()); + else + replaceAll(str, "<PLAYER>", ""); + } + if (str.find("<MONSTER>") != std::string::npos) + { + Being *target = player_node->getTarget(); + if (!target || target->getType() != ActorSprite::MONSTER) + { + target = actorSpriteManager->findNearestLivingBeing( + player_node, 20, ActorSprite::MONSTER); + } + if (target) + replaceAll(str, "<MONSTER>", target->getName()); + else + replaceAll(str, "<MONSTER>", ""); + } + if (str.find("<PEOPLE>") != std::string::npos) + { + std::vector<std::string> names; + std::string newStr = ""; + actorSpriteManager->getPlayerNames(names, false); + std::vector<std::string>::const_iterator it = names.begin(); + std::vector<std::string>::const_iterator it_end = names.end(); + for (; it != it_end; ++ it) + { + if (*it != player_node->getName()) + newStr += *it + ","; + } + if (newStr[newStr.size() - 1] == ',') + newStr = newStr.substr(0, newStr.size() - 1); + if (!newStr.empty()) + replaceAll(str, "<PEOPLE>", newStr); + else + replaceAll(str, "<PEOPLE>", ""); + } + if (str.find("<PARTY>") != std::string::npos) + { + std::vector<std::string> names; + std::string newStr = ""; + Party *party = nullptr; + if (player_node->isInParty() && (party = player_node->getParty())) + { + party->getNames(names); + std::vector<std::string>::const_iterator it = names.begin(); + std::vector<std::string>::const_iterator it_end = names.end(); + for (; it != it_end; ++ it) + { + if (*it != player_node->getName()) + newStr += *it + ","; + } + if (newStr[newStr.size() - 1] == ',') + newStr = newStr.substr(0, newStr.size() - 1); + if (!newStr.empty()) + replaceAll(str, "<PARTY>", newStr); + else + replaceAll(str, "<PARTY>", ""); + } + else + { + replaceAll(str, "<PARTY>", ""); + } + } +} |