moved relevant functions into fs_initial_config.lua

This commit is contained in:
Sokomine 2021-06-02 21:43:11 +02:00
parent 3769f05880
commit 30db4209e2
4 changed files with 161 additions and 122 deletions

134
fs_initial_config.lua Normal file
View File

@ -0,0 +1,134 @@
-- set name, description and owner of the NPC
-- (owner can only be set if the player has the npc_talk_master
-- priv - not with npc_talk_owner priv alone)
yl_speak_up.input_fs_initial_config = function(player, formname, fields)
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
-- wait until we get the save dialog
if(not(fields.save_initial_config)) then
-- TODO: show this fs again?
return
end
-- the player is trying to save the initial configuration
-- is the player allowed to initialize this npc?
if(not(yl_speak_up.may_edit_npc(player, n_id))) then
return
end
if(not(fields.n_npc) or string.len(fields.n_npc) < 2) then
-- TODO: show this as a formspec
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
-- sensible length limit
if(string.len(fields.n_npc)>40 or string.len(fields.n_description)>40) then
minetest.chat_send_player(pname, "The name and description of your NPC cannot be longer than 40 characters.")
return
end
local dialog = yl_speak_up.speak_to[pname].dialog
local d_id = yl_speak_up.speak_to[pname].d_id
local count = 0
if(dialog and dialog.n_dialogs) then
for k,v in pairs(dialog.n_dialogs) do
count = count + 1
end
end
-- give the NPC its first dialog
if(not(dialog) or count==0) then
local f = {}
-- create a new dialog
f.d_id = yl_speak_up.text_new_dialog_id
-- ...with this text
f.d_text = "$GOOD_DAY$ $PLAYER_NAME$,\nI am $NPC_NAME$. I don't know much yet.\n"..
"Hopefully $OWNER_NAME$ 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 = yl_speak_up.fields_to_dialog(pname, f)
yl_speak_up.save_dialog(n_id, dialog)
yl_speak_up.speak_to[pname].dialog = dialog
yl_speak_up.log_change(pname, n_id,
"Initial config saved. "..
"NPC name: \""..tostring(fields.n_npc)..
"\" Description: \""..tostring(fields.n_description).."\"")
-- just change name and description
else
dialog = yl_speak_up.speak_to[pname].dialog
dialog.n_npc = fields.n_npc
dialog.n_description = fields.n_description
yl_speak_up.save_dialog(n_id, dialog)
yl_speak_up.log_change(pname, n_id,
"Name and/or description changed. "..
"NPC name: \""..tostring(fields.n_npc)..
"\" Description: \""..tostring(fields.n_description).."\"")
end
dialog = yl_speak_up.speak_to[pname].dialog
-- show nametag etc.
if yl_speak_up.speak_to[pname].obj then
local obj = yl_speak_up.speak_to[pname].obj
local ent = obj:get_luaentity()
if ent ~= nil then
ent.yl_speak_up.npc_name = dialog.n_npc
ent.yl_speak_up.npc_description = dialog.n_description
ent.owner = dialog.npc_owner
local i_text = dialog.n_npc .. "\n" ..
dialog.n_description .. "\n" ..
yl_speak_up.infotext
obj:set_properties({infotext = i_text})
yl_speak_up.update_nametag(ent)
end
end
-- actually start a chat with our new npc
yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = d_id})
end
-- initialize the npc without having to use a staff;
-- returns true when initialization possible
yl_speak_up.get_fs_initial_config = function(player, n_id, d_id, is_initial_config)
local pname = player:get_player_name()
-- is the player allowed to edit this npc?
if(not(yl_speak_up.may_edit_npc(player, n_id))) then
-- TODO: better error message (this one is a local function...)
return get_error_message()
end
local tmp_name = n_id
local tmp_descr = "A new NPC without description"
local tmp_text = "Please provide your new NPC with a name and description!"
-- use existing name and description as presets when just editing
if(not(is_initial_config)) then
local dialog = yl_speak_up.speak_to[pname].dialog
tmp_name = dialog.n_npc
tmp_descr = dialog.n_description
tmp_text = "You can change the name and description of your NPC."
end
local formspec = "size[10,4]"..
"label[0.2,0.2;"..tmp_text.."]"..
-- name of the npc
"label[0.2,1.05;Name:]"..
"field[2.0,1.2;4,0.9;n_npc;;"..minetest.formspec_escape(tmp_name).."]"..
"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;;"..minetest.formspec_escape(tmp_descr).."]"..
"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

View File

@ -176,7 +176,8 @@ local function load_dialog(n_id) -- returns the saved dialog
return dialog
end
local function fields_to_dialog(pname, fields)
-- used by staff and input_inital_config
yl_speak_up.fields_to_dialog = function(pname, fields)
local n_id = yl_speak_up.speak_to[pname].n_id
local dialog = load_dialog(n_id)
local save_d_id = ""
@ -822,45 +823,6 @@ local function get_fs_optiondialog(player, n_id, d_id, o_id, p_id, r_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, is_initial_config)
local pname = player:get_player_name()
-- is the player allowed to edit this npc?
if(not(yl_speak_up.may_edit_npc(player, n_id))) then
return get_error_message()
end
local tmp_name = n_id
local tmp_descr = "A new NPC without description"
local tmp_text = "Please provide your new NPC with a name and description!"
-- use existing name and description as presets when just editing
if(not(is_initial_config)) then
local dialog = yl_speak_up.speak_to[pname].dialog
tmp_name = dialog.n_npc
tmp_descr = dialog.n_description
tmp_text = "You can change the name and description of your NPC."
end
local formspec = "size[10,4]"..
"label[0.2,0.2;"..tmp_text.."]"..
-- name of the npc
"label[0.2,1.05;Name:]"..
"field[2.0,1.2;4,0.9;n_npc;;"..minetest.formspec_escape(tmp_name).."]"..
"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;;"..minetest.formspec_escape(tmp_descr).."]"..
"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
yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id)
@ -921,7 +883,8 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id)
n_id .. ", position of user was " .. minetest.pos_to_string(player:get_pos(), 0)
)
-- this is the initial config
return get_fs_initial_config(player, n_id, d_id, true)
-- (input ends up at yl_speak_up.input_talk and needs to be rerouted)
return yl_speak_up.get_fs_initial_config(player, n_id, d_id, true)
end
if c_d_id == nil then return get_error_message() end
@ -1753,7 +1716,7 @@ yl_speak_up.input_setdialog = function(player, formname, fields)
-- Button Save: Save the settings, but do not exit
if fields.button_save and fields.d_id then
local dialog = fields_to_dialog(pname, fields)
local dialog = yl_speak_up.fields_to_dialog(pname, fields)
yl_speak_up.save_dialog(n_id, dialog)
-- TODO: a better change detection would be great (name, description, owner can be changed as well)
yl_speak_up.log_change(pname, n_id,
@ -2426,85 +2389,12 @@ yl_speak_up.input_talk = function(player, formname, fields)
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.may_edit_npc(player, n_id))) 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
-- sensible length limit
if(string.len(fields.n_npc)>40 or string.len(fields.n_description)>40) then
minetest.chat_send_player(pname, "The name and description of your NPC cannot be longer than 40 characters.")
return
end
local dialog = yl_speak_up.speak_to[pname].dialog
local d_id = yl_speak_up.speak_to[pname].d_id
local count = 0
if(dialog and dialog.n_dialogs) then
for k,v in pairs(dialog.n_dialogs) do
count = count + 1
end
end
-- give the NPC its first dialog
if(not(dialog) or count==0) then
local f = {}
-- create a new dialog
f.d_id = yl_speak_up.text_new_dialog_id
-- ...with this text
f.d_text = "$GOOD_DAY$ $PLAYER_NAME$,\nI am $NPC_NAME$. I don't know much yet.\n"..
"Hopefully $OWNER_NAME$ 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)
yl_speak_up.save_dialog(n_id, dialog)
yl_speak_up.speak_to[pname].dialog = dialog
yl_speak_up.log_change(pname, n_id,
"Initial config saved. "..
"NPC name: \""..tostring(fields.n_npc)..
"\" Description: \""..tostring(fields.n_description).."\"")
-- just change name and description
else
dialog = yl_speak_up.speak_to[pname].dialog
dialog.n_npc = fields.n_npc
dialog.n_description = fields.n_description
yl_speak_up.save_dialog(n_id, dialog)
yl_speak_up.log_change(pname, n_id,
"Name and/or description changed. "..
"NPC name: \""..tostring(fields.n_npc)..
"\" Description: \""..tostring(fields.n_description).."\"")
end
dialog = yl_speak_up.speak_to[pname].dialog
-- show nametag etc.
if yl_speak_up.speak_to[pname].obj then
local obj = yl_speak_up.speak_to[pname].obj
local ent = obj:get_luaentity()
if ent ~= nil then
ent.yl_speak_up.npc_name = dialog.n_npc
ent.yl_speak_up.npc_description = dialog.n_description
ent.owner = dialog.npc_owner
local i_text = dialog.n_npc .. "\n" .. dialog.n_description .. "\n" .. yl_speak_up.infotext
obj:set_properties({infotext = i_text})
yl_speak_up.update_nametag(ent)
end
end
-- actually start a chat with our new npc
yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = d_id})
-- the NPC needs to be configured first; route input to the configuration dialog
if(not(yl_speak_up.speak_to[pname].dialog)
or not(yl_speak_up.speak_to[pname].dialog.n_npc)
or not(yl_speak_up.speak_to[pname].d_id)) then
yl_speak_up.input_fs_initial_config(player, formname, fields)
return
end
@ -2519,8 +2409,8 @@ yl_speak_up.input_talk = function(player, formname, fields)
-- the player wants to change name and description; show the formspec
if(edit_mode and fields.button_edit_name_and_description) then
-- this is not the initial config - but the same formspec can be used
yl_speak_up.show_fs(player, "msg", {input_to = "yl_speak_up:talk", formspec =
get_fs_initial_config(player, n_id, yl_speak_up.speak_to[pname].d_id, false)})
yl_speak_up.show_fs(player, "initial_config",
{n_id = n_id, d_id = yl_speak_up.speak_to[pname].d_id, false})
return
end

View File

@ -21,6 +21,8 @@ dofile(modpath .. "privs.lua")
dofile(modpath .. "show_fs.lua")
-- edit options dialog (detailed configuration of options in edit mode)
dofile(modpath .. "fs_edit_options_dialog.lua")
-- set name, description and owner (owner only with npc_talk_master priv)
dofile(modpath .. "fs_initial_config.lua")
-- inventory management, trading and handling of quest items:
dofile(modpath .. "inventory.lua")
-- trade one item(stack) against one other item(stack)

View File

@ -37,6 +37,10 @@ minetest.register_on_player_receive_fields( function(player, formname, fields)
elseif formname == "yl_speak_up:add_trade_simple" then
yl_speak_up.input_add_trade_simple(player, formname, fields)
return true
-- handled in fs_initial_config.lua
elseif formname == "yl_speak_up:initial_config" then
yl_speak_up.input_fs_initial_config(player, formname, fields)
return true
end
end)
@ -116,6 +120,15 @@ yl_speak_up.show_fs = function(player, fs_name, param)
end
minetest.show_formspec(pname, "yl_speak_up:add_trade_simple",
yl_speak_up.get_fs_add_trade_simple(player, param))
elseif(fs_name == "initial_config") then
if(not(param)) then
param = {}
end
minetest.show_formspec(pname, "yl_speak_up:initial_config",
yl_speak_up.get_fs_initial_config(player, param.n_id, param.d_id,
param.is_initial_config))
-- fallback in case of wrong call
else
minetest.chat_send_player(pname, "Error: Trying to show wrong "..