diff --git a/TODO b/TODO index fdd11e66..7de755ef 100644 --- a/TODO +++ b/TODO @@ -66,6 +66,7 @@ [bart] draw lights using shaders [bart] chat with tabs [bart] limit FPS in options +[baxnie] display 'You are dead.' message [baxnie] do lua game event calls from Game instead from GameProtocol [baxnie] classic control [baxnie] trade window diff --git a/modules/game/game.lua b/modules/game/game.lua index 6b07c236..e7a1132b 100644 --- a/modules/game/game.lua +++ b/modules/game/game.lua @@ -56,5 +56,9 @@ function Game.onConnectionError(message) errorBox.onOk = CharacterList.show end +function Game.onDeath() + print('dead') +end + connect(Game, { onLogin = Game.createInterface }, true) connect(Game, { onLogout = Game.destroyInterface }) diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 9500bc6b..0884ffec 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -32,6 +32,8 @@ Game g_game; void Game::loginWorld(const std::string& account, const std::string& password, const std::string& worldHost, int worldPort, const std::string& characterName) { + m_online = false; + m_dead = false; m_protocolGame = ProtocolGamePtr(new ProtocolGame); m_protocolGame->login(account, password, worldHost, (uint16)worldPort, characterName); } @@ -72,7 +74,6 @@ void Game::processLogin(const LocalPlayerPtr& localPlayer) { m_localPlayer = localPlayer; m_online = true; - g_lua.callGlobalField("Game", "onLogin", m_localPlayer); } @@ -91,6 +92,15 @@ void Game::processLogout() } } +void Game::processDeath() +{ + m_dead = true; + g_lua.callGlobalField("Game","onDeath"); + + // force logout in five seconds + g_dispatcher.scheduleEvent(std::bind(&Game::forceLogout, &g_game), 5 * 1000); +} + void Game::processTextMessage(int type, const std::string& message) { g_lua.callGlobalField("Game","onTextMessage", type, message); @@ -104,9 +114,17 @@ void Game::processInventoryChange(int slot, const ItemPtr& item) g_lua.callGlobalField("Game","onInventoryChange", slot, item); } +void Game::processAttackCancel() +{ + if(m_attackingCreature) { + m_attackingCreature->hideStaticSquare(); + m_attackingCreature = nullptr; + } +} + void Game::walk(Otc::Direction direction) { - if(!m_online || !m_localPlayer->canWalk(direction) || !checkBotProtection()) + if(!isOnline() || isDead() || !checkBotProtection() || !m_localPlayer->canWalk(direction)) return; cancelFollow(); @@ -205,14 +223,6 @@ void Game::cancelAttack() } } -void Game::onAttackCancelled() -{ - if(m_attackingCreature) { - m_attackingCreature->hideStaticSquare(); - m_attackingCreature = nullptr; - } -} - void Game::follow(const CreaturePtr& creature) { if(!m_online || !creature || !checkBotProtection()) diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index fe9828a5..fd642c37 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -31,20 +31,23 @@ class Game { public: - // login/logout related void loginWorld(const std::string& account, const std::string& password, const std::string& worldHost, int worldPort, const std::string& characterName); void cancelLogin(); void logout(bool force); + void forceLogout() { logout(true); } + void cleanLogout() { logout(false); } void processLoginError(const std::string& error); void processConnectionError(const boost::system::error_code& error); void processLogin(const LocalPlayerPtr& localPlayer); void processLogout(); + void processDeath(); void processTextMessage(int type, const std::string& message); void processInventoryChange(int slot, const ItemPtr& item); + void processAttackCancel(); void walk(Otc::Direction direction); void turn(Otc::Direction direction); @@ -63,7 +66,6 @@ public: void addVip(const std::string& name); void removeVip(int playerId); int getThingStackpos(const ThingPtr& thing); - void onAttackCancelled(); bool checkBotProtection(); @@ -71,6 +73,7 @@ public: CreaturePtr getFollowingCreature() { return m_followingCreature; } bool isOnline() { return m_online; } + bool isDead() { return m_dead; } void setServerBeat(int serverBeat) { m_serverBeat = serverBeat; } int getServerBeat() { return m_serverBeat; } @@ -82,6 +85,7 @@ private: LocalPlayerPtr m_localPlayer; ProtocolGamePtr m_protocolGame; bool m_online; + bool m_dead; int m_serverBeat; CreaturePtr m_attackingCreature, m_followingCreature; diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index 5c8c000d..972bceb5 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -306,6 +306,7 @@ void ProtocolGame::parsePing(InputMessage&) void ProtocolGame::parseDeath(InputMessage& msg) { msg.getU8(); // 100 is a fair death. < 100 is a unfair death. + g_dispatcher.addEvent(std::bind(&Game::processDeath, &g_game)); } void ProtocolGame::parseMapDescription(InputMessage& msg) @@ -736,7 +737,7 @@ void ProtocolGame::parsePlayerIcons(InputMessage& msg) void ProtocolGame::parsePlayerCancelAttack(InputMessage& msg) { msg.getU32(); // unknown - g_game.onAttackCancelled(); + g_dispatcher.addEvent(std::bind(&Game::processAttackCancel, &g_game)); } void ProtocolGame::parseCreatureSpeak(InputMessage& msg)