added limit for amount of option/answers per dialog

This commit is contained in:
Sokomine 2021-05-08 21:06:27 +02:00
parent 63653d4dbd
commit 5db7b9c909
2 changed files with 47 additions and 23 deletions

View File

@ -18,4 +18,7 @@ yl_speak_up.text_new_result_id = "New result"
yl_speak_up.text_version_warning = "You are using an outdated Minetest version!\nI will have a hard time talking to you properly, but I will try my best.\nYou can help me by upgrading to at least 5.3.0!\nGet it at https://minetest.net/downloads"
yl_speak_up.infotext = "Rightclick to talk"
yl_speak_up.max_number_of_buttons = 7
-- how many buttons will be shown simultaneously without having to scroll?
yl_speak_up.max_number_of_buttons = 7
-- how many buttons can be added to one dialog?
yl_speak_up.max_number_of_options_per_dialog = 15

View File

@ -1291,13 +1291,13 @@ local function get_fs_talkdialog(player, n_id, d_id)
h = -0.8
-- allow to delete entries that have no options later on
local has_at_least_one_option = false
local anz_options = 0
-- Let#s sort the options by o_sort
if active_dialog ~= nil and active_dialog.d_options ~= nil then
local sorted_buttons = {}
for _, ad_v in pairs(active_dialog.d_options) do
sorted_buttons[tonumber(ad_v.o_sort)] = ad_v
has_at_least_one_option = true
anz_options = anz_options + 1
end
for _, sb_v in pairs(sorted_buttons) do
@ -1439,15 +1439,24 @@ local function get_fs_talkdialog(player, n_id, d_id)
if(edit_mode) then
-- chat option: "Add a new answer/option to this dialog."
h = h + 1
table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;add_option;]")
table.insert(formspec, "tooltip[add_option;Adds a new option to this dialog. You can delete options via the option edit menu.]")
table.insert(formspec, "label[0.7,"..(h+0.45)..";Add a new option/answer to this dialog. You can delete options via the option edit menu.]")
if(anz_options < yl_speak_up.max_number_of_options_per_dialog) then
table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;add_option;]")
table.insert(formspec, "tooltip[add_option;Adds a new option to this dialog. You can "..
"delete options via the option edit menu.]")
table.insert(formspec, "label[0.7,"..(h+0.45)..";Add a new option/answer to this dialog. "..
"You can delete options via the option edit menu.]")
-- the amount of allowed options/answers has been reached
else
table.insert(formspec, "box[0.5,"..h..";53.8,0.9;#BBBBBB]")
table.insert(formspec, "label[0.7,"..(h+0.45)..";Maximum number of allowed answers/options "..
"reached. No further options/answers can be added.]")
end
-- chat option: "Delete this dialog."
h = h + 1
if(active_dialog
and active_dialog.d_text == ""
and not(has_at_least_one_option)) then
and anz_options == 0) then
table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;delete_this_empty_dialog;]")
table.insert(formspec, "tooltip[delete_this_empty_dialog;Dialogs can only be deleted "..
"when they are empty and have no more options/answers. This is the case here, "..
@ -2151,27 +2160,39 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields)
end
-- add a new option/answer
-- TODO: limit the amount of possible answers to a sensible amount
if(fields[ "add_option"]) then
-- count the options/answers so that the limit is not exceeded
local anz_options = 0
local future_o_id = "o_" .. 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 = {}
else
local sorted_list = yl_speak_up.get_sorted_options(dialog.n_dialogs[d_id].d_options, "o_sort")
anz_options = #sorted_list
end
-- we don't want an infinite amount of answers per dialog
if(anz_options >= yl_speak_up.max_number_of_options_per_dialog) then
minetest.chat_send_player(pname, "Sorry. Only "..
tostring(yl_speak_up.max_number_of_options_per_dialog)..
" options/answers are allowed per dialog.")
fields.add_option = nil
else
dialog.n_dialogs[d_id].d_options[future_o_id] = {
o_id = future_o_id,
o_hide_when_prerequisites_not_met = "false",
o_grey_when_prerequisites_not_met = "false",
o_sort = -1,
o_text_when_prerequisites_not_met = "",
o_text_when_prerequisites_met = "",
}
-- necessary in order for it to work
local s = 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
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Dialog "..d_id..": Added new option/answer "..future_o_id..".")
-- 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
dialog.n_dialogs[d_id].d_options[future_o_id] = {
o_id = future_o_id,
o_hide_when_prerequisites_not_met = "false",
o_grey_when_prerequisites_not_met = "false",
o_sort = -1,
o_text_when_prerequisites_not_met = "",
o_text_when_prerequisites_met = "",
}
-- necessary in order for it to work
local s = 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
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Dialog "..d_id..": Added new option/answer "..future_o_id..".")
-- 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
if(fields[ "del_option"] and fields.o_id and dialog.n_dialogs[d_id].d_options[fields.o_id]) then