diff --git a/functions_dialogs.lua b/functions_dialogs.lua index 77cb17e..6572cf5 100644 --- a/functions_dialogs.lua +++ b/functions_dialogs.lua @@ -152,13 +152,16 @@ end -- update existing or create a new dialog named d_name with d_text -- (useful for import from ink and likewise functionality) +-- this also prepares the dialog for options update yl_speak_up.update_dialog = function(log, dialog, dialog_name, dialog_text) -- does a dialog with name d_name already exist? local d_id = yl_speak_up.d_name_to_d_id(dialog, dialog_name) -- name the thing for logging purposes local log_str = "Dialog "..tostring(d_id) if(dialog_name and dialog_name ~= d_id) then - log_str = log_str.." ["..tostring(dialog_name).."]: " + log_str = log_str.." ["..tostring(dialog_name).."]:" + else + log_str = log_str..": " end local is_new = false if(not(d_id)) then @@ -174,32 +177,83 @@ yl_speak_up.update_dialog = function(log, dialog, dialog_name, dialog_text) -- we got a new name for the log log_str = "New dialog "..tostring(d_id).." ["..tostring(dialog_name).."]: " is_new = true - table.insert(log, log_str.."Created successfully.") + table.insert(log, log_str.." Created successfully.") elseif(dialog.n_dialogs[d_id].d_text ~= dialog_text) then -- else update the text - table.insert(log, log_str.."Changed dialog text from \"".. + table.insert(log, log_str.." Changed dialog text from \"".. tostring(dialog.n_dialogs[d_id].d_text).."\" to \""..tostring(dialog_text).."\".") -- actually change the dialog text dialog.n_dialogs[d_id].d_text = dialog_text end + local d_data = dialog.n_dialogs[d_id] -- set d_name if it differs from d_id if(d_id ~= dialog_name - and (not(dialog.n_dialogs[d_id].d_name) - or(dialog.n_dialogs[d_id].d_name ~= dialog_name))) then + and (not(d_data.d_name) + or(d_data.d_name ~= dialog_name))) then if(not(is_new)) then -- log only if it's not a new dialog table.insert(log, log_str.."Changed dialog name from \"".. - tostring(dialog.n_dialogs[d_id].d_name).."\" to \""..tostring(dialog_name).."\".") + tostring(d_data.d_name).."\" to \""..tostring(dialog_name).."\".") end -- actually change the dialog name - dialog.n_dialogs[d_id].d_name = dialog_name + d_data.d_name = dialog_name + end + + -- the random option is set for the dialog entire; we will have to process the individual + -- options in order to find out if this dialog is o_random; the first option that is sets + -- it for the dialog + d_data.o_random = nil + + -- there may be existing options that won't get updated; deal with them: + -- remember which options the dialog has and which sort order they had + d_data.d_tmp_sorted_option_list = yl_speak_up.get_sorted_options(d_data.d_options or {}, "o_sort") or {} + -- this value is increased whenever an option gets updated - so that we can have options + -- that don't get an update sorted in after those options that did + d_data.d_tmp_sort_value = 1 + -- mark all existing options as requirilng an update + for i, o_id in ipairs(d_data.d_tmp_sorted_option_list or {}) do + d_data.d_options[o_id].o_tmp_needs_update = true end return d_id end +-- call this *after* all dialog options have been updated for dialog_name +yl_speak_up.update_dialog_options_completed = function(log, dialog, d_id) + local d_data = dialog.n_dialogs[d_id] + if(not(d_data)) then + return + end + for i, o_id in ipairs(d_data.d_tmp_sorted_option_list or {}) do + local o_data = d_data.d_options[o_id] + if(o_data.o_tmp_needs_update) then + -- update the sort value so that this option will be listed *after* those + -- options that actually did get updated + o_data.o_sort = d_data.d_tmp_sort_value + d_data.d_tmp_sort_value = d_data.d_tmp_sort_value + 1 + -- this option has now been processed + o_data.o_tmp_needs_update = nil + + -- name the thing for logging purposes + local log_str = "Dialog "..tostring(d_id) + if(dialog_name and dialog_name ~= d_id) then + log_str = log_str.." ["..tostring(d_id).."]" + end + table.insert(log, log_str..", option <"..tostring(o_id)..">: ".. + "Option exists in old dialog but not in import. Keeping option.") + + -- TODO: this option may need a precondition that sets it to false (if that precondition doesn't already exist) + end + end + -- clean up the dialog + d_data.d_tmp_sorted_option_list = nil + d_data.d_tmp_sort_value = nil +end + + + -- add a new option/answer to dialog d_id with option_text (or default "") -- option_text (optional) the text that shall be shown as option/answer -- target_dialog (optional) the target dialog where the player will end up when choosing @@ -528,9 +582,15 @@ yl_speak_up.update_dialog_option = function(log, dialog, dialog_name, option_nam o_data.o_visit_only_once = nil end -- set sort order of options (no logging because that might get too spammy) - if(sort_oder) then + if(sort_order) then o_data.o_sort = sort_order end + -- this option has been updated + o_data.o_tmp_needs_update = false + if(o_data.o_sort and d_data.d_tmp_sort_value and o_data.o_sort >= d_data.d_tmp_sort_value) then + -- make sure this stores the highest o_sort value we found + d_data.d_tmp_sort_value = o_data.o_sort + 1 + end return o_id end