From adaa4cc2f3c6e624b2c9ab7f40df4139b2a61c5a Mon Sep 17 00:00:00 2001 From: cx384 Date: Wed, 7 Feb 2024 20:13:23 +0100 Subject: [PATCH] Move hard coded minimap to builtin (#14071) --- builtin/game/statbars.lua | 34 +++++++++++++++++++++++++++-- doc/lua_api.md | 2 +- src/client/client.cpp | 10 --------- src/client/client.h | 5 ----- src/client/game.cpp | 16 +++----------- src/client/gameui.cpp | 5 ----- src/client/gameui.h | 3 --- src/client/hud.cpp | 8 +++++++ src/client/minimap.cpp | 11 ---------- src/client/minimap.h | 1 - src/client/render/core.cpp | 5 ++--- src/client/render/core.h | 2 +- src/client/render/pipeline.h | 1 - src/client/render/plain.cpp | 3 --- src/client/renderingengine.cpp | 4 ++-- src/client/renderingengine.h | 2 +- src/network/clientpackethandler.cpp | 9 -------- src/network/networkprotocol.h | 1 + src/script/lua_api/l_minimap.cpp | 2 -- src/unittest/test_gameui.cpp | 14 ------------ 20 files changed, 51 insertions(+), 87 deletions(-) diff --git a/builtin/game/statbars.lua b/builtin/game/statbars.lua index 72b711bdc..2475f9d26 100644 --- a/builtin/game/statbars.lua +++ b/builtin/game/statbars.lua @@ -24,6 +24,13 @@ local bar_definitions = { size = {x = 24, y = 24}, offset = {x = 25, y= -(48 + 24 + 16)}, }, + minimap = { + hud_elem_type = "minimap", + position = {x = 1, y = 0}, + alignment = {x = -1, y = 1}, + offset = {x = -10, y = 10}, + size = {x = 256 , y = 256}, + }, } local hud_ids = {} @@ -92,6 +99,16 @@ local function update_builtin_statbars(player) end, name, hud.id_breathbar) hud.id_breathbar = nil end + + -- Don't add a minimap for clients which already have it hardcoded in C++. + local show_minimap = flags.minimap and + minetest.get_player_information(name).protocol_version >= 44 + if show_minimap and not hud.id_minimap then + hud.id_minimap = player:hud_add(bar_definitions.minimap) + elseif not show_minimap and hud.id_minimap then + player:hud_remove(hud.id_minimap) + hud.id_minimap = nil + end end local function cleanup_builtin_statbars(player) @@ -138,8 +155,7 @@ local function player_event_handler(player,eventname) end function core.hud_replace_builtin(hud_name, definition) - if type(definition) ~= "table" or - (definition.type or definition.hud_elem_type) ~= "statbar" then + if type(definition) ~= "table" then return false end @@ -175,6 +191,20 @@ function core.hud_replace_builtin(hud_name, definition) return true end + if hud_name == "minimap" then + bar_definitions.minimap = definition + + for name, ids in pairs(hud_ids) do + local player = core.get_player_by_name(name) + if player and ids.id_minimap then + player:hud_remove(ids.id_minimap) + ids.id_minimap = nil + update_builtin_statbars(player) + end + end + return true + end + return false end diff --git a/doc/lua_api.md b/doc/lua_api.md index 49c96b925..6f5bb8683 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -6800,7 +6800,7 @@ Misc. (regardless of online status) * `minetest.hud_replace_builtin(name, hud_definition)` * Replaces definition of a builtin hud element - * `name`: `"breath"` or `"health"` + * `name`: `"breath"`, `"health"` or `"minimap"` * `hud_definition`: definition to replace builtin definition * `minetest.parse_relative_number(arg, relative_to)`: returns number or nil * Helper function for chat commands. diff --git a/src/client/client.cpp b/src/client/client.cpp index 5d9c07ce5..435775d87 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -1971,21 +1971,11 @@ void Client::makeScreenshot() raw_image->drop(); } -bool Client::shouldShowMinimap() const -{ - return !m_minimap_disabled_by_server; -} - void Client::pushToEventQueue(ClientEvent *event) { m_client_event_queue.push(event); } -void Client::showMinimap(const bool show) -{ - m_game_ui->showMinimap(show); -} - // IGameDef interface // Under envlock IItemDefManager* Client::getItemDefManager() diff --git a/src/client/client.h b/src/client/client.h index 3a630c27c..9f898e78a 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -369,8 +369,6 @@ public: Camera* getCamera () { return m_camera; } scene::ISceneManager *getSceneManager(); - bool shouldShowMinimap() const; - // IGameDef interface IItemDefManager* getItemDefManager() override; const NodeDefManager* getNodeDefManager() override; @@ -412,8 +410,6 @@ public: void pushToEventQueue(ClientEvent *event); - void showMinimap(bool show = true); - // IP and port we're connected to const Address getServerAddress(); @@ -498,7 +494,6 @@ private: ELoginRegister m_allow_login_or_register = ELoginRegister::Any; Camera *m_camera = nullptr; Minimap *m_minimap = nullptr; - bool m_minimap_disabled_by_server = false; // Server serialization version u8 m_server_ser_ver; diff --git a/src/client/game.cpp b/src/client/game.cpp index b0890a844..01f252be6 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -1290,9 +1290,6 @@ void Game::run() updateFrame(&graph, &stats, dtime, cam_view); updateProfilerGraphs(&graph); - // Update if minimap has been disabled by the server - m_game_ui->m_flags.show_minimap &= client->shouldShowMinimap(); - if (m_does_lost_focus_pause_game && !device->isWindowFocused() && !isMenuActive()) { showPauseMenu(); } @@ -2474,23 +2471,16 @@ void Game::toggleMinimap(bool shift_pressed) // --> u32 hud_flags = client->getEnv().getLocalPlayer()->hud_flags; - if (!(hud_flags & HUD_FLAG_MINIMAP_VISIBLE)) { - m_game_ui->m_flags.show_minimap = false; - } else { - + if (hud_flags & HUD_FLAG_MINIMAP_VISIBLE) { // If radar is disabled, try to find a non radar mode or fall back to 0 if (!(hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE)) while (mapper->getModeIndex() && mapper->getModeDef().type == MINIMAP_TYPE_RADAR) mapper->nextMode(); - - m_game_ui->m_flags.show_minimap = mapper->getModeDef().type != - MINIMAP_TYPE_OFF; } // <-- // End of 'not so satifying code' - if ((hud_flags & HUD_FLAG_MINIMAP_VISIBLE) || - (hud && hud->hasElementOfType(HUD_ELEM_MINIMAP))) + if (hud && hud->hasElementOfType(HUD_ELEM_MINIMAP)) m_game_ui->showStatusText(utf8_to_wide(mapper->getModeDef().label)); else m_game_ui->showTranslatedStatusText("Minimap currently disabled by game or mod"); @@ -4358,7 +4348,7 @@ void Game::drawScene(ProfilerGraph *graph, RunStats *stats) draw_crosshair = false; #endif this->m_rendering_engine->draw_scene(sky_color, this->m_game_ui->m_flags.show_hud, - this->m_game_ui->m_flags.show_minimap, draw_wield_tool, draw_crosshair); + draw_wield_tool, draw_crosshair); /* Profiler graph diff --git a/src/client/gameui.cpp b/src/client/gameui.cpp index 3ea1e2624..0577943cb 100644 --- a/src/client/gameui.cpp +++ b/src/client/gameui.cpp @@ -219,11 +219,6 @@ void GameUI::initFlags() m_flags.show_minimal_debug = g_settings->getBool("show_debug"); } -void GameUI::showMinimap(bool show) -{ - m_flags.show_minimap = show; -} - void GameUI::showTranslatedStatusText(const char *str) { showStatusText(wstrgettext(str)); diff --git a/src/client/gameui.h b/src/client/gameui.h index f97ee7e50..5b87d43e6 100644 --- a/src/client/gameui.h +++ b/src/client/gameui.h @@ -57,7 +57,6 @@ public: { bool show_chat = true; bool show_hud = true; - bool show_minimap = false; bool show_minimal_debug = false; bool show_basic_debug = false; bool show_profiler_graph = false; @@ -71,8 +70,6 @@ public: void initFlags(); const Flags &getFlags() const { return m_flags; } - void showMinimap(bool show); - inline void setInfoText(const std::wstring &str) { m_infotext = str; } inline void clearInfoText() { m_infotext.clear(); } diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 1201b95b9..d23b96e5b 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -340,6 +340,14 @@ void Hud::drawLuaElements(const v3s16 &camera_offset) std::vector elems; elems.reserve(player->maxHudId()); + // Add builtin minimap if the server doesn't send it. + HudElement minimap; + if (client->getProtoVersion() < 44 && (player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE)) { + minimap = {HUD_ELEM_MINIMAP, v2f(1, 0), "", v2f(), "", 0 , 0, 0, v2f(-1, 1), + v2f(-10, 10), v3f(), v2s32(256, 256), 0, "", 0}; + elems.push_back(&minimap); + } + for (size_t i = 0; i != player->maxHudId(); i++) { HudElement *e = player->getHud(i); if (!e) diff --git a/src/client/minimap.cpp b/src/client/minimap.cpp index 8ebe2fb03..535a78d1d 100644 --- a/src/client/minimap.cpp +++ b/src/client/minimap.cpp @@ -571,17 +571,6 @@ scene::SMeshBuffer *Minimap::getMinimapMeshBuffer() return buf; } -void Minimap::drawMinimap() -{ - // Non hud managed minimap drawing (legacy minimap) - v2u32 screensize = RenderingEngine::getWindowSize(); - const u32 size = 0.25 * screensize.Y; - - drawMinimap(core::rect( - screensize.X - size - 10, 10, - screensize.X - 10, size + 10)); -} - void Minimap::drawMinimap(core::rect rect) { video::ITexture *minimap_texture = getMinimapTexture(); diff --git a/src/client/minimap.h b/src/client/minimap.h index f06a2e173..f7cbacfb2 100644 --- a/src/client/minimap.h +++ b/src/client/minimap.h @@ -152,7 +152,6 @@ public: void removeMarker(MinimapMarker **marker); void updateActiveMarkers(); - void drawMinimap(); void drawMinimap(core::rect rect); video::IVideoDriver *driver; diff --git a/src/client/render/core.cpp b/src/client/render/core.cpp index 37a5106df..dfd9dc02e 100644 --- a/src/client/render/core.cpp +++ b/src/client/render/core.cpp @@ -36,7 +36,7 @@ RenderingCore::~RenderingCore() delete shadow_renderer; } -void RenderingCore::draw(video::SColor _skycolor, bool _show_hud, bool _show_minimap, +void RenderingCore::draw(video::SColor _skycolor, bool _show_hud, bool _draw_wield_tool, bool _draw_crosshair) { v2u32 screensize = device->getVideoDriver()->getScreenSize(); @@ -46,7 +46,6 @@ void RenderingCore::draw(video::SColor _skycolor, bool _show_hud, bool _show_min context.draw_crosshair = _draw_crosshair; context.draw_wield_tool = _draw_wield_tool; context.show_hud = _show_hud; - context.show_minimap = _show_minimap; pipeline->reset(context); pipeline->run(context); @@ -55,4 +54,4 @@ void RenderingCore::draw(video::SColor _skycolor, bool _show_hud, bool _show_min v2u32 RenderingCore::getVirtualSize() const { return virtual_size; -} \ No newline at end of file +} diff --git a/src/client/render/core.h b/src/client/render/core.h index 2d9e41047..c5617bcb2 100644 --- a/src/client/render/core.h +++ b/src/client/render/core.h @@ -53,7 +53,7 @@ public: RenderingCore &operator=(const RenderingCore &) = delete; RenderingCore &operator=(RenderingCore &&) = delete; - void draw(video::SColor _skycolor, bool _show_hud, bool _show_minimap, + void draw(video::SColor _skycolor, bool _show_hud, bool _draw_wield_tool, bool _draw_crosshair); v2u32 getVirtualSize() const; diff --git a/src/client/render/pipeline.h b/src/client/render/pipeline.h index f446773ba..abb108652 100644 --- a/src/client/render/pipeline.h +++ b/src/client/render/pipeline.h @@ -46,7 +46,6 @@ struct PipelineContext v2u32 target_size; bool show_hud {true}; - bool show_minimap {true}; bool draw_wield_tool {true}; bool draw_crosshair {true}; }; diff --git a/src/client/render/plain.cpp b/src/client/render/plain.cpp index dccdaedb3..3357d9d10 100644 --- a/src/client/render/plain.cpp +++ b/src/client/render/plain.cpp @@ -64,9 +64,6 @@ void DrawHUD::run(PipelineContext &context) context.hud->drawHotbar(context.client->getEnv().getLocalPlayer()->getWieldIndex()); context.hud->drawLuaElements(context.client->getCamera()->getOffset()); context.client->getCamera()->drawNametags(); - auto mapper = context.client->getMinimap(); - if (mapper && context.show_minimap) - mapper->drawMinimap(); } context.device->getGUIEnvironment()->drawAll(); } diff --git a/src/client/renderingengine.cpp b/src/client/renderingengine.cpp index 5e82185e7..35a27b1f0 100644 --- a/src/client/renderingengine.cpp +++ b/src/client/renderingengine.cpp @@ -319,9 +319,9 @@ void RenderingEngine::finalize() } void RenderingEngine::draw_scene(video::SColor skycolor, bool show_hud, - bool show_minimap, bool draw_wield_tool, bool draw_crosshair) + bool draw_wield_tool, bool draw_crosshair) { - core->draw(skycolor, show_hud, show_minimap, draw_wield_tool, draw_crosshair); + core->draw(skycolor, show_hud, draw_wield_tool, draw_crosshair); } const VideoDriverInfo &RenderingEngine::getVideoDriverInfo(irr::video::E_DRIVER_TYPE type) diff --git a/src/client/renderingengine.h b/src/client/renderingengine.h index 7d2291172..b8293f49a 100644 --- a/src/client/renderingengine.h +++ b/src/client/renderingengine.h @@ -118,7 +118,7 @@ public: float dtime = 0, int percent = 0, bool sky = true); void draw_scene(video::SColor skycolor, bool show_hud, - bool show_minimap, bool draw_wield_tool, bool draw_crosshair); + bool draw_wield_tool, bool draw_crosshair); void initialize(Client *client, Hud *hud); void finalize(); diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index d28ff66ef..6b5e6add3 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -1281,24 +1281,15 @@ void Client::handleCommand_HudSetFlags(NetworkPacket* pkt) LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); - bool was_minimap_visible = player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE; bool was_minimap_radar_visible = player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE; player->hud_flags &= ~mask; player->hud_flags |= flags; - m_minimap_disabled_by_server = !(player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE); bool m_minimap_radar_disabled_by_server = !(player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE); // Not so satisying code to keep compatibility with old fixed mode system // --> - - // Hide minimap if it has been disabled by the server - if (m_minimap && m_minimap_disabled_by_server && was_minimap_visible) - // defers a minimap update, therefore only call it if really - // needed, by checking that minimap was visible before - m_minimap->setModeIndex(0); - // If radar has been disabled, try to find a non radar mode or fall back to 0 if (m_minimap && m_minimap_radar_disabled_by_server && was_minimap_radar_visible) { diff --git a/src/network/networkprotocol.h b/src/network/networkprotocol.h index 76d1a1170..6de00803e 100644 --- a/src/network/networkprotocol.h +++ b/src/network/networkprotocol.h @@ -222,6 +222,7 @@ with this program; if not, write to the Free Software Foundation, Inc., PROTOCOL VERSION 44: AO_CMD_SET_BONE_POSITION extended Add TOCLIENT_MOVE_PLAYER_REL + Move default minimap from client-side C++ to server-side builtin Lua [scheduled bump for 5.9.0] */ diff --git a/src/script/lua_api/l_minimap.cpp b/src/script/lua_api/l_minimap.cpp index d70063c51..36384b201 100644 --- a/src/script/lua_api/l_minimap.cpp +++ b/src/script/lua_api/l_minimap.cpp @@ -144,7 +144,6 @@ int LuaMinimap::l_show(lua_State *L) if (m->getModeIndex() == 0 && m->getMaxModeIndex() > 0) m->setModeIndex(1); - client->showMinimap(true); return 1; } @@ -162,7 +161,6 @@ int LuaMinimap::l_hide(lua_State *L) if (m->getModeIndex() != 0) m->setModeIndex(0); - client->showMinimap(false); return 1; } diff --git a/src/unittest/test_gameui.cpp b/src/unittest/test_gameui.cpp index a6d853f19..7170031ba 100644 --- a/src/unittest/test_gameui.cpp +++ b/src/unittest/test_gameui.cpp @@ -30,7 +30,6 @@ public: void runTests(IGameDef *gamedef); void testInit(); - void testFlagSetters(); void testInfoText(); void testStatusText(); }; @@ -40,7 +39,6 @@ static TestGameUI g_test_instance; void TestGameUI::runTests(IGameDef *gamedef) { TEST(testInit); - TEST(testFlagSetters); TEST(testInfoText); TEST(testStatusText); } @@ -51,30 +49,18 @@ void TestGameUI::testInit() // Ensure flags on GameUI init UASSERT(gui.getFlags().show_chat) UASSERT(gui.getFlags().show_hud) - UASSERT(!gui.getFlags().show_minimap) UASSERT(!gui.getFlags().show_profiler_graph) // And after the initFlags init stage gui.initFlags(); UASSERT(gui.getFlags().show_chat) UASSERT(gui.getFlags().show_hud) - UASSERT(!gui.getFlags().show_minimap) UASSERT(!gui.getFlags().show_profiler_graph) // @TODO verify if we can create non UI nulldevice to test this function // gui.init(); } -void TestGameUI::testFlagSetters() -{ - GameUI gui{}; - gui.showMinimap(true); - UASSERT(gui.getFlags().show_minimap); - - gui.showMinimap(false); - UASSERT(!gui.getFlags().show_minimap); -} - void TestGameUI::testStatusText() { GameUI gui{};