diff --git a/add_generic_dialogs.lua b/add_generic_dialogs.lua index 5a3a83e..0ac167d 100644 --- a/add_generic_dialogs.lua +++ b/add_generic_dialogs.lua @@ -172,6 +172,8 @@ end yl_speak_up.check_and_add_as_generic_dialog = function(dialog, n_id) yl_speak_up.generic_dialogs[n_id] = nil yl_speak_up.generic_dialog_conditions[n_id] = nil + -- we do *not* want d_dynamic in generic dialogs (each NPC will get its own anyway): + dialog.n_dialogs["d_dynamic"] = nil -- get the start dialog local d_id = yl_speak_up.get_start_dialog_id(dialog) if(not(d_id) @@ -443,6 +445,19 @@ end yl_speak_up.add_generic_dialogs = function(dialog, current_n_id, player) dialog = yl_speak_up.strip_generic_dialogs(dialog) + -- make sure we can add d_dynamic dialog: + if(not(dialog)) then + dialog = {} + end + if(not(dialog.n_dialogs)) then + dialog.n_dialogs = {} + end + -- make sure the dynamic dialog exists (as an empty dialog): + -- (initial_dialog looks for dialog.n_npc in order to determine if it's a new npc; + -- so we are safe here with an initialized dialog) + dialog.n_dialogs["d_dynamic"] = {} + dialog.n_dialogs["d_dynamic"].d_options = {} + if(not(player) or not(current_n_id)) then return dialog end @@ -453,12 +468,6 @@ yl_speak_up.add_generic_dialogs = function(dialog, current_n_id, player) start_dialog_current = "d_1" end -- unconfigured NPC are in special need of generic dialogs - if(not(dialog)) then - dialog = {} - end - if(not(dialog.n_dialogs)) then - dialog.n_dialogs = {} - end if(not(dialog.n_dialogs[start_dialog_current])) then dialog.n_dialogs[start_dialog_current] = {} end diff --git a/editor/edit_mode_apply_changes.lua b/editor/edit_mode_apply_changes.lua index 0b1628d..e8c0d6f 100644 --- a/editor/edit_mode_apply_changes.lua +++ b/editor/edit_mode_apply_changes.lua @@ -92,6 +92,11 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields) yl_speak_up.set_muted(pname, obj, false) end + -- changes to d_dynamic are *not* changed (the content of that dialog has to be provided + -- dynamicly by a function): + if(d_id == "d_dynamic") then + return + end -- new options etc. may be added; store these IDs so that we can switch to the right target local result = {} diff --git a/editor/fs/fs_save_or_discard_or_back.lua b/editor/fs/fs_save_or_discard_or_back.lua index abba4b5..26736a5 100644 --- a/editor/fs/fs_save_or_discard_or_back.lua +++ b/editor/fs/fs_save_or_discard_or_back.lua @@ -34,6 +34,10 @@ yl_speak_up.input_save_or_discard_changes = function(player, formname, fields) end -- clear list of changes yl_speak_up.npc_was_changed[ n_id ] = {} + -- save_dialog removed d_dynamic (because that is never to be saved!); we have + -- to add d_dynamic back so that we can use it as a target dialog in further editing: + yl_speak_up.speak_to[pname].dialog.n_dialogs["d_dynamic"] = {} + yl_speak_up.speak_to[pname].dialog.n_dialogs["d_dynamic"].d_options = {} -- discard changes and continue on to the next dialog elseif(edit_mode and fields.discard_dialog_changes) then diff --git a/editor/fs/fs_talkdialog_edit_mode.lua b/editor/fs/fs_talkdialog_edit_mode.lua index b826aa2..577e818 100644 --- a/editor/fs/fs_talkdialog_edit_mode.lua +++ b/editor/fs/fs_talkdialog_edit_mode.lua @@ -308,7 +308,7 @@ yl_speak_up.get_fs_talkdialog_main_text = function(pname, formspec, h, dialog, d -- static help text instead of text input field for d_got_item if(c_d_id == "d_got_item") then table.insert(formspec, "hypertext[0.2,5;19.6,17.8;d_text;".. - "Note:\nThis is a special dialog.".. + "Note:\nThis is a special dialog. ".. "It will be called when the player clicks on ".. "I want to give you something.".. "\nMost of the things listed below will be added automaticly when you add a ".. @@ -337,7 +337,7 @@ yl_speak_up.get_fs_talkdialog_main_text = function(pname, formspec, h, dialog, d -- static help text instead of text input field for d_trade elseif(c_d_id == "d_trade") then table.insert(formspec, "hypertext[0.2,5;19.6,17.8;d_text;".. - "Note:\nThis is a special dialog.".. + "Note:\nThis is a special dialog. ".. "It will be called when the player clicks on ".. "Let's trade!.".. "\nSome of the things listed below will be added automaticly when you add a ".. @@ -367,6 +367,21 @@ yl_speak_up.get_fs_talkdialog_main_text = function(pname, formspec, h, dialog, d "executing the effect(s), the trade list view will be shown to the ".. "player.".. "\n]") + elseif(c_d_id == "d_dynamic") then + table.insert(formspec, "hypertext[0.2,5;19.6,17.8;d_text;".. + "Note:\nThis is a special dialog. ".. + "Each time right before this special dialog is displayed, a ".. + "function is called that can fill the d_dynamic dialog ".. + "with text and options.".. + "\nThat function has to decide based on NPC, player and context what ".. + "it wants to display this time.".. + "\nThe d_dynamic dialog is never saved as part of the dialog. ".. + "It has to be dynamicly created by your function each time it is needed.".. + "\nThe d_dynamic dialog will always be available as a legitimate target ".. + "dialog of a dialog option. Its options can do all that options of ".. + "other dialogs can do. Its options they can lead back to normal static ".. + "parts of the dialog.".. + "\n]") elseif(active_dialog and active_dialog.d_text) then table.insert(formspec, "textarea[0.2,5;19.6,17.8;d_text;;".. minetest.formspec_escape(active_dialog.d_text or "?").. diff --git a/functions.lua b/functions.lua index 73b35fc..101930f 100644 --- a/functions.lua +++ b/functions.lua @@ -33,7 +33,7 @@ yl_speak_up.debug = true --### yl_speak_up.get_number_from_id = function(any_id) - if(not(any_id) or any_id == "d_got_item" or any_id == "d_end") then + if(not(any_id) or any_id == "d_got_item" or any_id == "d_end" or any_id == "d_dynamic") then return "0" end return string.split(any_id, "_")[2] @@ -102,6 +102,10 @@ yl_speak_up.save_dialog = function(n_id, dialog) yl_speak_up.update_stored_npc_data(n_id, dialog) -- make sure we never store any automaticly added generic dialogs dialog = yl_speak_up.strip_generic_dialogs(dialog) + -- never store d_dynamic dialogs + if(dialog.n_dialogs and dialog.n_dialogs["d_dynamic"]) then + dialog.n_dialogs["d_dynamic"] = nil + end local content = minetest.write_json(dialog) return minetest.safe_file_write(p, content) end @@ -111,6 +115,7 @@ end yl_speak_up.load_dialog = function(n_id, player) -- returns the saved dialog local p = save_path(n_id) + -- note: add_generic_dialogs will also add an empty d_dynamic dialog local file, err = io.open(p, "r") if err then return yl_speak_up.add_generic_dialogs({}, n_id, player) @@ -561,6 +566,7 @@ function yl_speak_up.talk(self, clicker) self = yl_speak_up.initialize_npc(self) end + -- TODO: load inventory only when the npc actually uses one? -- create a detached inventory for the npc and load its inventory yl_speak_up.load_npc_inventory(id_prefix.."_"..tostring(self.yl_speak_up.id))