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
22#include "net/tmwa/npchandler.h"
23
24#include "actorspritemanager.h"
25#include "event.h"
26#include "localplayer.h"
27
28#include "net/net.h"
29#include "net/npchandler.h"
30
31#include "net/tmwa/messagein.h"
32#include "net/tmwa/messageout.h"
33#include "net/tmwa/protocol.h"
34
35#include "utils/stringutils.h"
36
38
39static void parseMenu(Event &event, const std::string &options)
40{
41 std::istringstream iss(options);
42
43 int count = 0;
44 std::string tmp;
45 while (getline(iss, tmp, ':'))
46 {
47 count++;
48 event.setString("choice" + toString(count), tmp);
49 }
50
51 event.setInt("choiceCount", count);
52}
53
54namespace TmwAthena {
55
57{
58 static const uint16_t _messages[] = {
66 0
67 };
68 handledMessages = _messages;
69 npcHandler = this;
70}
71
73{
74 if (msg.getId() == SMSG_NPC_CHOICE || msg.getId() == SMSG_NPC_MESSAGE)
75 {
76 msg.readInt16(); // length
77 }
78
79 int npcId = msg.readInt32();
80
81 switch (msg.getId())
82 {
83 case SMSG_NPC_CHOICE:
84 {
85 Event event { Event::Menu };
86 event.setInt("id", npcId);
87 parseMenu(event, msg.readString(msg.getLength() - 8));
88 event.trigger(Event::NpcChannel);
89 break;
90 }
91
93 {
94 Event event { Event::Message };
95 event.setInt("id", npcId);
96 event.setString("text", msg.readString(msg.getLength() - 8));
97 event.trigger(Event::NpcChannel);
98 break;
99 }
100
101 case SMSG_NPC_CLOSE:
102 {
103 // Show the close button
104 Event event { Event::Close };
105 event.setInt("id", npcId);
106 event.trigger(Event::NpcChannel);
107 break;
108 }
109
110 case SMSG_NPC_COMMAND:
111 {
112 auto command = msg.readInt16();
113 msg.readInt32(); // id
114 msg.readInt16(); // x
115 msg.readInt16(); // y
116
117 switch (command)
118 {
119 case NPC_CLOSE_DIALOG:
120 {
121 Event event { Event::CloseDialog };
122 event.setInt("id", npcId);
123 event.trigger(Event::NpcChannel);
124 break;
125 }
126
127 case NPC_CLEAR_DIALOG:
128 {
129 Event event { Event::ClearDialog };
130 event.setInt("id", npcId);
131 event.trigger(Event::NpcChannel);
132 break;
133 }
134 }
135
136 break;
137 }
138
139 case SMSG_NPC_NEXT:
140 {
141 // Show the next button
142 Event event { Event::Next };
143 event.setInt("id", npcId);
144 event.trigger(Event::NpcChannel);
145 break;
146 }
147
149 {
150 // Request for an integer
151 Event event { Event::IntegerInput };
152 event.setInt("id", npcId);
153 event.trigger(Event::NpcChannel);
154 break;
155 }
156
158 {
159 // Request for a string
160 Event event { Event::StringInput };
161 event.setInt("id", npcId);
162 event.trigger(Event::NpcChannel);
163 break;
164 }
165 }
166
169}
170
172{
173 // TODO
174}
175
176void NpcHandler::buy(int beingId)
177{
179 outMsg.writeInt32(beingId);
180 outMsg.writeInt8(0); // Buy
181}
182
183void NpcHandler::sell(int beingId)
184{
186 outMsg.writeInt32(beingId);
187 outMsg.writeInt8(1); // Sell
188}
189
190void NpcHandler::buyItem(int beingId, int itemId, int amount)
191{
193 outMsg.writeInt16(8); // One item (length of packet)
194 outMsg.writeInt16(amount);
195 outMsg.writeInt16(itemId);
196}
197
198void NpcHandler::sellItem(int beingId, int itemId, int amount)
199{
201 outMsg.writeInt16(8); // One item (length of packet)
202 outMsg.writeInt16(itemId + INVENTORY_OFFSET);
203 outMsg.writeInt16(amount);
204}
205
206void NpcHandler::endShopping(int beingId)
207{
208 // TODO
209}
210
211void NpcHandler::talk(int npcId)
212{
214 outMsg.writeInt32(npcId);
215 outMsg.writeInt8(0); // Unused
216}
217
219{
221 outMsg.writeInt32(npcId);
222}
223
225{
227 outMsg.writeInt32(npcId);
228}
229
230void NpcHandler::menuSelect(int npcId, int choice)
231{
233 outMsg.writeInt32(npcId);
234 outMsg.writeInt8(choice);
235}
236
237void NpcHandler::integerInput(int npcId, int value)
238{
240 outMsg.writeInt32(npcId);
241 outMsg.writeInt32(value);
242}
243
244void NpcHandler::stringInput(int npcId, const std::string &value)
245{
247 outMsg.writeInt16(value.length() + 9);
248 outMsg.writeInt32(npcId);
249 outMsg.writeString(value, value.length());
250 outMsg.writeInt8(0); // Prevent problems with string reading
251}
252
253void NpcHandler::sendLetter(int npcId, const std::string &recipient,
254 const std::string &text)
255{
256 //NOTE: eA doesn't have letters
257}
258
259} // namespace TmwAthena
@ SIT
Definition being.h:77
@ STAND
Definition being.h:74
Action getCurrentAction() const
Get the being's action currently performed.
Definition being.h:346
Definition event.h:42
@ Next
Definition event.h:90
@ CloseDialog
Definition event.h:67
@ Menu
Definition event.h:88
@ Close
Definition event.h:65
@ Message
Definition event.h:89
@ ClearDialog
Definition event.h:64
@ StringInput
Definition event.h:98
@ IntegerInput
Definition event.h:86
@ NpcChannel
Definition event.h:55
void setInt(const std::string &key, int value)
Sets the given variable to the given integer, if it isn't already set.
Definition event.cpp:36
void setAction(Action action, int attackId=1) override
Sets the current action.
const uint16_t * handledMessages
Used for parsing an incoming message from eAthena.
Definition messagein.h:35
std::string readString(int length=-1)
Reads a string.
uint16_t readInt16()
Reads an unsigned 16-bit integer from the message.
Definition messagein.cpp:57
uint16_t getId() const
Returns the message ID.
Definition messagein.h:42
uint32_t readInt32()
Reads an unsigned 32-bit integer from the message.
Definition messagein.cpp:69
unsigned int getLength() const
Returns the message length.
Definition messagein.h:47
Used for building an outgoing message to eAthena.
Definition messageout.h:35
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 writeInt16(uint16_t value)
Writes an unsigned 16-bit integer to the message.
void writeString(const std::string &string, int length=-1)
Writes a string.
void integerInput(int npcId, int value) override
void handleMessage(MessageIn &msg) override
void closeDialog(int npcId) override
void sell(int beingId) override
void sendLetter(int npcId, const std::string &recipient, const std::string &text) override
void buy(int beingId) override
void sellItem(int beingId, int itemId, int amount) override
void talk(int npcId) override
void nextDialog(int npcId) override
void buyItem(int beingId, int itemId, int amount) override
void endShopping(int beingId) override
void stringInput(int npcId, const std::string &value) override
void startShopping(int beingId) override
void menuSelect(int npcId, int choice) override
LocalPlayer * local_player
Net::NpcHandler * npcHandler
Definition net.cpp:54
Warning: buffers and other variables are shared, so there can be only one connection active at a time...
@ CMSG_NPC_SELL_REQUEST
Definition protocol.h:223
@ CMSG_NPC_NEXT_REQUEST
Definition protocol.h:211
@ SMSG_NPC_COMMAND
Definition protocol.h:298
@ CMSG_NPC_STR_RESPONSE
Definition protocol.h:287
@ SMSG_NPC_INT_INPUT
Definition protocol.h:273
@ SMSG_NPC_NEXT
Definition protocol.h:207
@ SMSG_NPC_MESSAGE
Definition protocol.h:206
@ CMSG_NPC_INT_RESPONSE
Definition protocol.h:274
@ CMSG_NPC_LIST_CHOICE
Definition protocol.h:210
@ SMSG_NPC_STR_INPUT
Definition protocol.h:286
@ CMSG_NPC_TALK
Definition protocol.h:176
@ CMSG_NPC_BUY_REQUEST
Definition protocol.h:222
@ CMSG_NPC_CLOSE
Definition protocol.h:275
@ CMSG_NPC_BUY_SELL_REQUEST
Definition protocol.h:219
@ SMSG_NPC_CHOICE
Definition protocol.h:209
@ SMSG_NPC_CLOSE
Definition protocol.h:208
@ NPC_CLOSE_DIALOG
Definition protocol.h:123
@ NPC_CLEAR_DIALOG
Definition protocol.h:127
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68