2016-12-01 20:37:00 +01:00
|
|
|
-- NPC dialogue code by Zorman2000
|
2016-11-14 12:28:37 +01:00
|
|
|
|
2016-11-29 20:05:09 +01:00
|
|
|
npc.dialogue = {}
|
|
|
|
|
2016-12-01 15:26:57 +01:00
|
|
|
npc.dialogue.POSITIVE_GIFT_ANSWER_PREFIX = "Yes, give "
|
2016-11-29 20:44:27 +01:00
|
|
|
npc.dialogue.NEGATIVE_ANSWER_LABEL = "Nevermind"
|
|
|
|
|
|
|
|
-- This table contains the answers of dialogue boxes
|
|
|
|
npc.dialogue.dialogue_results = {
|
|
|
|
yes_no_dialogue = {}
|
|
|
|
}
|
2016-11-29 20:05:09 +01:00
|
|
|
|
2016-12-01 20:37:00 +01:00
|
|
|
-- Dialogue box definitions
|
|
|
|
-- The dialogue boxes are used for the player to interact with the
|
|
|
|
-- NPC in dialogues.
|
2016-11-14 12:28:37 +01:00
|
|
|
--------------------------------------------------------------------
|
2016-12-01 20:37:00 +01:00
|
|
|
-- Multi-option dialogue
|
2016-11-14 12:28:37 +01:00
|
|
|
local function create_formspec(options, close_option)
|
|
|
|
local options_length = table.getn(options) + 1
|
|
|
|
local formspec_height = (options_length * 0.7) + 1
|
|
|
|
local formspec = "size[7,"..tostring(formspec_height).."]"
|
|
|
|
for i, opt in ipairs(options) do
|
|
|
|
local y = 0.7;
|
|
|
|
if i > 1 then
|
|
|
|
y = (y * i)
|
|
|
|
end
|
|
|
|
formspec = formspec.."button[0.5,"..y..";6,0.5;opt"..tostring(i)..";"..options[i].opt.."]"
|
|
|
|
end
|
|
|
|
formspec = formspec.."button_exit[0.5,"..(formspec_height - 1)..";6,0.5;exit;"..close_option.."]"
|
|
|
|
return formspec
|
|
|
|
end
|
|
|
|
|
2016-11-29 10:27:07 +01:00
|
|
|
-- New function for getting dialogue formspec
|
2016-11-29 20:44:27 +01:00
|
|
|
function npc.dialogue.show_yes_no_dialogue(prompt,
|
|
|
|
positive_answer_label,
|
2016-11-30 05:53:46 +01:00
|
|
|
positive_callback,
|
2016-11-29 20:44:27 +01:00
|
|
|
negative_answer_label,
|
2016-11-30 05:53:46 +01:00
|
|
|
negative_callback,
|
2016-11-29 20:44:27 +01:00
|
|
|
player_name)
|
2016-11-29 20:05:09 +01:00
|
|
|
|
2016-12-01 15:26:57 +01:00
|
|
|
local formspec = "size[7,3]"..
|
|
|
|
"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.95;6,0.5;no_option;"..negative_answer_label.."]"
|
2016-11-29 20:05:09 +01:00
|
|
|
|
2016-11-29 20:44:27 +01:00
|
|
|
-- Create entry into responses table
|
2016-11-30 05:53:46 +01:00
|
|
|
npc.dialogue.dialogue_results.yes_no_dialogue[1] = {
|
|
|
|
name = player_name,
|
|
|
|
response = "",
|
|
|
|
yes_callback = positive_callback,
|
|
|
|
no_callback = negative_callback
|
|
|
|
}
|
2016-11-29 20:44:27 +01:00
|
|
|
|
|
|
|
minetest.show_formspec(player_name, "advanced_npc:yes_no", formspec)
|
2016-11-29 20:05:09 +01:00
|
|
|
end
|
2016-11-14 12:28:37 +01:00
|
|
|
|
2016-12-01 20:37:00 +01:00
|
|
|
-- Dialogue methods
|
2016-11-14 12:28:37 +01:00
|
|
|
-- Returns all chatlines for a specific NPC
|
2016-12-01 20:37:00 +01:00
|
|
|
function npc.dialogue.select_random_dialogues_for_npc(sex, )
|
2016-11-14 12:28:37 +01:00
|
|
|
local result = {}
|
|
|
|
for i,chatline in ipairs(chat_options) do
|
|
|
|
if chatline.name == npc_name then
|
|
|
|
table.insert(result, chatline)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Functions for rotating NPC to look at player (taken from the API itself)
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local atan = function(x)
|
|
|
|
if x ~= x then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return math.atan(x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function rotate_npc_to_player(self)
|
|
|
|
local s = self.object:getpos()
|
|
|
|
local objs = minetest.get_objects_inside_radius(s, 4)
|
|
|
|
local lp = nil
|
|
|
|
local yaw = 0
|
|
|
|
|
|
|
|
for n = 1, #objs do
|
|
|
|
if objs[n]:is_player() then
|
|
|
|
lp = objs[n]:getpos()
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if lp then
|
|
|
|
local vec = {
|
|
|
|
x = lp.x - s.x,
|
|
|
|
y = lp.y - s.y,
|
|
|
|
z = lp.z - s.z
|
|
|
|
}
|
|
|
|
|
|
|
|
yaw = (atan(vec.z / vec.x) + math.pi / 2) - self.rotate
|
|
|
|
|
|
|
|
if lp.x > s.x then
|
|
|
|
yaw = yaw + math.pi
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.object:setyaw(yaw)
|
|
|
|
end
|
|
|
|
|
|
|
|
---------------------------------------------------------------------
|
|
|
|
-- Drives conversation
|
|
|
|
---------------------------------------------------------------------
|
|
|
|
local function show_chat_option(npc_name, self, player_name, chat_options, close_option)
|
|
|
|
rotate_npc_to_player(self)
|
|
|
|
self.order = "stand"
|
|
|
|
|
|
|
|
local chatline = get_random_chatline(chat_options)
|
|
|
|
minetest.chat_send_player(player_name, chatline.text)
|
|
|
|
if chatline.options ~= nil then
|
|
|
|
minetest.log("Current options: "..dump(chatline.options))
|
|
|
|
local formspec = create_formspec(chatline.options, close_option)
|
|
|
|
minetest.show_formspec(player_name, "rndform", formspec)
|
|
|
|
end
|
|
|
|
|
|
|
|
self.order = "follow"
|
2016-11-26 20:30:24 +01:00
|
|
|
end
|
2016-11-29 20:44:27 +01:00
|
|
|
|
2016-11-30 05:53:46 +01:00
|
|
|
-- 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
|
|
|
|
|
2016-11-29 20:44:27 +01:00
|
|
|
-- Handler for chat formspec
|
|
|
|
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
|
|
|
-- Additional checks for other forms should be handled here
|
2016-11-30 05:53:46 +01:00
|
|
|
if formname == "advanced_npc:yes_no" then
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
|
|
|
|
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))
|
2016-11-29 20:44:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end)
|