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-2019 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 
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::MessageIn
Definition: messagein.h:40
anonymous_namespace{stringutils.cpp}::base
unsigned int base
Definition: stringutils.cpp:226
playerhandler.h
notifymanager.h
Viewport::returnCamera
void returnCamera()
Definition: viewport.cpp:1110
settings.h
Map::getWidth
int getWidth() const
Definition: map.h:165
Map::setMask
void setMask(const int mask)
Definition: map.cpp:1675
stdmove.h
Notify_false
const bool Notify_false
Definition: notify.h:29
NoStat
static const int NoStat
Definition: nostat.h:26
PlayerInfo::getAttribute
int32_t getAttribute(const AttributesT id)
Definition: playerinfo.cpp:101
statuswindow.h
Settings::enableRemoteCommands
bool enableRemoteCommands
Definition: settings.h:161
LocalPlayer::setAction
void setAction(const BeingActionT &action, const int attackId)
Definition: localplayer.cpp:408
Ea::PlayerRecv::processPlayerWarp
void processPlayerWarp(Net::MessageIn &msg)
Definition: playerrecv.cpp:59
settings
Settings settings
Definition: settings.cpp:31
NotifyTypes::ARROWS_EQUIP_NEEDED
@ ARROWS_EQUIP_NEEDED
Definition: notifytypes.h:86
Actions::msg
bool msg(InputEvent &event)
Definition: chat.cpp:38
Ea::PlayerRecv::processPlayerArrowMessage
void processPlayerArrowMessage(Net::MessageIn &msg)
Definition: playerrecv.cpp:206
Settings::awayMode
bool awayMode
Definition: settings.h:157
notifytypes.h
Ea::PlayerRecv::processMapMusic
void processMapMusic(Net::MessageIn &msg)
Definition: playerrecv.cpp:225
NotifyTypes::SKILL_RAISE_ERROR
@ SKILL_RAISE_ERROR
Definition: notifytypes.h:85
NotifyManager::notify
void notify(const unsigned int message)
Definition: notifymanager.cpp:81
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
Net::PlayerHandler::setStat
virtual void setStat(Net::MessageIn &msg, const int type, const int64_t base, const int mod, const Notify notify) const =0
Game::instance
static Game * instance()
Definition: game.h:81
parse2Str
bool parse2Str(const std::string &args, std::string &str1, std::string &str2)
Definition: stringutils.cpp:898
playerHandler
Net::PlayerHandler * playerHandler
Definition: net.cpp:95
Ea::PlayerRecv::processPlayerStatUpdate4
void processPlayerStatUpdate4(Net::MessageIn &msg)
Definition: playerrecv.cpp:173
Ea::PlayerRecv::processPlayerStatUpdate2
void processPlayerStatUpdate2(Net::MessageIn &msg)
Definition: playerrecv.cpp:153
LocalPlayer::navigateClean
void navigateClean()
Definition: localplayer.cpp:2154
inputmanager.h
LocalPlayer::setTileCoords
void setTileCoords(const int x, const int y)
Definition: localplayer.cpp:2662
logger
Logger * logger
Definition: logger.cpp:88
LocalPlayer::stopAttack
void stopAttack(const bool keepAttack)
Definition: localplayer.cpp:864
AttributesT
Attributes ::T AttributesT
Definition: attributes.h:117
Ea::PlayerRecv::processPlayerStatUpdate1
void processPlayerStatUpdate1(Net::MessageIn &msg)
Definition: playerrecv.cpp:138
localplayer.h
SoundManager::playMusic
void playMusic(const std::string &fileName, const SkipError skipError)
Definition: soundmanager.cpp:351
localPlayer
LocalPlayer * localPlayer
Definition: localplayer.cpp:108
Ea::PlayerRecv::processPlayerClientCommand
void processPlayerClientCommand(Net::MessageIn &msg)
Definition: playerrecv.cpp:246
inputManager
InputManager inputManager
Definition: inputmanager.cpp:68
MAP_TELEPORT_SCROLL_DISTANCE
static const int MAP_TELEPORT_SCROLL_DISTANCE
Definition: playerrecv.cpp:54
playerrecv.h
Notify_true
const bool Notify_true
Definition: notify.h:29
Map::getTileWidth
int getTileWidth() const
Definition: map.h:177
BeingAction::STAND
@ STAND
Definition: beingaction.h:30
playerinfo.h
Actions::ok
bool ok(InputEvent &event)
Definition: actions.cpp:33
Settings::pseudoAwayMode
bool pseudoAwayMode
Definition: settings.h:158
nostat.h
x
x
Definition: graphics_calcImageRect.hpp:72
Ea::PlayerRecv::processPlayerStatUpdate6
void processPlayerStatUpdate6(Net::MessageIn &msg)
Definition: playerrecv.cpp:196
Game::getCurrentMapName
const std::string & getCurrentMapName() const
Definition: game.h:109
InputManager::executeRemoteChatCommand
bool executeRemoteChatCommand(const std::string &cmd, const std::string &args, ChatTab *const tab)
Definition: inputmanager.cpp:921
soundmanager.h
CompoundSprite::reset
bool reset()
Definition: compoundsprite.cpp:100
stringutils.h
Logger::log1
void log1(const char *const log_text)
Definition: logger.cpp:237
viewport
Viewport * viewport
Definition: viewport.cpp:35
PlayerInfo::setAttribute
void setAttribute(const AttributesT id, const int64_t value, const Notify notify)
Definition: playerinfo.cpp:109
Game::getCurrentMap
Map * getCurrentMap() const
Definition: game.h:106
statusWindow
StatusWindow * statusWindow
Definition: statuswindow.cpp:64
Viewport::getMap
Map * getMap() const
Definition: viewport.h:134
Map::getHeight
int getHeight() const
Definition: map.h:171
EmoteDB::size
int size()
Definition: emotedb.cpp:305
UNIMPLEMENTEDPACKETFIELD
#define UNIMPLEMENTEDPACKETFIELD(field)
Definition: logger.h:58
Ea
Definition: adminhandler.cpp:30
Game::changeMap
void changeMap(const std::string &mapName)
Definition: game.cpp:1092
STD_MOVE
#define STD_MOVE(var)
Definition: stdmove.h:27
Map
Definition: map.h:71
Map::getTileHeight
int getTileHeight() const
Definition: map.h:183
BLOCK_START
#define BLOCK_START(name)
Definition: perfomance.h:78
Attributes::PLAYER_CHAR_POINTS
@ PLAYER_CHAR_POINTS
Definition: attributes.h:48
Ea::PlayerRecv::processMapMask
void processMapMask(Net::MessageIn &msg)
Definition: playerrecv.cpp:237
BLOCK_END
#define BLOCK_END(name)
Definition: perfomance.h:79
game.h
Viewport::scrollBy
void scrollBy(const int x, const int y)
Definition: viewport.h:126
Ea::PlayerRecv::processPlayerStatUpdate3
void processPlayerStatUpdate3(Net::MessageIn &msg)
Definition: playerrecv.cpp:162
Being::getTileY
int getTileY() const
Definition: being.h:173
Game
Definition: game.h:62
y
y
Definition: graphics_calcImageRect.hpp:72
soundManager
SoundManager soundManager
Definition: soundmanager.cpp:54
Map::setMusicFile
void setMusicFile(const std::string &file)
Definition: map.cpp:1685
PlayerInfo::getStatBase
int getStatBase(const AttributesT id)
Definition: playerinfo.cpp:134
Logger::log
void log(const char *const log_text,...)
Definition: logger.cpp:268
debug.h
Being::getTileX
int getTileX() const
Definition: being.h:167