yl_speak_up/staff_based_editing.lua
2021-07-25 23:28:17 +02:00

770 lines
30 KiB
Lua

-----------------------------------------------------------------------------
-- these functions are used/needed for staff based editing
-----------------------------------------------------------------------------
-- this was created by AliasAlreadyTaken and the original edit mechanism
local function options_to_dialog(pname)
local dialog = yl_speak_up.speak_to[pname].dialog
local n_id = yl_speak_up.speak_to[pname].n_id
local d_id = yl_speak_up.speak_to[pname].d_id
local o_id = yl_speak_up.speak_to[pname].o_id
local p_id = yl_speak_up.speak_to[pname].p_id
local r_id = yl_speak_up.speak_to[pname].r_id
if yl_speak_up.speak_to[pname].d_text then
dialog.n_dialogs[d_id].d_text = yl_speak_up.speak_to[pname].d_text
end
--Find the o_id to save to
local future_o_id = ""
if yl_speak_up.speak_to[pname].o_id ~= nil and yl_speak_up.speak_to[pname].o_id ~= yl_speak_up.text_new_option_id then
future_o_id = yl_speak_up.speak_to[pname].o_id
else
future_o_id = "o_" .. yl_speak_up.find_next_id(dialog.n_dialogs[d_id].d_options)
if dialog.n_dialogs[d_id].d_options == nil then
dialog.n_dialogs[d_id].d_options = {}
end
dialog.n_dialogs[d_id].d_options[future_o_id] = {}
end
--Find the p_id to save to
local future_p_id = ""
if
yl_speak_up.speak_to[pname].p_id ~= nil and
yl_speak_up.speak_to[pname].p_id ~= yl_speak_up.text_new_prerequisite_id
then
future_p_id = yl_speak_up.speak_to[pname].p_id
else
future_p_id = "p_" .. yl_speak_up.find_next_id(dialog.n_dialogs[d_id].d_options[future_o_id].o_prerequisites)
if future_p_id == "p_1" then
dialog.n_dialogs[d_id].d_options[future_o_id].o_prerequisites = {}
end
dialog.n_dialogs[d_id].d_options[future_o_id].o_prerequisites[future_p_id] = {}
end
--Find the r_id to save to
local future_r_id = ""
if yl_speak_up.speak_to[pname].r_id ~= nil and yl_speak_up.speak_to[pname].r_id ~= yl_speak_up.text_new_result_id then
future_r_id = yl_speak_up.speak_to[pname].r_id
else
future_r_id = yl_speak_up.add_new_result(dialog, d_id, future_o_id)
end
--Edit options
dialog.n_dialogs[d_id].d_options[future_o_id].o_id = future_o_id
dialog.n_dialogs[d_id].d_options[future_o_id].o_hide_when_prerequisites_not_met =
yl_speak_up.speak_to[pname].o_hide or "false"
dialog.n_dialogs[d_id].d_options[future_o_id].o_grey_when_prerequisites_not_met =
yl_speak_up.speak_to[pname].o_grey or "false"
local s = yl_speak_up.sanitize_sort(dialog.n_dialogs[d_id].d_options, yl_speak_up.speak_to[pname].o_sort)
dialog.n_dialogs[d_id].d_options[future_o_id].o_sort = s
dialog.n_dialogs[d_id].d_options[future_o_id].o_text_when_prerequisites_not_met =
yl_speak_up.speak_to[pname].o_not_met or ""
dialog.n_dialogs[d_id].d_options[future_o_id].o_text_when_prerequisites_met =
yl_speak_up.speak_to[pname].o_met or ""
--Edit prerequisites
--Do we delete the prerequisite?
if
yl_speak_up.speak_to[pname].p_type == "delete" and yl_speak_up.speak_to[pname].p_id ~= nil and
yl_speak_up.speak_to[pname].p_id ~= yl_speak_up.text_new_prerequisite_id
then
dialog.n_dialogs[d_id].d_options[future_o_id].o_prerequisites[future_p_id] = nil
else
if yl_speak_up.speak_to[pname].p_value ~= nil and yl_speak_up.speak_to[pname].p_value ~= "" then
dialog.n_dialogs[d_id].d_options[future_o_id].o_prerequisites[future_p_id].p_id = future_p_id
dialog.n_dialogs[d_id].d_options[future_o_id].o_prerequisites[future_p_id].p_type =
yl_speak_up.speak_to[pname].p_type
dialog.n_dialogs[d_id].d_options[future_o_id].o_prerequisites[future_p_id].p_value =
yl_speak_up.speak_to[pname].p_value
end
end
--Do we delete the result?
if
yl_speak_up.speak_to[pname].r_type == "delete" and yl_speak_up.speak_to[pname].r_id ~= nil and
yl_speak_up.speak_to[pname].r_id ~= yl_speak_up.text_new_result_id
then
dialog.n_dialogs[d_id].d_options[future_o_id].o_results[future_r_id] = nil
else
if yl_speak_up.speak_to[pname].r_value ~= nil and yl_speak_up.speak_to[pname].r_value ~= "" then
dialog.n_dialogs[d_id].d_options[future_o_id].o_results[future_r_id].r_id = future_r_id
dialog.n_dialogs[d_id].d_options[future_o_id].o_results[future_r_id].r_type =
yl_speak_up.speak_to[pname].r_type
dialog.n_dialogs[d_id].d_options[future_o_id].o_results[future_r_id].r_value =
yl_speak_up.speak_to[pname].r_value
end
end
return dialog
end
--###
-- Helpers
--###
local function get_prerequisite_types()
--local t_gpt = {"item", "quest", "function", "auto", "delete"}
local t_gpt = {"item", "function", "delete"}
local s_gpt = ""
for _, v in pairs(t_gpt) do
s_gpt = s_gpt .. v .. ","
end
s_gpt = s_gpt:sub(1, -2) -- cut last comma
return s_gpt, t_gpt
end
local function get_result_types()
--local t_grt = {"dialog", "give_item", "quest", "function", "auto", "delete"}
local t_grt = {"dialog","give_item","take_item","move","function","trade","delete"}
local s_grt = ""
for _, v in pairs(t_grt) do
s_grt = s_grt .. v .. ","
end
s_grt = s_grt:sub(1, -2) -- cut last comma
return s_grt, t_grt
end
local function delete_option(n_id, d_id, o_id)
if d_id == yl_speak_up.text_new_dialog_id then
return false
end -- We don't delete "New dialog" Also, something might have gone wrong here.
if o_id == yl_speak_up.text_new_option_id then
return false
end -- We don't delete "New option"
local dialog = yl_speak_up.load_dialog(n_id)
dialog.n_dialogs[d_id].d_options[o_id] = nil
yl_speak_up.save_dialog(n_id, dialog)
end
--###
--Formspecs
--###
-- get formspecs
-- dialog
yl_speak_up.get_fs_setdialog = function(clicker, n_id, d_id)
local dialog = yl_speak_up.load_dialog(n_id)
local items = yl_speak_up.text_new_dialog_id
local count = 1
local d_sort = ""
local text = ""
if next(dialog) == nil then -- file does not exist
dialog.n_id = n_id
dialog.n_npc = ""
dialog.n_description = ""
elseif dialog.n_dialogs == nil then -- file does exist, but no dialogs are set
dialog.n_id = n_id
text = ""
else -- file exists and there's already content
local n = 1
for k, v in pairs(dialog.n_dialogs) do
n = n + 1
if k == d_id then
count = n
d_sort = v.d_sort or ""
text = v.d_text or ""
end
items = items .. "," .. v.d_id
end
text = minetest.formspec_escape(text)
end
local formspec = {
"formspec_version[3]",
"size[13.4,8.5]",
"label[0.2,0.35;",
dialog.n_id,
"]",
"field[1.3,0.1;3.7,0.5;n_npc;;",
dialog.n_npc,
"]",
"field[5.2,0.1;8,0.5;n_description;;",
dialog.n_description,
"]",
"dropdown[5.2,0.75;5,0.75;d_id;",
items,
";",
count,
"]",
-- allow to change the owner of the npc
"label[0.2,1;Owner]",
"field[1.3,0.75;3.7,0.5;npc_owner;;",
(yl_speak_up.npc_owner[ n_id ] or "- nobody -"),
"]",
"label[10.9,1;Sort]",
"field[11.7,0.75;1.5,0.5;d_sort;;",
d_sort,
"]",
"textarea[0.2,1.6;13,6;d_text;;",
text,
"]",
"button_exit[0.2,7.7;3,0.75;button_cancel;Cancel]",
"button[4,7.7;3,0.75;button_delete;Delete]",
"button[7.1,7.7;3,0.75;button_option;Options]",
"button[10.2,7.7;3,0.75;button_save;Save]",
-- tooltips
"tooltip[npc_owner;npc_owner: The name of the owner of the NPC - who can edit dialogs of this NPC if he has the npc_talk_owner priv;#FFFFFF;#000000]",
"tooltip[n_npc;n_npc: The name of the NPC;#FFFFFF;#000000]",
"tooltip[n_description;n_description: A description for the NPC;#FFFFFF;#000000]",
"tooltip[d_sort;d_sort: Make this 0 on your dialog if you want it to be shown the first time a player talks to the NPC\nNegative values are ignored\n;#FFFFFF;#000000]",
"tooltip[5.2,0.75;5,0.75;d_id: Dialog Id;#FFFFFF;#000000]",
"tooltip[d_text;d_text: Dialog text. What the NPC says to you;#FFFFFF;#000000]"
}
return table.concat(formspec, "")
end
-- options
yl_speak_up.get_fs_optiondialog = function(player, n_id, d_id, o_id, p_id, r_id)
local dialog = yl_speak_up.load_dialog(n_id)
local pname = player:get_player_name()
if next(dialog) == nil or d_id == yl_speak_up.text_new_dialog_id or dialog.n_dialogs == nil then -- file does not exist or the user sent the New dialog
return yl_speak_up.get_error_message(n_id)
end
-- default values
local out = {}
-- npc
out.n_id = n_id
-- dialog
out.d_id = yl_speak_up.text_new_dialog_id
out.d_text = "Dialog Text"
-- option
out.o_id_items = yl_speak_up.text_new_option_id
out.o_id_count = 1
out.o_hide = "false"
out.o_grey = "false"
out.o_sort = ""
out.o_met = "Text when conditions are met"
out.o_not_met = "Text when conditions are not met"
-- prerequisite
out.p_id_items = yl_speak_up.text_new_prerequisite_id
out.p_id_count = 1
out.p_type_items, out.p_type_items_table = get_prerequisite_types()
out.p_type_count = 1
out.p_value = ""
-- result
out.r_id_items = yl_speak_up.text_new_result_id
out.r_id_count = 1
out.r_type_items = get_result_types()
out.r_type_count = 1
out.r_value = ""
--dialogs
for k, v in pairs(dialog.n_dialogs) do
if k == d_id then
out.d_id = v.d_id
out.d_text = minetest.formspec_escape(v.d_text):trim()
end
end
--options
if dialog.n_dialogs[d_id].d_options ~= nil then
local o_n = 1
for o_k, o_v in pairs(dialog.n_dialogs[d_id].d_options) do
o_n = o_n + 1
if o_k == o_id then
out.o_id_count = o_n
end
out.o_id_items = out.o_id_items .. "," .. o_v.o_id
end
if dialog.n_dialogs[d_id].d_options[o_id] then
if dialog.n_dialogs[d_id].d_options[o_id].o_hide_when_prerequisites_not_met then
out.o_hide = dialog.n_dialogs[d_id].d_options[o_id].o_hide_when_prerequisites_not_met
end
if dialog.n_dialogs[d_id].d_options[o_id].o_grey_when_prerequisites_not_met then
out.o_grey = dialog.n_dialogs[d_id].d_options[o_id].o_grey_when_prerequisites_not_met
end
if dialog.n_dialogs[d_id].d_options[o_id].o_sort then
out.o_sort = dialog.n_dialogs[d_id].d_options[o_id].o_sort
end
if dialog.n_dialogs[d_id].d_options[o_id].o_text_when_prerequisites_met then
out.o_met =
minetest.formspec_escape(dialog.n_dialogs[d_id].d_options[o_id].o_text_when_prerequisites_met):trim(
)
end
if dialog.n_dialogs[d_id].d_options[o_id].o_text_when_prerequisites_not_met then
out.o_not_met =
minetest.formspec_escape(dialog.n_dialogs[d_id].d_options[o_id].o_text_when_prerequisites_not_met):trim(
)
end
-- prerequisite
--if dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites ~= nil and p_id == yl_speak_up.text_new_prerequisite_id then -- new prerequisite
--end
if dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites ~= nil then
local p_n = 1
for p_k, p_v in pairs(dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites) do
p_n = p_n + 1
if p_k == p_id then
out.p_id_count = p_n
end
out.p_id_items = out.p_id_items .. "," .. p_v.p_id
end
out.p_type_items, out.p_type_items_table = get_prerequisite_types()
if
dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites[p_id] ~= nil and
dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites[p_id].p_type ~= nil
then
local p_type_n = 1
for _, p_type_v in pairs(out.p_type_items_table) do
if p_type_v == dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites[p_id].p_type then
out.p_type_count = p_type_n
end
p_type_n = p_type_n + 1
end
out.p_value =
minetest.formspec_escape(dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites[p_id].p_value):trim(
)
end
end
-- result
if dialog.n_dialogs[d_id].d_options[o_id].o_results ~= nil then
local r_n = 1
for r_k, r_v in pairs(dialog.n_dialogs[d_id].d_options[o_id].o_results) do
r_n = r_n + 1
if r_k == r_id then
out.r_id_count = r_n
end
out.r_id_items = out.r_id_items .. "," .. r_v.r_id
end
out.r_type_items, out.r_type_items_table = get_result_types()
if
dialog.n_dialogs[d_id].d_options[o_id].o_results[r_id] ~= nil and
dialog.n_dialogs[d_id].d_options[o_id].o_results[r_id].r_type ~= nil
then
local r_type_n = 1
for _, r_type_v in pairs(out.r_type_items_table) do
if r_type_v == dialog.n_dialogs[d_id].d_options[o_id].o_results[r_id].r_type then
out.r_type_count = r_type_n
end
r_type_n = r_type_n + 1
end
out.r_value =
minetest.formspec_escape(dialog.n_dialogs[d_id].d_options[o_id].o_results[r_id].r_value):trim()
end
end
end
end
local formspec = {
"formspec_version[3]",
"size[13.4,8.5]",
-- npc
"field[-10,-10;4,0.5;n_id;;",
out.n_id,
"]",
-- dialog
"field[-10,-10;4,0.5;d_id;;",
out.d_id,
"]",
"label[0.2,0.35;",
out.d_id,
"]",
"field[1,0.1;12.2,0.5;d_text;;",
out.d_text,
"]",
-- option
"dropdown[0.2,0.75;2,0.75;o_id;",
out.o_id_items,
";",
out.o_id_count,
"]",
"checkbox[0.2,1.85;o_hide;Hide;",
out.o_hide,
"]",
"checkbox[0.2,2.35;o_grey;Grey;",
out.o_grey,
"]",
"field[1.6,2.05;0.75,0.5;o_sort;Sort;",
out.o_sort,
"]",
"textarea[2.4,0.75;10.8,0.95;o_met;;",
out.o_met,
"]",
"textarea[2.4,1.75;10.8,0.95;o_not_met;;",
out.o_not_met,
"]",
-- prerequisite
"dropdown[0.2,2.8;3,0.75;p_id;",
out.p_id_items,
";",
out.p_id_count,
"]",
"dropdown[0.2,3.8;3,0.75;p_type;",
out.p_type_items,
";",
out.p_type_count,
"]",
"textarea[3.4,2.8;9.8,2.1;p_value;;",
out.p_value,
"]",
-- result
"dropdown[0.2,5;3,0.75;r_id;",
out.r_id_items,
";",
out.r_id_count,
"]",
"dropdown[0.2,6;3,0.75;r_type;",
out.r_type_items,
";",
out.r_type_count,
"]",
"textarea[3.4,5;9.8,2.1;r_value;;",
out.r_value,
"]",
-- buttons
"button[0.2,7.7;3,0.75;button_back;Back]",
"button[7.1,7.7;3,0.75;button_delete;Delete OPTION]",
"button[10.2,7.7;3,0.75;button_save;Save]",
-- tooltips
"tooltip[d_text;d_text: Dialog text. What the NPC says to you\n\n",
out.d_text,
";#FFFFFF;#000000]",
"tooltip[0.2,0.75;2,0.75;o_id: Option Id;#FFFFFF;#000000]",
"tooltip[o_met;o_met: Option text when prerequisites are met\n(What you say to the NPC)\n\n",
out.o_met,
";#FFFFFF;#000000]",
"tooltip[o_not_met;o_not_met: Option text when prerequisites are not met\n(What you say to the NPC instead)\n\n",
out.o_not_met,
";#FFFFFF;#000000]",
"tooltip[o_hide;o_hide: If checked, this option is hidden when prerequistes are not met;#FFFFFF;#000000]",
"tooltip[o_grey;o_grey: If checked, this option is shown when prerequistes are not met, but grey and not selectable;#FFFFFF;#000000]",
"tooltip[o_sort;o_sort: The lower the number, the higher up in the list this option goes\nNegative values are ignored;#FFFFFF;#000000]",
"tooltip[0.2,2.8;3,0.75;p_id: Prerequisite Id;#FFFFFF;#000000]",
"tooltip[0.2,3.8;3,0.75;p_type: Defines what the p_value stands for.\n\n",
"item: requires the user to have this item in the inventory\n",
"quest: requires the user to have completed a quest with this id\n",
"function: executes the given LUA function which must return true or false. true = prerequisite met\n",
"auto: ???\n",
"delete: Deletes the chosen prerequiste when the dialog is saved",
";#FFFFFF;#000000]",
"tooltip[p_value;p_value: This is evaluated to decide whether the prerequisites are met or not;#FFFFFF;#000000]",
"tooltip[0.2,5;3,0.75;r_id: Result Id;#FFFFFF;#000000]",
"tooltip[0.2,6;3,0.75;r_type: Defines what the r_value stands for.\n\n",
"dialog: forwards the user to the given dialog d_id\n",
"item: places this item in the inventory of the user\n",
"quest: starts a quest with this id for the user\n",
"function: executes the given LUA function\n",
"auto: automatically forwards the user to the given dialog d_id after a given time\n",
"delete: Deletes the chosen prerequiste when the dialog is saved",
";#FFFFFF;#000000]",
"tooltip[r_value;r_value: This is what happens when the user chooses this option;#FFFFFF;#000000]"
}
return table.concat(formspec, "")
end
-- receive fields
-- options
yl_speak_up.input_optiondialog = function(player, formname, fields)
if formname ~= "yl_speak_up:optiondialog" then
return
end
if(not(minetest.check_player_privs(player, {npc_master=true}))) then
return
end
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
local d_id = yl_speak_up.speak_to[pname].d_id
local dialog = yl_speak_up.speak_to[pname].dialog
if fields then
-- Button Cancel: Exit the form
if fields.button_back or fields.quit then
local temp_dialog = yl_speak_up.speak_to[pname].dialog
yl_speak_up.speak_to[pname] = {}
yl_speak_up.speak_to[pname].dialog = temp_dialog
yl_speak_up.speak_to[pname].n_id = n_id
yl_speak_up.speak_to[pname].d_id = d_id
yl_speak_up.show_fs(player, "setdialog", {n_id = n_id, d_id = d_id})
return
end
-- Button Delete Option: Delete option dialog, but do not exit
if fields.button_delete and fields.d_id and fields.o_id ~= yl_speak_up.text_new_option_id then
local temp_dialog = yl_speak_up.speak_to[pname].dialog
yl_speak_up.speak_to[pname] = {}
yl_speak_up.speak_to[pname].dialog = temp_dialog
yl_speak_up.speak_to[pname].n_id = n_id
yl_speak_up.speak_to[pname].d_id = d_id
yl_speak_up.speak_to[pname].o_id = yl_speak_up.text_new_option_id
yl_speak_up.speak_to[pname].p_id = yl_speak_up.text_new_prerequisite_id
yl_speak_up.speak_to[pname].r_id = yl_speak_up.text_new_result_id
local o_id = yl_speak_up.text_new_option_id
local p_id = yl_speak_up.text_new_prerequisite_id
local r_id = yl_speak_up.text_new_result_id
delete_option(fields.n_id, fields.d_id, fields.o_id)
yl_speak_up.log_change(pname, n_id,
"(staff) Deleted option "..tostring(fields.o_id).." in dialog "..tostring(fields.d_id)..".")
yl_speak_up.show_fs(player, "optiondialog",
{n_id = n_id, d_id = d_id, o_id = o_id, p_id = p_id, r_id = r_id})
return
end
if fields.d_text then
yl_speak_up.speak_to[pname].d_text = fields.d_text
end
if fields.d_type then
yl_speak_up.speak_to[pname].d_type = fields.d_type
end
-- When a new o_id is chosen, then all the other stuff is invalidated and we need to take values from the dialog or even default values
if fields.o_id ~= nil and fields.o_id ~= yl_speak_up.speak_to[pname].o_id then
local o_id = fields.o_id
yl_speak_up.speak_to[pname].o_id = o_id
yl_speak_up.speak_to[pname].p_id = yl_speak_up.text_new_prerequisite_id
yl_speak_up.speak_to[pname].r_id = yl_speak_up.text_new_result_id
-- Depends on the dialog
if o_id == yl_speak_up.text_new_option_id then --New dialog
yl_speak_up.speak_to[pname].o_hide = "false"
yl_speak_up.speak_to[pname].o_grey = "false"
yl_speak_up.speak_to[pname].o_sort = ""
yl_speak_up.speak_to[pname].o_met = "Text when conditions are not met"
yl_speak_up.speak_to[pname].o_not_met = "Text when conditions are met"
else -- existing dialog
yl_speak_up.speak_to[pname].o_hide =
dialog.n_dialogs[d_id].d_options[o_id].o_hide_when_prerequisites_not_met or "false"
yl_speak_up.speak_to[pname].o_grey =
dialog.n_dialogs[d_id].d_options[o_id].o_grey_when_prerequisites_not_met or "false"
yl_speak_up.speak_to[pname].o_sort = dialog.n_dialogs[d_id].d_options[o_id].o_sort or ""
yl_speak_up.speak_to[pname].o_met =
dialog.n_dialogs[d_id].d_options[o_id].o_text_when_prerequisites_not_met or
"Text when conditions are not met"
yl_speak_up.speak_to[pname].o_not_met =
dialog.n_dialogs[d_id].d_options[o_id].o_text_when_prerequisites_met or
"Text when conditions are met"
end
else
if fields.o_hide then
yl_speak_up.speak_to[pname].o_hide = fields.o_hide
end
if fields.o_grey then
yl_speak_up.speak_to[pname].o_grey = fields.o_grey
end
if fields.o_sort then
yl_speak_up.speak_to[pname].o_sort = fields.o_sort
end
if fields.o_met then
yl_speak_up.speak_to[pname].o_met = fields.o_met
end
if fields.o_not_met then
yl_speak_up.speak_to[pname].o_not_met = fields.o_not_met
end
if fields.p_id ~= nil and fields.p_id ~= yl_speak_up.speak_to[pname].p_id then
yl_speak_up.speak_to[pname].p_id = fields.p_id
yl_speak_up.speak_to[pname].p_value = fields.p_value
yl_speak_up.speak_to[pname].p_type = fields.p_type
end
if fields.p_value then
yl_speak_up.speak_to[pname].p_value = fields.p_value
end
if fields.p_type then
yl_speak_up.speak_to[pname].p_type = fields.p_type
end
if fields.r_id ~= nil and fields.r_id ~= yl_speak_up.speak_to[pname].r_id then
yl_speak_up.speak_to[pname].r_id = fields.r_id
yl_speak_up.speak_to[pname].r_value = fields.r_value
yl_speak_up.speak_to[pname].r_type = fields.r_type
end
if fields.r_value then
yl_speak_up.speak_to[pname].r_value = fields.r_value
end
if fields.r_type then
yl_speak_up.speak_to[pname].r_type = fields.r_type
end
end
end
local o_id = fields.o_id or yl_speak_up.speak_to[pname].o_id
local p_id = fields.p_id or yl_speak_up.speak_to[pname].p_id
local r_id = fields.r_id or yl_speak_up.speak_to[pname].r_id
-- Button Save
if fields and fields.button_save and fields.n_id and fields.d_id then
local temp_dialog = options_to_dialog(pname)
yl_speak_up.save_dialog(n_id, temp_dialog)
-- TODO: a better change detection would be good - because name, description and owner can change here
yl_speak_up.log_change(pname, n_id,
"(staff) Saved dialog "..tostring(fields.d_id)..".")
yl_speak_up.show_fs(player, "optiondialog",
{n_id = fields.n_id, d_id = fields.d_id, o_id = fields.o_id,
p_id = fields.p_id, r_id = fields.r_id})
end
yl_speak_up.show_fs(player, "optiondialog",
{n_id = n_id, d_id = d_id, o_id = o_id, p_id = p_id, r_id = r_id})
end
-- dialog
yl_speak_up.input_setdialog = function(player, formname, fields)
if formname ~= "yl_speak_up:setdialog" then
return
end
if(not(minetest.check_player_privs(player, {npc_master=true}))) then
return
end
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
local d_id = yl_speak_up.speak_to[pname].d_id or yl_speak_up.text_new_dialog_id
-- Button Cancel: Exit the form
if fields.button_cancel or fields.quit then
yl_speak_up.speak_to[pname] = nil
minetest.close_formspec(pname, "yl_speak_up:setdialog")
return
end
-- Button Options with valid d_id: Show options formspec
if fields.button_option and fields.d_id and fields.d_id ~= yl_speak_up.text_new_dialog_id then
local o_id = yl_speak_up.text_new_option_id
local p_id = yl_speak_up.text_new_prerequisite_id
local r_id = yl_speak_up.text_new_result_id
-- Context
yl_speak_up.speak_to[pname].d_id = fields.d_id
yl_speak_up.speak_to[pname].o_id = o_id
yl_speak_up.speak_to[pname].p_id = p_id
yl_speak_up.speak_to[pname].r_id = r_id
yl_speak_up.show_fs(player, "optiondialog",
{n_id = n_id, d_id = fields.d_id, o_id = o_id, p_id = p_id, r_id = r_id})
return
end
-- Button Options with invalid d_id: Show options formspec
if fields.button_option and fields.d_id and fields.d_id == yl_speak_up.text_new_dialog_id then
yl_speak_up.speak_to[pname].d_id = yl_speak_up.text_new_dialog_id
yl_speak_up.show_fs(player, "msg", {input_to = "yl_speak_up:optiondialog",
formspec = yl_speak_up.get_error_message()})
return
end
-- Button Save: Save the settings, but do not exit
if fields.button_save and fields.d_id then
local dialog = yl_speak_up.fields_to_dialog(pname, fields)
yl_speak_up.save_dialog(n_id, dialog)
-- TODO: a better change detection would be great (name, description, owner can be changed as well)
yl_speak_up.log_change(pname, n_id,
"(staff) Saved dialog "..tostring(fields.d_id)..".")
yl_speak_up.speak_to[pname].dialog = dialog
if yl_speak_up.speak_to[pname].obj then
local obj = yl_speak_up.speak_to[pname].obj
local ent = obj:get_luaentity()
if ent ~= nil then
ent.yl_speak_up.npc_name = dialog.n_npc
ent.yl_speak_up.npc_description = dialog.n_description
ent.owner = dialog.npc_owner
local i_text = dialog.n_npc .. "\n" .. dialog.n_description .. "\n" .. yl_speak_up.infotext
obj:set_properties({infotext = i_text})
yl_speak_up.update_nametag(ent)
end
end
yl_speak_up.show_fs(player, "setdialog", {n_id = n_id, d_id = d_id})
return
end
-- Button Delete: Delete dialog, but do not exit
if fields.button_delete and fields.d_id then
yl_speak_up.delete_dialog(n_id, fields.d_id)
yl_speak_up.speak_to[pname].d_id = yl_speak_up.text_new_dialog_id
yl_speak_up.show_fs(player, "setdialog", {n_id = n_id, d_id = yl_speak_up.text_new_dialog_id})
yl_speak_up.log_change(pname, n_id,
"(staff) Deleted dialog "..tostring(fields.d_id)..".")
end
-- Change in Dropdown List: Show dialog formspec again with different dialog selected
if fields.d_id then
yl_speak_up.speak_to[pname].d_id = fields.d_id
yl_speak_up.show_fs(player, "setdialog", {n_id = n_id, d_id = fields.d_id})
end
end
-- Make the NPC talk
function yl_speak_up.config(clicker, npc)
if not clicker and not clicker:is_player() then
return
end
if not npc then
return
end
local npc_id = npc:get_luaentity().yl_speak_up.id
local n_id = "n_" .. npc_id
local pname = clicker:get_player_name()
yl_speak_up.speak_to[pname] = {}
yl_speak_up.speak_to[pname].n_id = n_id -- Memorize which player talks to which NPC
yl_speak_up.speak_to[pname].d_id = yl_speak_up.text_new_dialog_id -- The only d_id we can rely on existing
yl_speak_up.speak_to[pname].dialog = yl_speak_up.load_dialog(n_id) -- Load the dialog and see what we can do with it
yl_speak_up.speak_to[pname].obj = npc
-- find out who owns the npc while we still have easy access to the luaentity
yl_speak_up.npc_owner[ n_id ] = npc:get_luaentity().owner
yl_speak_up.show_fs(clicker, "setdialog", {n_id = n_id, d_id = yl_speak_up.text_new_dialog_id})
end