Mana
Loading...
Searching...
No Matches
playerhandler.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2013 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "client.h"
25#include "effectmanager.h"
26#include "event.h"
27#include "game.h"
28#include "localplayer.h"
29#include "log.h"
30#include "particle.h"
31#include "playerinfo.h"
32
33#include "gui/viewport.h"
34
35#include "net/net.h"
36
37#include "net/abilityhandler.h"
42
43#include "resources/abilitydb.h"
45
52
55
56namespace ManaServ {
57
58extern Connection *gameServerConnection;
59extern std::string netToken;
61
68
85
87{
88 switch (msg.getId())
89 {
92 break;
93
95 { // TODO: Fix the servers to test this
96 netToken = msg.readString(32);
97 std::string address = msg.readString();
98 int port = msg.readInt16();
99 Log::info("Changing server to %s:%d", address.c_str(), port);
100
101 gameServer.hostname = address;
102 gameServer.port = port;
103
106 local_player->setMap(nullptr);
107 } break;
108
110 {
111 while (msg.getUnreadLength())
112 {
113 int attrId = msg.readInt16();
114 double base = msg.readInt32() / 256.0;
115 double value = msg.readInt32() / 256.0;
116
117 // Set the core player attribute the stat
118 // depending on attribute link.
119 int playerInfoId =
121 if (playerInfoId > -1)
122 {
123 PlayerInfo::setAttribute(playerInfoId, value);
124 }
125 else
126 {
127 PlayerInfo::setStatBase(attrId, base);
128 PlayerInfo::setStatMod(attrId, value - base);
129 }
130 }
131 } break;
132
136 break;
137
139 {
140 int errCode = msg.readInt8();
141 int attrNum = msg.readInt16();
142 switch (errCode)
143 {
144 case ATTRIBMOD_OK:
145 {
146 // feel(acknowledgment);
147 } break;
149 {
150 Log::warn("Server denied increase of attribute %d (unknown attribute) ", attrNum);
151 } break;
153 {
154 // when the server says "you got no points" it
155 // has to be correct. The server is always right!
156 // undo attribute change and set points to 0
157 Log::warn("Server denied increase of attribute %d (no points left) ", attrNum);
158 int attrValue = PlayerInfo::getStatBase(attrNum) - 1;
160 PlayerInfo::setStatBase(attrNum, attrValue);
161 } break;
162 case ATTRIBMOD_DENIED:
163 {
164 // undo attribute change
165 Log::warn("Server denied increase of attribute %d (reason unknown) ", attrNum);
166 int points = PlayerInfo::getAttribute(CHAR_POINTS) - 1;
168
169 int attrValue = PlayerInfo::getStatBase(attrNum) - 1;
170 PlayerInfo::setStatBase(attrNum, attrValue);
171 } break;
172 }
173 } break;
174
176 {
177 int errCode = msg.readInt8();
178 int attrNum = msg.readInt16();
179 switch (errCode)
180 {
181 case ATTRIBMOD_OK:
182 {
183 // feel(acknowledgment);
184 } break;
186 {
187 Log::warn("Server denied reduction of attribute %d (unknown attribute) ", attrNum);
188 } break;
190 {
191 // when the server says "you got no points" it
192 // has to be correct. The server is always right!
193 // undo attribute change and set points to 0
194 Log::warn("Server denied reduction of attribute %d (no points left) ", attrNum);
195 int attrValue = PlayerInfo::getStatBase(attrNum) + 1;
196 // TODO are these right?
199 PlayerInfo::setStatBase(attrNum, attrValue);
200 } break;
201 case ATTRIBMOD_DENIED:
202 {
203 // undo attribute change
204 Log::warn("Server denied reduction of attribute %d (reason unknown) ", attrNum);
205 int charaPoints = PlayerInfo::getAttribute(CHAR_POINTS) - 1;
207
208 int correctPoints = PlayerInfo::getAttribute(CORR_POINTS) + 1;
210
211 int attrValue = PlayerInfo::getStatBase(attrNum) + 1;
212 PlayerInfo::setStatBase(attrNum, attrValue);
213 } break;
214 }
215
216 } break;
217
219 {
220 while (msg.getUnreadLength())
221 {
222 int id = msg.readInt8();
223 int current = msg.readInt32();
224 int max = msg.readInt32();
225 int recharge = msg.readInt32();
226 PlayerInfo::setAbilityStatus(id, current, max, recharge);
227 }
228 } break;
229
231 {
232 int id = msg.readInt8();
234 } break;
235
236 /*
237 case SMSG_PLAYER_ARROW_MESSAGE:
238 {
239 Sint16 type = msg.readInt16();
240
241 switch (type)
242 {
243 case 0:
244 localChatTab->chatLog(_("Equip arrows first."),
245 BY_SERVER);
246 break;
247 default:
248 Log::info("0x013b: Unhandled message %i", type);
249 break;
250 }
251 }
252 break;
253 */
254 }
255}
256
258{
259 const std::string mapName = msg.readString();
260 const unsigned short x = msg.readInt16();
261 const unsigned short y = msg.readInt16();
262
263 Game *game = Game::instance();
264 const bool sameMap = (game->getCurrentMapName() == mapName);
265
266 Log::info("Changing map to %s (%d, %d)", mapName.c_str(), x, y);
267
268 // Switch the actual map, deleting the previous one
269 game->changeMap(mapName);
270
271 const Vector &playerPos = local_player->getPosition();
272 float scrollOffsetX = 0.0f;
273 float scrollOffsetY = 0.0f;
274
275 /* Scroll if neccessary */
276 if (!sameMap
277 || (abs(x - (int) playerPos.x) > MAP_TELEPORT_SCROLL_DISTANCE)
278 || (abs(y - (int) playerPos.y) > MAP_TELEPORT_SCROLL_DISTANCE))
279 {
280 scrollOffsetX = x - (int) playerPos.x;
281 scrollOffsetY = y - (int) playerPos.y;
282 }
283
287
288 Log::info("Adjust scrolling by %d,%d", (int) scrollOffsetX,
289 (int) scrollOffsetY);
290 viewport->scrollBy(scrollOffsetX, scrollOffsetY);
291}
292
294{
295 auto ability = AbilityDB::find("Strike");
296 if (!ability)
297 {
298 Log::info("PlayerHandler::attack: 'Strike' ability not found.");
299 return;
300 }
301
302 switch (ability->targetMode) {
304 abilityHandler->useOn(ability->id, id);
305 break;
307 Log::info("PlayerHandler::attack: Unsupported target mode 'point' for 'Strike' ability.");
308 break;
311 break;
312 }
313}
314
315void PlayerHandler::emote(int emoteId)
316{
318 msg.writeInt8(emoteId);
320}
321
328
335
337{
338 // Not used atm
339}
340
342{
343 if (floorItem)
344 {
346 msg.writeInt16(floorItem->getPixelX());
347 msg.writeInt16(floorItem->getPixelY());
349 }
350}
351
352void PlayerHandler::setDirection(char direction)
353{
355 msg.writeInt8(direction);
357}
358
359void PlayerHandler::setDestination(int x, int y, int /* direction */)
360{
362 msg.writeInt16(x);
363 msg.writeInt16(y);
365}
366
368{
369 local_player->setAction(action);
370
372 msg.writeInt8(action);
374}
375
381
382void PlayerHandler::ignorePlayer(const std::string &player, bool ignore)
383{
384 // TODO
385}
386
388{
389 // TODO
390}
391
393{
394 return true;
395}
396
398{
399 return true;
400}
401
403{
404 return -1;
405}
406
408{
409 // Return default speed at 6 tiles per second.
410 return Vector(6.0f, 6.0f, 0.0f);
411}
412
414{
415 Vector speedInPixels;
416
417 Game *game = Game::instance();
418 if (game && !map)
419 map = game->getCurrentMap();
420
421 if (!map)
422 {
423 Log::info("Manaserv::PlayerHandler: Speed wasn't given back"
424 " because Map not initialized.");
425 return speedInPixels;
426 }
427
428 speedInPixels.x = speed.x * map->getTileWidth();
429 speedInPixels.y = speed.y * map->getTileHeight();
430
431 return speedInPixels;
432}
433
434} // namespace ManaServ
int getPixelX() const
Returns the pixels X coordinate of the actor.
Definition actor.h:65
int getPixelY() const
Returns the pixel Y coordinate of the actor.
Definition actor.h:71
const Vector & getPosition() const
Returns the pixel position of this actor.
Definition actor.h:53
void setPosition(const Vector &pos) final
Sets the pixel position of this actor.
Definition being.cpp:187
void setMap(Map *map) final
Definition being.cpp:1398
uint8_t getDirection() const
Returns the current direction.
Definition being.h:356
Action
Action the being is currently performing WARNING: Has to be in sync with the same enum in the Being c...
Definition being.h:73
@ STAND
Definition being.h:74
static void setState(State state)
Definition client.h:169
@ CloseAll
Definition event.h:66
void trigger(Channel channel) const
Sends this event to all classes listening to the given channel.
Definition event.h:275
@ NpcChannel
Definition event.h:55
An item lying on the floor.
Definition flooritem.h:32
The main class responsible for running the game.
Definition game.h:37
const std::string & getCurrentMapName()
Definition game.h:72
Map * getCurrentMap()
Returns the currently active map.
Definition game.h:70
static Game * instance()
Provides access to the game instance.
Definition game.h:53
void changeMap(const std::string &mapName)
Changes the currently active map.
Definition game.cpp:857
void setAction(Action action, int attackId=1) override
Sets the current action.
virtual void setDestination(int x, int y)
Sets a new destination for this being to walk to.
void send(const ManaServ::MessageOut &msg)
Sends a message.
void disconnect()
Disconnects from the given server.
Used for parsing an incoming message from manaserv.
Definition messagein.h:37
uint16_t readInt16()
Reads an unsigned 16-bit integer from the message.
Definition messagein.cpp:58
unsigned int getUnreadLength() const
Returns the length of unread data.
Definition messagein.h:54
std::string readString(int length=-1)
Reads a string.
Definition messagein.cpp:92
uint16_t getId() const
Returns the message ID.
Definition messagein.h:44
uint32_t readInt32()
Reads an unsigned 32-bit integer from the message.
Definition messagein.cpp:75
uint8_t readInt8()
Reads an unsigned 8-bit integer from the message.
Definition messagein.cpp:43
Used for building an outgoing message to manaserv.
Definition messageout.h:37
void writeInt16(uint16_t value)
Writes an unsigned 16-bit integer to the message.
void writeInt8(uint8_t value)
Writes an unsigned 8-bit integer to the message.
void decreaseAttribute(int attr) override
void emote(int emoteId) override
void attack(int id) override
void setDestination(int x, int y, int direction=-1) override
Vector getPixelsPerSecondMoveSpeed(const Vector &speed, Map *map=nullptr) override
Convert the original server-dependant speed for internal use.
void handleMessage(MessageIn &msg) override
void handleMapChangeMessage(MessageIn &msg)
void increaseAttribute(int attr) override
void ignoreAll(bool ignore) override
bool canCorrectAttributes() override
bool canUseMagic() override
void ignorePlayer(const std::string &player, bool ignore) override
void setDirection(char direction) override
void changeAction(Being::Action action) override
void pickUp(FloorItem *floorItem) override
Vector getDefaultMoveSpeed() const override
Get the original default movement speed.
int getJobLocation() override
void increaseSkill(int skillId) override
A tile map.
Definition map.h:147
int getTileHeight() const
Returns the tile height used by this map.
Definition map.h:271
int getTileWidth() const
Returns the tile width of this map.
Definition map.h:265
virtual void useInDirection(int id, int direction)=0
virtual void useOn(int id, int beingId)=0
const uint16_t * handledMessages
virtual void respawn()=0
std::string hostname
Definition serverinfo.h:42
uint16_t port
Definition serverinfo.h:43
Vector class.
Definition vector.h:33
float y
Definition vector.h:172
float x
Definition vector.h:171
void scrollBy(float x, float y)
Changes viewpoint by relative pixel coordinates.
Definition viewport.h:141
@ STATE_CHANGE_MAP
Definition client.h:74
Viewport * viewport
Viewport on the map.
Definition game.cpp:115
LocalPlayer * local_player
Net::AbilityHandler * abilityHandler
Definition net.cpp:57
const int MAP_TELEPORT_SCROLL_DISTANCE
Max.
Net::AbilityHandler * abilityHandler
Definition net.cpp:57
Net::PlayerHandler * playerHandler
Definition net.cpp:56
AbilityInfo * find(std::string_view name)
Finds an ability by name.
Definition abilitydb.cpp:98
int getPlayerInfoIdFromAttrId(int attrId)
Give back the corresponding playerinfo Id from the attribute id defined in the xml file.
void warn(const char *log_text,...) LOG_PRINTF_ATTR
void info(const char *log_text,...) LOG_PRINTF_ATTR
ServerInfo gameServer
@ ATTRIBMOD_INVALID_ATTRIBUTE
Connection * gameServerConnection
std::string netToken
@ GPMSG_ATTRIBUTE_POINTS_STATUS
@ GPMSG_RAISE_ATTRIBUTE_RESPONSE
@ GPMSG_LOWER_ATTRIBUTE_RESPONSE
@ GPMSG_PLAYER_ATTRIBUTE_CHANGE
PlayerHandler * getPlayerHandler()
Definition net.cpp:110
int getStatBase(int id)
Returns the base value of the given stat.
void setAbilityStatus(int id, int current, int max, int recharge)
Changes the status of the given ability.
void setStatBase(int id, int value, bool notify)
Changes the base value of the given stat.
void setAttribute(int id, int value, bool notify)
Changes the value of the given attribute.
void clearAbilityStatus(int id)
Removes the status for the given ability.
int getAttribute(int id)
Returns the value of the given attribute.
void setStatMod(int id, int value, bool notify)
Changes the modifier for the given stat.
@ CORR_POINTS
Definition playerinfo.h:37
@ CHAR_POINTS
Definition playerinfo.h:37
@ TARGET_DIRECTION
Definition abilitydb.h:32
void action(const gcn::ActionEvent &event) override