diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 5d3de7bfb..1618b757f 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -416,7 +416,9 @@ void Hud::drawLuaElements(const v3s16 &camera_offset) (e->number >> 0) & 0xFF); std::wstring text = unescape_translate(utf8_to_wide(e->name)); const std::string &unit = e->text; - // waypoints reuse the item field to store precision, item = precision + 1 + // Waypoints reuse the item field to store precision, + // item = precision + 1 and item = 0 <=> precision = 10 for backwards compatibility. + // Also see `push_hud_element`. u32 item = e->item; float precision = (item == 0) ? 10.0f : (item - 1.f); bool draw_precision = precision > 0; diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 8e499a7bd..b50e02a80 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -2025,11 +2025,16 @@ void read_hud_element(lua_State *L, HudElement *elem) elem->name = getstringfield_default(L, 2, "name", ""); elem->text = getstringfield_default(L, 2, "text", ""); elem->number = getintfield_default(L, 2, "number", 0); - if (elem->type == HUD_ELEM_WAYPOINT) - // waypoints reuse the item field to store precision, item = precision + 1 - elem->item = getintfield_default(L, 2, "precision", -1) + 1; - else + if (elem->type == HUD_ELEM_WAYPOINT) { + // Waypoints reuse the item field to store precision, + // item = precision + 1 and item = 0 <=> precision = 10 for backwards compatibility. + int precision = getintfield_default(L, 2, "precision", 10); + if (precision < 0) + throw LuaError("Waypoint precision must be non-negative"); + elem->item = precision + 1; + } else { elem->item = getintfield_default(L, 2, "item", 0); + } elem->dir = getintfield_default(L, 2, "direction", 0); elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX, getintfield_default(L, 2, "z_index", 0))); @@ -2081,8 +2086,10 @@ void push_hud_element(lua_State *L, HudElement *elem) lua_setfield(L, -2, "number"); if (elem->type == HUD_ELEM_WAYPOINT) { - // waypoints reuse the item field to store precision, precision = item - 1 - lua_pushnumber(L, elem->item - 1); + // Waypoints reuse the item field to store precision, + // item = precision + 1 and item = 0 <=> precision = 10 for backwards compatibility. + // See `Hud::drawLuaElements`, case `HUD_ELEM_WAYPOINT`. + lua_pushnumber(L, (elem->item == 0) ? 10 : (elem->item - 1)); lua_setfield(L, -2, "precision"); } // push the item field for waypoints as well for backwards compatibility