From 7af69982e343ffed1693d589f6df244be8db3414 Mon Sep 17 00:00:00 2001 From: cx384 Date: Mon, 4 Dec 2023 10:05:50 +0100 Subject: [PATCH 01/14] make hud_elem_type deprecated for type --- builtin/game/statbars.lua | 6 +++--- clientmods/preview/init.lua | 2 +- doc/client_lua_api.md | 4 +++- doc/lua_api.md | 7 +++++-- games/devtest/mods/testentities/selectionbox.lua | 2 +- games/devtest/mods/testhud/init.lua | 10 +++++----- src/script/common/c_content.cpp | 14 ++++++++++++-- 7 files changed, 30 insertions(+), 15 deletions(-) diff --git a/builtin/game/statbars.lua b/builtin/game/statbars.lua index 78d1d2728a172..42ae379105957 100644 --- a/builtin/game/statbars.lua +++ b/builtin/game/statbars.lua @@ -3,7 +3,7 @@ local enable_damage = core.settings:get_bool("enable_damage") local bar_definitions = { hp = { - hud_elem_type = "statbar", + type = "statbar", position = {x = 0.5, y = 1}, text = "heart.png", text2 = "heart_gone.png", @@ -14,7 +14,7 @@ local bar_definitions = { offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 16)}, }, breath = { - hud_elem_type = "statbar", + type = "statbar", position = {x = 0.5, y = 1}, text = "bubble.png", text2 = "bubble_gone.png", @@ -139,7 +139,7 @@ end function core.hud_replace_builtin(hud_name, definition) if type(definition) ~= "table" or - definition.hud_elem_type ~= "statbar" then + definition.type ~= "statbar" then return false end diff --git a/clientmods/preview/init.lua b/clientmods/preview/init.lua index 46d59ec9ad2f6..f8d3a006b729f 100644 --- a/clientmods/preview/init.lua +++ b/clientmods/preview/init.lua @@ -33,7 +33,7 @@ end) core.after(1, function() print("armor: " .. dump(core.localplayer:get_armor_groups())) id = core.localplayer:hud_add({ - hud_elem_type = "text", + type = "text", name = "example", number = 0xff0000, position = {x=0, y=1}, diff --git a/doc/client_lua_api.md b/doc/client_lua_api.md index 30c6d1f82e384..82d3559039a27 100644 --- a/doc/client_lua_api.md +++ b/doc/client_lua_api.md @@ -1332,8 +1332,10 @@ It can be created via `Raycast(pos1, pos2, objects, liquids)` or ```lua { - hud_elem_type = "image", -- see HUD element types, default "text" + type = "image", -- see HUD element types, default "text" -- ^ type of HUD element, can be either of "image", "text", "statbar", or "inventory" + hud_elem_type = "image", +-- ^ Deprecated, same as "type" position = {x=0.5, y=0.5}, -- ^ Left corner position of element, default `{x=0,y=0}`. name = "", -- default "" diff --git a/doc/lua_api.md b/doc/lua_api.md index ab4ff52cc9eb5..cb52dea2bd92a 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -7791,7 +7791,7 @@ child will follow movement and rotation of that bone. * `hud_change(id, stat, value)`: change a value of a previously added HUD element. * `stat` supports the same keys as in the hud definition table except for - `"hud_elem_type"`. + `"type"` (and deprecated `"hud_elem_type"`). * `hud_get(id)`: gets the HUD element definition structure of the specified ID * `hud_set_flags(flags)`: sets specified HUD flags of player. * `flags`: A table with the following fields set to boolean values @@ -10051,9 +10051,12 @@ Used by `ObjectRef:hud_add`. Returned by `ObjectRef:hud_get`. ```lua { - hud_elem_type = "image", + type = "image", -- Type of element, can be "image", "text", "statbar", "inventory", -- "waypoint", "image_waypoint", "compass" or "minimap" + + hud_elem_type = "image", + -- Deprecated, same as "type" position = {x=0.5, y=0.5}, -- Top left corner position of element diff --git a/games/devtest/mods/testentities/selectionbox.lua b/games/devtest/mods/testentities/selectionbox.lua index 36a8daa41e85a..a455fd459c875 100644 --- a/games/devtest/mods/testentities/selectionbox.lua +++ b/games/devtest/mods/testentities/selectionbox.lua @@ -66,7 +66,7 @@ minetest.register_globalstep(function() local ent = pointed_thing.ref:get_luaentity() if ent and ent.name == "testentities:selectionbox" then hud_ids[pname] = hud_id or player:hud_add({ - hud_elem_type = "text", -- See HUD element types + type = "text", -- See HUD element types position = {x=0.5, y=0.5}, text = "X", number = 0xFF0000, diff --git a/games/devtest/mods/testhud/init.lua b/games/devtest/mods/testhud/init.lua index 6b55b924b177e..da07f17e4811c 100644 --- a/games/devtest/mods/testhud/init.lua +++ b/games/devtest/mods/testhud/init.lua @@ -12,7 +12,7 @@ local font_states = { local font_default_def = { - hud_elem_type = "text", + type = "text", position = {x = 0.5, y = 0.5}, scale = {x = 2, y = 2}, alignment = { x = 0, y = 0 }, @@ -102,14 +102,14 @@ minetest.register_chatcommand("hudwaypoints", { return false end local regular = player:hud_add { - hud_elem_type = "waypoint", + type = "waypoint", name = "regular waypoint", text = "m", number = 0xFFFFFF, world_pos = vector.add(player:get_pos(), {x = 0, y = 1.5, z = 0}) } local reduced_precision = player:hud_add { - hud_elem_type = "waypoint", + type = "waypoint", name = "imprecise waypoint", text = "m (0.1 steps, precision = 10)", precision = 10, @@ -117,7 +117,7 @@ minetest.register_chatcommand("hudwaypoints", { world_pos = vector.add(player:get_pos(), {x = 0, y = 1, z = 0}) } local hidden_distance = player:hud_add { - hud_elem_type = "waypoint", + type = "waypoint", name = "waypoint with hidden distance", text = "this text is hidden as well (precision = 0)", precision = 0, @@ -149,7 +149,7 @@ minetest.register_chatcommand("hudwaypoints", { minetest.after(0.5, change, player) end local image_waypoint = player:hud_add { - hud_elem_type = "image_waypoint", + type = "image_waypoint", text = "testhud_waypoint.png", world_pos = player:get_pos(), scale = {x = 3, y = 3}, diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 1756c49c5c977..5c3a120a5ee1f 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1959,8 +1959,18 @@ void push_objectRef(lua_State *L, const u16 id) void read_hud_element(lua_State *L, HudElement *elem) { - elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type", - es_HudElementType, HUD_ELEM_TEXT); + // Handle deprecated hud_elem_type + std::string type_string; + if (getstringfield(L, 2, "hud_elem_type", type_string)) { + log_deprecated(L, "Deprecated \"hud_elem_type\" field, use \"type\" instead."); + int type_enum; + if (string_to_enum(es_HudElementType, type_enum, type_string)) + elem->type = (HudElementType)type_enum; + else + elem->type = HUD_ELEM_TEXT; + } else { + elem->type = (HudElementType)getenumfield(L, 2, "type", es_HudElementType, HUD_ELEM_TEXT); + } lua_getfield(L, 2, "position"); elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f(); From 4c17de542de03d44443e223732f7f74f9528d201 Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 7 Dec 2023 13:50:22 +0100 Subject: [PATCH 02/14] use script_log_unique --- src/script/common/c_content.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 5c3a120a5ee1f..31f132faaf7ba 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1962,7 +1962,8 @@ void read_hud_element(lua_State *L, HudElement *elem) // Handle deprecated hud_elem_type std::string type_string; if (getstringfield(L, 2, "hud_elem_type", type_string)) { - log_deprecated(L, "Deprecated \"hud_elem_type\" field, use \"type\" instead."); + script_log_unique(L, "Deprecated \"hud_elem_type\" field, use \"type\" instead.", + warningstream); int type_enum; if (string_to_enum(es_HudElementType, type_enum, type_string)) elem->type = (HudElementType)type_enum; From 900c290ed46acb1c75209e83c55941a49b8844ec Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 7 Dec 2023 14:28:15 +0100 Subject: [PATCH 03/14] throw error when ambiguous HUD element type --- src/script/common/c_content.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 31f132faaf7ba..4d336b399d98d 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1959,19 +1959,28 @@ void push_objectRef(lua_State *L, const u16 id) void read_hud_element(lua_State *L, HudElement *elem) { - // Handle deprecated hud_elem_type std::string type_string; - if (getstringfield(L, 2, "hud_elem_type", type_string)) { + bool has_type = false; + if (getstringfield(L, 2, "type", type_string)) { + has_type = true; + } + + // Handle deprecated hud_elem_type + std::string deprecated_type_string; + if (getstringfield(L, 2, "hud_elem_type", deprecated_type_string)) { + if (has_type && deprecated_type_string != type_string) + throw LuaError("Ambiguous HUD element fields: \"type\", \"hud_elem_type\"."); + has_type = true; + type_string = deprecated_type_string; script_log_unique(L, "Deprecated \"hud_elem_type\" field, use \"type\" instead.", warningstream); - int type_enum; - if (string_to_enum(es_HudElementType, type_enum, type_string)) - elem->type = (HudElementType)type_enum; - else - elem->type = HUD_ELEM_TEXT; - } else { - elem->type = (HudElementType)getenumfield(L, 2, "type", es_HudElementType, HUD_ELEM_TEXT); } + + int type_enum; + if (has_type && string_to_enum(es_HudElementType, type_enum, type_string)) + elem->type = (HudElementType)type_enum; + else + elem->type = HUD_ELEM_TEXT; lua_getfield(L, 2, "position"); elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f(); From 0ee164660ac251cbfe97b1dd746fd95d1d93cef9 Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 7 Dec 2023 14:50:32 +0100 Subject: [PATCH 04/14] replace error by warning --- src/script/common/c_content.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 4d336b399d98d..60e7509afbb56 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1968,12 +1968,15 @@ void read_hud_element(lua_State *L, HudElement *elem) // Handle deprecated hud_elem_type std::string deprecated_type_string; if (getstringfield(L, 2, "hud_elem_type", deprecated_type_string)) { - if (has_type && deprecated_type_string != type_string) - throw LuaError("Ambiguous HUD element fields: \"type\", \"hud_elem_type\"."); - has_type = true; - type_string = deprecated_type_string; - script_log_unique(L, "Deprecated \"hud_elem_type\" field, use \"type\" instead.", - warningstream); + if (has_type && deprecated_type_string != type_string) { + script_log_unique(L, "Ambiguous HUD element fields \"type\" and \"hud_elem_type\", " + "\"type\" will be used.", warningstream); + } else { + has_type = true; + type_string = deprecated_type_string; + script_log_unique(L, "Deprecated \"hud_elem_type\" field, use \"type\" instead.", + warningstream); + } } int type_enum; From 5051a4d5506a922e4670dfd040492123a81b0d67 Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 7 Dec 2023 14:54:27 +0100 Subject: [PATCH 05/14] fix if --- src/script/common/c_content.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 60e7509afbb56..010a2175797c0 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1960,10 +1960,7 @@ void push_objectRef(lua_State *L, const u16 id) void read_hud_element(lua_State *L, HudElement *elem) { std::string type_string; - bool has_type = false; - if (getstringfield(L, 2, "type", type_string)) { - has_type = true; - } + bool has_type = getstringfield(L, 2, "type", type_string); // Handle deprecated hud_elem_type std::string deprecated_type_string; From b0bb44d82d41f52c5ed4263a7f6c03223431431f Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 7 Dec 2023 15:07:21 +0100 Subject: [PATCH 06/14] improve documentation --- doc/client_lua_api.md | 3 ++- doc/lua_api.md | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/client_lua_api.md b/doc/client_lua_api.md index 82d3559039a27..10c055c337b49 100644 --- a/doc/client_lua_api.md +++ b/doc/client_lua_api.md @@ -1334,8 +1334,9 @@ It can be created via `Raycast(pos1, pos2, objects, liquids)` or { type = "image", -- see HUD element types, default "text" -- ^ type of HUD element, can be either of "image", "text", "statbar", or "inventory" +-- If undefined "text" will be used. hud_elem_type = "image", --- ^ Deprecated, same as "type" +-- ^ Deprecated, same as `type`. In case both are specified `type` will be used. position = {x=0.5, y=0.5}, -- ^ Left corner position of element, default `{x=0,y=0}`. name = "", -- default "" diff --git a/doc/lua_api.md b/doc/lua_api.md index cb52dea2bd92a..e8b7b4351bcab 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -10054,9 +10054,11 @@ Used by `ObjectRef:hud_add`. Returned by `ObjectRef:hud_get`. type = "image", -- Type of element, can be "image", "text", "statbar", "inventory", -- "waypoint", "image_waypoint", "compass" or "minimap" + -- If undefined "text" will be used. hud_elem_type = "image", - -- Deprecated, same as "type" + -- Deprecated, same as `type`. + -- In case both are specified `type` will be used. position = {x=0.5, y=0.5}, -- Top left corner position of element From 24464aefea352ba22638ff77db0673c19db79d26 Mon Sep 17 00:00:00 2001 From: cx384 Date: Wed, 13 Dec 2023 18:04:44 +0100 Subject: [PATCH 07/14] Add flag to minetest.features --- builtin/game/features.lua | 1 + doc/lua_api.md | 2 ++ 2 files changed, 3 insertions(+) diff --git a/builtin/game/features.lua b/builtin/game/features.lua index c76566ed359cf..f438743b787e1 100644 --- a/builtin/game/features.lua +++ b/builtin/game/features.lua @@ -29,6 +29,7 @@ core.features = { compress_zstd = true, sound_params_start_time = true, physics_overrides_v2 = true, + hud_def_type_field = true, } function core.has_feature(arg) diff --git a/doc/lua_api.md b/doc/lua_api.md index e8b7b4351bcab..f6cc4447081dd 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5278,6 +5278,8 @@ Utilities -- liquid_fluidity, liquid_fluidity_smooth, liquid_sink, -- acceleration_default, acceleration_air (5.8.0) physics_overrides_v2 = true, + -- In HUD definitions the field `type` is used and `hud_elem_type` is deprecated + hud_def_type_field = true, } ``` From 43522cf17d924dd3cb4f3583ef23a42a3de92116 Mon Sep 17 00:00:00 2001 From: cx384 Date: Wed, 13 Dec 2023 18:22:42 +0100 Subject: [PATCH 08/14] Fix redundancy in client API reference --- doc/client_lua_api.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/client_lua_api.md b/doc/client_lua_api.md index 10c055c337b49..ef0d9e1d3e282 100644 --- a/doc/client_lua_api.md +++ b/doc/client_lua_api.md @@ -1334,7 +1334,6 @@ It can be created via `Raycast(pos1, pos2, objects, liquids)` or { type = "image", -- see HUD element types, default "text" -- ^ type of HUD element, can be either of "image", "text", "statbar", or "inventory" --- If undefined "text" will be used. hud_elem_type = "image", -- ^ Deprecated, same as `type`. In case both are specified `type` will be used. position = {x=0.5, y=0.5}, From 451b541dc8b7d7ddb3af85ea20fab4a48096f7d1 Mon Sep 17 00:00:00 2001 From: cx384 Date: Mon, 25 Dec 2023 00:57:05 +0100 Subject: [PATCH 09/14] Add hud_elem_type check for hud_replace_builtin --- builtin/game/statbars.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/builtin/game/statbars.lua b/builtin/game/statbars.lua index 42ae379105957..ea7f39ca80105 100644 --- a/builtin/game/statbars.lua +++ b/builtin/game/statbars.lua @@ -139,7 +139,8 @@ end function core.hud_replace_builtin(hud_name, definition) if type(definition) ~= "table" or - definition.type ~= "statbar" then + (definition.hud_elem_type ~= "statbar" and + definition.type ~= "statbar") then return false end From a414b913b8a54ccd5c7165e632da46ef681a08eb Mon Sep 17 00:00:00 2001 From: cx384 Date: Mon, 25 Dec 2023 01:08:19 +0100 Subject: [PATCH 10/14] Add version information to minetest.features reference --- doc/lua_api.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index f6cc4447081dd..ec2f3250a0246 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5278,7 +5278,7 @@ Utilities -- liquid_fluidity, liquid_fluidity_smooth, liquid_sink, -- acceleration_default, acceleration_air (5.8.0) physics_overrides_v2 = true, - -- In HUD definitions the field `type` is used and `hud_elem_type` is deprecated + -- In HUD definitions the field `type` is used and `hud_elem_type` is deprecated (5.9.0) hud_def_type_field = true, } ``` @@ -10057,7 +10057,7 @@ Used by `ObjectRef:hud_add`. Returned by `ObjectRef:hud_get`. -- Type of element, can be "image", "text", "statbar", "inventory", -- "waypoint", "image_waypoint", "compass" or "minimap" -- If undefined "text" will be used. - + hud_elem_type = "image", -- Deprecated, same as `type`. -- In case both are specified `type` will be used. From b9f5a7b90a2e5cc22eba13058828087c4ef3cee9 Mon Sep 17 00:00:00 2001 From: cx384 Date: Mon, 25 Dec 2023 01:11:47 +0100 Subject: [PATCH 11/14] Use static_cast --- src/script/common/c_content.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 010a2175797c0..eed6dd929191a 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1961,7 +1961,7 @@ void read_hud_element(lua_State *L, HudElement *elem) { std::string type_string; bool has_type = getstringfield(L, 2, "type", type_string); - + // Handle deprecated hud_elem_type std::string deprecated_type_string; if (getstringfield(L, 2, "hud_elem_type", deprecated_type_string)) { @@ -1975,10 +1975,10 @@ void read_hud_element(lua_State *L, HudElement *elem) warningstream); } } - + int type_enum; if (has_type && string_to_enum(es_HudElementType, type_enum, type_string)) - elem->type = (HudElementType)type_enum; + elem->type = static_cast(type_enum); else elem->type = HUD_ELEM_TEXT; From 57d10dac2b7626c7532fc1218c115919c2ccac48 Mon Sep 17 00:00:00 2001 From: cx384 Date: Mon, 25 Dec 2023 16:12:02 +0100 Subject: [PATCH 12/14] Improve lua_api.md Co-authored-by: sfan5 --- doc/lua_api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index ec2f3250a0246..f66c4da9ae688 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -7793,7 +7793,7 @@ child will follow movement and rotation of that bone. * `hud_change(id, stat, value)`: change a value of a previously added HUD element. * `stat` supports the same keys as in the hud definition table except for - `"type"` (and deprecated `"hud_elem_type"`). + `"type"` (or the deprecated `"hud_elem_type"`). * `hud_get(id)`: gets the HUD element definition structure of the specified ID * `hud_set_flags(flags)`: sets specified HUD flags of player. * `flags`: A table with the following fields set to boolean values From 298e7029a4b032d03201b33ff0a0c0396ddd89b5 Mon Sep 17 00:00:00 2001 From: cx384 Date: Mon, 25 Dec 2023 17:12:44 +0100 Subject: [PATCH 13/14] Fix hud_replace_builtin type determination --- builtin/game/statbars.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/builtin/game/statbars.lua b/builtin/game/statbars.lua index ea7f39ca80105..72b711bdc7f26 100644 --- a/builtin/game/statbars.lua +++ b/builtin/game/statbars.lua @@ -139,8 +139,7 @@ end function core.hud_replace_builtin(hud_name, definition) if type(definition) ~= "table" or - (definition.hud_elem_type ~= "statbar" and - definition.type ~= "statbar") then + (definition.type or definition.hud_elem_type) ~= "statbar" then return false end From d715b91a9cce733394298762a5b09b324d084e7f Mon Sep 17 00:00:00 2001 From: cx384 Date: Mon, 25 Dec 2023 20:14:45 +0100 Subject: [PATCH 14/14] Use log_deprecated --- src/script/common/c_content.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index eed6dd929191a..97b651ce8088a 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1966,13 +1966,13 @@ void read_hud_element(lua_State *L, HudElement *elem) std::string deprecated_type_string; if (getstringfield(L, 2, "hud_elem_type", deprecated_type_string)) { if (has_type && deprecated_type_string != type_string) { - script_log_unique(L, "Ambiguous HUD element fields \"type\" and \"hud_elem_type\", " - "\"type\" will be used.", warningstream); + log_deprecated(L, "Ambiguous HUD element fields \"type\" and \"hud_elem_type\", " + "\"type\" will be used.", 1, true); } else { has_type = true; type_string = deprecated_type_string; - script_log_unique(L, "Deprecated \"hud_elem_type\" field, use \"type\" instead.", - warningstream); + log_deprecated(L, "Deprecated \"hud_elem_type\" field, use \"type\" instead.", + 1, true); } }