diff --git a/edit_mode_apply_changes.lua b/edit_mode_apply_changes.lua index 552fa10..724d55c 100644 --- a/edit_mode_apply_changes.lua +++ b/edit_mode_apply_changes.lua @@ -3,23 +3,27 @@ -- helper function for yl_speak_up.edit_mode_apply_changes; -- makes sure the new dialog (and a result/effect "dialog" for each option) exist yl_speak_up.prepare_new_dialog_for_option = function(dialog, pname, n_id, d_id, o_id,target_dialog,o_results) - -- is there a result/effect of the type "dialog" already? else use a fallback - local result = {r_value = "-default-"} - if(o_results) then - for kr, vr in pairs(o_results) do - if( vr.r_type == "dialog" ) then - result = vr - end - end - end -- this may also point to a new dialog if(target_dialog == yl_speak_up.text_new_dialog_id) then -- create a new dialog and show it as new target dialog - but do not display -- this dialog directly (the player may follow the -> button) target_dialog = yl_speak_up.add_new_dialog(dialog, pname, nil) - elseif( result.r_value and result.r_value == target_dialog) then - -- no problem - the right dialog is set already - return target_dialog + end + -- is there a result/effect of the type "dialog" already? else use a fallback + local result = {} --{r_value = "-default-"} + if(o_results) then + for kr, vr in pairs(o_results) do + if( vr.r_type == "dialog" ) then + result = vr + -- no problem - the right dialog is set already + if(result.r_value and result.r_value == target_dialog) then + return target_dialog + else + -- no need to search any further + break + end + end + end end -- store that a new option has been added to this dialog table.insert(yl_speak_up.npc_was_changed[ n_id ], @@ -147,10 +151,8 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields) " options/answers are allowed per dialog.") fields.add_option = nil else - -- give this new dialog a dialog result that leads back to this dialog - -- (which is more helpful than creating tons of empty dialogs) - local new_target_dialog = yl_speak_up.prepare_new_dialog_for_option( - dialog, pname, n_id, d_id, future_o_id, d_id, nil) + -- add_new_option has added a dialog result for us already - no need to do that again + -- if this is selected in the options edit menu, we want to move straight on to the new option result["show_next_option"] = future_o_id end diff --git a/fs_talkdialog.lua b/fs_talkdialog.lua index 4ff4f0d..bf8e67a 100644 --- a/fs_talkdialog.lua +++ b/fs_talkdialog.lua @@ -381,7 +381,36 @@ yl_speak_up.get_fs_talkdialog_main_text_in_edit_mode = function( "Save this dialog.", true) - if(active_dialog and active_dialog.d_text) then + -- 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.".. + "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 ".. + "new option to this dialog. In most cases you may just have to edit the ".. + "precondition so that the right item is accepted, and then ".. + "set the target dialog according to your needs. Please also ".. + "edit the alternate text so that it fits your item!".. + "\nThis is how it works in detail:".. + "\nEach option you add here ought to deal with one item(stack) that ".. + "the NPC expects from the player, i.e. farming:bread 2. ".. + "Each option needs to be selected automaticly and ought to contain:".. + "\n* a precondition regarding ".. + "an item the player offered/gave to the NPC ".. + "(shown as player_offered_item in overview) ".. + "where you define which item(stack) is relevant for this option".. + "\n* an effect regarding an item the player offered to the NPC ".. + "(shown as deal_with_offered_item in overview) ".. + "where you define what shall happen to the offered item. Usually ".. + "the NPC will accept the item and put it into its inventory.".. + "\n* Don't forget to set a suitable target dialog for the effect! ".. + "Your NPC ought to comment on what he got, i.e. ".. + "Thank you for those two breads! You saved me from starving.".. + "You can also work with an alternate text here (as is done in the ".. + "default setup when adding a new option here).".. + "\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 0d42674..cb54490 100644 --- a/functions.lua +++ b/functions.lua @@ -281,9 +281,22 @@ yl_speak_up.add_new_option = function(dialog, pname, next_id, d_id, option_text, "Dialog "..d_id..": Added new option/answer "..future_o_id..".") end - -- create a fitting dialog result automaticly if possible + -- letting d_got_item point back to itself is not a good idea because the + -- NPC will then end up in a loop; plus the d_got_item dialog is intended for + -- automatic processing, not for showing to the player + if(d_id == "d_got_item") then + -- unless the player specifies something better, we go back to the start dialog + -- (that is where d_got_item got called from anyway) + target_dialog = yl_speak_up.get_start_dialog_id(dialog) + -- ...and this option needs to be selected automaticly + dialog.n_dialogs[d_id].d_options[future_o_id].o_autoanswer = 1 + end + local future_r_id = nil + -- create a fitting dialog result automaticly if possible: + -- give this new dialog a dialog result that leads back to this dialog + -- (which is more helpful than creating tons of empty dialogs) if(target_dialog and (dialog.n_dialogs[target_dialog] or target_dialog == "d_end")) then - local future_r_id = yl_speak_up.add_new_result(dialog, d_id, future_o_id) + future_r_id = yl_speak_up.add_new_result(dialog, d_id, future_o_id) -- actually store the new result dialog.n_dialogs[d_id].d_options[future_o_id].o_results = {} dialog.n_dialogs[d_id].d_options[future_o_id].o_results[future_r_id] = { @@ -291,6 +304,33 @@ yl_speak_up.add_new_option = function(dialog, pname, next_id, d_id, option_text, r_type = "dialog", r_value = target_dialog} end + + -- the d_got_item dialog is special; players can easily forget to add the + -- necessary preconditions and effects, so we do that manually here + if(d_id == "d_got_item") then + -- we also need a precondition so that the o_autoanswer can actually get called + dialog.n_dialogs[d_id].d_options[future_o_id].o_prerequisites = {} + -- we just added this option; this is the first and for now only precondition for it; + -- the player still has to adjust it, but at least it is a reasonable default + dialog.n_dialogs[d_id].d_options[future_o_id].o_prerequisites["p_1"] = { + p_id = "p_1", + p_type = "player_offered_item", + p_item_stack_size = tostring(next_id), + p_match_stack_size = "exactly", + -- this is just a simple example item and ought to be changed after adding + p_value = "default:stick "..tostring(next_id)} + -- we need to show the player that his action was successful + dialog.n_dialogs[d_id].d_options[future_o_id].o_results[future_r_id].alternate_text = + "Thank you for the "..tostring(next_id).." stick(s)! ".. + "Never can't have enough sticks.\n$TEXT$" + -- we need an effect for accepting the item; + -- taking all that was offered and putting it into the NPC's inventory is a good default + future_r_id = yl_speak_up.add_new_result(dialog, d_id, future_o_id) + dialog.n_dialogs[d_id].d_options[future_o_id].o_results[future_r_id] = { + r_id = future_r_id, + r_type = "deal_with_offered_item", + r_value = "take_all"} + end return future_o_id end