diff --git a/fs/fs_initial_config.lua b/fs/fs_initial_config.lua index aa1b2e6..9e10b76 100644 --- a/fs/fs_initial_config.lua +++ b/fs/fs_initial_config.lua @@ -81,17 +81,6 @@ yl_speak_up.input_fs_initial_config = function(player, formname, fields) "\' exists. This NPC got assigned the same name!") end - local d_id = yl_speak_up.speak_to[pname].d_id - local count = 0 - if(dialog and dialog.n_dialogs) then - for k,v in pairs(dialog.n_dialogs) do - if(k and k ~= "d_dynamic") then - count = count + 1 - end - end - end - - -- we checked earlier if the player doing this change and the -- player getting the NPC have appropriate privs if(fields.n_owner ~= yl_speak_up.npc_owner[ n_id ]) then @@ -104,15 +93,21 @@ yl_speak_up.input_fs_initial_config = function(player, formname, fields) end -- give the NPC its first dialog - if(not(dialog) or count==0) then + if(not(dialog) + or not(dialog.created_at) + or not(dialog.n_npc) + or not(dialog.npc_owner)) then -- TODO: pname == yl_speak_up.npc_owner[ n_id ] required -- initialize the NPC with first dialog, name, description and owner: - yl_speak_up.initialize_npc_dialog_once(pname, nil, n_id, fields.n_npc, fields.n_description) + yl_speak_up.initialize_npc_dialog_once(pname, dialog, n_id, fields.n_npc, fields.n_description) + end + -- initializing the dialog in the code above may have changed it + dialog = yl_speak_up.speak_to[pname].dialog -- just change name and description - elseif((fields.n_npc and fields.n_npc ~= "") - or (fields.n_description and fields.n_description ~= "")) then - dialog = yl_speak_up.speak_to[pname].dialog + if((fields.n_npc and fields.n_npc ~= "") + and (fields.n_description and fields.n_description ~= "")) then + -- we checked that these fields contain values; are they diffrent from the existing ones? if(dialog.n_npc ~= fields.n_npc or dialog.n_description ~= fields.n_description) then dialog.n_npc = fields.n_npc @@ -124,12 +119,10 @@ yl_speak_up.input_fs_initial_config = function(player, formname, fields) "NPC name: \""..tostring(fields.n_npc).. "\" Description: \""..tostring(fields.n_description).. "\" May be edited by: \"".. - table.concat(yl_speak_up.sort_keys(dialog.n_may_edit, true), " ").."\".") + table.concat(yl_speak_up.sort_keys(dialog.n_may_edit or {}, true), " ").."\".") end end - dialog = yl_speak_up.speak_to[pname].dialog - -- show nametag etc. if yl_speak_up.speak_to[pname].obj then local obj = yl_speak_up.speak_to[pname].obj @@ -166,16 +159,18 @@ yl_speak_up.input_fs_initial_config = function(player, formname, fields) end end + -- the dialog id may be new due to the dialog having been initialized + local d_id = yl_speak_up.speak_to[pname].d_id if(not(fields.save_initial_config)) then yl_speak_up.show_fs(player, "initial_config", - {n_id = n_id, d_id = yl_speak_up.speak_to[pname].d_id, false}) + {n_id = n_id, d_id = d_id, false}) return end if((fields.add_may_edit and fields.add_may_edit ~= "") or (fields.delete_may_edit and fields.delete_may_edit ~= "")) then -- show this formspec again yl_speak_up.show_fs(player, "initial_config", - {n_id = n_id, d_id = yl_speak_up.speak_to[pname].d_id, false}) + {n_id = n_id, d_id = d_id, false}) else -- actually start a chat with our new npc yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = d_id}) diff --git a/initial_config.lua b/initial_config.lua index b059809..1dee804 100644 --- a/initial_config.lua +++ b/initial_config.lua @@ -38,18 +38,15 @@ end -- example call: -- yl_speak_up.initialize_npc_dialog_once(pname, nil, n_id, fields.n_npc, fields.n_description) yl_speak_up.initialize_npc_dialog_once = function(pname, dialog, n_id, npc_name, npc_description) - local count = 0 - if(dialog and dialog.n_dialogs) then - for k,v in pairs(dialog.n_dialogs) do - if(k and k ~= "d_dynamic") then - count = count + 1 - end - end - end - -- the NPC already has a dialog - do not overwrite it! - if(dialog or count > 0) then - return + if(dialog.created_at) then + return dialog + end + if(yl_speak_up.count_dialogs(dialog) > 0) then + -- add the created_at flag if the dialog is already set up + -- (this affects only NPC created before this time) + dialog.created_at = os.time() + return dialog end -- give the NPC its first dialog @@ -69,10 +66,13 @@ yl_speak_up.initialize_npc_dialog_once = function(pname, dialog, n_id, npc_name, local dialog = yl_speak_up.fields_to_dialog(pname, f) -- overwrite this function if you want something diffrent added: yl_speak_up.setup_initial_dialog(dialog, pname) + dialog.created_at = os.time() -- save our new dialog yl_speak_up.save_dialog(n_id, dialog) - yl_speak_up.speak_to[pname].dialog = dialog dialog.n_may_edit = {} + -- update the dialog for the player + yl_speak_up.speak_to[pname].dialog = dialog + yl_speak_up.speak_to[pname].d_id = yl_speak_up.get_start_dialog_id(dialog) -- now connect the dialogs via results yl_speak_up.log_change(pname, n_id, "Initial config saved. "..