Mana
Loading...
Searching...
No Matches
gamehandler.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 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 "event.h"
26#include "game.h"
27#include "localplayer.h"
28#include "log.h"
29
30#include "gui/okdialog.h"
31
33#include "net/tmwa/messagein.h"
34#include "net/tmwa/messageout.h"
35#include "net/tmwa/network.h"
36#include "net/tmwa/protocol.h"
37
38#include "utils/gettext.h"
39
41
42namespace TmwAthena {
43
45
47{
48 static const Uint16 _messages[] = {
52 0
53 };
54 handledMessages = _messages;
55 gameHandler = this;
56
58}
59
61{
62 switch (msg.getId())
63 {
65 {
66 unsigned char direction;
67 Uint16 x, y;
68 msg.readInt32(); // server tick
69 msg.readCoordinates(x, y, direction);
70 msg.skip(2); // unknown
71 Log::info("Protocol: Player start position: (%d, %d), Direction: %d",
72 x, y, direction);
73 // Switch now or we'll have problems
75 // Stores the position until the map is loaded.
76 mTileX = x; mTileY = y;
77 } break;
78
80 if (msg.readInt8())
81 {
83 }
84 break;
85
87 if (msg.readInt16())
88 {
89 new OkDialog(_("Game"), _("Request to quit denied!"), false,
90 nullptr);
91 }
92 break;
93 }
94}
95
96void GameHandler::event(Event::Channel channel, const Event &event)
97{
98 if (channel == Event::GameChannel)
99 {
100 if (event.getType() == Event::EnginesInitialized)
101 {
102 Game *game = Game::instance();
103 game->changeMap(mMap);
104
105 // It could be that loading the map failed
106 if (Map *map = game->getCurrentMap())
107 {
108 if (mTileX && mTileY)
109 {
110 local_player->setPosition(map->getTileCenter(mTileX, mTileY));
111 mTileX = mTileY = 0;
112 }
113 }
114 }
115 else if (event.getType() == Event::MapLoaded)
116 {
118 }
119 }
120}
121
123{
125
126 const Token &token =
127 static_cast<LoginHandler*>(Net::getLoginHandler())->getToken();
128
129
131 {
133 // Change the player's ID to the account ID to match what eAthena uses
135 }
136
137 // Send login infos
139 outMsg.writeInt32(token.account_ID);
140 outMsg.writeInt32(mCharID);
141 outMsg.writeInt32(token.session_ID1);
142 outMsg.writeInt32(token.session_ID2);
143 outMsg.writeInt8(static_cast<uint8_t>(token.sex));
144
145 // We get 4 useless bytes before the real answer comes in (what are these?)
146 mNetwork->skip(4);
147}
148
150{
151 return mNetwork->isConnected();
152}
153
158
160{
162}
163
164void GameHandler::setMap(const std::string &map)
165{
166 mMap = map.substr(0, map.rfind("."));
167}
168
170{
171 if (Game *game = Game::instance())
172 return game->getCurrentTileWidth();
173 return DEFAULT_TILE_LENGTH;
174}
175
176} // namespace TmwAthena
int getId() const
Definition actorsprite.h:63
void setId(int id)
Definition actorsprite.h:66
void setPosition(const Vector &pos) final
Sets the pixel position of this actor.
Definition being.cpp:187
static void setState(State state)
Definition client.h:169
static State getState()
Definition client.h:172
void listen(Event::Channel channel)
Definition event.h:42
@ EnginesInitialized
Definition event.h:80
@ MapLoaded
Definition event.h:87
Channel
Definition event.h:45
@ GameChannel
Definition event.h:52
The main class responsible for running the game.
Definition game.h:37
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
A tile map.
Definition map.h:147
const uint16_t * handledMessages
An 'Ok' button dialog.
Definition okdialog.h:34
void handleMessage(MessageIn &msg) override
void connect() override
int getPickupRange() const override
Tells the range is pixel where the player can pickup items from.
void disconnect() override
void setMap(const std::string &map)
bool isConnected() override
void event(Event::Channel channel, const Event &event) override
std::string mMap
Keeps the map filename.
Definition gamehandler.h:66
int mTileX
< Saved for map-server switching
Definition gamehandler.h:72
Used for parsing an incoming message from eAthena.
Definition messagein.h:35
void readCoordinates(uint16_t &x, uint16_t &y, uint8_t &direction)
Reads a special 3 byte block used by eAthena, containing x and y coordinates and direction.
Definition messagein.cpp:81
void skip(unsigned int length)
Skips a given number of bytes.
uint16_t readInt16()
Reads an unsigned 16-bit integer from the message.
Definition messagein.cpp:57
uint8_t readInt8()
Reads an unsigned 8-bit integer from the message.
Definition messagein.cpp:46
uint16_t getId() const
Returns the message ID.
Definition messagein.h:42
uint32_t readInt32()
Reads an unsigned 32-bit integer from the message.
Definition messagein.cpp:69
Used for building an outgoing message to eAthena.
Definition messageout.h:35
void writeInt32(uint32_t value)
Writes an unsigned 32-bit integer to the message.
void writeInt8(uint8_t value)
Writes an unsigned 8-bit integer to the message.
bool isConnected() const
Definition network.h:75
bool connect(const ServerInfo &server)
Definition network.cpp:278
void skip(int len)
Definition network.cpp:445
@ STATE_CONNECT_GAME
Definition client.h:72
@ STATE_GAME
Definition client.h:73
@ STATE_SWITCH_CHARACTER
Definition client.h:91
#define _(s)
Definition gettext.h:38
LocalPlayer * local_player
ManaServ::GameHandler * gameHandler
Definition net.cpp:52
Net::GameHandler * gameHandler
Definition net.cpp:52
const int DEFAULT_TILE_LENGTH
Definition map.h:38
void info(const char *log_text,...) LOG_PRINTF_ATTR
LoginHandler * getLoginHandler()
Definition net.cpp:95
Warning: buffers and other variables are shared, so there can be only one connection active at a time...
ServerInfo mapServer
@ SMSG_CHAR_SWITCH_RESPONSE
Definition protocol.h:205
@ SMSG_MAP_QUIT_RESPONSE
Definition protocol.h:278
@ CMSG_MAP_SERVER_CONNECT
Definition protocol.h:159
@ CMSG_MAP_LOADED
Definition protocol.h:164
@ CMSG_CLIENT_QUIT
Definition protocol.h:277
@ SMSG_MAP_LOGIN_SUCCESS
Definition protocol.h:160
unsigned char uint8_t
Definition sha256.cpp:81
int session_ID1
Definition token.h:31
int session_ID2
Definition token.h:32