added yl_speak_up.get_variable_metadata for cleaner access to metadata

This commit is contained in:
Sokomine 2021-07-31 20:36:47 +02:00
parent 4f524f7fc1
commit 372a37f8e6

View File

@ -94,22 +94,15 @@ yl_speak_up.del_quest_variable = function(owner_name, variable_name, force_delet
-- variables can be deleted;
-- check if the variable is used by an NPC
local var_data = yl_speak_up.player_vars[ var_name ]
if(var_data["$META$"] and var_data["$META$"]["used_by_npc" ]
and type(var_data["$META$"]["used_by_npc" ]) == "table") then
for k, v in pairs(var_data["$META$"]["used_by_npc" ]) do
if(k) then
return text.." could not be deleted.\nIt is used by at least one NPC."
end
end
local npc_users = yl_speak_up.get_variable_metadata(var_name, "used_by_npc")
if(npc_users and #npc_users > 0) then
return text.." could not be deleted.\nIt is used by "..tostring(#npc_users).." NPC."
end
-- check if the variable is used by a node position (for quests)
if(var_data["$META$"] and var_data["$META$"]["used_by_node_pos" ]
and type(var_data["$META$"]["used_by_node_pos" ]) == "table") then
for k, v in pairs(var_data["$META$"]["used_by_node_pos" ]) do
if(k) then
return text.." could not be deleted.\nIt is used by at least one node pos."
end
end
local node_pos_users = yl_speak_up.get_variable_metadata(var_name, "used_by_node_pos")
if(node_pos_users and #node_pos_users > 0) then
return text.." could not be deleted.\nIt is used by "..tostring(#node_pos_users)..
" node positions (quest)."
end
-- check if the variable has any values stored
for k, v in pairs(var_data) do
@ -487,8 +480,8 @@ yl_speak_up.get_fs_manage_variables = function(player, param)
"you entered in the field to the left *write* access\n"..
"to your variable.]"
end
local list_of_npc_users = ""
local list_of_node_pos_users = ""
local list_of_npc_users = "- none -"
local list_of_node_pos_users = "- none -"
-- expand name of variable k again
local k_long = yl_speak_up.add_pname_to_var(k, pname)
-- which npc and which node_pos use this variable? create a list for display
@ -497,38 +490,17 @@ yl_speak_up.get_fs_manage_variables = function(player, param)
if(k_long
and yl_speak_up.player_vars[ k_long ]
and yl_speak_up.player_vars[ k_long ][ "$META$"]) then
local t = yl_speak_up.player_vars[ k_long ][ "$META$"]["used_by_npc" ]
if(type(t) ~= "table") then
t = {}
local npc_users = yl_speak_up.get_variable_metadata(k_long, "used_by_npc")
c1 = #npc_users
if(npc_users and c1 > 0) then
list_of_npc_users = minetest.formspec_escape(table.concat(npc_users, ", "))
end
for key, v in pairs(t or {}) do
if(c1==0) then
list_of_npc_users = minetest.formspec_escape(key)
else
list_of_npc_users = list_of_npc_users..", "..
minetest.formspec_escape(key)
end
c1 = c1 + 1
local node_pos_users = yl_speak_up.get_variable_metadata(k_long, "used_by_node_pos")
c2 = #node_pos_users
if(node_pos_users and c2 > 0) then
list_of_node_pos_users = minetest.formspec_escape(table.concat(
node_pos_users, ", "))
end
local t = yl_speak_up.player_vars[ k_long ][ "$META$"]["used_by_node_pos" ]
if(type(t) ~= "table") then
t = {}
end
for key, v in pairs(t or {}) do
if(c2==0) then
list_of_node_pos_users = minetest.formspec_escape(key)
else
list_of_node_pos_users = list_of_node_pos_users..", "..
minetest.formspec_escape(key)
end
c2 = c2 + 1
end
end
if(list_of_npc_users == "") then
list_of_npc_users = "- none -"
end
if(list_of_node_pos_users == "") then
list_of_node_pos_users = "- none -"
end
additional_buttons = "button[11.4,1.9;2.5,0.9;show_var_usage;Where is it used?]"..
"tooltip[show_var_usage;Show which NPC use this variable in which context.]"..
@ -799,10 +771,9 @@ yl_speak_up.update_stored_npc_data = function(n_id, dialog)
-- make sure all variables have a metadata field
for k, v in pairs(yl_speak_up.player_vars) do
if( yl_speak_up.player_vars[ k ][ "$META$" ]
and yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc) then
if(type(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc) ~= "table") then
yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc = {}
end
and yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc
and type(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc) == "table"
and yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc[ n_id ]) then
-- delete old entries (the correct new ones will be set shortly after this)
yl_speak_up.player_vars[ k ][ "$META$"].used_by_npc[ n_id ] = nil
end
@ -818,7 +789,11 @@ yl_speak_up.update_stored_npc_data = function(n_id, dialog)
if(not(yl_speak_up.player_vars[ k ][ "$META$" ])) then
yl_speak_up.player_vars[ k ][ "$META$"] = { used_by_npc = {} }
end
yl_speak_up.player_vars[ k ][ "$META$"][ "used_by_npc" ][ n_id ] = true
if(not(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc)
or type(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc) ~= "table") then
yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc = {}
end
yl_speak_up.player_vars[ k ][ "$META$"].used_by_npc[ n_id ] = true
end
for k, v in pairs(variables_e) do
-- if the variable does not exist: create it
@ -828,7 +803,11 @@ yl_speak_up.update_stored_npc_data = function(n_id, dialog)
if(not(yl_speak_up.player_vars[ k ][ "$META$" ])) then
yl_speak_up.player_vars[ k ][ "$META$"] = { used_by_npc = {} }
end
yl_speak_up.player_vars[ k ][ "$META$"][ "used_by_npc" ][ n_id ] = true
if(not(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc)
or type(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc) ~= "table") then
yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc = {}
end
yl_speak_up.player_vars[ k ][ "$META$"].used_by_npc[ n_id ] = true
end
-- force writing the data
yl_speak_up.save_quest_variables(true)
@ -836,21 +815,22 @@ end
-- which NPC do use this variable?
yl_speak_up.get_npc_users_of_variable = function(var_name)
yl_speak_up.get_variable_metadata = function(var_name, meta_name)
-- no variable, or nothing stored? then it's not used by any NPC either
if(not(var_name)
or not(yl_speak_up.player_vars[ var_name ])
or not(yl_speak_up.player_vars[ var_name ][ "$META$"])
or not(yl_speak_up.player_vars[ var_name ][ "$META$"][ "used_by_npc" ])
or type(yl_speak_up.player_vars[ var_name ][ "$META$"][ "used_by_npc" ]) ~= "table") then
or not(meta_name)
or not(yl_speak_up.player_vars[ var_name ])
or not(yl_speak_up.player_vars[ var_name ][ "$META$"])
or not(yl_speak_up.player_vars[ var_name ][ "$META$"][ meta_name ])
or type(yl_speak_up.player_vars[ var_name ][ "$META$"][ meta_name ]) ~= "table") then
return {}
end
local npc_list = {}
for n_id, v in pairs(yl_speak_up.player_vars[ var_name ][ "$META$"][ "used_by_npc" ]) do
table.insert(npc_list, n_id)
local meta_list = {}
for k, v in pairs(yl_speak_up.player_vars[ var_name ][ "$META$"][ meta_name ]) do
table.insert(meta_list, k)
end
table.sort(npc_list)
return npc_list
table.sort(meta_list)
return meta_list
end
@ -863,7 +843,7 @@ yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_prec
end
-- which NPC (might be several) is using this variable?
-- TODO: ..or if the player at least is owner of these NPC or has extended privs
local npc_list = yl_speak_up.get_npc_users_of_variable(var_name)
local npc_list = yl_speak_up.get_variable_metadata(var_name, "used_by_npc")
-- list of all relevant preconditions, actions and effects
local res = {}
local count_read = 0