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 | |
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>.
-rw-r--r-- | src/commandhandler.cpp | 80 | ||||
-rw-r--r-- | src/commandhandler.h | 2 | ||||
-rw-r--r-- | src/gui/chatwindow.cpp | 5 | ||||
-rw-r--r-- | src/gui/widgets/chattab.cpp | 3 |
4 files changed, 90 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>", ""); + } + } +} diff --git a/src/commandhandler.h b/src/commandhandler.h index f8b44e04f..b97b191ff 100644 --- a/src/commandhandler.h +++ b/src/commandhandler.h @@ -59,6 +59,8 @@ class CommandHandler void handleCommands(const std::string &command, ChatTab *tab = localChatTab); + void replaceVars(std::string &str); + static char parseBoolean(const std::string &value); protected: diff --git a/src/gui/chatwindow.cpp b/src/gui/chatwindow.cpp index 33c24e9c7..84d61cf65 100644 --- a/src/gui/chatwindow.cpp +++ b/src/gui/chatwindow.cpp @@ -24,6 +24,7 @@ #include "actorspritemanager.h" #include "client.h" +#include "commandhandler.h" #include "configuration.h" #include "guild.h" #include "keyboardconfig.h" @@ -305,6 +306,10 @@ void ChatWindow::fillCommands() mCommands.push_back("/serverunignoreall"); mCommands.push_back("/dumpg"); mCommands.push_back("/pseudoaway "); + mCommands.push_back("<PLAYER>"); + mCommands.push_back("<MONSTER>"); + mCommands.push_back("<PEOPLE>"); + mCommands.push_back("<PARTY>"); } void ChatWindow::loadGMCommands() diff --git a/src/gui/widgets/chattab.cpp b/src/gui/widgets/chattab.cpp index 72af1b0b8..c2db20574 100644 --- a/src/gui/widgets/chattab.cpp +++ b/src/gui/widgets/chattab.cpp @@ -349,6 +349,9 @@ void ChatTab::chatInput(const std::string &message) start = msg.find('[', start + 1); } + if (commandHandler) + commandHandler->replaceVars(msg); + // Prepare ordinary message if (msg[0] != '/') handleInput(msg); |