From 01e48fbcc8d6f6bc4221daa482c6acd66b3d98a0 Mon Sep 17 00:00:00 2001 From: BeniS Date: Wed, 23 Jan 2013 01:01:30 +1300 Subject: [PATCH] More work towards autowalking (needs more work). * Added tile minimap color override. * Changed zoom in and out of minimap from inverted control. --- modules/game_interface/gameinterface.lua | 2 +- modules/game_minimap/minimap.lua | 4 +- src/client/game.cpp | 8 +-- src/client/localplayer.cpp | 80 ++++++------------------ src/client/localplayer.h | 10 +-- src/client/luafunctions.cpp | 5 +- src/client/tile.cpp | 5 ++ src/client/tile.h | 2 + 8 files changed, 39 insertions(+), 77 deletions(-) diff --git a/modules/game_interface/gameinterface.lua b/modules/game_interface/gameinterface.lua index afcc8279..e7d29324 100644 --- a/modules/game_interface/gameinterface.lua +++ b/modules/game_interface/gameinterface.lua @@ -539,7 +539,7 @@ function processMouseAction(menuPosition, mouseButton, autoWalkPos, lookThing, u end local player = g_game.getLocalPlayer() - player:stopAutoWalkUpdate() + player:stopAutoWalk() if autoWalkPos and keyboardModifiers == KeyboardNoModifier and mouseButton == MouseLeftButton then if not player:autoWalk(autoWalkPos) then diff --git a/modules/game_minimap/minimap.lua b/modules/game_minimap/minimap.lua index 581065ac..005717da 100644 --- a/modules/game_minimap/minimap.lua +++ b/modules/game_minimap/minimap.lua @@ -398,9 +398,9 @@ function onMinimapMouseWheel(self, mousePos, direction) local keyboardModifiers = g_keyboard.getModifiers() if direction == MouseWheelUp and keyboardModifiers == KeyboardNoModifier then - miniMapZoomIn() - elseif direction == MouseWheelDown and keyboardModifiers == KeyboardNoModifier then miniMapZoomOut() + elseif direction == MouseWheelDown and keyboardModifiers == KeyboardNoModifier then + miniMapZoomIn() elseif direction == MouseWheelDown and keyboardModifiers == KeyboardCtrlModifier then minimapFloorUp(1) elseif direction == MouseWheelUp and keyboardModifiers == KeyboardCtrlModifier then diff --git a/src/client/game.cpp b/src/client/game.cpp index c8f775ba..9c8289f4 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -592,7 +592,7 @@ bool Game::walk(Otc::Direction direction) return false; } - m_localPlayer->stopAutoWalkUpdate(); + m_localPlayer->stopAutoWalk(); g_lua.callGlobalField("g_game", "onWalk", direction); @@ -857,7 +857,7 @@ void Game::attack(CreaturePtr creature) cancelFollow(); setAttackingCreature(creature); - m_localPlayer->stopAutoWalkUpdate(); + m_localPlayer->stopAutoWalk(); if(m_protocolVersion >= 963) { if(creature) @@ -881,7 +881,7 @@ void Game::follow(CreaturePtr creature) cancelAttack(); setFollowingCreature(creature); - m_localPlayer->stopAutoWalkUpdate(); + m_localPlayer->stopAutoWalk(); if(m_protocolVersion >= 963) { if(creature) @@ -902,7 +902,7 @@ void Game::cancelAttackAndFollow() if(isAttacking()) setAttackingCreature(nullptr); - m_localPlayer->stopAutoWalkUpdate(); + m_localPlayer->stopAutoWalk(); m_protocolGame->sendCancelAttackAndFollow(); diff --git a/src/client/localplayer.cpp b/src/client/localplayer.cpp index 2e678534..17a491d9 100644 --- a/src/client/localplayer.cpp +++ b/src/client/localplayer.cpp @@ -162,57 +162,38 @@ void LocalPlayer::cancelWalk(Otc::Direction direction) if(direction != Otc::InvalidDirection) setDirection(direction); - updateAutoWalkSteps(true); - callLuaField("onCancelWalk", direction); } -void LocalPlayer::updateAutoWalkSteps(bool walkFailed) -{ - if(!m_lastAutoWalkDestination.isValid()) { - return; - } - - m_autoWalkSteps.push_back(m_position); - if(m_lastAutoWalkDestination != m_position && (m_autoWalkSteps.size() >= Otc::MAX_AUTOWALK_STEPS_RETRY || walkFailed)) { - autoWalk(m_lastAutoWalkDestination); - } - else if(m_position == m_lastAutoWalkDestination && !m_autoWalkQueue.empty()) { - m_autoWalkQueue.pop_back(); - - // task the next destination - if(!m_autoWalkQueue.empty()) { - std::vector destinations = m_position.translatedToDirections(m_autoWalkQueue.back()); - autoWalk(destinations.back()); - } - } -} - bool LocalPlayer::autoWalk(const Position& destination) { - m_autoWalkSteps.clear(); - m_lastAutoWalkDestination = destination; + m_autoWalkDestination = destination; std::tuple, Otc::PathFindResult> result = g_map.findPath(m_position, destination, 1000, Otc::PathFindAllowNullTiles); - - std::vector dirs = std::get<0>(result); - if(dirs.size() == 0) + if(std::get<1>(result) != Otc::PathFindResultOk) return false; - if(dirs.size() > Otc::MAX_AUTOWALK_DIST) { - dirs = calculateAutoWalk(dirs); - m_lastAutoWalkDestination = m_position.translatedToDirections(dirs).back(); + Position currentPos = m_position; + std::vector limitedPath; + + for(auto dir : std::get<0>(result)) { + currentPos = currentPos.translatedToDirection(dir); + if(!hasSight(currentPos)) + break; + else + limitedPath.push_back(dir); } - g_game.autoWalk(dirs); + m_lastAutoWalkPosition = m_position.translatedToDirections(limitedPath).back(); + + g_game.autoWalk(limitedPath); return true; } -void LocalPlayer::stopAutoWalkUpdate() +void LocalPlayer::stopAutoWalk() { - m_lastAutoWalkDestination = Position(); - m_autoWalkSteps.clear(); - m_autoWalkQueue.clear(); + m_autoWalkDestination = Position(); + m_lastAutoWalkPosition = Position(); } void LocalPlayer::stopWalk() @@ -223,29 +204,6 @@ void LocalPlayer::stopWalk() m_lastPrewalkDestionation = Position(); } -std::vector& LocalPlayer::calculateAutoWalk(std::vector& dirs) -{ - if(dirs.size() > Otc::MAX_AUTOWALK_DIST) { - // populate auto walk queue - m_autoWalkQueue.clear(); - size_t blocks = std::ceil((float)dirs.size() / Otc::MAX_AUTOWALK_DIST); - - size_t size = Otc::MAX_AUTOWALK_DIST; - for(size_t i = 0, offset=0 ; i < blocks ; ++i) { - std::vector::iterator next = dirs.begin() + offset; - if((offset + size) < dirs.size()) - m_autoWalkQueue.push_back(std::vector(next, next + size)); - else - m_autoWalkQueue.push_back(std::vector(next, dirs.end())); - offset += size; - } - std::reverse(m_autoWalkQueue.begin(), m_autoWalkQueue.end()); - - return m_autoWalkQueue.back(); - } - return dirs; -} - void LocalPlayer::updateWalkOffset(int totalPixelsWalked) { // pre walks offsets are calculated in the oposite direction @@ -310,7 +268,8 @@ void LocalPlayer::onPositionChange(const Position& newPos, const Position& oldPo { Creature::onPositionChange(newPos, oldPos); - updateAutoWalkSteps(); + if(m_autoWalkDestination.isValid() && newPos == m_lastAutoWalkPosition) + autoWalk(m_autoWalkDestination); } void LocalPlayer::setStates(int states) @@ -318,6 +277,7 @@ void LocalPlayer::setStates(int states) if(m_states != states) { int oldStates = m_states; m_states = states; + callLuaField("onStatesChange", states, oldStates); } } diff --git a/src/client/localplayer.h b/src/client/localplayer.h index 29ba04ee..abdcef2f 100644 --- a/src/client/localplayer.h +++ b/src/client/localplayer.h @@ -37,11 +37,9 @@ public: void unlockWalk() { m_walkLockExpiration = 0; } void lockWalk(int millis = 250); - void stopAutoWalkUpdate(); - void updateAutoWalkSteps(bool walkFailed = false); + void stopAutoWalk(); bool autoWalk(const Position& destination); bool canWalk(Otc::Direction direction); - std::vector& calculateAutoWalk(std::vector& dirs); void setStates(int states); void setSkill(Otc::Skill skill, int level, int levelPercent); @@ -91,7 +89,6 @@ public: double getOfflineTrainingTime() { return m_offlineTrainingTime; } std::vector getSpells() { return m_spells; } ItemPtr getInventoryItem(Otc::InventorySlot inventory) { return m_inventoryItems[inventory]; } - std::vector getAutoWalkSteps() { return m_autoWalkSteps; } bool hasSight(const Position& pos); bool isKnown() { return m_known; } @@ -123,7 +120,8 @@ private: // walk related Timer m_walkPingTimer; Position m_lastPrewalkDestionation; - Position m_lastAutoWalkDestination; + Position m_autoWalkDestination; + Position m_lastAutoWalkPosition; ScheduledEventPtr m_autoWalkEndEvent; ticks_t m_walkLockExpiration; int m_lastWalkPing; @@ -143,8 +141,6 @@ private: std::array m_skillsBaseLevel; std::array m_skillsLevelPercent; std::vector m_spells; - std::vector m_autoWalkSteps; - std::vector > m_autoWalkQueue; int m_states; int m_vocation; diff --git a/src/client/luafunctions.cpp b/src/client/luafunctions.cpp index 4f00d85f..b6336379 100644 --- a/src/client/luafunctions.cpp +++ b/src/client/luafunctions.cpp @@ -477,10 +477,8 @@ void Client::registerLuaFunctions() g_lua.bindClassMemberFunction("isPreWalking", &LocalPlayer::isPreWalking); g_lua.bindClassMemberFunction("hasSight", &LocalPlayer::hasSight); g_lua.bindClassMemberFunction("isAutoWalking", &LocalPlayer::isAutoWalking); - g_lua.bindClassMemberFunction("stopAutoWalkUpdate", &LocalPlayer::stopAutoWalkUpdate); - g_lua.bindClassMemberFunction("updateAutoWalkSteps", &LocalPlayer::updateAutoWalkSteps); + g_lua.bindClassMemberFunction("stopAutoWalk", &LocalPlayer::stopAutoWalk); g_lua.bindClassMemberFunction("autoWalk", &LocalPlayer::autoWalk); - g_lua.bindClassMemberFunction("getAutoWalkSteps", &LocalPlayer::getAutoWalkSteps); g_lua.registerClass(); g_lua.bindClassMemberFunction("clean", &Tile::clean); @@ -508,6 +506,7 @@ void Client::registerLuaFunctions() g_lua.bindClassMemberFunction("hasCreature", &Tile::hasCreature); g_lua.bindClassMemberFunction("isEmpty", &Tile::isEmpty); g_lua.bindClassMemberFunction("isClickable", &Tile::isClickable); + g_lua.bindClassMemberFunction("overwriteMinimapColor", &Tile::overwriteMinimapColor); g_lua.registerClass(); g_lua.bindClassStaticFunction("create", []{ return UIItemPtr(new UIItem); }); diff --git a/src/client/tile.cpp b/src/client/tile.cpp index 015e8c22..6983ea2a 100644 --- a/src/client/tile.cpp +++ b/src/client/tile.cpp @@ -34,6 +34,7 @@ Tile::Tile(const Position& position) : m_position(position), m_drawElevation(0), + m_minimapColor(0), m_flags(0) { } @@ -338,6 +339,10 @@ int Tile::getGroundSpeed() uint8 Tile::getMinimapColorByte() { uint8 color = 0; + if(m_minimapColor != 0) { + return m_minimapColor; + } + for(const ThingPtr& thing : m_things) { if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop()) break; diff --git a/src/client/tile.h b/src/client/tile.h index eb662e22..0f3b9ebe 100644 --- a/src/client/tile.h +++ b/src/client/tile.h @@ -110,6 +110,7 @@ public: bool limitsFloorsView(); bool canErase(); bool hasElevation(int elevation = 1); + void overwriteMinimapColor(uint8 color) { m_minimapColor = color; } void setFlags(tileflags_t flags) { m_flags |= (uint32)flags; } uint32 getFlags() { return m_flags; } @@ -128,6 +129,7 @@ private: stdext::packed_vector m_things; Position m_position; uint8 m_drawElevation; + uint8 m_minimapColor; uint32 m_flags, m_houseId; }; #pragma pack(pop)