diff options
Diffstat (limited to 'src/net/manaserv/npchandler.cpp')
-rw-r--r-- | src/net/manaserv/npchandler.cpp | 232 |
1 files changed, 107 insertions, 125 deletions
diff --git a/src/net/manaserv/npchandler.cpp b/src/net/manaserv/npchandler.cpp index 392ec4fd..f7e04c1b 100644 --- a/src/net/manaserv/npchandler.cpp +++ b/src/net/manaserv/npchandler.cpp @@ -21,17 +21,16 @@ #include "net/manaserv/npchandler.h" -#include "beingmanager.h" -#include "npc.h" - -#include "gui/npcdialog.h" -#include "gui/npcpostdialog.h" +#include "actorspritemanager.h" +#include "event.h" #include "net/manaserv/connection.h" #include "net/manaserv/messagein.h" #include "net/manaserv/messageout.h" #include "net/manaserv/protocol.h" +#include "utils/stringutils.h" + extern Net::NpcHandler *npcHandler; namespace ManaServ { @@ -52,143 +51,131 @@ NpcHandler::NpcHandler() }; handledMessages = _messages; npcHandler = this; + + listen("NPC"); } void NpcHandler::handleMessage(Net::MessageIn &msg) { - Being *being = beingManager->findBeing(msg.readInt16()); - if (!being || being->getType() != Being::NPC) + Being *being = actorSpriteManager->findBeing(msg.readInt16()); + if (!being || being->getType() != ActorSprite::NPC) { return; } - int npcId = being->getId(); - NpcDialogs::iterator diag = mNpcDialogs.find(npcId); - NpcDialog *dialog; - - if (diag == mNpcDialogs.end()) - { - if (msg.getId() == GPMSG_NPC_ERROR || msg.getId() == GPMSG_NPC_CLOSE) - return; // Dialog is pointless in these cases - - dialog = new NpcDialog(npcId); - Wrapper wrap; - wrap.dialog = dialog; - mNpcDialogs[npcId] = wrap; - } - else - { - dialog = diag->second.dialog; - } + int npcId = being->getId(), count = 0; + Mana::Event *event = 0; switch (msg.getId()) { - case GPMSG_NPC_CHOICE: - dialog->choiceRequest(); - while (msg.getUnreadLength()) - { - dialog->addChoice(msg.readString()); - } - break; - - case GPMSG_NPC_NUMBER: + case GPMSG_NPC_CHOICE: + event = new Mana::Event("Menu"); + event->setInt("id", npcId); + while (msg.getUnreadLength()) { - int min_num = msg.readInt32(); - int max_num = msg.readInt32(); - dialog->integerRequest(msg.readInt32(), min_num, max_num); - break; + count++; + event->setString("choice" + toString(count), msg.readString()); } - - case GPMSG_NPC_STRING: - dialog->textRequest(""); - break; - - case GPMSG_NPC_POST: - { - new NpcPostDialog(npcId); - break; - } - - case GPMSG_NPC_ERROR: - dialog->close(); - if (diag != mNpcDialogs.end()) - { - mNpcDialogs.erase(diag); - } - break; - - case GPMSG_NPC_MESSAGE: - dialog->addText(msg.readString(msg.getUnreadLength())); - dialog->showNextButton(); - break; - - case GPMSG_NPC_CLOSE: - dialog->showCloseButton(); - break; + event->setInt("choiceCount", count); + event->trigger("NPC"); + break; + + case GPMSG_NPC_NUMBER: + event = new Mana::Event("IntegerInput"); + event->setInt("id", npcId); + event->setInt("min", msg.readInt32()); + event->setInt("max", msg.readInt32()); + event->setInt("default", msg.readInt32()); + event->trigger("NPC"); + break; + + case GPMSG_NPC_STRING: + event = new Mana::Event("StringInput"); + event->setInt("id", npcId); + event->trigger("NPC"); + break; + + case GPMSG_NPC_POST: + event = new Mana::Event("Post"); + event->setInt("id", npcId); + event->trigger("NPC"); + break; + + case GPMSG_NPC_ERROR: + event = new Mana::Event("End"); + event->setInt("id", npcId); + event->trigger("NPC"); + break; + + case GPMSG_NPC_MESSAGE: + event = new Mana::Event("Message"); + event->setInt("id", npcId); + event->setString("text", msg.readString(msg.getUnreadLength())); + event->trigger("NPC"); + delete event; + + event = new Mana::Event("Next"); + event->setInt("id", npcId); + event->trigger("NPC"); + break; + + case GPMSG_NPC_CLOSE: + event = new Mana::Event("Close"); + event->setInt("id", npcId); + event->trigger("NPC"); + break; } -} -void NpcHandler::talk(int npcId) -{ - MessageOut msg(PGMSG_NPC_TALK); - msg.writeInt16(npcId); - gameServerConnection->send(msg); -} - -void NpcHandler::nextDialog(int npcId) -{ - MessageOut msg(PGMSG_NPC_TALK_NEXT); - msg.writeInt16(npcId); - gameServerConnection->send(msg); + delete event; } -void NpcHandler::closeDialog(int npcId) +void NpcHandler::event(const std::string &channel, const Mana::Event &event) { - MessageOut msg(PGMSG_NPC_TALK_NEXT); - msg.writeInt16(npcId); - gameServerConnection->send(msg); - - NpcDialogs::iterator it = mNpcDialogs.find(npcId); - if (it != mNpcDialogs.end()) + if (channel == "NPC") { - (*it).second.dialog->close(); - mNpcDialogs.erase(it); + if (event.getName() == "doTalk") + { + MessageOut msg(PGMSG_NPC_TALK); + msg.writeInt16(event.getInt("npcId")); + gameServerConnection->send(msg); + } + else if (event.getName() == "doNext" || event.getName() == "doClose") + { + MessageOut msg(PGMSG_NPC_TALK_NEXT); + msg.writeInt16(event.getInt("npcId")); + gameServerConnection->send(msg); + } + else if (event.getName() == "doMenu") + { + MessageOut msg(PGMSG_NPC_SELECT); + msg.writeInt16(event.getInt("npcId")); + msg.writeInt8(event.getInt("choice")); + gameServerConnection->send(msg); + } + else if (event.getName() == "doIntegerInput") + { + MessageOut msg(PGMSG_NPC_NUMBER); + msg.writeInt16(event.getInt("npcId")); + msg.writeInt32(event.getInt("value")); + gameServerConnection->send(msg); + } + else if (event.getName() == "doStringInput") + { + MessageOut msg(PGMSG_NPC_STRING); + msg.writeInt16(event.getInt("npcId")); + msg.writeString(event.getString("value")); + gameServerConnection->send(msg); + } + else if (event.getName() == "doSendLetter") + { + MessageOut msg(PGMSG_NPC_POST_SEND); + msg.writeString(event.getString("recipient")); + msg.writeString(event.getString("text")); + gameServerConnection->send(msg); + } } } -void NpcHandler::listInput(int npcId, int value) -{ - MessageOut msg(PGMSG_NPC_SELECT); - msg.writeInt16(npcId); - msg.writeInt8(value); - gameServerConnection->send(msg); -} - -void NpcHandler::integerInput(int npcId, int value) -{ - MessageOut msg(PGMSG_NPC_NUMBER); - msg.writeInt16(npcId); - msg.writeInt32(value); - gameServerConnection->send(msg); -} - -void NpcHandler::stringInput(int npcId, const std::string &value) -{ - MessageOut msg(PGMSG_NPC_STRING); - msg.writeInt16(npcId); - msg.writeString(value); - gameServerConnection->send(msg); -} - -void NpcHandler::sendLetter(int npcId, const std::string &recipient, - const std::string &text) -{ - MessageOut msg(PGMSG_NPC_POST_SEND); - msg.writeString(recipient); - msg.writeString(text); - gameServerConnection->send(msg); -} - void NpcHandler::startShopping(int beingId) { // TODO @@ -225,9 +212,4 @@ void NpcHandler::endShopping(int beingId) // TODO } -void NpcHandler::clearDialogs() -{ - mNpcDialogs.clear(); -} - } // namespace ManaServ |