Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. /**
  2. * The Forgotten Server - a free and open-source MMORPG server emulator
  3. * Copyright (C) 2016 Mark Samman <mark.samman@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19.  
  20. #include "otpch.h"
  21.  
  22. #include "player.h"
  23. #include "talkaction.h"
  24. #include "pugicast.h"
  25.  
  26. TalkActions::TalkActions()
  27. : scriptInterface("TalkAction Interface")
  28. {
  29. scriptInterface.initState();
  30. }
  31.  
  32. TalkActions::~TalkActions()
  33. {
  34. clear();
  35. }
  36.  
  37. void TalkActions::clear()
  38. {
  39. for (TalkAction* talkAction : talkActions) {
  40. delete talkAction;
  41. }
  42. talkActions.clear();
  43.  
  44. scriptInterface.reInitState();
  45. }
  46.  
  47. LuaScriptInterface& TalkActions::getScriptInterface()
  48. {
  49. return scriptInterface;
  50. }
  51.  
  52. std::string TalkActions::getScriptBaseName() const
  53. {
  54. return "talkactions";
  55. }
  56.  
  57. Event* TalkActions::getEvent(const std::string& nodeName)
  58. {
  59. if (strcasecmp(nodeName.c_str(), "talkaction") != 0) {
  60. return nullptr;
  61. }
  62. return new TalkAction(&scriptInterface);
  63. }
  64.  
  65. bool TalkActions::registerEvent(Event* event, const pugi::xml_node&)
  66. {
  67. auto talkAction = std::unique_ptr<TalkAction>(static_cast<TalkAction*>(event)); // event is guaranteed to be a TalkAction
  68. talkActions.push_front(*talkAction);
  69. return true;
  70. }
  71.  
  72. TalkActionResult_t TalkActions::playerSaySpell(Player* player, SpeakClasses type, const std::string& words) const
  73. {
  74. size_t wordsLength = words.length();
  75. for (TalkAction* talkAction : talkActions) {
  76. const std::string& talkactionWords = talkAction->getWords();
  77. size_t talkactionLength = talkactionWords.length();
  78. if (wordsLength < talkactionLength || strncasecmp(words.c_str(), talkactionWords.c_str(), talkactionLength) != 0) {
  79. continue;
  80. }
  81.  
  82. std::string param;
  83. if (wordsLength != talkactionLength) {
  84. param = words.substr(talkactionLength);
  85. if (param.front() != ' ') {
  86. continue;
  87. }
  88. trim_left(param, ' ');
  89.  
  90. char separator = talkAction->getSeparator();
  91. if (separator != ' ') {
  92. if (!param.empty()) {
  93. if (param.front() != separator) {
  94. continue;
  95. } else {
  96. param.erase(param.begin());
  97. }
  98. }
  99. }
  100. }
  101.  
  102. if (talkAction->executeSay(player, param, type)) {
  103. return TALKACTION_CONTINUE;
  104. } else {
  105. return TALKACTION_BREAK;
  106. }
  107. }
  108. return TALKACTION_CONTINUE;
  109. }
  110.  
  111. TalkAction::TalkAction(LuaScriptInterface* _interface) :
  112. Event(_interface)
  113. {
  114. separator = '"';
  115. }
  116.  
  117. bool TalkAction::configureEvent(const pugi::xml_node& node)
  118. {
  119. pugi::xml_attribute wordsAttribute = node.attribute("words");
  120. if (!wordsAttribute) {
  121. std::cout << "[Error - TalkAction::configureEvent] Missing words for talk action or spell" << std::endl;
  122. return false;
  123. }
  124.  
  125. pugi::xml_attribute separatorAttribute = node.attribute("separator");
  126. if (separatorAttribute) {
  127. separator = pugi::cast<char>(separatorAttribute.value());
  128. }
  129.  
  130. words = wordsAttribute.as_string();
  131. return true;
  132. }
  133.  
  134. std::string TalkAction::getScriptEventName() const
  135. {
  136. return "onSay";
  137. }
  138.  
  139. bool TalkAction::executeSay(Player* player, const std::string& param, SpeakClasses type) const
  140. {
  141. //onSay(player, words, param, type)
  142. if (!scriptInterface->reserveScriptEnv()) {
  143. std::cout << "[Error - TalkAction::executeSay] Call stack overflow" << std::endl;
  144. return false;
  145. }
  146.  
  147. ScriptEnvironment* env = scriptInterface->getScriptEnv();
  148. env->setScriptId(scriptId, scriptInterface);
  149.  
  150. lua_State* L = scriptInterface->getLuaState();
  151.  
  152. scriptInterface->pushFunction(scriptId);
  153.  
  154. LuaScriptInterface::pushUserdata<Player>(L, player);
  155. LuaScriptInterface::setMetatable(L, -1, "Player");
  156.  
  157. LuaScriptInterface::pushString(L, words);
  158. LuaScriptInterface::pushString(L, param);
  159. lua_pushnumber(L, type);
  160.  
  161. return scriptInterface->callFunction(4);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement