ManaPlus
playerrecv.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/playerrecv.h"
24 
25 #include "game.h"
26 #include "notifymanager.h"
27 #include "settings.h"
28 #include "soundmanager.h"
29 
30 #include "being/localplayer.h"
31 #include "being/playerinfo.h"
32 
33 #include "const/net/nostat.h"
34 
36 
37 #include "gui/viewport.h"
38 
40 
41 #include "input/inputmanager.h"
42 
43 #include "resources/map/map.h"
44 
45 #include "net/playerhandler.h"
46 
47 #include "utils/stdmove.h"
48 #include "utils/stringutils.h"
49 
50 #include "debug.h"
51 
52 // Max. distance we are willing to scroll after a teleport;
53 // everything beyond will reset the port hard.
54 static const int MAP_TELEPORT_SCROLL_DISTANCE = 8;
55 
56 namespace Ea
57 {
58 
60 {
61  BLOCK_START("PlayerRecv::processPlayerWarp")
62  std::string mapPath = msg.readString(16, "map name");
63  int x = msg.readInt16("x");
64  int y = msg.readInt16("y");
65 
66  logger->log("Warping to %s (%d, %d)", mapPath.c_str(), x, y);
67 
68  if (localPlayer == nullptr)
69  logger->log1("SMSG_PLAYER_WARP localPlayer null");
70 
71  /*
72  * We must clear the local player's target *before* the call
73  * to changeMap, as it deletes all beings.
74  */
75  if (localPlayer != nullptr)
76  localPlayer->stopAttack(false);
77 
78  Game *const game = Game::instance();
79  if (game == nullptr)
80  {
81  BLOCK_END("PlayerRecv::processPlayerWarp")
82  return;
83  }
84 
85  const std::string &currentMapName = game->getCurrentMapName();
86  const bool sameMap = (currentMapName == mapPath);
87 
88  // Switch the actual map, deleting the previous one if necessary
89  mapPath = mapPath.substr(0, mapPath.rfind('.'));
90  game->changeMap(mapPath);
91 
92  int scrollOffsetX = 0;
93  int scrollOffsetY = 0;
94 
95  if (localPlayer != nullptr)
96  {
97  const Map *const map = game->getCurrentMap();
98  if (map != nullptr)
99  {
100  if (x >= map->getWidth())
101  x = map->getWidth() - 1;
102  if (y >= map->getHeight())
103  y = map->getHeight() - 1;
104  if (x < 0)
105  x = 0;
106  if (y < 0)
107  y = 0;
108  /* Scroll if neccessary */
109  if (!sameMap
110  || (abs(x - localPlayer->getTileX())
112  || (abs(y - localPlayer->getTileY())
114  {
115  scrollOffsetX = (x - localPlayer->getTileX())
116  * map->getTileWidth();
117  scrollOffsetY = (y - localPlayer->getTileY())
118  * map->getTileHeight();
119  }
120  }
121 
123  localPlayer->setTileCoords(x, y);
125  localPlayer->reset();
126  }
127 
128  logger->log("Adjust scrolling by %d:%d", scrollOffsetX, scrollOffsetY);
129 
130  if (viewport != nullptr)
131  {
133  viewport->scrollBy(scrollOffsetX, scrollOffsetY);
134  }
135  BLOCK_END("PlayerRecv::processPlayerWarp")
136 }
137 
139 {
140  BLOCK_START("PlayerRecv::processPlayerStatUpdate1")
141  const int type = msg.readInt16("type");
142  const int value = msg.readInt32("value");
143  if (localPlayer == nullptr)
144  {
145  BLOCK_END("PlayerRecv::processPlayerStatUpdate1")
146  return;
147  }
148 
149  playerHandler->setStat(msg, type, value, NoStat, Notify_true);
150  BLOCK_END("PlayerRecv::processPlayerStatUpdate1")
151 }
152 
154 {
155  BLOCK_START("PlayerRecv::processPlayerStatUpdate2")
156  const int type = msg.readInt16("type");
157  const int value = msg.readInt32("value");
158  playerHandler->setStat(msg, type, value, NoStat, Notify_true);
159  BLOCK_END("PlayerRecv::processPlayerStatUpdate2")
160 }
161 
163 {
164  BLOCK_START("PlayerRecv::processPlayerStatUpdate3")
165  const int type = msg.readInt32("type");
166  const int base = msg.readInt32("base");
167  const int bonus = msg.readInt32("bonus");
168 
169  playerHandler->setStat(msg, type, base, bonus, Notify_false);
170  BLOCK_END("PlayerRecv::processPlayerStatUpdate3")
171 }
172 
174 {
175  BLOCK_START("PlayerRecv::processPlayerStatUpdate4")
176  const uint16_t type = msg.readInt16("type");
177  const uint8_t ok = msg.readUInt8("flag");
178  const int value = msg.readUInt8("value");
179 
180  if (ok != 1)
181  {
182  const int oldValue = PlayerInfo::getStatBase(
183  static_cast<AttributesT>(type));
184  const int points = PlayerInfo::getAttribute(
185  Attributes::PLAYER_CHAR_POINTS) + oldValue - value;
187  points,
188  Notify_true);
190  }
191 
192  playerHandler->setStat(msg, type, value, NoStat, Notify_true);
193  BLOCK_END("PlayerRecv::processPlayerStatUpdate4")
194 }
195 
197 {
198  BLOCK_START("PlayerRecv::processPlayerStatUpdate6")
199  const int type = msg.readInt16("type");
200  const int value = msg.readUInt8("value");
201  if (statusWindow != nullptr)
202  playerHandler->setStat(msg, type, value, NoStat, Notify_true);
203  BLOCK_END("PlayerRecv::processPlayerStatUpdate6")
204 }
205 
207 {
208  BLOCK_START("PlayerRecv::processPlayerArrowMessage")
209  const int type = msg.readInt16("type");
210  switch (type)
211  {
212  case 0:
214  break;
215  case 3:
216  // arrows equiped
217  break;
218  default:
220  break;
221  }
222  BLOCK_END("PlayerRecv::processPlayerArrowMessage")
223 }
224 
226 {
227  const int size = msg.readInt16("len") - 4;
228  const std::string music = msg.readString(size, "name");
229  soundManager.playMusic(music,
231 
232  Map *const map = viewport->getMap();
233  if (map != nullptr)
234  map->setMusicFile(music);
235 }
236 
238 {
239  const int mask = msg.readInt32("mask");
240  msg.readInt32("unused");
241  Map *const map = Game::instance()->getCurrentMap();
242  if (map != nullptr)
243  map->setMask(mask);
244 }
245 
247 {
248  const int sz = msg.readInt16("len") - 4;
249  std::string command = msg.readString(sz, "command");
250  std::string cmd;
251  std::string args;
252 
253  if (settings.awayMode ||
256  {
257  return;
258  }
259 
260  if (!parse2Str(command, cmd, args))
261  {
262  cmd = STD_MOVE(command);
263  args.clear();
264  }
265  inputManager.executeRemoteChatCommand(cmd, args, nullptr);
266 }
267 
268 } // namespace Ea
Net::PlayerHandler * playerHandler
Definition: net.cpp:89
virtual unsigned char readUInt8(const char *const str)
Definition: messagein.cpp:74
const bool SkipError_false
Definition: skiperror.h:29
void log1(const char *const log_text)
Definition: logger.cpp:222
int getTileY() const
Definition: being.h:173
void setTileCoords(const int x, const int y)
Definition: being.cpp:4980
int getTileHeight() const
Definition: map.h:180
const std::string & getCurrentMapName() const
Definition: game.h:109
bool parse2Str(const std::string &args, std::string &str1, std::string &str2)
#define BLOCK_START(name)
Definition: perfomance.h:78
SoundManager soundManager
void processPlayerStatUpdate4(Net::MessageIn &msg)
Definition: playerrecv.cpp:173
virtual int16_t readInt16(const char *const str)=0
void processPlayerWarp(Net::MessageIn &msg)
Definition: playerrecv.cpp:59
StatusWindow * statusWindow
#define BLOCK_END(name)
Definition: perfomance.h:79
void processPlayerArrowMessage(Net::MessageIn &msg)
Definition: playerrecv.cpp:206
bool msg(InputEvent &event)
Definition: chat.cpp:38
void notify(const unsigned int message)
Map * getCurrentMap() const
Definition: game.h:106
int getHeight() const
Definition: map.h:168
bool enableRemoteCommands
Definition: settings.h:160
Logger * logger
Definition: logger.cpp:95
bool ok(InputEvent &event)
Definition: actions.cpp:33
Settings settings
Definition: settings.cpp:31
void processPlayerStatUpdate6(Net::MessageIn &msg)
Definition: playerrecv.cpp:196
void changeMap(const std::string &mapName)
Definition: game.cpp:1086
void returnCamera()
Definition: viewport.cpp:1110
void processPlayerStatUpdate3(Net::MessageIn &msg)
Definition: playerrecv.cpp:162
void scrollBy(const int x, const int y)
Definition: viewport.h:126
static const int NoStat
Definition: nostat.h:26
void navigateClean()
Definition: game.h:62
void setMask(const int mask)
Definition: map.cpp:1664
static const int MAP_TELEPORT_SCROLL_DISTANCE
Definition: playerrecv.cpp:54
LocalPlayer * localPlayer
void setAttribute(const AttributesT id, const int64_t value, const Notify notify)
Definition: playerinfo.cpp:108
void stopAttack(const bool keepAttack)
int getStatBase(const AttributesT id)
Definition: playerinfo.cpp:133
Definition: map.h:68
#define STD_MOVE(var)
Definition: stdmove.h:27
int getWidth() const
Definition: map.h:162
void playMusic(const std::string &fileName, const SkipError skipError)
bool executeRemoteChatCommand(const std::string &cmd, const std::string &args, ChatTab *const tab)
int getTileWidth() const
Definition: map.h:174
bool awayMode
Definition: settings.h:155
void processPlayerStatUpdate2(Net::MessageIn &msg)
Definition: playerrecv.cpp:153
virtual int32_t readInt32(const char *const str)=0
static Game * instance()
Definition: game.h:81
virtual std::string readString(int length, const char *const dstr)
Definition: messagein.cpp:219
void processPlayerClientCommand(Net::MessageIn &msg)
Definition: playerrecv.cpp:246
int getTileX() const
Definition: being.h:167
bool pseudoAwayMode
Definition: settings.h:156
InputManager inputManager
void log(const char *const log_text,...)
Definition: logger.cpp:243
Map * getMap() const
Definition: viewport.h:134
void processMapMusic(Net::MessageIn &msg)
Definition: playerrecv.cpp:225
void processMapMask(Net::MessageIn &msg)
Definition: playerrecv.cpp:237
int32_t getAttribute(const AttributesT id)
Definition: playerinfo.cpp:100
void setMusicFile(const std::string &file)
Definition: map.cpp:1674
virtual void setStat(Net::MessageIn &msg, const int type, const int64_t base, const int mod, const Notify notify) const =0
void processPlayerStatUpdate1(Net::MessageIn &msg)
Definition: playerrecv.cpp:138
const bool Notify_false
Definition: notify.h:29
void setAction(const BeingActionT &action, const int attackId)
const bool Notify_true
Definition: notify.h:29
Viewport * viewport
Definition: viewport.cpp:35
#define UNIMPLEMENTEDPACKETFIELD(field)
Definition: logger.h:58