show usage of variable in texts and alternate texts in manage variables/display usage

This commit is contained in:
Sokomine 2024-12-26 00:12:21 +01:00
parent fbf232da96
commit ebd2999687
2 changed files with 42 additions and 0 deletions

View File

@ -17,16 +17,31 @@ yl_speak_up.fs_get_list_of_usage_of_variable = function(var_name, pname, check_p
local res = {}
local count_read = 0
local count_changed = 0
local var_val_in_text = "$VAR "..string.sub(var_name, 3)
for i, n_id in ipairs(npc_list) do
-- the NPC may not even be loaded
local dialog = yl_speak_up.load_dialog(n_id, false)
if(dialog and dialog.n_dialogs) then
for d_id, d in pairs(dialog.n_dialogs) do
-- show the value of the variable in the dialog text?
if(string.find(d.d_text or "", var_val_in_text)) then
-- we are not really dealing with an option - this is a variable displayed in the main text
local o = {o_id = "", o_text_when_prerequisites_met = "[The value of the variable is displayed in the dialog text.]"}
-- at least one blank is needed so that the text is displayed at all
yl_speak_up.print_as_table_dialog(" ", " ", dialog,
n_id, d_id, "", res, o, sort_value)
end
if(d and d.d_options) then
for o_id, o in pairs(d.d_options) do
local p_text = ""
local r_text = ""
local sort_value = 0
-- show the value of the variable in the options' text?
if(string.find(o.o_text_when_prerequisites_met or "", var_val_in_text)) then
p_text = p_text..yl_speak_up.print_as_table_alternate_text(
tostring(o.o_id), "", "",
"[Answer shows value of the variable in the text shown if ALL preconditions are FULFILLED.]")
end
if(o and o.o_prerequisites and check_preconditions) then
for p_id, p in pairs(o.o_prerequisites) do
if(p and p.p_type and p.p_type == "state"
@ -37,6 +52,12 @@ yl_speak_up.fs_get_list_of_usage_of_variable = function(var_name, pname, check_p
end
end
end
-- show the value of the variable in the options' text if preconditions failed?
if(string.find(o.o_text_when_prerequisites_not_met or "", var_val_in_text)) then
p_text = p_text..yl_speak_up.print_as_table_alternate_text(
tostring(o.o_id), "", "",
"[Answer shows value of the variable in the text shown if a precondition FAILED.]")
end
if(o and o.o_results) then
for r_id, r in pairs(o.o_results) do
if(r and r.r_type and r.r_type == "state"
@ -47,6 +68,11 @@ yl_speak_up.fs_get_list_of_usage_of_variable = function(var_name, pname, check_p
sort_value = (r.r_var_cmp_value or 0)
count_changed = count_changed + 1
end
if(r and r.alternate_text and string.find(r.alternate_text, var_val_in_text)) then
r_text = r_text..yl_speak_up.print_as_table_alternate_text(
tostring(r.r_id), "effect", "failed",
r.alternate_text)
end
end
end
-- if preconditions or effects apply: show the action as well
@ -55,6 +81,11 @@ yl_speak_up.fs_get_list_of_usage_of_variable = function(var_name, pname, check_p
-- no need to introduce an a_text; this will follow
-- directly after p_text, and p_text is finished
p_text = p_text..yl_speak_up.print_as_table_action(a, pname)
if(a and a.alternate_text and string.find(r.alternate_text, var_val_in_text)) then
p_text = p_text..yl_speak_up.print_as_table_alternate_text(
tostring(a.a_id), "action", "failed",
a.alternate_text)
end
end
end
yl_speak_up.print_as_table_dialog(p_text, r_text, dialog,

View File

@ -42,6 +42,17 @@ yl_speak_up.print_as_table_effect = function(r, pname)
end
yl_speak_up.print_as_table_alternate_text = function(where_used, col1, col2, alternate_text)
if(not(alternate_text) or alternate_text == "") then
return ""
end
return ",#777777,"..minetest.formspec_escape(where_used or "")..
",#777777,"..minetest.formspec_escape(col1 or "")..
",#777777,"..minetest.formspec_escape(col2 or "")..
",#777777,"..minetest.formspec_escape(alternate_text)
end
yl_speak_up.print_as_table_action = function(a, pname)
return ",#FF9900,"..
minetest.formspec_escape(tostring(a.a_id))..