better setup of d_got_item when adding a new option; changed text displayed in edit mode for d_got_item in order to provide more help

This commit is contained in:
Sokomine 2022-10-17 06:55:45 +02:00
parent f2f8195285
commit e91dc93b11
3 changed files with 90 additions and 19 deletions

View File

@ -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

View File

@ -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;"..
"<normal>Note:\nThis is a special dialog."..
"It will be called when the player clicks on "..
"<b>I want to give you something</b>."..
"\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 "..
"<b>precondition</b> so that the <i>right item</i> is accepted, and then "..
"set the <b>target dialog</b> <i>according to your needs</i>. Please also "..
"edit the <b>alternate text</b> so that it fits your <i>item</i>!"..
"\nThis is how it works in detail:"..
"\n<b>Each option</b> you add here ought to deal with one item(stack) that "..
"the NPC expects from the player, i.e. <i>farming:bread 2</i>. "..
"Each option needs to be selected <b>automaticly</b> and ought to contain:"..
"\n* a <b>precondition</b> regarding "..
"<b>an item the player offered/gave to the NPC</b> "..
"(shown as <b>player_offered_item</b> in overview) "..
"where you define which item(stack) is relevant for this option"..
"\n* an <b>effect</b> regarding <b>an item the player offered to the NPC</b> "..
"(shown as <b>deal_with_offered_item</b> 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 <b>effect</b>! "..
"Your NPC ought to comment on what he got, i.e. "..
"<i>Thank you for those two breads! You saved me from starving.</i>"..
"You can also work with an alternate text here (as is done in the "..
"default setup when adding a new option here)."..
"\n</normal>]")
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 "?")..
"]")

View File

@ -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