Mana
Loading...
Searching...
No Matches
npchandler.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "actorspritemanager.h"
25#include "event.h"
26
31
32#include "utils/stringutils.h"
33
35
36namespace ManaServ {
37
38extern Connection *gameServerConnection;
39
41{
42 static const uint16_t _messages[] = {
51 0
52 };
53 handledMessages = _messages;
54 npcHandler = this;
55}
56
58{
60 if (!being || being->getType() != ActorSprite::NPC)
61 {
62 return;
63 }
64
65 int npcId = being->getId(), count = 0;
66 Event *event = nullptr;
67
68 switch (msg.getId())
69 {
71 event = new Event(Event::Menu);
72 event->setInt("id", npcId);
73 while (msg.getUnreadLength())
74 {
75 count++;
76 event->setString("choice" + toString(count), msg.readString());
77 }
78 event->setInt("choiceCount", count);
79 event->trigger(Event::NpcChannel);
80 break;
81
83 event = new Event(Event::IntegerInput);
84 event->setInt("id", npcId);
85 event->setInt("min", msg.readInt32());
86 event->setInt("max", msg.readInt32());
87 event->setInt("default", msg.readInt32());
88 event->trigger(Event::NpcChannel);
89 break;
90
92 event = new Event(Event::StringInput);
93 event->setInt("id", npcId);
94 event->trigger(Event::NpcChannel);
95 break;
96
97 case GPMSG_NPC_POST:
98 event = new Event(Event::Post);
99 event->setInt("id", npcId);
100 event->trigger(Event::NpcChannel);
101 break;
102
103 case GPMSG_NPC_ERROR:
104 event = new Event(Event::CloseDialog);
105 event->setInt("id", npcId);
106 event->trigger(Event::NpcChannel);
107 break;
108
110 event = new Event(Event::Message);
111 event->setInt("id", npcId);
112 event->setString("text", msg.readString());
113 event->trigger(Event::NpcChannel);
114 delete event;
115
116 event = new Event(Event::Next);
117 event->setInt("id", npcId);
118 event->trigger(Event::NpcChannel);
119 break;
120
121 case GPMSG_NPC_CLOSE:
122 event = new Event(Event::Close);
123 event->setInt("id", npcId);
124 event->trigger(Event::NpcChannel);
125 break;
126
128 break;
129 }
130
131 delete event;
132}
133
135{
136 // TODO
137}
138
139void NpcHandler::buy(int beingId)
140{
141 // TODO
142}
143
144void NpcHandler::sell(int beingId)
145{
146 // TODO
147}
148
149void NpcHandler::buyItem(int beingId, int itemId, int amount)
150{
152 msg.writeInt16(itemId);
153 msg.writeInt16(amount);
155}
156
157void NpcHandler::sellItem(int beingId, int itemId, int amount)
158{
160 msg.writeInt16(itemId);
161 msg.writeInt16(amount);
163}
164
165void NpcHandler::endShopping(int beingId)
166{
167 // TODO
168}
169
170void NpcHandler::talk(int npcId)
171{
173 msg.writeInt16(npcId);
175}
176
178{
180 msg.writeInt16(npcId);
182}
183
185{
187 msg.writeInt16(npcId);
189}
190
191void NpcHandler::menuSelect(int npcId, int choice)
192{
194 msg.writeInt16(npcId);
195 msg.writeInt8(choice);
197}
198
199void NpcHandler::integerInput(int npcId, int value)
200{
202 msg.writeInt16(npcId);
203 msg.writeInt32(value);
205}
206
207void NpcHandler::stringInput(int npcId, const std::string &value)
208{
210 msg.writeInt16(npcId);
211 msg.writeString(value);
213}
214
215void NpcHandler::sendLetter(int npcId, const std::string &recipient,
216 const std::string &text)
217{
219 msg.writeString(recipient);
220 msg.writeString(text);
222}
223
224} // namespace ManaServ
ActorSpriteManager * actorSpriteManager
Definition game.cpp:110
Being * findBeing(int id) const
Returns a specific Being, by id;.
int getId() const
Definition actorsprite.h:63
Definition being.h:65
Type getType() const final
Returns the type of the ActorSprite.
Definition being.h:122
Definition event.h:42
@ Next
Definition event.h:90
@ CloseDialog
Definition event.h:67
@ Menu
Definition event.h:88
@ Close
Definition event.h:65
@ Post
Definition event.h:93
@ Message
Definition event.h:89
@ StringInput
Definition event.h:98
@ IntegerInput
Definition event.h:86
@ NpcChannel
Definition event.h:55
void send(const ManaServ::MessageOut &msg)
Sends a message.
Used for parsing an incoming message from manaserv.
Definition messagein.h:37
uint16_t readInt16()
Reads an unsigned 16-bit integer from the message.
Definition messagein.cpp:58
unsigned int getUnreadLength() const
Returns the length of unread data.
Definition messagein.h:54
std::string readString(int length=-1)
Reads a string.
Definition messagein.cpp:92
uint16_t getId() const
Returns the message ID.
Definition messagein.h:44
uint32_t readInt32()
Reads an unsigned 32-bit integer from the message.
Definition messagein.cpp:75
Used for building an outgoing message to manaserv.
Definition messageout.h:37
void writeInt16(uint16_t value)
Writes an unsigned 16-bit integer to the message.
void writeInt32(uint32_t value)
Writes an unsigned 32-bit integer to the message.
void writeInt8(uint8_t value)
Writes an unsigned 8-bit integer to the message.
void writeString(const std::string &string, int length=-1)
Writes a string.
void sell(int beingId) override
void sendLetter(int npcId, const std::string &recipient, const std::string &text) override
void endShopping(int beingId) override
void handleMessage(MessageIn &msg) override
void buy(int beingId) override
void closeDialog(int npcId) override
void startShopping(int beingId) override
void integerInput(int npcId, int value) override
void stringInput(int npcId, const std::string &value) override
void buyItem(int beingId, int itemId, int amount) override
void menuSelect(int npcId, int choice) override
void sellItem(int beingId, int itemId, int amount) override
void nextDialog(int npcId) override
void talk(int npcId) override
const uint16_t * handledMessages
Net::NpcHandler * npcHandler
Definition net.cpp:54
Connection * gameServerConnection
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68