From 26ad9b3c5ae30b30c8a2f570d7643bbd093e6cbe Mon Sep 17 00:00:00 2001 From: Sokomine Date: Tue, 4 May 2021 18:57:53 +0200 Subject: [PATCH] allow to setup the npc without a staff --- functions.lua | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/functions.lua b/functions.lua index 55e8d09..8dcb15c 100644 --- a/functions.lua +++ b/functions.lua @@ -1051,6 +1051,36 @@ local function get_fs_edit_option_dialog(player, n_id, d_id, o_id) end +-- initialize the npc without having to use a staff; +-- returns true when initialization possible +local function get_fs_initial_config(player, n_id, d_id) + local pname = player:get_player_name() + + -- is the player allowed to edit this npc? + if(not(yl_speak_up.npc_owner[ n_id ] == pname + and minetest.check_player_privs(player, {npc_talk_owner=true})) + and not(minetest.check_player_privs(player, {npc_master=true}))) then + return get_error_message() + end + + local formspec = "size[10,4]".. + "label[0.2,0.2;Please provide your new NPC with a name and description!]".. + -- name of the npc + "label[0.2,1.05;Name:]".. + "field[2.0,1.2;4,0.9;n_npc;;"..minetest.formspec_escape(n_id).."]".. + "tooltip[n_npc;n_npc: The name of the NPC;#FFFFFF;#000000]".. + -- description of the npc + "label[0.2,2.05;Description:]".. + "field[2.0,2.2;8,0.9;n_description;;A new NPC without description]".. + "tooltip[n_description;n_description: A description for the NPC;#FFFFFF;#000000]".. + -- save and exit buttons + "button_exit[3.2,3.2;2,0.9;save_initial_config;Save]".. + "button_exit[5.4,3.2;2,0.9;exit;Exit]" + -- show the formspec to the player + return formspec +end + + -- talk local function get_fs_talkdialog(player, n_id, d_id) @@ -1098,6 +1128,7 @@ local function get_fs_talkdialog(player, n_id, d_id) end end else + -- it may be possible that this player can initialize this npc minetest.log( "action", "[MOD] yl_speak_up: User " .. @@ -1105,7 +1136,7 @@ local function get_fs_talkdialog(player, n_id, d_id) " talked to unconfigured NPC with ID n_" .. n_id .. ", position of user was " .. minetest.pos_to_string(player:get_pos(), 0) ) - return get_error_message() + return get_fs_initial_config(player, n_id, d_id) end if c_d_id == nil then return get_error_message() end @@ -2296,6 +2327,44 @@ minetest.register_on_player_receive_fields( local pname = player:get_player_name() local o = "" + local n_id = yl_speak_up.speak_to[pname].n_id + -- the player is trying to save the initial configuration + if(fields.save_initial_config) then + -- is the player allowed to initialize this npc? + if(not(yl_speak_up.npc_owner[ n_id ] == pname + and minetest.check_player_privs(player, {npc_talk_owner=true})) + and not(minetest.check_player_privs(player, {npc_master=true}))) then + return + end + if(not(fields.n_npc) or string.len(fields.n_npc) < 2) then + minetest.chat_send_player(pname, "The name of your NPC needs to be at least two characters long.") + return + end + if(not(fields.n_description) or string.len(fields.n_description) < 2) then + minetest.chat_send_player(pname, "Please provide a description of your NPC!") + return + end + -- give the NPC its first dialog + local f = {} + -- create a new dialog + f.d_id = yl_speak_up.text_new_dialog_id + -- ...with this text + f.d_text = "Hi. I am "..tostring(fields.n_npc)..". I don't know much yet.\n".. + "Hopefully "..tostring(pname).." will teach me to talk soon." + -- it is the first, initial dialog + f.d_sort = "0" + f.n_npc = fields.n_npc + f.n_description = fields.n_description + f.npc_owner = yl_speak_up.npc_owner[ n_id ] + -- create and save the first dialog for this npc + local dialog = fields_to_dialog(pname, f) + save_dialog(n_id, dialog) + yl_speak_up.speak_to[pname].dialog = dialog + -- actually start a chat with our new npc + minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, n_id, nil)) + return + end + -- Is the player working on this particular npc? local edit_mode = (yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id)