From 47719872e47c8b619bc1dbba783743a3f65adf4c Mon Sep 17 00:00:00 2001 From: kay27 Date: Tue, 1 Mar 2022 03:34:44 +0400 Subject: [PATCH 1/8] [hud] Show player biome and position --- mods/HUD/mcl_info/init.lua | 68 ++++++++++++++++++++++++++++++++++++++ mods/HUD/mcl_info/mod.conf | 3 ++ 2 files changed, 71 insertions(+) create mode 100644 mods/HUD/mcl_info/init.lua create mode 100644 mods/HUD/mcl_info/mod.conf diff --git a/mods/HUD/mcl_info/init.lua b/mods/HUD/mcl_info/init.lua new file mode 100644 index 000000000..ae0fdc526 --- /dev/null +++ b/mods/HUD/mcl_info/init.lua @@ -0,0 +1,68 @@ +local refresh_interval = .63 +local huds = {} +local after = minetest.after +local get_connected_players = minetest.get_connected_players +local get_biome_name = minetest.get_biome_name +local get_biome_data = minetest.get_biome_data +local format = string.format + +local min1, min2, min3 = mcl_mapgen.overworld.min, mcl_mapgen.end_.min, mcl_mapgen.nether.min +local max1, max2, max3 = mcl_mapgen.overworld.max, mcl_mapgen.end_.max, mcl_mapgen.nether.max + 128 + +local function get_text(pos) + local y = pos.y + if y >= min1 then + y = y - min1 + elseif y >= min3 and y <= max3 then + y = y - min3 + elseif y >= min2 and y <= max2 then + y = y - min2 + end + local biome_data = get_biome_data(pos) + local biome_name = biome_data and get_biome_name(biome_data.biome) or "No biome" + local text = format("%s x:%.1f y:%.1f z:%.1f", biome_name, pos.x, y, pos.z) + return text +end + +local function info() + for _, player in pairs(get_connected_players()) do + local name = player:get_player_name() + local pos = player:get_pos() + local text = get_text(pos) + local hud = huds[name] + if not hud then + local def = { + hud_elem_type = "text", + alignment = {x = 1, y = -1}, + scale = {x = 100, y = 100}, + position = {x = 0.0073, y = 0.989}, + text = text, + style = 5, + ["number"] = 0xcccac0, + z_index = 0, + } + local def_bg = table.copy(def) + def_bg.offset = {x = 2, y = 1} + def_bg["number"] = 0 + def_bg.z_index = -1 + huds[name] = { + player:hud_add(def), + player:hud_add(def_bg), + text, + } + elseif text ~= hud[3] then + hud[3] = text + player:hud_change(huds[name][1], "text", text) + player:hud_change(huds[name][2], "text", text) + end + end + after(refresh_interval, info) +end + +minetest.register_on_authplayer(function(name, ip, is_success) + if is_success then + huds[name] = nil + end +end) + +info() diff --git a/mods/HUD/mcl_info/mod.conf b/mods/HUD/mcl_info/mod.conf new file mode 100644 index 000000000..da3e10fff --- /dev/null +++ b/mods/HUD/mcl_info/mod.conf @@ -0,0 +1,3 @@ +name = mcl_info +description = Prints biome name and player position +optional_depends = mcl_mapgen From cb2852e88f5e8fa4f6e0873eace2a26db66e4f66 Mon Sep 17 00:00:00 2001 From: kay27 Date: Wed, 2 Mar 2022 02:47:38 +0400 Subject: [PATCH 2/8] #233 Add /debug chat command --- mods/HUD/mcl_info/init.lua | 43 +++++++++++++++++++++++-- mods/HUD/mcl_info/locale/mcl_info.ru.tr | 4 +++ mods/HUD/mcl_info/locale/template.txt | 4 +++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 mods/HUD/mcl_info/locale/mcl_info.ru.tr create mode 100644 mods/HUD/mcl_info/locale/template.txt diff --git a/mods/HUD/mcl_info/init.lua b/mods/HUD/mcl_info/init.lua index ae0fdc526..02af53fbc 100644 --- a/mods/HUD/mcl_info/init.lua +++ b/mods/HUD/mcl_info/init.lua @@ -1,5 +1,6 @@ local refresh_interval = .63 local huds = {} +local default_debug = 3 local after = minetest.after local get_connected_players = minetest.get_connected_players local get_biome_name = minetest.get_biome_name @@ -9,7 +10,15 @@ local format = string.format local min1, min2, min3 = mcl_mapgen.overworld.min, mcl_mapgen.end_.min, mcl_mapgen.nether.min local max1, max2, max3 = mcl_mapgen.overworld.max, mcl_mapgen.end_.max, mcl_mapgen.nether.max + 128 -local function get_text(pos) +local modname = minetest.get_current_modname() +local modpath = minetest.get_modpath(modname) +local S = minetest.get_translator(modname) +local storage = minetest.get_mod_storage() +local player_dbg = minetest.deserialize(storage:get_string("player_dbg") or "return {}") or {} + +local function get_text(pos, bits) + local bits = bits + if bits == 0 then return "" end local y = pos.y if y >= min1 then y = y - min1 @@ -20,7 +29,14 @@ local function get_text(pos) end local biome_data = get_biome_data(pos) local biome_name = biome_data and get_biome_name(biome_data.biome) or "No biome" - local text = format("%s x:%.1f y:%.1f z:%.1f", biome_name, pos.x, y, pos.z) + local text + if bits == 1 then + text = biome_name + elseif bits == 2 then + text = format("x:%.1f y:%.1f z:%.1f", pos.x, y, pos.z) + elseif bits == 3 then + text = format("%s x:%.1f y:%.1f z:%.1f", biome_name, pos.x, y, pos.z) + end return text end @@ -28,7 +44,7 @@ local function info() for _, player in pairs(get_connected_players()) do local name = player:get_player_name() local pos = player:get_pos() - local text = get_text(pos) + local text = get_text(pos, player_dbg[name] or default_debug) local hud = huds[name] if not hud then local def = { @@ -65,4 +81,25 @@ minetest.register_on_authplayer(function(name, ip, is_success) end end) +minetest.register_chatcommand("debug",{ + description = S("Set debug bit mask: 0 = disable, 1 = biome name, 2 = coordinates, 3 = all"), + func = function(name, params) + local dbg = math.floor(tonumber(params) or default_debug) + if dbg < 0 or dbg > 3 then + minetest.chat_send_player(name, S("Error! Possible values are integer numbers from @1 to @2", 0, 3)) + return + end + if dbg == default_dbg then + player_dbg[name] = nil + else + player_dbg[name] = dbg + end + minetest.chat_send_player(name, S("Debug bit mask set to @1", dbg)) + end +}) + +minetest.register_on_shutdown(function() + storage:set_string("player_dbg", minetest.serialize(player_dbg)) +end) + info() diff --git a/mods/HUD/mcl_info/locale/mcl_info.ru.tr b/mods/HUD/mcl_info/locale/mcl_info.ru.tr new file mode 100644 index 000000000..7f5b79fe1 --- /dev/null +++ b/mods/HUD/mcl_info/locale/mcl_info.ru.tr @@ -0,0 +1,4 @@ +# textdomain: mcl_info +Set debug bit mask: 0 @= disable, 1 @= biome name, 2 @= coordinates, 3 @= all=Установка отладочной битовой маски: 0 @= отключить, 1 @= биом, 2 @= координаты, 3 @= всё +Error! Possible values are integer numbers from @1 to @2=Ошибка! Допустимые значения - целые числа от @1 до @2 +Debug bit mask set to @1=Отладочной битовой маске присвоено значение @1 diff --git a/mods/HUD/mcl_info/locale/template.txt b/mods/HUD/mcl_info/locale/template.txt new file mode 100644 index 000000000..1a0b70ebc --- /dev/null +++ b/mods/HUD/mcl_info/locale/template.txt @@ -0,0 +1,4 @@ +# textdomain: mcl_info +Set debug bit mask: 0 @= disable, 1 @= biome name, 2 @= coordinates, 3 @= all= +Error! Possible values are integer numbers from @1 to @2= +Debug bit mask set to @1= From d5b2e60e3202bc84ca14de778d8597a4a7bdc2a5 Mon Sep 17 00:00:00 2001 From: kay27 Date: Mon, 4 Apr 2022 03:49:14 +0300 Subject: [PATCH 3/8] #278 Rename default_dbg to default_debug --- mods/HUD/mcl_info/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/HUD/mcl_info/init.lua b/mods/HUD/mcl_info/init.lua index 02af53fbc..9cf95b43a 100644 --- a/mods/HUD/mcl_info/init.lua +++ b/mods/HUD/mcl_info/init.lua @@ -89,7 +89,7 @@ minetest.register_chatcommand("debug",{ minetest.chat_send_player(name, S("Error! Possible values are integer numbers from @1 to @2", 0, 3)) return end - if dbg == default_dbg then + if dbg == default_debug then player_dbg[name] = nil else player_dbg[name] = dbg From 00ef88e01cca8de42aac1436f8a7d19e018a9685 Mon Sep 17 00:00:00 2001 From: cora Date: Mon, 6 Jun 2022 11:40:31 +0200 Subject: [PATCH 4/8] fix mistakes from mcl5 import, clean up output --- mods/HUD/mcl_info/init.lua | 33 +++++++++++---------------------- mods/HUD/mcl_info/mod.conf | 2 +- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/mods/HUD/mcl_info/init.lua b/mods/HUD/mcl_info/init.lua index 9cf95b43a..c447f0de9 100644 --- a/mods/HUD/mcl_info/init.lua +++ b/mods/HUD/mcl_info/init.lua @@ -7,8 +7,8 @@ local get_biome_name = minetest.get_biome_name local get_biome_data = minetest.get_biome_data local format = string.format -local min1, min2, min3 = mcl_mapgen.overworld.min, mcl_mapgen.end_.min, mcl_mapgen.nether.min -local max1, max2, max3 = mcl_mapgen.overworld.max, mcl_mapgen.end_.max, mcl_mapgen.nether.max + 128 +local min1, min2, min3 = mcl_vars.mg_overworld_min, mcl_vars.mg_end_min, mcl_vars.mg_nether_min +local max1, max2, max3 = mcl_vars.mg_overworld_max, mcl_vars.mg_end_max, mcl_vars.mg_nether_max + 128 local modname = minetest.get_current_modname() local modpath = minetest.get_modpath(modname) @@ -19,25 +19,13 @@ local player_dbg = minetest.deserialize(storage:get_string("player_dbg") or "ret local function get_text(pos, bits) local bits = bits if bits == 0 then return "" end - local y = pos.y - if y >= min1 then - y = y - min1 - elseif y >= min3 and y <= max3 then - y = y - min3 - elseif y >= min2 and y <= max2 then - y = y - min2 - end + local biome_data = get_biome_data(pos) local biome_name = biome_data and get_biome_name(biome_data.biome) or "No biome" - local text - if bits == 1 then - text = biome_name - elseif bits == 2 then - text = format("x:%.1f y:%.1f z:%.1f", pos.x, y, pos.z) - elseif bits == 3 then - text = format("%s x:%.1f y:%.1f z:%.1f", biome_name, pos.x, y, pos.z) - end - return text + local biome = format("%s (%s), Humidity: %.1f, Temperature: %.1f",biome_name, biome_data.biome, biome_data.humidity, biome_data.heat) + local coord = format("x:%.1f y:%.1f z:%.1f", pos.x, pos.y, pos.z) + --local pointed = + return biome.."\n"..coord end local function info() @@ -51,7 +39,7 @@ local function info() hud_elem_type = "text", alignment = {x = 1, y = -1}, scale = {x = 100, y = 100}, - position = {x = 0.0073, y = 0.989}, + position = {x = 0.0073, y = 0.889}, text = text, style = 5, ["number"] = 0xcccac0, @@ -85,8 +73,8 @@ minetest.register_chatcommand("debug",{ description = S("Set debug bit mask: 0 = disable, 1 = biome name, 2 = coordinates, 3 = all"), func = function(name, params) local dbg = math.floor(tonumber(params) or default_debug) - if dbg < 0 or dbg > 3 then - minetest.chat_send_player(name, S("Error! Possible values are integer numbers from @1 to @2", 0, 3)) + if dbg < 0 or dbg > 4 then + minetest.chat_send_player(name, S("Error! Possible values are integer numbers from @1 to @2", 0, 4)) return end if dbg == default_debug then @@ -98,6 +86,7 @@ minetest.register_chatcommand("debug",{ end }) +--why is this saved on shutdown but not on playerleave / changes ? minetest.register_on_shutdown(function() storage:set_string("player_dbg", minetest.serialize(player_dbg)) end) diff --git a/mods/HUD/mcl_info/mod.conf b/mods/HUD/mcl_info/mod.conf index da3e10fff..41fdfb0b5 100644 --- a/mods/HUD/mcl_info/mod.conf +++ b/mods/HUD/mcl_info/mod.conf @@ -1,3 +1,3 @@ name = mcl_info description = Prints biome name and player position -optional_depends = mcl_mapgen +depends = mcl_init From 39b9a058e9c04750ea317676a4b981c86b258fdf Mon Sep 17 00:00:00 2001 From: cora Date: Mon, 18 Jul 2022 20:47:04 +0200 Subject: [PATCH 5/8] Save persistent setting in player meta --- mods/HUD/mcl_info/init.lua | 55 +++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/mods/HUD/mcl_info/init.lua b/mods/HUD/mcl_info/init.lua index c447f0de9..2340f9b2d 100644 --- a/mods/HUD/mcl_info/init.lua +++ b/mods/HUD/mcl_info/init.lua @@ -1,6 +1,6 @@ local refresh_interval = .63 local huds = {} -local default_debug = 3 +local default_debug = 0 local after = minetest.after local get_connected_players = minetest.get_connected_players local get_biome_name = minetest.get_biome_name @@ -14,7 +14,26 @@ local modname = minetest.get_current_modname() local modpath = minetest.get_modpath(modname) local S = minetest.get_translator(modname) local storage = minetest.get_mod_storage() -local player_dbg = minetest.deserialize(storage:get_string("player_dbg") or "return {}") or {} +local player_dbg = {} + +local function check_setting(s) + return s +end + +--return player setting, set it to 2nd argument if supplied +local function player_setting(p,s) + local name = p:get_player_name() + if check_setting(s) then + p:get_meta():set_string("mcl_info_show",s) + player_dbg[name] = tonumber(s) + end + if not player_dbg[name] then + local r = p:get_meta():get_string("mcl_info_show") + if r == nil or r == "" then r = 0 end + player_dbg[name] = tonumber(r) + end + return player_dbg[name] +end local function get_text(pos, bits) local bits = bits @@ -24,17 +43,17 @@ local function get_text(pos, bits) local biome_name = biome_data and get_biome_name(biome_data.biome) or "No biome" local biome = format("%s (%s), Humidity: %.1f, Temperature: %.1f",biome_name, biome_data.biome, biome_data.humidity, biome_data.heat) local coord = format("x:%.1f y:%.1f z:%.1f", pos.x, pos.y, pos.z) - --local pointed = return biome.."\n"..coord end local function info() for _, player in pairs(get_connected_players()) do local name = player:get_player_name() + local s = player_setting(player) local pos = player:get_pos() - local text = get_text(pos, player_dbg[name] or default_debug) + local text = get_text(pos, s) local hud = huds[name] - if not hud then + if s and not hud then local def = { hud_elem_type = "text", alignment = {x = 1, y = -1}, @@ -63,32 +82,26 @@ local function info() after(refresh_interval, info) end -minetest.register_on_authplayer(function(name, ip, is_success) - if is_success then - huds[name] = nil - end +minetest.register_on_leaveplayer(function(p) + local name = p:get_player_name() + huds[name] = nil + player_dbg[name] = nil end) minetest.register_chatcommand("debug",{ description = S("Set debug bit mask: 0 = disable, 1 = biome name, 2 = coordinates, 3 = all"), + params = S(""), + privs = { debug = true }, func = function(name, params) + local player = minetest.get_player_by_name(name) + if params == "" then return true, "Debug bitmask is "..player_setting(player) end local dbg = math.floor(tonumber(params) or default_debug) if dbg < 0 or dbg > 4 then minetest.chat_send_player(name, S("Error! Possible values are integer numbers from @1 to @2", 0, 4)) - return + return false,"Current bitmask: "..player_setting(player) end - if dbg == default_debug then - player_dbg[name] = nil - else - player_dbg[name] = dbg - end - minetest.chat_send_player(name, S("Debug bit mask set to @1", dbg)) + return true, "Debug bit mask set to "..player_setting(player,dbg) end }) ---why is this saved on shutdown but not on playerleave / changes ? -minetest.register_on_shutdown(function() - storage:set_string("player_dbg", minetest.serialize(player_dbg)) -end) - info() From 7e6daa0e6006f7496f9adc1bb040a3e81b6cb40b Mon Sep 17 00:00:00 2001 From: cora Date: Mon, 18 Jul 2022 21:45:20 +0200 Subject: [PATCH 6/8] Add api to register custom debug fields also add node info as first fields --- mods/HUD/mcl_info/init.lua | 51 ++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/mods/HUD/mcl_info/init.lua b/mods/HUD/mcl_info/init.lua index 2340f9b2d..199e48375 100644 --- a/mods/HUD/mcl_info/init.lua +++ b/mods/HUD/mcl_info/init.lua @@ -1,3 +1,4 @@ +mcl_info = {} local refresh_interval = .63 local huds = {} local default_debug = 0 @@ -35,15 +36,55 @@ local function player_setting(p,s) return player_dbg[name] end -local function get_text(pos, bits) +mcl_info.registered_debug_fields = {} +function mcl_info.register_debug_field(name,def) + mcl_info.registered_debug_fields[name]=def +end + +local function nodeinfo(pos) + local n = minetest.get_node_or_nil(pos) + if not n then return "" end + local l = minetest.get_node_light(pos) + local ld = minetest.get_node_light(pos,0.5) + local r = n.name .. " p1:"..n.param1.." p2:"..n.param2 + if l and ld then + r = r .. " Light: "..l.."/"..ld + end + return r +end + +mcl_info.register_debug_field("Node feet",{ + level = 4, + func = function(pl,pos) + return nodeinfo(pos) + end +}) +mcl_info.register_debug_field("Node below",{ + level = 4, + func = function(pl,pos) + return nodeinfo(vector.offset(pos,0,-1,0)) + end +}) + +local function get_text(player, bits) + local pos = vector.offset(player:get_pos(),0,0.5,0) local bits = bits if bits == 0 then return "" end + local r = "" + for n,def in pairs(mcl_info.registered_debug_fields) do + if def.level == nil or def.level <= bits then + r = r .. n..": "..tostring(def.func(player,pos)).."\n" + end + end + local biome_data = get_biome_data(pos) - local biome_name = biome_data and get_biome_name(biome_data.biome) or "No biome" - local biome = format("%s (%s), Humidity: %.1f, Temperature: %.1f",biome_name, biome_data.biome, biome_data.humidity, biome_data.heat) + local biome = biome_data and get_biome_name(biome_data.biome) or "No biome" + if biome_data then + biome = format("%s (%s), Humidity: %.1f, Temperature: %.1f",biome, biome_data.biome, biome_data.humidity, biome_data.heat) + end local coord = format("x:%.1f y:%.1f z:%.1f", pos.x, pos.y, pos.z) - return biome.."\n"..coord + return r..biome.."\n"..coord end local function info() @@ -51,7 +92,7 @@ local function info() local name = player:get_player_name() local s = player_setting(player) local pos = player:get_pos() - local text = get_text(pos, s) + local text = get_text(player, s) local hud = huds[name] if s and not hud then local def = { From ac45ed8a0ad6ce5f96b2e0440d47b04acd6cb701 Mon Sep 17 00:00:00 2001 From: cora Date: Mon, 18 Jul 2022 22:05:09 +0200 Subject: [PATCH 7/8] coord and biome use api too, fields well ordered --- mods/HUD/mcl_info/init.lua | 59 +++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/mods/HUD/mcl_info/init.lua b/mods/HUD/mcl_info/init.lua index 199e48375..5331a9bd6 100644 --- a/mods/HUD/mcl_info/init.lua +++ b/mods/HUD/mcl_info/init.lua @@ -37,7 +37,9 @@ local function player_setting(p,s) end mcl_info.registered_debug_fields = {} +local fields_keyset = {} function mcl_info.register_debug_field(name,def) + table.insert(fields_keyset,name) mcl_info.registered_debug_fields[name]=def end @@ -53,38 +55,20 @@ local function nodeinfo(pos) return r end -mcl_info.register_debug_field("Node feet",{ - level = 4, - func = function(pl,pos) - return nodeinfo(pos) - end -}) -mcl_info.register_debug_field("Node below",{ - level = 4, - func = function(pl,pos) - return nodeinfo(vector.offset(pos,0,-1,0)) - end -}) - local function get_text(player, bits) local pos = vector.offset(player:get_pos(),0,0.5,0) local bits = bits if bits == 0 then return "" end local r = "" - for n,def in pairs(mcl_info.registered_debug_fields) do + for _,key in ipairs(fields_keyset) do + local def = mcl_info.registered_debug_fields[key] if def.level == nil or def.level <= bits then - r = r .. n..": "..tostring(def.func(player,pos)).."\n" + r = r ..key..": "..tostring(def.func(player,pos)).."\n" end end - local biome_data = get_biome_data(pos) - local biome = biome_data and get_biome_name(biome_data.biome) or "No biome" - if biome_data then - biome = format("%s (%s), Humidity: %.1f, Temperature: %.1f",biome, biome_data.biome, biome_data.humidity, biome_data.heat) - end - local coord = format("x:%.1f y:%.1f z:%.1f", pos.x, pos.y, pos.z) - return r..biome.."\n"..coord + return r end local function info() @@ -122,6 +106,7 @@ local function info() end after(refresh_interval, info) end +info() minetest.register_on_leaveplayer(function(p) local name = p:get_player_name() @@ -145,4 +130,32 @@ minetest.register_chatcommand("debug",{ end }) -info() +mcl_info.register_debug_field("Node feet",{ + level = 4, + func = function(pl,pos) + return nodeinfo(pos) + end +}) +mcl_info.register_debug_field("Node below",{ + level = 4, + func = function(pl,pos) + return nodeinfo(vector.offset(pos,0,-1,0)) + end +}) +mcl_info.register_debug_field("Biome",{ + level = 3, + func = function(pl,pos) + local biome_data = get_biome_data(pos) + local biome = biome_data and get_biome_name(biome_data.biome) or "No biome" + if biome_data then + return format("%s (%s), Humidity: %.1f, Temperature: %.1f",biome, biome_data.biome, biome_data.humidity, biome_data.heat) + end + return "No biome" + end +}) +mcl_info.register_debug_field("Coords",{ + level = 2, + func = function(pl,pos) + return format("x:%.1f y:%.1f z:%.1f", pos.x, pos.y, pos.z) + end +}) From 37114e7d1a73c0107c6e475d5511039c5b73e8a4 Mon Sep 17 00:00:00 2001 From: cora Date: Mon, 18 Jul 2022 22:12:56 +0200 Subject: [PATCH 8/8] Add API documentation --- mods/HUD/mcl_info/API.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 mods/HUD/mcl_info/API.md diff --git a/mods/HUD/mcl_info/API.md b/mods/HUD/mcl_info/API.md new file mode 100644 index 000000000..18c901162 --- /dev/null +++ b/mods/HUD/mcl_info/API.md @@ -0,0 +1,18 @@ +## mcl_info +An api to make custom entries in the mcl2 debug hud. + +### mcl_info.register_debug_field(name,defintion) +Debug field defintion example: +{ + level = 3, + --show with debug level 3 and upwards + + func = function(player,pos) return minetest.pos_to_string(pos) end, + -- Function that is run for at each debug + -- sample (default: every .63 seconds) + -- It should output a string and determines + -- the content of the debug field. +} + +### mcl_info.registered_debug_fields +Table the debug definitions are stored in. Do not modify this directly. If you need to overwrite a field just set it again with mcl_info.register_debug_field().