From 8bc63e25dfa7af6759c3762718dcc692a817d39e Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Fri, 23 Mar 2012 11:48:00 -0300 Subject: [PATCH] simplistic autowalk * add simple and stupid autowalk algorithm * fix issue in classic control --- modules/core_lib/const.lua | 10 ------- modules/game/const.lua | 9 ++++++ modules/game/gameinterface.lua | 2 +- modules/game/widgets/uigamemap.lua | 34 ++++++++++++++++++++- modules/game_textmessage/textmessage.lua | 2 +- src/otclient/core/game.cpp | 4 +-- src/otclient/core/game.h | 2 +- src/otclient/luafunctions.cpp | 2 +- src/otclient/net/protocolcodes.h | 2 +- src/otclient/net/protocolgame.h | 2 +- src/otclient/net/protocolgamesend.cpp | 38 ++++++++++++++++++++++-- 11 files changed, 85 insertions(+), 22 deletions(-) diff --git a/modules/core_lib/const.lua b/modules/core_lib/const.lua index de7a2fd4..18ce7d13 100644 --- a/modules/core_lib/const.lua +++ b/modules/core_lib/const.lua @@ -49,16 +49,6 @@ AlignTopCenter = 20 AlignBottomCenter = 24 AlignCenter = 48 -North = 0 -East = 1 -South = 2 -West = 3 -NorthEast = 4 -SouthEast = 5 -SouthWest = 6 -NorthWest = 7 - - KeyUnknown = 0 KeyEscape = 1 KeyTab = 2 diff --git a/modules/game/const.lua b/modules/game/const.lua index cc712f29..24f65899 100644 --- a/modules/game/const.lua +++ b/modules/game/const.lua @@ -22,3 +22,12 @@ EmblemNone = 0 EmblemGreen = 1 EmblemRed = 2 EmblemBlue = 3 + +North = 0 +East = 1 +South = 2 +West = 3 +NorthEast = 4 +SouthEast = 5 +SouthWest = 6 +NorthWest = 7 diff --git a/modules/game/gameinterface.lua b/modules/game/gameinterface.lua index 3cd52550..048bd496 100644 --- a/modules/game/gameinterface.lua +++ b/modules/game/gameinterface.lua @@ -211,7 +211,7 @@ function GameInterface.processMouseAction(menuPosition, mouseButton, autoWalk, l return true end - if not Options['classicControl'] then + if not Options.getOption('classicControl') then if keyboardModifiers == KeyboardNoModifier and mouseButton == MouseRightButton then GameInterface.createThingMenu(menuPosition, lookThing, useThing, creatureThing) return true diff --git a/modules/game/widgets/uigamemap.lua b/modules/game/widgets/uigamemap.lua index 597f7be3..10c564da 100644 --- a/modules/game/widgets/uigamemap.lua +++ b/modules/game/widgets/uigamemap.lua @@ -59,6 +59,38 @@ end function UIGameMap:onMouseRelease(mousePosition, mouseButton) local tile = self:getTile(mousePosition) - if tile and GameInterface.processMouseAction(mousePosition, mouseButton, nil, tile:getTopLookThing(), tile:getTopUseThing(), tile:getTopCreature(), tile:getTopMultiUseThing()) then return true end + if tile == nil then return false end + if GameInterface.processMouseAction(mousePosition, mouseButton, nil, tile:getTopLookThing(), tile:getTopUseThing(), tile:getTopCreature(), tile:getTopMultiUseThing()) then + return true + elseif mouseButton == MouseLeftButton then + local fromPos = g_game.getLocalPlayer():getPosition() + local toPos = tile:getPosition() + if fromPos.z ~= toPos.z then + TextMessage.displayStatus('There is no way.') + return true + end + + -- simple and stupid pathfinding algorithm + local dirs = {} + local pathPos = fromPos + while pathPos.x ~= toPos.x or pathPos.y ~= toPos.y do + if pathPos.x < toPos.x then + pathPos.x = pathPos.x + 1 + table.insert(dirs, East) + elseif pathPos.x > toPos.x then + pathPos.x = pathPos.x - 1 + table.insert(dirs, West) + elseif pathPos.y < toPos.y then + pathPos.y = pathPos.y + 1 + table.insert(dirs, South) + else --if pathPos.y > toPos.y then + pathPos.y = pathPos.y - 1 + table.insert(dirs, North) + end + end + + g_game.autoWalk(dirs) + return true + end return false end diff --git a/modules/game_textmessage/textmessage.lua b/modules/game_textmessage/textmessage.lua index fcf2f492..9cf0e734 100644 --- a/modules/game_textmessage/textmessage.lua +++ b/modules/game_textmessage/textmessage.lua @@ -113,7 +113,7 @@ function TextMessage.clearMessages() end function TextMessage.displayStatus(msg, time) - displayMessage(MessageTypes.warning, msg) + displayMessage(MessageTypes.statusSmall, msg) end function TextMessage.displayEventAdvance(msg, time) diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 39d577d8..da002c7d 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -372,7 +372,7 @@ void Game::walk(Otc::Direction direction) forceWalk(direction); } -void Game::walkPath(const std::vector& dir) +void Game::autoWalk(const std::vector& dir) { if(!canPerformGameAction()) return; @@ -380,7 +380,7 @@ void Game::walkPath(const std::vector& dir) if(isFollowing()) cancelFollow(); - m_protocolGame->sendWalkPath(dir); + m_protocolGame->sendAutoWalk(dir); } void Game::forceWalk(Otc::Direction direction) diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index 850bac1e..007887c4 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -123,7 +123,7 @@ public: // walk related void walk(Otc::Direction direction); - void walkPath(const std::vector& dir); + void autoWalk(const std::vector& dir); void forceWalk(Otc::Direction direction); void turn(Otc::Direction direction); void stop(); diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index 942a7533..79dc5a4e 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -82,7 +82,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassStaticFunction("g_game", "forceLogout", std::bind(&Game::forceLogout, &g_game)); g_lua.bindClassStaticFunction("g_game", "safeLogout", std::bind(&Game::safeLogout, &g_game)); g_lua.bindClassStaticFunction("g_game", "walk", std::bind(&Game::walk, &g_game, _1)); - g_lua.bindClassStaticFunction("g_game", "walkPath", std::bind(&Game::walkPath, &g_game, _1)); + g_lua.bindClassStaticFunction("g_game", "autoWalk", std::bind(&Game::autoWalk, &g_game, _1)); g_lua.bindClassStaticFunction("g_game", "forceWalk", std::bind(&Game::forceWalk, &g_game, _1)); g_lua.bindClassStaticFunction("g_game", "turn", std::bind(&Game::turn, &g_game, _1)); g_lua.bindClassStaticFunction("g_game", "stop", std::bind(&Game::stop, &g_game)); diff --git a/src/otclient/net/protocolcodes.h b/src/otclient/net/protocolcodes.h index fc378d6d..ab55a751 100644 --- a/src/otclient/net/protocolcodes.h +++ b/src/otclient/net/protocolcodes.h @@ -155,7 +155,7 @@ namespace Proto { ClientEnterGame = 10, ClientLeaveGame = 20, ClientPingResponse = 30, - ClientWalkPath = 100, + ClientAutoWalk = 100, ClientWalkNorth = 101, ClientWalkEast = 102, ClientWalkSouth = 103, diff --git a/src/otclient/net/protocolgame.h b/src/otclient/net/protocolgame.h index 0bbe1fa6..123fdc0c 100644 --- a/src/otclient/net/protocolgame.h +++ b/src/otclient/net/protocolgame.h @@ -42,7 +42,7 @@ public: void sendLogout(); void sendPingResponse(); - void sendWalkPath(const std::vector& path); + void sendAutoWalk(const std::vector& path); void sendWalkNorth(); void sendWalkEast(); void sendWalkSouth(); diff --git a/src/otclient/net/protocolgamesend.cpp b/src/otclient/net/protocolgamesend.cpp index b9189f79..aa9a68b1 100644 --- a/src/otclient/net/protocolgamesend.cpp +++ b/src/otclient/net/protocolgamesend.cpp @@ -79,12 +79,44 @@ void ProtocolGame::sendPingResponse() send(msg); } -void ProtocolGame::sendWalkPath(const std::vector& path) +void ProtocolGame::sendAutoWalk(const std::vector& path) { OutputMessage msg; + msg.addU8(Proto::ClientAutoWalk); msg.addU8(path.size()); - for(Otc::Direction dir : path) - msg.addU8(dir); + for(Otc::Direction dir : path) { + uint8 byte; + switch(dir) { + case Otc::East: + byte = 1; + break; + case Otc::NorthEast: + byte = 2; + break; + case Otc::North: + byte = 3; + break; + case Otc::NorthWest: + byte = 4; + break; + case Otc::West: + byte = 5; + break; + case Otc::SouthWest: + byte = 6; + break; + case Otc::South: + byte = 7; + break; + case Otc::SouthEast: + byte = 8; + break; + default: + byte = 0; + break; + } + msg.addU8(byte); + } send(msg); }