Dialogues: added support for custom functions on multi-option dialogues.

Data: dialogues are now Lua tables indexed by numbers
This commit is contained in:
zorman2000 2016-12-04 09:30:25 -05:00
parent e89cd1e18d
commit f462e6af75
2 changed files with 62 additions and 26 deletions

View File

@ -20,7 +20,7 @@ npc.dialogue.dialogue_results = {
-------------------------------------------------------------------- --------------------------------------------------------------------
-- Creates and shows a multi-option dialogue based on the number of responses -- Creates and shows a multi-option dialogue based on the number of responses
-- that the dialogue object contains -- that the dialogue object contains
function npc.dialogue.show_options_dialogue(responses, dismiss_option_label, player_name) function npc.dialogue.show_options_dialogue(self, responses, dismiss_option_label, player_name)
local options_length = table.getn(responses) + 1 local options_length = table.getn(responses) + 1
local formspec_height = (options_length * 0.7) + 0.7 local formspec_height = (options_length * 0.7) + 0.7
local formspec = "size[7,"..tostring(formspec_height).."]" local formspec = "size[7,"..tostring(formspec_height).."]"
@ -36,6 +36,7 @@ function npc.dialogue.show_options_dialogue(responses, dismiss_option_label, pla
-- Create entry on options_dialogue table -- Create entry on options_dialogue table
npc.dialogue.dialogue_results.options_dialogue[player_name] = { npc.dialogue.dialogue_results.options_dialogue[player_name] = {
npc = self,
options = responses options = responses
} }
@ -78,8 +79,28 @@ function npc.dialogue.select_random_dialogues_for_npc(sex, phase)
-- Determine how many dialogue lines the NPC will have -- Determine how many dialogue lines the NPC will have
local number_of_dialogues = math.random(npc.dialogue.MIN_DIALOGUES, npc.dialogue.MAX_DIALOGUES) local number_of_dialogues = math.random(npc.dialogue.MIN_DIALOGUES, npc.dialogue.MAX_DIALOGUES)
minetest.log("Dialogues by string: "..dump(dialogues["1"]))
minetest.log("Dialogues by int: "..dump(dialogues[1]))
minetest.log("Dialogues: "..dump(dialogues))
for i = 1,number_of_dialogues do for i = 1,number_of_dialogues do
result[i] = dialogues[math.random(1, #dialogues)] local dialogue_id = math.random(1, #dialogues)
result[i] = dialogues[dialogue_id]
-- Check if this particular dialogue has responses
if result[i].responses then
-- Check each response to see if they have action_type == "function".
-- This way the indices for this particular response will be stored
-- and the function can be retrieved for execution later.
for key,value in ipairs(result[i].responses) do
if value.action_type == "function" then
result[i].responses[key].dialogue_id = dialogue_id
result[i].responses[key].response_id = key
minetest.log("Storing dialogue and response id: "..dump(result[i].responses[key]))
end
end
end
end end
return result return result
@ -90,12 +111,12 @@ end
function npc.dialogue.start_dialogue(self, player) function npc.dialogue.start_dialogue(self, player)
-- Choose a dialogue randomly -- Choose a dialogue randomly
local dialogue = self.dialogues[math.random(1, #self.dialogues)] local dialogue = self.dialogues[math.random(1, #self.dialogues)]
npc.dialogue.process_dialogue(dialogue, player:get_player_name()) npc.dialogue.process_dialogue(self, dialogue, player:get_player_name())
end end
-- This function processes a dialogue object and performs -- This function processes a dialogue object and performs
-- actions depending on what is defined in the object -- actions depending on what is defined in the object
function npc.dialogue.process_dialogue(dialogue, player_name) function npc.dialogue.process_dialogue(self, dialogue, player_name)
-- Send dialogue line -- Send dialogue line
if dialogue.text then if dialogue.text then
minetest.chat_send_player(player_name, dialogue.text) minetest.chat_send_player(player_name, dialogue.text)
@ -104,6 +125,7 @@ function npc.dialogue.process_dialogue(dialogue, player_name)
-- Check if there are responses, then show multi-option dialogue if there are -- Check if there are responses, then show multi-option dialogue if there are
if dialogue.responses then if dialogue.responses then
npc.dialogue.show_options_dialogue( npc.dialogue.show_options_dialogue(
self,
dialogue.responses, dialogue.responses,
npc.dialogue.NEGATIVE_ANSWER_LABEL, npc.dialogue.NEGATIVE_ANSWER_LABEL,
player_name player_name
@ -185,9 +207,23 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
-- Process dialogue object -- Process dialogue object
npc.dialogue.process_dialogue(player_response.options[i].action, player_name) npc.dialogue.process_dialogue(player_response.options[i].action, player_name)
elseif player_response.options[i].action_type == "function" then elseif player_response.options[i].action_type == "function" then
-- Execute function - get it directly from definition
-- Find NPC relationship phase with player
local phase = nil
for i = 1, #player_response.npc.relationships do
if player_name == player_response.npc.relationships[i].name then
phase = player_response.npc.relationships[i].phase
break
end
end
-- Get dialogues for sex and phase
local dialogues = npc.data.DIALOGUES[player_response.npc.sex][phase]
-- Execute function -- Execute function
-- Bug: for some reason function is null dialogues[player_response.options[i].dialogue_id]
player_response.options[i].action() .responses[player_response.options[i].response_id]
.action(player_response.npc, player_name)
end end
return return
end end

View File

@ -11,40 +11,40 @@ npc.data.DIALOGUES = {
-- Female dialogue options defined by phase -- Female dialogue options defined by phase
-- Phase 1 -- Phase 1
npc.data.DIALOGUES.female["phase1"] = { npc.data.DIALOGUES.female["phase1"] = {
{ [1] = {
text = "Hello there!" text = "Hello there!"
}, },
{ [2] = {
text = "How are you doing?" text = "How are you doing?"
}, },
{ [3] = {
text = "Hey, I haven't seen you before!" text = "Hey, I haven't seen you before!"
}, },
{ [4] = {
text = "Just another day..." text = "Just another day..."
}, },
{ [5] = {
text = "The weather is nice today" text = "The weather is nice today"
}, },
{ [6] = {
text = "Hello! Have you been to the sea?", text = "Hello! Have you been to the sea?",
responses = { responses = {
{ [1] = {
text = "No, never before", text = "No, never before",
action_type = "function", action_type = "function",
action = function(player_name, item) action = function(self, player_name)
minetest.chat_send_player(player_name, "Oh, never? How come! You should.".. minetest.chat_send_player(player_name, "Oh, never? How come! You should."..
"\nHere, take this. It will guide you to the sea...") "\nHere, take this. It will guide you to the sea...")
end end
}, },
{ [2] = {
text = "Yes, sure", text = "Yes, sure",
action_type = "dialogue", action_type = "dialogue",
action = { action = {
text = "It's so beautiful, and big, and large, and infinite, and..." text = "It's so beautiful, and big, and large, and infinite, and..."
} }
}, },
{ [3] = {
text = "Of course! And to all the seas in the world!", text = "Of course! And to all the seas in the world!",
action_type = "dialogue", action_type = "dialogue",
action = { action = {
@ -58,39 +58,39 @@ npc.data.DIALOGUES.female["phase1"] = {
-- Male dialogue options defined by phase -- Male dialogue options defined by phase
-- Phase 1 -- Phase 1
npc.data.DIALOGUES.male["phase1"] = { npc.data.DIALOGUES.male["phase1"] = {
{ [1] = {
text = "Hello!" text = "Hello!"
}, },
{ [2] = {
text = "Welcome to our village, stranger." text = "Welcome to our village, stranger."
}, },
{ [3] = {
text = "Just a great day to go to the woods..." text = "Just a great day to go to the woods..."
}, },
{ [4] = {
text = "Bah, stone! Useless stuff." text = "Bah, stone! Useless stuff."
}, },
{ [5] = {
text = "What do you think of this weather?" text = "What do you think of this weather?"
}, },
{ [6] = {
text = "Hello! Have you been to the sea?", text = "Hello! Have you been to the sea?",
responses = { responses = {
{ [1] = {
text = "No, never before", text = "No, never before",
action_type = "function", action_type = "function",
action = function(player_name, item) action = function(npc, player_name)
minetest.chat_send_player(player_name, "Then you are not worth my time.") minetest.chat_send_player(player_name, "Then you are not worth my time.")
end end
}, },
{ [2] = {
text = "Yes, sure", text = "Yes, sure",
action_type = "dialogue", action_type = "dialogue",
action = { action = {
text = "Then you should appreciate it as a great pirate of the seven seas do!" text = "Then you should appreciate it as a great pirate of the seven seas do!"
} }
}, },
{ [3] = {
text = "Of course! And to all the seas in the world!", text = "Of course! And to all the seas in the world!",
action_type = "dialogue", action_type = "dialogue",
action = { action = {