From c324f5a8d9b278b62c545e46baebe85bb81a2b66 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sun, 25 Jul 2021 02:07:52 +0200 Subject: [PATCH] added yl_speak_up.print_as_table_* functions for two functions: show usage of a variable and show what points to this dialog --- fs_alternate_text.lua | 62 ++++++++++++++++ quest_api.lua | 169 +++++++++++++++++++++++++----------------- 2 files changed, 165 insertions(+), 66 deletions(-) diff --git a/fs_alternate_text.lua b/fs_alternate_text.lua index 938618c..59c5e38 100644 --- a/fs_alternate_text.lua +++ b/fs_alternate_text.lua @@ -351,24 +351,44 @@ yl_speak_up.show_what_points_to_this_dialog = function(player, pname, dialog, th end local found = {} + -- colored lines for the table showing the results + local res = {} -- iterate over all dialogs for d_id, d in pairs(dialog.n_dialogs) do -- we are looking for dialogs that point here - not the dialog itself if(d_id ~= this_dialog and d.d_options) then -- iterate over all options 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; -- effects are (dialog and on_failure) if(o.o_results) then for r_id, r in pairs(o.o_results) do if(r and r.r_type and r.r_type == "dialog" 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, "option was selected"}) elseif(r and r.r_type and r.r_type == "on_failure" 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, "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 @@ -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 if(a and a.a_on_failure 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, "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 + 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 + 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 = { "formspec_version[3]", "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])..",") end return table.concat(formspec, "").."]" +--]] end diff --git a/quest_api.lua b/quest_api.lua index 44a4fdb..e9d7d04 100644 --- a/quest_api.lua +++ b/quest_api.lua @@ -403,12 +403,12 @@ end -- helper function yl_speak_up.wrap_long_lines_for_table = function(text, prefix, line_length, max_lines) -- 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("
")) -- break the text up into lines of length x local parts = minetest.wrap_text(text, line_length, true) if(not(parts) or #parts < 2) then - return text + return minetest.formspec_escape(text) end local show_parts = {} -- 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 +-- 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 yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_preconditions, 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 if(p and p.p_type and p.p_type == "state" and p.p_variable and p.p_variable == var_name) then - p_text = p_text..",#FFFF00,".. - 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)) + p_text = p_text..yl_speak_up.print_as_table_precon(p,pname) sort_value = (p.p_var_cmp_value or 0) count_read = count_read + 1 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 if(r and r.r_type and r.r_type == "state" and r.r_variable and r.r_variable == var_name) then - r_text = r_text..",#55FF55,".. - 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)) + r_text = r_text..yl_speak_up.print_as_table_effect(r,pname) -- values set in the results are more important than -- those set in preconditions 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(o and o.actions and (p_text ~= "" or r_text ~= "")) then 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 -- directly after p_text, and p_text is finished - p_text = p_text..",FF9900,".. - minetest.formspec_escape(tostring(a_id)).. - ",#FF9900,(A)ction,#FF9900,".. - minetest.formspec_escape(a.a_type)..",#FF9900,".. - a_text + p_text = p_text..yl_speak_up.print_as_table_action(a, pname) end end - if(p_text ~= "" or r_text ~= "") then - 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) - 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 + yl_speak_up.print_as_table_dialog(p_text, r_text, dialog, + n_id, d_id, o_id, res, o, sort_value) end end end end end - 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).."]", + local formspec = yl_speak_up.print_as_table_prepare_formspec(res, "table_of_variable_uses", + back_button_name, back_button_text) + table.insert(formspec, "label[20.0,1.8;".. minetest.formspec_escape("Variable \"".. minetest.colorize("#FFFF00", tostring(var_name or "- ? -")).. - "\" 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,,").."]") + "\" is used here:").."]") - 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, "label[16.0,31.0;The variable is accessed in ".. minetest.colorize("#FFFF00", tostring(count_read).." pre(C)onditions")..