diff --git a/formspec_helpers.lua b/formspec_helpers.lua index aec8536..8628107 100644 --- a/formspec_helpers.lua +++ b/formspec_helpers.lua @@ -77,8 +77,9 @@ yl_speak_up.input_fs_manage_general = function(player, formname, fields, -- leave this formspec if(fields and (fields.quit or fields.exit or fields.back)) then local last_fs = yl_speak_up.speak_to[pname][ "working_at" ] + local last_params = yl_speak_up.speak_to[pname][ "working_at_params" ] yl_speak_up.speak_to[pname].tmp_index_general = nil - yl_speak_up.show_fs(player, last_fs) + yl_speak_up.show_fs(player, last_fs, last_params) return -- add a new entry? elseif(fields and fields.add_list_entry) then @@ -174,7 +175,8 @@ yl_speak_up.input_fs_manage_general = function(player, formname, fields, return end local last_fs = yl_speak_up.speak_to[pname][ "working_at" ] - yl_speak_up.show_fs(player, last_fs) + local last_params = yl_speak_up.speak_to[pname][ "working_at_params" ] + yl_speak_up.show_fs(player, last_fs, last_params) end diff --git a/fs_assign_quest_step.lua b/fs_assign_quest_step.lua new file mode 100644 index 0000000..ab8c557 --- /dev/null +++ b/fs_assign_quest_step.lua @@ -0,0 +1,125 @@ +-- assign a quest step to a dialog option/answe +-- This is the formspec where this is handled. + +yl_speak_up.input_fs_assign_quest_step = function(player, formname, fields) + if(not(player)) then + return "" + end + local pname = player:get_player_name() + -- what are we talking about? + 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 + + if(not(n_id) or yl_speak_up.edit_mode[pname] ~= n_id) then + return + end + local dialog = yl_speak_up.speak_to[pname].dialog + if(not(dialog) or not(dialog.n_dialogs) + or not(d_id) or not(o_id) + or not(dialog.n_dialogs[d_id]) + or not(dialog.n_dialogs[d_id].d_options) + or not(dialog.n_dialogs[d_id].d_options[o_id])) then + return + end + + -- go back to edit options field + if((fields and fields.quit) + or (fields and fields.back and fields.back ~= "")) then + yl_speak_up.show_fs(player, "edit_option_dialog", + {n_id = n_id, d_id = d_id, o_id = o_id}) + return + elseif(fields and fields.back_from_error_msg) then + yl_speak_up.show_fs(player, "assign_quest_step", nil) + return + -- show manage quests formspec + elseif(fields and fields.manage_quests) then + -- store information so that the back button can work + yl_speak_up.speak_to[pname][ "working_at" ] = "assign_quest_step" + yl_speak_up.show_fs(player, "manage_quests", nil) + return + elseif(not(fields) or not(fields.save)) then + return + end + -- actually store the data + local error_msg = "" + -- check if the quest exists + local quest_list = yl_speak_up.get_sorted_quest_list(pname) + local idx = table.indexof(quest_list, fields.quest_id or "") + if(not(fields.quest_id) or fields.quest_id == "" or idx < 1) then + error_msg = "Quest not found." + elseif(not(fields.quest_step) + or string.len(fields.quest_step) < 1 + or string.len(fields.quest_step) > 80) then + error_msg = "The name of the quest step has to be between\n".. + "1 and 80 characters long." + end + if(error_msg ~= "") then + yl_speak_up.show_fs(player, "msg", { + input_to = "yl_speak_up:assign_quest_step", + formspec = "size[9,2]".. + "label[0.2,0.5;Error: "..minetest.formspec_escape(error_msg).."]".. + "button[1.5,1.5;2,0.9;back_from_error_msg;Back]"}) + return + end + dialog.n_dialogs[d_id].d_options[o_id].quest_id = fields.quest_id + dialog.n_dialogs[d_id].d_options[o_id].quest_step = fields.quest_step + if(not(yl_speak_up.npc_was_changed[ n_id ])) then + yl_speak_up.npc_was_changed[ n_id ] = {} + end + table.insert(yl_speak_up.npc_was_changed[ n_id ], + "Dialog "..d_id..": Option "..tostring(o_id).. + " has been set as quest step \"".. + tostring(fields.quest_step).."\" for quest \""..tostring(fields.quest_id).."\".") + yl_speak_up.show_fs(player, "edit_option_dialog", + {n_id = n_id, d_id = d_id, o_id = o_id}) +end + + +yl_speak_up.get_fs_assign_quest_step = function(player, param) + if(not(player)) 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 o_id = yl_speak_up.speak_to[pname].o_id + -- this only works in edit mode + if(not(n_id) or yl_speak_up.edit_mode[pname] ~= n_id) then + return "size[1,1]label[0,0;You cannot edit this NPC.]" + end + local dialog = yl_speak_up.speak_to[pname].dialog + if(not(dialog) or not(dialog.n_dialogs) + or not(d_id) or not(o_id) + or not(dialog.n_dialogs[d_id]) + or not(dialog.n_dialogs[d_id].d_options) + or not(dialog.n_dialogs[d_id].d_options[o_id])) then + return "size[4,1]label[0,0;Dialog option does not exist.]" + end + + local d_option = dialog.n_dialogs[d_id].d_options[o_id] + local quest_id = d_option.quest_id or "" + local quest_step = d_option.quest_step or "" + + local selected = 1 + local quest_list = yl_speak_up.get_sorted_quest_list(pname) + for i, v in ipairs(quest_list) do + quest_list[i] = minetest.formspec_escape(v) + if(quest_id and v == quest_id) then + selected = i + end + end + return "size[14,4]".. + "label[4.0,0.5;Using this option/answer shall be a quest step.]".. + "label[0.2,1.4;Select a quest:]".. + "dropdown[4.0,1.0;9.5,0.8;quest_id;".. + table.concat(quest_list, ',')..";".. + tostring(selected)..",]".. + "label[0.2,2.4;Name of the quest step:]".. + "field[4.0,2.0;9.5,0.8;quest_step;;".. + minetest.formspec_escape(quest_step).."]".. + "button[0.2,3.0;3.6,0.8;manage_quests;Manage Quests]".. + "button[6.5,3.0;2.0,0.8;save;Save]".. + "button[9.0,3.0;2.0,0.8;back;Abort]".. + "button[11.5,3.0;2.0,0.8;back;Back]" +end diff --git a/fs_edit_options_dialog.lua b/fs_edit_options_dialog.lua index 8e7718e..c74001c 100644 --- a/fs_edit_options_dialog.lua +++ b/fs_edit_options_dialog.lua @@ -64,6 +64,12 @@ yl_speak_up.input_edit_option_dialog = function(player, formname, fields) return end + if(fields.assign_quest_step and fields.assign_quest_step ~= "") then + yl_speak_up.show_fs(player, "assign_quest_step", + {n_id = n_id, d_id = d_id, o_id = o_id}) + return + end + if(fields.switch_tab and fields.switch_tab == "2") then yl_speak_up.show_fs(player, "edit_option_dialog", {n_id = n_id, d_id = d_id, o_id = o_id, @@ -128,7 +134,7 @@ yl_speak_up.input_edit_option_dialog = function(player, formname, fields) else yl_speak_up.show_fs(player, "edit_option_dialog", {n_id = n_id, d_id = d_id, o_id = o_id, - caller="back-from_edit_dialog_modifications"}) + caller="back_from_edit_dialog_modifications"}) return end @@ -218,6 +224,23 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle end local d_option = n_dialog.d_options[o_id] + -- if it is a quest step, then show that; else allow creating a quest step + local quest_step_text = "button[15.4,0.1;6.0,0.9;assign_quest_step;Turn this into a quest step]" + if( d_option.quest_id and d_option.quest_id ~= "" + and d_option.quest_step and d_option.quest_step ~= "") then + quest_step_text = "box[4.9,0.0;14.0,1.1;#BB77BB]".. + "label[0.2,0.3;This is quest step:]".. + "label[5.0,0.3;".. + minetest.colorize("#00FFFF", + minetest.formspec_escape(d_option.quest_step)).."]".. + "label[0.2,0.8;of the quest:]".. + "label[5.0,0.8;".. + minetest.colorize("#CCCCFF", + minetest.formspec_escape(d_option.quest_id)).."]".. + "button[19.4,0.1;2.0,0.9;assign_quest_step;Change]" + end + + -- offer the correct preselection for hidden/grey/show text local alternate_answer_option = "3" if(d_option.o_hide_when_prerequisites_not_met == "true") then @@ -237,7 +260,7 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle local answer_text = -- answer of the player (the actual option) - "container[0.0,7.3]".. + "container[0.0,8.3]".. "label[0.2,0.0;..the player may answer with this text".. minetest.formspec_escape(" [dialog option \""..tostring(o_id).."\"]:").."]".. "dropdown[16.0,-0.4;5.3,0.7;option_autoanswer;".. @@ -247,7 +270,7 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle answer_mode = 1 d_option.o_autoanswer = 1 answer_text = - "container[0.0,7.3]".. + "container[0.0,8.3]".. "label[0.2,0.0;..this option will be selected automaticly.]" end if(answer_mode == 0 and (d_id ~= "d_got_item" and d_id ~= "d_trade")) then @@ -359,7 +382,7 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle -- list of (A)ctions (there can only be one per option; i.e. a trade) local action_list_text = - "container[0.0,11.0]".. + "container[0.0,12.0]".. "label[0.2,0.0;When this answer has been selected, start the following (A)ction:]".. "tablecolumns[text;color,span=1;text;text]" if(answer_mode == 1) then @@ -472,7 +495,7 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle end -- make all following coordinates relative - local action_text = "container[0.2,13.0]".. + local action_text = "container[0.2,14.0]".. "box[0.25,0.0;21.0,6.7;#555555]" local tab_list = "tabheader[0.2,0.0;switch_tab;".. "If the action was successful:,".. @@ -662,7 +685,7 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle -- build up the formspec local formspec = "".. - "size[22,21]".. + "size[22,22]".. "bgcolor[#00000000;false]".. -- button back to the current dialog (of which this is an option) "button[16.4,0.2;5.0,0.9;show_current_dialog;Back to dialog ".. @@ -687,8 +710,12 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle "tooltip[1.2,0.3;20.2,3.0;This is what the NPC says to the player.]".. "container_end[]".. + "container[0.0,3.9]".. + quest_step_text.. + "container_end[]".. + -- list the preconditions - "container[0.0,4.4]".. + "container[0.0,5.4]".. "label[0.2,0.0;If all of the following pre(C)onditions are fulfilled:]".. "tablecolumns[text;color,span=1;text;text]".. "table[1.2,0.3;20.2,2.0;table_of_preconditions;".. @@ -711,7 +738,7 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle action_text.. -- container for the buttons/footer - "container[0.0,19.9]".. + "container[0.0,20.9]".. -- button: delete "button[0.2,0.0;2.0,0.9;del_option;Delete]".. "tooltip[del_option;Delete this option/answer.]".. diff --git a/init.lua b/init.lua index 580d644..f1fb8ef 100644 --- a/init.lua +++ b/init.lua @@ -166,6 +166,9 @@ yl_speak_up.reload = function(modpath, log_entry) -- file "local_server_do_on_reload.lua"! dofile(modpath .. "custom_functions_you_can_override.lua") + -- assign a quest step to a dialog option/answer + dofile(modpath .. "fs_assign_quest_step.lua") + -- execute preconditions, actions and effects dofile(modpath .. "exec_eval_preconditions.lua") dofile(modpath .. "exec_actions.lua") diff --git a/show_fs.lua b/show_fs.lua index f1d11f9..2d5f531 100644 --- a/show_fs.lua +++ b/show_fs.lua @@ -70,6 +70,10 @@ yl_speak_up.input_handler = function(player, formname, fields) elseif formname == "yl_speak_up:initial_config" then yl_speak_up.input_fs_initial_config(player, formname, fields) return true + -- handled in fs_assign_quest_step.lua + elseif formname == "yl_speak_up:assign_quest_step" then + yl_speak_up.input_fs_assign_quest_step(player, formname, fields) + return true -- handled in fs_edit_preconditions.lua elseif formname == "yl_speak_up:edit_preconditions" then yl_speak_up.input_fs_edit_preconditions(player, formname, fields) @@ -365,6 +369,10 @@ yl_speak_up.show_fs = function(player, fs_name, param) yl_speak_up.get_fs_initial_config(player, param.n_id, param.d_id, param.is_initial_config)) + elseif(fs_name == "assign_quest_step") then + yl_speak_up.show_fs_ver(pname, "yl_speak_up:assign_quest_step", + yl_speak_up.get_fs_assign_quest_step(player, param)) + elseif(fs_name == "edit_preconditions") then yl_speak_up.show_fs_ver(pname, "yl_speak_up:edit_preconditions", yl_speak_up.get_fs_edit_preconditions(player, param))