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-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "net/ea/npcrecv.h"
24 
25 #include "gui/viewport.h"
26 
27 #include "gui/windows/npcdialog.h"
28 
29 #include "net/messagein.h"
30 #include "net/npchandler.h"
31 
32 #include "utils/langs.h"
33 
34 #include "debug.h"
35 
36 namespace Ea
37 {
38 
39 namespace NpcRecv
40 {
41  NpcDialog *mDialog = nullptr;
42  bool mRequestLang = false;
43 } // namespace NpcRecv
44 
46 {
47  msg.readInt16("len");
49  mRequestLang = false;
50 
51  if (mDialog != nullptr)
52  {
55  "select items"));
56  }
57  else
58  {
59  msg.readString(msg.getLength() - 8, "select items");
60  }
61 }
62 
64 {
65  msg.readInt16("len");
67  mRequestLang = false;
68 
69  const std::string message = msg.readString(msg.getLength() - 8, "message");
70  // ignore future legacy npc commands.
71  if (message.size() > 3 && message.substr(0, 3) == "###")
72  return;
73  if (mDialog != nullptr)
74  mDialog->addText(message, true);
75 }
76 
78 {
79  // Show the close button
81  mRequestLang = false;
82  if (mDialog != nullptr)
84 }
85 
87 {
88  // Show the next button
90  mRequestLang = false;
91  if (mDialog != nullptr)
93 }
94 
96 {
97  // Request for an integer
99  mRequestLang = false;
100  if (mDialog != nullptr)
101  mDialog->integerRequest(0, 0, 2147483647);
102 }
103 
105 {
106  // Request for a string
107  BeingId npcId = npcHandler->getNpc(msg, NpcAction::Other);
108  if (mRequestLang)
109  {
110  mRequestLang = false;
112  }
113  else if (mDialog != nullptr)
114  {
115  mDialog->textRequest(std::string());
116  }
117 }
118 
120 {
121  const BeingId npcId = npcHandler->getNpc(msg, NpcAction::Other);
122  mRequestLang = false;
123 
124  const int cmd = msg.readInt16("cmd");
125  const BeingId id = msg.readBeingId("id");
126  const int x = msg.readInt16("x");
127  const int y = msg.readInt16("y");
128  switch (cmd)
129  {
130  case 0:
131  mRequestLang = true;
132  break;
133 
134  case 1:
135  if (viewport != nullptr)
136  viewport->moveCameraToActor(npcId, 0, 0);
137  break;
138 
139  case 2:
140  if (viewport != nullptr)
141  {
142  if (id == BeingId_zero)
144  else
145  viewport->moveCameraToActor(id, x, y);
146  }
147  break;
148 
149  case 3:
150  if (viewport != nullptr)
152  break;
153 
154  case 4:
155  if (viewport != nullptr)
156  {
158  }
159  break;
160  case 5: // close dialog
161  if (mDialog != nullptr)
163  npcHandler->closeDialog(npcId);
164  break;
165  case 6: // show avatar
166  if (mDialog != nullptr)
167  {
169  }
170  break;
171  case 7: // set avatar direction
172  if (mDialog != nullptr)
173  {
176  CAST_U8(id)));
177  }
178  break;
179  case 8: // set avatar action
180  if (mDialog != nullptr)
181  mDialog->setAvatarAction(toInt(id, int));
182  break;
183  case 9: // clear npc dialog
184  if (mDialog != nullptr)
185  mDialog->clearRows();
186  break;
187  case 10: // send selected item id
188  {
189  int invSize = toInt(id, int);
190  if (invSize == 0)
191  invSize = 1;
192  if (mDialog != nullptr)
193  mDialog->itemRequest(invSize);
194  break;
195  }
196  case 11: // send selected item index
197  {
198  int invSize = toInt(id, int);
199  if (invSize == 0)
200  invSize = 1;
201  if (mDialog != nullptr)
202  mDialog->itemIndexRequest(invSize);
203  break;
204  }
205  case 12: // send complex items
206  {
207  int invSize = toInt(id, int);
208  if (invSize == 0)
209  invSize = 1;
210  if (mDialog != nullptr)
211  mDialog->itemCraftRequest(invSize);
212  break;
213  }
214  case 14:
215  {
216  const NpcDialogs::iterator it = NpcDialog::mNpcDialogs.find(npcId);
217  if (it != NpcDialog::mNpcDialogs.end())
218  {
219  NpcDialog *const dialog = (*it).second;
220  if (dialog != nullptr)
221  dialog->close();
222  if (dialog == Ea::NpcRecv::mDialog)
223  Ea::NpcRecv::mDialog = nullptr;
224  NpcDialog::mNpcDialogs.erase(it);
225  }
226  break;
227  }
228  default:
230  break;
231  }
232 }
233 
235 {
236  msg.readInt16("len");
238  mRequestLang = false;
239  if (mDialog != nullptr)
240  {
241  const std::string str = msg.readString(-1, "title");
242  mDialog->setCaption(str);
243  }
244 }
245 
246 } // namespace Ea
void moveCameraToPosition(const int x, const int y)
Definition: viewport.cpp:1091
#define CAST_U8
Definition: cast.h:26
void addText(const std::string &string, const bool save)
Definition: npcdialog.cpp:290
void moveCameraRelative(const int x, const int y)
Definition: viewport.cpp:1102
BeingId readBeingId(const char *const str)
Definition: messagein.cpp:211
void showNextButton()
Definition: npcdialog.cpp:306
void moveCameraToActor(const BeingId actorId, const int x, const int y)
Definition: viewport.cpp:1076
#define fromInt(val, name)
Definition: intdefines.h:45
void processChangeTitle(Net::MessageIn &msg)
Definition: npcrecv.cpp:234
void processNpcCommand(Net::MessageIn &msg)
Definition: npcrecv.cpp:119
virtual void stringInput(const BeingId npcId, const std::string &value) const =0
NpcDialog * mDialog
Definition: npcrecv.cpp:41
unsigned int getLength() const
Definition: messagein.h:58
int BeingId
Definition: beingid.h:29
void choiceRequest()
Definition: npcdialog.cpp:685
void integerRequest(const int defaultValue, const int min, const int max)
Definition: npcdialog.cpp:778
bool msg(InputEvent &event)
Definition: chat.cpp:38
void setAvatarDirection(const uint8_t direction)
Definition: npcdialog.cpp:1176
void restoreCamera()
Definition: npcdialog.cpp:1114
void itemCraftRequest(const int size)
Definition: npcdialog.cpp:806
bool mRequestLang
Definition: npcrecv.cpp:42
virtual void close()
Definition: window.cpp:897
#define toInt(val, name)
Definition: intdefines.h:46
int16_t readInt16(const char *const str)
Definition: messagein.cpp:125
void clearRows()
Definition: npcdialog.cpp:1211
void returnCamera()
Definition: viewport.cpp:1110
static NpcDialogs mNpcDialogs
Definition: npcdialog.h:241
void processNpcMessage(Net::MessageIn &msg)
Definition: npcrecv.cpp:63
void processNpcChoice(Net::MessageIn &msg)
Definition: npcrecv.cpp:45
virtual BeingId getNpc(Net::MessageIn &msg, const NpcActionT action)=0
void parseListItems(const std::string &itemString)
Definition: npcdialog.cpp:705
void showAvatar(const BeingTypeId avatarId)
Definition: npcdialog.cpp:1129
void processNpcIntInput(Net::MessageIn &msg)
Definition: npcrecv.cpp:95
void processNpcStrInput(Net::MessageIn &msg)
Definition: npcrecv.cpp:104
void processNpcClose(Net::MessageIn &msg)
Definition: npcrecv.cpp:77
void showCloseButton()
Definition: npcdialog.cpp:312
void textRequest(const std::string &defaultText)
Definition: npcdialog.cpp:746
std::string readString(int length, const char *const dstr)
Definition: messagein.cpp:355
void processNpcNext(Net::MessageIn &msg)
Definition: npcrecv.cpp:86
Net::NpcHandler * npcHandler
Definition: net.cpp:89
const BeingId BeingId_zero
Definition: beingid.h:29
void setAvatarAction(const int actionId)
Definition: npcdialog.cpp:1183
static uint8_t fromServerDirection(const uint8_t serverDir)
Definition: messagein.cpp:237
void itemIndexRequest(const int size)
Definition: npcdialog.cpp:798
void setCaption(const std::string &caption)
Definition: window.h:530
int BeingTypeId
Definition: beingtypeid.h:29
void itemRequest(const int size)
Definition: npcdialog.cpp:790
virtual void closeDialog(const BeingId npcId)=0
Viewport * viewport
Definition: viewport.cpp:35
#define UNIMPLEMENTEDPACKETFIELD(field)
Definition: logger.h:58
std::string getLangSimple()
Definition: langs.cpp:84