From 3ef8732d57d69c476a238e3f43354b4801ef87ff Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 23 Jul 2016 01:14:01 +0100 Subject: [PATCH] Create dialogue mod --- mods/dialogue/LICENSE.txt | 11 +++++++ mods/dialogue/README.txt | 33 ++++++++++++++++++++ mods/dialogue/init.lua | 64 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 mods/dialogue/LICENSE.txt create mode 100644 mods/dialogue/README.txt create mode 100644 mods/dialogue/init.lua diff --git a/mods/dialogue/LICENSE.txt b/mods/dialogue/LICENSE.txt new file mode 100644 index 0000000..1b2b268 --- /dev/null +++ b/mods/dialogue/LICENSE.txt @@ -0,0 +1,11 @@ +License for Code +---------------- + +Copyright (C) 2016 rubenwardy + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +http://www.gnu.org/licenses/lgpl-2.1.html diff --git a/mods/dialogue/README.txt b/mods/dialogue/README.txt new file mode 100644 index 0000000..e086332 --- /dev/null +++ b/mods/dialogue/README.txt @@ -0,0 +1,33 @@ +# Dialogue + +Formspec API to chat with NPCs + +```lua +minetest.register_chatcommand("a", { + func = function(name, param) + local d = dialogue.new("Hi! I'm the blacksmith. How can I help you?") + d:add_option("What do you have for sale?", function(name) + print("ans: sale") + end) + d:add_option("Could you use any help?", function(name) + print("ans: ask for quests") + end) + d:add_option("Nevermind", function(name) + print("ans: nevermind") + end) + d:show(name) + end +}) +``` + +## Methods + +* `dialogue.new(question_text)` - returns dialogue builder (used below as `d` variable) + * `d:add_option(answer_text, callback_func)` - adds option to dialogue builder + * `d:show(player_name)` - show to player name using dialogue.ask + * `callback_func` - called like callback_func(name_of_player_shown_to) +* `dialogue.ask(name, question, answers)` + * Shouldn't be used directly unless you need finer control. + * `name`: player to show formspec to + * `question`: text, what the NPC said + * `answer`: array of objects. `{ { text = "answer", callback = function(name) end } }` diff --git a/mods/dialogue/init.lua b/mods/dialogue/init.lua new file mode 100644 index 0000000..0a68c3e --- /dev/null +++ b/mods/dialogue/init.lua @@ -0,0 +1,64 @@ +dialogue = { + contexts = {} +} + +function dialogue.ask(name, question, answers) + dialogue.contexts[name] = { + answers = answers + } + + -- generate formspec and show + local arrays = { + "size[8,3]", + default.gui_colors, + default.gui_bg, + "label[0,0;", + question + } + + for i = 1, #answers do + local ans = answers[i] + arrays[#arrays + 1] = "]button_exit[0," + arrays[#arrays + 1] = i * 0.75 + arrays[#arrays + 1] = ";8,1;btn_" + arrays[#arrays + 1] = i + arrays[#arrays + 1] = ";" + arrays[#arrays + 1] = ans.text + end + arrays[#arrays + 1] = "]" + + minetest.show_formspec(name, "dialogue:ask", table.concat(arrays, "")) +end + +function dialogue.new(question) + return { + _answers = {}, + add_option = function(self, text, callback) + self._answers[#self._answers + 1] = { + text = text, + callback = callback + } + end, + show = function(self, name) + dialogue.ask(name, question, self._answers) + end + } +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname ~= "dialogue:ask" then + return + end + + local context = dialogue.contexts[player:get_player_name()] + if context then + for i = 1, #context.answers do + if fields["btn_" .. i] then + local ans = context.answers[i] + if ans.callback then + ans.callback(player:get_player_name()) + end + end + end + end +end)