Skip to content

Commit 81c66d6

Browse files
pyrolloSmallJoker
authored andcommittedOct 4, 2020
Minimap as HUD element with API control
Features: * Define Minimap available modes (surface/radar, scale) from Lua, using player:set_minimap_modes() * New HUD elements for displaying minimap with custom size and placing * New minimap mode for displaying a texture instead of the map
1 parent 3068853 commit 81c66d6

20 files changed

+471
-145
lines changed
 

Diff for: ‎doc/lua_api.txt

+32-1
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,15 @@ Displays an image oriented or translated according to current heading direction.
14841484

14851485
If translation is chosen, texture is repeated horizontally to fill the whole element.
14861486

1487+
### `minimap`
1488+
1489+
Displays a minimap on the HUD.
1490+
1491+
* `size`: Size of the minimap to display. Minimap should be a square to avoid
1492+
distortion.
1493+
* `alignment`: The alignment of the minimap.
1494+
* `offset`: offset in pixels from position.
1495+
14871496
Representations of simple things
14881497
================================
14891498

@@ -6366,6 +6375,27 @@ object you are working with still exists.
63666375
* `hud_set_hotbar_selected_image(texturename)`
63676376
* sets image for selected item of hotbar
63686377
* `hud_get_hotbar_selected_image`: returns texturename
6378+
* `set_minimap_modes({mode, mode, ...}, selected_mode)`
6379+
* Overrides the available minimap modes (and toggle order), and changes the
6380+
selected mode.
6381+
* `mode` is a table consisting of up to four fields:
6382+
* `type`: Available type:
6383+
* `off`: Minimap off
6384+
* `surface`: Minimap in surface mode
6385+
* `radar`: Minimap in radar mode
6386+
* `texture`: Texture to be displayed instead of terrain map
6387+
(texture is centered around 0,0 and can be scaled).
6388+
Texture size is limited to 512 x 512 pixel.
6389+
* `label`: Optional label to display on minimap mode toggle
6390+
The translation must be handled within the mod.
6391+
* `size`: Sidelength or diameter, in number of nodes, of the terrain
6392+
displayed in minimap
6393+
* `texture`: Only for texture type, name of the texture to display
6394+
* `scale`: Only for texture type, scale of the texture map in nodes per
6395+
pixel (for example a `scale` of 2 means each pixel represents a 2x2
6396+
nodes square)
6397+
* `selected_mode` is the mode index to be selected after modes have been changed
6398+
(0 is the first mode).
63696399
* `set_sky(parameters)`
63706400
* `parameters` is a table with the following optional fields:
63716401
* `base_color`: ColorSpec, changes fog in "skybox" and "plain".
@@ -8047,7 +8077,8 @@ Used by `Player:hud_add`. Returned by `Player:hud_get`.
80478077

80488078
{
80498079
hud_elem_type = "image", -- See HUD element types
8050-
-- Type of element, can be "image", "text", "statbar", "inventory" or "compass"
8080+
-- Type of element, can be "image", "text", "statbar", "inventory",
8081+
-- "compass" or "minimap"
80518082

80528083
position = {x=0.5, y=0.5},
80538084
-- Left corner position of element

Diff for: ‎src/client/client.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Client::Client(
129129
if (g_settings->getBool("enable_minimap")) {
130130
m_minimap = new Minimap(this);
131131
}
132+
132133
m_cache_save_interval = g_settings->getU16("server_map_save_interval");
133134
}
134135

Diff for: ‎src/client/client.h

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
223223
void handleCommand_CSMRestrictionFlags(NetworkPacket *pkt);
224224
void handleCommand_PlayerSpeed(NetworkPacket *pkt);
225225
void handleCommand_MediaPush(NetworkPacket *pkt);
226+
void handleCommand_MinimapModes(NetworkPacket *pkt);
226227

227228
void ProcessData(NetworkPacket *pkt);
228229

Diff for: ‎src/client/game.cpp

+29-46
Original file line numberDiff line numberDiff line change
@@ -1416,11 +1416,9 @@ bool Game::createClient(const GameStartData &start_data)
14161416
}
14171417

14181418
mapper = client->getMinimap();
1419-
if (mapper) {
1420-
mapper->setMinimapMode(MINIMAP_MODE_OFF);
1421-
if (client->modsLoaded())
1422-
client->getScript()->on_minimap_ready(mapper);
1423-
}
1419+
1420+
if (mapper && client->modsLoaded())
1421+
client->getScript()->on_minimap_ready(mapper);
14241422

14251423
return true;
14261424
}
@@ -2222,52 +2220,37 @@ void Game::toggleMinimap(bool shift_pressed)
22222220
if (!mapper || !m_game_ui->m_flags.show_hud || !g_settings->getBool("enable_minimap"))
22232221
return;
22242222

2225-
if (shift_pressed) {
2223+
if (shift_pressed)
22262224
mapper->toggleMinimapShape();
2227-
return;
2228-
}
2225+
else
2226+
mapper->nextMode();
2227+
2228+
// TODO: When legacy minimap is deprecated, keep only HUD minimap stuff here
22292229

2230+
// Not so satisying code to keep compatibility with old fixed mode system
2231+
// -->
22302232
u32 hud_flags = client->getEnv().getLocalPlayer()->hud_flags;
22312233

2232-
MinimapMode mode = MINIMAP_MODE_OFF;
2233-
if (hud_flags & HUD_FLAG_MINIMAP_VISIBLE) {
2234-
mode = mapper->getMinimapMode();
2235-
mode = (MinimapMode)((int)mode + 1);
2236-
// If radar is disabled and in, or switching to, radar mode
2237-
if (!(hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE) && mode > 3)
2238-
mode = MINIMAP_MODE_OFF;
2239-
}
2234+
if (!(hud_flags & HUD_FLAG_MINIMAP_VISIBLE)) {
2235+
m_game_ui->m_flags.show_minimap = false;
2236+
} else {
22402237

2241-
m_game_ui->m_flags.show_minimap = true;
2242-
switch (mode) {
2243-
case MINIMAP_MODE_SURFACEx1:
2244-
m_game_ui->showTranslatedStatusText("Minimap in surface mode, Zoom x1");
2245-
break;
2246-
case MINIMAP_MODE_SURFACEx2:
2247-
m_game_ui->showTranslatedStatusText("Minimap in surface mode, Zoom x2");
2248-
break;
2249-
case MINIMAP_MODE_SURFACEx4:
2250-
m_game_ui->showTranslatedStatusText("Minimap in surface mode, Zoom x4");
2251-
break;
2252-
case MINIMAP_MODE_RADARx1:
2253-
m_game_ui->showTranslatedStatusText("Minimap in radar mode, Zoom x1");
2254-
break;
2255-
case MINIMAP_MODE_RADARx2:
2256-
m_game_ui->showTranslatedStatusText("Minimap in radar mode, Zoom x2");
2257-
break;
2258-
case MINIMAP_MODE_RADARx4:
2259-
m_game_ui->showTranslatedStatusText("Minimap in radar mode, Zoom x4");
2260-
break;
2261-
default:
2262-
mode = MINIMAP_MODE_OFF;
2263-
m_game_ui->m_flags.show_minimap = false;
2264-
if (hud_flags & HUD_FLAG_MINIMAP_VISIBLE)
2265-
m_game_ui->showTranslatedStatusText("Minimap hidden");
2266-
else
2267-
m_game_ui->showTranslatedStatusText("Minimap currently disabled by game or mod");
2268-
}
2238+
// If radar is disabled, try to find a non radar mode or fall back to 0
2239+
if (!(hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE))
2240+
while (mapper->getModeIndex() &&
2241+
mapper->getModeDef().type == MINIMAP_TYPE_RADAR)
2242+
mapper->nextMode();
22692243

2270-
mapper->setMinimapMode(mode);
2244+
m_game_ui->m_flags.show_minimap = mapper->getModeDef().type !=
2245+
MINIMAP_TYPE_OFF;
2246+
}
2247+
// <--
2248+
// End of 'not so satifying code'
2249+
if ((hud_flags & HUD_FLAG_MINIMAP_VISIBLE) ||
2250+
(hud && hud->hasElementOfType(HUD_ELEM_MINIMAP)))
2251+
m_game_ui->showStatusText(utf8_to_wide(mapper->getModeDef().label));
2252+
else
2253+
m_game_ui->showTranslatedStatusText("Minimap currently disabled by game or mod");
22712254
}
22722255

22732256
void Game::toggleFog()
@@ -3954,7 +3937,7 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
39543937
/*
39553938
Update minimap pos and rotation
39563939
*/
3957-
if (mapper && m_game_ui->m_flags.show_minimap && m_game_ui->m_flags.show_hud) {
3940+
if (mapper && m_game_ui->m_flags.show_hud) {
39583941
mapper->setPos(floatToInt(player->getPosition(), BS));
39593942
mapper->setAngle(player->getYaw());
39603943
}

Diff for: ‎src/client/hud.cpp

+29-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3636
#include "mesh.h"
3737
#include "wieldmesh.h"
3838
#include "client/renderingengine.h"
39+
#include "client/minimap.h"
3940

4041
#ifdef HAVE_TOUCHSCREENGUI
4142
#include "gui/touchscreengui.h"
@@ -297,6 +298,18 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
297298
}
298299
}
299300

301+
bool Hud::hasElementOfType(HudElementType type)
302+
{
303+
for (size_t i = 0; i != player->maxHudId(); i++) {
304+
HudElement *e = player->getHud(i);
305+
if (!e)
306+
continue;
307+
if (e->type == type)
308+
return true;
309+
}
310+
return false;
311+
}
312+
300313
// Calculates screen position of waypoint. Returns true if waypoint is visible (in front of the player), else false.
301314
bool Hud::calculateScreenPos(const v3s16 &camera_offset, HudElement *e, v2s32 *pos)
302315
{
@@ -491,7 +504,22 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
491504
default:
492505
break;
493506
}
494-
507+
break; }
508+
case HUD_ELEM_MINIMAP: {
509+
if (e->size.X <= 0 || e->size.Y <= 0)
510+
break;
511+
if (!client->getMinimap())
512+
break;
513+
// Draw a minimap of size "size"
514+
v2s32 dstsize(e->size.X * m_scale_factor,
515+
e->size.Y * m_scale_factor);
516+
// (no percent size as minimap would likely be anamorphosed)
517+
v2s32 offset((e->align.X - 1.0) * dstsize.X / 2,
518+
(e->align.Y - 1.0) * dstsize.Y / 2);
519+
core::rect<s32> rect(0, 0, dstsize.X, dstsize.Y);
520+
rect += pos + offset + v2s32(e->offset.X * m_scale_factor,
521+
e->offset.Y * m_scale_factor);
522+
client->getMinimap()->drawMinimap(rect);
495523
break; }
496524
default:
497525
infostream << "Hud::drawLuaElements: ignoring drawform " << e->type <<

Diff for: ‎src/client/hud.h

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class Hud
8181
m_selected_face_normal = face_normal;
8282
}
8383

84+
bool hasElementOfType(HudElementType type);
85+
8486
void drawLuaElements(const v3s16 &camera_offset);
8587

8688
private:

Diff for: ‎src/client/mapblock_mesh.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
10441044
m_use_tangent_vertices = data->m_use_tangent_vertices;
10451045
m_enable_vbo = g_settings->getBool("enable_vbo");
10461046

1047-
if (g_settings->getBool("enable_minimap")) {
1047+
if (data->m_client->getMinimap()) {
10481048
m_minimap_mapblock = new MinimapMapblock;
10491049
m_minimap_mapblock->getMinimapNodes(
10501050
&data->m_vmanip, data->m_blockpos * MAP_BLOCKSIZE);

0 commit comments

Comments
 (0)
Please sign in to comment.