search texts for variable (display) usage when storing npc data

This commit is contained in:
Sokomine 2024-12-25 22:50:33 +01:00
parent 68e37e24b8
commit e3094962c3

View File

@ -394,6 +394,22 @@ yl_speak_up.get_access_list_for_var = function(k, pname, access_what)
end end
-- helper function that searces for variables that will be replaced with their
-- values in text when displayed; helper function for yl_speak_up.update_stored_npc_data
-- (for keeping track of which NPC uses which variables)
-- changes table vars_used
yl_speak_up.find_player_vars_in_text = function(vars_used, text)
if(not(text) or text == "") then
return vars_used
end
for v in string.gmatch(text, "%$VAR ([%w%s_%-%.]+)%$") do
-- add the $ prefix again
vars_used["$ "..tostring(v)] = true
end
return vars_used
end
-- the dialog data of an NPC is saved - use this to save some statistical data -- the dialog data of an NPC is saved - use this to save some statistical data
-- plus store which variables are used by this NPC -- plus store which variables are used by this NPC
-- TODO: show this data in a formspec to admins for maintenance -- TODO: show this data in a formspec to admins for maintenance
@ -414,26 +430,36 @@ yl_speak_up.update_stored_npc_data = function(n_id, dialog)
local anz_actions = 0 local anz_actions = 0
local anz_effects = 0 local anz_effects = 0
local anz_trades = 0 local anz_trades = 0
local variables_p = {} -- used in d.d_text dialog texts,
local variables_e = {} -- o.o_text_when_prerequisites_met, o.o_text_when_prerequisites_not_met,
-- preconditions and effects
local variables_used = {}
if(dialog and dialog.n_dialogs) then if(dialog and dialog.n_dialogs) then
for d_id, d in pairs(dialog.n_dialogs) do for d_id, d in pairs(dialog.n_dialogs) do
anz_dialogs = anz_dialogs + 1 anz_dialogs = anz_dialogs + 1
if(d) then
-- find all variables used in the text
variables_used = yl_speak_up.find_player_vars_in_text(variables_used, d.d_text)
end
if(d and d.d_options) then if(d and d.d_options) then
for o_id, o in pairs(d.d_options) do for o_id, o in pairs(d.d_options) do
anz_options = anz_options + 1 anz_options = anz_options + 1
variables_used = yl_speak_up.find_player_vars_in_text(variables_used, o.o_text_when_prerequisites_met)
variables_used = yl_speak_up.find_player_vars_in_text(variables_used, o.o_text_when_prerequisites_not_met)
if(o and o.o_prerequisites) then if(o and o.o_prerequisites) then
for p_id, p in pairs(o.o_prerequisites) do for p_id, p in pairs(o.o_prerequisites) do
anz_preconditions = anz_preconditions + 1 anz_preconditions = anz_preconditions + 1
if(p and p.p_type and p.p_type == "state" if(p and p.p_type and p.p_type == "state"
and p.p_variable and p.p_variable ~= "") then and p.p_variable and p.p_variable ~= "") then
variables_p[ p.p_variable ] = true variables_used[ p.p_variable ] = true
end end
end end
end end
if(o and o.actions) then if(o and o.actions) then
for a_id, a_data in pairs(o.actions) do for a_id, a_data in pairs(o.actions) do
anz_actions = anz_actions + 1 anz_actions = anz_actions + 1
-- actions can have alternate_text
variables_used = yl_speak_up.find_player_vars_in_text(variables_used, a.alternate_text)
end end
end end
if(o and o.o_results) then if(o and o.o_results) then
@ -441,8 +467,10 @@ yl_speak_up.update_stored_npc_data = function(n_id, dialog)
anz_effects = anz_effects + 1 anz_effects = anz_effects + 1
if(r and r.r_type and r.r_type == "state" if(r and r.r_type and r.r_type == "state"
and r.r_variable and r.r_variable ~= "") then and r.r_variable and r.r_variable ~= "") then
variables_e[ r.r_variable ] = true variables_used[ r.r_variable ] = true
end end
-- effects can have alternate_text
variables_used = yl_speak_up.find_player_vars_in_text(variables_used, r.alternate_text)
end end
end end
end end
@ -478,17 +506,14 @@ yl_speak_up.update_stored_npc_data = function(n_id, dialog)
-- delete all old entries that are not longer needed -- delete all old entries that are not longer needed
for k, v in pairs(yl_speak_up.player_vars) do for k, v in pairs(yl_speak_up.player_vars) do
if(not(variables_p[ k ]) and not(variables_e[ k ])) then if(not(variables_used[ k ])) then
yl_speak_up.set_variable_metadata(k, pname, "used_by_npc", n_id, false) yl_speak_up.set_variable_metadata(k, pname, "used_by_npc", n_id, false)
end end
end end
-- save in the variables' metadata which NPC uses it -- save in the variables' metadata which NPC uses it
-- (this is what we're mostly after - know which variable is used in which NPC) -- (this is what we're mostly after - know which variable is used in which NPC)
for k, v in pairs(variables_p) do for k, v in pairs(variables_used) do
yl_speak_up.set_variable_metadata(k, pname, "used_by_npc", n_id, true)
end
for k, v in pairs(variables_e) do
yl_speak_up.set_variable_metadata(k, pname, "used_by_npc", n_id, true) yl_speak_up.set_variable_metadata(k, pname, "used_by_npc", n_id, true)
end end
-- force writing the data -- force writing the data