diff --git a/modules/game_minimap/minimap.lua b/modules/game_minimap/minimap.lua index 27250f21..04386b08 100644 --- a/modules/game_minimap/minimap.lua +++ b/modules/game_minimap/minimap.lua @@ -2,8 +2,6 @@ DEFAULT_ZOOM = 60 MAX_FLOOR_UP = 0 MAX_FLOOR_DOWN = 15 -G.minimapFirstLoad = true - navigating = false minimapWidget = nil minimapButton = nil @@ -15,8 +13,11 @@ minimapWindow = nil you change floor it will not update the minimap. ]] function init() - connect(g_game, { onGameStart = reset, - onForceWalk = center }) + connect(g_game, { + onGameStart = online, + onGameEnd = offline, + onForceWalk = center, + }) g_keyboard.bindKeyDown('Ctrl+M', toggle) @@ -37,29 +38,18 @@ function init() minimapWidget.onMouseWheel = onMinimapMouseWheel reset() - - -- load only the first time (avoid load/save between reloads) - --[[ - if G.minimapFirstLoad then - G.minimapFirstLoad = false - if g_resources.fileExists('/minimap.otcm') then - if g_game.isOnline() then - perror('cannot load minimap while online') - else - g_map.loadOtcm('/minimap.otcm') - end - end - - -- save only when closing the client - connect(g_app, { onTerminate = function() - g_map.saveOtcm('/minimap.otcm') - end}) - end]]-- end function terminate() - disconnect(g_game, { onGameStart = reset, - onForceWalk = center }) + disconnect(g_game, { + onGameStart = online, + onGameEnd = offline, + onForceWalk = center, + }) + + if g_game.isOnline() then + saveMap() + end g_keyboard.unbindKeyDown('Ctrl+M') @@ -67,6 +57,28 @@ function terminate() minimapWindow:destroy() end +function online() + reset() + loadMap() +end + +function offline() + saveMap() +end + +function loadMap() + local clientVersion = g_game.getClientVersion() + local minimapName = '/minimap_' .. clientVersion .. '.otcm' + g_map.clean() + g_map.loadOtcm(minimapName) +end + +function saveMap() + local clientVersion = g_game.getClientVersion() + local minimapName = '/minimap_' .. clientVersion .. '.otcm' + g_map.saveOtcm(minimapName) +end + function toggle() if minimapButton:isOn() then minimapWindow:close() diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index 8496077d..4f0d980a 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -89,6 +89,7 @@ void OTClient::registerLuaFunctions() g_lua.bindSingletonFunction("g_map", "getThing", &Map::getThing, &g_map); g_lua.bindSingletonFunction("g_map", "removeThingByPos", &Map::removeThingByPos, &g_map); g_lua.bindSingletonFunction("g_map", "removeThing", &Map::removeThing, &g_map); + g_lua.bindSingletonFunction("g_map", "clean", &Map::clean, &g_map); g_lua.bindSingletonFunction("g_map", "cleanTile", &Map::cleanTile, &g_map); g_lua.bindSingletonFunction("g_map", "cleanTexts", &Map::cleanTexts, &g_map); g_lua.bindSingletonFunction("g_map", "getTile", &Map::getTile, &g_map); diff --git a/src/otclient/mapio.cpp b/src/otclient/mapio.cpp index 8d50b44d..21ae6c17 100644 --- a/src/otclient/mapio.cpp +++ b/src/otclient/mapio.cpp @@ -22,7 +22,7 @@ #include "map.h" #include "tile.h" - +#include "game.h" #include #include @@ -300,7 +300,7 @@ void Map::saveOtbm(const std::string &fileName) Position base(-1, -1, -1); bool firstNode = true; - for(uint8_t z = 0; z < Otc::MAX_Z + 1; ++z) { + for(uint8_t z = 0; z <= Otc::MAX_Z; ++z) { for(const auto& it : m_tileBlocks[z]) { const TileBlock& block = it.second; for(const TilePtr& tile : block.getTiles()) { @@ -499,9 +499,8 @@ bool Map::loadOtcm(const std::string& fileName) void Map::saveOtcm(const std::string& fileName) { -#if 0 try { - g_clock.update(); + stdext::timer saveTimer; FileStreamPtr fin = g_resources.createFile(fileName); fin->cache(); @@ -527,29 +526,33 @@ void Map::saveOtcm(const std::string& fileName) fin->addU16(start); fin->seek(start); - for(auto& pair : m_tiles) { - const TilePtr& tile = pair.second; - if(!tile || tile->isEmpty()) - continue; + for(uint8_t z = 0; z <= Otc::MAX_Z; ++z) { + for(const auto& it : m_tileBlocks[z]) { + const TileBlock& block = it.second; + for(const TilePtr& tile : block.getTiles()) { + if(!tile || tile->isEmpty()) + continue; - Position pos = pair.first; - fin->addU16(pos.x); - fin->addU16(pos.y); - fin->addU8(pos.z); + Position pos = tile->getPosition(); + fin->addU16(pos.x); + fin->addU16(pos.y); + fin->addU8(pos.z); - const auto& list = tile->getThings(); - auto first = std::find_if(list.begin(), list.end(), [](const ThingPtr& thing) { return thing->isItem(); }); - for(auto it = first, end = list.end(); it != end; ++it) { - const ThingPtr& thing = *it; - if(thing->isItem()) { - ItemPtr item = thing->static_self_cast(); - fin->addU16(item->getId()); - fin->addU8(item->getCountOrSubType()); + const auto& list = tile->getThings(); + auto first = std::find_if(list.begin(), list.end(), [](const ThingPtr& thing) { return thing->isItem(); }); + for(auto it = first, end = list.end(); it != end; ++it) { + const ThingPtr& thing = *it; + if(thing->isItem()) { + ItemPtr item = thing->static_self_cast(); + fin->addU16(item->getId()); + fin->addU8(item->getCountOrSubType()); + } + } + + // end of tile + fin->addU16(0xFFFF); } } - - // end of tile - fin->addU16(0xFFFF); } // end of file @@ -559,9 +562,10 @@ void Map::saveOtcm(const std::string& fileName) fin->addU8(invalidPos.z); fin->flush(); + fin->close(); + g_logger.debug(stdext::format("Otcm save time: %.2f seconds", saveTimer.elapsed_seconds())); } catch(stdext::exception& e) { g_logger.error(stdext::format("failed to save OTCM map: %s", e.what())); } -#endif }