From d6fd49b1aef33635cde09869114a04890a49e91b Mon Sep 17 00:00:00 2001 From: zorman2000 Date: Tue, 29 Nov 2016 23:53:46 -0500 Subject: [PATCH] Finished yes/no dialogue for dialogues. Implemented using function callbacks for each option (positive or negative answer) Implemented dialogue for confirming that player wants to give a gift to NPC rather than just losing the item --- chat.lua | 50 +++++++++++++++++++++++++++++++++++--------------- npc.lua | 24 ++++++++++++++++-------- 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/chat.lua b/chat.lua index a743295..6b7aeaa 100644 --- a/chat.lua +++ b/chat.lua @@ -57,17 +57,24 @@ end -- New function for getting dialogue formspec function npc.dialogue.show_yes_no_dialogue(prompt, positive_answer_label, + positive_callback, negative_answer_label, + negative_callback, player_name) -- Send prompt message to player minetest.chat_send_player(player_name, prompt) - local formspec = "size[7, 2.4]".. - "button[0.5, 0.7; 6, 0.5; yes_option; "..positive_answer_label.." ]".. - "button_exit[0.5, 1.4; 6, 0.5; no_option; "..negative_answer_label.." ]" + local formspec = "size[7,2.4]".. + "button_exit[0.5,0.65;6,0.5;yes_option;"..positive_answer_label.."]".. + "button_exit[0.5,1.45;6,0.5;no_option;"..negative_answer_label.."]" -- Create entry into responses table - npc.dialogue.dialogue_results[player_name] = nil + npc.dialogue.dialogue_results.yes_no_dialogue[1] = { + name = player_name, + response = "", + yes_callback = positive_callback, + no_callback = negative_callback + } minetest.show_formspec(player_name, "advanced_npc:yes_no", formspec) end @@ -143,22 +150,35 @@ local function show_chat_option(npc_name, self, player_name, chat_options, close self.order = "follow" end +-- Function to get response by player name +local function get_yes_no_dialogue_response_by_player_name(player_name) + for i = 1,#npc.dialogue.dialogue_results.yes_no_dialogue do + local current_result = npc.dialogue.dialogue_results.yes_no_dialogue[i] + if current_result.name == player_name then + return current_result + end + end + return nil +end + -- Handler for chat formspec minetest.register_on_player_receive_fields(function (player, formname, fields) -- Additional checks for other forms should be handled here - if formname ~= "advanced_npc:yes_no" then - return false - end - minetest.log(player:get_player_name().." current response: "..dump(npc.dialogue.dialogue_results[player:get_player_name()])) + if formname == "advanced_npc:yes_no" then + local player_name = player:get_player_name() - if fields then - minetest.log(dump(fields)) - if fields.yes_option then - npc.dialogue.dialogue_results[player:get_player_name()] = true - elseif fields.no_option then - npc.dialogue.dialogue_results[player:get_player_name()] = false + if fields then + local player_response = get_yes_no_dialogue_response_by_player_name(player_name) + if fields.yes_option then + player_response.response = true + player_response.yes_callback() + elseif fields.no_option then + player_response.response = false + player_response.no_callback() + end + minetest.log(player_name.." chose response: " + ..dump(get_yes_no_dialogue_response_by_player_name(player_name).response)) end - minetest.log(player:get_player_name().." after response: "..dump(npc.dialogue.dialogue_results[player:get_player_name()])) end end) diff --git a/npc.lua b/npc.lua index a82d44f..f269d40 100755 --- a/npc.lua +++ b/npc.lua @@ -599,16 +599,24 @@ mobs:register_mob("advanced_npc:npc", { minetest.log(dump(self)) - -- Receive gift or start chat - if self.can_have_relationship then + -- Receive gift or start chat. If player has no item in hand + -- then it is going to start chat directly + if self.can_have_relationship and item:to_table() ~= nil then -- Show dialogue to confirm that player is giving item as gift - npc.dialogue.show_yes_no_dialogue("Do you want to give this item to NPC?", - "Yes, I want to give it!", - npc.dialogue.NEGATIVE_ANSWER_LABEL, - name) - if receive_gift(self, clicker) == false then + npc.dialogue.show_yes_no_dialogue( + "Do you want to give this item to "..self.nametag.."?", + "Yes, I want to give it!", + function() + if receive_gift(self, clicker) == false then + start_chat(self, clicker) + end + end, + npc.dialogue.NEGATIVE_ANSWER_LABEL, + function() start_chat(self, clicker) - end + end, + name + ) else start_chat(self, clicker) end