search texts for variable (display) usage when storing npc data
This commit is contained in:
parent
68e37e24b8
commit
e3094962c3
@ -394,6 +394,22 @@ yl_speak_up.get_access_list_for_var = function(k, pname, access_what)
|
||||
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
|
||||
-- plus store which variables are used by this NPC
|
||||
-- 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_effects = 0
|
||||
local anz_trades = 0
|
||||
local variables_p = {}
|
||||
local variables_e = {}
|
||||
-- used in d.d_text dialog texts,
|
||||
-- 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
|
||||
for d_id, d in pairs(dialog.n_dialogs) do
|
||||
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
|
||||
for o_id, o in pairs(d.d_options) do
|
||||
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
|
||||
for p_id, p in pairs(o.o_prerequisites) do
|
||||
anz_preconditions = anz_preconditions + 1
|
||||
if(p and p.p_type and p.p_type == "state"
|
||||
and p.p_variable and p.p_variable ~= "") then
|
||||
variables_p[ p.p_variable ] = true
|
||||
variables_used[ p.p_variable ] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
if(o and o.actions) then
|
||||
for a_id, a_data in pairs(o.actions) do
|
||||
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
|
||||
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
|
||||
if(r and r.r_type and r.r_type == "state"
|
||||
and r.r_variable and r.r_variable ~= "") then
|
||||
variables_e[ r.r_variable ] = true
|
||||
variables_used[ r.r_variable ] = true
|
||||
end
|
||||
-- effects can have alternate_text
|
||||
variables_used = yl_speak_up.find_player_vars_in_text(variables_used, r.alternate_text)
|
||||
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
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
-- save in the variables' metadata which NPC uses it
|
||||
-- (this is what we're mostly after - know which variable is used in which NPC)
|
||||
for k, v in pairs(variables_p) do
|
||||
yl_speak_up.set_variable_metadata(k, pname, "used_by_npc", n_id, true)
|
||||
end
|
||||
for k, v in pairs(variables_e) do
|
||||
for k, v in pairs(variables_used) do
|
||||
yl_speak_up.set_variable_metadata(k, pname, "used_by_npc", n_id, true)
|
||||
end
|
||||
-- force writing the data
|
||||
|
Loading…
Reference in New Issue
Block a user