tibia-client/src/otclient/core/game.cpp

155 lines
4.0 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2011-08-15 16:11:24 +02:00
#include "game.h"
2011-08-15 23:02:52 +02:00
#include "localplayer.h"
2011-08-16 02:30:31 +02:00
#include <otclient/net/protocolgame.h>
2011-08-15 16:11:24 +02:00
Game g_game;
2011-08-16 02:30:31 +02:00
void Game::init()
2011-08-15 16:11:24 +02:00
{
m_online = false;
}
2011-08-15 23:02:52 +02:00
2011-08-16 02:30:31 +02:00
void Game::terminate()
{
if(m_online)
logout();
}
2011-08-26 20:00:22 +02:00
void Game::loginWorld(const std::string& account, const std::string& password, const std::string& worldHost, int worldPort, const std::string& characterName)
2011-08-16 02:30:31 +02:00
{
m_protocolGame = ProtocolGamePtr(new ProtocolGame);
2011-08-26 20:00:22 +02:00
m_protocolGame->login(account, password, worldHost, (uint16)worldPort, characterName);
2011-08-16 02:30:31 +02:00
}
2011-08-29 20:38:01 +02:00
void Game::cancelLogin()
{
if(m_protocolGame->isConnected()) {
logout();
} else if(m_protocolGame->isConnecting()) {
m_protocolGame->disconnect();
m_protocolGame.reset();
}
}
2011-08-16 02:30:31 +02:00
void Game::logout()
{
m_protocolGame->sendLogout();
2011-08-29 20:38:01 +02:00
}
void Game::processConnectionError(const boost::system::error_code& error)
{
// connection errors only have meaning if we still have a protocol
if(m_protocolGame) {
if(error != asio::error::eof)
g_lua.callGlobalField("Game", "onConnectionError", error.message());
2011-08-29 20:38:01 +02:00
if(m_online) {
2011-08-29 20:38:01 +02:00
processLogout();
} else {
m_protocolGame->disconnect();
m_protocolGame.reset();
}
2011-08-29 20:38:01 +02:00
}
2011-08-16 02:30:31 +02:00
}
void Game::processLogin(const LocalPlayerPtr& localPlayer)
2011-08-15 23:02:52 +02:00
{
m_localPlayer = localPlayer;
2011-08-15 23:02:52 +02:00
m_online = true;
2011-08-29 20:38:01 +02:00
g_lua.callGlobalField("Game", "onLogin", m_localPlayer);
2011-08-15 23:02:52 +02:00
}
void Game::processLogout()
2011-08-15 23:02:52 +02:00
{
g_lua.callGlobalField("Game", "onLogout", m_localPlayer);
2011-08-29 20:38:01 +02:00
2011-08-16 05:27:46 +02:00
if(m_protocolGame) {
m_protocolGame->disconnect();
m_protocolGame.reset();
}
2011-08-29 20:38:01 +02:00
2011-08-15 23:02:52 +02:00
m_localPlayer.reset();
m_online = false;
}
void Game::walk(Otc::Direction direction)
{
if(!m_online)
return;
// TODO: check if we can walk.
//m_localPlayer->setDirection(direction);
switch(direction) {
case Otc::North:
m_protocolGame->sendWalkNorth();
break;
case Otc::East:
m_protocolGame->sendWalkEast();
break;
case Otc::South:
m_protocolGame->sendWalkSouth();
break;
case Otc::West:
m_protocolGame->sendWalkWest();
break;
2011-08-29 07:54:28 +02:00
case Otc::NorthEast:
m_protocolGame->sendWalkNorthEast();
break;
case Otc::SouthEast:
m_protocolGame->sendWalkSouthEast();
break;
case Otc::SouthWest:
m_protocolGame->sendWalkSouthWest();
break;
case Otc::NorthWest:
m_protocolGame->sendWalkNorthWest();
break;
}
}
void Game::turn(Otc::Direction direction)
{
if(!m_online)
return;
switch(direction) {
case Otc::North:
m_protocolGame->sendTurnNorth();
break;
case Otc::East:
m_protocolGame->sendTurnEast();
break;
case Otc::South:
m_protocolGame->sendTurnSouth();
break;
case Otc::West:
m_protocolGame->sendTurnWest();
break;
}
}