added yl_speak_up.print_as_table_* functions for two functions: show usage of a variable and show what points to this dialog

This commit is contained in:
Sokomine 2021-07-25 02:07:52 +02:00
parent 2eecc18bda
commit c324f5a8d9
2 changed files with 165 additions and 66 deletions

View File

@ -351,24 +351,44 @@ yl_speak_up.show_what_points_to_this_dialog = function(player, pname, dialog, th
end end
local found = {} local found = {}
-- colored lines for the table showing the results
local res = {}
-- iterate over all dialogs -- iterate over all dialogs
for d_id, d in pairs(dialog.n_dialogs) do for d_id, d in pairs(dialog.n_dialogs) do
-- we are looking for dialogs that point here - not the dialog itself -- we are looking for dialogs that point here - not the dialog itself
if(d_id ~= this_dialog and d.d_options) then if(d_id ~= this_dialog and d.d_options) then
-- iterate over all options -- iterate over all options
for o_id, o in pairs(d.d_options) do for o_id, o in pairs(d.d_options) do
local r_text = ""
local p_text = ""
local alternate_dialog = nil
local alternate_text = nil
-- preconditions are not relevant; -- preconditions are not relevant;
-- effects are (dialog and on_failure) -- effects are (dialog and on_failure)
if(o.o_results) then if(o.o_results) then
for r_id, r in pairs(o.o_results) do for r_id, r in pairs(o.o_results) do
if(r and r.r_type and r.r_type == "dialog" if(r and r.r_type and r.r_type == "dialog"
and r.r_value == this_dialog) then and r.r_value == this_dialog) then
r_text = r_text..yl_speak_up.print_as_table_effect(
r, pname)
table.insert(found, {d_id, o_id, r_id, table.insert(found, {d_id, o_id, r_id,
"option was selected"}) "option was selected"})
elseif(r and r.r_type and r.r_type == "on_failure" elseif(r and r.r_type and r.r_type == "on_failure"
and r.r_value == this_dialog) then and r.r_value == this_dialog) then
r_text = r_text..yl_speak_up.print_as_table_effect(
r, pname)
table.insert(found, {d_id, o_id, r_id, table.insert(found, {d_id, o_id, r_id,
"the previous effect failed"}) "the previous effect failed"})
alternate_dialog = r.r_value
alternate_text = "The previous effect failed. "
if(r.alternate_text) then
alternate_text = alternate_text..
"Show alternate text: "..
tostring(r.alternate_text)
else
alternate_text = alternate_text..
"Show this dialog here."
end
end end
end end
end end
@ -377,15 +397,56 @@ yl_speak_up.show_what_points_to_this_dialog = function(player, pname, dialog, th
for a_id, a in pairs(o.actions) do for a_id, a in pairs(o.actions) do
if(a and a.a_on_failure if(a and a.a_on_failure
and a.a_on_failure == this_dialog) then and a.a_on_failure == this_dialog) then
p_text = p_text..yl_speak_up.print_as_table_action(
a, pname)
table.insert(found, {d_id, o_id, a_id, table.insert(found, {d_id, o_id, a_id,
"action failed"}) "action failed"})
alternate_dialog = a.a_on_failure
alternate_text = "The action failed. "
if(a.alternate_text) then
alternate_text = alternate_text..
"Show this alternate text: "..
tostring(a.alternate_text)
else
alternate_text = alternate_text..
"Show this dialog here."
end
end end
end end
end end
yl_speak_up.print_as_table_dialog(p_text, r_text, dialog,
dialog.n_id, d_id, o_id,
-- sort value: formed by dialog and option id (not perfect but
-- good enough)
res, o, tostring(d_id).." "..tostring(o_id),
alternate_dialog, alternate_text)
end end
end end
end end
local d_id = this_dialog
local formspec = yl_speak_up.print_as_table_prepare_formspec(res, "table_of_dialog_uses",
"back_from_show_what_points_here", "Back to dialog \""..tostring(d_id).."\"")
table.insert(formspec,
"label[20.0,1.8;Dialog \""..minetest.formspec_escape(this_dialog)..
"\" is referenced here:]")
if(#found ~= 1) then
table.insert(formspec,
"label[16.0,31.0;"..
minetest.formspec_escape("This dialog \""..tostring(d_id)..
"\" can be reached from "..
minetest.colorize("#FFFF00", tostring(#found))..
" options/actions/results.").."]")
else
-- TODO. make turn into alternate_text work
table.insert(formspec,
"button[0.2,30.6;56.6,1.2;turn_dialog_into_alternate_text;"..
minetest.formspec_escape("Turn this dialog \""..
tostring(d_id)).."\" into an alternate text.]")
end
return table.concat(formspec, "\n")
--[[
local formspec = { local formspec = {
"formspec_version[3]", "formspec_version[3]",
"size[20,8]", "size[20,8]",
@ -411,4 +472,5 @@ yl_speak_up.show_what_points_to_this_dialog = function(player, pname, dialog, th
",#FFFFFF,"..minetest.formspec_escape(v[4])..",") ",#FFFFFF,"..minetest.formspec_escape(v[4])..",")
end end
return table.concat(formspec, "").."]" return table.concat(formspec, "").."]"
--]]
end end

View File

@ -403,12 +403,12 @@ end
-- helper function -- helper function
yl_speak_up.wrap_long_lines_for_table = function(text, prefix, line_length, max_lines) yl_speak_up.wrap_long_lines_for_table = function(text, prefix, line_length, max_lines)
-- show newlines as <\n> in order to save space -- show newlines as <\n> in order to save space
local text = minetest.formspec_escape(text or "?") local text = (text or "?")
text = string.gsub(text, "\n", minetest.formspec_escape("<br>")) text = string.gsub(text, "\n", minetest.formspec_escape("<br>"))
-- break the text up into lines of length x -- break the text up into lines of length x
local parts = minetest.wrap_text(text, line_length, true) local parts = minetest.wrap_text(text, line_length, true)
if(not(parts) or #parts < 2) then if(not(parts) or #parts < 2) then
return text return minetest.formspec_escape(text)
end end
local show_parts = {} local show_parts = {}
-- only show the first two lines (we don't have infinite room) -- only show the first two lines (we don't have infinite room)
@ -424,6 +424,97 @@ yl_speak_up.wrap_long_lines_for_table = function(text, prefix, line_length, max_
end end
-- helper functions for yl_speak_up.get_list_of_usage_of_variable
-- and yl_speak_up.show_what_points_to_this_dialog
yl_speak_up.print_as_table_precon = function(p, pname)
return ",#FFFF00,"..
minetest.formspec_escape(tostring(p.p_id))..
",#FFFF00,pre(C)ondition,#FFFF00,"..
minetest.formspec_escape(p.p_type)..",#FFFF00,"..
minetest.formspec_escape(yl_speak_up.show_precondition(p, pname))
end
yl_speak_up.print_as_table_effect = function(r, pname)
return ",#55FF55,"..
minetest.formspec_escape(tostring(r.r_id))..
",#55FF55,(Ef)fect,#55FF55,"..
minetest.formspec_escape(r.r_type)..",#55FF55,"..
minetest.formspec_escape(yl_speak_up.show_effect(r, pname))
end
yl_speak_up.print_as_table_action = function(a, pname)
return ",#FF9900,"..
minetest.formspec_escape(tostring(a.a_id))..
",#FF9900,(A)ction,#FF9900,"..
minetest.formspec_escape(a.a_type)..",#FF9900,"..
-- these lines can get pretty long when a description for a quest item is set
yl_speak_up.wrap_long_lines_for_table(
yl_speak_up.show_action(a, pname),
",#FFFFFF,,#FFFFFF,,#FFFFFF,,#FF9900,",
80, 4)
end
yl_speak_up.print_as_table_dialog = function(p_text, r_text, dialog, n_id, d_id, o_id, res, o, sort_value,
alternate_dialog, alternate_text)
if(p_text == "" and r_text == "" ) then
return
end
local d_text = yl_speak_up.wrap_long_lines_for_table(
dialog.n_dialogs[ d_id ].d_text or "?",
",#FFFFFF,,#FFFFFF,,#FFFFFF,,#BBBBFF,",
80, 3)
if(not(alternate_dialog) or not(alternate_text)) then
alternate_text = ""
else
alternate_text = ",#BBBBFF,"..minetest.formspec_escape(tostring(alternate_dialog))..
-- show alternate text in a diffrent color
",#BBBBFF,Dialog,#BBBBFF,says next:,#FFBBBB,"..
yl_speak_up.wrap_long_lines_for_table(
alternate_text,
",#FFFFFF,,#FFFFFF,,#FFFFFF,,#FFBBBB,",
80, 3)
end
res[ tostring(n_id).." "..tostring(d_id).." "..tostring(o_id) ] = {
text = "#6666FF,"..
tostring(n_id)..",#6666FF,NPC,#6666FF,named:,#6666FF,"..
minetest.formspec_escape(dialog.n_npc or "?")..","..
"#BBBBFF,"..
tostring(d_id)..",#BBBBFF,Dialog,#BBBBFF,says:,#BBBBFF,"..
d_text..","..
"#FFFFFF,"..
tostring(o_id)..",#FFFFFF,Option,#FFFFFF,A:,#FFFFFF,"..
minetest.formspec_escape(tostring(o.o_text_when_prerequisites_met or "?"))..
p_text..r_text..
alternate_text,
sort_value = sort_value}
end
yl_speak_up.print_as_table_prepare_formspec = function(res, table_name, back_button_name, back_button_text)
local sorted_list = yl_speak_up.get_sorted_options(res, "sort_value")
local sorted_res = {}
for i, k in pairs(sorted_list) do
table.insert(sorted_res, res[ k ].text)
end
local formspec = {
"formspec_version[3]",
"size[57,33]",
-- back to the list with that one precondition or effect
"button[0.2,0.2;56.6,1.2;"..back_button_name..";"..
minetest.formspec_escape(back_button_text).."]",
"button[0.2,31.6;56.6,1.2;"..back_button_name..";"..
minetest.formspec_escape(back_button_text).."]",
"tablecolumns[color,span=1;text;color,span=1;text;color,span=1;text;color,span=1;text]",
"table[1.2,2.4;55.0,28.0;"..tostring(table_name)..";"
}
-- insert blank lines between lines belonging together
table.insert(formspec, table.concat(sorted_res, ",#FFFFFF,,#FFFFFF,,#FFFFFF,,#FFFFFF,,").."]")
return formspec
end
-- find out where this variable is used in NPCs -- find out where this variable is used in NPCs
yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_preconditions, yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_preconditions,
back_button_name, back_button_text, is_internal_var) back_button_name, back_button_text, is_internal_var)
@ -452,12 +543,7 @@ yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_prec
for p_id, p in pairs(o.o_prerequisites) do for p_id, p in pairs(o.o_prerequisites) do
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 == var_name) then and p.p_variable and p.p_variable == var_name) then
p_text = p_text..",#FFFF00,".. p_text = p_text..yl_speak_up.print_as_table_precon(p,pname)
minetest.formspec_escape(tostring(p_id))..
",#FFFF00,pre(C)ondition,#FFFF00,"..
minetest.formspec_escape(p.p_type)..",#FFFF00,"..
minetest.formspec_escape(
yl_speak_up.show_precondition(p, pname))
sort_value = (p.p_var_cmp_value or 0) sort_value = (p.p_var_cmp_value or 0)
count_read = count_read + 1 count_read = count_read + 1
end end
@ -467,12 +553,7 @@ yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_prec
for r_id, r in pairs(o.o_results) do for r_id, r in pairs(o.o_results) do
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 == var_name) then and r.r_variable and r.r_variable == var_name) then
r_text = r_text..",#55FF55,".. r_text = r_text..yl_speak_up.print_as_table_effect(r,pname)
minetest.formspec_escape(tostring(r_id))..
",#55FF55,(Ef)fect,#55FF55,"..
minetest.formspec_escape(r.r_type)..",#55FF55,"..
minetest.formspec_escape(
yl_speak_up.show_effect(r, pname))
-- values set in the results are more important than -- values set in the results are more important than
-- those set in preconditions -- those set in preconditions
sort_value = (r.r_var_cmp_value or 0) sort_value = (r.r_var_cmp_value or 0)
@ -483,72 +564,28 @@ yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_prec
-- if preconditions or effects apply: show the action as well -- if preconditions or effects apply: show the action as well
if(o and o.actions and (p_text ~= "" or r_text ~= "")) then if(o and o.actions and (p_text ~= "" or r_text ~= "")) then
for a_id, a in pairs(o.actions) do for a_id, a in pairs(o.actions) do
-- these lines can get pretty long when a description for a quest
-- item is set
local a_text = yl_speak_up.wrap_long_lines_for_table(
yl_speak_up.show_action(a, pname),
",#FFFFFF,,#FFFFFF,,#FFFFFF,,#FF9900,",
80, 4)
-- no need to introduce an a_text; this will follow -- no need to introduce an a_text; this will follow
-- directly after p_text, and p_text is finished -- directly after p_text, and p_text is finished
p_text = p_text..",FF9900,".. p_text = p_text..yl_speak_up.print_as_table_action(a, pname)
minetest.formspec_escape(tostring(a_id))..
",#FF9900,(A)ction,#FF9900,"..
minetest.formspec_escape(a.a_type)..",#FF9900,"..
a_text
end end
end end
if(p_text ~= "" or r_text ~= "") then yl_speak_up.print_as_table_dialog(p_text, r_text, dialog,
local d_text = yl_speak_up.wrap_long_lines_for_table( n_id, d_id, o_id, res, o, sort_value)
dialog.n_dialogs[ d_id ].d_text or "?",
",#FFFFFF,,#FFFFFF,,#FFFFFF,,#BBBBFF,",
80, 3)
res[ tostring(n_id).." "..tostring(d_id).." "..tostring(o_id) ] = {
text =
"#6666FF,"..
tostring(n_id)..",#6666FF,NPC,#6666FF,named:,#6666FF,"..
minetest.formspec_escape(dialog.n_npc or "?")..","..
"#BBBBFF,"..
tostring(d_id)..",#BBBBFF,Dialog,#BBBBFF,says:,#BBBBFF,"..
d_text..","..
"#FFFFFF,"..
tostring(o_id)..",#FFFFFF,Option,#FFFFFF,A:,#FFFFFF,"..
minetest.formspec_escape(tostring(
o.o_text_when_prerequisites_met or "?"))..
p_text..r_text,
sort_value = sort_value}
end
end end
end end
end end
end end
end end
local sorted_list = yl_speak_up.get_sorted_options(res, "sort_value") local formspec = yl_speak_up.print_as_table_prepare_formspec(res, "table_of_variable_uses",
local sorted_res = {} back_button_name, back_button_text)
for i, k in pairs(sorted_list) do table.insert(formspec,
table.insert(sorted_res, res[ k ].text)
end
local formspec = {
"formspec_version[3]",
"size[57,33]",
-- back to the list with that one precondition or effect
"button[0.2,0.2;56.6,1.2;"..back_button_name..";"..
minetest.formspec_escape(back_button_text).."]",
"button[0.2,31.6;56.6,1.2;"..back_button_name..";"..
minetest.formspec_escape(back_button_text).."]",
"label[20.0,1.8;".. "label[20.0,1.8;"..
minetest.formspec_escape("Variable \"".. minetest.formspec_escape("Variable \""..
minetest.colorize("#FFFF00", tostring(var_name or "- ? -")).. minetest.colorize("#FFFF00", tostring(var_name or "- ? -"))..
"\" is used here:").."]".. "\" is used here:").."]")
-- type x_id color type text
"tablecolumns[color,span=1;text;color,span=1;text;color,span=1;text;color,span=1;text]",
"table[1.2,2.4;55.0,28.0;table_of_variable_uses;"
}
-- insert blank lines between lines belonging together
table.insert(formspec, table.concat(sorted_res, ",#FFFFFF,,#FFFFFF,,#FFFFFF,,#FFFFFF,,").."]")
if(#sorted_res > 0 and (count_read > 0 or count_changed > 0)) then if(count_read > 0 or count_changed > 0) then
table.insert(formspec, table.insert(formspec,
"label[16.0,31.0;The variable is accessed in ".. "label[16.0,31.0;The variable is accessed in "..
minetest.colorize("#FFFF00", tostring(count_read).." pre(C)onditions").. minetest.colorize("#FFFF00", tostring(count_read).." pre(C)onditions")..