aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_object.cpp
diff options
context:
space:
mode:
authorPierre-Yves Rollo <dev@pyrollo.com>2020-10-04 15:24:29 +0200
committerSmallJoker <mk939@ymail.com>2020-10-04 15:24:34 +0200
commit81c66d6efb9fb0ab8a03b40e2bc22aa49eff9a04 (patch)
tree0f1c256eb6f24eb95df60804d9db82cead0f91e2 /src/script/lua_api/l_object.cpp
parent3068853e8a58ccc7370a5ce977c08223601c497a (diff)
downloadminetest-81c66d6efb9fb0ab8a03b40e2bc22aa49eff9a04.tar.gz
minetest-81c66d6efb9fb0ab8a03b40e2bc22aa49eff9a04.tar.bz2
minetest-81c66d6efb9fb0ab8a03b40e2bc22aa49eff9a04.zip
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
Diffstat (limited to 'src/script/lua_api/l_object.cpp')
-rw-r--r--src/script/lua_api/l_object.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp
index fead4e849..57ed7e2cd 100644
--- a/src/script/lua_api/l_object.cpp
+++ b/src/script/lua_api/l_object.cpp
@@ -2199,6 +2199,67 @@ int ObjectRef::l_get_day_night_ratio(lua_State *L)
return 1;
}
+// set_minimap_modes(self, modes, modeindex)
+int ObjectRef::l_set_minimap_modes(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ // Arg 1 should be a player
+ ObjectRef *ref = checkobject(L, 1);
+ RemotePlayer *player = getplayer(ref);
+ if (player == NULL)
+ return 0;
+
+ // Arg 2 should be a table (of tables)
+ if (!lua_istable(L, 2)) {
+ return 0;
+ }
+
+ // Arg 3 should be a number
+ s16 wantedmode = lua_tonumber(L, 3);
+
+ std::vector<MinimapMode> modes;
+
+ lua_pushnil(L);
+ while (lua_next(L, 2) != 0) {
+ /* key is at index -2, value is at index -1 */
+ if (lua_istable(L, -1)) {
+ bool ok = true;
+ MinimapMode mode;
+ std::string type = getstringfield_default(L, -1, "type", "");
+ if (type == "off")
+ mode.type = MINIMAP_TYPE_OFF;
+ else if (type == "surface")
+ mode.type = MINIMAP_TYPE_SURFACE;
+ else if (type == "radar")
+ mode.type = MINIMAP_TYPE_RADAR;
+ else if (type == "texture") {
+ mode.type = MINIMAP_TYPE_TEXTURE;
+ mode.texture = getstringfield_default(L, -1, "texture", "");
+ mode.scale = getintfield_default(L, -1, "scale", 1);
+ } else {
+ warningstream << "Minimap mode of unknown type \"" << type.c_str()
+ << "\" ignored.\n" << std::endl;
+ ok = false;
+ }
+
+ if (ok) {
+ mode.label = getstringfield_default(L, -1, "label", "");
+ // Size is limited to 512. Performance gets poor if size too large, and
+ // segfaults have been experienced.
+ mode.size = rangelim(getintfield_default(L, -1, "size", 0), 1, 512);
+ modes.push_back(mode);
+ }
+ }
+ /* removes 'value'; keeps 'key' for next iteration */
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 1); // Remove key
+
+ getServer(L)->SendMinimapModes(player->getPeerId(), modes, wantedmode);
+ return 0;
+}
+
ObjectRef::ObjectRef(ServerActiveObject *object):
m_object(object)
{
@@ -2359,5 +2420,6 @@ luaL_Reg ObjectRef::methods[] = {
luamethod(ObjectRef, set_eye_offset),
luamethod(ObjectRef, get_eye_offset),
luamethod(ObjectRef, send_mapblock),
+ luamethod(ObjectRef, set_minimap_modes),
{0,0}
};