ManaPlus
charserverrecv.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 
24 
25 #include "client.h"
26 #include "configuration.h"
27 #include "settings.h"
28 
30 #include "gui/windows/okdialog.h"
31 
33 
34 #include "net/character.h"
35 #include "net/charserverhandler.h"
36 #include "net/messagein.h"
37 #include "net/serverfeatures.h"
38 
39 #include "net/ea/token.h"
40 
41 #include "net/tmwa/gamehandler.h"
42 #include "net/tmwa/loginhandler.h"
43 #include "net/tmwa/network.h"
44 #include "net/tmwa/sprite.h"
45 
46 #include "resources/iteminfo.h"
47 
48 #include "resources/db/itemdb.h"
49 
50 #include "utils/dtor.h"
51 #include "utils/gettext.h"
52 
53 #include "debug.h"
54 
55 namespace TmwAthena
56 {
57 
59 
61 
63  Net::Character *const character)
64 {
65  if (character == nullptr)
66  return;
67 
68  const Token &token =
69  static_cast<LoginHandler*>(loginHandler)->getToken();
70 
71  LocalPlayer *const tempPlayer = new LocalPlayer(
72  msg.readBeingId("account id"), BeingTypeId_zero);
73 
74  PlayerInfoBackend &data = character->data;
75  data.mAttributes[Attributes::PLAYER_EXP] = msg.readInt32("base exp");
76  data.mAttributes[Attributes::MONEY] = msg.readInt32("money");
77  data.mAttributes[Attributes::PLAYER_JOB_EXP] = msg.readInt32("job exp");
78  data.mAttributes[Attributes::PLAYER_JOB_LEVEL] =
79  msg.readInt32("job level");
80 
81  const int shoes = msg.readInt16("shoes");
82  const int gloves = msg.readInt16("gloves");
83  const int cape = msg.readInt16("cape");
84  const int misc1 = msg.readInt16("misc1");
85 
86  msg.readInt32("option");
87  tempPlayer->setKarma(msg.readInt32("karma"));
88  tempPlayer->setManner(msg.readInt32("manner"));
89  msg.readInt16("character points left");
90 
91  data.mAttributes[Attributes::PLAYER_HP] = msg.readInt16("hp");
92  data.mAttributes[Attributes::PLAYER_MAX_HP] = msg.readInt16("max hp");
93  data.mAttributes[Attributes::PLAYER_MP] = msg.readInt16("mp");
94  data.mAttributes[Attributes::PLAYER_MAX_MP] = msg.readInt16("max mp");
95 
96  msg.readInt16("speed");
97  const uint16_t race = msg.readInt16("class");
98  const uint8_t hairStyle = msg.readUInt8("hair style");
99  const uint16_t look = msg.readUInt8("look");
100  tempPlayer->setSubtype(fromInt(race, BeingTypeId), look);
101  const uint16_t weapon = msg.readInt16("weapon");
102  tempPlayer->setSpriteId(SPRITE_BODY,
103  weapon);
104  tempPlayer->setWeaponId(weapon);
105 
106  data.mAttributes[Attributes::PLAYER_BASE_LEVEL] = msg.readInt16("level");
107 
108  msg.readInt16("skill point");
109  const int bottomClothes = msg.readInt16("bottom clothes");
110  const int shield = msg.readInt16("shield");
111 
112  const int hat = msg.readInt16("hat");
113  const int topClothes = msg.readInt16("top clothes");
114 
115  const ItemColor hairColor = fromInt(
116  msg.readUInt8("hair color"), ItemColor);
117  msg.readUInt8("unused");
118  if (hairStyle == 0)
119  {
120  tempPlayer->unSetSprite(SPRITE_HAIR_COLOR);
121  }
122  else
123  {
124  tempPlayer->setSpriteColor(SPRITE_HAIR_COLOR,
125  hairStyle * -1,
126  ItemDB::get(-hairStyle).getDyeColorsString(hairColor));
127  }
128  tempPlayer->setHairColor(hairColor);
129 
130  const int misc2 = msg.readInt16("misc2");
131  tempPlayer->setName(msg.readString(24, "name"));
132 
133  character->dummy = tempPlayer;
134 
135  character->data.mStats[Attributes::PLAYER_STR].base = msg.readUInt8("str");
136  character->data.mStats[Attributes::PLAYER_AGI].base = msg.readUInt8("agi");
137  character->data.mStats[Attributes::PLAYER_VIT].base = msg.readUInt8("vit");
138  character->data.mStats[Attributes::PLAYER_INT].base = msg.readUInt8("int");
139  character->data.mStats[Attributes::PLAYER_DEX].base = msg.readUInt8("dex");
140  character->data.mStats[Attributes::PLAYER_LUK].base = msg.readUInt8("luk");
141 
142  tempPlayer->setSpriteId(SPRITE_HAIR,
143  shoes);
144  tempPlayer->setSpriteId(SPRITE_SHOES,
145  gloves);
146  tempPlayer->setSpriteId(SPRITE_SHIELD,
147  cape);
148  tempPlayer->setSpriteId(SPRITE_HEAD_TOP,
149  misc1);
150  tempPlayer->setSpriteId(SPRITE_WEAPON,
151  bottomClothes);
152  tempPlayer->setSpriteId(SPRITE_FLOOR,
153  shield);
154  tempPlayer->setSpriteId(SPRITE_CLOTHES_COLOR,
155  hat);
156  tempPlayer->setSpriteId(SPRITE_HEAD_BOTTOM,
157  topClothes);
158  tempPlayer->setSpriteId(SPRITE_HEAD_MID,
159  misc2);
160 
161  character->slot = msg.readUInt8("slot");
162  const uint8_t sex = CAST_U8(msg.readUInt8("gender"));
164  tempPlayer->setGender(Being::intToGender(sex));
165  else
166  tempPlayer->setGender(token.sex);
167 }
168 
170 {
171  BLOCK_START("CharServerRecv::processCharLogin")
172 
173  msg.readInt16("len");
174  const int slots = msg.readInt16("slots");
175  if (slots > 0 && slots < 30)
177 
178  msg.skip(18, "unused");
179 
182 
183  // Derive number of characters from message length
184  const int count = (msg.getLength() - 24) / 106;
185 
186  for (int i = 0; i < count; ++i)
187  {
188  Net::Character *const character = new Net::Character;
189  readPlayerData(msg, character);
190  Net::CharServerHandler::mCharacters.push_back(character);
191  if (character->dummy != nullptr)
192  {
193  logger->log("CharServer: Player: %s (%d)",
194  character->dummy->getName().c_str(), character->slot);
195  }
196  }
197 
199  BLOCK_END("CharServerRecv::processCharLogin")
200 }
201 
203 {
204  Network *const network = Network::mInstance;
205  ServerInfo &server = mapServer;
206  BLOCK_START("CharServerRecv::processCharMapInfo")
207  PlayerInfo::setCharId(msg.readInt32("char id?"));
208  GameHandler::setMap(msg.readString(16, "map name"));
209  if (config.getBoolValue("usePersistentIP") || settings.persistentIp)
210  {
211  msg.readInt32("ip address");
212  server.hostname = settings.serverName;
213  }
214  else
215  {
216  server.hostname = ipToString(msg.readInt32("ip address"));
217  }
218  server.port = msg.readInt16("port");
220 
221  // Prevent the selected local player from being deleted
224 
226 
229 
230  if (network != nullptr)
231  network->disconnect();
233  BLOCK_END("CharServerRecv::processCharMapInfo")
234 }
235 
237 {
238  Network *const network = Network::mInstance;
239  ServerInfo &server = mapServer;
240  BLOCK_START("CharServerRecv::processChangeMapServer")
241  if (network == nullptr)
242  {
243  BLOCK_END("CharServerRecv::processChangeMapServer")
244  return;
245  }
246  GameHandler::setMap(msg.readString(16, "map name"));
247  const int x = msg.readInt16("x");
248  const int y = msg.readInt16("y");
249  if (config.getBoolValue("usePersistentIP") || settings.persistentIp)
250  {
251  msg.readInt32("ip address");
252  server.hostname = settings.serverName;
253  }
254  else
255  {
256  server.hostname = ipToString(msg.readInt32("ip address"));
257  }
258  server.port = msg.readInt16("port");
259 
260  network->disconnect();
262  if (localPlayer != nullptr)
263  {
265  localPlayer->setMap(nullptr);
266  }
267  BLOCK_END("CharServerRecv::processChangeMapServer")
268 }
269 
271 {
272  BLOCK_START("CharServerRecv::processCharCreate")
273  Net::Character *const character = new Net::Character;
274  readPlayerData(msg, character);
275  Net::CharServerHandler::mCharacters.push_back(character);
276 
278 
279  // Close the character create dialog
282  BLOCK_END("CharServerRecv::processCharCreate")
283 }
284 
286 {
287  BLOCK_START("CharServerRecv::processCharDeleteFailed")
289  msg.readUInt8("error");
291  // TRANSLATORS: error header
292  _("Error"),
293  // TRANSLATORS: error message
294  _("Failed to delete character."),
295  // TRANSLATORS: ok dialog button
296  _("OK"),
298  Modal_true,
300  nullptr,
301  260);
302  BLOCK_END("CharServerRecv::processCharDeleteFailed")
303 }
304 
305 } // namespace TmwAthena
Attributes::PLAYER_MP
@ PLAYER_MP
Definition: attributes.h:33
TmwAthena::CharServerRecv::processCharMapInfo
void processCharMapInfo(Net::MessageIn &msg)
Definition: charserverrecv.cpp:202
Net::MessageIn
Definition: messagein.h:40
Ea::Network::disconnect
void disconnect()
Definition: network.cpp:138
Attributes::PLAYER_JOB_EXP
@ PLAYER_JOB_EXP
Definition: attributes.h:65
SPRITE_HEAD_MID
@ SPRITE_HEAD_MID
Definition: sprite.h:33
Net::ServerFeatures::haveCreateCharGender
virtual bool haveCreateCharGender() const =0
PlayerInfoBackend::mStats
StatMap mStats
Definition: playerinfo.h:73
Settings::serverName
std::string serverName
Definition: settings.h:113
LoginData::characterSlots
uint16_t characterSlots
Definition: logindata.h:76
Attributes::PLAYER_EXP
@ PLAYER_EXP
Definition: attributes.h:35
ServerInfo::althostname
std::string althostname
Definition: serverinfo.h:45
Attributes::PLAYER_VIT
@ PLAYER_VIT
Definition: attributes.h:43
TmwAthena::Network::mInstance
static Network * mInstance
Definition: network.h:53
Being::unSetSprite
void unSetSprite(const unsigned int slot)
Definition: being.cpp:2858
settings.h
Window::scheduleDelete
virtual void scheduleDelete()
Definition: window.cpp:830
DialogType::ERROR
@ ERROR
Definition: dialogtype.h:33
TmwAthena::Network
Definition: network.h:36
loginData
LoginData loginData
Definition: client.cpp:184
Net::CharServerHandler::mCharacters
static Net::Characters mCharacters
Definition: charserverhandler.h:106
TmwAthena::CharServerRecv::processChangeMapServer
void processChangeMapServer(Net::MessageIn &msg)
Definition: charserverrecv.cpp:236
gamehandler.h
itemdb.h
TmwAthena::CharServerRecv::processCharDeleteFailed
void processCharDeleteFailed(Net::MessageIn &msg)
Definition: charserverrecv.cpp:285
SPRITE_SHIELD
@ SPRITE_SHIELD
Definition: sprite.h:36
State::CHAR_SELECT
@ CHAR_SELECT
Definition: state.h:46
ServerInfo::port
uint16_t port
Definition: serverinfo.h:57
TmwAthena::charServer
ServerInfo charServer
Definition: charserverrecv.cpp:60
BeingTypeId
int BeingTypeId
Definition: beingtypeid.h:29
Token
Definition: token.h:29
PlayerInfo::setBackend
void setBackend(const PlayerInfoBackend &backend)
Definition: playerinfo.cpp:375
settings
Settings settings
Definition: settings.cpp:31
Attributes::PLAYER_HP
@ PLAYER_HP
Definition: attributes.h:31
Actions::msg
bool msg(InputEvent &event)
Definition: chat.cpp:38
OkDialog
Definition: okdialog.h:43
Ea::GameHandler::setMap
static void setMap(const std::string &map)
Definition: gamehandler.cpp:45
charcreatedialog.h
charserverhandler.h
Attributes::PLAYER_MAX_HP
@ PLAYER_MAX_HP
Definition: attributes.h:32
Net::Character
Definition: character.h:39
Attributes::PLAYER_JOB_LEVEL
@ PLAYER_JOB_LEVEL
Definition: attributes.h:40
Being::intToGender
static GenderT intToGender(const uint8_t sex) A_CONST
Definition: being.h:942
SPRITE_WEAPON
@ SPRITE_WEAPON
Definition: sprite.h:30
SPRITE_CLOTHES_COLOR
@ SPRITE_CLOTHES_COLOR
Definition: sprite.h:35
iteminfo.h
TmwAthena::CharServerRecv::processCharLogin
void processCharLogin(Net::MessageIn &msg)
Definition: charserverrecv.cpp:169
Attributes::PLAYER_INT
@ PLAYER_INT
Definition: attributes.h:44
Attributes::PLAYER_DEX
@ PLAYER_DEX
Definition: attributes.h:45
LocalPlayer::setTileCoords
void setTileCoords(const int x, const int y)
Definition: localplayer.cpp:2662
Being::setGender
virtual void setGender(const GenderT gender)
Definition: being.cpp:3578
ServerInfo::hostname
std::string hostname
Definition: serverinfo.h:44
logger
Logger * logger
Definition: logger.cpp:88
serverFeatures
Net::ServerFeatures * serverFeatures
Definition: net.cpp:100
TmwAthena
Definition: achievementhandler.cpp:25
CAST_U8
#define CAST_U8
Definition: cast.h:26
Client::setState
void setState(const StateT state)
Definition: client.h:65
TmwAthena::mapServer
ServerInfo mapServer
Definition: charserverrecv.cpp:58
State::CONNECT_GAME
@ CONNECT_GAME
Definition: state.h:47
ipToString
const char * ipToString(const uint32_t address)
Definition: stringutils.cpp:85
localPlayer
LocalPlayer * localPlayer
Definition: localplayer.cpp:108
Being::getName
const std::string & getName() const
Definition: being.h:231
Net::CharServerHandler::unlockCharSelectDialog
static void unlockCharSelectDialog()
Definition: charserverhandler.cpp:42
Being::setSpriteColor
void setSpriteColor(const unsigned int slot, const int id, const std::string &color)
Definition: being.cpp:2887
SPRITE_HAIR_COLOR
@ SPRITE_HAIR_COLOR
Definition: sprite.h:34
character.h
BeingTypeId_zero
const BeingTypeId BeingTypeId_zero
Definition: beingtypeid.h:29
Actions::hat
bool hat(InputEvent &event)
Definition: chat.cpp:61
data
uint32_t data
Definition: maptypeproperty2.h:3
SPRITE_HEAD_BOTTOM
@ SPRITE_HEAD_BOTTOM
Definition: sprite.h:31
Net::Character::slot
uint16_t slot
Definition: character.h:57
createwidget.h
gettext.h
Token::sex
GenderT sex
Definition: token.h:43
Being::setKarma
void setKarma(const int karma)
Definition: being.h:1034
Being::setSubtype
void setSubtype(const BeingTypeId subtype, const uint16_t look)
Definition: being.cpp:370
loginhandler.h
Attributes::PLAYER_STR
@ PLAYER_STR
Definition: attributes.h:41
x
x
Definition: graphics_calcImageRect.hpp:72
Attributes::PLAYER_BASE_LEVEL
@ PLAYER_BASE_LEVEL
Definition: attributes.h:30
LocalPlayer
Definition: localplayer.h:49
loginHandler
Net::LoginHandler * loginHandler
Definition: net.cpp:89
ItemColor
uint16_t ItemColor
Definition: itemcolor.h:29
Attributes::MONEY
@ MONEY
Definition: attributes.h:37
network.h
CAST_U16
#define CAST_U16
Definition: cast.h:28
TmwAthena::LoginHandler
Definition: loginhandler.h:31
ItemDbType::SPRITE_HAIR
@ SPRITE_HAIR
Definition: itemdbtype.h:46
config
Configuration config
Definition: configuration.cpp:51
TmwAthena::CharServerRecv::processCharCreate
void processCharCreate(Net::MessageIn &msg)
Definition: charserverrecv.cpp:270
Being::setManner
void setManner(const int manner)
Definition: being.h:1040
ShowCenter_true
const bool ShowCenter_true
Definition: showcenter.h:29
Attributes::PLAYER_MAX_MP
@ PLAYER_MAX_MP
Definition: attributes.h:34
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
State::CHANGE_MAP
@ CHANGE_MAP
Definition: state.h:49
SPRITE_BODY
@ SPRITE_BODY
Definition: sprite.h:38
SPRITE_SHOES
@ SPRITE_SHOES
Definition: sprite.h:37
charServerHandler
Net::CharServerHandler * charServerHandler
Definition: net.cpp:84
Being::setSpriteId
void setSpriteId(const unsigned int slot, const int id)
Definition: being.cpp:2786
Net::CharServerHandler::mSelectedCharacter
static Net::Character * mSelectedCharacter
Definition: charserverhandler.h:112
configuration.h
PlayerInfoBackend
Definition: playerinfo.h:61
TmwAthena::CharServerRecv::readPlayerData
void readPlayerData(Net::MessageIn &msg, Net::Character *const character)
Definition: charserverrecv.cpp:62
restrict
#define restrict
Definition: localconsts.h:164
client
Client * client
Definition: client.cpp:117
fromInt
#define fromInt(val, name)
Definition: intdefines.h:45
Being::setName
void setName(const std::string &name)
Definition: being.cpp:1135
serverfeatures.h
Net::CharServerHandler::clear
virtual void clear() const =0
Modal_true
const bool Modal_true
Definition: modal.h:29
BLOCK_START
#define BLOCK_START(name)
Definition: perfomance.h:78
BLOCK_END
#define BLOCK_END(name)
Definition: perfomance.h:79
Net::CharServerHandler::mCharCreateDialog
static CharCreateDialog * mCharCreateDialog
Definition: charserverhandler.h:109
okdialog.h
Net::Character::dummy
LocalPlayer * dummy
Definition: character.h:55
sprite.h
ItemDB::get
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
EAthena::CharServerRecv::readPlayerData
void readPlayerData(Net::MessageIn &msg, Net::Character *const character)
Definition: charserverrecv.cpp:73
dtor.h
_
#define _(s)
Definition: gettext.h:34
Net::CharServerHandler::updateCharSelectDialog
static void updateCharSelectDialog()
Definition: charserverhandler.cpp:36
Being::setHairColor
void setHairColor(const unsigned int slot, const ItemColor color)
Definition: being.cpp:3386
LocalPlayer::setMap
void setMap(Map *const map)
Definition: localplayer.cpp:1828
ServerInfo
Definition: serverinfo.h:36
PlayerInfo::setCharId
void setCharId(const int charId)
Definition: playerinfo.cpp:380
token.h
Net::Character::data
PlayerInfoBackend data
Definition: character.h:56
SPRITE_FLOOR
@ SPRITE_FLOOR
Definition: sprite.h:39
y
y
Definition: graphics_calcImageRect.hpp:72
client.h
Attributes::PLAYER_LUK
@ PLAYER_LUK
Definition: attributes.h:46
messagein.h
charserverrecv.h
Being::setWeaponId
void setWeaponId(const int id)
Definition: being.cpp:3132
SPRITE_HEAD_TOP
@ SPRITE_HEAD_TOP
Definition: sprite.h:32
Logger::log
void log(const char *const log_text,...)
Definition: logger.cpp:268
debug.h
CREATEWIDGET
#define CREATEWIDGET(type,...)
Definition: createwidget.h:28
Settings::persistentIp
bool persistentIp
Definition: settings.h:152
Attributes::PLAYER_AGI
@ PLAYER_AGI
Definition: attributes.h:42
Configuration::getBoolValue
bool getBoolValue(const std::string &key) const
Definition: configuration.cpp:596