ManaPlus
npcrecv.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/ea/npcrecv.h"
25 
26 #include "gui/viewport.h"
27 
28 #include "gui/windows/npcdialog.h"
29 
30 #include "net/messagein.h"
31 #include "net/npchandler.h"
32 
33 #include "utils/langs.h"
34 
35 #include "debug.h"
36 
37 namespace Ea
38 {
39 
40 namespace NpcRecv
41 {
42  NpcDialog *mDialog = nullptr;
43  bool mRequestLang = false;
44 } // namespace NpcRecv
45 
47 {
48  msg.readInt16("len");
50  mRequestLang = false;
51 
52  if (mDialog != nullptr)
53  {
55  mDialog->parseListItems(msg.readString(msg.getLength() - 8,
56  "select items"));
57  }
58  else
59  {
60  msg.readString(msg.getLength() - 8, "select items");
61  }
62 }
63 
65 {
66  msg.readInt16("len");
68  mRequestLang = false;
69 
70  const std::string message = msg.readString(msg.getLength() - 8, "message");
71  // ignore future legacy npc commands.
72  if (message.size() > 3 && message.substr(0, 3) == "###")
73  return;
74  if (mDialog != nullptr)
75  mDialog->addText(message, true);
76 }
77 
79 {
80  // Show the close button
82  mRequestLang = false;
83  if (mDialog != nullptr)
85 }
86 
88 {
89  // Show the next button
91  mRequestLang = false;
92  if (mDialog != nullptr)
94 }
95 
97 {
98  // Request for an integer
100  mRequestLang = false;
101  if (mDialog != nullptr)
102  mDialog->integerRequest(0, 0, 2147483647);
103 }
104 
106 {
107  // Request for a string
109  if (mRequestLang)
110  {
111  mRequestLang = false;
113  }
114  else if (mDialog != nullptr)
115  {
116  mDialog->textRequest(std::string());
117  }
118 }
119 
121 {
122  const BeingId npcId = npcHandler->getNpc(msg, NpcAction::Other);
123  mRequestLang = false;
124 
125  const int cmd = msg.readInt16("cmd");
126  const BeingId id = msg.readBeingId("id");
127  const int x = msg.readInt16("x");
128  const int y = msg.readInt16("y");
129  switch (cmd)
130  {
131  case 0:
132  mRequestLang = true;
133  break;
134 
135  case 1:
136  if (viewport != nullptr)
137  viewport->moveCameraToActor(npcId, 0, 0);
138  break;
139 
140  case 2:
141  if (viewport != nullptr)
142  {
143  if (id == BeingId_zero)
145  else
146  viewport->moveCameraToActor(id, x, y);
147  }
148  break;
149 
150  case 3:
151  if (viewport != nullptr)
153  break;
154 
155  case 4:
156  if (viewport != nullptr)
157  {
159  }
160  break;
161  case 5: // close dialog
162  if (mDialog != nullptr)
164  npcHandler->closeDialog(npcId);
165  break;
166  case 6: // show avatar
167  if (mDialog != nullptr)
168  {
170  }
171  break;
172  case 7: // set avatar direction
173  if (mDialog != nullptr)
174  {
177  CAST_U8(id)));
178  }
179  break;
180  case 8: // set avatar action
181  if (mDialog != nullptr)
182  mDialog->setAvatarAction(toInt(id, int));
183  break;
184  case 9: // clear npc dialog
185  if (mDialog != nullptr)
186  mDialog->clearRows();
187  break;
188  case 10: // send selected item id
189  {
190  int invSize = toInt(id, int);
191  if (invSize == 0)
192  invSize = 1;
193  if (mDialog != nullptr)
194  mDialog->itemRequest(invSize);
195  break;
196  }
197  case 11: // send selected item index
198  {
199  int invSize = toInt(id, int);
200  if (invSize == 0)
201  invSize = 1;
202  if (mDialog != nullptr)
203  mDialog->itemIndexRequest(invSize);
204  break;
205  }
206  case 12: // send complex items
207  {
208  int invSize = toInt(id, int);
209  if (invSize == 0)
210  invSize = 1;
211  if (mDialog != nullptr)
212  mDialog->itemCraftRequest(invSize);
213  break;
214  }
215  case 14:
216  {
217  const NpcDialogs::iterator it = NpcDialog::mNpcDialogs.find(npcId);
218  if (it != NpcDialog::mNpcDialogs.end())
219  {
220  NpcDialog *const dialog = (*it).second;
221  if (dialog != nullptr)
222  dialog->close();
223  if (dialog == Ea::NpcRecv::mDialog)
224  Ea::NpcRecv::mDialog = nullptr;
225  NpcDialog::mNpcDialogs.erase(it);
226  }
227  break;
228  }
229  default:
231  break;
232  }
233 }
234 
236 {
237  msg.readInt16("len");
239  mRequestLang = false;
240  if (mDialog != nullptr)
241  {
242  const std::string str = msg.readString(-1, "title");
243  mDialog->setCaption(str);
244  }
245 }
246 
247 } // namespace Ea
int BeingId
Definition: beingid.h:30
const BeingId BeingId_zero
Definition: beingid.h:30
int BeingTypeId
Definition: beingtypeid.h:30
#define CAST_U8
Definition: cast.h:27
static uint8_t fromServerDirection(const uint8_t serverDir)
Definition: messagein.cpp:266
virtual BeingId getNpc(Net::MessageIn &msg, const NpcActionT action)=0
virtual void closeDialog(const BeingId npcId)=0
virtual void stringInput(const BeingId npcId, const std::string &value) const =0
void itemRequest(const int size)
Definition: npcdialog.cpp:791
void textRequest(const std::string &defaultText)
Definition: npcdialog.cpp:747
void addText(const std::string &string, const bool save)
Definition: npcdialog.cpp:291
void itemCraftRequest(const int size)
Definition: npcdialog.cpp:807
void itemIndexRequest(const int size)
Definition: npcdialog.cpp:799
void parseListItems(const std::string &itemString)
Definition: npcdialog.cpp:706
void choiceRequest()
Definition: npcdialog.cpp:686
void setAvatarAction(const int actionId)
Definition: npcdialog.cpp:1184
void showNextButton()
Definition: npcdialog.cpp:307
void showAvatar(const BeingTypeId avatarId)
Definition: npcdialog.cpp:1130
void setAvatarDirection(const uint8_t direction)
Definition: npcdialog.cpp:1177
static NpcDialogs mNpcDialogs
Definition: npcdialog.h:242
void showCloseButton()
Definition: npcdialog.cpp:313
void clearRows()
Definition: npcdialog.cpp:1212
void restoreCamera()
Definition: npcdialog.cpp:1115
void integerRequest(const int defaultValue, const int min, const int max)
Definition: npcdialog.cpp:779
void moveCameraToActor(const BeingId actorId, const int x, const int y)
Definition: viewport.cpp:1077
void moveCameraRelative(const int x, const int y)
Definition: viewport.cpp:1103
void returnCamera()
Definition: viewport.cpp:1111
void moveCameraToPosition(const int x, const int y)
Definition: viewport.cpp:1092
virtual void close()
Definition: window.cpp:902
void setCaption(const std::string &caption)
Definition: window.h:531
Viewport * viewport
Definition: viewport.cpp:36
#define toInt(val, name)
Definition: intdefines.h:47
#define fromInt(val, name)
Definition: intdefines.h:46
std::string getLangSimple()
Definition: langs.cpp:85
#define UNIMPLEMENTEDPACKETFIELD(field)
Definition: logger.h:59
bool msg(InputEvent &event)
Definition: chat.cpp:39
void processNpcMessage(Net::MessageIn &msg)
Definition: npcrecv.cpp:64
NpcDialog * mDialog
Definition: npcrecv.cpp:42
void processNpcStrInput(Net::MessageIn &msg)
Definition: npcrecv.cpp:105
void processChangeTitle(Net::MessageIn &msg)
Definition: npcrecv.cpp:235
void processNpcIntInput(Net::MessageIn &msg)
Definition: npcrecv.cpp:96
bool mRequestLang
Definition: npcrecv.cpp:43
void processNpcCommand(Net::MessageIn &msg)
Definition: npcrecv.cpp:120
void processNpcNext(Net::MessageIn &msg)
Definition: npcrecv.cpp:87
void processNpcClose(Net::MessageIn &msg)
Definition: npcrecv.cpp:78
void processNpcChoice(Net::MessageIn &msg)
Definition: npcrecv.cpp:46
Net::NpcHandler * npcHandler
Definition: net.cpp:93