From 63653d4dbdbfd168136626fdf3b1ffc4fcf43960 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sat, 8 May 2021 20:46:27 +0200 Subject: [PATCH] allow to delete empty dialogs --- functions.lua | 67 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/functions.lua b/functions.lua index 7ce73c0..b6c227c 100644 --- a/functions.lua +++ b/functions.lua @@ -12,6 +12,7 @@ yl_sokomine["q_apple_explorer"] = {} -- -- TODO: allow owner to mute/unmute npc (would be bad if players can already see what is going -- to happen while the owner creates a long quest) +-- TODO: log changes to the npc in the server logfile --### -- Init @@ -1238,7 +1239,6 @@ local function get_fs_talkdialog(player, n_id, d_id) local d_id_to_dropdown_index = {} -- display the window with the text the NPC is saying - -- TODO: make name of dialog and sort option editable? if(edit_mode and dialog and dialog.n_dialogs) then -- sort all dialogs by d_sort local sorted_list = yl_speak_up.get_sorted_options(dialog.n_dialogs, "d_sort") @@ -1290,11 +1290,14 @@ local function get_fs_talkdialog(player, n_id, d_id) table.insert(formspec, "scroll_container[0,24;56,7;scr0;vertical;1]") h = -0.8 + -- allow to delete entries that have no options later on + local has_at_least_one_option = false -- Let#s sort the options by o_sort if active_dialog ~= nil and active_dialog.d_options ~= nil then local sorted_buttons = {} for _, ad_v in pairs(active_dialog.d_options) do sorted_buttons[tonumber(ad_v.o_sort)] = ad_v + has_at_least_one_option = true end for _, sb_v in pairs(sorted_buttons) do @@ -1439,6 +1442,24 @@ local function get_fs_talkdialog(player, n_id, d_id) table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;add_option;]") table.insert(formspec, "tooltip[add_option;Adds a new option to this dialog. You can delete options via the option edit menu.]") table.insert(formspec, "label[0.7,"..(h+0.45)..";Add a new option/answer to this dialog. You can delete options via the option edit menu.]") + + -- chat option: "Delete this dialog." + h = h + 1 + if(active_dialog + and active_dialog.d_text == "" + and not(has_at_least_one_option)) then + table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;delete_this_empty_dialog;]") + table.insert(formspec, "tooltip[delete_this_empty_dialog;Dialogs can only be deleted ".. + "when they are empty and have no more options/answers. This is the case here, ".. + "so the dialog can be deleted.]") + table.insert(formspec, "label[0.7,"..(h+0.45)..";Delete this empty dialog.]") + -- (but only show this option if the dialog is empty) + else + table.insert(formspec, "box[0.5,"..h..";53.8,0.9;#BBBBBB]") + table.insert(formspec, "label[0.7,"..(h+0.45)..";If you want to delete this dialog, you ".. + "need to delete all options and its text first.]") + end + -- chat option: "Make this dialog the first one shown when starting to talk." h = h + 1 if(active_dialog and active_dialog.d_sort and tonumber(active_dialog.d_sort) ~= 0) then @@ -2127,7 +2148,6 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields) "\" to \""..tostring(fields.d_text).."\".") -- actually change the text - but do not save to disk yet dialog.n_dialogs[ d_id ].d_text = fields.d_text - -- TODO: option to delete dialogs (when text empty and no options present?) end -- add a new option/answer @@ -2295,6 +2315,49 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields) fields["show_new_dialog"] = nil end + -- delete one empty dialog + if(fields.delete_this_empty_dialog) then + local anz_options = 0 + -- we need to show a new dialog after this one was deleted + local new_dialog = d_id + local sorted_list = yl_speak_up.get_sorted_options(dialog.n_dialogs, "d_sort") + for i, k in ipairs(sorted_list) do + -- count the options of this dialog + if(k == d_id) then + if(dialog.n_dialogs[d_id].d_options) then + for o, w in pairs(dialog.n_dialogs[d_id].d_options) do + anz_options = anz_options + 1 + end + end + if(sorted_list[i+1]) then + new_dialog = sorted_list[i+1] + elseif(sorted_list[i-1]) then + new_dialog = sorted_list[i-1] + end + end + end + -- there needs to be one dialog left after deleting this one, + if(#sorted_list > 1 + -- this dialog isn't allowed to hold any more options/answers + and anz_options == 0 + -- we really found a new dialog to show + and new_dialog ~= d_id + -- and the text needs to be empty + and dialog.n_dialogs[ d_id ].d_text == "") then + -- actually delete this dialog + dialog.n_dialogs[ d_id ] = nil + -- ..and store it to disk + delete_dialog(n_id, d_id) + -- switch to another dialog (this one was deleted after all) + fields["d_id"] = new_dialog + fields["show_new_dialog"] = nil + else + minetest.chat_send_player(pname, "Sorry. This dialog cannot be deleted (yet). ".. + "It is either the only dialog left or has a non-empty text or has at ".. + "least on remaining option/answer.") + end + end + -- not in options edit menu? local o_id = fields.o_id if(not(o_id)) then