From 3c7ece2f77beb7d24b4fc7b0c10cb2aed97c367e Mon Sep 17 00:00:00 2001 From: Sokomine Date: Fri, 22 Apr 2022 03:45:52 +0200 Subject: [PATCH] preparations for add_generic_dialogs --- add_generic_dialogs.lua | 8 ++++++++ fs_fashion.lua | 2 +- fs_get_list_of_usage_of_variable.lua | 2 +- fs_save_or_discard_or_back.lua | 2 +- functions.lua | 26 +++++++++++++++++++------- init.lua | 2 ++ staff_based_editing.lua | 8 ++++---- 7 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 add_generic_dialogs.lua diff --git a/add_generic_dialogs.lua b/add_generic_dialogs.lua new file mode 100644 index 0000000..9fe1e25 --- /dev/null +++ b/add_generic_dialogs.lua @@ -0,0 +1,8 @@ + +yl_speak_up.add_generic_dialogs = function(dialog, n_id, player) + if(not(player)) then + return dialog + end + -- TODO: extend the dialog with generic dialog texts + return dialog +end diff --git a/fs_fashion.lua b/fs_fashion.lua index bf85c44..5f3f66c 100644 --- a/fs_fashion.lua +++ b/fs_fashion.lua @@ -356,7 +356,7 @@ function yl_speak_up.fashion(player, obj) yl_speak_up.speak_to[pname].textures = t yl_speak_up.speak_to[pname].skins = textures2skin(t) - local dialog = yl_speak_up.load_dialog(n_id) + local dialog = yl_speak_up.load_dialog(n_id, false) if next(dialog) then yl_speak_up.speak_to[pname].n_npc = dialog.n_npc else diff --git a/fs_get_list_of_usage_of_variable.lua b/fs_get_list_of_usage_of_variable.lua index c879439..6118a6b 100644 --- a/fs_get_list_of_usage_of_variable.lua +++ b/fs_get_list_of_usage_of_variable.lua @@ -19,7 +19,7 @@ yl_speak_up.fs_get_list_of_usage_of_variable = function(var_name, pname, check_p local count_changed = 0 for i, n_id in ipairs(npc_list) do -- the NPC may not even be loaded - local dialog = yl_speak_up.load_dialog(n_id) + local dialog = yl_speak_up.load_dialog(n_id, false) if(dialog and dialog.n_dialogs) then for d_id, d in pairs(dialog.n_dialogs) do if(d and d.d_options) then diff --git a/fs_save_or_discard_or_back.lua b/fs_save_or_discard_or_back.lua index c0f6688..c63877d 100644 --- a/fs_save_or_discard_or_back.lua +++ b/fs_save_or_discard_or_back.lua @@ -41,7 +41,7 @@ yl_speak_up.input_save_or_discard_changes = function(player, formname, fields) -- if we just reload the old state, they would both get lost local target_dialog_data = yl_speak_up.speak_to[pname].dialog.n_dialogs[ target_dialog ] -- actually restore the old state and discard the changes by loading the dialog anew - yl_speak_up.speak_to[pname].dialog = yl_speak_up.load_dialog(n_id) + yl_speak_up.speak_to[pname].dialog = yl_speak_up.load_dialog(n_id, false) -- clear list of changes yl_speak_up.npc_was_changed[ n_id ] = {} local dialog = yl_speak_up.speak_to[pname].dialog diff --git a/functions.lua b/functions.lua index 3dd8421..e4626f7 100644 --- a/functions.lua +++ b/functions.lua @@ -136,12 +136,13 @@ yl_speak_up.save_dialog = function(n_id, dialog) end -yl_speak_up.load_dialog = function(n_id) -- returns the saved dialog +-- if a player is supplied: include generic dialogs +yl_speak_up.load_dialog = function(n_id, player) -- returns the saved dialog local p = save_path(n_id) local file, err = io.open(p, "r") if err then - return {} + return yl_speak_up.add_generic_dialogs({}, n_id, player) end io.input(file) local content = io.read() @@ -152,13 +153,13 @@ yl_speak_up.load_dialog = function(n_id) -- returns the saved dialog dialog = {} end - return dialog + return yl_speak_up.add_generic_dialogs(dialog, n_id, player) end -- used by staff and input_inital_config yl_speak_up.fields_to_dialog = function(pname, fields) local n_id = yl_speak_up.speak_to[pname].n_id - local dialog = yl_speak_up.load_dialog(n_id) + local dialog = yl_speak_up.load_dialog(n_id, false) local save_d_id = "" if next(dialog) == nil then -- No file found. Let's create the basic values @@ -205,7 +206,7 @@ yl_speak_up.delete_dialog = function(n_id, d_id) return false end -- We don't delete "New dialog" - local dialog = yl_speak_up.load_dialog(n_id) + local dialog = yl_speak_up.load_dialog(n_id, false) dialog.n_dialogs[d_id] = nil @@ -352,7 +353,9 @@ end -- identify multiple results that lead to target dialogs yl_speak_up.check_for_disambigous_results = function(n_id, pname) local errors_found = false - local dialog = yl_speak_up.load_dialog(n_id) + -- this is only checked when trying to edit this npc; + -- let's stick to check the dialogs of this one without generic dialogs + local dialog = yl_speak_up.load_dialog(n_id, false) -- nothing defined yet - nothing to repair if(not(dialog.n_dialogs)) then return @@ -427,10 +430,19 @@ function yl_speak_up.talk(self, clicker) yl_speak_up.speak_to[pname] = {} yl_speak_up.speak_to[pname].n_id = n_id -- Memorize which player talks to which NPC - yl_speak_up.speak_to[pname].dialog = yl_speak_up.load_dialog(n_id) -- Load the dialog and see what we can do with it yl_speak_up.speak_to[pname].textures = self.yl_speak_up.textures yl_speak_up.speak_to[pname].option_index = 1 + -- the object itself may be needed in load_dialog for adding generic dialogs yl_speak_up.speak_to[pname].obj = self.object + -- Load the dialog and see what we can do with it + -- this inculdes generic dialog parts; + -- make sure this is never true in edit mode (because in edit mode we want to + -- edit this particular NPC without generic parts) + local player = clicker + if(yl_speak_up.edit_mode[pname] == n_id) then + player = nil + end + yl_speak_up.speak_to[pname].dialog = yl_speak_up.load_dialog(n_id, clicker) -- is this player explicitly allowed to edit this npc? if(yl_speak_up.speak_to[pname].dialog diff --git a/init.lua b/init.lua index 486b86c..75e4884 100644 --- a/init.lua +++ b/init.lua @@ -28,6 +28,8 @@ yl_speak_up.custom_server_functions = {} dofile(modpath .. "config.lua") dofile(modpath .. "privs.lua") +-- add generic dialogs +dofile(modpath .. "add_generic_dialogs.lua") -- react to right-click etc. dofile(modpath .. "interface_mobs_api.lua") -- handle on_player_receive_fields and showing of formspecs diff --git a/staff_based_editing.lua b/staff_based_editing.lua index 51ed103..8ba0dea 100644 --- a/staff_based_editing.lua +++ b/staff_based_editing.lua @@ -142,7 +142,7 @@ local function delete_option(n_id, d_id, o_id) return false end -- We don't delete "New option" - local dialog = yl_speak_up.load_dialog(n_id) + local dialog = yl_speak_up.load_dialog(n_id, false) dialog.n_dialogs[d_id].d_options[o_id] = nil @@ -159,7 +159,7 @@ end -- dialog yl_speak_up.get_fs_setdialog = function(clicker, n_id, d_id) - local dialog = yl_speak_up.load_dialog(n_id) + local dialog = yl_speak_up.load_dialog(n_id, false) local items = yl_speak_up.text_new_dialog_id local count = 1 @@ -236,7 +236,7 @@ end -- options yl_speak_up.get_fs_optiondialog = function(player, n_id, d_id, o_id, p_id, r_id) - local dialog = yl_speak_up.load_dialog(n_id) + local dialog = yl_speak_up.load_dialog(n_id, false) local pname = player:get_player_name() if next(dialog) == nil or d_id == yl_speak_up.text_new_dialog_id or dialog.n_dialogs == nil then -- file does not exist or the user sent the New dialog @@ -756,7 +756,7 @@ function yl_speak_up.config(clicker, npc) yl_speak_up.speak_to[pname] = {} yl_speak_up.speak_to[pname].n_id = n_id -- Memorize which player talks to which NPC yl_speak_up.speak_to[pname].d_id = yl_speak_up.text_new_dialog_id -- The only d_id we can rely on existing - yl_speak_up.speak_to[pname].dialog = yl_speak_up.load_dialog(n_id) -- Load the dialog and see what we can do with it + yl_speak_up.speak_to[pname].dialog = yl_speak_up.load_dialog(n_id, false) -- Load the dialog and see what we can do with it yl_speak_up.speak_to[pname].obj = npc -- find out who owns the npc while we still have easy access to the luaentity