Occupation: Add ability to set some of the NPCs properties.
Currently, you can set: - Trader status - Whether to show or hide gift items hints NPC: Add enable/disable gift item hints flag Schedules: Allow to set the enable/disable gift item hints flag Dialogues: Add ability to choose from normal dialogues if hint dialogues are disabled.
This commit is contained in:
parent
8e5d6d03f4
commit
6c3988a731
417
dialogue.lua
417
dialogue.lua
@ -11,10 +11,10 @@ npc.dialogue.MIN_DIALOGUES = 2
|
|||||||
npc.dialogue.MAX_DIALOGUES = 4
|
npc.dialogue.MAX_DIALOGUES = 4
|
||||||
|
|
||||||
npc.dialogue.dialogue_type = {
|
npc.dialogue.dialogue_type = {
|
||||||
married = 1,
|
married = 1,
|
||||||
casual_trade = 2,
|
casual_trade = 2,
|
||||||
dedicated_trade = 3,
|
dedicated_trade = 3,
|
||||||
custom_trade = 4
|
custom_trade = 4
|
||||||
}
|
}
|
||||||
|
|
||||||
-- This table contains the answers of dialogue boxes
|
-- This table contains the answers of dialogue boxes
|
||||||
@ -47,8 +47,8 @@ npc.dialogue.tags = {
|
|||||||
-- Occupation-based tags - these are one-to-one with the
|
-- Occupation-based tags - these are one-to-one with the
|
||||||
-- default occupation names
|
-- default occupation names
|
||||||
BASIC = "basic", -- Dialogues related to the basic occupation should
|
BASIC = "basic", -- Dialogues related to the basic occupation should
|
||||||
-- use this. As basic occupation is generic, any occupation
|
-- use this. As basic occupation is generic, any occupation
|
||||||
-- should be able to use these dialogues.
|
-- should be able to use these dialogues.
|
||||||
DEFAULT_FARMER = "default_farmer",
|
DEFAULT_FARMER = "default_farmer",
|
||||||
DEFAULT_COOKER = "default_cooker"
|
DEFAULT_COOKER = "default_cooker"
|
||||||
}
|
}
|
||||||
@ -125,25 +125,25 @@ npc.dialogue.cache = {}
|
|||||||
-- each response that features a function. This is to be able to locate the
|
-- each response that features a function. This is to be able to locate the
|
||||||
-- function easily later
|
-- function easily later
|
||||||
local function set_response_ids_recursively(dialogue, depth, dialogue_id)
|
local function set_response_ids_recursively(dialogue, depth, dialogue_id)
|
||||||
-- Base case: dialogue object with no responses and no responses below it
|
-- Base case: dialogue object with no responses and no responses below it
|
||||||
if dialogue.responses == nil
|
if dialogue.responses == nil
|
||||||
and (dialogue.action_type == "dialogue" and dialogue.action.responses == nil) then
|
and (dialogue.action_type == "dialogue" and dialogue.action.responses == nil) then
|
||||||
return
|
return
|
||||||
elseif dialogue.responses ~= nil then
|
elseif dialogue.responses ~= nil then
|
||||||
-- Assign a response ID to each response
|
-- Assign a response ID to each response
|
||||||
local response_id_prefix = tostring(depth)..":"
|
local response_id_prefix = tostring(depth)..":"
|
||||||
for key,value in ipairs(dialogue.responses) do
|
for key,value in ipairs(dialogue.responses) do
|
||||||
if value.action_type == "function" then
|
if value.action_type == "function" then
|
||||||
value.response_id = response_id_prefix..key
|
value.response_id = response_id_prefix..key
|
||||||
value.dialogue_id = dialogue_id
|
value.dialogue_id = dialogue_id
|
||||||
else
|
else
|
||||||
-- We have a dialogue action type. Need to check if dialogue has further responses
|
-- We have a dialogue action type. Need to check if dialogue has further responses
|
||||||
if value.action.responses ~= nil then
|
if value.action.responses ~= nil then
|
||||||
set_response_ids_recursively(value.action, depth + 1, dialogue_id)
|
set_response_ids_recursively(value.action, depth + 1, dialogue_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- The register dialogue function will just receive the definition as
|
-- The register dialogue function will just receive the definition as
|
||||||
@ -212,9 +212,9 @@ function npc.dialogue.get_cached_dialogue_key(_cache_key, tags)
|
|||||||
if not key then
|
if not key then
|
||||||
-- Search for the dialogue
|
-- Search for the dialogue
|
||||||
local dialogues = npc.dialogue.search_dialogue_by_tags(tags, true)
|
local dialogues = npc.dialogue.search_dialogue_by_tags(tags, true)
|
||||||
key = npc.utils.get_map_keys(dialogues)[1]
|
key = npc.utils.get_map_keys(dialogues)[1]
|
||||||
-- Populate cache
|
-- Populate cache
|
||||||
npc.dialogue.cache[cache_key] = key
|
npc.dialogue.cache[cache_key] = key
|
||||||
-- Return key
|
-- Return key
|
||||||
return key
|
return key
|
||||||
else
|
else
|
||||||
@ -231,11 +231,11 @@ end
|
|||||||
-- 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(self,
|
function npc.dialogue.show_options_dialogue(self,
|
||||||
dialogue_key,
|
dialogue_key,
|
||||||
dialogue,
|
dialogue,
|
||||||
dismiss_option_label,
|
dismiss_option_label,
|
||||||
player_name)
|
player_name)
|
||||||
local responses = dialogue.responses
|
local responses = dialogue.responses
|
||||||
local options_length = table.getn(responses) + 1
|
local options_length = table.getn(responses) + 1
|
||||||
local formspec_height = (options_length * 0.7) + 0.4
|
local formspec_height = (options_length * 0.7) + 0.4
|
||||||
local formspec = "size[7,"..tostring(formspec_height).."]"
|
local formspec = "size[7,"..tostring(formspec_height).."]"
|
||||||
@ -246,10 +246,10 @@ function npc.dialogue.show_options_dialogue(self,
|
|||||||
y = (0.75 * i)
|
y = (0.75 * i)
|
||||||
end
|
end
|
||||||
formspec = formspec.."button_exit[0.5,"
|
formspec = formspec.."button_exit[0.5,"
|
||||||
..(y - 0.5)..";6,0.5;opt"..tostring(i)..";"..responses[i].text.."]"
|
..(y - 0.5)..";6,0.5;opt"..tostring(i)..";"..responses[i].text.."]"
|
||||||
end
|
end
|
||||||
formspec = formspec.."button_exit[0.5,"
|
formspec = formspec.."button_exit[0.5,"
|
||||||
..(formspec_height - 0.7)..";6,0.5;exit;"..dismiss_option_label.."]"
|
..(formspec_height - 0.7)..";6,0.5;exit;"..dismiss_option_label.."]"
|
||||||
|
|
||||||
-- 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] = {
|
||||||
@ -257,9 +257,9 @@ function npc.dialogue.show_options_dialogue(self,
|
|||||||
dialogue = dialogue,
|
dialogue = dialogue,
|
||||||
dialogue_key = dialogue_key,
|
dialogue_key = dialogue_key,
|
||||||
is_married_dialogue =
|
is_married_dialogue =
|
||||||
(dialogue.dialogue_type == npc.dialogue.dialogue_type.married),
|
(dialogue.dialogue_type == npc.dialogue.dialogue_type.married),
|
||||||
is_custom_trade_dialogue =
|
is_custom_trade_dialogue =
|
||||||
(dialogue.dialogue_type == npc.dialogue.dialogue_type.custom_trade),
|
(dialogue.dialogue_type == npc.dialogue.dialogue_type.custom_trade),
|
||||||
casual_trade_type = dialogue.casual_trade_type,
|
casual_trade_type = dialogue.casual_trade_type,
|
||||||
options = responses
|
options = responses
|
||||||
}
|
}
|
||||||
@ -269,23 +269,23 @@ end
|
|||||||
|
|
||||||
-- This function is used for showing a yes/no dialogue formspec
|
-- This function is used for showing a yes/no dialogue formspec
|
||||||
function npc.dialogue.show_yes_no_dialogue(self,
|
function npc.dialogue.show_yes_no_dialogue(self,
|
||||||
prompt,
|
prompt,
|
||||||
positive_answer_label,
|
positive_answer_label,
|
||||||
positive_callback,
|
positive_callback,
|
||||||
negative_answer_label,
|
negative_answer_label,
|
||||||
negative_callback,
|
negative_callback,
|
||||||
player_name)
|
player_name)
|
||||||
|
|
||||||
npc.lock_actions(self)
|
npc.lock_actions(self)
|
||||||
|
|
||||||
local formspec = "size[7,3]"..
|
local formspec = "size[7,3]"..
|
||||||
"label[0.5,0.1;"..prompt.."]"..
|
"label[0.5,0.1;"..prompt.."]"..
|
||||||
"button_exit[0.5,1.15;6,0.5;yes_option;"..positive_answer_label.."]"..
|
"button_exit[0.5,1.15;6,0.5;yes_option;"..positive_answer_label.."]"..
|
||||||
"button_exit[0.5,1.95;6,0.5;no_option;"..negative_answer_label.."]"
|
"button_exit[0.5,1.95;6,0.5;no_option;"..negative_answer_label.."]"
|
||||||
|
|
||||||
-- Create entry into responses table
|
-- Create entry into responses table
|
||||||
npc.dialogue.dialogue_results.yes_no_dialogue[player_name] = {
|
npc.dialogue.dialogue_results.yes_no_dialogue[player_name] = {
|
||||||
npc = self,
|
npc = self,
|
||||||
yes_callback = positive_callback,
|
yes_callback = positive_callback,
|
||||||
no_callback = negative_callback
|
no_callback = negative_callback
|
||||||
}
|
}
|
||||||
@ -359,31 +359,31 @@ end
|
|||||||
-- This function creates a multi-option dialogue from the custom trades that the
|
-- This function creates a multi-option dialogue from the custom trades that the
|
||||||
-- NPC have.
|
-- NPC have.
|
||||||
function npc.dialogue.create_custom_trade_options(self, player)
|
function npc.dialogue.create_custom_trade_options(self, player)
|
||||||
-- Create the action for each option
|
-- Create the action for each option
|
||||||
local actions = {}
|
local actions = {}
|
||||||
for i = 1, #self.trader_data.custom_trades do
|
for i = 1, #self.trader_data.custom_trades do
|
||||||
table.insert(actions,
|
table.insert(actions,
|
||||||
function()
|
function()
|
||||||
npc.trade.show_custom_trade_offer(self, player, self.trader_data.custom_trades[i])
|
npc.trade.show_custom_trade_offer(self, player, self.trader_data.custom_trades[i])
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
-- Default text to be shown for dialogue prompt
|
-- Default text to be shown for dialogue prompt
|
||||||
local text = npc.trade.CUSTOM_TRADES_PROMPT_TEXT
|
local text = npc.trade.CUSTOM_TRADES_PROMPT_TEXT
|
||||||
-- Get the options from each custom trade entry
|
-- Get the options from each custom trade entry
|
||||||
local options = {}
|
local options = {}
|
||||||
if #self.trader_data.custom_trades == 1 then
|
if #self.trader_data.custom_trades == 1 then
|
||||||
table.insert(options, self.trader_data.custom_trades[1].button_prompt)
|
table.insert(options, self.trader_data.custom_trades[1].button_prompt)
|
||||||
text = self.trader_data.custom_trades[1].option_prompt
|
text = self.trader_data.custom_trades[1].option_prompt
|
||||||
else
|
else
|
||||||
for i = 1, #self.trader_data.custom_trades do
|
for i = 1, #self.trader_data.custom_trades do
|
||||||
table.insert(options, self.trader_data.custom_trades[i].button_prompt)
|
table.insert(options, self.trader_data.custom_trades[i].button_prompt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- Create dialogue object
|
-- Create dialogue object
|
||||||
local dialogue = npc.dialogue.create_option_dialogue(text, options, actions)
|
local dialogue = npc.dialogue.create_option_dialogue(text, options, actions)
|
||||||
dialogue.dialogue_type = npc.dialogue.dialogue_type.custom_trade
|
dialogue.dialogue_type = npc.dialogue.dialogue_type.custom_trade
|
||||||
|
|
||||||
return dialogue
|
return dialogue
|
||||||
end
|
end
|
||||||
|
|
||||||
-- This function will choose randomly a dialogue from the NPC data
|
-- This function will choose randomly a dialogue from the NPC data
|
||||||
@ -394,17 +394,17 @@ function npc.dialogue.start_dialogue(self, player, show_married_dialogue)
|
|||||||
|
|
||||||
-- Construct dialogue for marriage
|
-- Construct dialogue for marriage
|
||||||
if npc.relationships.get_relationship_phase(self, player:get_player_name()) == "phase6"
|
if npc.relationships.get_relationship_phase(self, player:get_player_name()) == "phase6"
|
||||||
and show_married_dialogue == true then
|
and show_married_dialogue == true then
|
||||||
dialogue = npc.relationships.MARRIED_NPC_DIALOGUE
|
dialogue = npc.relationships.MARRIED_NPC_DIALOGUE
|
||||||
npc.dialogue.process_dialogue(self, dialogue, player:get_player_name())
|
npc.dialogue.process_dialogue(self, dialogue, player:get_player_name())
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Show options dialogue for dedicated trader
|
-- Show options dialogue for dedicated trader
|
||||||
if self.trader_data.trader_status == npc.trade.TRADER then
|
if self.trader_data.trader_status == npc.trade.TRADER then
|
||||||
dialogue = npc.dialogue.get_cached_dialogue_key(npc.dialogue.cache_keys.DEDICATED_TRADER_DIALOGUE)
|
dialogue = npc.dialogue.get_cached_dialogue_key(npc.dialogue.cache_keys.DEDICATED_TRADER_DIALOGUE)
|
||||||
npc.dialogue.process_dialogue(self, dialogue, player:get_player_name())
|
npc.dialogue.process_dialogue(self, dialogue, player:get_player_name())
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local chance = math.random(1, 100)
|
local chance = math.random(1, 100)
|
||||||
@ -414,92 +414,99 @@ function npc.dialogue.start_dialogue(self, player, show_married_dialogue)
|
|||||||
-- If NPC has custom trading options, these will be
|
-- If NPC has custom trading options, these will be
|
||||||
-- shown as well with equal chance as the casual
|
-- shown as well with equal chance as the casual
|
||||||
-- buy/sell options
|
-- buy/sell options
|
||||||
if self.trader_data.trader_status == npc.trade.NONE then
|
if self.trader_data.trader_status == npc.trade.NONE then
|
||||||
-- Show custom trade options if available
|
-- Show custom trade options if available
|
||||||
if table.getn(self.trader_data.custom_trades) > 0 then
|
if table.getn(self.trader_data.custom_trades) > 0 then
|
||||||
-- Show custom trade options
|
-- Show custom trade options
|
||||||
dialogue = npc.dialogue.create_custom_trade_options(self, player)
|
dialogue = npc.dialogue.create_custom_trade_options(self, player)
|
||||||
else
|
else
|
||||||
-- If not available, choose normal dialogue
|
-- If not available, choose normal dialogue
|
||||||
dialogue = self.dialogues.normal[math.random(1, #self.dialogues.normal)]
|
dialogue = self.dialogues.normal[math.random(1, #self.dialogues.normal)]
|
||||||
end
|
end
|
||||||
elseif self.trader_data.trader_status == npc.trade.CASUAL then
|
elseif self.trader_data.trader_status == npc.trade.CASUAL then
|
||||||
local max_trade_chance = 2
|
local max_trade_chance = 2
|
||||||
if table.getn(self.trader_data.custom_trades) > 0 then
|
if table.getn(self.trader_data.custom_trades) > 0 then
|
||||||
max_trade_chance = 3
|
max_trade_chance = 3
|
||||||
end
|
end
|
||||||
-- Show buy/sell with 50% chance each
|
-- Show buy/sell with 50% chance each
|
||||||
local trade_chance = math.random(1, max_trade_chance)
|
local trade_chance = math.random(1, max_trade_chance)
|
||||||
if trade_chance == 1 then
|
if trade_chance == 1 then
|
||||||
-- Show casual buy dialogue
|
-- Show casual buy dialogue
|
||||||
dialogue = npc.dialogue.get_cached_dialogue_key(npc.dialogue.cache_keys.CASUAL_BUY_DIALOGUE)
|
dialogue = npc.dialogue.get_cached_dialogue_key(npc.dialogue.cache_keys.CASUAL_BUY_DIALOGUE)
|
||||||
elseif trade_chance == 2 then
|
elseif trade_chance == 2 then
|
||||||
-- Show casual sell dialogue
|
-- Show casual sell dialogue
|
||||||
dialogue = npc.dialogue.get_cached_dialogue_key(npc.dialogue.cache_keys.CASUAL_SELL_DIALOGUE)
|
dialogue = npc.dialogue.get_cached_dialogue_key(npc.dialogue.cache_keys.CASUAL_SELL_DIALOGUE)
|
||||||
elseif trade_chance == 3 then
|
elseif trade_chance == 3 then
|
||||||
-- Show custom trade options
|
-- Show custom trade options
|
||||||
dialogue = npc.dialogue.create_custom_trade_options(self, player)
|
dialogue = npc.dialogue.create_custom_trade_options(self, player)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif chance >= 30 and chance < 90 then
|
elseif chance >= 30 and chance < 90 then
|
||||||
-- Choose a random dialogue from the common ones
|
-- Choose a random dialogue from the common ones
|
||||||
dialogue = self.dialogues.normal[math.random(1, #self.dialogues.normal)]
|
dialogue = self.dialogues.normal[math.random(1, #self.dialogues.normal)]
|
||||||
elseif chance >= 90 then
|
elseif chance >= 90 then
|
||||||
-- Choose a random dialogue line from the favorite/disliked item hints
|
-- Check if gift items hints are enabled
|
||||||
dialogue = self.dialogues.hints[math.random(1, 4)]
|
minetest.log("Self gift data enable: "..dump(self.gift_data.enable_gift_items_hints))
|
||||||
|
if self.gift_data.enable_gift_items_hints then
|
||||||
|
-- Choose a random dialogue line from the favorite/disliked item hints
|
||||||
|
dialogue = self.dialogues.hints[math.random(1, 4)]
|
||||||
|
else
|
||||||
|
-- Choose a random dialogue from the common ones
|
||||||
|
dialogue = self.dialogues.normal[math.random(1, #self.dialogues.normal)]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local dialogue_result = npc.dialogue.process_dialogue(self, dialogue, player:get_player_name())
|
local dialogue_result = npc.dialogue.process_dialogue(self, dialogue, player:get_player_name())
|
||||||
if dialogue_result == false then
|
if dialogue_result == false then
|
||||||
-- Try to find another dialogue line
|
-- Try to find another dialogue line
|
||||||
npc.dialogue.start_dialogue(self, player, show_married_dialogue)
|
npc.dialogue.start_dialogue(self, player, show_married_dialogue)
|
||||||
end
|
end
|
||||||
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(self, dialogue, player_name)
|
function npc.dialogue.process_dialogue(self, dialogue, player_name)
|
||||||
-- Freeze NPC actions
|
-- Freeze NPC actions
|
||||||
npc.lock_actions(self)
|
npc.lock_actions(self)
|
||||||
|
|
||||||
local dialogue_key = -1
|
local dialogue_key = -1
|
||||||
|
|
||||||
if type(dialogue) ~= "table" then
|
if type(dialogue) ~= "table" then
|
||||||
dialogue_key = dialogue
|
dialogue_key = dialogue
|
||||||
dialogue = npc.dialogue.registered_dialogues[dialogue]
|
dialogue = npc.dialogue.registered_dialogues[dialogue]
|
||||||
--minetest.log("Found dialogue: "..dump(dialogue))
|
--minetest.log("Found dialogue: "..dump(dialogue))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if this dialogue has a flag definition
|
-- Check if this dialogue has a flag definition
|
||||||
if dialogue.flag then
|
if dialogue.flag then
|
||||||
-- Check if the NPC has this flag
|
-- Check if the NPC has this flag
|
||||||
local flag_value = npc.get_flag(self, dialogue.flag.name)
|
local flag_value = npc.get_flag(self, dialogue.flag.name)
|
||||||
if flag_value ~= nil then
|
if flag_value ~= nil then
|
||||||
-- Check if value of the flag is equal to the expected value
|
-- Check if value of the flag is equal to the expected value
|
||||||
if flag_value ~= dialogue.flag.value then
|
if flag_value ~= dialogue.flag.value then
|
||||||
-- Do not process this dialogue
|
-- Do not process this dialogue
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|
||||||
if (type(dialogue.flag.value) == "boolean" and dialogue.flag.value ~= false)
|
if (type(dialogue.flag.value) == "boolean" and dialogue.flag.value ~= false)
|
||||||
or (type(dialogue.flag.value) == "number" and dialogue.flag.value > 0) then
|
or (type(dialogue.flag.value) == "number" and dialogue.flag.value > 0) then
|
||||||
-- Do not process this dialogue
|
-- Do not process this dialogue
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Send dialogue line
|
-- Send dialogue line
|
||||||
if dialogue.text then
|
if dialogue.text then
|
||||||
npc.chat(self.npc_name, player_name, dialogue.text)
|
npc.chat(self.npc_name, player_name, dialogue.text)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if dialogue has responses. If it doesn't, unlock the actions
|
-- Check if dialogue has responses. If it doesn't, unlock the actions
|
||||||
-- queue and reset actions timer.'
|
-- queue and reset actions timer.'
|
||||||
if not dialogue.responses then
|
if not dialogue.responses then
|
||||||
npc.unlock_actions(self)
|
npc.unlock_actions(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 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
|
||||||
@ -512,18 +519,18 @@ function npc.dialogue.process_dialogue(self, dialogue, player_name)
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Dialogue object processed successfully
|
-- Dialogue object processed successfully
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function npc.dialogue.create_option_dialogue(prompt, options, actions)
|
function npc.dialogue.create_option_dialogue(prompt, options, actions)
|
||||||
local result = {}
|
local result = {}
|
||||||
result.text = prompt
|
result.text = prompt
|
||||||
result.responses = {}
|
result.responses = {}
|
||||||
for i = 1, #options do
|
for i = 1, #options do
|
||||||
table.insert(result.responses, {text = options[i], action_type="function", action=actions[i]})
|
table.insert(result.responses, {text = options[i], action_type="function", action=actions[i]})
|
||||||
end
|
end
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -571,38 +578,38 @@ end
|
|||||||
---------------------------------------------------------------------------------------
|
---------------------------------------------------------------------------------------
|
||||||
-- This function locates a response object that has function on the dialogue tree.
|
-- This function locates a response object that has function on the dialogue tree.
|
||||||
local function get_response_object_by_id_recursive(dialogue, current_depth, response_id)
|
local function get_response_object_by_id_recursive(dialogue, current_depth, response_id)
|
||||||
if dialogue.responses == nil
|
if dialogue.responses == nil
|
||||||
and (dialogue.action_type == "dialogue" and dialoge.action.responses == nil) then
|
and (dialogue.action_type == "dialogue" and dialoge.action.responses == nil) then
|
||||||
return nil
|
return nil
|
||||||
elseif dialogue.responses ~= nil then
|
elseif dialogue.responses ~= nil then
|
||||||
-- Get current depth and response ID
|
-- Get current depth and response ID
|
||||||
local d_i1, d_i2 = string.find(response_id, ":")
|
local d_i1, d_i2 = string.find(response_id, ":")
|
||||||
minetest.log("N1: "..dump(string.sub(response_id, 0, d_i1))..", N2: "..dump(string.sub(response_id, 1, d_i1-1)))
|
--minetest.log("N1: "..dump(string.sub(response_id, 0, d_i1))..", N2: "..dump(string.sub(response_id, 1, d_i1-1)))
|
||||||
local depth = tonumber(string.sub(response_id, 0, d_i1-1))
|
local depth = tonumber(string.sub(response_id, 0, d_i1-1))
|
||||||
local id = tonumber(string.sub(response_id, d_i2 + 1))
|
local id = tonumber(string.sub(response_id, d_i2 + 1))
|
||||||
minetest.log("Depth: "..dump(depth)..", id: "..dump(id))
|
--minetest.log("Depth: "..dump(depth)..", id: "..dump(id))
|
||||||
-- Check each response
|
-- Check each response
|
||||||
for key,value in ipairs(dialogue.responses) do
|
for key,value in ipairs(dialogue.responses) do
|
||||||
minetest.log("Key: "..dump(key)..", value: "..dump(value)..", comp1: "..dump(current_depth == depth))
|
--minetest.log("Key: "..dump(key)..", value: "..dump(value)..", comp1: "..dump(current_depth == depth))
|
||||||
if value.action_type == "function" then
|
if value.action_type == "function" then
|
||||||
-- Check if we are on correct response and correct depth
|
-- Check if we are on correct response and correct depth
|
||||||
if current_depth == depth then
|
if current_depth == depth then
|
||||||
if key == id then
|
if key == id then
|
||||||
return value
|
return value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
minetest.log("Entering again...")
|
--minetest.log("Entering again...")
|
||||||
-- We have a dialogue action type. Need to check if dialogue has further responses
|
-- We have a dialogue action type. Need to check if dialogue has further responses
|
||||||
if value.action.responses ~= nil then
|
if value.action.responses ~= nil then
|
||||||
local response = get_response_object_by_id_recursive(value.action, current_depth + 1, response_id)
|
local response = get_response_object_by_id_recursive(value.action, current_depth + 1, response_id)
|
||||||
if response ~= nil then
|
if response ~= nil then
|
||||||
return response
|
return response
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Handler for dialogue formspec
|
-- Handler for dialogue formspec
|
||||||
@ -615,8 +622,8 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
|
|||||||
if fields then
|
if fields then
|
||||||
local player_response = npc.dialogue.dialogue_results.yes_no_dialogue[player_name]
|
local player_response = npc.dialogue.dialogue_results.yes_no_dialogue[player_name]
|
||||||
|
|
||||||
-- Unlock queue, reset action timer and unfreeze NPC.
|
-- Unlock queue, reset action timer and unfreeze NPC.
|
||||||
npc.unlock_actions(player_response.npc)
|
npc.unlock_actions(player_response.npc)
|
||||||
|
|
||||||
if fields.yes_option then
|
if fields.yes_option then
|
||||||
player_response.yes_callback()
|
player_response.yes_callback()
|
||||||
@ -634,11 +641,11 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
|
|||||||
-- Get player response
|
-- Get player response
|
||||||
local player_response = npc.dialogue.dialogue_results.options_dialogue[player_name]
|
local player_response = npc.dialogue.dialogue_results.options_dialogue[player_name]
|
||||||
|
|
||||||
-- Check if the player hit the negative option or esc button
|
-- Check if the player hit the negative option or esc button
|
||||||
if fields["exit"] or fields["quit"] == "true" then
|
if fields["exit"] or fields["quit"] == "true" then
|
||||||
-- Unlock queue, reset action timer and unfreeze NPC.
|
-- Unlock queue, reset action timer and unfreeze NPC.
|
||||||
npc.unlock_actions(player_response.npc)
|
npc.unlock_actions(player_response.npc)
|
||||||
end
|
end
|
||||||
|
|
||||||
for i = 1, #player_response.options do
|
for i = 1, #player_response.options do
|
||||||
local button_label = "opt"..tostring(i)
|
local button_label = "opt"..tostring(i)
|
||||||
@ -646,34 +653,34 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
|
|||||||
if player_response.options[i].action_type == "dialogue" then
|
if player_response.options[i].action_type == "dialogue" then
|
||||||
-- Process dialogue object
|
-- Process dialogue object
|
||||||
npc.dialogue.process_dialogue(player_response.npc,
|
npc.dialogue.process_dialogue(player_response.npc,
|
||||||
player_response.options[i].action,
|
player_response.options[i].action,
|
||||||
player_name)
|
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
|
-- Execute function - get it directly from definition
|
||||||
-- Find NPC relationship phase with player
|
-- Find NPC relationship phase with player
|
||||||
local phase =
|
local phase =
|
||||||
npc.relationships.get_relationship_phase(player_response.npc, player_name)
|
npc.relationships.get_relationship_phase(player_response.npc, player_name)
|
||||||
-- Check if NPC is married and the married NPC dialogue should be shown
|
-- Check if NPC is married and the married NPC dialogue should be shown
|
||||||
if phase == "phase6" and player_response.is_married_dialogue == true then
|
if phase == "phase6" and player_response.is_married_dialogue == true then
|
||||||
-- Get the function definitions from the married dialogue
|
-- Get the function definitions from the married dialogue
|
||||||
npc.relationships.MARRIED_NPC_DIALOGUE
|
npc.relationships.MARRIED_NPC_DIALOGUE
|
||||||
.responses[player_response.options[i].response_id]
|
.responses[player_response.options[i].response_id]
|
||||||
.action(player_response.npc, player)
|
.action(player_response.npc, player)
|
||||||
elseif player_response.is_custom_trade_dialogue == true then
|
elseif player_response.is_custom_trade_dialogue == true then
|
||||||
-- Functions for a custom trade should be available from the same dialogue
|
-- Functions for a custom trade should be available from the same dialogue
|
||||||
-- object as they are created on demand
|
-- object as they are created on demand
|
||||||
minetest.log("Player response: "..dump(player_response.options[i]))
|
minetest.log("Player response: "..dump(player_response.options[i]))
|
||||||
player_response.options[i].action(player_response.npc, player)
|
player_response.options[i].action(player_response.npc, player)
|
||||||
else
|
else
|
||||||
-- Get dialogue from registered dialogues
|
-- Get dialogue from registered dialogues
|
||||||
local dialogue = npc.dialogue.registered_dialogues[player_response.options[i].dialogue_id]
|
local dialogue = npc.dialogue.registered_dialogues[player_response.options[i].dialogue_id]
|
||||||
local response = get_response_object_by_id_recursive(dialogue, 0, player_response.options[i].response_id)
|
local response = get_response_object_by_id_recursive(dialogue, 0, player_response.options[i].response_id)
|
||||||
|
|
||||||
-- Execute function
|
-- Execute function
|
||||||
response.action(player_response.npc, player)
|
response.action(player_response.npc, player)
|
||||||
|
|
||||||
-- Unlock queue, reset action timer and unfreeze NPC.
|
-- Unlock queue, reset action timer and unfreeze NPC.
|
||||||
npc.unlock_actions(player_response.npc)
|
npc.unlock_actions(player_response.npc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
|
7
npc.lua
7
npc.lua
@ -344,6 +344,8 @@ function npc.initialize(entity, pos, is_lua_entity, npc_stats, occupation_name)
|
|||||||
favorite_items = npc.relationships.select_random_favorite_items(ent.sex, "phase1"),
|
favorite_items = npc.relationships.select_random_favorite_items(ent.sex, "phase1"),
|
||||||
-- Choose disliked items. Choose phase1 per default
|
-- Choose disliked items. Choose phase1 per default
|
||||||
disliked_items = npc.relationships.select_random_disliked_items(ent.sex),
|
disliked_items = npc.relationships.select_random_disliked_items(ent.sex),
|
||||||
|
-- Enable/disable gift item hints dialogue lines
|
||||||
|
enable_gift_items_hints = true
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Flag that determines if NPC can have a relationship
|
-- Flag that determines if NPC can have a relationship
|
||||||
@ -845,7 +847,8 @@ npc.schedule_properties = {
|
|||||||
take_item = "take_item",
|
take_item = "take_item",
|
||||||
trader_status = "trader_status",
|
trader_status = "trader_status",
|
||||||
can_receive_gifts = "can_receive_gifts",
|
can_receive_gifts = "can_receive_gifts",
|
||||||
flag = "flag"
|
flag = "flag",
|
||||||
|
enable_gift_items_hints = "enable_gift_items_hints"
|
||||||
}
|
}
|
||||||
|
|
||||||
local function get_time_in_hours()
|
local function get_time_in_hours()
|
||||||
@ -1012,6 +1015,8 @@ function npc.schedule_change_property(self, property, args)
|
|||||||
self.flags[args.flag_name] = false
|
self.flags[args.flag_name] = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
elseif property == npc.schedule_properties.enable_gift_item_hints then
|
||||||
|
self.gift_data.enable_gift_items_hints = args.value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -30,6 +30,9 @@
|
|||||||
-- occupation that can be used to initialize NPCs. The format is the following:
|
-- occupation that can be used to initialize NPCs. The format is the following:
|
||||||
-- {
|
-- {
|
||||||
-- dialogues = {
|
-- dialogues = {
|
||||||
|
-- enable_gift_item_dialogues = true,
|
||||||
|
-- -- This flag enables/disables gift item dialogues.
|
||||||
|
-- -- If not set, it defaults to true.
|
||||||
-- type = "",
|
-- type = "",
|
||||||
-- -- The type can be "given", "mix" or "tags"
|
-- -- The type can be "given", "mix" or "tags"
|
||||||
-- data = {},
|
-- data = {},
|
||||||
@ -374,6 +377,11 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
|
|||||||
|
|
||||||
-- Initialize dialogues
|
-- Initialize dialogues
|
||||||
if def.dialogues then
|
if def.dialogues then
|
||||||
|
-- Check for gift item dialogues enable
|
||||||
|
if def.dialogues.disable_gift_item_dialogues then
|
||||||
|
self.dialogues.hints = {}
|
||||||
|
end
|
||||||
|
|
||||||
local dialogue_keys = {}
|
local dialogue_keys = {}
|
||||||
-- Check which type of dialogues we have
|
-- Check which type of dialogues we have
|
||||||
if def.dialogues.type == "given" and def.dialogues.keys then
|
if def.dialogues.type == "given" and def.dialogues.keys then
|
||||||
@ -421,11 +429,20 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Initialize trader status
|
-- Initialize properties
|
||||||
if def.initial_trader_status then
|
minetest.log("def.properties: "..dump(def.properties))
|
||||||
self.trader_data.trader_status = def.initial_trader_status
|
if def.properties then
|
||||||
|
-- Initialize trader status
|
||||||
|
if def.properties.initial_trader_status then
|
||||||
|
self.trader_data.trader_status = def.properties.initial_trader_status
|
||||||
|
end
|
||||||
|
-- Enable/disable gift items hints
|
||||||
|
if def.properties.enable_gift_items_hints ~= nil then
|
||||||
|
self.gift_data.enable_gift_items_hints = def.properties.enable_gift_items_hints
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Initialize schedule entries
|
-- Initialize schedule entries
|
||||||
if def.schedules_entries and table.getn(npc.utils.get_map_keys(def.schedules_entries)) > 0 then
|
if def.schedules_entries and table.getn(npc.utils.get_map_keys(def.schedules_entries)) > 0 then
|
||||||
-- Create schedule in NPC
|
-- Create schedule in NPC
|
||||||
|
Loading…
Reference in New Issue
Block a user