From 1e6e7e96d8fe786acc52b2bf4283d33a79b34671 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Mon, 23 May 2022 03:00:52 +0200 Subject: [PATCH] merge d_got_item for generic dialogs with existing ones of current npc --- add_generic_dialogs.lua | 81 ++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 21 deletions(-) diff --git a/add_generic_dialogs.lua b/add_generic_dialogs.lua index c6c6e06..1ddec0b 100644 --- a/add_generic_dialogs.lua +++ b/add_generic_dialogs.lua @@ -64,6 +64,42 @@ yl_speak_up.rewrite_dialog_id = function(data, field_name, start_dialog, append_ end +-- helper function for yl_speak_up.check_and_add_as_generic_dialog(..); +-- has to be called for all those dialogs where options from the generic +-- NPC need to be injected in a dialog from the special NPC (and not in +-- dialogs that are specific to the generic NPC) +yl_speak_up.generic_dialog_rewrite_options = function(n_id, dialog_name, append_str, anz_generic) + if(not(yl_speak_up.generic_dialogs[n_id]) + or not(yl_speak_up.generic_dialogs[n_id][dialog_name])) then + return + end + local options = yl_speak_up.generic_dialogs[n_id][dialog_name].d_options + if(not(options)) then + return + end + local new_options = {} + local anz_options = 0 + for o_id, o in pairs(options) do + -- o_sort needs to be set accordingly + local o_sort = tonumber(o.o_sort or "0") + if(not(o_sort) or o_sort < 0) then + o_sort = anz_options + end + -- make sure there is enough room for internal sorting + o.o_sort = o_sort + (anz_generic * 100000) + anz_options = anz_options + 1 + + -- adjust o_id + o.o_id = o_id..append_str + -- the options of the first dialog need to be renamed to r.r_id_n_id to avoid duplicates + new_options[o_id..append_str] = o + -- TODO: there may be preconditions refering this option which also might need changing + end + -- store the new options + yl_speak_up.generic_dialogs[n_id][dialog_name].d_options = new_options +end + + -- returns "OK" if the dialog can be used as a generic dialog, that is: -- * has at least one dialog yl_speak_up.check_and_add_as_generic_dialog = function(dialog, n_id) @@ -299,25 +335,11 @@ yl_speak_up.check_and_add_as_generic_dialog = function(dialog, n_id) if(not(options)) then return "There are no options/answers that might be injected/made generic." end - local new_options = {} - local anz_options = 0 - for o_id, o in pairs(options) do - -- o_sort needs to be set accordingly - local o_sort = tonumber(o.o_sort or "0") - if(not(o_sort) or o_sort < 0) then - o_sort = anz_options - end - -- make sure there is enough room for internal sorting - o.o_sort = o_sort + (anz_generic * 100000) - anz_options = anz_options + 1 + yl_speak_up.generic_dialog_rewrite_options(n_id, start_dialog, append_str, anz_generic) - -- adjust o_id - o.o_id = o_id..append_str - -- the options of the first dialog need to be renamed to r.r_id_n_id to avoid duplicates - new_options[o_id..append_str] = o - -- TODO: there may be preconditions refering this option which also might need changing - end - yl_speak_up.generic_dialogs[n_id][start_dialog].d_options = new_options + -- rename the options of d_got_item so that it can be used in combination with d_got_item + -- from other generic npc or the specific npc + yl_speak_up.generic_dialog_rewrite_options(n_id, "d_got_item", append_str, anz_generic) -- all fine - no error occoured, no error message to display; -- the NPC's dialog has been added @@ -360,8 +382,11 @@ yl_speak_up.add_generic_dialogs = function(dialog, current_n_id, player) for i, n_id in ipairs(n_id_list) do -- add the dialogs as such first for d_id, d in pairs(yl_speak_up.generic_dialogs[n_id]) do - -- no need to deep copy here - this is not added in edit mode - dialog.n_dialogs[d_id] = d + -- d_got_item needs to be combined + if(d_id ~= "d_got_item" and d_id ~= "d_got_item".."_"..tostring(n_id)) then + -- no need to deep copy here - this is not added in edit mode + dialog.n_dialogs[d_id] = d + end end -- add the options so that these new dialogs can be accessed local d = yl_speak_up.generic_dialogs[n_id]["d_generic_start_dialog"] @@ -378,8 +403,22 @@ yl_speak_up.add_generic_dialogs = function(dialog, current_n_id, player) dialog.n_dialogs[start_dialog_current].d_text.."\n"..d.d_text end + d = yl_speak_up.generic_dialogs[n_id]["d_got_item"] + -- add the options from d_got_item + if(not(dialog.n_dialogs["d_got_item"]) + or not(dialog.n_dialogs["d_got_item"].d_options)) then + -- if the current NPC doesn't accept any items: just copy them from the generic npc + dialog.n_dialogs["d_got_item"] = d + elseif(d and d.d_options) then + for o_id, o in pairs(d.d_options) do + -- actually insert the new options + -- note: the o_id needs to have been rewritten before this can be done! + dialog.n_dialogs["d_got_item"].d_options[o.o_id] = o + end + end + + end - -- TODO: deal with d_got_item return dialog end