diff options
author | Chuck Miller <shadowmil@gmail.com> | 2010-11-12 20:58:30 -0500 |
---|---|---|
committer | Chuck Miller <shadowmil@gmail.com> | 2010-11-12 21:16:08 -0500 |
commit | 009cfa4b2959bf89370e9d271f2244ef5446f3a0 (patch) | |
tree | 88f9185140a636b8c6fc3badfdec55cfee826290 | |
parent | 9ac7645f10d1e419703bdd35b276ce6e4eaf8152 (diff) | |
download | mana-009cfa4b2959bf89370e9d271f2244ef5446f3a0.tar.gz mana-009cfa4b2959bf89370e9d271f2244ef5446f3a0.tar.bz2 mana-009cfa4b2959bf89370e9d271f2244ef5446f3a0.tar.xz mana-009cfa4b2959bf89370e9d271f2244ef5446f3a0.zip |
Change NPC handling in the net code
Instead of using events to invoke netcode,
invoke netcode directly and have it send events
Reviewed-by: Freeyorp
-rw-r--r-- | src/being.cpp | 5 | ||||
-rw-r--r-- | src/event.h | 14 | ||||
-rw-r--r-- | src/gui/npcdialog.cpp | 30 | ||||
-rw-r--r-- | src/gui/npcpostdialog.cpp | 11 | ||||
-rw-r--r-- | src/net/manaserv/npchandler.cpp | 137 | ||||
-rw-r--r-- | src/net/manaserv/npchandler.h | 21 | ||||
-rw-r--r-- | src/net/npchandler.h | 16 | ||||
-rw-r--r-- | src/net/tmwa/npchandler.cpp | 121 | ||||
-rw-r--r-- | src/net/tmwa/npchandler.h | 21 |
9 files changed, 237 insertions, 139 deletions
diff --git a/src/being.cpp b/src/being.cpp index a9790246..47410620 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -49,6 +49,7 @@ #include "net/gamehandler.h" #include "net/net.h" #include "net/playerhandler.h" +#include "net/npchandler.h" #include "resources/beinginfo.h" #include "resources/colordb.h" @@ -1208,9 +1209,7 @@ bool Being::canTalk() void Being::talkTo() { - Mana::Event event(EVENT_DOTALK); - event.setInt("npcId", mId); - event.trigger(CHANNEL_NPC); + Net::getNpcHandler()->talk(mId); } void Being::event(Channels channel, const Mana::Event &event) diff --git a/src/event.h b/src/event.h index 99bd6fcb..4264232f 100644 --- a/src/event.h +++ b/src/event.h @@ -49,23 +49,17 @@ enum Events EVENT_BEING, EVENT_CLOSE, EVENT_CLOSEALL, + EVENT_CLOSESENT, EVENT_CONSTRUCTED, EVENT_DBSLOADING, EVENT_DESTROYED, EVENT_DESTRUCTED, EVENT_DESTRUCTING, - EVENT_DOCLOSE, EVENT_DOCLOSEINVENTORY, EVENT_DODROP, EVENT_DOEQUIP, - EVENT_DOINTEGERINPUT, - EVENT_DOMENU, EVENT_DOMOVE, - EVENT_DONEXT, - EVENT_DOSENDLETTER, EVENT_DOSPLIT, - EVENT_DOSTRINGINPUT, - EVENT_DOTALK, EVENT_DOUNEQUIP, EVENT_DOUSE, EVENT_END, @@ -76,19 +70,25 @@ enum Events EVENT_GUIWINDOWSUNLOADED, EVENT_GUIWINDOWSUNLOADING, EVENT_INTEGERINPUT, + EVENT_INTEGERINPUTSENT, EVENT_MAPLOADED, EVENT_MENU, + EVENT_MENUSENT, EVENT_MESSAGE, EVENT_NEXT, + EVENT_NEXTSENT, EVENT_NPCCOUNT, EVENT_PLAYER, EVENT_POST, EVENT_POSTCOUNT, + EVENT_SENDLETTERSENT, EVENT_SERVERNOTICE, EVENT_STATECHANGE, EVENT_STORAGECOUNT, EVENT_STRINGINPUT, + EVENT_STRINGINPUTSENT, EVENT_STUN, + EVENT_TALKSENT, EVENT_TRADING, EVENT_UPDATEATTRIBUTE, EVENT_UPDATESTAT, diff --git a/src/gui/npcdialog.cpp b/src/gui/npcdialog.cpp index 33c18eca..5b1939a6 100644 --- a/src/gui/npcdialog.cpp +++ b/src/gui/npcdialog.cpp @@ -37,6 +37,9 @@ #include "gui/widgets/textbox.h" #include "gui/widgets/textfield.h" +#include "net/net.h" +#include "net/npchandler.h" + #include "utils/gettext.h" #include "utils/stringutils.h" @@ -47,9 +50,6 @@ #define CAPTION_CLOSE _("Close") #define CAPTION_SUBMIT _("Submit") -#define NpcEvent(name) Mana::Event event(name);\ -event.setInt("npcId", mNpcId); - typedef std::map<int, NpcDialog*> NpcDialogs; class NpcEventListener : public Mana::Listener @@ -225,25 +225,19 @@ void NpcDialog::action(const gcn::ActionEvent &event) printText = mItems[selectedIndex]; - NpcEvent(EVENT_DOMENU) - event.setInt("choice", selectedIndex + 1); - event.trigger(CHANNEL_NPC); + Net::getNpcHandler()->menuSelect(mNpcId, selectedIndex + 1); } else if (mInputState == NPC_INPUT_STRING) { printText = mTextField->getText(); - NpcEvent(EVENT_DOSTRINGINPUT) - event.setString("value", printText); - event.trigger(CHANNEL_NPC); + Net::getNpcHandler()->stringInput(mNpcId, printText); } else if (mInputState == NPC_INPUT_INTEGER) { printText = strprintf("%d", mIntField->getValue()); - NpcEvent(EVENT_DOINTEGERINPUT) - event.setInt("value", mIntField->getValue()); - event.trigger(CHANNEL_NPC); + Net::getNpcHandler()->integerInput(mNpcId, mIntField->getValue()); } // addText will auto remove the input layout addText(strprintf("\n> \"%s\"\n", printText.c_str()), false); @@ -281,14 +275,12 @@ void NpcDialog::action(const gcn::ActionEvent &event) void NpcDialog::nextDialog() { - NpcEvent(EVENT_DONEXT); - event.trigger(CHANNEL_NPC); + Net::getNpcHandler()->nextDialog(mNpcId); } void NpcDialog::closeDialog() { - NpcEvent(EVENT_DOCLOSE); - event.trigger(CHANNEL_NPC); + Net::getNpcHandler()->closeDialog(mNpcId); close(); } @@ -559,8 +551,7 @@ void NpcEventListener::event(Channels channel, if (!dialog) { int mNpcId = id; - NpcEvent(EVENT_DONEXT); - event.trigger(CHANNEL_NPC); + Net::getNpcHandler()->nextDialog(mNpcId); return; } @@ -574,8 +565,7 @@ void NpcEventListener::event(Channels channel, if (!dialog) { int mNpcId = id; - NpcEvent(EVENT_DOCLOSE); - event.trigger(CHANNEL_NPC); + Net::getNpcHandler()->closeDialog(mNpcId); return; } diff --git a/src/gui/npcpostdialog.cpp b/src/gui/npcpostdialog.cpp index 7a85dda5..c53203be 100644 --- a/src/gui/npcpostdialog.cpp +++ b/src/gui/npcpostdialog.cpp @@ -30,6 +30,9 @@ #include "gui/widgets/textfield.h" #include "gui/widgets/scrollarea.h" +#include "net/net.h" +#include "net/npchandler.h" + #include "utils/gettext.h" NpcPostDialog::DialogList NpcPostDialog::instances; @@ -97,11 +100,9 @@ void NpcPostDialog::action(const gcn::ActionEvent &event) } else { - Mana::Event event(EVENT_DOSENDLETTER); - event.setInt("npcId", mNpcId); - event.setString("recipient", mSender->getText()); - event.setString("text", mText->getText()); - event.trigger(CHANNEL_NPC); + Net::getNpcHandler()->sendLetter(mNpcId, + mSender->getText(), + mText->getText()); } setVisible(false); } diff --git a/src/net/manaserv/npchandler.cpp b/src/net/manaserv/npchandler.cpp index 403dedb6..9620f7c1 100644 --- a/src/net/manaserv/npchandler.cpp +++ b/src/net/manaserv/npchandler.cpp @@ -51,8 +51,6 @@ NpcHandler::NpcHandler() }; handledMessages = _messages; npcHandler = this; - - listen(CHANNEL_NPC); } void NpcHandler::handleMessage(Net::MessageIn &msg) @@ -129,54 +127,6 @@ void NpcHandler::handleMessage(Net::MessageIn &msg) delete event; } -void NpcHandler::event(Channels channel, const Mana::Event &event) -{ - if (channel == CHANNEL_NPC) - { - if (event.getName() == EVENT_DOTALK) - { - MessageOut msg(PGMSG_NPC_TALK); - msg.writeInt16(event.getInt("npcId")); - gameServerConnection->send(msg); - } - else if (event.getName() == EVENT_DONEXT || - event.getName() == EVENT_DOCLOSE) - { - MessageOut msg(PGMSG_NPC_TALK_NEXT); - msg.writeInt16(event.getInt("npcId")); - gameServerConnection->send(msg); - } - else if (event.getName() == EVENT_DOMENU) - { - MessageOut msg(PGMSG_NPC_SELECT); - msg.writeInt16(event.getInt("npcId")); - msg.writeInt8(event.getInt("choice")); - gameServerConnection->send(msg); - } - else if (event.getName() == EVENT_DOINTEGERINPUT) - { - MessageOut msg(PGMSG_NPC_NUMBER); - msg.writeInt16(event.getInt("npcId")); - msg.writeInt32(event.getInt("value")); - gameServerConnection->send(msg); - } - else if (event.getName() == EVENT_DOSTRINGINPUT) - { - MessageOut msg(PGMSG_NPC_STRING); - msg.writeInt16(event.getInt("npcId")); - msg.writeString(event.getString("value")); - gameServerConnection->send(msg); - } - else if (event.getName() == EVENT_DOSENDLETTER) - { - MessageOut msg(PGMSG_NPC_POST_SEND); - msg.writeString(event.getString("recipient")); - msg.writeString(event.getString("text")); - gameServerConnection->send(msg); - } - } -} - void NpcHandler::startShopping(int beingId) { // TODO @@ -213,4 +163,91 @@ void NpcHandler::endShopping(int beingId) // TODO } +void NpcHandler::talk(int npcId) +{ + MessageOut msg(PGMSG_NPC_TALK); + msg.writeInt16(npcId); + gameServerConnection->send(msg); + + Mana::Event event(EVENT_TALKSENT); + event.setInt("npcId", npcId); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::nextDialog(int npcId) +{ + MessageOut msg(PGMSG_NPC_TALK_NEXT); + msg.writeInt16(npcId); + gameServerConnection->send(msg); + + Mana::Event event(EVENT_NEXTSENT); + event.setInt("npcId", npcId); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::closeDialog(int npcId) +{ + MessageOut msg(PGMSG_NPC_TALK_NEXT); + msg.writeInt16(npcId); + gameServerConnection->send(msg); + + Mana::Event event(EVENT_CLOSESENT); + event.setInt("npcId", npcId); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::menuSelect(int npcId, int choice) +{ + MessageOut msg(PGMSG_NPC_SELECT); + msg.writeInt16(npcId); + msg.writeInt8(choice); + gameServerConnection->send(msg); + + Mana::Event event(EVENT_MENUSENT); + event.setInt("npcId", npcId); + event.setInt("choice", choice); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::integerInput(int npcId, int value) +{ + MessageOut msg(PGMSG_NPC_NUMBER); + msg.writeInt16(npcId); + msg.writeInt32(value); + gameServerConnection->send(msg); + + Mana::Event event(EVENT_INTEGERINPUTSENT); + event.setInt("npcId", npcId); + event.setInt("value", value); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::stringInput(int npcId, const std::string &value) +{ + MessageOut msg(PGMSG_NPC_STRING); + msg.writeInt16(npcId); + msg.writeString(value); + gameServerConnection->send(msg); + + Mana::Event event(EVENT_STRINGINPUTSENT); + event.setInt("npcId", npcId); + event.setString("value", value); + event.trigger(CHANNEL_NPC); +} + +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); + + Mana::Event event(EVENT_SENDLETTERSENT); + event.setInt("npcId", npcId); + event.setString("recipient", recipient); + event.setString("text", text); + event.trigger(CHANNEL_NPC); +} + } // namespace ManaServ diff --git a/src/net/manaserv/npchandler.h b/src/net/manaserv/npchandler.h index 397d3569..cb8fd67d 100644 --- a/src/net/manaserv/npchandler.h +++ b/src/net/manaserv/npchandler.h @@ -32,16 +32,13 @@ namespace ManaServ { -class NpcHandler : public MessageHandler, public Net::NpcHandler, - public Mana::Listener +class NpcHandler : public MessageHandler, public Net::NpcHandler { public: NpcHandler(); void handleMessage(Net::MessageIn &msg); - void event(Channels channel, const Mana::Event &event); - void startShopping(int beingId); void buy(int beingId); @@ -53,6 +50,22 @@ class NpcHandler : public MessageHandler, public Net::NpcHandler, void sellItem(int beingId, int itemId, int amount); void endShopping(int beingId); + + void talk(int npcId); + + void nextDialog(int npcId); + + void closeDialog(int npcId); + + void menuSelect(int npcId, int choice); + + void integerInput(int npcId, int value); + + void stringInput(int npcId, const std::string &value); + + void sendLetter(int npcId, const std::string &recipient, + const std::string &text); + }; } // namespace ManaServ diff --git a/src/net/npchandler.h b/src/net/npchandler.h index fb8ab7ec..35535c61 100644 --- a/src/net/npchandler.h +++ b/src/net/npchandler.h @@ -42,6 +42,22 @@ class NpcHandler virtual void sellItem(int beingId, int itemId, int amount) = 0; virtual void endShopping(int beingId) = 0; + + virtual void talk(int npcId) = 0; + + virtual void nextDialog(int npcId) = 0; + + virtual void closeDialog(int npcId) = 0; + + virtual void menuSelect(int npcId, int choice) = 0; + + virtual void integerInput(int npcId, int value) = 0; + + virtual void stringInput(int npcId, const std::string &value) = 0; + + virtual void sendLetter(int npcId, const std::string &recipient, + const std::string &text) = 0; + }; } // namespace Net diff --git a/src/net/tmwa/npchandler.cpp b/src/net/tmwa/npchandler.cpp index 1b12b63f..337226a9 100644 --- a/src/net/tmwa/npchandler.cpp +++ b/src/net/tmwa/npchandler.cpp @@ -68,8 +68,6 @@ NpcHandler::NpcHandler() }; handledMessages = _messages; npcHandler = this; - - listen(CHANNEL_NPC); } void NpcHandler::handleMessage(Net::MessageIn &msg) @@ -133,50 +131,6 @@ void NpcHandler::handleMessage(Net::MessageIn &msg) player_node->setAction(Being::STAND); } -void NpcHandler::event(Channels channel, const Mana::Event &event) -{ - if (channel == CHANNEL_NPC) - { - if (event.getName() == EVENT_DOTALK) - { - MessageOut outMsg(CMSG_NPC_TALK); - outMsg.writeInt32(event.getInt("npcId")); - outMsg.writeInt8(0); // Unused - } - else if (event.getName() == EVENT_DONEXT) - { - MessageOut outMsg(CMSG_NPC_NEXT_REQUEST); - outMsg.writeInt32(event.getInt("npcId")); - } - else if (event.getName() == EVENT_DOCLOSE) - { - MessageOut outMsg(CMSG_NPC_CLOSE); - outMsg.writeInt32(event.getInt("npcId")); - } - else if (event.getName() == EVENT_DOMENU) - { - MessageOut outMsg(CMSG_NPC_LIST_CHOICE); - outMsg.writeInt32(event.getInt("npcId")); - outMsg.writeInt8(event.getInt("choice")); - } - else if (event.getName() == EVENT_DOINTEGERINPUT) - { - MessageOut outMsg(CMSG_NPC_INT_RESPONSE); - outMsg.writeInt32(event.getInt("npcId")); - outMsg.writeInt32(event.getInt("value")); - } - else if (event.getName() == EVENT_DOSTRINGINPUT) - { - const std::string &value = event.getString("value"); - MessageOut outMsg(CMSG_NPC_STR_RESPONSE); - outMsg.writeInt16(value.length() + 9); - outMsg.writeInt32(event.getInt("npcId")); - outMsg.writeString(value, value.length()); - outMsg.writeInt8(0); // Prevent problems with string reading - } - } -} - void NpcHandler::startShopping(int beingId) { // TODO @@ -217,4 +171,79 @@ void NpcHandler::endShopping(int beingId) // TODO } +void NpcHandler::talk(int npcId) +{ + MessageOut outMsg(CMSG_NPC_TALK); + outMsg.writeInt32(npcId); + outMsg.writeInt8(0); // Unused + + Mana::Event event(EVENT_TALKSENT); + event.setInt("npcId", npcId); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::nextDialog(int npcId) +{ + MessageOut outMsg(CMSG_NPC_NEXT_REQUEST); + outMsg.writeInt32(npcId); + + Mana::Event event(EVENT_NEXTSENT); + event.setInt("npcId", npcId); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::closeDialog(int npcId) +{ + MessageOut outMsg(CMSG_NPC_CLOSE); + outMsg.writeInt32(npcId); + + Mana::Event event(EVENT_CLOSESENT); + event.setInt("npcId", npcId); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::menuSelect(int npcId, int choice) +{ + MessageOut outMsg(CMSG_NPC_LIST_CHOICE); + outMsg.writeInt32(npcId); + outMsg.writeInt8(choice); + + Mana::Event event(EVENT_MENUSENT); + event.setInt("npcId", npcId); + event.setInt("choice", choice); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::integerInput(int npcId, int value) +{ + MessageOut outMsg(CMSG_NPC_INT_RESPONSE); + outMsg.writeInt32(npcId); + outMsg.writeInt32(value); + + Mana::Event event(EVENT_INTEGERINPUTSENT); + event.setInt("npcId", npcId); + event.setInt("value", value); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::stringInput(int npcId, const std::string &value) +{ + MessageOut outMsg(CMSG_NPC_STR_RESPONSE); + outMsg.writeInt16(value.length() + 9); + outMsg.writeInt32(npcId); + outMsg.writeString(value, value.length()); + outMsg.writeInt8(0); // Prevent problems with string reading + + Mana::Event event(EVENT_STRINGINPUTSENT); + event.setInt("npcId", npcId); + event.setString("value", value); + event.trigger(CHANNEL_NPC); +} + +void NpcHandler::sendLetter(int npcId, const std::string &recipient, + const std::string &text) +{ + //NOTE: eA doesn't have letters +} + } // namespace TmwAthena diff --git a/src/net/tmwa/npchandler.h b/src/net/tmwa/npchandler.h index 9fcb0041..1e933418 100644 --- a/src/net/tmwa/npchandler.h +++ b/src/net/tmwa/npchandler.h @@ -32,16 +32,13 @@ namespace TmwAthena { -class NpcHandler : public MessageHandler, public Net::NpcHandler, - public Mana::Listener +class NpcHandler : public MessageHandler, public Net::NpcHandler { public: NpcHandler(); void handleMessage(Net::MessageIn &msg); - void event(Channels channel, const Mana::Event &event); - void startShopping(int beingId); void buy(int beingId); @@ -53,6 +50,22 @@ class NpcHandler : public MessageHandler, public Net::NpcHandler, void sellItem(int beingId, int itemId, int amount); void endShopping(int beingId); + + void talk(int npcId); + + void nextDialog(int npcId); + + void closeDialog(int npcId); + + void menuSelect(int npcId, int choice); + + void integerInput(int npcId, int value); + + void stringInput(int npcId, const std::string &value); + + void sendLetter(int npcId, const std::string &recipient, + const std::string &text); + }; } // namespace TmwAthena |