From 6381ea59b916d32c9c210f2f3f3c24c0068e2be4 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Thu, 29 Feb 2024 12:18:23 +0100 Subject: [PATCH] count visits to dialogs --- api/api_decorated.lua | 1 + api/api_talk.lua | 18 ++++++++++++++++++ editor/edit_mode.lua | 10 ++++++++++ editor/fs/fs_talkdialog_edit_mode.lua | 8 ++++---- fs/fs_talkdialog.lua | 3 +++ 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/api/api_decorated.lua b/api/api_decorated.lua index 36286a6..b2299d0 100644 --- a/api/api_decorated.lua +++ b/api/api_decorated.lua @@ -191,6 +191,7 @@ yl_speak_up.show_fs_npc_text = function(pname, formspec, dialog, alternate_text, -- replace $NPC_NAME$ etc. local t = minetest.formspec_escape(yl_speak_up.replace_vars_in_text( alternate_text, dialog, pname)) + -- t = "Visits to this dialog: "..tostring(active_dialog.visits).."\n"..t if(fs_version > 2) then yl_speak_up.add_formspec_element_with_tooltip_if(formspec, "hypertext", "0.2,5;19.6,17.8", "d_text", diff --git a/api/api_talk.lua b/api/api_talk.lua index b92db08..a952d4d 100644 --- a/api/api_talk.lua +++ b/api/api_talk.lua @@ -30,3 +30,21 @@ yl_speak_up.get_start_dialog_id = function(dialog) end +-- count visits to this dialog - but *not* for generic dialogs as those are just linked and not +-- copied for each player; also not in edit_mode as it makes no sense there +yl_speak_up.count_visits_to_dialog = function(pname) + if(not(pname)) then + return + end + local d_id = yl_speak_up.speak_to[pname].d_id + local dialog = yl_speak_up.speak_to[pname].dialog + if(not(d_id) or not(dialog) or not(dialog.n_dialogs) or not(dialog.n_dialogs[d_id])) then + return + end + if(not(dialog.n_dialogs[d_id].is_generic)) then + if(not(dialog.n_dialogs[d_id].visits)) then + dialog.n_dialogs[d_id].visits = 0 + end + dialog.n_dialogs[d_id].visits = dialog.n_dialogs[d_id].visits + 1 + end +end diff --git a/editor/edit_mode.lua b/editor/edit_mode.lua index 5216159..c0e5306 100644 --- a/editor/edit_mode.lua +++ b/editor/edit_mode.lua @@ -45,6 +45,16 @@ yl_speak_up.load_dialog = function(n_id, player) -- returns the saved dialog end +-- in edit mode the dialog may be saved. visits to a particular dialog are of no intrest here +local old_count_visits_to_dialog = yl_speak_up.count_visits_to_dialog +yl_speak_up.count_visits_to_dialog = function(pname) + if(yl_speak_up.in_edit_mode(pname)) then + return + end + return old_count_visits_to_dialog(pname) +end + + local modname = minetest.get_current_modname() if(not(modname)) then modname = "yl_speak_up" diff --git a/editor/fs/fs_talkdialog_edit_mode.lua b/editor/fs/fs_talkdialog_edit_mode.lua index 9738340..b6907b6 100644 --- a/editor/fs/fs_talkdialog_edit_mode.lua +++ b/editor/fs/fs_talkdialog_edit_mode.lua @@ -201,11 +201,11 @@ end -- in edit mode, *all* options are displayed local old_calculate_displayable_options = yl_speak_up.calculate_displayable_options yl_speak_up.calculate_displayable_options = function(pname, d_options, allow_recursion) - -- no options - nothing to do - if(not(d_options)) then - return {} - end if(yl_speak_up.in_edit_mode(pname)) then + -- no options - nothing to do + if(not(d_options)) then + return {} + end -- in edit mode: show all options (but sort them first) local retval = {} local sorted_list = yl_speak_up.get_sorted_options(d_options, "o_sort") diff --git a/fs/fs_talkdialog.lua b/fs/fs_talkdialog.lua index 55c8ddb..b3554fe 100644 --- a/fs/fs_talkdialog.lua +++ b/fs/fs_talkdialog.lua @@ -477,6 +477,9 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec "label[0.2,0.5;Ups! Something went wrong. Please try again.]" end + -- how often has the player visted this dialog? + yl_speak_up.count_visits_to_dialog(pname) + -- evaluate the preconditions of each option and check if the option can be offered local allowed = yl_speak_up.calculate_displayable_options(pname, active_dialog.d_options, -- avoid loops by limiting max recoursion depths for autoanswers