fixed bug with discarding new dialog

This commit is contained in:
Sokomine 2021-05-14 14:55:42 +02:00
parent 0184fe6337
commit 26e3655c10

View File

@ -1980,8 +1980,13 @@ minetest.register_on_player_receive_fields(
-- talk
-- helper function
yl_speak_up.add_new_dialog = function(dialog, pname)
local next_id = find_next_id(dialog.n_dialogs)
-- the option to override next_id and provide a value is needed when a new dialog was
-- added, then edited, and then discarded; it's still needed after that, but has to
-- be reset to empty state (wasn't stored before)
yl_speak_up.add_new_dialog = function(dialog, pname, next_id)
if(not(next_id)) then
next_id = find_next_id(dialog.n_dialogs)
end
local future_d_id = "d_" .. next_id
-- Initialize empty dialog
dialog.n_dialogs[future_d_id] = {
@ -2031,15 +2036,32 @@ yl_speak_up.save_changes_and_switch_to_other_dialog = function(player, fields, t
-- discard changes and continue on to the next dialog
elseif(edit_mode and fields.discard_dialog_changes) then
-- the current dialog and the one we want to show next may both be new dialogs;
-- 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 = load_dialog(n_id)
-- clear list of changes
yl_speak_up.npc_was_changed[ n_id ] = {}
-- it is possible that the target dialog was newly added - and just discarded;
-- in such a case stick to the old dialog
local dialog = yl_speak_up.speak_to[pname].dialog
if(not(dialog[target_dialog])) then
target_dialog = yl_speak_up.speak_to[pname].d_id
-- do we have to save again after restoring current and target dialog?
local need_to_save = false
-- if the current dialog was a new one, it will be gone now - restore it
if(not(dialog.n_dialogs[ d_id ])) then
-- we can't just restore the current dialog - after all the player wanted
-- to discard the changes; but we can recreate the current dialog so that it
-- is in the "new dialog" state again
local next_id = tonumber(string.sub( d_id, 3))
yl_speak_up.add_new_dialog(dialog, pname, next_id)
need_to_save = true
end
if(not(dialog.n_dialogs[ target_dialog ])) then
-- restore the new target dialog
dialog.n_dialogs[ target_dialog ] = target_dialog_data
need_to_save = true
end
if(need_to_save) then
save_dialog(n_id, dialog)
end
end
@ -2326,7 +2348,7 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields)
-- this may also point to a new dialog
if(fields[ "d_id_"..k ] == 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)
fields[ "d_id_"..k ] = yl_speak_up.add_new_dialog(dialog, pname)
fields[ "d_id_"..k ] = yl_speak_up.add_new_dialog(dialog, pname, nil)
end
-- store that there have been changes to this npc
table.insert(yl_speak_up.npc_was_changed[ n_id ],
@ -2345,7 +2367,7 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields)
-- this may also point to a new dialog
if(fields[ "d_id_"..k ] == 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)
fields[ "d_id_"..k ] = yl_speak_up.add_new_dialog(dialog, pname)
fields[ "d_id_"..k ] = yl_speak_up.add_new_dialog(dialog, pname, nil)
end
-- store that a new option has been added to this dialog
table.insert(yl_speak_up.npc_was_changed[ n_id ],
@ -2375,7 +2397,7 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields)
if(fields.show_new_dialog
or(fields["d_id"] and fields["d_id"] == yl_speak_up.text_new_dialog_id)) then
-- create the new dialog and make sure it gets shown
local d_id = yl_speak_up.add_new_dialog(dialog, pname)
local d_id = yl_speak_up.add_new_dialog(dialog, pname, nil)
-- actually show the new dialog
fields["d_id"] = d_id
fields["show_new_dialog"] = nil
@ -2698,7 +2720,7 @@ minetest.register_on_player_receive_fields(
-- if in edit mode: detect if something was changed;
if(edit_mode or fields.button_edit_name_and_description) then
yl_speak_up.edit_mode_apply_changes(pname, fields)
result = yl_speak_up.edit_mode_apply_changes(pname, fields)
end
-- the player wants to change name and description; show the formspec
@ -3580,6 +3602,5 @@ yl_speak_up.replace_vars_in_text = function(text, dialog, pname)
text = string.gsub(text, "%$GOOD_DAY%$", "Good "..day_time_name)
text = string.gsub(text, "%$good_DAY%$", "good "..day_time_name)
minetest.chat_send_player("singleplayer","replaced text: "..tostring(text))
return text
end