From be9024a397506dff1151b16e5f7a91e393b00d7e Mon Sep 17 00:00:00 2001
From: kwolekr <kwolekr@minetest.net>
Date: Thu, 13 Aug 2015 03:16:50 -0400
Subject: [PATCH] minimap: Add ability to disable from server

---
 doc/lua_api.txt                 |  6 ++-
 src/game.cpp                    | 85 +++++++++++++++++++--------------
 src/hud.h                       | 12 +++--
 src/player.cpp                  | 10 ++--
 src/script/lua_api/l_object.cpp |  3 ++
 5 files changed, 69 insertions(+), 47 deletions(-)

diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 9f435fd44..f771a360d 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -2519,11 +2519,13 @@ This is basically a reference to a C++ `ServerActiveObject`
     * element `stat` values: `position`, `name`, `scale`, `text`, `number`, `item`, `dir`
 * `hud_get(id)`: gets the HUD element definition structure of the specified ID
 * `hud_set_flags(flags)`: sets specified HUD flags to `true`/`false`
-    * `flags`: (is visible) `hotbar`, `healthbar`, `crosshair`, `wielditem`
+    * `flags`: (is visible) `hotbar`, `healthbar`, `crosshair`, `wielditem`, `minimap`
     * pass a table containing a `true`/`false` value of each flag to be set or unset
     * if a flag equals `nil`, the flag is not modified
+    * note that setting `minimap` modifies the client's permission to view the minimap -
+    * the client may locally elect to not view the minimap
 * `hud_get_flags()`: returns a table containing status of hud flags
-    * returns `{ hotbar=true, healthbar=true, crosshair=true, wielditem=true, breathbar=true }`
+    * returns `{ hotbar=true, healthbar=true, crosshair=true, wielditem=true, breathbar=true, minimap=true }`
 * `hud_set_hotbar_itemcount(count)`: sets number of items in builtin hotbar
     * `count`: number of items, must be between `1` and `23`
 * `hud_get_hotbar_itemcount`: returns number of visible items
diff --git a/src/game.cpp b/src/game.cpp
index 6655fe820..11e868a80 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -1497,7 +1497,7 @@ class Game {
 
 	void toggleChat(float *statustext_time, bool *flag);
 	void toggleHud(float *statustext_time, bool *flag);
-	void toggleMinimap(float *statustext_time, bool *flag1, bool *flag2,
+	void toggleMinimap(float *statustext_time, bool *flag, bool show_hud,
 			bool shift_pressed);
 	void toggleFog(float *statustext_time, bool *flag);
 	void toggleDebug(float *statustext_time, bool *show_debug,
@@ -2642,7 +2642,7 @@ void Game::processKeyboardInput(VolatileRunFlags *flags,
 	} else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_TOGGLE_HUD])) {
 		toggleHud(statustext_time, &flags->show_hud);
 	} else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_MINIMAP])) {
-		toggleMinimap(statustext_time, &flags->show_minimap, &flags->show_hud,
+		toggleMinimap(statustext_time, &flags->show_minimap, flags->show_hud,
 			input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_SNEAK]));
 	} else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_TOGGLE_CHAT])) {
 		toggleChat(statustext_time, &flags->show_chat);
@@ -2864,43 +2864,54 @@ void Game::toggleHud(float *statustext_time, bool *flag)
 		client->setHighlighted(client->getHighlighted(), *flag);
 }
 
-void Game::toggleMinimap(float *statustext_time, bool *flag, bool *show_hud, bool shift_pressed)
+void Game::toggleMinimap(float *statustext_time, bool *flag,
+	bool show_hud, bool shift_pressed)
 {
-	if (*show_hud && g_settings->getBool("enable_minimap")) {
-		if (shift_pressed) {
-			mapper->toggleMinimapShape();
-			return;
-		}
-		MinimapMode mode = mapper->getMinimapMode();
-		mode = (MinimapMode)((int)(mode) + 1);
-		*flag = true;
-		switch (mode) {
-			case MINIMAP_MODE_SURFACEx1:
-				statustext = L"Minimap in surface mode, Zoom x1";
-				break;
-			case MINIMAP_MODE_SURFACEx2:
-				statustext = L"Minimap in surface mode, Zoom x2";
-				break;
-			case MINIMAP_MODE_SURFACEx4:
-				statustext = L"Minimap in surface mode, Zoom x4";
-				break;
-			case MINIMAP_MODE_RADARx1:
-				statustext = L"Minimap in radar mode, Zoom x1";
-				break;
-			case MINIMAP_MODE_RADARx2:
-				statustext = L"Minimap in radar mode, Zoom x2";
-				break;
-			case MINIMAP_MODE_RADARx4:
-				statustext = L"Minimap in radar mode, Zoom x4";
-				break;
-			default:
-				mode = MINIMAP_MODE_OFF;
-				*flag = false;
-				statustext = L"Minimap hidden";
-		}
-		*statustext_time = 0;
-		mapper->setMinimapMode(mode);
+	if (!show_hud || !g_settings->getBool("enable_minimap"))
+		return;
+
+	if (shift_pressed) {
+		mapper->toggleMinimapShape();
+		return;
 	}
+
+	u32 hud_flags = client->getEnv().getLocalPlayer()->hud_flags;
+
+	MinimapMode mode = MINIMAP_MODE_OFF;
+	if (hud_flags & HUD_FLAG_MINIMAP_VISIBLE) {
+		mode = mapper->getMinimapMode();
+		mode = (MinimapMode)((int)mode + 1);
+	}
+
+	*flag = true;
+	switch (mode) {
+		case MINIMAP_MODE_SURFACEx1:
+			statustext = L"Minimap in surface mode, Zoom x1";
+			break;
+		case MINIMAP_MODE_SURFACEx2:
+			statustext = L"Minimap in surface mode, Zoom x2";
+			break;
+		case MINIMAP_MODE_SURFACEx4:
+			statustext = L"Minimap in surface mode, Zoom x4";
+			break;
+		case MINIMAP_MODE_RADARx1:
+			statustext = L"Minimap in radar mode, Zoom x1";
+			break;
+		case MINIMAP_MODE_RADARx2:
+			statustext = L"Minimap in radar mode, Zoom x2";
+			break;
+		case MINIMAP_MODE_RADARx4:
+			statustext = L"Minimap in radar mode, Zoom x4";
+			break;
+		default:
+			mode = MINIMAP_MODE_OFF;
+			*flag = false;
+			statustext = (hud_flags & HUD_FLAG_MINIMAP_VISIBLE) ?
+				L"Minimap hidden" : L"Minimap disabled by server";
+	}
+
+	*statustext_time = 0;
+	mapper->setMinimapMode(mode);
 }
 
 void Game::toggleFog(float *statustext_time, bool *flag)
diff --git a/src/hud.h b/src/hud.h
index 2e6838eb1..614e7c92d 100644
--- a/src/hud.h
+++ b/src/hud.h
@@ -32,11 +32,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #define HUD_CORNER_LOWER  1
 #define HUD_CORNER_CENTER 2
 
+// Note that these visibility flags do not determine if the hud items are
+// actually drawn, but rather, allows the item to be drawn should the rest of
+// the game state permit it.
 #define HUD_FLAG_HOTBAR_VISIBLE    (1 << 0)
 #define HUD_FLAG_HEALTHBAR_VISIBLE (1 << 1)
 #define HUD_FLAG_CROSSHAIR_VISIBLE (1 << 2)
 #define HUD_FLAG_WIELDITEM_VISIBLE (1 << 3)
 #define HUD_FLAG_BREATHBAR_VISIBLE (1 << 4)
+#define HUD_FLAG_MINIMAP_VISIBLE   (1 << 5)
 
 #define HUD_PARAM_HOTBAR_ITEMCOUNT 1
 #define HUD_PARAM_HOTBAR_IMAGE 2
@@ -116,11 +120,11 @@ class Hud {
 	std::string hotbar_selected_image;
 	bool use_hotbar_selected_image;
 	v3s16 camera_offset;
-	
+
 	Hud(video::IVideoDriver *driver,scene::ISceneManager* smgr,
 		gui::IGUIEnvironment* guienv, IGameDef *gamedef, LocalPlayer *player,
 		Inventory *inventory);
-	
+
 	void drawHotbar(u16 playeritem);
 	void resizeHotbar();
 	void drawCrosshair();
@@ -129,12 +133,12 @@ class Hud {
 private:
 	void drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture,
 			s32 count, v2s32 offset, v2s32 size=v2s32());
-	
+
 	void drawItems(v2s32 upperleftpos, s32 itemcount, s32 offset,
 		InventoryList *mainlist, u16 selectitem, u16 direction);
 
 	void drawItem(const ItemStack &item, const core::rect<s32>& rect, bool selected);
-	
+
 	v2u32 m_screensize;
 	v2s32 m_displaycenter;
 	s32 m_hotbar_imagesize;
diff --git a/src/player.cpp b/src/player.cpp
index 0e8fd86d2..cb2286ef6 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -75,7 +75,8 @@ Player::Player(IGameDef *gamedef, const char *name):
 		"listring[]"
 		"list[current_player;craftpreview;7,1;1,1;]";
 
-	// Initialize movement settings at default values, so movement can work if the server fails to send them
+	// Initialize movement settings at default values, so movement can work
+	// if the server fails to send them
 	movement_acceleration_default   = 3    * BS;
 	movement_acceleration_air       = 2    * BS;
 	movement_acceleration_fast      = 10   * BS;
@@ -97,9 +98,10 @@ Player::Player(IGameDef *gamedef, const char *name):
 	physics_override_sneak        = true;
 	physics_override_sneak_glitch = true;
 
-	hud_flags = HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
-			 HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE |
-			 HUD_FLAG_BREATHBAR_VISIBLE;
+	hud_flags =
+		HUD_FLAG_HOTBAR_VISIBLE    | HUD_FLAG_HEALTHBAR_VISIBLE |
+		HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE |
+		HUD_FLAG_BREATHBAR_VISIBLE | HUD_FLAG_MINIMAP_VISIBLE;
 
 	hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
 }
diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp
index fca69b996..3ac8eeefb 100644
--- a/src/script/lua_api/l_object.cpp
+++ b/src/script/lua_api/l_object.cpp
@@ -68,6 +68,7 @@ struct EnumString es_HudBuiltinElement[] =
 	{HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
 	{HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
 	{HUD_FLAG_BREATHBAR_VISIBLE, "breathbar"},
+	{HUD_FLAG_MINIMAP_VISIBLE,   "minimap"},
 	{0, NULL},
 };
 
@@ -1384,6 +1385,8 @@ int ObjectRef::l_hud_get_flags(lua_State *L)
 	lua_setfield(L, -2, "wielditem");
 	lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
 	lua_setfield(L, -2, "breathbar");
+	lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
+	lua_setfield(L, -2, "minimap");
 
 	return 1;
 }
-- 
GitLab