From fc06ddc8ba43b0c2e2a124ccca8b0fbe4d870689 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sun, 25 Jul 2021 02:12:35 +0200 Subject: [PATCH] moved some helper functions from quest_api.lua to print_as_table.lua --- init.lua | 2 + print_as_table.lua | 114 ++++++++++++++++++++++++++++++++++++++++++++ quest_api.lua | 115 --------------------------------------------- 3 files changed, 116 insertions(+), 115 deletions(-) create mode 100644 print_as_table.lua diff --git a/init.lua b/init.lua index 8474c6c..66c6805 100644 --- a/init.lua +++ b/init.lua @@ -27,6 +27,8 @@ dofile(modpath .. "fs_save_or_discard_or_back.lua") -- of course you can change them only if you have access to -- the server's file system or can execute lua code. dofile(modpath .. "custrom_functions_you_can_override.lua") +-- some helper functions for formatting text for a formspec talbe +dofile(modpath .. "print_as_table.lua") -- handle alternate text for dialogs dofile(modpath .. "fs_alternate_text.lua") -- common functions for editing preconditions and effects diff --git a/print_as_table.lua b/print_as_table.lua new file mode 100644 index 0000000..19d71e9 --- /dev/null +++ b/print_as_table.lua @@ -0,0 +1,114 @@ +-- 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 = (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 minetest.formspec_escape(text) + end + local show_parts = {} + -- only show the first two lines (we don't have infinite room) + for i, p in ipairs(parts) do + if(i <= max_lines) then + table.insert(show_parts, minetest.formspec_escape(p)) + end + end + if(#parts > max_lines) then + return table.concat(show_parts, prefix)..minetest.formspec_escape(" [...]") + end + return table.concat(show_parts, prefix) +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 diff --git a/quest_api.lua b/quest_api.lua index e9d7d04..5e29f0a 100644 --- a/quest_api.lua +++ b/quest_api.lua @@ -400,121 +400,6 @@ yl_speak_up.get_npc_users_of_variable = function(var_name) 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 = (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 minetest.formspec_escape(text) - end - local show_parts = {} - -- only show the first two lines (we don't have infinite room) - for i, p in ipairs(parts) do - if(i <= max_lines) then - table.insert(show_parts, minetest.formspec_escape(p)) - end - end - if(#parts > max_lines) then - return table.concat(show_parts, prefix)..minetest.formspec_escape(" [...]") - end - return table.concat(show_parts, prefix) -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)