2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* 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-12-30 19:14:50 +01:00
|
|
|
#include "map.h"
|
|
|
|
#include "tile.h"
|
2011-08-16 02:30:31 +02:00
|
|
|
#include <otclient/net/protocolgame.h>
|
2011-11-10 06:29:25 +01:00
|
|
|
#include <framework/core/eventdispatcher.h>
|
2012-01-05 13:48:10 +01:00
|
|
|
#include <framework/ui/uimanager.h>
|
2011-08-15 16:11:24 +02:00
|
|
|
|
|
|
|
Game g_game;
|
|
|
|
|
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()
|
2011-08-30 01:20:30 +02:00
|
|
|
{
|
|
|
|
processLogout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::logout(bool force)
|
2011-08-29 20:38:01 +02:00
|
|
|
{
|
2011-12-03 22:41:37 +01:00
|
|
|
if(!m_protocolGame || !m_online)
|
|
|
|
return;
|
2011-08-30 01:20:30 +02:00
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
m_protocolGame->sendLogout();
|
|
|
|
|
|
|
|
if(force)
|
|
|
|
processLogout();
|
2011-08-29 20:38:01 +02:00
|
|
|
}
|
|
|
|
|
2011-08-30 01:20:30 +02:00
|
|
|
void Game::processLoginError(const std::string& error)
|
2011-08-16 02:30:31 +02:00
|
|
|
{
|
2011-08-30 01:20:30 +02:00
|
|
|
g_lua.callGlobalField("Game", "onLoginError", error);
|
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) {
|
2011-08-29 21:35:58 +02:00
|
|
|
if(error != asio::error::eof)
|
|
|
|
g_lua.callGlobalField("Game", "onConnectionError", error.message());
|
2011-08-29 20:38:01 +02:00
|
|
|
|
2011-08-30 01:20:30 +02:00
|
|
|
processLogout();
|
2011-08-29 20:38:01 +02:00
|
|
|
}
|
2011-08-16 02:30:31 +02:00
|
|
|
}
|
|
|
|
|
2011-08-29 16:14:21 +02:00
|
|
|
void Game::processLogin(const LocalPlayerPtr& localPlayer)
|
2011-08-15 23:02:52 +02:00
|
|
|
{
|
2011-08-29 16:14:21 +02:00
|
|
|
m_localPlayer = localPlayer;
|
2011-08-15 23:02:52 +02:00
|
|
|
m_online = true;
|
2011-08-29 20:38:01 +02:00
|
|
|
|
2011-08-29 16:14:21 +02:00
|
|
|
g_lua.callGlobalField("Game", "onLogin", m_localPlayer);
|
2011-08-15 23:02:52 +02:00
|
|
|
}
|
|
|
|
|
2011-08-29 16:14:21 +02:00
|
|
|
void Game::processLogout()
|
2011-08-15 23:02:52 +02:00
|
|
|
{
|
2011-08-30 01:20:30 +02:00
|
|
|
if(m_online) {
|
|
|
|
g_lua.callGlobalField("Game", "onLogout", m_localPlayer);
|
|
|
|
|
|
|
|
m_localPlayer.reset();
|
|
|
|
m_online = false;
|
|
|
|
}
|
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-15 23:02:52 +02:00
|
|
|
}
|
2011-08-17 06:45:55 +02:00
|
|
|
|
2011-09-04 19:21:42 +02:00
|
|
|
void Game::processTextMessage(int type, const std::string& message)
|
2011-08-30 17:12:57 +02:00
|
|
|
{
|
2011-09-04 19:21:42 +02:00
|
|
|
g_lua.callGlobalField("Game","onTextMessage", type, message);
|
2011-08-30 17:12:57 +02:00
|
|
|
}
|
|
|
|
|
2011-11-10 06:29:25 +01:00
|
|
|
void Game::processInventoryChange(int slot, const ItemPtr& item)
|
|
|
|
{
|
2012-01-03 21:41:00 +01:00
|
|
|
if(item)
|
2012-01-04 11:26:58 +01:00
|
|
|
item->setPos(Position(65535, slot, 0));
|
2012-01-03 21:41:00 +01:00
|
|
|
|
2011-11-11 21:26:10 +01:00
|
|
|
g_lua.callGlobalField("Game","onInventoryChange", slot, item);
|
2011-11-10 06:29:25 +01:00
|
|
|
}
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
void Game::walk(Otc::Direction direction)
|
2011-08-17 06:45:55 +02:00
|
|
|
{
|
2012-01-07 02:48:16 +01:00
|
|
|
if(!m_online || !m_localPlayer->canWalk(direction) || !checkBotProtection())
|
2011-11-03 17:26:17 +01:00
|
|
|
return;
|
2011-08-17 06:45:55 +02:00
|
|
|
|
2012-01-05 15:24:38 +01:00
|
|
|
cancelFollow();
|
|
|
|
|
2011-11-05 21:34:49 +01:00
|
|
|
m_localPlayer->clientWalk(direction);
|
2011-08-17 06:45:55 +02:00
|
|
|
|
|
|
|
switch(direction) {
|
2011-08-28 18:02:26 +02:00
|
|
|
case Otc::North:
|
2011-08-17 06:45:55 +02:00
|
|
|
m_protocolGame->sendWalkNorth();
|
|
|
|
break;
|
2011-08-28 18:02:26 +02:00
|
|
|
case Otc::East:
|
2011-08-17 06:45:55 +02:00
|
|
|
m_protocolGame->sendWalkEast();
|
|
|
|
break;
|
2011-08-28 18:02:26 +02:00
|
|
|
case Otc::South:
|
2011-08-17 06:45:55 +02:00
|
|
|
m_protocolGame->sendWalkSouth();
|
|
|
|
break;
|
2011-08-28 18:02:26 +02:00
|
|
|
case Otc::West:
|
2011-08-17 06:45:55 +02:00
|
|
|
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;
|
2011-08-17 06:45:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
void Game::turn(Otc::Direction direction)
|
2011-08-17 06:45:55 +02:00
|
|
|
{
|
|
|
|
if(!m_online)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch(direction) {
|
2011-08-28 18:02:26 +02:00
|
|
|
case Otc::North:
|
2011-08-17 06:45:55 +02:00
|
|
|
m_protocolGame->sendTurnNorth();
|
|
|
|
break;
|
2011-08-28 18:02:26 +02:00
|
|
|
case Otc::East:
|
2011-08-17 06:45:55 +02:00
|
|
|
m_protocolGame->sendTurnEast();
|
|
|
|
break;
|
2011-08-28 18:02:26 +02:00
|
|
|
case Otc::South:
|
2011-08-17 06:45:55 +02:00
|
|
|
m_protocolGame->sendTurnSouth();
|
|
|
|
break;
|
2011-08-28 18:02:26 +02:00
|
|
|
case Otc::West:
|
2011-08-17 06:45:55 +02:00
|
|
|
m_protocolGame->sendTurnWest();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-11-03 17:26:17 +01:00
|
|
|
|
2012-01-03 21:41:00 +01:00
|
|
|
void Game::look(const ThingPtr& thing)
|
2011-12-30 19:14:50 +01:00
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !thing || !checkBotProtection())
|
2012-01-04 14:02:35 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
int stackpos = getThingStackpos(thing);
|
|
|
|
if(stackpos != -1)
|
|
|
|
m_protocolGame->sendLookAt(thing->getPos(), thing->getId(), stackpos);
|
2012-01-03 01:10:39 +01:00
|
|
|
}
|
|
|
|
|
2012-01-03 23:27:31 +01:00
|
|
|
void Game::use(const ThingPtr& thing)
|
2012-01-04 14:02:35 +01:00
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !thing || !checkBotProtection())
|
2012-01-04 14:02:35 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
int stackpos = getThingStackpos(thing);
|
|
|
|
if(stackpos != -1)
|
|
|
|
m_protocolGame->sendUseItem(thing->getPos(), thing->getId(), stackpos, 0);// last 0 has something to do with container
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::attack(const CreaturePtr& creature)
|
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !creature || !checkBotProtection())
|
2012-01-04 14:02:35 +01:00
|
|
|
return;
|
|
|
|
|
2012-01-05 15:24:38 +01:00
|
|
|
if(m_attackingCreature)
|
2012-01-06 18:29:18 +01:00
|
|
|
m_attackingCreature->hideStaticSquare();
|
2012-01-05 15:24:38 +01:00
|
|
|
|
2012-01-06 18:29:18 +01:00
|
|
|
creature->showStaticSquare(Fw::red);
|
2012-01-05 15:24:38 +01:00
|
|
|
m_attackingCreature = creature;
|
|
|
|
|
2012-01-04 14:02:35 +01:00
|
|
|
m_protocolGame->sendAttack(creature->getId());
|
|
|
|
}
|
|
|
|
|
2012-01-05 15:24:38 +01:00
|
|
|
void Game::cancelAttack()
|
|
|
|
{
|
|
|
|
if(m_attackingCreature) {
|
|
|
|
m_protocolGame->sendAttack(0);
|
2012-01-06 18:29:18 +01:00
|
|
|
m_attackingCreature->hideStaticSquare();
|
2012-01-05 15:24:38 +01:00
|
|
|
m_attackingCreature = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::onAttackCancelled()
|
|
|
|
{
|
|
|
|
if(m_attackingCreature) {
|
2012-01-06 18:29:18 +01:00
|
|
|
m_attackingCreature->hideStaticSquare();
|
2012-01-05 15:24:38 +01:00
|
|
|
m_attackingCreature = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 14:02:35 +01:00
|
|
|
void Game::follow(const CreaturePtr& creature)
|
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !creature || !checkBotProtection())
|
2012-01-04 14:02:35 +01:00
|
|
|
return;
|
|
|
|
|
2012-01-05 15:24:38 +01:00
|
|
|
if(m_followingCreature)
|
2012-01-06 18:29:18 +01:00
|
|
|
m_followingCreature->hideStaticSquare();
|
2012-01-05 15:24:38 +01:00
|
|
|
|
2012-01-06 18:29:18 +01:00
|
|
|
creature->showStaticSquare(Fw::green);
|
2012-01-05 15:24:38 +01:00
|
|
|
m_followingCreature = creature;
|
|
|
|
|
2012-01-04 14:02:35 +01:00
|
|
|
m_protocolGame->sendFollow(creature->getId());
|
|
|
|
}
|
|
|
|
|
2012-01-05 15:24:38 +01:00
|
|
|
void Game::cancelFollow()
|
|
|
|
{
|
|
|
|
if(m_followingCreature) {
|
|
|
|
m_protocolGame->sendFollow(0);
|
2012-01-06 18:29:18 +01:00
|
|
|
m_followingCreature->hideStaticSquare();
|
2012-01-05 15:24:38 +01:00
|
|
|
m_followingCreature = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 14:02:35 +01:00
|
|
|
void Game::rotate(const ThingPtr& thing)
|
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !thing || !checkBotProtection())
|
2012-01-04 14:02:35 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
int stackpos = getThingStackpos(thing);
|
|
|
|
if(stackpos != -1)
|
|
|
|
m_protocolGame->sendRotateItem(thing->getPos(), thing->getId(), stackpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Game::getThingStackpos(const ThingPtr& thing)
|
2012-01-03 23:27:31 +01:00
|
|
|
{
|
|
|
|
// thing is at map
|
2012-01-04 11:26:58 +01:00
|
|
|
if(thing->getPos().x != 65535) {
|
|
|
|
TilePtr tile = g_map.getTile(thing->getPos());
|
2012-01-04 14:02:35 +01:00
|
|
|
return tile->getThingStackpos(thing);
|
2012-01-03 23:27:31 +01:00
|
|
|
}
|
2012-01-04 14:02:35 +01:00
|
|
|
|
|
|
|
// thing is at container or inventory
|
|
|
|
return 0;
|
2012-01-03 23:27:31 +01:00
|
|
|
}
|
|
|
|
|
2011-11-03 17:26:17 +01:00
|
|
|
void Game::talkChannel(int channelType, int channelId, const std::string& message)
|
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !checkBotProtection())
|
2011-11-03 17:26:17 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_protocolGame->sendTalk(channelType, channelId, "", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::talkPrivate(int channelType, const std::string& receiver, const std::string& message)
|
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !checkBotProtection())
|
2011-11-03 17:26:17 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_protocolGame->sendTalk(channelType, 0, receiver, message);
|
|
|
|
}
|
2011-11-14 23:32:55 +01:00
|
|
|
|
2012-01-05 13:48:10 +01:00
|
|
|
void Game::inviteToParty(int creatureId)
|
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !checkBotProtection())
|
2012-01-05 13:48:10 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_protocolGame->sendInviteToParty(creatureId);
|
|
|
|
}
|
|
|
|
|
2011-11-14 23:32:55 +01:00
|
|
|
void Game::openOutfitWindow()
|
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !checkBotProtection())
|
2012-01-04 15:30:28 +01:00
|
|
|
return;
|
|
|
|
|
2011-11-14 23:32:55 +01:00
|
|
|
m_protocolGame->sendGetOutfit();
|
|
|
|
}
|
2011-11-16 18:33:43 +01:00
|
|
|
|
|
|
|
void Game::setOutfit(const Outfit& outfit)
|
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !checkBotProtection())
|
2012-01-04 15:30:28 +01:00
|
|
|
return;
|
|
|
|
|
2011-11-16 18:33:43 +01:00
|
|
|
m_protocolGame->sendSetOutfit(outfit);
|
|
|
|
}
|
2012-01-04 15:30:28 +01:00
|
|
|
|
|
|
|
void Game::addVip(const std::string& name)
|
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || name.empty() || !checkBotProtection())
|
2012-01-04 15:30:28 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_protocolGame->sendAddVip(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::removeVip(int playerId)
|
|
|
|
{
|
2012-01-06 20:25:27 +01:00
|
|
|
if(!m_online || !checkBotProtection())
|
2012-01-04 15:30:28 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_protocolGame->sendRemoveVip(playerId);
|
|
|
|
}
|
2012-01-06 20:25:27 +01:00
|
|
|
|
|
|
|
bool Game::checkBotProtection()
|
|
|
|
{
|
|
|
|
#ifndef DISABLE_BOT_PROTECTION
|
|
|
|
if(g_lua.isInCppCallback() && !g_ui.isOnInputEvent()) {
|
2012-01-07 02:48:16 +01:00
|
|
|
logError("caught a lua call to a bot protected game function, the call was canceled");
|
2012-01-06 20:25:27 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|