ManaPlus
npchandler.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "net/tmwa/npchandler.h"
25 
26 #include "being/localplayer.h"
27 
28 #include "const/net/inventory.h"
29 
30 #include "gui/windows/npcdialog.h"
31 
33 
34 #include "net/messagein.h"
35 
36 #include "net/tmwa/messageout.h"
37 #include "net/tmwa/protocolout.h"
38 
39 #include "net/ea/npcrecv.h"
40 
41 #include "debug.h"
42 
43 namespace TmwAthena
44 {
45 
47  Ea::NpcHandler()
48 {
49  npcHandler = this;
50 }
51 
53 {
54  npcHandler = nullptr;
55 }
56 
57 void NpcHandler::talk(const Being *const being) const
58 {
59  if (being == nullptr)
60  return;
61  createOutPacket(CMSG_NPC_TALK);
62  outMsg.writeBeingId(being->getId(), "npc id");
63  outMsg.writeInt8(0, "unused");
64 }
65 
66 void NpcHandler::nextDialog(const BeingId npcId) const
67 {
68  createOutPacket(CMSG_NPC_NEXT_REQUEST);
69  outMsg.writeBeingId(npcId, "npc id");
70 }
71 
73 {
74  createOutPacket(CMSG_NPC_CLOSE);
75  outMsg.writeBeingId(npcId, "npc id");
76 
77  const NpcDialogs::iterator it = NpcDialog::mNpcDialogs.find(npcId);
78  if (it != NpcDialog::mNpcDialogs.end())
79  {
80  NpcDialog *const dialog = (*it).second;
81  if (dialog != nullptr)
82  dialog->close();
83  if (dialog == Ea::NpcRecv::mDialog)
84  Ea::NpcRecv::mDialog = nullptr;
85  NpcDialog::mNpcDialogs.erase(it);
86  }
87 }
88 
89 void NpcHandler::listInput(const BeingId npcId,
90  const unsigned char value) const
91 {
92  createOutPacket(CMSG_NPC_LIST_CHOICE);
93  outMsg.writeBeingId(npcId, "npc id");
94  outMsg.writeInt8(value, "value");
95 }
96 
98  const int value) const
99 {
100  createOutPacket(CMSG_NPC_INT_RESPONSE);
101  outMsg.writeBeingId(npcId, "npc id");
102  outMsg.writeInt32(value, "value");
103 }
104 
106  const std::string &value) const
107 {
108  createOutPacket(CMSG_NPC_STR_RESPONSE);
109  outMsg.writeInt16(CAST_S16(value.length() + 9), "len");
110  outMsg.writeBeingId(npcId, "npc id");
111  outMsg.writeString(value, CAST_S32(value.length()), "value");
112  outMsg.writeInt8(0, "null byte");
113 }
114 
115 void NpcHandler::buy(const Being *const being) const
116 {
117  if (being == nullptr)
118  return;
119  createOutPacket(CMSG_NPC_BUY_SELL_REQUEST);
120  outMsg.writeBeingId(being->getId(), "npc id");
121  outMsg.writeInt8(0, "action");
122 }
123 
124 void NpcHandler::buy(const BeingId beingId) const
125 {
126  createOutPacket(CMSG_NPC_BUY_SELL_REQUEST);
127  outMsg.writeBeingId(beingId, "npc id");
128  outMsg.writeInt8(0, "action");
129 }
130 
131 void NpcHandler::sell(const BeingId beingId) const
132 {
133  createOutPacket(CMSG_NPC_BUY_SELL_REQUEST);
134  outMsg.writeBeingId(beingId, "npc id");
135  outMsg.writeInt8(1, "action");
136 }
137 
139  const int itemId,
140  const ItemColor color A_UNUSED,
141  const int amount) const
142 {
143  createOutPacket(CMSG_NPC_BUY_REQUEST);
144  outMsg.writeInt16(8, "len");
145  outMsg.writeInt16(CAST_S16(amount), "amount");
146  outMsg.writeInt16(CAST_S16(itemId), "item id");
147 }
148 
149 void NpcHandler::buyItems(STD_VECTOR<ShopItem*> &items A_UNUSED) const
150 {
151 }
152 
154  const int itemId,
155  const int amount) const
156 {
157  createOutPacket(CMSG_NPC_SELL_REQUEST);
158  outMsg.writeInt16(8, "len");
159  outMsg.writeInt16(CAST_S16(
160  itemId + INVENTORY_OFFSET), "index");
161  outMsg.writeInt16(CAST_S16(amount), "amount");
162 }
163 
164 void NpcHandler::sellItems(STD_VECTOR<ShopItem*> &items A_UNUSED) const
165 {
166 }
167 
169 {
170 }
171 
172 void NpcHandler::produceMix(const int nameId A_UNUSED,
173  const int materialId1 A_UNUSED,
174  const int materialId2 A_UNUSED,
175  const int materialId3 A_UNUSED) const
176 {
177 }
178 
180  const int nameId A_UNUSED) const
181 {
182 }
183 
184 void NpcHandler::repair(const int index A_UNUSED) const
185 {
186 }
187 
188 void NpcHandler::refine(const int index A_UNUSED) const
189 {
190 }
191 
192 void NpcHandler::identify(const int index A_UNUSED) const
193 {
194 }
195 
196 void NpcHandler::selectArrow(const int nameId A_UNUSED) const
197 {
198 }
199 
200 void NpcHandler::selectAutoSpell(const int skillId A_UNUSED) const
201 {
202 }
203 
205  const NpcActionT action)
206 {
207  const BeingId npcId = msg.readBeingId("npc id");
208 
209  const NpcDialogs::const_iterator diag = NpcDialog::mNpcDialogs.find(npcId);
210 
211  if (diag == NpcDialog::mNpcDialogs.end())
212  {
213  Ea::NpcRecv::mDialog = nullptr;
214  // Empty dialogs don't help
215  if (action == NpcAction::Close)
216  {
217  closeDialog(npcId);
218  return npcId;
219  }
220  else if (action == NpcAction::Next)
221  {
222  nextDialog(npcId);
223  return npcId;
224  }
225  else
226  {
229  if (localPlayer != nullptr)
230  localPlayer->stopWalking(false);
232  }
233  }
234  else
235  {
236  NpcDialog *const dialog = diag->second;
237  if (Ea::NpcRecv::mDialog != nullptr && Ea::NpcRecv::mDialog != dialog)
239  Ea::NpcRecv::mDialog = dialog;
240  if (Ea::NpcRecv::mDialog != nullptr)
242  }
243  return npcId;
244 }
245 
246 void NpcHandler::requestAirship(const std::string &mapName A_UNUSED,
247  const int itemId A_UNUSED) const
248 {
249 }
250 
251 } // namespace TmwAthena
int BeingId
Definition: beingid.h:30
#define CAST_S16
Definition: cast.h:28
#define CAST_S32
Definition: cast.h:30
BeingId getId() const
Definition: actorsprite.h:64
Definition: being.h:96
void stopWalking(const bool sendToServer)
void saveCamera()
Definition: npcdialog.cpp:1105
static NpcDialogs mNpcDialogs
Definition: npcdialog.h:242
void restoreCamera()
Definition: npcdialog.cpp:1115
void buyItem(const BeingId beingId, const int itemId, const ItemColor color, const int amount) const
Definition: npchandler.cpp:138
void repair(const int index) const
Definition: npchandler.cpp:184
void cooking(const CookingTypeT type, const int nameId) const
Definition: npchandler.cpp:179
void sellItem(const BeingId beingId, const int itemId, const int amount) const
Definition: npchandler.cpp:153
void refine(const int index) const
Definition: npchandler.cpp:188
void listInput(const BeingId npcId, const unsigned char value) const
Definition: npchandler.cpp:89
void nextDialog(const BeingId npcId) const
Definition: npchandler.cpp:66
void identify(const int index) const
Definition: npchandler.cpp:192
void talk(const Being *const being) const
Definition: npchandler.cpp:57
void completeProgressBar() const
Definition: npchandler.cpp:168
void produceMix(const int nameId, const int materialId1, const int materialId2, const int materialId3) const
Definition: npchandler.cpp:172
void integerInput(const BeingId npcId, const int value) const
Definition: npchandler.cpp:97
void requestAirship(const std::string &mapName, const int itemId) const
Definition: npchandler.cpp:246
void sellItems(std::vector< ShopItem * > &items) const
Definition: npchandler.cpp:164
void closeDialog(const BeingId npcId)
Definition: npchandler.cpp:72
BeingId getNpc(Net::MessageIn &msg, const NpcActionT action)
Definition: npchandler.cpp:204
void sell(const BeingId beingId) const
Definition: npchandler.cpp:131
void selectArrow(const int nameId) const
Definition: npchandler.cpp:196
void selectAutoSpell(const int skillId) const
Definition: npchandler.cpp:200
void buyItems(std::vector< ShopItem * > &items) const
Definition: npchandler.cpp:149
void stringInput(const BeingId npcId, const std::string &value) const
Definition: npchandler.cpp:105
void buy(const Being *const being) const
Definition: npchandler.cpp:115
virtual void close()
Definition: window.cpp:902
static const int INVENTORY_OFFSET
Definition: inventory.h:27
CookingType ::T CookingTypeT
Definition: cookingtype.h:36
#define CREATEWIDGETV(var, type,...)
Definition: createwidget.h:25
uint16_t ItemColor
Definition: itemcolor.h:30
#define A_UNUSED
Definition: localconsts.h:160
LocalPlayer * localPlayer
#define createOutPacket(name)
Definition: messageout.h:37
bool msg(InputEvent &event)
Definition: chat.cpp:39
NpcDialog * mDialog
Definition: npcrecv.cpp:42
Net::NpcHandler * npcHandler
Definition: net.cpp:93
NpcAction ::T NpcActionT
Definition: npcaction.h:33